![]() |
Home | Libraries | People | FAQ | More |
#include <boost/math/quadrature/sparse_grid.hpp>
namespace boost{ namespace math{ namespace quadrature{ // Rule: leja_legendre<Real>, leja_hermite<Real>, leja_laguerre<Real> or leja_jacobi<Real>, Real = typename Rule::value_type; f(const std::vector<Real>& x), or f(x) with x a std::array<Real, N>, Eigen::VectorXd or other container with operator[] and size(). template <class F, class Rule> auto sparse_grid(F f, const Rule& rule, unsigned dimension, std::size_t max_calls = 100000, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr) -> decltype(f(x)); // Over the box [a[0], b[0]] x ... x [a[d-1], b[d-1]], d = a.size(), for leja_legendre and leja_jacobi; Box has operator[] and size(). template <class F, class Rule, class Box> auto sparse_grid(F f, const Rule& rule, const Box& a, const Box& b, std::size_t max_calls = 100000, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr) -> decltype(f(x)); }}} // namespaces
sparse_grid integrates f
over [-1, 1]d with leja_legendre,
or f(x) exp(-|x|2)
over the whole space with leja_hermite,
or against the product of the weights of leja_laguerre
or leja_jacobi, by the dimension-adaptive
sparse grid of Gerstner and Griebel. Coordinate i at level
ki uses the first 2ki - 1 points of
the rule, and the grid is the sum of the tensor products of differences of
consecutive levels over a set of multi-indices k. The
set grows where these contributions are largest, so the grid spends its points
on the coordinates, and the interactions between them, that matter. Exact integrals
of products of polynomials of degree 2ki - 2 need only
the multi-index k and those below it, and a function of
a few coordinates costs little more than in those dimensions.
The levels add two points at a time, not one: for an even f
such as x2 the rules with one and two points agree, and
a zero difference would stop the refinement of that coordinate. For the same
reason, when the error estimate (the sum of the norms of the contributions
not yet refined) falls below tolerance
* L1, every such contribution is refined once more, and
the result is accepted only if the estimate is still below the tolerance. Nothing
is accepted while L1 is 0, as it is while f
has been zero at every point: x12 x22,
with leja_legendre, is zero
at the first point, the mean of the weight, and on the axes through it. After
d + 1 such rounds of refinement with f
still zero everywhere, the result is 0 with an error estimate of 0. An interaction
of three or more variables that vanishes on the planes through the mean can
still be missed if f is not zero there, such as 1 + x12 x22 x32.
L1 is the grid's estimate of the integral of |f|
times the weight, the scale of the tolerance; before the grid has converged
it can be far off, even negative. It is not a condition number: the sparse
grid's weights have mixed signs, and the sum of |wi f(xi)|
can be much larger. If error
or L1 are not null they are
set to these. The grid computes the points of rule
it needs, and keeps them there for the next call.
max_calls bounds the number
of evaluations of f: a refinement that would exceed it
is not made, and the result has an error estimate above the tolerance. A contribution
that would need more points than that in a coordinate is not refined further,
and its norm stays in the error estimate; if the calls run out during the final
refinements the error estimate is NaN. No error is raised. A domain_error
is raised if dimension or
max_calls is zero, or if f
takes a fixed-size container whose size is not dimension.
f may return a real or complex value or a vector of them,
as for the one-dimensional rules; a vector is integrated componentwise, and
its error estimate and L1 use the max norm. With bounds
a and b,
each coordinate is mapped as integrate(f,
a[i], b[i])
maps it, and the result, error and L1 are scaled by the
product of the factors; a domain_error
is raised if a bound is infinite or NaN or b.size()
is not a.size().
The bookkeeping costs under 2 microseconds per evaluation of f in double precision on Genz's test integrands, more where the levels are high, so a sparse grid pays off when f costs more than that. On Genz's test integrands, with 104 calls or more, it beats quasi-Monte Carlo (Boost.Random's Sobol' sequence) by orders of magnitude up to four dimensions; in eight it still wins for entire functions such as cos(0.7 + ∑ /ci xi), but not for peaked ones such as Genz's product peak and Gaussian, and in sixteen quasi-Monte Carlo is better unless /f depends mostly on a few coordinates. For integrands with kinks, such as exp(-∑ ci|xi - ui|), quasi-Monte Carlo is better in any dimension, and the error estimate, a heuristic as for the one-dimensional rule, can be too small.