Search code examples
c#math.netmathnet-numericsmathdotnet-symbolics

How to set double precision in MathNet.Symbolics Evaluate function?


I have a Dictionary<string, double> values and values precision is important for me. I have a string Formula that Infix can Parse. I wonder how to do something like this:

var expr = Infix.ParseOrThrow(m.Formula);
var result = Evaluate.Evaluate<double>(values, expr);

to get a result with double precision?


Solution

  • Evaluate operates on a custom FloatingPoint type which can represent various double-precision floating-point values (mostly real or complex numbers, in theory also vectors or matrices). You can cast a double to a FloatingPoint, and use RealValue to get a double back from a FloatingPoint.

    Example:

    var variables = new Dictionary<string,FloatingPoint>
       {{ "a", 2.0 },
        { "b", 3.0 }};
    
    var expr = Infix.ParseOrThrow("2/3*a");
    double result = Evaluate.Evaluate(variables, expr).RealValue;