MCPcopy Create free account
hub / github.com/numpy/numpy / legroots

Function legroots

numpy/polynomial/legendre.py:1459–1517  ·  view source on GitHub ↗

Compute the roots of a Legendre series. Return the roots (a.k.a. "zeros") of the polynomial .. math:: p(x) = \\sum_i c[i] * L_i(x). Parameters ---------- c : 1-D array_like 1-D array of coefficients. Returns ------- out : ndarray Array of the

(c)

Source from the content-addressed store, hash-verified

1457
1458
1459def legroots(c):
1460 """
1461 Compute the roots of a Legendre series.
1462
1463 Return the roots (a.k.a. "zeros") of the polynomial
1464
1465 .. math:: p(x) = \\sum_i c[i] * L_i(x).
1466
1467 Parameters
1468 ----------
1469 c : 1-D array_like
1470 1-D array of coefficients.
1471
1472 Returns
1473 -------
1474 out : ndarray
1475 Array of the roots of the series. If all the roots are real,
1476 then `out` is also real, otherwise it is complex.
1477
1478 See Also
1479 --------
1480 numpy.polynomial.polynomial.polyroots
1481 numpy.polynomial.chebyshev.chebroots
1482 numpy.polynomial.laguerre.lagroots
1483 numpy.polynomial.hermite.hermroots
1484 numpy.polynomial.hermite_e.hermeroots
1485
1486 Notes
1487 -----
1488 The root estimates are obtained as the eigenvalues of the companion
1489 matrix, Roots far from the origin of the complex plane may have large
1490 errors due to the numerical instability of the series for such values.
1491 Roots with multiplicity greater than 1 will also show larger errors as
1492 the value of the series near such points is relatively insensitive to
1493 errors in the roots. Isolated roots near the origin can be improved by
1494 a few iterations of Newton's method.
1495
1496 The Legendre series basis polynomials aren't powers of ``x`` so the
1497 results of this function may seem unintuitive.
1498
1499 Examples
1500 --------
1501 >>> import numpy.polynomial.legendre as leg
1502 >>> leg.legroots((1, 2, 3, 4)) # 4L_3 + 3L_2 + 2L_1 + 1L_0, all real roots
1503 array([-0.85099543, -0.11407192, 0.51506735]) # may vary
1504
1505 """
1506 # c is a trimmed copy
1507 [c] = pu.as_series([c])
1508 if len(c) < 2:
1509 return np.array([], dtype=c.dtype)
1510 if len(c) == 2:
1511 return np.array([-c[0]/c[1]])
1512
1513 # rotated companion matrix reduces error
1514 m = legcompanion(c)[::-1,::-1]
1515 r = la.eigvals(m)
1516 r.sort()

Callers

nothing calls this directly

Calls 2

legcompanionFunction · 0.85
sortMethod · 0.80

Tested by

no test coverage detected