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
CubicSpline!(packs == [2], const(T)*) pchip(T, size_t[] packs)(Slice!(Contiguous, [1], const(T)*) points, Slice!(Contiguous, packs, const(T)*) values)
if (packs == [1] || packs == [2]);
Unbounded piecewise cubic hermite interpolating polynomial.
Parameters:
Slice!(Contiguous, [1], const(T)*) points x values for interpolation
Slice!(Contiguous, packs, const(T)*) values f(x) values for interpolation

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

Returns:

Allocation: Allocates slopes using GC.

@trusted CubicSpline!(false, ConstIfPointer!IG, ConstIfPointer!IV, ConstIfPointer!IS) pchip(IG, IV, IS)(Slice!(Contiguous, [1], IG) points, Slice!(Contiguous, [1], IV) values, Slice!(Contiguous, [1], IS) slopes);

@trusted CubicSpline!(true, ConstIfPointer!IG, ConstIfPointer!IV, ConstIfPointer!IS) pchip(IG, IV, IS)(Slice!(Contiguous, [1], IG) points, Slice!(Contiguous, [2], IV) values, Slice!(Contiguous, [2], IS) slopes);
Piecewise cubic hermite interpolating polynomial.
Parameters:
Slice!(Contiguous, [1], IG) points x values for interpolation
Slice!(Contiguous, [1], IV) values f(x) values for interpolation
Slice!(Contiguous, [1], IS) slopes uninitialized ndslice to write slopes into

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

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

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

auto xs = x[0 .. $ - 1] + 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,
    ]));