1D interpolation routines discussed in the previous section, work by constructing certain piecewise
polynomials: the interpolation range is split into intervals by the so-called
breakpoints, and there is a certain polynomial on each interval. These
polynomial pieces then match at the breakpoints with a predefined smoothness:
the second derivatives for cubic splines, the first derivatives for monotone
interpolants and so on.
A polynomial of degree k can be thought of as a linear combination of
k+1 monomial basis elements, 1,x,x2,⋯,xk.
In some applications, it is useful to consider alternative (if formally
equivalent) bases. Two popular bases, implemented in are
B-splines () and Bernstein polynomials ().
B-splines are often used, for example, in non-parametric regression problems,
and Bernstein polynomials are used for constructing Bezier curves.
PPoly objects represent piecewise polynomials in the ‘usual’ power basis.
This is the case for CubicSpline instances and monotone interpolants.
In general, PPoly objects can represent polynomials of
arbitrary orders, not only cubics. For the data array x, breakpoints are at
the data points, and the array of coefficients, c , define polynomials of
degree k, such that c[i,j] is a coefficient for
(x-x[j])**(k-i) on the segment between x[j] and x[j+1] .
BSpline objects represent B-spline functions — linear combinations of
b-spline basis elements.
These objects can be instantiated directly or constructed from data with the
make_interp_spline factory function.
Finally, Bernstein polynomials are represented as instances of the BPoly class.
All these classes implement a (mostly) similar interface, PPoly being the most
feature-complete. We next consider the main features of this interface and
discuss some details of the alternative bases for piecewise polynomials.
PPoly objects have convenient methods for constructing derivatives
and antiderivatives, computing integrals and root-finding. For example, we
tabulate the sine function and find the roots of its derivative.
Here dspl is a PPoly instance which represents a polynomial approximation
to the derivative of the original object, spl . Evaluating dspl at a
fixed argument is equivalent to evaluating the original spline with the nu=1
argument:
This agrees well with the roots π/2+πn of
cos(x)=sin′(x).
Note that by default it computes the roots extrapolated to the outside of
the interpolation interval 0⩽x⩽10, and that
the extrapolated results (the first and last values) are much less accurate.
We can switch off the extrapolation and limit the root-finding to the
interpolation interval:
In fact, the roots method is a special case of a more general solve
method which finds for a given constant y the solutions of the
equation f(x)=y , where f(x) is the piecewise polynomial:
which agrees well with the expected values of ±arccos(1/2)+2πn.
Integrals of piecewise polynomials can be computed using the .integrate
method which accepts the lower and the upper limits of integration. As an
example, we compute an approximation to the complete elliptic integral
K(m)=∫0π/2[1−msin2x]−1/2dx:
All piecewise polynomials can be constructed with N-dimensional y values.
If y.ndim>1, it is interpreted as a stack of 1D y values, which are
arranged along the interpolation axis (with the default value of 0).
The latter is specified via the axis argument, and the invariant is that
len(x)==y.shape[axis]. As an example, we extend the elliptic integral
example above to compute the approximation for a range of m values, using
the NumPy broadcasting:
We see that the knot vector by default is constructed from the input
array x: first, it is made (k+1) -regular (it has k
repeated knots appended and prepended); then, the second and
second-to-last points of the input array are removed—this is the so-called
not-a-knot boundary condition.
In general, an interpolating spline of degree k needs
len(t)-len(x)-k-1 boundary conditions. For cubic splines with
(k+1)-regular knot arrays this means two boundary conditions—or
removing two values from the x array. Various boundary conditions can be
requested using the optional bc_type argument of make_interp_spline.
The b-spline coefficients are accessed via the c attribute of a BSpline
object:
>>> len(bspl.c)7
The convention is that for len(t) knots there are len(t)-k-1
coefficients. Some routines (see the Smoothing splines section) zero-pad the c arrays so that
len(c)==len(t). These additional coefficients are ignored for evaluation.
We stress that the coefficients are given in the
b-spline basis, not the power basis
of 1,x,⋯,xk.
B-spline basis elements
The b-spline basis is used in a variety of applications which include interpolation,
regression and curve representation.
B-splines are piecewise polynomials, represented as linear combinations of
b-spline basis elements — which themselves are certain linear combinations
of usual monomials, xm with m=0,1,…,k.
The properties of b-splines are well described in the literature (see, for example,
references listed in the docstring). For our purposes, it is enough to know
that a b-spline function is uniquely defined by an array of coefficients and
an array of the so-called , which may or may not coincide with the data points,
.
Design matrices in the B-spline basis
One common application of b-splines is in non-parametric regression. The reason
is that the localized nature of the b-spline basis elements makes linear
algebra banded. This is because at most k+1 basis elements are non-zero
at a given evaluation point, thus a design matrix built on b-splines has at most
k+1 diagonals.
As an illustration, we consider a toy example. Suppose our data are
one-dimensional and are confined to an interval [0,6].
We construct a 4-regular knot vector which corresponds to 7 data points and
cubic, , splines:
Bernstein polynomials, BPoly
For t∈[0,1], Bernstein basis polynomials of degree k are defined via
b(t;k,a)=Ckata(1−t)k−a
where Cka is the binomial coefficient, and a=0,1,…,k, so that
there are k+1 basis polynomials of degree .
A BPoly object represents a piecewise Bernstein polynomial in terms of
breakpoints, x, and coefficients, c: c[a,j] gives the coefficient for
b(t;k,a) for t on the interval between x[j] and x[j+1].
The user interface of BPoly objects is very similar to that of PPoly objects:
both can be evaluated, differentiated and integrated.
One additional feature of BPoly objects is the alternative constructor,
BPoly.from_derivatives, which constructs a BPoly object from data values and derivatives.
Specifically, b=BPoly.from_derivatives(x,y) returns a callable that interpolates
the provided values, b(x[i])==y[i]), and has the provided derivatives,
b(x[i],nu=j)==y[i][j].
This operation is similar to CubicHermiteSpline, but it is more flexible in that
it can handle varying numbers of derivatives at different data points; i.e., the y
argument can be a list of arrays of different lengths. See BPoly.from_derivatives
for further discussion and examples.
Conversion between bases
In principle, all three bases for piecewise polynomials (the power basis, the Bernstein
basis, and b-splines) are equivalent, and a polynomial in one basis can be converted
into a different basis. One reason for converting between bases is that not all bases
implement all operations. For instance, root-finding is only implemented for PPoly,
and therefore to find roots of a BSpline object, you need to convert to PPoly first.
See methods PPoly.from_bernstein_basis, PPoly.from_spline,
BPoly.from_power_basis, and BSpline.from_power_basis for details about conversion.
In floating-point arithmetic, though, conversions always incur some precision loss.
Whether this is significant is problem-dependent, so it is therefore recommended to
exercise caution when converting between bases.
2
)
**
(
-
1
/
2
)
>>>
spl
=
PchipInterpolator
(
x
,
y
)
y
=
1
/
np
.
sqrt
(
1
-
m
[:,
None
]
*
np
.
sin
(
x
)
**
2
)
2
),
'--'
)
BSpline
knots
x
Specifically, a b-spline basis element of degree k (e.g. k=3 for cubics)
is defined by k+2 knots and is zero outside of these knots.
To illustrate, plot a collection of non-zero basis elements on a certain
interval:
Here BSpline.basis_element is essentially a shorthand for constructing a spline
with only a single non-zero coefficient. For instance, the j=2 element in
the above example is equivalent to
If desired, a b-spline can be converted into a PPoly object using
PPoly.from_spline method which accepts a BSpline instance and returns a
PPoly instance. The reverse conversion is performed by the
BSpline.from_power_basis method. However, conversions between bases is best
avoided because it accumulates rounding errors.
k=3
>>> t=[0.,0.,0.,0.,2.,3.,4.,6.,6.,6.,6.]
Next, take ‘observations’ to be
>>> xnew=[1,2,3]
and construct the design matrix in the sparse CSR format
>>> fromscipy.interpolateimportBSpline>>> mat=BSpline.design_matrix(xnew,t,k=3)>>> mat<Compressed Sparse Row sparse array of dtype 'float64' with 12 stored elements and shape (3, 7)>
Here each row of the design matrix corresponds to a value in the xnew array,
and a row has no more than k+1=4 non-zero elements; row j
contains basis elements evaluated at xnew[j]: