mathnet / mathnet-numerics

Math.NET Numerics
http://numerics.mathdotnet.com
MIT License
3.44k stars 891 forks source link

Does MathNet have an Eval function #602

Open dgxhubbard opened 5 years ago

dgxhubbard commented 5 years ago

Does MathNet have a method that would take a formula

3x + y or 3 x * x + y

and create executable code for it?

cdrnet commented 5 years ago

Yes - but in Math.NET Symbolics, not in Numerics.

Example:

var expr = SymbolicExpression.Parse("3*x^2 + y");

// option: direct dynamic evaluation
var args = new Dictionary<string, FloatingPoint> { { "x", 1.5 }, { "y", 2.0 }};
FloatingPoint result1 = expr.Evaluate(args); // result1.RealValue == 8.75

// option: compile to .Net function
Func<double, double, double> compiled = expr.Compile("x", "y");
double result2 = compiled(1.5, 2.0); // 8.75

// collect variables in an expression:
expr.CollectVariables().Select(v => v.VariableName) // "x", "y"
chmn08 commented 1 week ago

@cdrnet still Math.NET doesn't have function to evaluate expression from string?