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.

mir.interpolation.pchip

Piecewise Cubic Hermite Interpolating Polynomial

See Also:
Authors:
Ilya Yaroshenko
struct Pchip(RangeG, RangeV, RangeS);
Structure for unbounded piecewise cubic hermite interpolating polynomial.
RangeG _grid;
RangeV _values;
RangeS _slopes;
size_t interval(T)(in T x);
Interval index for x.
auto opCall(T)(in T x);
(x) and [x] operators.

Complexity: O(log(_grid.length))

auto opCall(T)(in T x, size_t interval);
(x, interval) and [x, interval] operators.

Complexity: O(1)

auto opCall(T)(G x0, G x1, V y0, V y1, S s0, S s1, in T x);

alias opIndex = opCall;
auto pchip(RangeG, RangeV)(RangeG grid, RangeV values);
Unbounded piecewise cubic hermite interpolating polynomial.
Parameters:
RangeG grid x values for interpolation
RangeV values f(x) values for interpolation

Constraints: grid and values must have the same length >= 3

Returns:

Allocation: Allocates slopes using GC.

Pchip!(RangeG, RangeV, RangeS) pchip(RangeG, RangeV, RangeS)(RangeG grid, RangeV values, RangeS slopes);
Piecewise cubic hermite interpolating polynomial.
Parameters:
RangeG grid x values for interpolation
RangeV values f(x) values for interpolation
RangeS slopes uninitialized arrays to write slopes into

Constraints: grid, values, and slopes must have the same length >= 3

Returns:
Examples:
import std.math: approxEqual;
import mir.ndslice.slice: sliced;
import mir.ndslice.topology: map, indexed;

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

auto xs = x.sliced[0 .. $ - 1].map!"a + 0.5";
auto ys = xs.map!interpolation;

auto ys2 = interpolation.indexed(xs); // alternative to map
version(X86_64)
    assert(ys == ys2);

assert(ys.approxEqual([
    5.333333333333334,
    2.500000000000000,
    10.000000000000000,
    4.288971807628524,
    11.202580845771145,
    16.250000000000000,
    17.962962962962962,
    5.558593750000000,
    17.604662698412699,
    ]));