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.interpolate.spline

Cubic Spline Interpolation

The module provides common C2 splines, monotone (PCHIP) splines, Akima splines and others.
See Also:
License:
Authors:
Ilia Ki
public import mir.interpolate : atInterval;
enum SplineType: int;
Cubic Spline types.
The first derivatives are guaranteed to be continuous for all cubic splines.
c2
Spline with contiguous second derivative.
cardinal
Cardinal and Catmull–Rom splines.
monotone
The interpolant preserves monotonicity in the interpolation data and does not overshoot if the data is not smooth. It is also known as PCHIP in numpy and Matlab.
doubleQuadratic
Weighted sum of two nearbor quadratic functions. It is used in financial analysis.
akima
makima
enum SplineConvexity: int;
Spline convexity type
none
Neither convex nor concave spline
concave
Concave spline
convex
Convex spline
template spline(T, size_t N = 1, X = T) if (isFloatingPoint!T && is(T == Unqual!T) && (N <= 6))
Constructs multivariate cubic spline in symmetrical form with nodes on rectilinear grid. Result has continues second derivatives throughout the curve / nd-surface.
Spline!(T, N, X) spline(yIterator, SliceKind ykind)(Repeat!(N, Slice!(RCI!(immutable(X)))) grid, Slice!(yIterator, N, ykind) values, SplineBoundaryType typeOfBoundaries = SplineBoundaryType.notAKnot, in T valueOfBoundaryConditions = 0);
Parameters:
Repeat!(N, Slice!(RCI!(immutable(X)))) grid immutable x values for interpolant
Slice!(yIterator, N, ykind) values f(x) values for interpolant
SplineBoundaryType typeOfBoundaries SplineBoundaryType for both tails (optional).
T valueOfBoundaryConditions value of the boundary type (optional).

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

Returns:
Spline!(T, N, X) spline(yIterator, SliceKind ykind)(Repeat!(N, Slice!(RCI!(immutable(X)))) grid, Slice!(yIterator, N, ykind) values, SplineBoundaryCondition!T boundaries, SplineType kind = SplineType.c2, in T param = 0);
Parameters:
Repeat!(N, Slice!(RCI!(immutable(X)))) grid immutable x values for interpolant
Slice!(yIterator, N, ykind) values f(x) values for interpolant
SplineBoundaryCondition!T boundaries SplineBoundaryCondition for both tails.
SplineType kind SplineType type of cubic spline.
T param tangent power parameter for cardinal SplineType (ignored by other spline types). Use 1 for zero derivatives at knots and 0 for Catmull–Rom spline.

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

Returns:
Spline!(T, N, X) spline(yIterator, SliceKind ykind)(Repeat!(N, Slice!(RCI!(immutable(X)))) grid, Slice!(yIterator, N, ykind) values, SplineBoundaryCondition!T lBoundary, SplineBoundaryCondition!T rBoundary, SplineType kind = SplineType.c2, in T param = 0);
Parameters:
Repeat!(N, Slice!(RCI!(immutable(X)))) grid immutable x values for interpolant
Slice!(yIterator, N, ykind) values f(x) values for interpolant
SplineBoundaryCondition!T lBoundary SplineBoundaryCondition for left tail.
SplineBoundaryCondition!T rBoundary SplineBoundaryCondition for right tail.
SplineType kind SplineType type of cubic spline.
T param tangent power parameter for cardinal SplineType (ignored by other spline types). Use 1 for zero derivatives at knots and 0 for Catmull–Rom spline.

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

Returns:
Spline!(T, N, X) spline(yIterator, SliceKind ykind)(Repeat!(N, Slice!(RCI!(immutable(X)))) grid, Slice!(yIterator, N, ykind) values, SplineConfiguration!T configuration);
Parameters:
Repeat!(N, Slice!(RCI!(immutable(X)))) grid immutable x values for interpolant
Slice!(yIterator, N, ykind) values f(x) values for interpolant
SplineConfiguration!T configuration SplineConfiguration

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

Returns:
enum SplineBoundaryType: int;
Cubic Spline Boundary Condition Type.
notAKnot
Not-a-knot (or cubic) boundary condition. It is an aggresive boundary condition that is used only for C2 splines and is default for all API calls. For other then C2 splines, notAKnot is changed internally to a default boundary type for used SplineType.
firstDerivative
Set the first derivative.
secondDerivative
Set the second derivative.
parabolic
Default for Cardinal and Double-Quadratic splines.
monotone
Default for monotone (aka PHCIP ) splines.
akima
Default for Akima splines.
makima
Default for Modified Akima splines.
periodic
Not implemented.
struct SplineBoundaryCondition(T) if (__traits(isFloating, T));
Cubic Spline Boundary Condition
SplineBoundaryType type;
type (default is SplineBoundaryType.notAKnot)
T value;
value (default is 0)
struct SplineConfiguration(T) if (__traits(isFloating, T));
Spline configuration
SplineType kind;
SplineBoundaryCondition!T leftBoundary;
SplineBoundaryCondition!T rightBoundary;
const @property bool symmetric();
Returns:
true of leftBoundary equals rightBoundary.
@property void boundary(SplineBoundaryCondition!T boundary);
const @property SplineBoundaryCondition!T boundary();
T param;
Tangent power parameter for cardinal SplineType (ignored by other spline types). Use 1 for zero derivatives at knots and 0 for Catmull–Rom spline.
struct SplineSymmetricConfiguration(T) if (__traits(isFloating, T));
Spline configuration with two boundaries
SplineType type;
SplineBoundaryCondition!T boundary;
T param;
Tangent power parameter for cardinal SplineType (ignored by other spline types). Use 1 for zero derivatives at knots and 0 for Catmull–Rom spline.
struct Spline(F, size_t N = 1, X = F) if (N && (N <= 6));
Multivariate cubic spline with nodes on rectilinear grid.
Slice!(RCI!(F[2 ^^ N]), N) _data;
Aligned buffer allocated with mir.internal.memory. For internal use.
Repeat!(N, RCI!(immutable(X))) _grid;
Grid iterators. For internal use.
SplineConvexity[N] convexity;
@nogc @safe this(Repeat!(N, Slice!(RCI!(immutable(X)))) grid);
const @trusted Spline opBinary(string op)(const Spline rhs)
if (op == "+" || op == "-");

Note defined only for 1D splines

alias argmin = argminImpl!"a < b";
Returns:
spline argmin on the interpolation interval

Note defined only for 1D splines

alias argmax = argminImpl!"a > b";
Returns:
spline argmax on the interpolation interval

Note defined only for 1D splines

@property @trusted void _values(SliceKind kind, Iterator)(Slice!(Iterator, N, kind) values) scope;
Assigns function values to the internal memory. For internal use.
nothrow @nogc @trusted void _computeDerivatives(SplineType kind, F param, SplineBoundaryCondition!F lBoundary, SplineBoundaryCondition!F rBoundary) scope;

nothrow @nogc @system void _computeDerivativesTemp(SplineType kind, F param, SplineBoundaryCondition!F lBoundary, SplineBoundaryCondition!F rBoundary, Slice!(F*) temp) scope;
Computes derivatives and stores them in _data. _data is assumed to be preinitialized with function values filled in F[2 ^^ N][0].
Parameters:
SplineBoundaryCondition!F lBoundary left boundary condition
SplineBoundaryCondition!F rBoundary right boundary condition
Slice!(F*) temp temporal buffer length points count (optional)
For internal use.
const @property Spline lightConst();
immutable @property Spline lightImmutable();
const @property Slice!(RCI!(immutable(X))) grid(size_t dimension = 0)() return scope
if (dimension < N);
const @property @trusted immutable(X)[] gridScopeView(size_t dimension = 0)() return scope
if (dimension < N);
const @property size_t intervalCount(size_t dimension = 0)() scope;
Returns:
intervals count.
const @property size_t[N] gridShape() scope;
alias withDerivative = opCall!1;
alias withTwoDerivatives = opCall!2;
enum uint derivativeOrder;
template opCall(uint derivative : 2)
template opCall(uint derivative = 0) if (derivative == 0 || derivative == 1 || derivative == 3)
const @trusted auto opCall(X...)(in X xs) scope
if (X.length == N);
(x) operator.

Complexity O(log(points.length))

@trusted SplineConvexity splineSlopes(F, T, P, IV, IS, SliceKind gkind, SliceKind vkind, SliceKind skind)(Slice!(P*, 1, gkind) points, Slice!(IV, 1, vkind) values, Slice!(IS, 1, skind) slopes, Slice!(T*) temp, SplineType kind, F param, SplineBoundaryCondition!F lBoundary, SplineBoundaryCondition!F rBoundary);
Piecewise cubic hermite interpolating polynomial.
Parameters:
Slice!(P*, 1, gkind) points x values for interpolant
Slice!(IV, 1, vkind) values f(x) values for interpolant
Slice!(IS, 1, skind) slopes uninitialized ndslice to write slopes into
Slice!(T*) temp uninitialized temporary ndslice
SplineType kind SplineType type of cubic spline.
F param tangent power parameter for cardinal SplineType (ignored by other spline types). Use 1 for zero derivatives at knots and 0 for Catmull–Rom spline.
SplineBoundaryCondition!F lBoundary left boundary condition
SplineBoundaryCondition!F rBoundary right boundary condition

Constraints points, values, and slopes, must have the same length > 3; temp must have length greater or equal to points less minus one.

Returs Spline convexity type

struct SplineKernel(X);
this(X x0, X x1, X x);
template opCall(uint derivative = 0) if (derivative <= 3)
const auto opCall(Y)(const Y y0, const Y y1, const Y s0, const Y s1);
alias withDerivative = opCall!1;
alias withTwoDerivatives = opCall!2;
MetaSpline!(T, X) metaSpline(F, X, T)(RCArray!(immutable(X)) grid, RCArray!(const(T)) data, SplineBoundaryType typeOfBoundaries = SplineBoundaryType.notAKnot, const F valueOfBoundaryConditions = 0);
Spline interpolator used for non-rectiliner trapezoid-like greeds.
Parameters:
RCArray!(immutable(X)) grid rc-array of interpolation grid
RCArray!(const(T)) data rc-array of interpolator-like structures
SplineBoundaryType typeOfBoundaries SplineBoundaryType for both tails (optional).
F valueOfBoundaryConditions value of the boundary type (optional).

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

Returns:
MetaSpline!(T, X) metaSpline(F, X, T)(RCArray!(immutable(X)) grid, RCArray!(const(T)) data, SplineType kind, const F param = 0, SplineBoundaryType typeOfBoundaries = SplineBoundaryType.notAKnot, const F valueOfBoundaryConditions = 0);
Spline interpolator used for non-rectiliner trapezoid-like greeds.
Parameters:
RCArray!(immutable(X)) grid rc-array of interpolation grid
RCArray!(const(T)) data rc-array of interpolator-like structures
SplineType kind SplineType type of cubic spline.
F param tangent power parameter for cardinal SplineType (ignored by other spline types). Use 1 for zero derivatives at knots and 0 for Catmull–Rom spline.
SplineBoundaryType typeOfBoundaries SplineBoundaryType for both tails (optional).
F valueOfBoundaryConditions value of the boundary type (optional).

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

Returns:
MetaSpline!(T, X) metaSpline(F, X, T)(RCArray!(immutable(X)) grid, RCArray!(const(T)) data, SplineBoundaryCondition!F boundaries, SplineType kind = SplineType.c2, const F param = 0);
Spline interpolator used for non-rectiliner trapezoid-like greeds.
Parameters:
RCArray!(immutable(X)) grid rc-array of interpolation grid
RCArray!(const(T)) data rc-array of interpolator-like structures
SplineBoundaryCondition!F boundaries SplineBoundaryCondition for both tails.
SplineType kind SplineType type of cubic spline.
F param tangent power parameter for cardinal SplineType (ignored by other spline types). Use 1 for zero derivatives at knots and 0 for Catmull–Rom spline.

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

Returns:
MetaSpline!(T, X) metaSpline(F, X, T)(RCArray!(immutable(X)) grid, RCArray!(const(T)) data, SplineBoundaryCondition!F lBoundary, SplineBoundaryCondition!F rBoundary, SplineType kind = SplineType.c2, const F param = 0);
Spline interpolator used for non-rectiliner trapezoid-like greeds.
Parameters:
RCArray!(immutable(X)) grid rc-array of interpolation grid
RCArray!(const(T)) data rc-array of interpolator-like structures
SplineBoundaryCondition!F lBoundary SplineBoundaryCondition for left tail.
SplineBoundaryCondition!F rBoundary SplineBoundaryCondition for right tail.
SplineType kind SplineType type of cubic spline.
F param tangent power parameter for cardinal SplineType (ignored by other spline types). Use 1 for zero derivatives at knots and 0 for Catmull–Rom spline.

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

Returns:
MetaSpline!(T, X) metaSpline(F, X, T)(RCArray!(immutable(X)) grid, RCArray!(const(T)) data, SplineConfiguration!F configuration);

struct MetaSpline(T, X);
Spline interpolator used for non-rectiliner trapezoid-like greeds.
Parameters:
RCArray!(immutable(X)) grid rc-array of interpolation grid
RCArray!(const(T)) data rc-array of interpolator-like structures
SplineConfiguration!F configuration SplineConfiguration

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

Returns:
Examples:
2D trapezoid-like (not rectilinear) linear interpolation
auto x = [
    [0.0, 1, 2, 3, 5],
    [-4.0, 3, 4],
    [0.0, 10],
];
auto y = [
    [4.0, 0, 9, 23, 40],
    [9.0, 0, 3],
    [4.0, 40],
];

auto g = [7.0, 10, 15];

import mir.rc.array: RCArray;
import mir.ndslice.allocation: rcslice;

auto d = RCArray!(Spline!double)(3);

foreach (i; 0 .. x.length)
    d[i] = spline!double(x[i].rcslice!(immutable double), y[i].rcslice!(const double));

auto trapezoidInterpolator = metaSpline!double(g.rcarray!(immutable double), d.lightConst);

auto val = trapezoidInterpolator(9.0, 1.8);