Multidimensional cubature
| Problem | Approximate over a box in dimensions |
| Devised by | Product rules: classical, after Gauss. Symmetric cubature: James Clerk Maxwell (1877), Arthur Stroud (1971). Adaptive degree-7 rule: Alan Genz and A. A. Malik (1980) |
| Accuracy | Tensor Gauss with points per axis: exact to degree in each variable. Genz-Malik: exact to total degree 7 on each region, with a degree-5 rule for the error estimate |
| Cost | Tensor rule: evaluations. Genz-Malik: per region |
| Read first | Gaussian quadrature, Adaptive Gauss-Kronrod quadrature |
| API | tensor_gauss_legendre, adaptive_cubature, CubatureOptions, CubatureResult |
Overview
Integrals over two, three or more variables arise everywhere: expected values of functions of several random variables, moments of mass distributions, flux through a surface, averages over a phase space. The rules of the previous entries generalise to several dimensions, but at a price that grows dramatically with the dimension, a phenomenon Richard Bellman named the curse of dimensionality.
This entry covers two ways to integrate over a box. The tensor-product Gauss rule applies a one-dimensional Gauss rule along every axis; it is simple and extremely accurate for smooth integrands in a few dimensions. Adaptive cubature with the Genz-Malik rule uses a cleverly symmetric set of points that grows much more slowly with the dimension, estimates its own error, and subdivides the box where the integrand is difficult. Beyond about ten dimensions, both give way to the Monte Carlo methods of the next entry.
Where it is used
Cubature computes integrals over regions in two to a few dimensions when exact answers do not exist.
- Engineering mechanics. Areas, volumes, centroids and moments of inertia of irregular parts.
- Probability. Probabilities of regions under a joint density, expected values of functions of several random variables.
- Electromagnetics and acoustics. Total power radiated through a surface, field integrals over apertures.
- Finite elements. Element stiffness matrices are integrals over triangles and tetrahedra, evaluated by small cubature rules.
History
Product rules are as old as Gaussian quadrature itself: integrating one variable at a time is the obvious extension. The first genuinely multidimensional rules were found by James Clerk Maxwell in 1877, who derived formulas for the square and the cube that use fewer points than products. The subject grew with the computer. Arthur Stroud’s Approximate Calculation of Multiple Integrals (1971) collected hundreds of rules for squares, cubes, spheres and simplices, and Sergei Smolyak (1963) showed how to combine one-dimensional rules into sparse grids that avoid most of the cost of full products.
Adaptivity arrived in 1980, when Alan Genz and A. A. Malik published a degree-7 rule for
-dimensional boxes with an embedded degree-5 rule for error estimation, together with a
heuristic for choosing the direction in which to split a region. Their algorithm, refined by
Berntsen, Espelid and Genz as DCUHRE (1991), is the basis of most adaptive cubature codes today,
including the cubature and HCubature packages and this library’s implementation.
The idea
Tensor-product rules and the curse of dimensionality
If is a one-dimensional Gauss rule with points on , the product rule
is exact whenever is a polynomial of degree at most in each variable separately, and for smooth it inherits the exponential convergence of the one-dimensional rule. It costs evaluations. Four points per axis, enough for degree 7, cost 64 evaluations in three dimensions and a million in ten.
The fundamental problem is not the product construction but the dimension itself. To reach an error with a rule of order in each direction needs about points per axis, so in total. Exponential growth in is unavoidable for any method that resolves the integrand along every axis.
Fully symmetric rules
A rule of total degree must integrate every monomial with exactly, not every monomial of degree in each variable. That is a much weaker requirement: there are such monomials, a polynomial in rather than an exponential. Symmetry reduces the work further. If the point set is invariant under permutations of the coordinates and changes of sign, every monomial with an odd exponent integrates to zero automatically, and monomials related by permuting the variables give the same equation.
Such a fully symmetric rule is built from a few generators, each expanded into all its signed permutations:
| generator | points | weight |
|---|---|---|
Requiring exactness for the even monomials up to degree 7 (, , , , , , ) gives nonlinear equations for the ’s and ’s. Genz and Malik solved them in closed form: , , , with weights that are rational functions of . The rule uses points in total; dropping the last generator and reweighting the rest gives an embedded degree-5 rule on the same points, and the difference of the two estimates the error.
Table 2.17. Points used by the Genz-Malik rule and a tensor Gauss rule by dimension.
| dimension | monomials of degree | Genz-Malik, degree 7 | tensor Gauss, 4 points per axis |
|---|---|---|---|
| 1 | 8 | 7 | 4 |
| 2 | 36 | 17 | 16 |
| 3 | 120 | 33 | 64 |
| 4 | 330 | 57 | 256 |
| 5 | 792 | 93 | 1024 |
| 6 | 1716 | 149 | 4096 |
| 8 | 6435 | 401 | 65536 |
| 10 | 19448 | 1245 | 1048576 |
| 15 | 170544 | 33249 | 1073741824 |
| 20 | 888030 | 1049417 | 1099511627776 |
The Genz-Malik rule needs fewer points than there are monomials to integrate, thanks to symmetry, and far fewer than the tensor rule from four dimensions on. Its corner points eventually dominate, and above about fifteen dimensions it is no longer practical.
Global adaptivity in several dimensions
The adaptive algorithm is the global strategy of one-dimensional Gauss-Kronrod quadrature: keep all regions in a priority queue keyed by their error estimates, and repeatedly bisect the worst one until the estimates sum to less than the tolerance. Only the direction of the cut is new. Splitting a box along every axis would multiply the number of regions by , so Genz and Malik cut along one axis, the one where the integrand is roughest. The rule’s own points measure this cheaply: the fourth divided difference
cancels the second-derivative term and estimates the fourth derivative along axis .
Figure 2.14. Adaptive cubature samples.
On a Gaussian peak the evaluation points pile up around the peak, and the narrow bands of points along the axes through it show the single-axis cuts.
Which method when?
The library’s results on two integrands make the choice clear. On a smooth integrand the tensor Gauss rule wins, in three dimensions and still in six:
Figure 2.15. Smooth integrand in 3 and 6 dimensions.
Two effects work against the adaptive rule here. Its degree-7 rule converges more slowly than a tensor Gauss rule of growing degree, and its error estimate, the difference from the degree-5 rule, is very pessimistic: at a requested relative tolerance of it already delivers about , so most of its work is spent on accuracy nobody asked for.
On a non-smooth integrand the picture reverses. The integral of over the unit square has an integrable singularity at the corner, which spoils the convergence of every polynomial-based rule. The tensor rule improves only algebraically, while the adaptive rule concentrates regions at the corner and converges rapidly:
Figure 2.16. Corner singularity.
In short: for smooth integrands in up to about five dimensions, use a tensor Gauss rule (and increase the points until the answer stops changing); for integrands with peaks, kinks or singularities, or when you need an error estimate, use adaptive cubature; beyond about ten dimensions, use quasi-Monte Carlo.
Algorithm
Genz-Malik rule on a box with centre and half-widths : evaluate at the centre, at and for each axis (recording the fourth difference), at for each pair , and at the points . Form the degree-7 and degree-5 sums, scaled by the volume; return the degree-7 value, the difference as the error, and the axis with the largest fourth difference.
Adaptive cubature:
- Apply the rule to the whole box; put the region in a priority queue keyed by its error.
- While the summed error exceeds and the budget allows two more regions: remove the worst region, halve it along its chosen axis, apply the rule to both halves, and insert them.
- Return the summed values and errors, and whether the tolerance was met.
Implementation
fn genz_malik<F: Fn(&[f64]) -> f64>(f: &F, centre: &[f64], half: &[f64]) -> (f64, f64, usize, usize) {
let n = centre.len();
let nf = n as f64;
let (l2, l3, l4, l5) = ((9.0f64 / 70.0).sqrt(), (9.0f64 / 10.0).sqrt(), (9.0f64 / 10.0).sqrt(), (9.0f64 / 19.0).sqrt());
let w = [
(12824.0 - 9120.0 * nf + 400.0 * nf * nf) / 19683.0,
980.0 / 6561.0,
(1820.0 - 400.0 * nf) / 19683.0,
200.0 / 19683.0,
6859.0 / 19683.0 / 2f64.powi(n as i32),
];
let w5 = [(729.0 - 950.0 * nf + 50.0 * nf * nf) / 729.0, 245.0 / 486.0, (265.0 - 100.0 * nf) / 1458.0, 25.0 / 729.0];
let mut x = centre.to_vec();
let mut evaluations = 0;
let mut eval = |x: &[f64]| {
evaluations += 1;
f(x)
};
let f0 = eval(&x);
let (mut s2, mut s3, mut s4, mut s5) = (0.0, 0.0, 0.0, 0.0);
let mut split_axis = 0;
let mut largest_difference = -1.0;
for i in 0..n {
let mut pair = |lambda: f64, x: &mut Vec<f64>| {
x[i] = centre[i] + lambda * half[i];
let a = eval(x);
x[i] = centre[i] - lambda * half[i];
let b = eval(x);
x[i] = centre[i];
a + b
};
let p2 = pair(l2, &mut x);
let p3 = pair(l3, &mut x);
s2 += p2;
s3 += p3;
// Fourth-difference estimate of roughness along this axis.
let difference = (p2 - 2.0 * f0 - (l2 * l2 / (l3 * l3)) * (p3 - 2.0 * f0)).abs();
if difference > largest_difference * (1.0 + 1e-12) {
largest_difference = difference;
split_axis = i;
}
}
for i in 0..n {
for j in i + 1..n {
for (si, sj) in [(1.0, 1.0), (1.0, -1.0), (-1.0, 1.0), (-1.0, -1.0)] {
x[i] = centre[i] + si * l4 * half[i];
x[j] = centre[j] + sj * l4 * half[j];
s4 += eval(&x);
}
x[i] = centre[i];
x[j] = centre[j];
}
}
for corner in 0..(1usize << n) {
for k in 0..n {
let sign = if corner >> k & 1 == 1 { 1.0 } else { -1.0 };
x[k] = centre[k] + sign * l5 * half[k];
}
s5 += eval(&x);
}
let volume: f64 = half.iter().map(|h| 2.0 * h).product();
let seventh = volume * (w[0] * f0 + w[1] * s2 + w[2] * s3 + w[3] * s4 + w[4] * s5);
let fifth = volume * (w5[0] * f0 + w5[1] * s2 + w5[2] * s3 + w5[3] * s4);
(seventh, (seventh - fifth).abs(), split_axis, evaluations)
}
- The five generator sums are accumulated while the points are visited, so each point is evaluated once and the degree-5 rule costs nothing extra.
- The corner points are enumerated by the bits of an integer, which is why the rule refuses more than 20 dimensions: corners per region is already a million evaluations.
tensor_gauss_legendrevisits its points with an “odometer” of indices, so it needs no recursion and no storage beyond one point.
In practice
//! Integrals over boxes in several dimensions: tensor Gauss rules and adaptive cubature.
//!
//! Run with `cargo run -p lib_jsl_integration --example quadrature_cubature`.
use lib_jsl_integration::cubature::{CubatureOptions, adaptive_cubature, tensor_gauss_legendre};
fn main() {
// A smooth integrand: ∫ e^{x+y+z} over the unit cube = (e − 1)³.
let exact = (std::f64::consts::E - 1.0).powi(3);
let smooth = |x: &[f64]| (x[0] + x[1] + x[2]).exp();
for points in [2, 4, 8] {
let v = tensor_gauss_legendre(smooth, &[0.0; 3], &[1.0; 3], points).unwrap();
println!("tensor Gauss, {points}³ = {:4} points: error {:.1e}", points * points * points, (v - exact).abs());
}
let r = adaptive_cubature(smooth, &[0.0; 3], &[1.0; 3], CubatureOptions::default()).unwrap();
println!("adaptive Genz-Malik: error {:.1e} (estimate {:.1e}) with {} evaluations", (r.value - exact).abs(), r.error, r.nfev);
// A corner singularity: ∫∫ 1/√(x² + y²) over the unit square = 2 ln(1 + √2).
let exact = 2.0 * (1.0 + 2f64.sqrt()).ln();
let singular = |x: &[f64]| 1.0 / (x[0] * x[0] + x[1] * x[1]).sqrt();
let v = tensor_gauss_legendre(singular, &[0.0; 2], &[1.0; 2], 300).unwrap();
println!("corner singularity, tensor Gauss with 90000 points: error {:.1e}", (v - exact).abs());
let options = CubatureOptions { absolute_tolerance: 0.0, relative_tolerance: 1e-9, max_evaluations: 1_000_000 };
let r = adaptive_cubature(singular, &[0.0; 2], &[1.0; 2], options).unwrap();
println!("corner singularity, adaptive: error {:.1e} with {} evaluations (converged: {})", (r.value - exact).abs(), r.nfev, r.converged);
}
Strengths
- Tensor Gauss: exponential convergence for smooth integrands in low dimensions, with no parameters beyond the number of points.
- Adaptive Genz-Malik: an error estimate and automatic refinement around peaks, kinks and singularities, with a cost per region that grows only polynomially (until takes over).
Weaknesses
- The curse of dimensionality: both methods become impractical beyond roughly ten dimensions.
- The adaptive rule’s error estimate is conservative, so on smooth integrands it does much more work than needed.
- Only boxes are supported. Other regions must be mapped to a box (for example with polar coordinates, whose Jacobian often removes a singularity, as in Exercise 3) or handled by multiplying the integrand by an indicator function, which creates a discontinuity.
Exercises
- Verify that the Genz-Malik rule is exact for but not for on .
- Count the monomials of total degree at most in variables and show the count is . How many of them must a fully symmetric rule actually satisfy for ?
- Compute by splitting the square along its diagonal and using polar coordinates on each triangle. Why does the singularity disappear?
- The volume of the unit ball in dimensions is . Estimate it with adaptive cubature applied to the indicator function on for , and comment on the convergence.
- Replace the error estimate by and repeat the smooth-integrand comparison. When is such a scaling safe?
Hints and answers
- The rule is exact for all polynomials of total degree at most 7. has degree 6, so it is integrated exactly; has degree 8 and the rule’s value differs from the exact .
- A monomial of total degree at most in variables corresponds to a choice of non-negative exponents (including a slack exponent) summing to : by stars and bars, . For a fully symmetric rule needs only the even monomials up to permutation of variables, which are : seven conditions.
- On the triangle , the integrand is cancelled by the Jacobian : . The two triangles give . The singularity is removed because it sits at the origin, which the Jacobian cancels.
- The exact values are , and . The indicator function has a jump along the sphere, so adaptive cubature converges only slowly, by repeatedly refining boxes along the boundary.
- Dividing by 100 assumes the degree-5 rule is far worse than the degree-7 rule and that their difference overestimates the error of the degree-7 value by a known factor. That is safe only once the integrand is well resolved; before that, the difference can be small by accident and the scaled estimate unreliable.
Further reading
- A. C. Genz and A. A. Malik, “An adaptive algorithm for numerical integration over an n-dimensional rectangular region”, Journal of Computational and Applied Mathematics 6 (1980) 295–302.
- J. Berntsen, T. O. Espelid and A. Genz, “An adaptive algorithm for the approximate calculation of multiple integrals”, ACM Transactions on Mathematical Software 17 (1991) 437–451.
- A. H. Stroud, Approximate Calculation of Multiple Integrals, Prentice-Hall, 1971.
- J. C. Maxwell, “On approximate multiple integration between limits by summation”, Cambridge Philosophical Society Proceedings 3 (1877) 39–47.
- S. A. Smolyak, “Quadrature and interpolation formulas for tensor products of certain classes of functions”, Soviet Mathematics Doklady 4 (1963) 240–243. The origin of sparse grids.
- R. Cools, “Constructing cubature formulae: the science behind the art”, Acta Numerica 6 (1997) 1–54.