interpax.CubicSpline

class interpax.CubicSpline(x: Real[Array, 'n'] | Real[ndarray, 'n'], y: Num[Array, 'n ...'] | Num[ndarray, 'n ...'], axis: int = 0, bc_type: str | Iterable = 'not-a-knot', extrapolate: bool | str | None = None, check: bool = True)Source

Cubic spline data interpolator.

Interpolate data with a piecewise cubic polynomial which is twice continuously differentiable [1]. The result is represented as a PPoly instance with breakpoints matching the given data.

Parameters:
  • x (array_like, shape (n,)) – 1-D array containing values of the independent variable. Values must be real, finite and in strictly increasing order.

  • y (array_like) – Array containing values of the dependent variable. It can have arbitrary number of dimensions, but the length along axis (see below) must match the length of x. Values must be finite.

  • axis (int, optional) – Axis along which y is assumed to be varying. Meaning that for x[i] the corresponding values are np.take(y, i, axis=axis). Default is 0.

  • bc_type (string or 2-tuple, optional) –

    Boundary condition type. Two additional equations, given by the boundary conditions, are required to determine all coefficients of polynomials on each segment [2].

    If bc_type is a string, then the specified condition will be applied at both ends of a spline. Available conditions are:

    • ’not-a-knot’ (default): The first and second segment at a curve end are the same polynomial. It is a good default when there is no information on boundary conditions.

    • ’periodic’: The interpolated functions is assumed to be periodic of period x[-1] - x[0]. The first and last value of y must be identical: y[0] == y[-1]. This boundary condition will result in y'[0] == y'[-1] and y''[0] == y''[-1].

    • ’clamped’: The first derivative at curves ends are zero. Assuming a 1D y, bc_type=((1, 0.0), (1, 0.0)) is the same condition.

    • ’natural’: The second derivative at curve ends are zero. Assuming a 1D y, bc_type=((2, 0.0), (2, 0.0)) is the same condition.

    If bc_type is a 2-tuple, the first and the second value will be applied at the curve start and end respectively. The tuple values can be one of the previously mentioned strings (except ‘periodic’) or a tuple (order, deriv_values) allowing to specify arbitrary derivatives at curve ends:

    • order: the derivative order, 1 or 2.

    • deriv_value: array_like containing derivative values, shape must be the same as y, excluding axis dimension. For example, if y is 1-D, then deriv_value must be a scalar. If y is 3-D with the shape (n0, n1, n2) and axis=2, then deriv_value must be 2-D and have the shape (n0, n1).

  • extrapolate ({bool, 'periodic', None}, optional) – If bool, determines whether to extrapolate to out-of-bounds points based on first and last intervals, or to return NaNs. If ‘periodic’, periodic extrapolation is used. If None (default), extrapolate is set to ‘periodic’ for bc_type='periodic' and to True otherwise.

  • check (bool) – Whether to perform checks on the input. Should be False if used under JIT.

See also

Akima1DInterpolator

Akima 1D interpolator.

PchipInterpolator

PCHIP 1-D monotonic cubic interpolator.

PPoly

Piecewise polynomial in terms of coefficients and breakpoints.

Notes

Parameters bc_type and extrapolate work independently, i.e. the former controls only construction of a spline, and the latter only evaluation.

When a boundary condition is ‘not-a-knot’ and n = 2, it is replaced by a condition that the first derivative is equal to the linear interpolant slope. When both boundary conditions are ‘not-a-knot’ and n = 3, the solution is sought as a parabola passing through given points.

References

Methods

__call__(x[, nu, extrapolate])

Evaluate the piecewise polynomial or its derivative.

antiderivative([nu])

Construct a new piecewise polynomial representing the antiderivative.

construct_fast(c, x[, extrapolate, axis])

Construct the piecewise polynomial without making checks.

derivative([nu])

Construct a new piecewise polynomial representing the derivative.

extend(c, x[, right])

Not currently implemented.

from_bernstein_basis(bp[, extrapolate])

Not currently implemented.

from_spline(tck[, extrapolate])

Not currently implemented.

integrate(a, b[, extrapolate])

Compute a definite integral over a piecewise polynomial.

roots([discontinuity, extrapolate])

Not currently implemented.

solve([y, discontinuity, extrapolate])

Not currently implemented.

Attributes

axis

Axis along which to interpolate.

c

Array of spline coefficients, shape(order, knots-1, ...).

extrapolate

Whether to extrapolate beyond domain of known values.

x

Array of knot values, shape(knots).