CodePlexProject Hosting for Open Source Software

```
using ILCalc;
```

var calc = new CalcContext("x");

For example, you may add another argument named "y":

```
calc.Arguments.Add("y");
```

// built-in Pi, E, Inf and NaN constants: calc.Constants.ImportBuiltIn( ); // user constants: calc.Constants.Add("lambda0", 1.234); calc.Constants.Add("epsilon", double.Epsilon);

// standard System.Math methods: calc.Functions.ImportBuiltIn( ); // user methods as delegates: calc.Functions.Add("Simple", Program.SimpleMethod); // lambda expressions: double val = 1.23; calc.Functions.Add("Magic", () => val); // even with closure! calc.Functions.Add("Triple", x => 3 * x); // all suitable methods from whole class: calc.Functions.Import(typeof(MyFunctionsClass)); // public method reflections: calc.Functions.AddStatic("Some1", typeof(SomeClass).GetMethod("SomeStaticMethod")); // even instance methods with targets: var c = new SomeClass(); calc.Functions.AddInstance("Some2", typeof(SomeClass).GetMethod("SomeInstanceMethod"), c);

Look for documentation for more detailed information about this methods.

Now it's time to set expression parse culture.

It's affects on parsing the decimal separator and arguments list separator symbols and number literals parsing.

Culture property can be set to

calc.Culture = CultureInfo.InvariantCulture;

We can disable the ignore-case mode for the identifiers names, that is used by default:

```
calc.IgnoreCase = false;
```

```
calc.OverflowCheck = false;
```

string expression = "4x + 2sin(pi/6) - lambda0";

double result = calc.Evaluate(expression, 1.0); // = 1.0

We should specify expression string and another one parameter, that would be threated as argument "х" value.

Specified parameters count (except expression) should be exactly the same, as the context's Arguments.Count, otherwise we will get an exception here.

Value of arguments should be provided to method in the same order as their names presented in ArgumentCollection class. For example:

calc.Arguments.AddRange("X", "Z", "Lambda"); calc.Evaluate(expression, 1, 2, 3); // expression will get: X = 1, Z = 2, Lambda = 3

// create object: Interpret interpret = calc.CreateInterpret(expression); // and separately invokes the evaluation: double result = interpret.Evaluate(4.0); // = 13.0

Optimizer options may be specified by context's

calc.Optimization = OptimizeModes.ConstantFolding | OptimizeModes.PowOptimize;

Creation and esuage looks exactly the same way, as

```
Evaluator eval = calc.CreateEvaluator(expression);
res = eval.Evaluate(3.0); // = 9.0
```

Argument range may be represented with

Tabulator tab = calc.CreateTabulator(expression); // use it by specify begin, end, step values: double[] table = tab.Tabulate(1.0, 5.0, 0.25); // = { 1.0, 2.0, 3.0 ... } // or via TabRange class: var range = new TabRange(1.0, 5.0, 1.0); range.Count = 100; // set points count (recalculate step) table = tab.Tabulate(range); // = { 1.0, 1.16, 1.32, 1.48 ... }

try { calc.Validate("2 / xyz"); // unresolved identifier "xyz" here } catch (SyntaxException err) { // TODO: Show error message // we can get some error information here: // err.Position = 4 // err.Length = 3 // err.Substring = "xyz" // err.Expression = "2 / xyz" }

#light open ILCalc let c = new CalcContext() c.Arguments.Add("x") c.Constants.ImportBuiltIn() c.Functions.ImportBuiltIn() c.Functions.Add("dbl", fun x -> 2.0 * x) c.Optimization <- OptimizeModes.ConstantFolding ||| OptimizeModes.PowOptimize let eval = c.CreateEvaluator "2sin(x) + dbl(x)" c.Functions.Add("func", eval.Evaluate1) let eval2 = c.CreateEvaluator "func(x) - 2sin(x)" let result = eval2.Evaluate 3.14 printfn "result = %f" result

Last edited Jul 28, 2009 at 12:10 PM by Pelmen64, version 1