Support for parameter initialization in code

Sep 18, 2009 at 12:38 PM

Is it possible to initialize parameters within the expression string itself, something like:

"set(a, 10), cos(a)"

or something like that.

Many thanks

Lee

 

 

Coordinator
Sep 18, 2009 at 1:50 PM

Going into this sort of script functionnality is opening some complexity. Actually this could be another project, like lighweight scripting engine. I don't say it is not useful, because it is really useful for some domains, but the goal on NCalc is not this one. It would make NCalc more complex to use, even for simple expression evaluation.

You should ask for a NScript project, I would do it ;)

Sep 18, 2009 at 2:24 PM
Edited Sep 18, 2009 at 2:43 PM

Hi Sebastien

My main reason is for evaluating an expression and then being able to re-use the result multiple times without the system re-evaluating it. Is there another solution to this?

Many thanks

Lee

-----Original Message-----
From: sebastienros
Sent: 9/18/2009 1:50:39 PM
To: [email removed]
Subject: Re: Support for parameter initialization in code [ncalc:69372]

From: sebastienros

Going into this sort of script functionnality is opening some complexity. Actually this could be another project, like lighweight scripting engine. I don't say it is not useful, because it is really useful for some domains, but the goal on NCalc is not this one. It would make NCalc more complex to use, even for simple expression evaluation.

You should ask for a NScript project, I would do it ;)

Coordinator
Sep 18, 2009 at 3:45 PM

You can evaluate it once and set the result as a parameter:

Expression e1 = new Expression("3.14 * x");
e1.SetParameter("x", 0.5);
// a is evaluated once
double a = e1.Evaluate();

Expression e = new Expression("cos(a) * b");
e.SetParameter("a", a);
e.SetParameter("b", 1);
e.Evaluate();
// define a new value for b
e.SetParameter("b", 2);
e.Evaluate();

And if you pass an Expression instance to a parameter, it will get evaluted each time it is needed, as the parameter might be dependent of another variable.

 

Sep 21, 2009 at 11:01 AM

Thanks

The trouble is, I want the user to be able to set the expression, and that the expression is only evaluated when needed. It's really just to save multiple evaluation of the same expression during multiple tests against it.

Lee