# pylab, matplotlib ... roots function question

Robert Kern robert.kern at gmail.com
Mon Jan 22 07:33:12 CET 2007

```Schüle Daniel wrote:
> Hello NG,
>
> given this call to roots funtion from pylab

It's actually from numpy and numpy questions are best asked (and best answered!)
on numpy-discussion.

http://www.scipy.org/Mailing_Lists

> In [342]: roots([0,2,2])
> Out[342]: array([-1.])
>
> as far as I understand it [a0,a1,a2] stands for a0+a1*x+a2*x^2
> in the above case it yields 2x^2+2x = 2x(1+x)
> and the roots are 0 and -1
> I am wondering why roots function gives me only the -1

No, it's the other way around.

In [1]: from numpy import *

In [2]: roots?
Type:           function
Base Class:     <type 'function'>
Namespace:      Interactive
File:
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/numpy-1.0.2.dev3507-py2.5-macosx-10.4-i386.egg/numpy/lib/polynomial.py
Definition:     roots(p)
Docstring:
Return the roots of the polynomial coefficients in p.

The values in the rank-1 array p are coefficients of a polynomial.
If the length of p is n+1 then the polynomial is
p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]

So you were really solving 2*x + 2 = 0, the single root of which is -1.

> second try
>
> In [343]: roots([1,0,0])
> Out[343]: array([], dtype=float64)
>
> ok, as it should be

No, that's actually wrong. What version of numpy are you using? With a recent
SVN checkout of numpy, I get the correct answer:

In [3]: roots([1,0,0])
Out[3]: array([ 0.,  0.])

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma