bundles / scipy latest / scipy / interpolate / _bsplines / BSpline
class
scipy.interpolate._bsplines:BSpline
Signature
class BSpline ( t , c , k , extrapolate = True , axis = 0 ) Members
-
__call__ -
__init__ -
_ensure_c_contiguous -
antiderivative -
basis_element -
construct_fast -
derivative -
design_matrix -
from_power_basis -
insert_knot -
integrate
Summary
Univariate spline in the B-spline basis.
Extended Summary
where are B-spline basis functions of degree k and knots t.
Parameters
t: ndarray, shape (n+k+1,)knots
c: ndarray, shape (>=n, ...)spline coefficients
k: intB-spline degree
extrapolate: bool or 'periodic', optionalwhether to extrapolate beyond the base interval,
t[k] .. t[n], or to return nans. If True, extrapolates the first and last polynomial pieces of b-spline functions active on the base interval. If 'periodic', periodic extrapolation is used. Default is True.axis: int, optionalInterpolation axis. Default is zero.
Attributes
t: ndarrayknot vector
c: ndarrayspline coefficients
k: intspline degree
extrapolate: boolIf True, extrapolates the first and last polynomial pieces of b-spline functions active on the base interval.
axis: intInterpolation axis.
tck: tupleA read-only equivalent of
(self.t, self.c, self.k)
Methods
__call__basis_elementderivativeantiderivativeintegrateinsert_knotconstruct_fastdesign_matrixfrom_power_basis
Notes
B-spline basis elements are defined via
Implementation details
At least
k+1coefficients are required for a spline of degreek, so thatn >= k+1. Additional coefficients,c[j]withj > n, are ignored.B-spline basis elements of degree
kform a partition of unity on the base interval,t[k] <= x <= t[n].
Array API Standard Support
BSpline has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
Translating the recursive definition of B-splines into Python code, we have:def B(x, k, i, t): if k == 0: return 1.0 if t[i] <= x < t[i+1] else 0.0 if t[i+k] == t[i]: c1 = 0.0 else: c1 = (x - t[i])/(t[i+k] - t[i]) * B(x, k-1, i, t) if t[i+k+1] == t[i+1]: c2 = 0.0 else: c2 = (t[i+k+1] - x)/(t[i+k+1] - t[i+1]) * B(x, k-1, i+1, t) return c1 + c2✓
def bspline(x, t, c, k): n = len(t) - k - 1 assert (n >= k+1) and (len(c) >= n) return sum(c[i] * B(x, k, i, t) for i in range(n))✓
from scipy.interpolate import BSpline k = 2 t = [0, 1, 2, 3, 4, 5, 6] c = [-1, 2, 0, -1] spl = BSpline(t, c, k)✓
spl(2.5)
✗bspline(2.5, t, c, k)
✓import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() xx = np.linspace(1.5, 4.5, 50)✓
ax.plot(xx, [bspline(x, t, c ,k) for x in xx], 'r-', lw=3, label='naive') ax.plot(xx, spl(xx), 'b-', lw=4, alpha=0.7, label='BSpline')✗
ax.grid(True)
✓ax.legend(loc='best')
✗plt.show()
✓
Aliases
-
scipy.interpolate.BSpline