Compute the roots of a polynomial. Return the roots (a.k.a. "zeros") of the polynomial .. math:: p(x) = \\sum_i c[i] * x^i. Parameters ---------- c : 1-D array_like 1-D array of polynomial coefficients. Returns ------- out : ndarray Array of t
(c)
| 1403 | |
| 1404 | |
| 1405 | def polyroots(c): |
| 1406 | """ |
| 1407 | Compute the roots of a polynomial. |
| 1408 | |
| 1409 | Return the roots (a.k.a. "zeros") of the polynomial |
| 1410 | |
| 1411 | .. math:: p(x) = \\sum_i c[i] * x^i. |
| 1412 | |
| 1413 | Parameters |
| 1414 | ---------- |
| 1415 | c : 1-D array_like |
| 1416 | 1-D array of polynomial coefficients. |
| 1417 | |
| 1418 | Returns |
| 1419 | ------- |
| 1420 | out : ndarray |
| 1421 | Array of the roots of the polynomial. If all the roots are real, |
| 1422 | then `out` is also real, otherwise it is complex. |
| 1423 | |
| 1424 | See Also |
| 1425 | -------- |
| 1426 | numpy.polynomial.chebyshev.chebroots |
| 1427 | numpy.polynomial.legendre.legroots |
| 1428 | numpy.polynomial.laguerre.lagroots |
| 1429 | numpy.polynomial.hermite.hermroots |
| 1430 | numpy.polynomial.hermite_e.hermeroots |
| 1431 | |
| 1432 | Notes |
| 1433 | ----- |
| 1434 | The root estimates are obtained as the eigenvalues of the companion |
| 1435 | matrix, Roots far from the origin of the complex plane may have large |
| 1436 | errors due to the numerical instability of the power series for such |
| 1437 | values. Roots with multiplicity greater than 1 will also show larger |
| 1438 | errors as the value of the series near such points is relatively |
| 1439 | insensitive to errors in the roots. Isolated roots near the origin can |
| 1440 | be improved by a few iterations of Newton's method. |
| 1441 | |
| 1442 | Examples |
| 1443 | -------- |
| 1444 | >>> import numpy.polynomial.polynomial as poly |
| 1445 | >>> poly.polyroots(poly.polyfromroots((-1,0,1))) |
| 1446 | array([-1., 0., 1.]) |
| 1447 | >>> poly.polyroots(poly.polyfromroots((-1,0,1))).dtype |
| 1448 | dtype('float64') |
| 1449 | >>> j = complex(0,1) |
| 1450 | >>> poly.polyroots(poly.polyfromroots((-j,0,j))) |
| 1451 | array([ 0.00000000e+00+0.j, 0.00000000e+00+1.j, 2.77555756e-17-1.j]) # may vary |
| 1452 | |
| 1453 | """ |
| 1454 | # c is a trimmed copy |
| 1455 | [c] = pu.as_series([c]) |
| 1456 | if len(c) < 2: |
| 1457 | return np.array([], dtype=c.dtype) |
| 1458 | if len(c) == 2: |
| 1459 | return np.array([-c[0]/c[1]]) |
| 1460 | |
| 1461 | # rotated companion matrix reduces error |
| 1462 | m = polycompanion(c)[::-1,::-1] |
nothing calls this directly
no test coverage detected