Search code examples
c#stringfunctionevaluate

Using custom formula with Matheval in C#


private double xpy(double x, double y)
{
    return x + y;
}

...
org.matheval.Expression exp = new org.matheval.Expression();            
exp.SetFomular("2 * xpy(a, b)");  
exp.Bind("a", 1.2);
exp.Bind("b", 0.8);
var result = (double)exp.Eval();
...

The Matheval homepage lists "Custom functions and operators can be added at runtime" as a feature, but I can't find an example how to use a custom function (like xpy in the example) with it. How should the function be defined or specified?

My example leads to this exception: System.Exception: 'Function XPY does not exist'


Solution

  • This is not currently supported by MathEval. There is an opened issue and an opened PR in the MathEval's repo for registering custom functions, but it's not merged.