|
limes 3.1.0
Composable Calculus Expressions for C++20
|
The limes::algorithms namespace provides composable building blocks for numerical integration: accumulators for precision control, quadrature rules for node/weight generation, and integrators that combine them.
Each layer is independent and composable via C++20 concepts.
Namespace: limes::algorithms::accumulators
Accumulators control how floating-point values are summed. Different strategies trade off speed vs. precision.
All accumulators satisfy the Accumulator<T> concept:
| Accumulator | Description | When to Use |
|---|---|---|
simple_accumulator<T> | Direct summation | Fast, low precision needs |
kahan_accumulator<T> | Kahan-Babuska compensation | General purpose |
neumaier_accumulator<T> | Improved Kahan | Mixed magnitude values |
klein_accumulator<T> | Second-order compensation | Extreme precision |
pairwise_accumulator<T, ChunkSize> | Divide-and-conquer | Many terms, cache-friendly |
any_accumulator<T> | Type-erased wrapper | Runtime polymorphism |
**simple_accumulator<T>**
Standard floating-point addition. Fast but accumulates rounding errors.
**kahan_accumulator<T>**
Compensated summation that tracks and corrects rounding errors. Provides ~2x the precision of simple summation.
Extra methods:
correction() — current error compensation term**neumaier_accumulator<T>**
Improved Kahan that handles the case where the running sum is smaller than the value being added.
**klein_accumulator<T>**
Second-order error compensation. Tracks corrections to the corrections.
Extra methods:
correction() — first-order correctionsecond_correction() — second-order correction**pairwise_accumulator<T, ChunkSize>**
Accumulates into chunks, then sums chunks pairwise. Good cache locality and O(log n) error growth.
Namespace: limes::algorithms::quadrature
Quadrature rules define where to sample a function and how to weight each sample. All rules work on the reference interval [-1, 1] and are scaled by integrators.
All rules satisfy the QuadratureRule<T> concept:
| Rule | Nodes | Exactness | Best For |
|---|---|---|---|
gauss_legendre<T, N> | N | Polynomials up to degree 2N-1 | Smooth functions |
gauss_kronrod_15<T> | 15 | Embedded 7-point for error | Adaptive integration |
clenshaw_curtis<T, N> | N | Uses Chebyshev nodes | Functions with endpoint issues |
simpson_rule<T> | 3 | Degree 3 polynomials | Simple, pedagogical |
trapezoidal_rule<T> | 2 | Degree 1 polynomials | Simple, periodic functions |
midpoint_rule<T> | 1 | Degree 1 polynomials | Simplest rule |
tanh_sinh_nodes<T> | Variable | Double exponential | Endpoint singularities |
**gauss_legendre<T, N>**
Optimal for smooth functions. N points integrate polynomials up to degree 2N-1 exactly.
Specialized implementations for N = 2, 3, 5 with precomputed high-precision nodes.
**gauss_kronrod_15<T>**
15-point rule with an embedded 7-point Gauss rule. The difference between them estimates error.
Extra members:
gauss_weights — weights for embedded 7-point rulegauss_indices — which nodes belong to embedded rule**clenshaw_curtis<T, N>**
Uses Chebyshev nodes (cosine-spaced). Good for functions that are well-approximated by polynomials. Includes endpoints.
**simpson_rule<T>**
Classic Simpson's 1/3 rule. 3 points at {-1, 0, 1} with weights {1/3, 4/3, 1/3}.
**trapezoidal_rule<T>**
2 points at endpoints. Exact for linear functions. Surprisingly good for periodic functions over a full period.
**midpoint_rule<T>**
Single point at center. Simplest possible rule.
**tanh_sinh_nodes<T>**
Double exponential (tanh-sinh) transformation. Generates nodes that cluster near endpoints, excellent for integrating functions with endpoint singularities.
Used internally by tanh_sinh_integrator.
Namespace: limes::algorithms
Integrators combine quadrature rules with accumulators to compute definite integrals.
All integrators return integration_result<T>:
Results support arithmetic for combining sub-integrals:
| Integrator | Method | Best For |
|---|---|---|
quadrature_integrator | Single rule or adaptive | General purpose |
romberg_integrator | Richardson extrapolation | Smooth functions |
tanh_sinh_integrator | Double exponential | Endpoint singularities, infinite intervals |
**quadrature_integrator<T, Rule, Acc>**
Combines any quadrature rule with any accumulator. Supports both single-pass and adaptive integration.
Adaptive mode uses recursive bisection with Richardson extrapolation.
**romberg_integrator<T, Acc>**
Romberg integration: repeatedly refines trapezoidal rule and extrapolates. Very efficient for smooth functions.
Uses Kahan accumulator by default. Converges rapidly (often 5-10 iterations) for analytic functions.
**tanh_sinh_integrator<T, Acc>**
Double exponential quadrature. Transforms the integral so that nodes cluster near endpoints, handling singularities gracefully.
Automatically detects and handles infinite bounds.
| Situation | Recommended |
|---|---|
| Smooth function, moderate precision | adaptive_integrator<T> |
| Smooth function, high precision | romberg_integrator<T> |
| Endpoint singularity (e.g., 1/√x) | tanh_sinh_integrator<T> |
| Infinite interval | tanh_sinh_integrator<T> |
| Known polynomial degree | gauss_legendre<T, N> with N > degree/2 |
| Periodic function over full period | trapezoidal_rule (surprisingly good) |
| Situation | Recommended |
|---|---|
| Speed critical, low precision OK | simple_accumulator |
| General purpose | kahan_accumulator |
| Mixed magnitude values | neumaier_accumulator |
| Extreme precision | klein_accumulator |
| Many terms (>1000) | pairwise_accumulator |