package scipy

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `BSpline
]
type t = [ `BSpline | `Object ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val create : ?extrapolate:[ `Periodic | `Bool of bool ] -> ?axis:int -> t:[> `Ndarray ] Np.Obj.t -> c:[> `Ndarray ] Np.Obj.t -> k:int -> unit -> t

Univariate spline in the B-spline basis.

.. math::

S(x) = \sum_j=0^n-1 c_j B_j, k; t(x)

where :math:`B_j, k; t` 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 : int B-spline degree extrapolate : bool or 'periodic', optional whether to extrapolate beyond the base interval, ``tk .. tn``, 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, optional Interpolation axis. Default is zero.

Attributes ---------- t : ndarray knot vector c : ndarray spline coefficients k : int spline degree extrapolate : bool If True, extrapolates the first and last polynomial pieces of b-spline functions active on the base interval. axis : int Interpolation axis. tck : tuple A read-only equivalent of ``(self.t, self.c, self.k)``

Methods ------- __call__ basis_element derivative antiderivative integrate construct_fast

Notes ----- B-spline basis elements are defined via

.. math::

B_, 0(x) = 1, \textrmf $t_i \le x < t_+1$, otherwise $0$,

B_, k(x) = \fracx - t_i

_+k t_i

B_, k-1(x)

  1. \frac

    _+k+1 x

    _+k+1 t_+1

    B_+1, k-1(x)

**Implementation details**

  • At least ``k+1`` coefficients are required for a spline of degree `k`, so that ``n >= k+1``. Additional coefficients, ``cj`` with ``j > n``, are ignored.
  • B-spline basis elements of degree `k` form a partition of unity on the *base interval*, ``tk <= x <= tn``.

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 ti <= x < ti+1 else 0.0 ... if ti+k == ti: ... c1 = 0.0 ... else: ... c1 = (x - ti)/(ti+k - ti) * B(x, k-1, i, t) ... if ti+k+1 == ti+1: ... c2 = 0.0 ... else: ... c2 = (ti+k+1 - x)/(ti+k+1 - ti+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(ci * B(x, k, i, t) for i in range(n))

Note that this is an inefficient (if straightforward) way to evaluate B-splines --- this spline class does it in an equivalent, but much more efficient way.

Here we construct a quadratic spline function on the base interval ``2 <= x <= 4`` and compare with the naive way of evaluating the spline:

>>> 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) array(1.375) >>> bspline(2.5, t, c, k) 1.375

Note that outside of the base interval results differ. This is because `BSpline` extrapolates the first and last polynomial pieces of B-spline functions active on the base interval.

>>> import matplotlib.pyplot as plt >>> 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()

References ---------- .. 1 Tom Lyche and Knut Morken, Spline methods, http://www.uio.no/studier/emner/matnat/ifi/INF-MAT5340/v05/undervisningsmateriale/ .. 2 Carl de Boor, A practical guide to splines, Springer, 2001.

val antiderivative : ?nu:int -> [> tag ] Obj.t -> Py.Object.t

Return a B-spline representing the antiderivative.

Parameters ---------- nu : int, optional Antiderivative order. Default is 1.

Returns ------- b : BSpline object A new instance representing the antiderivative.

Notes ----- 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.

See Also -------- splder, splantider

val basis_element : ?extrapolate:[ `Periodic | `Bool of bool ] -> t:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> Py.Object.t

Return a B-spline basis element ``B(x | t0, ..., tk+1)``.

Parameters ---------- t : ndarray, shape (k+1,) internal knots extrapolate : bool or 'periodic', optional whether to extrapolate beyond the base interval, ``t0 .. tk+1``, or to return nans. If 'periodic', periodic extrapolation is used. Default is True.

Returns ------- basis_element : callable A callable representing a B-spline basis element for the knot vector `t`.

Notes ----- The degree of the B-spline, `k`, is inferred from the length of `t` as ``len(t)-2``. The knot vector is constructed by appending and prepending ``k+1`` elements to internal knots `t`.

Examples --------

Construct a cubic B-spline:

>>> from scipy.interpolate import BSpline >>> b = BSpline.basis_element(0, 1, 2, 3, 4) >>> k = b.k >>> b.tk:-k array( 0., 1., 2., 3., 4.) >>> k 3

Construct a quadratic B-spline on ``0, 1, 1, 2``, and compare to its explicit form:

>>> t = -1, 0, 1, 1, 2 >>> b = BSpline.basis_element(t1:) >>> def f(x): ... return np.where(x < 1, x*x, (2. - x)**2)

>>> import matplotlib.pyplot as plt >>> fig, ax = plt.subplots() >>> x = np.linspace(0, 2, 51) >>> ax.plot(x, b(x), 'g', lw=3) >>> ax.plot(x, f(x), 'r', lw=8, alpha=0.4) >>> ax.grid(True) >>> plt.show()

val construct_fast : ?extrapolate:Py.Object.t -> ?axis:Py.Object.t -> t:Py.Object.t -> c:Py.Object.t -> k:Py.Object.t -> [> tag ] Obj.t -> Py.Object.t

Construct a spline without making checks.

Accepts same parameters as the regular constructor. Input arrays `t` and `c` must of correct shape and dtype.

val derivative : ?nu:int -> [> tag ] Obj.t -> Py.Object.t

Return a B-spline representing the derivative.

Parameters ---------- nu : int, optional Derivative order. Default is 1.

Returns ------- b : BSpline object A new instance representing the derivative.

See Also -------- splder, splantider

val integrate : ?extrapolate:[ `Bool of bool | `Periodic ] -> a:float -> b:float -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Compute a definite integral of the spline.

Parameters ---------- a : float Lower limit of integration. b : float Upper limit of integration. extrapolate : bool or 'periodic', optional whether to extrapolate beyond the base interval, ``tk .. t-k-1``, or take the spline to be zero outside of the base interval. If 'periodic', periodic extrapolation is used. If None (default), use `self.extrapolate`.

Returns ------- I : array_like Definite integral of the spline over the interval ``a, b``.

Examples -------- Construct the linear spline ``x if x < 1 else 2 - x`` on the base interval :math:`0, 2`, and integrate it

>>> from scipy.interpolate import BSpline >>> b = BSpline.basis_element(0, 1, 2) >>> b.integrate(0, 1) array(0.5)

If the integration limits are outside of the base interval, the result is controlled by the `extrapolate` parameter

>>> b.integrate(-1, 1) array(0.0) >>> b.integrate(-1, 1, extrapolate=False) array(0.5)

>>> import matplotlib.pyplot as plt >>> fig, ax = plt.subplots() >>> ax.grid(True) >>> ax.axvline(0, c='r', lw=5, alpha=0.5) # base interval >>> ax.axvline(2, c='r', lw=5, alpha=0.5) >>> xx = -1, 1, 2 >>> ax.plot(xx, b(xx)) >>> plt.show()

val t : t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Attribute t: get value or raise Not_found if None.

val t_opt : t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t option

Attribute t: get value as an option.

val c : t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Attribute c: get value or raise Not_found if None.

val c_opt : t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t option

Attribute c: get value as an option.

val k : t -> int

Attribute k: get value or raise Not_found if None.

val k_opt : t -> int option

Attribute k: get value as an option.

val extrapolate : t -> bool

Attribute extrapolate: get value or raise Not_found if None.

val extrapolate_opt : t -> bool option

Attribute extrapolate: get value as an option.

val axis : t -> int

Attribute axis: get value or raise Not_found if None.

val axis_opt : t -> int option

Attribute axis: get value as an option.

val tck : t -> Py.Object.t

Attribute tck: get value or raise Not_found if None.

val tck_opt : t -> Py.Object.t option

Attribute tck: get value as an option.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Stdlib.Format.formatter -> t -> unit

Pretty-print the object to a formatter.