Finding polynomial roots using Python — Possible Numpy Extension Bug

|▌冷眼眸甩不掉的悲伤 提交于 2021-02-06 09:04:49

问题


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()

回答1:


Simply pass it in the other order,

p = P([6, 5, 1])



回答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.



来源:https://stackoverflow.com/questions/19015935/finding-polynomial-roots-using-python-possible-numpy-extension-bug

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!