[Numpy-discussion] polynomial fromroots

Charles R Harris charlesr.harris@gmail....
Sat Oct 9 21:21:51 CDT 2010


On Sat, Oct 9, 2010 at 8:05 PM, Charles R Harris
<charlesr.harris@gmail.com>wrote:

>
>
> On Sat, Oct 9, 2010 at 8:01 PM, Charles R Harris <
> charlesr.harris@gmail.com> wrote:
>
>>
>>
>> On Sat, Oct 9, 2010 at 7:47 PM, <josef.pktd@gmail.com> wrote:
>>
>>> I'm trying to see whether I can do this without reading the full manual.
>>>
>>> Is it intended that fromroots normalizes the highest order term
>>> instead of the lowest?
>>>
>>>
>>> >>> import numpy.polynomial as poly
>>>
>>> >>> p = poly.Polynomial([1, -1.88494037,  0.0178126 ])
>>> >>> p
>>> Polynomial([ 1.        , -1.88494037,  0.0178126 ], [-1.,  1.])
>>> >>> pr = p.roots()
>>> >>> pr
>>> array([   0.53320748,  105.28741219])
>>> >>> poly.Polynomial.fromroots(pr)
>>> Polynomial([  56.14003571, -105.82061967,    1.        ], [-1.,  1.])
>>> >>>
>>>
>>> renormalizing
>>>
>>> >>> p2 = poly.Polynomial.fromroots(pr)
>>> >>> p2/p2.coef[0]
>>> Polynomial([ 1.        , -1.88494037,  0.0178126 ], [-1.,  1.])
>>>
>>>
>>> this is, I think what I want to do, invert roots that are
>>> inside/outside the unit circle (whatever that means
>>>
>>> >>> pr[np.abs(pr)<1] = 1./pr[np.abs(pr)<1]
>>> >>> p3 = poly.Polynomial.fromroots(pr)
>>> >>> p3/p3.coef[0]
>>> Polynomial([ 1.        , -0.54270529,  0.0050643 ], [-1.,  1.])
>>>
>>>
>> Wrong function ;) You defined the polynomial by its coefficients. What you
>> want to do is
>>
>> In [1]: import numpy.polynomial as poly
>>
>> In [2]: p = poly.Polynomial.fromroots([1, -1.88494037,  0.0178126 ])
>>
>> In [3]: p
>> Out[3]: Polynomial([ 0.03357569, -1.90070346,  0.86712777,  1.        ],
>> [-1.,  1.])
>>
>> In [4]: p.roots()
>> Out[4]: array([-1.88494037,  0.0178126 ,  1.        ])
>>
>>
> Oh, and least squares follows the same convention:
>
> In [5]: x = linspace(-1,1,10)
>
> In [6]: y = (x - 1)*( x + 1.88494037)*(x - 0.0178126)
>
> In [7]: p = poly.Polynomial.fit(x, y, 3)
>
> In [8]: p
> Out[8]: Polynomial([ 0.03357569, -1.90070346,  0.86712777,  1.        ],
> [-1.,  1.])
>
>
You can also use a scaled and shifted domain, which will change the
coefficient representation but can be used to obtain a polynomial less
subject to roundoff errors.

In [18]: p = poly.Polynomial.fromroots([1, -1.88494037,  0.0178126 ],
domain=[-2,1])

In [19]: p
Out[19]: Polynomial([ 0.3187287 , -0.89681388, -0.42191482,  1.        ],
[-2.,  1.])

In [20]: p.roots()
Out[20]: array([-1.88494037,  0.0178126 ,  1.        ])

In [21]: plot(*p.linspace())
Out[21]: [<matplotlib.lines.Line2D object at 0x3ea10d0>]

I attached the plot.

Chuck

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20101009/aa86bb1d/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 21525 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/numpy-discussion/attachments/20101009/aa86bb1d/attachment-0001.png 


More information about the NumPy-Discussion mailing list