mpmath icon indicating copy to clipboard operation
mpmath copied to clipboard

Polyroots error

Open seghier opened this issue 2 years ago • 1 comments

Hi i try this equation

from mpmath import *
mp.dps = 15; mp.pretty = True
x = [-1, -4, -2.3, 1.6, 1, 2, 12, -9, -1, 0, 1.5] # (-x^10-4*x^9-2.3*x^8+1.6*x^7+x^6+2*x^5+12*x^4-9*x^3-x^2+1.5 = 0)
a = nstr(polyroots(x),20)

Which give two real solutions: -3.17386869917664204976404 1.03398886646284382528904

But these two numbers don't solve the equation: -x^10-4*x^9-2.3*x^8+1.6*x^7+x^6+2*x^5+12*x^4-9*x^3-x^2+1.5 = 0 The result: 207452.923218 2.793757

What's wrong here?

seghier avatar May 21 '22 22:05 seghier

Hi, seems to be a typo in your script. I cannot reproduce the same results.

from mpmath import *
mp.pretty =  True
mp.dps = 15

y = [-1, -4, -2.3, 1.6, 1, 2, 12, -9, -1, 0, 1.5] # (-x^10-4*x^9-2.3*x^8+1.6*x^7+x^6+2*x^5+12*x^4-9*x^3-x^2+1.5 = 0)

a = (polyroots(y), 5)

def func(x):
    Q = -1*x**10-4*x**9-2.3*x**8+1.6*x**7+1*x**6+2*x**5+12*x**4-9*x**3-1*x**2+1.5
    return Q

#your solutions
print(func(-3.17386869917664204976404))
print(func(1.03398886646284382528904))

Which yields -6.686207143502543e-12 and -6.8833827526759706e-15

MartinMikkelsen avatar Jun 21 '22 17:06 MartinMikkelsen

@seghier, I also can't reproduce your issue:

In [7]: polyval([-1, -4, -2.3, 1.6, 1, 2, 12, -9, -1, 0, 1.5], -3.1738686991766420498)
Out[7]: -1.86117787848161e-12
In [8]: polyval([-1, -4, -2.3, 1.6, 1, 2, 12, -9, -1, 0, 1.5], 1.0339888664628438253)
Out[8]: -1.55431223447522e-15

So, if this issue is valid (maybe you did typo somewhere, indeed) - please reopen it with a reproducible example.

skirpichev avatar Mar 25 '23 06:03 skirpichev