I did this exact thing using NCalc . I went through the user string expression, replaced the variables with the values ββI am evaluating, and then using the Evaluate method and parsing it with a double.
private double Function(double t, double y) { NCalc.Expression expression = new NCalc.Expression(this.Expression); expression.Parameters["t"] = t; expression.Parameters["y"] = y; double value; double.TryParse(expression.Evaluate().ToString(), out value); return value; }
For example, given the inputs t = .5 and y = 1 and the expression "4 * y + Tan (2 * t)", we will evaluate the string "4 * 1 + Tan (2 * .5)" using NCalc.
This is not ideal, NCalc throws an exception if it cannot parse the user string or that the data types are different. I am working on polishing it.
I also asked the same question here .
kleineg Sep 03 '15 at 15:01 2015-09-03 15:01
source share