
    (phM                         S r SSKrSSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SSKJr  SS	KJrJrJr  S
/r1 Skr1 SkrSSSSSS.rS rS r " S S
5      rg)zModule for RBF interpolation.    N)combinations_with_replacement)LinAlgError)KDTree)comb)dgesv   )_build_system_build_evaluation_coefficients_polynomial_matrixRBFInterpolator>   cubiclinearquinticgaussianmultiquadricinverse_quadraticthin_plate_splineinverse_multiquadric>   r   r   r   r      )r   r   r   r   r   c                    [        X-   U SS9n[        R                  " X 4[        R                  " S5      S9nSn[	        US-   5       H;  n[        [	        U 5      U5       H  nU H  nX4U4==   S-  ss'   M     US-  nM!     M=     U$ )a,  Return the powers for each monomial in a polynomial.

Parameters
----------
ndim : int
    Number of variables in the polynomial.
degree : int
    Degree of the polynomial.

Returns
-------
(nmonos, ndim) int ndarray
    Array where each row contains the powers for each variable in a
    monomial.

T)exactlongdtyper   r   )r   npzerosr   ranger   )ndimdegreenmonosoutcountdegmonovars           O/var/www/html/venv/lib/python3.13/site-packages/scipy/interpolate/_rbfinterp.py_monomial_powersr'   2   s    " &-T2F
((F>&)9
:CEVaZ 1%+sCD 3J1$  QJE D ! J    c                 B   [        XX#XE5      u  pgp[        XgSSS9u    pnUS:  a  [        SU*  S35      eUS:  aa  SnUR                  S   nUS:  a?  [	        X-
  U	-  U5      n[
        R                  R                  U5      nUU:  a	  SU SU S	3n[        U5      eXU4$ )
a  Build and solve the RBF interpolation system of equations.

Parameters
----------
y : (P, N) float ndarray
    Data point coordinates.
d : (P, S) float ndarray
    Data values at `y`.
smoothing : (P,) float ndarray
    Smoothing parameter for each data point.
kernel : str
    Name of the RBF.
epsilon : float
    Shape parameter.
powers : (R, N) int ndarray
    The exponents for each monomial in the polynomial.

Returns
-------
coeffs : (P + R, S) float ndarray
    Coefficients for each RBF and monomial.
shift : (N,) float ndarray
    Domain shift used to create the polynomial matrix.
scale : (N,) float ndarray
    Domain scaling used to create the polynomial matrix.

T)overwrite_aoverwrite_br   zThe z"-th argument had an illegal value.zSingular matrix.zqSingular matrix. The matrix of monomials evaluated at the data point coordinates does not have full column rank (/z).)	r	   r   
ValueErrorshaper   r   linalgmatrix_rankr   )yd	smoothingkernelepsilonpowerslhsrhsshiftscale_coeffsinfomsgr    pmatranks                    r&   _build_and_solve_systemrA   R   s    8 +	i
Ce sTtLAq$ax4w&HIJJ	 aA:%qy%&7@D99((.Df}!F!F82/  #r(   c                   >    \ rS rSrSr     SS jr S	S jrS rSrg)
r      aq  Radial basis function (RBF) interpolation in N dimensions.

Parameters
----------
y : (npoints, ndims) array_like
    2-D array of data point coordinates.
d : (npoints, ...) array_like
    N-D array of data values at `y`. The length of `d` along the first
    axis must be equal to the length of `y`. Unlike some interpolators, the
    interpolation axis cannot be changed.
neighbors : int, optional
    If specified, the value of the interpolant at each evaluation point
    will be computed using only this many nearest data points. All the data
    points are used by default.
smoothing : float or (npoints, ) array_like, optional
    Smoothing parameter. The interpolant perfectly fits the data when this
    is set to 0. For large values, the interpolant approaches a least
    squares fit of a polynomial with the specified degree. Default is 0.
kernel : str, optional
    Type of RBF. This should be one of

        - 'linear'               : ``-r``
        - 'thin_plate_spline'    : ``r**2 * log(r)``
        - 'cubic'                : ``r**3``
        - 'quintic'              : ``-r**5``
        - 'multiquadric'         : ``-sqrt(1 + r**2)``
        - 'inverse_multiquadric' : ``1/sqrt(1 + r**2)``
        - 'inverse_quadratic'    : ``1/(1 + r**2)``
        - 'gaussian'             : ``exp(-r**2)``

    Default is 'thin_plate_spline'.
epsilon : float, optional
    Shape parameter that scales the input to the RBF. If `kernel` is
    'linear', 'thin_plate_spline', 'cubic', or 'quintic', this defaults to
    1 and can be ignored because it has the same effect as scaling the
    smoothing parameter. Otherwise, this must be specified.
degree : int, optional
    Degree of the added polynomial. For some RBFs the interpolant may not
    be well-posed if the polynomial degree is too small. Those RBFs and
    their corresponding minimum degrees are

        - 'multiquadric'      : 0
        - 'linear'            : 0
        - 'thin_plate_spline' : 1
        - 'cubic'             : 1
        - 'quintic'           : 2

    The default value is the minimum degree for `kernel` or 0 if there is
    no minimum degree. Set this to -1 for no added polynomial.

Notes
-----
An RBF is a scalar valued function in N-dimensional space whose value at
:math:`x` can be expressed in terms of :math:`r=||x - c||`, where :math:`c`
is the center of the RBF.

An RBF interpolant for the vector of data values :math:`d`, which are from
locations :math:`y`, is a linear combination of RBFs centered at :math:`y`
plus a polynomial with a specified degree. The RBF interpolant is written
as

.. math::
    f(x) = K(x, y) a + P(x) b,

where :math:`K(x, y)` is a matrix of RBFs with centers at :math:`y`
evaluated at the points :math:`x`, and :math:`P(x)` is a matrix of
monomials, which span polynomials with the specified degree, evaluated at
:math:`x`. The coefficients :math:`a` and :math:`b` are the solution to the
linear equations

.. math::
    (K(y, y) + \lambda I) a + P(y) b = d

and

.. math::
    P(y)^T a = 0,

where :math:`\lambda` is a non-negative smoothing parameter that controls
how well we want to fit the data. The data are fit exactly when the
smoothing parameter is 0.

The above system is uniquely solvable if the following requirements are
met:

    - :math:`P(y)` must have full column rank. :math:`P(y)` always has full
      column rank when `degree` is -1 or 0. When `degree` is 1,
      :math:`P(y)` has full column rank if the data point locations are not
      all collinear (N=2), coplanar (N=3), etc.
    - If `kernel` is 'multiquadric', 'linear', 'thin_plate_spline',
      'cubic', or 'quintic', then `degree` must not be lower than the
      minimum value listed above.
    - If `smoothing` is 0, then each data point location must be distinct.

When using an RBF that is not scale invariant ('multiquadric',
'inverse_multiquadric', 'inverse_quadratic', or 'gaussian'), an appropriate
shape parameter must be chosen (e.g., through cross validation). Smaller
values for the shape parameter correspond to wider RBFs. The problem can
become ill-conditioned or singular when the shape parameter is too small.

The memory required to solve for the RBF interpolation coefficients
increases quadratically with the number of data points, which can become
impractical when interpolating more than about a thousand data points.
To overcome memory limitations for large interpolation problems, the
`neighbors` argument can be specified to compute an RBF interpolant for
each evaluation point using only the nearest data points.

.. versionadded:: 1.7.0

See Also
--------
NearestNDInterpolator
LinearNDInterpolator
CloughTocher2DInterpolator

References
----------
.. [1] Fasshauer, G., 2007. Meshfree Approximation Methods with Matlab.
    World Scientific Publishing Co.

.. [2] http://amadeus.math.iit.edu/~fass/603_ch3.pdf

.. [3] Wahba, G., 1990. Spline Models for Observational Data. SIAM.

.. [4] http://pages.stat.wisc.edu/~wahba/stat860public/lect/lect8/lect8.pdf

Examples
--------
Demonstrate interpolating scattered data to a grid in 2-D.

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from scipy.interpolate import RBFInterpolator
>>> from scipy.stats.qmc import Halton

>>> rng = np.random.default_rng()
>>> xobs = 2*Halton(2, seed=rng).random(100) - 1
>>> yobs = np.sum(xobs, axis=1)*np.exp(-6*np.sum(xobs**2, axis=1))

>>> xgrid = np.mgrid[-1:1:50j, -1:1:50j]
>>> xflat = xgrid.reshape(2, -1).T
>>> yflat = RBFInterpolator(xobs, yobs)(xflat)
>>> ygrid = yflat.reshape(50, 50)

>>> fig, ax = plt.subplots()
>>> ax.pcolormesh(*xgrid, ygrid, vmin=-0.25, vmax=0.25, shading='gouraud')
>>> p = ax.scatter(*xobs.T, c=yobs, s=50, ec='k', vmin=-0.25, vmax=0.25)
>>> fig.colorbar(p)
>>> plt.show()

Nc           	         [         R                  " U[        SS9nUR                  S:w  a  [	        S5      eUR
                  u  p[         R                  " U5      (       a  [        O[        n
[         R                  " X*SS9nUR
                  S   U:w  a  [	        SU S35      eUR
                  SS  nUR                  US	45      nUR                  [        5      n[         R                  " U5      (       a  [         R                  " X[        S
9nO:[         R                  " U[        SS9nUR
                  U4:w  a  [	        SU S35      eUR                  5       nU[        ;  a  [	        S[         S35      eUc   U[        ;   a  SnO[	        S[         S35      e[        U5      n[        R!                  US	5      nUc  [#        US5      nOM[%        U5      nUS	:  a  [	        S5      eS	Us=:  a  U:  a$  O  O![&        R(                  " SU SU S3[*        SS9  Uc  UnO[%        [-        X85      5      nUn[/        X5      nUR
                  S   U:  a"  [	        SUR
                  S    SU SU	 S35      eUc&  [1        XXEXn5      u  nnnXl        UU l        UU l        O[9        U5      U l        Xl        X l        Xl         Xl!        X0l"        X@l#        XPl$        X`l%        Xl&        g )NCr   orderr   z"`y` must be a 2-dimensional array.r   z.Expected the first axis of `d` to have length .r   r   z3Expected `smoothing` to be a scalar or have shape (z,).z`kernel` must be one of g      ?z6`epsilon` must be specified if `kernel` is not one of z`degree` must be at least -1.z`degree` should not be below z except -1 when `kernel` is 'zk'.The interpolant may not be uniquely solvable, and the smoothing parameter may have an unintuitive effect.)
stacklevelz	At least z+ data points are required when `degree` is z! and the number of dimensions is )'r   asarrayfloatr   r-   r.   iscomplexobjcomplexreshapeviewisscalarfulllower
_AVAILABLE_SCALE_INVARIANT_NAME_TO_MIN_DEGREEgetmaxintwarningswarnUserWarningminr'   rA   _shift_scale_coeffsr   _treer1   r2   d_shaped_dtype	neighborsr3   r4   r5   r6   )selfr1   r2   rd   r3   r4   r5   r   nyr   rc   rb   
min_degreenobsr6   r9   r:   r<   s                     r&   __init__RBFInterpolator.__init__  s    JJqS166Q;ABB77__Q//'UJJqs3771:@AF  ''!"+IIr2h FF5M;;y!!U;I

9EEI2%' t3  
 #7
|1EFF?)) L'(+ 
 GnG(,,VR8
>Q'F[F{ !@AAf)z)3J< @))/ 1*+
  A D C	./ID!$/ <<?T!FLLO, -%h&GvQP 
 #:i$ E5&
  KDK!DL  DJ""r(   c           
         UR                   u  pxU R                  c  [        U5      n	OU R                  n	X`R                  R                   S   U	-   -  S-   n
X::  a  [        R
                  " XpR                  R                   S   4[        S9n[        SXz5       H\  n[        XX-   2SS24   UU R                  U R                  U R                  UU5      n[        R                  " X5      XX-   2SS24'   M^     U$ [        UUU R                  U R                  U R                  UU5      n[        R                  " X5      nU$ )a$  
Evaluate the interpolation while controlling memory consumption.
We chunk the input if we need more memory than specified.

Parameters
----------
x : (Q, N) float ndarray
    array of points on which to evaluate
y: (P, N) float ndarray
    array of points on which we know function values
shift: (N, ) ndarray
    Domain shift used to create the polynomial matrix.
scale : (N,) float ndarray
    Domain scaling used to create the polynomial matrix.
coeffs: (P+R, S) float ndarray
    Coefficients in front of basis functions
memory_budget: int
    Total amount of memory (in units of sizeof(float)) we wish
    to devote for storing the array of coefficients for
    interpolated points. If we need more memory than that, we
    chunk the input.

Returns
-------
(Q, S) float ndarray
Interpolated array
Nr   r   r   )r.   rd   lenr6   r   emptyr2   rL   r   r
   r4   r5   dot)re   xr1   r9   r:   r<   memory_budgetnxr   nnei	chunksizer!   ivecs                 r&   _chunk_evaluator RBFInterpolator._chunk_evaluator  s"   H 77>>!q6D>>D!kk&7&7&:T&ABQF	?((BQ0>C1b,4oq()KKLLKK +-&&*=amOQ&' -( 
 1C &&%C
r(   c           
         [         R                  " U[        SS9nUR                  S:w  a  [	        S5      eUR
                  u  p#X0R                  R
                  S   :w  a&  [	        SU R                  R
                  S    S35      e[        UR                  U R                  R                  -   U R                  R                  -   S5      nU R                  c>  U R                  UU R                  U R                  U R                  U R                  US
9nGOU R                  R!                  XR                  5      u  pgU R                  S:X  a	  US	S	2S	4   n[         R"                  " USS9n[         R$                  " USSS9u  px[         R&                  " US5      n[)        [+        U5      5       Vs/ s H  n/ PM     n	n[-        U5       H  u  pX   R/                  U
5        M     [         R0                  " X R                  R
                  S   4[        S9n[3        X5       H~  u  pX   nU R                  U   nU R                  U   nU R4                  U   n[7        UUUU R8                  U R:                  U R<                  5      u  nnnU R                  UUUUUUS
9X\'   M     UR?                  U R@                  5      nUR'                  U4U RB                  -   5      nU$ s  snf )zEvaluate the interpolant at `x`.

Parameters
----------
x : (Q, N) array_like
    Evaluation point coordinates.

Returns
-------
(Q, ...) ndarray
    Values of the interpolant at `x`.

rE   rF   r   z"`x` must be a 2-dimensional array.r   z/Expected the second axis of `x` to have length rH   @B N)rp   )axisTr   )return_inverserz   )rI   r   )"r   rK   rL   r   r-   r.   r1   rX   sizer2   rd   rv   r^   r_   r`   ra   querysortuniquerO   r   rl   	enumerateappendrm   zipr3   rA   r4   r5   r6   rP   rc   rb   )re   ro   rq   r   rp   r!   r;   yindicesinvxindicesrt   jxidxyidxxnbrynbrdnbrsnbrr9   r:   r<   s                        r&   __call__RBFInterpolator.__call__  s    JJqS166Q;ABB7766<<?"N $Q03 4 4 AFFTVV[[0466;;>H>>!''+ ( -C ****1nn=KA~~"#AtG, wwxa0HIIht!LMH**S%(C %*#h-$89$8q$8H9!#""1% ' ((BQ0>C!(5
 wvvd|vvd|~~d+'>KKLLKK($uf !11"/ 2 1	! 60 hht||$kk2&4<</0
? :s   /K)r`   r_   r^   ra   r2   rc   rb   r5   r4   rd   r6   r3   r1   )Ng        r   NN)ry   )	__name__
__module____qualname____firstlineno____doc__ri   rv   r   __static_attributes__ r(   r&   r   r      s3    Vr  +kh "AFWr(   )r   rZ   	itertoolsr   numpyr   numpy.linalgr   scipy.spatialr   scipy.specialr   scipy.linalg.lapackr   _rbfinterp_pythranr	   r
   r   __all__rT   rU   rV   r'   rA   r   r   r(   r&   <module>r      sr    #  3  $    %5 5
 
	
 G   @1 h` `r(   