interpolate::integrate
Integration: rules for sampled data, adaptive Gauss-Kronrod, Romberg and Gauss-Legendre for functions, and nested double and triple integrals.
Structs
QuadSource
pub struct QuadThe result of an adaptive integration: the estimate, an estimate of its absolute error, the integrand evaluations it took and the subintervals it ended with.
let q = try interpolate::quad(|x: f64| -> f64 { x * x }, 0.0..3.0);
assert((q.value - 9.0).abs() <= q.error && q.error < 1e-12);
assert(q.evals == 21 && q.intervals == 1);
The integral.
An estimate of
|value - exact|.Integrand evaluations.
Subintervals at the end.
Functions
quadSource
pub fn quad(f: fn(f64) -> f64, r: Range<f64>, epsabs: f64 = 1.49e-8, epsrel: f64 = 1.49e-8, limit: usize = 50) throws (InterpError) -> Quad The integral of f over r by adaptive Gauss-Kronrod quadrature
with extrapolation (QUADPACK's QAGS; QAGI when an end is infinite,
f64::INFINITY / f64::NEG_INFINITY). It stops when the error
estimate is at most max(epsabs, epsrel |value|), after at most
limit bisections. Endpoint singularities such as x^-1/2 or
ln x and integrable interior ones near a bisection point are
handled; for a known interior trouble spot, split the range there.
r.start > r.end gives the negative. Throws NoConvergence (with
the best estimate) when the tolerance is not met: the limit is
reached, roundoff stops progress, the integrand misbehaves or the
integral seems to diverge; NotFinite when f gives NaN or an
infinity.
let q = try interpolate::quad(|x: f64| -> f64 { 1.0 / x.sqrt() }, 0.0..1.0);
assert((q.value - 2.0).abs() <= q.error && q.error < 1e-10);
let g = try interpolate::quad(|x: f64| -> f64 { (-x * x).exp() }, f64::NEG_INFINITY..f64::INFINITY);
assert((g.value - f64::PI.sqrt()).abs() < 1e-12);
fixed_quadSource
pub fn fixed_quad(f: fn(f64) -> f64, r: Range<f64>, n: usize = 5) throws (InterpError) -> f64 The integral of f over the finite range r by the n-point
Gauss-Legendre rule (default 5): exact for polynomials of degree up
to 2n - 1, n evaluations, no error estimate.
let v = try interpolate::fixed_quad(|x: f64| -> f64 { x ** 5 }, 0.0..2.0, 3);
assert((v - 64.0 / 6.0).abs() < 1e-13);
gauss_legendreSource
pub fn gauss_legendre(n: usize) throws (InterpError) -> (Vector<f64>, Vector<f64>) The nodes and weights of the n-point Gauss-Legendre rule on [-1, 1], nodes rising. It integrates polynomials of degree up to 2n - 1 exactly.
let (x, w) = try interpolate::gauss_legendre(2);
assert((x[1] - 1.0 / 3.0.sqrt()).abs() < 1e-15 && (w[0] - 1.0).abs() < 1e-15);
dblquadSource
pub fn dblquad(f: fn(f64, f64) -> f64, rx: Range<f64>, ry: Range<f64>, epsabs: f64 = 1.49e-8, epsrel: f64 = 1.49e-8) throws (InterpError) -> Quad The integral of f(x, y) over the rectangle rx x ry (ends may
be infinite), x outer: quad over x of quad over y. The
error adds the outer estimate and the largest inner one times the
width of rx; evals counts calls of f.
let q = try interpolate::dblquad(|x: f64, y: f64| -> f64 { x * y * y }, 0.0..2.0, 0.0..1.0);
assert((q.value - 2.0 / 3.0).abs() < 1e-14);
dblquad_overSource
pub fn dblquad_over(f: fn(f64, f64) -> f64, rx: Range<f64>, ry: fn(f64) -> Range<f64>, epsabs: f64 = 1.49e-8, epsrel: f64 = 1.49e-8) throws (InterpError) -> Quad The integral of f(x, y) over x in rx and y in ry(x), inner
limits as a function of x. See dblquad.
use core::range::Range;
// The triangle 0 <= y <= x <= 1: area 1/2.
let q = try interpolate::dblquad_over(|x: f64, y: f64| -> f64 { 1.0 }, 0.0..1.0, |x: f64| -> Range<f64> { 0.0..x });
assert((q.value - 0.5).abs() < 1e-14);
tplquadSource
pub fn tplquad(f: fn(f64, f64, f64) -> f64, rx: Range<f64>, ry: Range<f64>, rz: Range<f64>, epsabs: f64 = 1.49e-8, epsrel: f64 = 1.49e-8) throws (InterpError) -> Quad The integral of f(x, y, z) over the box rx x ry x rz, nested
as dblquad.
let q = try interpolate::tplquad(|x: f64, y: f64, z: f64| -> f64 { x + y + z }, 0.0..1.0, 0.0..1.0, 0.0..1.0);
assert((q.value - 1.5).abs() < 1e-13);
tplquad_overSource
pub fn tplquad_over(f: fn(f64, f64, f64) -> f64, rx: Range<f64>, ry: fn(f64) -> Range<f64>, rz: fn(f64, f64) -> Range<f64>, epsabs: f64 = 1.49e-8, epsrel: f64 = 1.49e-8) throws (InterpError) -> Quad The integral of f(x, y, z) over x in rx, y in ry(x) and
z in rz(x, y). See tplquad.
use core::range::Range;
// The unit simplex x, y, z >= 0, x + y + z <= 1: volume 1/6.
let q = try interpolate::tplquad_over(|x: f64, y: f64, z: f64| -> f64 { 1.0 }, 0.0..1.0,
|x: f64| -> Range<f64> { 0.0..1.0 - x }, |x: f64, y: f64| -> Range<f64> { 0.0..1.0 - x - y });
assert((q.value - 1.0 / 6.0).abs() < 1e-13);
rombergSource
pub fn romberg(f: fn(f64) -> f64, r: Range<f64>, tol: f64 = 1.48e-8, rtol: f64 = 1.48e-8, divmax: usize = 10) throws (InterpError) -> Quad The integral of f over the finite range r by Romberg's method:
trapezoid sums with 2^k intervals, k up to divmax, and Richardson
extrapolation to O(h^(2k + 2)). It stops when two diagonal entries
of the table differ by at most max(tol, rtol |value|); that
difference is the error estimate. Best for smooth integrands; use
quad for singular or oscillatory ones and infinite ranges. Throws
NoConvergence after divmax halvings.
let q = try interpolate::romberg(f64::exp, 0.0..1.0);
assert((q.value - (f64::E - 1.0)).abs() < 1e-12);
cumulative_trapezoidSource
pub fn cumulative_trapezoid(y: Vector<f64>, x: Vector<f64> | f64 = 1.0, initial: f64? = none) throws (InterpError) -> Vector<f64> The running trapezoid integral: element i is the integral from
x[0] to x[i + 1] (n - 1 values), or with initial given, from
x[0] to x[i] plus initial (n values, the first initial).
Spacing as trapezoid. O(n).
use core::array::Vector;
let y: Vector<f64> = [1, 1, 1];
let c = try interpolate::cumulative_trapezoid(y, 0.5);
assert(c.len() == 2 && c[1] == 1.0);
let d = try interpolate::cumulative_trapezoid(y, initial: 0.0);
assert(d.len() == 3 && d[0] == 0.0 && d[2] == 2.0);
rombSource
pub fn romb<Y: AsVector<f64>>(y: Y, dx: f64 = 1.0) throws (InterpError) -> f64 Romberg integration of 2^k + 1 uniform samples with step dx:
trapezoid sums on the nested grids, extrapolated by Richardson's
table to O(h^(2k + 2)). Exact for polynomials of degree 2k + 1. One
sample gives 0. O(n).
use core::array::Vector;
let y: Vector<f64> = [0, 1, 16, 81, 256]; // x^4 on 0..4
assert((try interpolate::romb(y) - 204.8).abs() < 1e-12);
simpsonSource
pub fn simpson(y: Vector<f64>, x: Vector<f64> | f64 = 1.0) throws (InterpError) -> f64 Composite Simpson's rule for samples y at positions x or with a
uniform step dx (default 1). An odd sample count uses Simpson on
every pair of intervals: exact for cubics on a uniform grid, for
quadratics on any grid, error O(h^4 f''''). An even count does the
same on the first n - 2 intervals and integrates the parabola
through the last three samples over the last interval (Cartwright's
correction, as SciPy). Two samples give the trapezoid, fewer 0. O(n).
use core::array::Vector;
let y: Vector<f64> = [0, 1, 8, 27, 64]; // x^3 on 0..4
assert(try interpolate::simpson(y) == 64.0);
let x: Vector<f64> = [0, 1, 3];
let q: Vector<f64> = [0, 1, 9]; // x^2
assert((try interpolate::simpson(q, x) - 9.0).abs() < 1e-14);
trapezoidSource
pub fn trapezoid(y: Vector<f64>, x: Vector<f64> | f64 = 1.0) throws (InterpError) -> f64 The trapezoid rule for samples y at positions x (a vector of
the same length, any order: a falling x gives the negative), or a
uniform step dx (default 1). Exact for straight lines; error
O(h^2 f''). Fewer than two samples give 0. O(n).
use core::array::Vector;
let y: Vector<f64> = [0, 1, 4, 9];
assert(try interpolate::trapezoid(y) == 9.5);
let x: Vector<f64> = [0, 1, 3, 4];
assert(try interpolate::trapezoid(y, x) == 0.5 + 5.0 + 6.5);
assert(try interpolate::trapezoid(y, 0.5) == 4.75);