← Back
Editing: _interpolate.cpython-311.pyc
� d�c�V � � � g d �Z ddlZddlmZmZmZmZmZmZm Z m Z mZ ddlm Z ddlmZ ddlmZ ddlmZ ddlmZ dd lmZ dd lmZ ddlmZ ddlmZ dd lmZ ddlmZmZ d� Z dZ! G d� d� � Z"d� Z#d� Z$ G d� de� � Z% G d� d� � Z& G d� de&� � Z' G d� de&� � Z( G d� d� � Z)dS ))�interp1d�interp2d�lagrange�PPoly�BPoly�NdPPoly� N) �array� transpose�searchsorted� atleast_1d� atleast_2d�ravel�poly1d�asarray�intp)�comb)�prod� )�_fitpack_py)�dfitpack)�_fitpack)�_Interpolator1D)�_ppoly)�RectBivariateSpline)�_ndim_coords_from_arrays)�make_interp_spline�BSplinec �* � t | � � }t d� � }t |� � D ]d}t || � � }t |� � D ]8}||k r� | | | | z }|t d| | g� � |z z }�9||z }�e|S )ag Return a Lagrange interpolating polynomial. Given two 1-D arrays `x` and `w,` returns the Lagrange interpolating polynomial through the points ``(x, w)``. Warning: This implementation is numerically unstable. Do not expect to be able to use more than about 20 points even if they are chosen optimally. Parameters ---------- x : array_like `x` represents the x-coordinates of a set of datapoints. w : array_like `w` represents the y-coordinates of a set of datapoints, i.e., f(`x`). Returns ------- lagrange : `numpy.poly1d` instance The Lagrange interpolating polynomial. Examples -------- Interpolate :math:`f(x) = x^3` by 3 points. >>> import numpy as np >>> from scipy.interpolate import lagrange >>> x = np.array([0, 1, 2]) >>> y = x**3 >>> poly = lagrange(x, y) Since there are only 3 points, Lagrange polynomial has degree 2. Explicitly, it is given by .. math:: \begin{aligned} L(x) &= 1\times \frac{x (x - 2)}{-1} + 8\times \frac{x (x-1)}{2} \\ &= x (-2 + 3x) \end{aligned} >>> from numpy.polynomial.polynomial import Polynomial >>> Polynomial(poly.coef[::-1]).coef array([ 0., -2., 3.]) >>> import matplotlib.pyplot as plt >>> x_new = np.arange(0, 2.1, 0.1) >>> plt.scatter(x, y, label='data') >>> plt.plot(x_new, Polynomial(poly.coef[::-1])(x_new), label='Polynomial') >>> plt.plot(x_new, 3*x_new**2 - 2*x_new + 0*x_new, ... label=r"$3 x^2 - 2 x$", linestyle='-.') >>> plt.legend() >>> plt.show() � g �?)�lenr �range)�x�w�M�p�j�pt�k�facs �@/usr/lib/python3/dist-packages/scipy/interpolate/_interpolate.pyr r s� � �r �A���A��s���A� �1�X�X� � �� �A�a�D�\�\���q��� +� +�A��A�v�v���A�$�q��t�)�C��&�#��!��u��&�&�s�*�*�B�B� �R�����H� a� `interp2d` is deprecated in SciPy 1.10 and will be removed in SciPy 1.12.0. For legacy code, nearly bug-for-bug compatible replacements are `RectBivariateSpline` on regular grids, and `bisplrep`/`bisplev` for scattered 2D data. In new code, for regular grids use `RegularGridInterpolator` instead. For scattered data, prefer `LinearNDInterpolator` or `CloughTocher2DInterpolator`. For more details see `https://gist.github.com/ev-br/8544371b40f414b7eaf3fe6217209bff` c � � e Zd ZdZ ej d e�� � d d�� � Z ej d e�� � dd �� � ZdS )r a interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=None) .. deprecated:: 1.10.0 `interp2d` is deprecated in SciPy 1.10 and will be removed in SciPy 1.12.0. For legacy code, nearly bug-for-bug compatible replacements are `RectBivariateSpline` on regular grids, and `bisplrep`/`bisplev` for scattered 2D data. In new code, for regular grids use `RegularGridInterpolator` instead. For scattered data, prefer `LinearNDInterpolator` or `CloughTocher2DInterpolator`. For more details see `https://gist.github.com/ev-br/8544371b40f414b7eaf3fe6217209bff` Interpolate over a 2-D grid. `x`, `y` and `z` are arrays of values used to approximate some function f: ``z = f(x, y)`` which returns a scalar value `z`. This class returns a function whose call method uses spline interpolation to find the value of new points. If `x` and `y` represent a regular grid, consider using `RectBivariateSpline`. If `z` is a vector value, consider using `interpn`. Note that calling `interp2d` with NaNs present in input values, or with decreasing values in `x` an `y` results in undefined behaviour. Methods ------- __call__ Parameters ---------- x, y : array_like Arrays defining the data point coordinates. The data point coordinates need to be sorted by increasing order. If the points lie on a regular grid, `x` can specify the column coordinates and `y` the row coordinates, for example:: >>> x = [0,1,2]; y = [0,3]; z = [[1,2,3], [4,5,6]] Otherwise, `x` and `y` must specify the full coordinates for each point, for example:: >>> x = [0,1,2,0,1,2]; y = [0,0,0,3,3,3]; z = [1,4,2,5,3,6] If `x` and `y` are multidimensional, they are flattened before use. z : array_like The values of the function to interpolate at the data points. If `z` is a multidimensional array, it is flattened before use assuming Fortran-ordering (order='F'). The length of a flattened `z` array is either len(`x`)*len(`y`) if `x` and `y` specify the column and row coordinates or ``len(z) == len(x) == len(y)`` if `x` and `y` specify coordinates for each point. kind : {'linear', 'cubic', 'quintic'}, optional The kind of spline interpolation to use. Default is 'linear'. copy : bool, optional If True, the class makes internal copies of x, y and z. If False, references may be used. The default is to copy. bounds_error : bool, optional If True, when interpolated values are requested outside of the domain of the input data (x,y), a ValueError is raised. If False, then `fill_value` is used. fill_value : number, optional If provided, the value to use for points outside of the interpolation domain. If omitted (None), values outside the domain are extrapolated via nearest-neighbor extrapolation. See Also -------- RectBivariateSpline : Much faster 2-D interpolation if your input data is on a grid bisplrep, bisplev : Spline interpolation based on FITPACK BivariateSpline : a more recent wrapper of the FITPACK routines interp1d : 1-D version of this function RegularGridInterpolator : interpolation on a regular or rectilinear grid in arbitrary dimensions. interpn : Multidimensional interpolation on regular grids (wraps `RegularGridInterpolator` and `RectBivariateSpline`). Notes ----- The minimum number of data points required along the interpolation axis is ``(k+1)**2``, with k=1 for linear, k=3 for cubic and k=5 for quintic interpolation. The interpolator is constructed by `bisplrep`, with a smoothing factor of 0. If more control over smoothing is needed, `bisplrep` should be used directly. The coordinates of the data points to interpolate `xnew` and `ynew` have to be sorted by ascending order. `interp2d` is legacy and is not recommended for use in new code. New code should use `RegularGridInterpolator` instead. Examples -------- Construct a 2-D grid and interpolate on it: >>> import numpy as np >>> from scipy import interpolate >>> x = np.arange(-5.01, 5.01, 0.25) >>> y = np.arange(-5.01, 5.01, 0.25) >>> xx, yy = np.meshgrid(x, y) >>> z = np.sin(xx**2+yy**2) >>> f = interpolate.interp2d(x, y, z, kind='cubic') Now use the obtained interpolation function and plot the result: >>> import matplotlib.pyplot as plt >>> xnew = np.arange(-5.01, 5.01, 1e-2) >>> ynew = np.arange(-5.01, 5.01, 1e-2) >>> znew = f(xnew, ynew) >>> plt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-') >>> plt.show() )�old_name�message�linearTFNc �~ �� t |� � }t |� � }t |� � }|j t |� � t |� � z k }|r�|j dk r6|j t |� � t |� � fk rt d� � �t j |dd � |d d� k � � s(t j |� � } || }|d d �| f }t j |dd � |d d� k � � s(t j |� � } || }|| d d �f }t |j � � }nmt |� � }t |� � t |� � k rt d� � �t |� � t |� � k rt d� � �dddd �} | | x}}n\# t $ rO} t d t |� � � dd� t t | � � � � � d �� � | �d } ~ ww xY w|s t j |||||d�� � | _ nYt% j |||d d d d ||d�� � \ }}}}}}}|d |� |d |� |d ||z dz ||z dz z � ||f| _ || _ || _ �fd�|||fD � � \ | _ | _ | _ t j |� � t j |� � c| _ | _ t j |� � t j |� � c| _ | _ d S )N� zdWhen on a regular grid with x.size = m and y.size = n, if z.ndim == 2, then z must have shape (n, m)r ���z8x and y must have equal lengths for non rectangular gridz3Invalid length for input z for non rectangular grid� � )r/ �cubic�quinticzUnsupported interpolation type z, must be either of z, �.r )�kx�ky�sc �2 �� g | ]}t |�� � � ��S )��copy)r )�.0�ar= s �r* � <listcomp>z%interp2d.__init__.<locals>.<listcomp>& s&