9

I am using Numpy to obtain the roots of polynomials. Numpy provides a module 'polynomial'.

My hand calc for x^2 + 5*x + 6 = 0 is x = -2 & x = -3. (Simple)

But my code shows me the wrong answer: array([-0.5 , -0.33333333]) (Inversed?)

Could anyone please find the culprit in my code? Or is it simply a bug?

from numpy.polynomial import Polynomial as P
p = P([1, 5, 6])
p.roots()
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Fake Howard
  • 93
  • 1
  • 1
  • 3
  • flebool is essentially correct. It seems that they reverse the order for the new 'polynomial' module. The order in the old module 'poly1d' in Numpy was as same as the one in Matlab. Thanks again, flebool. – Fake Howard Sep 26 '13 at 12:41
  • to reverse the order of params for new 'polynomial'module can use: **p = P(np.flip([1, 5, 6]))** – JeeyCi Jul 17 '23 at 03:02

2 Answers2

13

Simply pass it in the other order,

p = P([6, 5, 1])
gg349
  • 21,996
  • 5
  • 54
  • 64
-2

You could have realized this yourself if you had determined that, for a polynomial P of degree n, R(x) = x^n P(1/x) equals the reversed version of P. So, except for 0, the roots of R are the reciprocals of the roots of P.

Eric Jablow
  • 7,874
  • 2
  • 22
  • 29