def roots(p):
<EOL>p = atleast_1d(p)<EOL>if len(p.shape) != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>non_zero = NX.nonzero(NX.ravel(p))[<NUM_LIT:0>]<EOL>if len(non_zero) == <NUM_LIT:0>:<EOL><INDENT>return NX.array([])<EOL><DEDENT>trailing_zeros = len(p) - non_zero[-<NUM_LIT:1>] - <NUM_LIT:1><EOL>p = p[int(non_zero[<NUM_LIT:0>]):int(non_zero[-<NUM_LIT:1>])+<NUM_LIT:1>]<EOL>if not issubclass(p.dtype.type, (NX.floating, NX.complexfloating)):<EOL><INDENT>p = p.astype(float)<EOL><DEDENT>N = len(p)<EOL>if N > <NUM_LIT:1>:<EOL><INDENT>A = diag(NX.ones((N-<NUM_LIT:2>,), p.dtype), -<NUM_LIT:1>)<EOL>A[<NUM_LIT:0>,:] = -p[<NUM_LIT:1>:] / p[<NUM_LIT:0>]<EOL>roots = eigvals(A)<EOL><DEDENT>else:<EOL><INDENT>roots = NX.array([])<EOL><DEDENT>roots = hstack((roots, NX.zeros(trailing_zeros, roots.dtype)))<EOL>return roots<EOL>
Return the roots of a polynomial with coefficients given 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 described by::

  p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]

Parameters
----------
p : array_like
    Rank-1 array of polynomial coefficients.

Returns
-------
out : ndarray
    An array containing the complex roots of the polynomial.

Raises
------
ValueError
    When `p` cannot be converted to a rank-1 array.

See also
--------
poly : Find the coefficients of a polynomial with a given sequence
       of roots.
polyval : Evaluate a polynomial at a point.
polyfit : Least squares polynomial fit.
poly1d : A one-dimensional polynomial class.

Notes
-----
The algorithm relies on computing the eigenvalues of the
companion matrix [1]_.

References
----------
.. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*.  Cambridge, UK:
    Cambridge University Press, 1999, pp. 146-7.

Examples
--------
>>> coeff = [3.2, 2, 1]
>>> np.roots(coeff)
array([-0.3125+0.46351241j, -0.3125-0.46351241j])