Numpy.poly1d, root search optimization, polynomial shift along the x axis

usually a difficult task is to create an nth-order polynomial and find the roots with numpy:

import numpy
f = numpy.poly1d([1,2,3])
print numpy.roots(f)
array([-1.+1.41421356j, -1.-1.41421356j])

However, suppose you need a polynomial like:

f(x) = a*(x-x0)**0 + b(x-x0)**1 + ... + n(x-x0)**n

Is there an easy way to build the numpy.poly1d function and find the roots? I tried scipy.fsolve, but it is very unstable, since it strongly depends on the choice of starting values ​​in my particular case.

Thanks in advance Regards rrrak

EDIT: Changed "polygon" (wrong) to "polynomial" (correct)

+5
source share
2 answers

First, of course, you mean a polynomial, not a polygon?

"x0" ? , y = x - x0, y x, x = y + x0.

-, . ,

f(x) = 1 + 3(x-1) + (x-1)**2

>>> g = numpy.poly1d([1,3,1])
>>> f = lambda x:g(x-1)
>>> f(0.0)
-1.0

f :

f.roots = numpy.roots(g) + 1
+4

, x0 , :

f(x) = 3*(x-0)**0 + 2*(x-2)**1 + 3*(x-1)**2 + 2*(x-2)**3

:

import numpy as np
import operator

ks = [3,2,3,2]
offsets = [0,2,1,2]

p = reduce(operator.add, [np.poly1d([1, -x0])**i * c for i, (c, x0) in enumerate(zip(ks, offsets))])

print p

:

   3     2
2 x - 9 x + 20 x - 14
+3

All Articles