package scipy

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `CubicSpline
]
type t = [ `CubicSpline | `Object ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val create : ?axis:int -> ?bc_type:[ `S of string | `T2_tuple of Py.Object.t ] -> ?extrapolate:[ `Periodic | `Bool of bool ] -> x:[> `Ndarray ] Np.Obj.t -> y:[> `Ndarray ] Np.Obj.t -> unit -> t

Cubic spline data interpolator.

Interpolate data with a piecewise cubic polynomial which is twice continuously differentiable 1_. The result is represented as a `PPoly` instance with breakpoints matching the given data.

Parameters ---------- x : array_like, shape (n,) 1-D array containing values of the independent variable. Values must be real, finite and in strictly increasing order. y : array_like Array containing values of the dependent variable. It can have arbitrary number of dimensions, but the length along ``axis`` (see below) must match the length of ``x``. Values must be finite. axis : int, optional Axis along which `y` is assumed to be varying. Meaning that for ``xi`` the corresponding values are ``np.take(y, i, axis=axis)``. Default is 0. bc_type : string or 2-tuple, optional Boundary condition type. Two additional equations, given by the boundary conditions, are required to determine all coefficients of polynomials on each segment 2_.

If `bc_type` is a string, then the specified condition will be applied at both ends of a spline. Available conditions are:

* 'not-a-knot' (default): The first and second segment at a curve end are the same polynomial. It is a good default when there is no information on boundary conditions. * 'periodic': The interpolated functions is assumed to be periodic of period ``x-1 - x0``. The first and last value of `y` must be identical: ``y0 == y-1``. This boundary condition will result in ``y'0 == y'-1`` and ``y''0 == y''-1``. * 'clamped': The first derivative at curves ends are zero. Assuming a 1D `y`, ``bc_type=((1, 0.0), (1, 0.0))`` is the same condition. * 'natural': The second derivative at curve ends are zero. Assuming a 1D `y`, ``bc_type=((2, 0.0), (2, 0.0))`` is the same condition.

If `bc_type` is a 2-tuple, the first and the second value will be applied at the curve start and end respectively. The tuple values can be one of the previously mentioned strings (except 'periodic') or a tuple `(order, deriv_values)` allowing to specify arbitrary derivatives at curve ends:

* `order`: the derivative order, 1 or 2. * `deriv_value`: array_like containing derivative values, shape must be the same as `y`, excluding ``axis`` dimension. For example, if `y` is 1-D, then `deriv_value` must be a scalar. If `y` is 3-D with the shape (n0, n1, n2) and axis=2, then `deriv_value` must be 2-D and have the shape (n0, n1). extrapolate : ool, '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), ``extrapolate`` is set to 'periodic' for ``bc_type='periodic'`` and to True otherwise.

Attributes ---------- x : ndarray, shape (n,) Breakpoints. The same ``x`` which was passed to the constructor. c : ndarray, shape (4, n-1, ...) Coefficients of the polynomials on each segment. The trailing dimensions match the dimensions of `y`, excluding ``axis``. For example, if `y` is 1-d, then ``ck, i`` is a coefficient for ``(x-xi)**(3-k)`` on the segment between ``xi`` and ``xi+1``. axis : int Interpolation axis. The same axis which was passed to the constructor.

Methods ------- __call__ derivative antiderivative integrate roots

See Also -------- Akima1DInterpolator : Akima 1D interpolator. PchipInterpolator : PCHIP 1-D monotonic cubic interpolator. PPoly : Piecewise polynomial in terms of coefficients and breakpoints.

Notes ----- Parameters `bc_type` and ``interpolate`` work independently, i.e. the former controls only construction of a spline, and the latter only evaluation.

When a boundary condition is 'not-a-knot' and n = 2, it is replaced by a condition that the first derivative is equal to the linear interpolant slope. When both boundary conditions are 'not-a-knot' and n = 3, the solution is sought as a parabola passing through given points.

When 'not-a-knot' boundary conditions is applied to both ends, the resulting spline will be the same as returned by `splrep` (with ``s=0``) and `InterpolatedUnivariateSpline`, but these two methods use a representation in B-spline basis.

.. versionadded:: 0.18.0

Examples -------- In this example the cubic spline is used to interpolate a sampled sinusoid. You can see that the spline continuity property holds for the first and second derivatives and violates only for the third derivative.

>>> from scipy.interpolate import CubicSpline >>> import matplotlib.pyplot as plt >>> x = np.arange(10) >>> y = np.sin(x) >>> cs = CubicSpline(x, y) >>> xs = np.arange(-0.5, 9.6, 0.1) >>> fig, ax = plt.subplots(figsize=(6.5, 4)) >>> ax.plot(x, y, 'o', label='data') >>> ax.plot(xs, np.sin(xs), label='true') >>> ax.plot(xs, cs(xs), label='S') >>> ax.plot(xs, cs(xs, 1), label='S'') >>> ax.plot(xs, cs(xs, 2), label='S''') >>> ax.plot(xs, cs(xs, 3), label='S'''') >>> ax.set_xlim(-0.5, 9.5) >>> ax.legend(loc='lower left', ncol=2) >>> plt.show()

In the second example, the unit circle is interpolated with a spline. A periodic boundary condition is used. You can see that the first derivative values, ds/dx=0, ds/dy=1 at the periodic point (1, 0) are correctly computed. Note that a circle cannot be exactly represented by a cubic spline. To increase precision, more breakpoints would be required.

>>> theta = 2 * np.pi * np.linspace(0, 1, 5) >>> y = np.c_np.cos(theta), np.sin(theta) >>> cs = CubicSpline(theta, y, bc_type='periodic') >>> print('ds/dx=.1f ds/dy=.1f'.format(cs(0, 1)0, cs(0, 1)1)) ds/dx=0.0 ds/dy=1.0 >>> xs = 2 * np.pi * np.linspace(0, 1, 100) >>> fig, ax = plt.subplots(figsize=(6.5, 4)) >>> ax.plot(y:, 0, y:, 1, 'o', label='data') >>> ax.plot(np.cos(xs), np.sin(xs), label='true') >>> ax.plot(cs(xs):, 0, cs(xs):, 1, label='spline') >>> ax.axes.set_aspect('equal') >>> ax.legend(loc='center') >>> plt.show()

The third example is the interpolation of a polynomial y = x**3 on the interval 0 <= x<= 1. A cubic spline can represent this function exactly. To achieve that we need to specify values and first derivatives at endpoints of the interval. Note that y' = 3 * x**2 and thus y'(0) = 0 and y'(1) = 3.

>>> cs = CubicSpline(0, 1, 0, 1, bc_type=((1, 0), (1, 3))) >>> x = np.linspace(0, 1) >>> np.allclose(x**3, cs(x)) True

References ---------- .. 1 `Cubic Spline Interpolation <https://en.wikiversity.org/wiki/Cubic_Spline_Interpolation>`_ on Wikiversity. .. 2 Carl de Boor, 'A Practical Guide to Splines', Springer-Verlag, 1978.

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

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 : PPoly Piecewise polynomial of order k2 = k + n representing the antiderivative of this polynomial.

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.

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

Construct the piecewise polynomial without making checks.

Takes the same parameters as the constructor. Input arguments ``c`` and ``x`` must be arrays of the correct shape and type. The ``c`` array can only be of dtypes float and complex, and ``x`` array must have dtype float.

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

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 : PPoly Piecewise polynomial of order k2 = k - n representing the derivative of this polynomial.

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]``.

val extend : ?right:Py.Object.t -> c:[ `Size_k_m_ of Py.Object.t | `Ndarray of [> `Ndarray ] Np.Obj.t ] -> x:[ `Ndarray of [> `Ndarray ] Np.Obj.t | `Size of Py.Object.t ] -> [> tag ] Obj.t -> Py.Object.t

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. right Deprecated argument. Has no effect.

.. deprecated:: 0.19

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

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.

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

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.

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

Compute a definite integral over a piecewise polynomial.

Parameters ---------- a : float Lower integration bound b : float Upper integration bound extrapolate : ool, '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 : array_like Definite integral of the piecewise polynomial over a, b

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

Find real roots of the the piecewise polynomial.

Parameters ---------- discontinuity : bool, optional Whether to report sign changes across discontinuities at breakpoints as roots. extrapolate : ool, '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 : ndarray 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.

See Also -------- PPoly.solve

val solve : ?y:float -> ?discontinuity:bool -> ?extrapolate:[ `Periodic | `Bool of bool ] -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Find real solutions of the 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 : ool, '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 : ndarray 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.

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``:

>>> from scipy.interpolate import PPoly >>> pp = PPoly(np.array([1, -4, 3], [1, 0, 0]).T, -2, 1, 2) >>> pp.solve() array(-1., 1.)

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

Attribute x: get value or raise Not_found if None.

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

Attribute x: 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 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 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 : Format.formatter -> t -> unit

Pretty-print the object to a formatter.

OCaml

Innovation. Community. Security.