scipy.interpolate.BSpline.

derivative#

BSpline.derivative(nu=1)[source]#

Return a B-spline representing the derivative.

Parameters:
nuint, optional

Derivative order. Default is 1.

Returns:
bBSpline object

A new instance representing the derivative.

See also

splder, splantider

Notes

The coefficient array of the returned instance will be padded with b.k + 1 trailing zeros, so that len(b.c) == len(b.t). The trailing coefficients never enter evaluations, only the first len(b.t) - b.k - 1 coefficients are used.

Examples

The derivative can be used for finding the local extrema of a curve:

>>> import numpy as np
>>> from scipy.interpolate import make_interp_spline, PPoly
>>> x = np.linspace(0, 10, 70)
>>> y = np.sin(x)
>>> spl = make_interp_spline(x, y, k=4)  # spl is a quartic (degree 4) spline

Now, differentiate the spline and find the zeros of the derivative. BSpline objects do not have a roots method, so we convert the derivative to a PPoly, which represents the same curve as a piecewise polynomial. We fit a spline of degree 4 rather than the default cubic, so that its derivative is smooth enough to locate the extrema accurately: the derivative of a quartic spline is a cubic spline with the same breakpoints, and PPoly.roots finds all real roots of each cubic piece directly, without iterating.

>>> dspl = spl.derivative()
>>> xe = PPoly.from_spline(dspl).roots(extrapolate=False)
>>> xe / np.pi
array([0.50000001, 1.5       , 2.49999998])

The array holds the locations, in units of \(\pi\), where the derivative vanishes on the data interval \([0, 10]\). This agrees well with roots \(\pi/2 + n\pi\) of \(\cos(x) = \sin'(x)\).

A zero of the derivative only tells us that the curve is flat there, which usually means a local extremum. To tell maxima from minima, check the sign of the second derivative: negative at a maximum, positive at a minimum.

>>> np.sign(spl.derivative(2)(xe))
array([-1.,  1., -1.])

So the curve has maxima at \(x \approx \pi/2\) and \(x \approx 5\pi/2\), and a minimum at \(x \approx 3\pi/2\).

The coefficient array of the returned spline is padded with k + 1 extra trailing entries, which are not used in evaluations, as the examples below demonstrate:

>>> from scipy.interpolate import BSpline, make_interp_spline
>>> x = np.arange(11)
>>> y = x**4 + np.sin(x)
>>> spl = make_interp_spline(x, y, k=3)  # spl is a cubic (degree 3) spline

A spline of degree k is built from polynomial pieces of degree k, joined at the knots t. The number of knots, the number of coefficients, and the degree are related by len(t) - len(c) == k + 1. Padded coefficients instead satisfy len(c) == len(t):

>>> len(spl.t), len(spl.c), spl.k    # knots, coefficients, degree of spl
(15, 11, 3)
>>> b = spl.derivative()             # b is a quadratic spline
>>> len(b.t), len(b.c), b.k          # knots, coefficients, degree of b
(13, 13, 2)
>>> b.c[len(b.t) - b.k - 1:]         # zero padding, unused in evaluations
array([0., 0., 0.])

Slice off the padding to get exactly len(b.t) - b.k - 1 coefficients:

>>> c = b.c[:len(b.t) - b.k - 1]
>>> len(c)
10

The trimmed coefficients define the same spline, since the padding never enters evaluations. Evaluating the spline before (b) and after (BSpline(b.t, c, b.k)) trimming on a grid of points spanning the data range gives the same values:

>>> xx = np.linspace(0, 10, 51)
>>> np.allclose(BSpline(b.t, c, b.k)(xx), b(xx))
True