Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Gauss-Hermite quadrature

Synopsis

#include <boost/math/quadrature/gauss_hermite.hpp>

namespace boost{ namespace math{ namespace quadrature{

template <class Real, unsigned Points, class Policy = boost::math::policies::policy<> >
struct gauss_hermite
{
   static const RandomAccessContainer& abscissa();
   static const RandomAccessContainer& weights();

   template <class F>
   static auto integrate(F f, Real* pL1 = nullptr)->decltype(std::declval<F>()(std::declval<Real>()));

   template <class F, class Norm>
   static auto integrate(F f, const decltype(f(Real(0)))& zero, Norm norm, Real* pL1 = nullptr) -> decltype(f(Real(0)));

   template <class F>
   static auto integrate_averaged(F f, Real* error = nullptr, Real* pL1 = nullptr)->decltype(std::declval<F>()(std::declval<Real>()));

   template <class F, class Norm>
   static auto integrate_averaged(F f, const decltype(f(Real(0)))& zero, Norm norm,
                                  Real* error = nullptr, Real* pL1 = nullptr) -> decltype(f(Real(0)));
};

}}} // namespaces

Description

The gauss_hermite class template computes

∫-∞∞ f(x) e-x2 dx ≈ ∑i wi f(xi)

using Points evaluations of f, where the abscissas /xi/ are the zeros of the Hermite polynomial /HPoints. The rule is exact when /f is a polynomial of degree at most 2/Points/ - 1. Note that the weight function exp(-x2) is not part of f: pass only the factor that multiplies it.

Like gauss, this is a "one shot" non-adaptive routine, intended for smooth f that grow no faster than a polynomial, such as expectations of smooth functions of a normally distributed variable. For Points + 1 more evaluations of f, integrate_averaged is exact to higher degree and estimates its error.

static const RandomAccessContainer& abscissa();
static const RandomAccessContainer& weights();

These provide direct access to the abscissas and weights. Only the nonnegative abscissas are stored, in increasing order; the rule is symmetric about zero.

template <class F>
static auto integrate(F f, Real* pL1 = nullptr)->decltype(std::declval<F>()(std::declval<Real>()));

Integrates f(x) exp(-x2) over the real line with the Points-point Gauss rule G. f may return a real or complex value.

If pL1 is not null, *pL1 is set to the corresponding integral of |f|. If that is substantially larger than the absolute value of the result, then the sum was ill-conditioned.

template <class F, class Norm>
static auto integrate(F f, const decltype(f(Real(0)))& zero, Norm norm, Real* pL1 = nullptr) -> decltype(f(Real(0)));

As for gauss, this overload handles vector- and matrix-valued integrands: zero is the additive identity of the returned type, and norm returns a nonnegative scalar convertible to Real. The L1 norm is measured with norm.

template <class F>
static auto integrate_averaged(F f, Real* error = nullptr, Real* pL1 = nullptr)->decltype(std::declval<F>()(std::declval<Real>()));

template <class F, class Norm>
static auto integrate_averaged(F f, const decltype(f(Real(0)))& zero, Norm norm,
                               Real* error = nullptr, Real* pL1 = nullptr) -> decltype(f(Real(0)));

Returns Laurie's averaged rule (G(f) + A(f))2, at the cost of /Points + 1 more evaluations of f. A is the anti-Gauss rule: the rule with Points + 1 nodes whose error on every polynomial of degree up to 2/Points/ + 1 is exactly that of G with the opposite sign, so the average is exact for polynomials of degree 2/Points/ + 1. Its nodes interlace the Gauss nodes and its weights are positive; it is computed the first time it is needed, then cached.

If error is not null, *error is set to |A(f) - G(f)|/2, but never less than 2ε times the L1 norm. When G and A bracket the integral, as they do for smooth f once the error falls steadily with Points, this bounds the error of the average; it is then also within a few percent of the error of G. Otherwise it is a heuristic, and integrands that alias onto the null space of A - G can fool it. If pL1 is not null, *pL1 is set to the L1 norm of the averaged rule, measured with norm in the second overload.

Example

If X is normally distributed with mean μ and standard deviation σ, then substituting X = μ + √2 σ x gives

E[g(X)] = π-1/2 ∫-∞∞ g(μ + √2 σ x) e-x2 dx

so, for example, E[cos X] = exp(-σ2/2) cos μ is computed by

using boost::math::quadrature::gauss_hermite;
using boost::math::constants::root_pi;
using boost::math::constants::root_two;
double mu = 0.5;
double sigma = 2;
auto g = [&](double x) { return std::cos(mu + root_two<double>() * sigma * x); };
double error;
double expectation = gauss_hermite<double, 30>::integrate_averaged(g, &error) / root_pi<double>();

and error / root_pi<double>(), about 3e-16, estimates its error.

Choosing the number of points

gauss_hermite has pre-computed tables of abscissas and weights for 7, 10 and 15 points, at up to 110 decimal digits of precision. For other numbers of points, or higher precision types, the abscissas and weights are computed the first time they are needed and then cached. They are found in O(Points) operations by following the differential equation of the Hermite functions from zero to zero in twice the working precision (Glaser, Liu and Rokhlin), so there is no limit from the overflow of the polynomials, though the outer weights underflow; in double precision the abscissas are within an ulp and the weights within a few. If the rule cannot be computed, integrate and integrate_averaged raise an evaluation_error through the Policy; if the policy does not throw, the result is a NaN.

As with gauss, defining BOOST_MATH_GAUSS_NO_COMPUTE_ON_DEMAND turns any combination of type and number of points without pre-computed values into a compile-time error. The program gauss_hermite_constants.cpp generates the pre-computed tables, and is easily modified for other numbers of points.

References


PrevUpHomeNext