jiggzson / nerdamer

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

Incorrect solve output for some polynomial equations #588

Closed jaypaik closed 3 years ago

jaypaik commented 3 years ago

Hi!

I'm seeing differences in solutions between nerdamer and Wolfram Alpha for this particular call:

nerdamer("diff(10-110/(20-100/(10+x))-x, x)").solveFor("x").toString()
// "-10,(1/4)*j-10,(-1/4)*j-10"
// Wolfram Alpha (linked above)
// -5 - sqrt(55/2), -5 + sqrt(55/2)

When taking the output of diff and solving it via Wolfram Alpha, it seems to produce the correct results, so it seems the bug is in the solve function.

nerdamer("diff(10-110/(20-100/(10+x))-x, x)").toString()
// "-1+11000*(-100*(10+x)^(-1)+20)^(-2)*(10+x)^(-2)"
// Wolfram Alpha's solution: -5 - sqrt(55/2), -5 + sqrt(55/2)

Am I using this wrong / is there some setting to produce the correct numbers here? Thanks for taking a look!

jiggzson commented 3 years ago

@jaypaik, thanks for bringing this to my attention.

Am I using this wrong / is there some setting to produce the correct numbers here? Thanks for taking a look!

A quick investigation seems to point at a bug in expand. I'll have to take a closer look to see exactly what's happening.

expand(-(-100*(10+x)^(-1)+20)^2*(10+x)^2+11000) gives -16000000*(10+x)^(-2)*x^2-1600000000*(10+x)^(-2)-29000-320000000*(10+x)^(-2)*x-400*x^2-8000*x but should give -(10000*x^2)/(x^2+20*x+100)-(200000*x)/(x^2+20*x+100)-1000000/(x^2+20*x+100)+(4000*x^2)/(x+10)+(80000*x)/(x+10)+400000/(x+10)-400*x^2-8000*x-29000

jiggzson commented 3 years ago

@jaypaik. Fixed. Quite an interesting issue by the way. It was a lot more involved than I initially thought. It actually ended up exposing three bugs.

jaypaik commented 3 years ago

Wow, that does seem like a pretty involved fix, and I see you had to deal with what seems like floating point precision issues too. Thanks for the fix and actively maintaining this project to this day. Cheers!

jiggzson commented 3 years ago

I see you had to deal with what seems like floating point precision issues too

Unfortunately you are correct. Someday I'll find the time and motivation to do a complete conversion to big numbers.

Thanks for the fix and actively maintaining this project to this day. Cheers!

You're welcome and thanks.