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.linear Linear Interpolant
mir.interpolate.pchip Piecewise Cubic Hermite Interpolating Polynomial
mir.interpolate.polynomial Lagrange Barycentric Interpolation
mir.interpolate.spline Cubic Spline Interpolant
Ilya Yaroshenko
template findInterval(size_t dimension = 0)
Interval index for x value given.
import mir.ndslice.slice: sliced;
import mir.interpolate.linear;

auto x = [0.0, 1, 2].idup.sliced;
auto y = [10.0, 2, 4].idup.sliced;
auto interpolation = linear!double(x, y);
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 .grid method.

Complexity O(range.length + interpolant.grid.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: slice;
import mir.interpolate: interp1;
import mir.interpolate.pchip;

auto x = [1.0, 2, 4, 5, 8, 10, 12, 15, 19, 22].idup.sliced;
auto y = [17.0, 0, 16, 4, 10, 15, 19, 5, 18, 6].idup.sliced;
auto interpolation = pchip!double(x, y);

auto xs = slice(x[0 .. $ - 1] + 0.5);

auto ys = xs.interp1(interpolation);

// assert(ys.approxEqual([
//     5.333333333333334,
//     2.500000000000000,
//     10.000000000000000,
//     4.288971807628524,
//     11.202580845771145,
//     16.250000000000000,
//     17.962962962962962,
//     5.558593750000000,
//     17.604662698412699,
//     ]));
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(.grid.length)) complexity. If an interval is given, interpolant uses it instead of binary search.
import mir.ndslice.slice;
import mir.interpolate.spline;
auto interpolant = spline!double([0.0, 1, 2].idup.sliced, [3, 4, -10].idup.sliced);
assert(interpolant(1.3) != interpolant(1.3.atInterval(0)));
assert(interpolant(1.3) == interpolant(1.3.atInterval(1)));