MCPcopy Index your code
hub / github.com/numpy/numpy / hermgauss

Function hermgauss

numpy/polynomial/hermite.py:1646–1713  ·  view source on GitHub ↗

Gauss-Hermite quadrature. Computes the sample points and weights for Gauss-Hermite quadrature. These sample points and weights will correctly integrate polynomials of degree :math:`2*deg - 1` or less over the interval :math:`[-\\inf, \\inf]` with the weight function :math:`f(x)

(deg)

Source from the content-addressed store, hash-verified

1644
1645
1646def hermgauss(deg):
1647 """
1648 Gauss-Hermite quadrature.
1649
1650 Computes the sample points and weights for Gauss-Hermite quadrature.
1651 These sample points and weights will correctly integrate polynomials of
1652 degree :math:`2*deg - 1` or less over the interval :math:`[-\\inf, \\inf]`
1653 with the weight function :math:`f(x) = \\exp(-x^2)`.
1654
1655 Parameters
1656 ----------
1657 deg : int
1658 Number of sample points and weights. It must be >= 1.
1659
1660 Returns
1661 -------
1662 x : ndarray
1663 1-D ndarray containing the sample points.
1664 y : ndarray
1665 1-D ndarray containing the weights.
1666
1667 Notes
1668 -----
1669 The results have only been tested up to degree 100, higher degrees may
1670 be problematic. The weights are determined by using the fact that
1671
1672 .. math:: w_k = c / (H'_n(x_k) * H_{n-1}(x_k))
1673
1674 where :math:`c` is a constant independent of :math:`k` and :math:`x_k`
1675 is the k'th root of :math:`H_n`, and then scaling the results to get
1676 the right value when integrating 1.
1677
1678 Examples
1679 --------
1680 >>> from numpy.polynomial.hermite import hermgauss
1681 >>> hermgauss(2)
1682 (array([-0.70710678, 0.70710678]), array([0.88622693, 0.88622693]))
1683
1684 """
1685 ideg = pu._as_int(deg, "deg")
1686 if ideg <= 0:
1687 raise ValueError("deg must be a positive integer")
1688
1689 # first approximation of roots. We use the fact that the companion
1690 # matrix is symmetric in this case in order to obtain better zeros.
1691 c = np.array([0] * deg + [1], dtype=np.float64)
1692 m = hermcompanion(c)
1693 x = np.linalg.eigvalsh(m)
1694
1695 # improve roots by one application of Newton
1696 dy = _normed_hermite_n(x, ideg)
1697 df = _normed_hermite_n(x, ideg - 1) * np.sqrt(2 * ideg)
1698 x -= dy / df
1699
1700 # compute the weights. We scale the factor to avoid possible numerical
1701 # overflow.
1702 fm = _normed_hermite_n(x, ideg - 1)
1703 fm /= np.abs(fm).max()

Callers

nothing calls this directly

Calls 4

hermcompanionFunction · 0.85
_normed_hermite_nFunction · 0.85
maxMethod · 0.45
sumMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…