concepts.math.interpolation_utils.CubicSpline#
- class CubicSpline[source]#
Bases:
CubicSpline
,SplineInterface
Methods
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)Add additional breakpoints and coefficients to the polynomial.
from_bernstein_basis
(bp[, extrapolate])Construct a piecewise polynomial in the power basis from a polynomial in Bernstein basis.
from_points
(ys)from_spline
(tck[, extrapolate])Construct a piecewise polynomial from a spline
get_next
(y, step_size[, minimum_x])Get the next target point on a spline interpolation.
integrate
(a, b[, extrapolate])Compute a definite integral over a piecewise polynomial.
project_to
(y[, minimum_x])Project a point to a cubic spline interpolation.
roots
([discontinuity, extrapolate])Find real roots of the piecewise polynomial.
solve
([y, discontinuity, extrapolate])Find real solutions of the equation
pp(x) == y
.Attributes
- __call__(x, nu=0, extrapolate=None)#
Evaluate the piecewise polynomial or its derivative.
- Parameters:
x (array_like) – Points to evaluate the interpolant at.
nu (int, optional) – Order of derivative to evaluate. Must be non-negative.
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), use self.extrapolate.
- Returns:
y – Interpolated values. Shape is determined by replacing the interpolation axis in the original array with the shape of x.
- Return type:
array_like
Notes
Derivatives are evaluated piecewise for each polynomial segment, even if the polynomial is not differentiable at the breakpoints. The polynomial intervals are considered half-open,
[a, b)
, except for the last interval which is closed[a, b]
.
- __new__(**kwargs)#
- antiderivative(nu=1)#
Construct a new piecewise polynomial representing the antiderivative.
Antiderivative is also the indefinite integral of the function, and derivative is its inverse operation.
- Parameters:
nu (int, optional) – Order of antiderivative to evaluate. Default is 1, i.e., compute the first integral. If negative, the derivative is returned.
- Returns:
pp – Piecewise polynomial of order k2 = k + n representing the antiderivative of this polynomial.
- Return type:
PPoly
Notes
The antiderivative returned by this function is continuous and continuously differentiable to order n-1, up to floating point rounding error.
If antiderivative is computed and
self.extrapolate='periodic'
, it will be set to False for the returned instance. This is done because the antiderivative is no longer periodic and its correct evaluation outside of the initially given x interval is difficult.
- classmethod construct_fast(c, x, extrapolate=None, axis=0)#
Construct the piecewise polynomial without making checks.
Takes the same parameters as the constructor. Input arguments
c
andx
must be arrays of the correct shape and type. Thec
array can only be of dtypes float and complex, andx
array must have dtype float.
- derivative(nu=1)#
Construct a new piecewise polynomial representing the derivative.
- Parameters:
nu (int, optional) – Order of derivative to evaluate. Default is 1, i.e., compute the first derivative. If negative, the antiderivative is returned.
- Returns:
pp – Piecewise polynomial of order k2 = k - n representing the derivative of this polynomial.
- Return type:
PPoly
Notes
Derivatives are evaluated piecewise for each polynomial segment, even if the polynomial is not differentiable at the breakpoints. The polynomial intervals are considered half-open,
[a, b)
, except for the last interval which is closed[a, b]
.
- extend(c, x)#
Add additional breakpoints and coefficients to the polynomial.
- Parameters:
c (ndarray, size (k, m, ...)) – Additional coefficients for polynomials in intervals. Note that the first additional interval will be formed using one of the
self.x
end points.x (ndarray, size (m,)) – Additional breakpoints. Must be sorted in the same order as
self.x
and either to the right or to the left of the current breakpoints.
- classmethod from_bernstein_basis(bp, extrapolate=None)#
Construct a piecewise polynomial in the power basis from a polynomial in Bernstein basis.
- Parameters:
bp (BPoly) – A Bernstein basis polynomial, as created by BPoly
extrapolate (bool or 'periodic', 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. Default is True.
- classmethod from_spline(tck, extrapolate=None)#
Construct a piecewise polynomial from a spline
- Parameters:
tck – A spline, as returned by splrep or a BSpline object.
extrapolate (bool or 'periodic', 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. Default is True.
Examples
Construct an interpolating spline and convert it to a PPoly instance
>>> import numpy as np >>> from scipy.interpolate import splrep, PPoly >>> x = np.linspace(0, 1, 11) >>> y = np.sin(2*np.pi*x) >>> tck = splrep(x, y, s=0) >>> p = PPoly.from_spline(tck) >>> isinstance(p, PPoly) True
Note that this function only supports 1D splines out of the box.
If the
tck
object represents a parametric spline (e.g. constructed by splprep or a BSpline withc.ndim > 1
), you will need to loop over the dimensions manually.>>> from scipy.interpolate import splprep, splev >>> t = np.linspace(0, 1, 11) >>> x = np.sin(2*np.pi*t) >>> y = np.cos(2*np.pi*t) >>> (t, c, k), u = splprep([x, y], s=0)
Note that
c
is a list of two arrays of length 11.>>> unew = np.arange(0, 1.01, 0.01) >>> out = splev(unew, (t, c, k))
To convert this spline to the power basis, we convert each component of the list of b-spline coefficients,
c
, into the corresponding cubic polynomial.>>> polys = [PPoly.from_spline((t, cj, k)) for cj in c] >>> polys[0].c.shape (4, 14)
Note that the coefficients of the polynomials polys are in the power basis and their dimensions reflect just that: here 4 is the order (degree+1), and 14 is the number of intervals—which is nothing but the length of the knot array of the original tck minus one.
Optionally, we can stack the components into a single PPoly along the third dimension:
>>> cc = np.dstack([p.c for p in polys]) # has shape = (4, 14, 2) >>> poly = PPoly(cc, polys[0].x) >>> np.allclose(poly(unew).T, # note the transpose to match `splev` ... out, atol=1e-15) True
- get_next(y, step_size, minimum_x=None)#
Get the next target point on a spline interpolation.
- integrate(a, b, extrapolate=None)#
Compute a definite integral over a piecewise polynomial.
- Parameters:
a (float) – Lower integration bound
b (float) – Upper integration bound
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), use self.extrapolate.
- Returns:
ig – Definite integral of the piecewise polynomial over [a, b]
- Return type:
array_like
- roots(discontinuity=True, extrapolate=None)#
Find real roots of the piecewise polynomial.
- Parameters:
discontinuity (bool, optional) – Whether to report sign changes across discontinuities at breakpoints as roots.
extrapolate ({bool, 'periodic', None}, optional) – If bool, determines whether to return roots from the polynomial extrapolated based on first and last intervals, ‘periodic’ works the same as False. If None (default), use self.extrapolate.
- Returns:
roots – Roots of the polynomial(s).
If the PPoly object describes multiple polynomials, the return value is an object array whose each element is an ndarray containing the roots.
- Return type:
ndarray
See also
PPoly.solve
- solve(y=0., discontinuity=True, extrapolate=None)#
Find real solutions of the equation
pp(x) == y
.- Parameters:
y (float, optional) – Right-hand side. Default is zero.
discontinuity (bool, optional) – Whether to report sign changes across discontinuities at breakpoints as roots.
extrapolate ({bool, 'periodic', None}, optional) – If bool, determines whether to return roots from the polynomial extrapolated based on first and last intervals, ‘periodic’ works the same as False. If None (default), use self.extrapolate.
- Returns:
roots – Roots of the polynomial(s).
If the PPoly object describes multiple polynomials, the return value is an object array whose each element is an ndarray containing the roots.
- Return type:
ndarray
Notes
This routine works only on real-valued polynomials.
If the piecewise polynomial contains sections that are identically zero, the root list will contain the start point of the corresponding interval, followed by a
nan
value.If the polynomial is discontinuous across a breakpoint, and there is a sign change across the breakpoint, this is reported if the discont parameter is True.
Examples
Finding roots of
[x**2 - 1, (x - 1)**2]
defined on intervals[-2, 1], [1, 2]
:>>> import numpy as np >>> from scipy.interpolate import PPoly >>> pp = PPoly(np.array([[1, -4, 3], [1, 0, 0]]).T, [-2, 1, 2]) >>> pp.solve() array([-1., 1.])
- axis#
- c#
- extrapolate#
- x#