Interpolation
Interpolation constructs a function that passes through given data points, in order to evaluate it between them. It is how tables are read, how curves are drawn through measurements, how images are resized, and how many other numerical methods (quadrature, differentiation, the solution of differential equations) are built: almost every method in the integration chapter integrates an interpolant.
The central questions
Every interpolation problem raises three questions.
- Where are the data? On a line, on a grid, or scattered in space; equally spaced, or placed where you choose. Global polynomial interpolation is excellent at Chebyshev points and disastrous at equally spaced ones; tensor-product methods need a grid.
- How smooth is the function? For analytic functions, global methods (polynomial, rational, Fourier) converge exponentially. For functions with kinks, steep gradients or noise, local methods (splines, Hermite cubics) are safer.
- What must the interpolant preserve? Monotonicity (PCHIP), positivity, smoothness of derivatives (splines), periodicity (Fourier), or simply the visual impression (image kernels).
Figure 3.1. Interpolation of the Runge function at 13 points.
The Runge phenomenon above, the failure of polynomial interpolation at equally spaced points, is the single most important cautionary example in the subject.
Choosing a method
| Your data | Recommended | Entry |
|---|---|---|
| A function you can evaluate anywhere | Chebyshev series | Polynomial interpolation |
| Samples at Chebyshev or well-spread points | Barycentric polynomial interpolation | Polynomial interpolation |
| Samples at equally spaced or irregular points, smooth function | Cubic spline, or Floater-Hormann | Splines, Rational |
| A spline of another degree, or with not-a-knot, clamped, periodic or given-derivative ends | make_interp_spline | B-splines |
| Monotone data, or data with sudden changes | PCHIP or Akima | Splines |
| A function with poles or steep gradients nearby | AAA or Bulirsch-Stoer | Rational interpolation |
| A Taylor series to extend beyond its radius | Padé approximants | Rational interpolation |
| Periodic samples | Trigonometric interpolation | Trigonometric |
| Damped oscillations, to analyse or extrapolate | Prony’s method | Trigonometric |
| Noisy samples, to smooth or differentiate | Savitzky-Golay | Savitzky-Golay smoothing |
| Noisy samples, a smooth curve with automatic smoothing | Smoothing spline with GCV, or least-squares spline | B-splines |
| Values on a 2D, 3D or N-D grid | Bicubic, tricubic, B-spline or multilinear | Interpolation on grids |
| Scattered points in the plane or in space | Radial basis functions | Scattered data |
| Scattered points in the plane, piecewise linear or C¹; RBFs in any dimension | LinearNDInterpolator, CloughTocher2DInterpolator, griddata, RbfInterpolator | Piecewise polynomials and scattered interpolation |
| Values with derivatives; derivatives, integrals or roots of a piecewise interpolant | PPoly, KroghInterpolator, PPoly::cubic_hermite | Piecewise polynomials and scattered interpolation |
| A smooth surface on a rectangular grid, with derivatives and integrals | RectBivariateSpline | Piecewise polynomials and scattered interpolation |
Testing against SciPy
The references come from SciPy 1.17.1 and NumPy 2.4, written by book/generator/data/make_interpolation_refs.py on data that the generator rebuilds from the same formulas. Each row gives the largest difference between the library’s interpolant and SciPy’s, at 40 to 100 evaluation points:
Table 3.1. Interpolants against SciPy.
| interpolant (SciPy reference) | largest difference |
|---|---|
natural cubic spline, a smooth curve (CubicSpline, natural) | 2e-15 |
PCHIP, a smooth curve (PchipInterpolator) | 2e-15 |
Akima, a smooth curve (Akima1DInterpolator) | 2e-15 |
linear, a smooth curve (numpy.interp) | 2e-15 |
natural cubic spline, data with flat stretches (CubicSpline, natural) | 5e-15 |
PCHIP, data with flat stretches (PchipInterpolator) | 5e-15 |
Akima, data with flat stretches (Akima1DInterpolator) | 6e-15 |
linear, data with flat stretches (numpy.interp) | 5e-15 |
polynomial through 9 Chebyshev points (BarycentricInterpolator) | 3e-15 |
Floater-Hormann, 21 equispaced points, d = 3 (FloaterHormannInterpolator) | 8e-16 |
AAA rational approximation of tan(pi x / 2.2) (AAA) | 1e-15 (relative); each within 5e-16 of the function |
[4/4] Pade approximant of exp, coefficients (pade) | numerator 2e-15, denominator 2e-15 |
Chebyshev coefficients of exp, 12 terms (numpy.polynomial.chebyshev.chebinterpolate) | 1e-15 |
Savitzky-Golay coefficients, 11 points, cubic, derivative 0 (savgol_coeffs) | 8e-16 |
Savitzky-Golay filter, derivative 0, with end fitting (savgol_filter, interp) | 4e-15 |
Savitzky-Golay coefficients, 11 points, cubic, derivative 1 (savgol_coeffs) | 1e-15 |
Savitzky-Golay filter, derivative 1, with end fitting (savgol_filter, interp) | 4e-15 |
trigonometric interpolation, 15 samples (signal.resample) | 2e-15 |
trigonometric interpolation, 16 samples (signal.resample) | 2e-15 |
bilinear on an uneven grid (RegularGridInterpolator, linear) | 2e-16 |
nearest neighbour on an uneven grid (RegularGridInterpolator, nearest) | 0 |
trilinear (RegularGridInterpolator, linear) | 3e-16 |
n-dimensional multilinear, in 3D (RegularGridInterpolator, linear) | 3e-16 |
tricubic natural B-spline (CubicSpline, natural, along each axis) | 9e-16 |
radial basis functions, thin plate spline kernel, 30 scattered points (RBFInterpolator) | 5e-15 |
radial basis functions, cubic kernel, 30 scattered points (RBFInterpolator) | 8e-14 |
radial basis functions, gaussian kernel, 30 scattered points (RBFInterpolator) | 7e-9 |
radial basis functions, multiquadric kernel, 30 scattered points (RBFInterpolator) | 1e-9 |
radial basis functions, inverse multiquadric kernel, 30 scattered points (RBFInterpolator) | 8e-11 |
The interpolants agree to rounding error. The radial basis functions with a shape parameter (Gaussian, multiquadric) differ more because their interpolation systems are ill-conditioned (condition numbers of to here, against for the cubic kernel), so each implementation’s rounding is amplified; both still reproduce the data. The library’s bicubic interpolant, whose node derivatives are finite-difference estimates, has no SciPy counterpart and is not listed.
In the library
The interpolation crate is lib_jsl_interpolation
(API reference). The one-dimensional interpolators of data
implement the InterpolationTrait,
whose interpolate_at method locates the interval containing the point (by bisection, remembering the
last interval) and evaluates the method’s formula there; the multidimensional interpolators implement
Interpolation2D,
Interpolation3D or
InterpolationND.