How do I calculate the sqrt in MathNet.Symbolics

535 views Asked by At

I'm using the MathNet.Symbolics library to simplyfy expressions like this :

string f = Infix.Print(Infix.ParseOrThrow("A+5*2"))

This works as expected (f = A+10) but trying to get the root of a number is a lot harder than I expected. For example :

string f = Infix.Print(Infix.ParseOrThrow("sqrt(9)"))

f = "sqrt(9)" instead of f = "3" as you would expect.

string f = Infix.Print(Infix.ParseOrThrow("sqrt(x^2)"))

f = "sqrt(x^2)" insted of f = "x"

string f = Infix.Print(Infix.ParseOrThrow("9^(1/2)"))

also doesn't work. Insted it gets simplified to f = "sqrt(9)"

How do I force it to calculate the sqrt of a number/variable?

Are there any other problems I could expect to run into when using the "auto-simplification" of MathNet.Symbolics?

1

There are 1 answers

0
xfx On

You need to run the expression through the Evaluate method:

string f = Infix.Print(Infix.ParseOrThrow("sqrt(9)"));
double result = Evaluate.Evaluate(null, f);