| Server IP : 35.80.110.71 / Your IP : 216.73.216.221 Web Server : Apache/2.4.58 (Ubuntu) System : Linux ip-172-31-21-44 6.17.0-1019-aws #19~24.04.1-Ubuntu SMP Tue Jun 23 18:53:06 UTC 2026 x86_64 User : ubuntu ( 1000) PHP Version : 8.3.31 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /lib/python3/dist-packages/scipy/interpolate/__pycache__/ |
Upload File : |
�
x�f � �" � d dl Z d dlmZ d dlZd dlmZ d dlmZm Z m
Z
mZmZm
Z
d dlmZ ddlmZ ddlmZ d d lmZ d d
lmZ d dlmZ g d�Zd
� Zd d�Zd� Zd� Z G d� d� Zd� Zd� Z d� Z!d� Z"d� Z#d� Z$d� Z%d� Z& d!d�Z'd"d�Z(d� Z)d� Z*d#d�Z+y)$� N)�prod)�normalize_axis_index)�get_lapack_funcs�LinAlgError�cholesky_banded�cho_solve_banded�solve�solve_banded)�minimize_scalar� )�_bspl)�
_fitpack_impl)� csr_array)�poch)�combinations)�BSpline�make_interp_spline�make_lsq_spline�make_smoothing_splinec � � t j | t j � rt j S t j S )z>Return np.complex128 for complex dtypes, np.float64 otherwise.)�np�
issubdtype�complexfloating�complex_�float_��dtypes �=/usr/lib/python3/dist-packages/scipy/interpolate/_bsplines.py�
_get_dtyper s* � � �}�}�U�B�.�.�/��{�{���y�y�� c �� � t j | � } t | j � }| j |d�� } |r.t j
| � j
� st d� �| S )zConvert the input into a C contiguous float array.
NB: Upcasts half- and single-precision floats to double precision.
F)�copyz$Array must not contain infs or nans.)r �ascontiguousarrayr r �astype�isfinite�all�
ValueError)�x�check_finite�dtyps r �_as_float_arrayr+ s[ � �
���Q��A��a�g�g��D� ����E��"�A��B�K�K��N�.�.�0��?�@�@��Hr c � � |dk( ryt j t d|dz � D �cg c]
}||| |z z
�� c}� S c c}w )z�
Dual polynomial of the B-spline B_{j,k,t} -
polynomial which is associated with B_{j,k,t}:
$p_{j,k}(y) = (y - t_{j+1})(y - t_{j+2})...(y - t_{j+k})$
r r )r r �range)�j�k�t�y�is r �
_dual_polyr3 ) sA � � �A�v��
�7�7�E�!�Q��U�O�<�q�Q��1�q�5��\�<�=�=��<s �A c � � |dk( rt | |||� S ||k( rt d|� S t t t | dz | |z dz � |� � }d}t t |� t |d � z � D ]K }|t
j t d|dz � D �cg c] }| |z |||z vr||| |z z
�� c}� z
}�M |S c c}w )z=
d-th derivative of the dual polynomial $p_{j,k}(y)$
r r )r3 r �listr r- �lenr r ) r. r/ r1 �dr0 �comb�resr2 �ps r �_diff_dual_polyr; 4 s� � � �A�v��!�Q��1�%�%��A�v��A�q�z����U�1�q�5�!�a�%�!�)�4�a�8�9�D�
�C�
�3�t�9�s�4��7�|�+�
,� 7���r�w�w��a��Q��� 6�1���E�$�q�!�t�*�4� �Q�q�1�u�X�� 6� 7� 7��7� �J��6s �Cc � � � e Zd ZdZd� fd� Zedd�� Zed� � Zedd�� Z edd�� Z
dd�Zd� Zd � Z
dd
�Zdd�Zdd�Zedd
�� Z� xZS )r a� 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, ``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, 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
design_matrix
from_power_basis
Notes
-----
B-spline basis elements are defined via
.. math::
B_{i, 0}(x) = 1, \textrm{if $t_i \le x < t_{i+1}$, otherwise $0$,}
B_{i, k}(x) = \frac{x - t_i}{t_{i+k} - t_i} B_{i, k-1}(x)
+ \frac{t_{i+k+1} - x}{t_{i+k+1} - t_{i+1}} B_{i+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, ``c[j]`` with
``j > n``, are ignored.
- B-spline basis elements of degree `k` form a partition of unity on the
*base interval*, ``t[k] <= x <= t[n]``.
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))
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
>>> 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()
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.
c �D �� t �| � � t j |� | _ t j |� | _ t j |t
j �� | _
|dk( r|| _ nt |� | _ | j j d | j z
dz
}t || j j � }|| _ |dk7 r&t j"