Return the roots of a polynomial with coefficients given in p. .. note:: This forms part of the old polynomial API. Since version 1.4, the new polynomial API defined in `numpy.polynomial` is preferred. A summary of the differences can be found in the :doc:`trans
(p)
| 167 | |
| 168 | @array_function_dispatch(_roots_dispatcher) |
| 169 | def roots(p): |
| 170 | """ |
| 171 | Return the roots of a polynomial with coefficients given in p. |
| 172 | |
| 173 | .. note:: |
| 174 | This forms part of the old polynomial API. Since version 1.4, the |
| 175 | new polynomial API defined in `numpy.polynomial` is preferred. |
| 176 | A summary of the differences can be found in the |
| 177 | :doc:`transition guide </reference/routines.polynomials>`. |
| 178 | |
| 179 | The values in the rank-1 array `p` are coefficients of a polynomial. |
| 180 | If the length of `p` is n+1 then the polynomial is described by:: |
| 181 | |
| 182 | p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n] |
| 183 | |
| 184 | Parameters |
| 185 | ---------- |
| 186 | p : array_like |
| 187 | Rank-1 array of polynomial coefficients. |
| 188 | |
| 189 | Returns |
| 190 | ------- |
| 191 | out : ndarray |
| 192 | An array containing the roots of the polynomial. |
| 193 | |
| 194 | Raises |
| 195 | ------ |
| 196 | ValueError |
| 197 | When `p` cannot be converted to a rank-1 array. |
| 198 | |
| 199 | See also |
| 200 | -------- |
| 201 | poly : Find the coefficients of a polynomial with a given sequence |
| 202 | of roots. |
| 203 | polyval : Compute polynomial values. |
| 204 | polyfit : Least squares polynomial fit. |
| 205 | poly1d : A one-dimensional polynomial class. |
| 206 | |
| 207 | Notes |
| 208 | ----- |
| 209 | The algorithm relies on computing the eigenvalues of the |
| 210 | companion matrix [1]_. |
| 211 | |
| 212 | References |
| 213 | ---------- |
| 214 | .. [1] R. A. Horn & C. R. Johnson, *Matrix Analysis*. Cambridge, UK: |
| 215 | Cambridge University Press, 1999, pp. 146-7. |
| 216 | |
| 217 | Examples |
| 218 | -------- |
| 219 | >>> import numpy as np |
| 220 | >>> coeff = [3.2, 2, 1] |
| 221 | >>> np.roots(coeff) |
| 222 | array([-0.3125+0.46351241j, -0.3125-0.46351241j]) |
| 223 | |
| 224 | """ |
| 225 | # If input is scalar, this makes it an array |
| 226 | p = atleast_1d(p) |
no test coverage detected
searching dependent graphs…