jiggzson / nerdamer

a symbolic math expression evaluator for javascript
http://www.nerdamer.com
MIT License
514 stars 82 forks source link

Simplify hangs depending on lexographic ordering of the variable names. #568

Closed thunderkid closed 3 years ago

thunderkid commented 4 years ago

Running the following does not hang:

simplify(z - ( ( - ( - 1 + x ) ^ ( - 1 ) + x * ( - 1 + x ) ^ ( - 1 ) ) ^ ( - 1 ) * ( - 1 + x ) ^ ( - 1 ) * z * x - ( - ( - 1 + x ) ^ ( - 1 ) + x * ( - 1 + x ) ^ ( - 1 ) ) ^ ( - 1 ) * ( - 1 + x ) ^ ( - 1 ) * z * x * x ^ ( - 1 ) ) )

Now change the variable name from z to w. So run this:

simplify(w - ( ( - ( - 1 + x ) ^ ( - 1 ) + x * ( - 1 + x ) ^ ( - 1 ) ) ^ ( - 1 ) * ( - 1 + x ) ^ ( - 1 ) * w * x - ( - ( - 1 + x ) ^ ( - 1 ) + x * ( - 1 + x ) ^ ( - 1 ) ) ^ ( - 1 ) * ( - 1 + x ) ^ ( - 1 ) * w * x * x ^ ( - 1 ) ) )

Now it hangs! A few experiments seemed to show that it hangs if the z variable is alphabetically named anything before the x variable, but I haven't exhaustively tested this. Running these examples on the demo website do not hang, but running version 1.1.5 locally under node does. The website says it uses version 1.1.5, so I don't know why these results should be different. I haven't investigated this difference.

(Note that even the non-hanging expression above does not fully simplify due to https://github.com/jiggzson/nerdamer/issues/565.)

jiggzson commented 3 years ago

Moved to #580