Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Leja quadrature

Synopsis

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

namespace boost{ namespace math{ namespace quadrature{

template <class Real, class Policy = boost::math::policies::policy<> >
class leja_legendre  // the integral of f over [-1, 1]
{
public:
   leja_legendre();

   const std::vector<Real>& abscissa() const;
   const std::vector<Real>& weights(unsigned n) const;
   static constexpr unsigned max_points();

   template <class F>
   auto integrate(F f, Real tolerance = tools::root_epsilon<Real>(),
                  Real* error = nullptr, Real* L1 = nullptr) const -> decltype(f(Real(0)));

   // The integral over [a, b]; leja_legendre and leja_jacobi only.
   template <class F>
   auto integrate(F f, Real a, Real b, Real tolerance = tools::root_epsilon<Real>(),
                  Real* error = nullptr, Real* L1 = nullptr) const -> decltype(f(Real(0)));
};

template <class Real, class Policy = boost::math::policies::policy<> >
class leja_hermite;  // the integral of f(x) exp(-x[super 2]) over the real line, with the same members except integrate(f, a, b)

// The integral of f(x) x[super alpha] exp(-x) over \[0, [infin]), alpha > -1; as leja_hermite, with the constructor
//    explicit leja_laguerre(Real alpha = 0);
template <class Real, class Policy = boost::math::policies::policy<> >
class leja_laguerre;

// The integral of f(x) (1 - x)[super alpha] (1 + x)[super beta] over \[-1, 1\], alpha, beta > -1, with the constructor
//    leja_jacobi(Real alpha, Real beta);
template <class Real, class Policy = boost::math::policies::policy<> >
class leja_jacobi;

}}} // namespaces

Description

A weighted Leja sequence is built greedily: each new point maximizes √w times the product of its distances to the points already chosen (Leja; Narayan and Jakeman). leja_legendre has the Legendre weight w(x) = 1 on [-1, 1], which gives the classical Leja points, here in the order 0, -1, 1; leja_hermite has w(x) = exp(-x2), leja_laguerre w(x) = xα exp(-x) on [0, ∞), and leja_jacobi w(x) = (1 - x)α (1 + x)β on [-1, 1]. All start from the mean of w, so that the one-point rule is the one-point Gauss rule, exact to degree 1, the natural first level of a sparse grid. Where an exponent is negative, √w has no maximum, and the points are chosen as if it were 0; the weights are those of w. leja_jacobi with α = β = 0 has the points of leja_legendre. The constructors raise a domain_error unless the exponents are > -1. The n-point rule uses the first n points and has the weights that make it exact for polynomials of degree n - 1.

The rules are nested: going from n to n + 1 points costs one more evaluation of f, where a Gauss rule must start again. This makes them the usual building block of sparse grids, and lets the adaptive integrate add one point at a time. The price is degree: a Leja rule with n points is exact to degree n - 1, a Gauss rule to 2n - 1.

The points and weights are computed when they are first needed, up to max_points(): 1023 for the built-in floating point types and 127 for others. weights(n) returns the n weights of the n-point rule, computing it and any smaller ones first, and raises a domain_error unless 1 ≤ n ≤ max_points(). abscissa() returns the points computed so far, in the order they were chosen. All members are const and safe to call from several threads, which share what any of them computes, as tanh_sinh does; points that another thread is computing read as NaN until it is done. example/leja_parallel_assembly.cpp assembles the Hamiltonian of the anharmonic oscillator x4 in 60 Hermite functions on all cores with one leja_legendre, in agreement with the exact entries to 2[times]10-15 times the largest: 0.21 s on one core of an Apple M-series and 0.026 s on ten, once the rule has its points. leja_hermite suits expectations over a normal distribution, such as the mean of the logistic function of X ~ N(1, 1):

leja_hermite<double> rule;
double error;
double mean = rule.integrate([](double x) { return 1 / (1 + std::exp(-1 - std::sqrt(2.0) * x)); }, 1e-10, &error)
            / std::sqrt(boost::math::constants::pi<double>());

takes 105 points for 0.69673467014367874, within 4.6[times]10-15 of the true value; the estimate of its error, error divided by √π, is 5.6[times]10-12.

When several points maximize the product to within rounding, the leftmost is taken; in the tests float, double, long double and cpp_bin_float_50 choose the same points. Some weights are negative, but not by much: up to 100 points the sum of |wi| is at most 1.32 times the total mass for the exponents tried, but 2.44 times for α = -0.9, β = 1/2.

integrate(f, tolerance, error, L1) evaluates f once at each point in turn, until n ≥ 4 and |Qm - Qm - max(1, m/4)| ≤ tolerance * L1 for every m from n - n/4 to n, where Qn is the n-point rule and L1 = ∑ |wi| |f(xi)|. If error or L1 are not null they are set to the largest of those differences, a heuristic estimate of the error, and to L1. If max_points() is reached first, its result is returned with its error estimate, as tanh_sinh does; no error is raised. f may return a real or complex value, or a vector of them: any type with operator[] and size(), such as std::array or std::vector, whose values all have the same size. A vector is integrated componentwise, and |y| above is its max norm. leja_legendre evaluates f at the endpoints ±1, and leja_laguerre and leja_jacobi at an end of the support where the exponent is ≤ 0.

integrate(f, a, b, tolerance, error, L1) integrates f over [a, b] with leja_legendre, and f(x) (b - x)α (x - a)β with leja_jacobi, by the affine map from [-1, 1], which also scales the error and L1. If a > b the result is minus the integral over [b, a], with α still at b. Infinite or NaN bounds raise a domain_error.

Accuracy and cost

The rules up to N points take O(N2) operations: about 0.3 ms for 64 points, 1 ms for 128, 16 ms for 512 and 60 ms for 1023 in double precision, and 0.4 s for 127 in cpp_bin_float_50. Each rule follows from the last in Newton form, with the integral of the nodal polynomial from a recurrence carried to twice the working precision: the rules integrate xk to within 20 n ε ∑ |wi xik|, and in double precision the weights are within an ulp of the largest weight of the same rules in 50 digits, up to 1023 points. A bounded sum of |wi| does not help integrands that grow like exp(x2) against leja_hermite, such as products of Hermite polynomials of high degree: L1 is 1019 times the integral of h402. Integrate such products with their Gaussian over a finite interval instead, as the example does.

References


PrevUpHomeNext