mathnet/mathnet-symbolics

-1**2 returns 1

diluculo opened this issue · 4 comments

No. Input Return Expected
(a) -1Q**2 1 -1
(b) negate (one**two) -1 -1
(c) 0 - 1Q**2 -1 -1
(d) -(x+y)**2 (x + y)^2 -(x + y)^2
(e) -1*(x+y)**2 -(x + y)^2 -(x + y)^2

Excel calculates -1^2 as 1, but we have to think it as -(1^2).

This results from the precedence of unary minus is higher than power operator. I think the list below may be right for us.

Precedence of operators, from highest to lowest.

  1. parentheses
  2. pow: a^b
  3. unary minus and plus: -a, +a
  4. multiply, divide : a*b, a/b
  5. add, subtract: a+b, a-b

I'm actually not entirely sure we can control this (at least when using F# as dsl), since prefix operators have a higher precedence in F# than the power operator (docs). What happens in the Infix parser is a different story (and would also need to be verified).

Yes, I can get right expression by changing the precedence of operators in the Infix parser.

Infix.parseOrUndefined "a*exp(-(x-b)^2/(2*c^2))";; // Gaussian function
val it : Expression = a*exp(-(-b + x)^2/(2*c^2))

a*exp(-(x-b)**2/(2*c**2));;
val it : Expression = a*exp((b - x)^2/(2*c^2))

Workaround we can select now is using "-1*" or "0 - " instead of "-" to get right answer, is it right?

Unlike F#, it is clear that exponentiation has higher precedence than unary minus in the mathematics, and in fact, it is already reflected in Infix.format. However, it is not easy to change the order of operators of F# now. So how about intentionally setting the precedence of operators of F# and the Infix parser differently? If we know this and can use parentheses properly when in doubt, I think it is not a bad idea.