Gauss-Legendre quadrature. Computes the sample points and weights for Gauss-Legendre quadrature. These sample points and weights will correctly integrate polynomials of degree :math:`2*deg - 1` or less over the interval :math:`[-1, 1]` with the weight function :math:`f(x) = 1`.
(deg)
| 1518 | |
| 1519 | |
| 1520 | def leggauss(deg): |
| 1521 | """ |
| 1522 | Gauss-Legendre quadrature. |
| 1523 | |
| 1524 | Computes the sample points and weights for Gauss-Legendre quadrature. |
| 1525 | These sample points and weights will correctly integrate polynomials of |
| 1526 | degree :math:`2*deg - 1` or less over the interval :math:`[-1, 1]` with |
| 1527 | the weight function :math:`f(x) = 1`. |
| 1528 | |
| 1529 | Parameters |
| 1530 | ---------- |
| 1531 | deg : int |
| 1532 | Number of sample points and weights. It must be >= 1. |
| 1533 | |
| 1534 | Returns |
| 1535 | ------- |
| 1536 | x : ndarray |
| 1537 | 1-D ndarray containing the sample points. |
| 1538 | y : ndarray |
| 1539 | 1-D ndarray containing the weights. |
| 1540 | |
| 1541 | Notes |
| 1542 | ----- |
| 1543 | |
| 1544 | .. versionadded:: 1.7.0 |
| 1545 | |
| 1546 | The results have only been tested up to degree 100, higher degrees may |
| 1547 | be problematic. The weights are determined by using the fact that |
| 1548 | |
| 1549 | .. math:: w_k = c / (L'_n(x_k) * L_{n-1}(x_k)) |
| 1550 | |
| 1551 | where :math:`c` is a constant independent of :math:`k` and :math:`x_k` |
| 1552 | is the k'th root of :math:`L_n`, and then scaling the results to get |
| 1553 | the right value when integrating 1. |
| 1554 | |
| 1555 | """ |
| 1556 | ideg = pu._deprecate_as_int(deg, "deg") |
| 1557 | if ideg <= 0: |
| 1558 | raise ValueError("deg must be a positive integer") |
| 1559 | |
| 1560 | # first approximation of roots. We use the fact that the companion |
| 1561 | # matrix is symmetric in this case in order to obtain better zeros. |
| 1562 | c = np.array([0]*deg + [1]) |
| 1563 | m = legcompanion(c) |
| 1564 | x = la.eigvalsh(m) |
| 1565 | |
| 1566 | # improve roots by one application of Newton |
| 1567 | dy = legval(x, c) |
| 1568 | df = legval(x, legder(c)) |
| 1569 | x -= dy/df |
| 1570 | |
| 1571 | # compute the weights. We scale the factor to avoid possible numerical |
| 1572 | # overflow. |
| 1573 | fm = legval(x, c[1:]) |
| 1574 | fm /= np.abs(fm).max() |
| 1575 | df /= np.abs(df).max() |
| 1576 | w = 1/(fm * df) |
| 1577 |
nothing calls this directly
no test coverage detected