mathnet/mathnet-symbolics

Suggestion: Hardcoding more than once is error prone

sadqiang opened this issue · 1 comments

I think hardcoding "x" more than once should be avoided because it is error prone. It potentially causes runtime errors.

            var x = Expr.Variable("x");
            Func<double, double> f = (x * x + x - 6).Compile("x");
            Console.WriteLine(f(0));

My suggestion is as follows:

            var x = Expr.Variable("x");
            Func<double, double> f = (x * x + x - 6).Compile(x);
            Console.WriteLine(f(0));

Any comments are welcome. Thank you.

It would also be more consistent with the rest of the API.

Technically, if we'd change the argument to expect a SymbolicExpression, the former notation with a string would still work due to the implicit cast from string to the expression. We'd need to verify it also works with the params array overload though.