11,812,712 members (72,289 online)

# Evaluating Mathematical Expressions by Compiling C# Code at Runtime

, 21 Apr 2003 183.8K 80
 Rate this:
A completely new way of making a mathematical expression evaluator.

## Introduction

There are many articles on creating mathematical expression parsers (i.e. for displaying 3D functions where user inputs the formulas manually). Most of them are based on lexical analysis, grammar definitions etc. With C# there is a solution for making mathematical expression evaluator in a completely new way: by compiling code at runtime.

Let's assume that we want to create an application that would calculate 3D function like that:

z = f(x,y)

But we want to let the user input function formula manually (we don't know what the formula will look like).

A solution to deal with that task in 10 minutes is:

1. Let the user input the function
2. Compile that function into memory
3. Run the function with given x and y
4. Do something with result z value

To do that in a couple of minutes we would need a class with virtual method:

```namespace MathEval
{
public class MyClassBase
{
public MyClassBase()
{
}
public virtual double eval(double x,double y)
{
return 0.0;
}
}
}
```

all we need more is a parser/evaluator class which would compile function body into assembly (created in memory) and calculate function result:

```using System;
using System.Reflection;
using System.Windows.Forms;
namespace MathEval
{
public class MathExpressionParser
{
MyClassBase myobj = null;
public MathExpressionParser()
{
}
public bool init(string expr)
{
Microsoft.CSharp.CSharpCodeProvider cp
= new Microsoft.CSharp.CSharpCodeProvider();
System.CodeDom.Compiler.ICodeCompiler ic = cp.CreateCompiler();
System.CodeDom.Compiler.CompilerParameters cpar
= new System.CodeDom.Compiler.CompilerParameters();
cpar.GenerateInMemory = true;
cpar.GenerateExecutable = false;
string src = "using System;"+
"class myclass:MathEval.MyClassBase" +
"{"+
"public myclass(){}"+
"public override double eval(double x,double y)"+
"{"+
"return "+ expr +";"+
"}"+
"}";
System.CodeDom.Compiler.CompilerResults cr
= ic.CompileAssemblyFromSource(cpar,src);
foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)
MessageBox.Show(ce.ErrorText);

if (cr.Errors.Count == 0 && cr.CompiledAssembly != null)
{
Type ObjType = cr.CompiledAssembly.GetType("myclass");
try
{
if (ObjType != null)
{
myobj = (MyClassBase)Activator.CreateInstance(ObjType);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return true;
}
else
return false;
}
public double eval(double x,double y)
{
double val = 0.0;
if (myobj != null)
{
val = myobj.eval(x,y);
}
return val;
}
}
}
```

Take a look at the init() method. All we need it to do is to create an assembly(in memory) with a class "myclass" derived from MyBaseClass. Note that "eval" method contains the formula given by the user at runtime. Also note that cpar.ReferencedAssemblies.Add("matheval.exe"); defines a reference to the application currently being created.
Then we can call myclass.eval method as many times as we want to:

```MathExpressionParser mp = new MathExpressionParser();
mp.init("Math.Sin(x)*y");

for(int i=0;i<1000000;i++)
{
mp.eval((double)i,(double)i);
}
```

Compiled code is of course much faster than any other solution used to parse and evaluate expressions.

A few things to do:

• Let the user input formulas as "xy*sin(x+y)" or "x^2+y^2" and convert them to c# "x*y*Math.Sin(x+y)" "x*x + y*y" etc...
• Do some error checking/handling other than compiler errors.
• Freeing and recreating the in-memory-assembly with another formula.
• Enjoy the code

PS: special thanks to AcidRain for some ideas

A list of licenses authors might use can be found here

## Share

 Architect Poland
No Biography provided

## You may also be interested in...

 First Prev Next
 Compile function in memory in objective-c for mac wonkypuh1018-Jun-11 12:17 wonkypuh10 18-Jun-11 12:17
 Completely New? Dewey21-Jan-08 14:28 Dewey 21-Jan-08 14:28
 Re: Completely New? Marcin Cuprjak (aka Vlad Tepes)23-Jan-08 9:33 Marcin Cuprjak (aka Vlad Tepes) 23-Jan-08 9:33
 What is wrong with double FuncN (params double[] x) ? hreba22-Nov-06 3:40 hreba 22-Nov-06 3:40
 Good Work! gajatko3-Oct-06 2:17 gajatko 3-Oct-06 2:17
 Not applicable for ASP.NET2 applications ZuZuKin23-Feb-06 9:10 ZuZuKin 23-Feb-06 9:10
 Very Nice msaroka20-Apr-05 12:48 msaroka 20-Apr-05 12:48
 This is excelent. It has a lot of uses, more then just math.
 "matheval.exe" xorenko17-Apr-05 0:11 xorenko 17-Apr-05 0:11
 Extremely insecure! ekolis14-Jun-04 15:59 ekolis 14-Jun-04 15:59
 How to unload an assembly? Heng Ma30-Jul-03 10:48 Heng Ma 30-Jul-03 10:48
 Re: How to unload an assembly? tuxic7-Mar-05 9:01 tuxic 7-Mar-05 9:01
 What about Microsoft.JScript.Eval()? onc29-Apr-03 3:59 onc 29-Apr-03 3:59
 Generate IL directly hsauro22-Apr-03 19:48 hsauro 22-Apr-03 19:48
 Re: Generate IL directly `leppie`23-Apr-03 7:02 `leppie` 23-Apr-03 7:02
 Re: Generate IL directly Kris Vandermotten29-Apr-03 4:25 Kris Vandermotten 29-Apr-03 4:25
 Compiling In Memory Blake Coverett22-Apr-03 16:26 Blake Coverett 22-Apr-03 16:26
 Re: Compiling In Memory Jonathan de Halleux22-Apr-03 23:02 Jonathan de Halleux 22-Apr-03 23:02
 Re: Compiling In Memory Rama Krishna23-Apr-03 3:12 Rama Krishna 23-Apr-03 3:12
 Re: Compiling In Memory `leppie`23-Apr-03 7:00 `leppie` 23-Apr-03 7:00
 Re: Compiling In Memory Rama Krishna23-Apr-03 7:06 Rama Krishna 23-Apr-03 7:06
 Re: Compiling In Memory Blake Coverett23-Apr-03 16:48 Blake Coverett 23-Apr-03 16:48
 Re: Compiling In Memory Rama Krishna24-Apr-03 1:46 Rama Krishna 24-Apr-03 1:46
 Re: Compiling In Memory Blake Coverett24-Apr-03 13:41 Blake Coverett 24-Apr-03 13:41
 Last Visit: 31-Dec-99 18:00     Last Update: 12-Oct-15 22:28 Refresh 1