View Source Scholar.Interpolation.BezierSpline (Scholar v0.3.1)
Cubic Bezier 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 (which makes them curves of class $C^2$).
In contrast to the Scholar.Interpolation.CubicSpline
algorithm,
the Bezier curves aren't necessarily of class $C^2$, but this interpolation
forces this so it yields a smooth function as the result.
The interpolated curve also has local control,
meaning that a point that would be problematic for other
interpolations, such as the Scholar.Interpolation.CubicSpline
or Scholar.Interpolation.Linear
algorithms, will only affect
the segments right next to it, instead of affecting the curve as a whole.
Computing Bezier curve is $O(N^2)$ where $N$ is the number of points.
Reference:
- [1] - Bezier theory
- [2] - Spline equation derivation
Summary
Functions
Fits a cubic Bezier spline interpolation of the given (x, y)
points.
Inputs are expected to be rank-1 tensors with the same shape and at least 4 entries.
Examples
iex> x = Nx.iota({4})
iex> y = Nx.tensor([2.0, 0.0, 1.0, 0.5])
iex> Scholar.Interpolation.BezierSpline.fit(x, y)
%Scholar.Interpolation.BezierSpline{
coefficients: Nx.tensor(
[
[
[0.0, 2.0],
[0.3333331048488617, 1.0333333015441895],
[0.6666665077209473, 0.06666667759418488],
[1.0, 0.0]
],
[
[1.0, 0.0],
[1.3333334922790527, -0.06666667759418488],
[1.6666665077209473, 0.7666666507720947],
[2.0, 1.0]
],
[
[2.0, 1.0],
[2.3333334922790527, 1.2333333492279053],
[2.6666667461395264, 0.8666666746139526],
[3.0, 0.5]
]
]
),
k: Nx.tensor(
[
[0.0, 2.0],
[1.0, 0.0],
[2.0, 1.0],
[3.0, 0.5]
]
)
}
Returns the value fit by fit/2
corresponding to the target_x
input.
Options
:max_iter
(pos_integer/0
) - determines the maximum iterations for converging the t parameter for each spline polynomial The default value is15
.:eps
(float/0
) - determines the tolerance to be used for converging the t parameter for each spline polynomial The default value is1.0e-6
.
Examples
iex> x = Nx.iota({4})
iex> y = Nx.tensor([2.0, 0.0, 1.0, 0.5])
iex> model = Scholar.Interpolation.BezierSpline.fit(x, y)
iex> Scholar.Interpolation.BezierSpline.predict(model, Nx.tensor([3.0, 4.0, 2.0, 7.0]))
Nx.tensor(
[0.5000335574150085, -4.2724612285383046e-5, 0.9999786615371704, 34.5]
)