Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.


Interpolation Algorithms

Interpolation modules

Module Interpolation kind
mir.interpolate.constant Constant Interpolant
mir.interpolate.generic Generic Piecewise Interpolant
mir.interpolate.linear Linear Interpolant
mir.interpolate.polynomial Lagrange Barycentric Interpolant
mir.interpolate.spline Piecewise Cubic Hermite Interpolant Spline: C2 (with contiguous second derivative), cardinal, monotone (aka PCHIP), double-quadratic, Akima
Ilya Yaroshenko
template findInterval(size_t dimension = 0)
Interval index for x value given.
import mir.ndslice.allocation: rcslice;
import mir.ndslice.topology: as;
import mir.ndslice.slice: sliced;
import mir.interpolate.linear;

static immutable x = [0.0, 1, 2];
static immutable y = [10.0, 2, 4];
auto interpolation = linear!double(x.rcslice,!(const double).rcslice);
assert(interpolation.findInterval(1.0) == 1);
@trusted size_t findInterval(Interpolant, X)(auto ref const Interpolant interpolant, in X x);
Interval index for x value given.
Interpolant interpolant interpolant
X x X value
auto interp1(Range, Interpolant)(Range range, Interpolant interpolant, size_t interval = 0);

struct Interp1(Range, Interpolant);
Lazy interpolation shell with linear complexity.
Range range sorted range
Interpolant interpolant interpolant structure with .gridScopeView method.

Complexity O(range.length + interpolant.gridScopeView.length) to evaluate all elements.

Lazy input range.
See Also:
PCHIP interpolation.
import mir.math.common: approxEqual;
import mir.ndslice.slice: sliced;
import mir.ndslice.allocation: rcslice;
import mir.interpolate: interp1;
import mir.interpolate.spline;

static immutable x = [1.0, 2, 4, 5, 8, 10, 12, 15, 19, 22];
static immutable y = [17.0, 0, 16, 4, 10, 15, 19, 5, 18, 6];
auto interpolation = spline!double(x.rcslice, y.sliced, SplineType.monotone);

auto xs = x[0 .. $ - 1].sliced + 0.5;

auto ys = xs.interp1(interpolation);
RefTuple!(T, size_t) atInterval(T)(in T value, size_t intervalIndex);
Optimization utility that can be used with interpolants if x should be extrapolated at interval given.
By default interpolants uses binary search to find appropriate interval, it has O(log(.gridScopeView.length)) complexity. If an interval is given, interpolant uses it instead of binary search.
import mir.ndslice.allocation;
import mir.ndslice.slice;
import mir.interpolate.spline;
static immutable x = [0.0, 1, 2];
static immutable y = [3.0, 4, -10];
auto interpolant = spline!double(x.rcslice, y.sliced);
assert(interpolant(1.3) != interpolant(1.3.atInterval(0)));
assert(interpolant(1.3) == interpolant(1.3.atInterval(1)));