How do I calculate the sqrt in Math.NET Symbolics

#1

I’m using the Math.Net 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 Math.Net Symbolics?

(Christoph Rüegg) #2

Automatic simplification does indeed not yet attempt to factorize the radix in expressions of the form a^(1/b) where both a and b are integers. This is a good idea, we should do that!

So, once implemented, I’d expect the term sqrt(18) to be automatically simplified to 3*sqrt(2). Similar simplifications should also apply to higher roots.

(Christoph Rüegg) #3

That sqrt(x^2) is not simplified is suspicious, I need to check that. However, note that sqrt(x^2) cannot actually simplify to x, as this would not hold e.g. for negative numbers.

Update: I’ve just checked, Mathematica does not simplify sqrt(x^2) either.

#4

What about sqrt(9)? Why doesn’t that display 3? Will you still consider adding support for simplyfing sqrt(x^2)?

(Christoph Rüegg) #5

What I meant with the factorization in case of square roots is that it would try find a solution for sqrt(a) = sqrt(b^2*c) where a, b and c are all positive integers, and then simplify that to b*sqrt(c). In the case of 9, this has a solution with b=3, c=1, so it would then simplify to 3*sqrt(1) and thus simply 3.

(Christoph Rüegg) #6

No, I don’t think we would want to simplify sqrt(x^2) to x, since that would be mathematically wrong unless we know e.g. x to be a positive integer.

Note that we do simplify in a related case: sqrt(x)^2 is automatically simplified to x.