[Numpy-discussion] Problem with roots and complex coefficients

Nils Wagner nwagner@iam.uni-stuttgart...
Thu Apr 19 10:44:17 CDT 2007


lorenzo bolla wrote:
> dear all,
> I've some problems with numpy.roots.
> take a look at the following code:
>
> ========================================
> import numpy
>
> OK = numpy.roots([1, 1, 1])
> OK = numpy.roots([1j, 1])
> KO = numpy.roots([1, 1j, 1])
> ========================================
>
> it fails with this error message, trying to execute the last line:
>
> TypeError: can't convert complex to float; use
> abs(z)/usr/lib/python2.4/site-packages/numpy/lib/polynomial.py in
> roots(p)
>     119     if N > 1:
>     120         # build companion matrix and find its eigenvalues (the
> roots)
> --> 121         A = diag(NX.ones((N-2,), p.dtype), -1)
>     122         A[0, :] = -p[1:] / p[0]
>     123         roots = _eigvals(A)
>
> /usr/lib/python2.4/site-packages/numpy/lib/twodim_base.py in diag(v, k)
>      66             i = arange(0,n+k)
>      67             fi = i+(i-k)*n
> ---> 68         res.flat[fi] = v
>      69         return res
>      70     elif len(s)==2:
>
> TypeError: can't convert complex to float; use abs(z)
>
> any ideas?
> thanks,
> Lorenzo
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>   
Works fine for me. Maybe you are using an old numpy version.

>>> from numpy import *
>>> roots([1, 1, 1])
array([-0.5+0.8660254j, -0.5-0.8660254j])
>>> roots([1j, 1])
array([-0.+1.j])
>>> roots([1, 1j, 1])
array([  0.00000000e+00-1.61803399j,   1.38777878e-17+0.61803399j])
>>> import numpy
>>> numpy.__version__
'1.0.3.dev3716'


Nils



More information about the Numpy-discussion mailing list