Module: numx/interpolate.h | Category: Foundation | Phase: P1.22–P1.24
Constructs a smooth function that passes through a given set of data points
For
Reproduction order: exact for degree
Constructs a piecewise cubic
-
Interpolating:
$S(x_i) = y_i$ for all$i$ -
$C^2$ continuous: first and second derivatives match at every interior knot -
Natural boundary conditions:
$S''(x_0) = S''(x_{n-1}) = 0$
On each interval,
where
The moments
Solved by the Thomas algorithm (forward elimination + back substitution) in
Two-step API: numx_interp_spline_precompute solves for the moments once; numx_interp_spline_eval evaluates cheaply for multiple query points. The one-shot wrapper numx_interp_spline_cubic combines both.
Samples
The barycentric form of the interpolating polynomial avoids the Runge phenomenon and is numerically stable:
Exact for polynomials of degree
The implementation evaluates priv_cos(π/2 - \text{arg}) — no <math.h> dependency.
| Function | Time | Stack (float32, max defaults) |
|---|---|---|
numx_interp_linear |
|
negligible |
numx_interp_spline_precompute |
|
~2 KB (diag + rhs buffers) |
numx_interp_spline_eval |
negligible | |
numx_interp_chebyshev |
~2 KB (nodes + fvals buffers) |
-
Linear: lookup tables from calibration data;
$C^0$ is sufficient; minimal computation. - Cubic spline: smooth reconstruction of sensor trajectories, waveform shaping; precompute once, evaluate many times.
-
Chebyshev: approximating a known analytic function
$f$ on a fixed interval (e.g. replacingsinfon a constrained MCU with a degree-8 approximation).
- Cubic spline on non-monotone
$x$ data — requires sorted knots. - Chebyshev outside
$[a, b]$ — extrapolation is undefined; the function clips to boundary values. - Spline on $n > $
NUMX_MAX_INTERP_NODES— increase the config limit (affects stack).
- de Boor, C. — A Practical Guide to Splines, Revised ed., Springer, 2001.
- Berrut, J.-P. & Trefethen, L. N. — "Barycentric Lagrange interpolation", SIAM Review 46(3):501–517, 2004.
- Higham, N. J. — "The numerical stability of barycentric Lagrange interpolation", IMA Journal of Numerical Analysis 24(4):547–556, 2004.
#include "numx/interpolate.h"
/* Cubic spline through 5 calibration points — precompute once */
numx_real_t xs[] = {0.0f, 1.0f, 2.0f, 3.0f, 4.0f};
numx_real_t ys[] = {0.0f, 0.8f, 0.9f, 0.1f,-0.8f};
numx_real_t m[5]; /* moments — reuse across many queries */
numx_interp_spline_precompute(xs, ys, 5, m);
numx_real_t result;
numx_interp_spline_eval(xs, ys, m, 5, 1.5f, &result); /* smooth evaluation */
/* Chebyshev approximation of a custom function on [0, π] */
static numx_real_t my_f(numx_real_t x) { return x * x - x; }
numx_interp_chebyshev(my_f, 8, 0.0f, 3.14159f, 1.57f, &result);