pylab, matplotlib ... roots function question
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!)
> In : roots([0,2,2])
> Out: 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 : from numpy import *
In : roots?
Base Class: <type 'function'>
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 * x**n + p * 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 : roots([1,0,0])
> Out: 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 : roots([1,0,0])
Out: array([ 0., 0.])
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list