CodePlexProject Hosting for Open Source Software

```
using ILCalc;
```

var calc = new CalcContext<double>("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: var 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 usage looks exactly the same way, as

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

Argument range may be represented with

var 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<double>(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" }

Last edited Sep 9, 2009 at 11:48 AM by Pelmen64, version 1