Tanh-sinh quadrature
| Problem | Approximate when may be singular, or infinitely steep, at the endpoints |
| Devised by | Hidetosi Takahasi and Masatake Mori (1974) |
| Accuracy | Error with evaluations, for analytic inside the interval, even with integrable endpoint singularities |
| Cost | evaluations; refinement halves the step and reuses every previous evaluation |
| Read first | Newton-Cotes rules: midpoint, trapezoid and Simpson, Preliminaries: complex numbers |
| API | TanhSinhEngine, integrate_with_error, integrate_with_complement |
Overview
Integrals such as , or have integrands that blow up at an endpoint. Polynomial-based rules converge painfully slowly on them, because no polynomial resembles near . Tanh-sinh quadrature changes variables so that the singularity disappears into an infinitely distant, exponentially flat tail, then applies the humble trapezoid rule. The result converges almost exponentially, whether or not the integrand is singular at the ends.
Tanh-sinh is the method of choice for endpoint singularities, for integrands whose behaviour at the endpoints is unknown, and for very high precision computation. It is the default quadrature in several arbitrary-precision systems.
Where it is used
Tanh-sinh quadrature is robust where other rules struggle.
- Endpoint singularities. Integrals like or that appear in physics formulas and probability.
- High-precision computation. Experimental mathematics (identifying constants by their numerical values) relies on it.
- Black-box use. It needs no knowledge of where the integrand misbehaves, only that it is analytic inside the interval.
History
Hidetosi Takahasi and Masatake Mori introduced the double exponential (DE) transformation in 1974, in “Double exponential formulas for numerical integration”. They were studying why the trapezoid rule is so accurate for some integrals over the whole real line, and they asked what change of variables would make a finite-interval integral look like the best possible such integral. The answer, a transformation whose derivative decays like , gave its name to a family of methods, and Mori later wrote a historical account of the discovery (2005).
In 1997 Masaaki Sugihara proved that DE formulas are optimal, in a precise sense, for broad classes of analytic functions. In the 2000s David Bailey, Jonathan Borwein and collaborators made tanh-sinh the workhorse of experimental mathematics, where integrals are computed to hundreds or thousands of digits to discover exact identities.
The idea
The trapezoid rule on the real line
For a function analytic in the strip and decaying along the real axis, the trapezoid rule
has a discretisation error that decays like , exponentially in . This follows from the Poisson summation formula: the error consists of the Fourier transform of evaluated at multiples of , and analyticity in a strip makes that transform decay exponentially. In practice the sum must also be truncated to , which adds an error of the size of the neglected tail. The faster decays, the fewer terms are needed.
Moving a finite interval to the real line
To use this, map to with
so that
As , the weight decays like , a double exponential. An endpoint singularity such as grows only like a single exponential in , so the product still decays double exponentially. The singularity has been neutralised.
Figure 2.11. The transformed integrand decays double exponentially.
The figure shows the transformed integrand for , and on . All three fall below by . The singular integrands decay more slowly on the singular side, but still double exponentially.
Balancing the two errors
With step and points, the discretisation error is about ; the strip half-width is for the tanh-sinh map. The truncation error is about . Balancing the two gives , and the total error for evaluations behaves like
That is almost exponential, and remarkably it does not care whether is singular at the endpoints, provided is analytic inside the interval. The clustering is extreme: at the node lies within about of the endpoint, and at within about .
Nested refinement
Halving keeps all existing points and adds the odd multiples of . As with Romberg integration, every function value is reused, and the change between successive levels serves as an error estimate.
Algorithm
For , with :
- Start with and sum the contributions at .
- Halve and add the contributions at the new points .
- The estimate is . Stop when two successive levels agree to the tolerance.
Each node is formed from its distance to the nearer endpoint, , rather than from itself. Near the endpoint, computed directly would round to zero long before the true distance stops being representable, and the nodes that capture the singular tail would be lost.
Implementation
The contribution of each symmetric pair of nodes :
// Contribution of the symmetric pair ±t, scaled by the weight.
let pair = |t: f64| -> f64 {
let u = FRAC_PI_2 * t.sinh();
let cosh_u = u.cosh();
let weight = FRAC_PI_2 * t.cosh() / (cosh_u * cosh_u);
if weight == 0.0 || !weight.is_finite() {
return 0.0;
}
let offset = width / (1.0 + (2.0 * u).exp());
if offset == 0.0 {
return 0.0;
}
let mut s = f(a + offset, offset) + f(b - offset, -offset);
if t == 0.0 {
s *= 0.5;
}
weight * s
};
and the refinement loop, which adds only the odd multiples of each new step:
let mut h = 1.0;
let mut sum: f64 = (0..=(T_MAX as usize)).map(|k| pair(k as f64)).sum();
let mut estimate = 0.5 * width * h * sum;
let mut error = f64::INFINITY;
for _ in 0..self.max_levels {
h *= 0.5;
// New points are the odd multiples of the halved step.
let count = (T_MAX / h) as usize;
let new: f64 = (1..=count).step_by(2).map(|k| pair(k as f64 * h)).sum();
sum += new;
let next = 0.5 * width * h * sum;
error = (next - estimate).abs();
estimate = next;
if error <= self.tolerance * estimate.abs() {
break;
}
}
Numerical details that matter:
- Endpoint offsets.
offset = width / (1 + e^{2u})is accurate to full relative precision even when it is , so the nodes really do approach the singularity. The sum runs to , where the offsets reach the bottom of the double-precision range. - Points that round onto an endpoint. For an interval such as ,
rounds to long before the offset underflows, and may be infinite.
integrate_with_errorskips such points, which loses a tiny tail.integrate_with_complementpasses the exact offset to the integrand as a second argument, so that it can evaluate as without cancellation. - Weights that underflow are skipped rather than multiplied, avoiding .
In practice
Tanh-sinh compared with a 100-point Gauss-Legendre rule on singular integrands over :
Table 2.13. Tanh-sinh quadrature on singular integrands.
| integrand on | exact | tanh-sinh error | evaluations | Gauss-Legendre (100 pts) error |
|---|---|---|---|---|
| -1 | 0.0e0 | 75 | 6.3e-5 | |
| 2 | 0.0e0 | 75 | 8.7e-3 | |
| 0.6666666666666666 | 0.0e0 | 75 | 1.0e-7 | |
| 10 | 3.6e-15 | 75 | 3.5e0 |
With fewer evaluations, tanh-sinh reaches full double precision on every case, while Gauss struggles, and fails completely on the nearly non-integrable .
A complete program, including the complement form:
//! Tanh-sinh quadrature on integrands with endpoint singularities.
//!
//! Run with `cargo run -p lib_jsl_integration --example quadrature_tanh_sinh`.
use lib_jsl_integration::tanh_sinh::TanhSinhEngine;
use std::f64::consts::PI;
/// An integrand on [0, 1] with its name and exact value.
type Case = (&'static str, fn(f64) -> f64, f64);
fn main() {
let engine = TanhSinhEngine::new(0.0, 1.0);
let cases: [Case; 3] = [
("ln x", f64::ln, -1.0),
("1/√x", |x| 1.0 / x.sqrt(), 2.0),
("ln(x) ln(1 − x)", |x| x.ln() * (-x).ln_1p(), 2.0 - PI * PI / 6.0),
];
for (name, f, exact) in cases {
let (value, estimate) = engine.integrate_with_error(f).unwrap();
println!("∫₀¹ {name:16} ≈ {value:+.15} error {:.1e} (estimate {estimate:.1e})", (value - exact).abs());
}
// A singularity at an endpoint away from zero: 1/√(1 − x²) on [−1, 1]. Computing 1 − x
// near x = 1 cancels catastrophically; the complement form passes the exact offset instead.
let (value, _) = TanhSinhEngine::new(-1.0, 1.0)
.integrate_with_complement(|x, offset| {
let (one_minus, one_plus) = if offset < 0.0 { (-offset, 1.0 + x) } else { (1.0 - x, offset) };
1.0 / (one_minus * one_plus).sqrt()
})
.unwrap();
println!("∫₋₁¹ dx/√(1 − x²) ≈ {value:.15} error {:.1e}", (value - PI).abs());
}
Strengths
- Near-exponential convergence regardless of endpoint singularities, so it is robust when the endpoint behaviour is unknown.
- Nested refinement with every evaluation reused, and a simple error estimate.
- Easy to implement at any precision; the nodes and weights are elementary functions.
Weaknesses
- Interior singularities, kinks and discontinuities are not helped. Split the interval at them so that they become endpoints.
- For smooth integrands Gaussian quadrature needs fewer evaluations; compare the smooth convergence figure.
- Singularities at endpoints away from zero need the complement form to avoid cancellation in .
- Poles or near-singularities just outside the interval, in the complex plane near it, shrink the strip of analyticity and slow convergence.
Exercises
- Show that maps onto , and derive . How close to is ?
- For on , write down for large and show that it decays double exponentially.
- Replace by the single exponential map . Implement the resulting rule and compare its convergence with tanh-sinh on .
- Compute with the library. Which endpoint is singular, and what goes wrong if the integrand is evaluated naively at points near ?
- Compute first over , then split at . Explain the difference.
Hints and answers
- maps onto and maps it onto , so is onto . . For : .
- For large , and . Then , a double exponential decay, since itself grows like .
- With the weight decays only like , so the singularity at the endpoint is tamed only to a single exponential, and many more points are needed for the same accuracy than with tanh-sinh.
- The singular endpoint is (the logarithm). At the integrand is finite (its limit is ), but the quotient is approached: for within rounding of 1 the subtraction loses all its digits. A robust implementation uses the distance to the endpoint, which the transformed variable gives directly and accurately, instead of forming .
- The singularity at is interior, so tanh-sinh’s endpoint clustering does not help there. Splitting at turns it into two endpoint singularities, which tanh-sinh handles easily.
Further reading
- H. Takahasi and M. Mori, “Double exponential formulas for numerical integration”, Publications of the Research Institute for Mathematical Sciences 9 (1974) 721–741.
- M. Mori, “Discovery of the double exponential transformation and its developments”, Publications of the Research Institute for Mathematical Sciences 41 (2005) 897–935. The inventor’s own account.
- M. Sugihara, “Optimality of the double exponential formula: functional analysis approach”, Numerische Mathematik 75 (1997) 379–395.
- D. H. Bailey, K. Jeyabalan and X. S. Li, “A comparison of three high-precision quadrature schemes”, Experimental Mathematics 14 (2005) 317–329.
- L. N. Trefethen and J. A. C. Weideman, “The exponentially convergent trapezoidal rule”, SIAM Review 56 (2014) 385–458. The trapezoid rule on the real line and in a strip.