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)
| 1543 | |
| 1544 | |
| 1545 | def polyroots(c): |
| 1546 | """ |
| 1547 | Compute the roots of a polynomial. |
| 1548 | |
| 1549 | Return the roots (a.k.a. "zeros") of the polynomial |
| 1550 | |
| 1551 | .. math:: p(x) = \\sum_i c[i] * x^i. |
| 1552 | |
| 1553 | Parameters |
| 1554 | ---------- |
| 1555 | c : 1-D array_like |
| 1556 | 1-D array of polynomial coefficients. |
| 1557 | |
| 1558 | Returns |
| 1559 | ------- |
| 1560 | out : ndarray |
| 1561 | Array of the roots of the polynomial. If all the roots are real, |
| 1562 | then `out` is also real, otherwise it is complex. |
| 1563 | |
| 1564 | See Also |
| 1565 | -------- |
| 1566 | numpy.polynomial.chebyshev.chebroots |
| 1567 | numpy.polynomial.legendre.legroots |
| 1568 | numpy.polynomial.laguerre.lagroots |
| 1569 | numpy.polynomial.hermite.hermroots |
| 1570 | numpy.polynomial.hermite_e.hermeroots |
| 1571 | |
| 1572 | Notes |
| 1573 | ----- |
| 1574 | The root estimates are obtained as the eigenvalues of the companion |
| 1575 | matrix, Roots far from the origin of the complex plane may have large |
| 1576 | errors due to the numerical instability of the power series for such |
| 1577 | values. Roots with multiplicity greater than 1 will also show larger |
| 1578 | errors as the value of the series near such points is relatively |
| 1579 | insensitive to errors in the roots. Isolated roots near the origin can |
| 1580 | be improved by a few iterations of Newton's method. |
| 1581 | |
| 1582 | Examples |
| 1583 | -------- |
| 1584 | >>> import numpy.polynomial.polynomial as poly |
| 1585 | >>> poly.polyroots(poly.polyfromroots((-1,0,1))) |
| 1586 | array([-1., 0., 1.]) |
| 1587 | >>> poly.polyroots(poly.polyfromroots((-1,0,1))).dtype |
| 1588 | dtype('float64') |
| 1589 | >>> j = complex(0,1) |
| 1590 | >>> poly.polyroots(poly.polyfromroots((-j,0,j))) |
| 1591 | array([ 0.00000000e+00+0.j, 0.00000000e+00+1.j, 2.77555756e-17-1.j]) # may vary |
| 1592 | |
| 1593 | """ # noqa: E501 |
| 1594 | # c is a trimmed copy |
| 1595 | [c] = pu.as_series([c]) |
| 1596 | if len(c) < 2: |
| 1597 | return np.array([], dtype=c.dtype) |
| 1598 | if len(c) == 2: |
| 1599 | return np.array([-c[0] / c[1]]) |
| 1600 | |
| 1601 | m = polycompanion(c) |
| 1602 | r = np.linalg.eigvals(m) |
nothing calls this directly
no test coverage detected
searching dependent graphs…