View Source Scholar.Interpolation.CubicSpline (Scholar v0.3.0)

Cubic Spline interpolation.

This kind of interpolation is calculated by fitting a set of continuous cubic polynomials of which the first and second derivatives are also continuous.

The interpolated curve is smooth and mitigates oscillations that could appear if a single n-th degree polynomial were to be fitted over all of the points.

Cubic spline interpolation is $O(N)$ where $N$ is the number of points.

Reference:

Summary

Functions

Fits a cubic spline interpolation of the given (x, y) points

Returns the value fit by fit/2 corresponding to the target_x input

Functions

Fits a cubic spline interpolation of the given (x, y) points

Inputs are expected to be rank-1 tensors with the same shape and at least 3 entries.

Options

  • :boundary_condition (atom/0) - one of :not_a_knot or :natural The default value is :not_a_knot.

Examples

iex> x = Nx.iota({3})
iex> y = Nx.tensor([2.0, 0.0, 1.0])
iex> Scholar.Interpolation.CubicSpline.fit(x, y)
%Scholar.Interpolation.CubicSpline{
  coefficients: Nx.tensor(
    [
      [0.0, 1.5, -3.5, 2.0],
      [0.0, 1.5, -0.5, 0.0]
    ]
  ),
  x: Nx.tensor(
    [0, 1, 2]
  )
}
Link to this function

predict(model, target_x, opts \\ [])

View Source

Returns the value fit by fit/2 corresponding to the target_x input

Options

  • :extrapolate (boolean/0) - if false, out-of-bounds x return NaN. The default value is true.

Examples

iex> x = Nx.iota({3})
iex> y = Nx.tensor([2.0, 0.0, 1.0])
iex> model = Scholar.Interpolation.CubicSpline.fit(x, y)
iex> Scholar.Interpolation.CubicSpline.predict(model, Nx.tensor([[1.0, 4.0], [3.0, 7.0]]))
Nx.tensor(
  [
    [0.0, 12.0],
    [5.0, 51.0]
  ]
)