jiggzson / nerdamer

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

Problems with nerdamer.simplify and {expr}.simplify with square roots #651

Open michaelcheers opened 2 years ago

michaelcheers commented 2 years ago

Review the following code examples:

nerdamer('sqrt(2)').simplify().toString()     -> 'sqrt(2)'
nerdamer('sqrt(a)/a').simplify().toString()   -> 'a^(-1/2)'
nerdamer('sqrt(2*a)/a').simplify().toString() -> 'a^(-1)*sqrt(2)*sqrt(a)' // should be sqrt(2/a)
nerdamer.simplify('sqrt(2)').toString()       -> '131836323/93222358'
nerdamer.simplify('sqrt(a)/a').toString()     -> 'sqrt(a)^(-1)'
nerdamer.simplify('sqrt(2*a)/a').toString()   -> 'sqrt((131836323/93222358)*a)^(-1)' // this is wrong sqrt(sqrt(2)*a)^(-1) is not equal to sqrt(2*a)^(-1)

So I've found three problems:

gunnarmein-ts commented 2 years ago

To your second point, it is not simplify() that does that, it is simply how Nerdamer represents numbers internally. .text("decimals") or simply .text() is your friend if that is what you want, instead of .toString(). As for the rest, yeah, I have given up on simplify(). Perhaps one of use should take the time to debug that, as a community contribution.

gunnarmein-ts commented 2 years ago

Also, if you want functions like sqrt(2) evaluated, you need to say "evaluate()". I agree with the concept of keeping things symbolic as long as one can.