§
    fŠtjæ  ã            	       ó’   — d Z ddlmZ ddlmZ d„ Zd„ Zd„ Zd„ Zd	„ Z	d
„ Z
d„ Zd„ Zd„ Zd„ Zeee	e
eeeedœZd„ Zd„ Zd„ Zd„ Zd„ ZdS )aj  
'Generic' Array API backend for RBF interpolation.

The general logic is this: `_rbfinterp.py` implements the user API and calls
into either `_rbfinterp_np` (the "numpy backend"), or `_rbfinterp_xp` (the
"generic backend".

The numpy backend offloads performance-critical computations to the
pythran-compiled `_rbfinterp_pythran` extension. This way, the call chain is

    _rbfinterp.py <-- _rbfinterp_np.py <-- _rbfinterp_pythran.py

The "generic" backend here is a drop-in replacement of the API of
`_rbfinterp_np.py` for use in `_rbfinterp.py` with non-numpy arrays.

The implementation closely follows `_rbfinterp_np + _rbfinterp_pythran`, with
the following differences:

  -  We used vectorized code not explicit loops in `_build_system` and
     `_build_evaluation_coefficients`; this is more torch/jax friendly;
  - RBF kernels are also "vectorized" and not scalar: they receive an
    array of norms not a single norm;
  - RBF kernels accept an extra xp= argument;

In general, we would prefer less code duplication. The main blocker ATM is
that pythran cannot compile functions with an xp= argument where xp is numpy.
é    )ÚLinAlgErroré   )Ú_monomial_powers_implc                 ó¢   — t          | |¦  «        }|                     |¦  «        }|j        d         dk    r|                     |d| f¦  «        }|S )Nr   )r   ÚasarrayÚshapeÚreshape)ÚndimÚdegreeÚxpÚouts       ú]/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/interpolate/_rbfinterp_xp.pyÚ_monomial_powersr       sM   € Ý
  fÑ
-Ô
-€CØ
�*Š*�S‰/Œ/€CØ
„y�„|�qÒÐØ�jŠj˜˜q $˜iÑ(Ô(ˆØ€Jó    c           	      ó`  — t          | ||||||¦  «        \  }}}	}
	 |j                             ||¦  «        }ns# t          $ rf d}|j        d         }|dk    rAt          | |	z
  |
z  ||¬¦  «        }|j                             |¦  «        }||k     r	d|› d|› d�}t          |¦  «        ‚w xY w|	|
|fS )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.

    zSingular matrixr   )r   zqSingular matrix. The matrix of monomials evaluated at the data point coordinates does not have full column rank (ú/z).)Ú_build_systemÚlinalgÚsolveÚ	Exceptionr   Úpolynomial_matrixÚmatrix_rankr   )ÚyÚdÚ	smoothingÚkernelÚepsilonÚpowersr   ÚlhsÚrhsÚshiftÚscaleÚcoeffsÚmsgÚnmonosÚpmatÚranks                   r   Ú_build_and_solve_systemr(   (   s   € õ8 +Ø	ˆ1ˆi˜ ¨&°"ñ
ô 
Ñ€Cˆˆe�UðØ”—’  cÑ*Ô*ˆˆøÝð ð ð ð
  ˆØ”˜a”ˆØ�AŠ:ˆ:Ý$ a¨%¡i°Ñ%6¸À2ÐFÑFÔFˆDØ”9×(Ò(¨Ñ.Ô.ˆDØ�fŠ}ˆ}ð/à!ð/ð /à$*ð/ð /ð /ð õ
 ˜#ÑÔÐð!øøøð$ �%˜ÐÐs
   œ8 ¸A0B(c                 ó   — |  S ©N© ©Úrr   s     r   Úlinearr.   ^   s	   € Øˆ2€Ir   c                 ój   — |                      | dk    d| dz  |                     | ¦  «        z  ¦  «        S )Nr   é   )ÚwhereÚlogr,   s     r   Úthin_plate_spliner3   b   s/   € à�8Š8�A˜’F˜A˜q !™t b§f¢f¨Q¡i¤iÑ/Ñ0Ô0Ð0r   c                 ó   — | dz  S )Né   r+   r,   s     r   Úcubicr6   g   s   € Øˆa‰4€Kr   c                 ó   — | dz   S )Né   r+   r,   s     r   Úquinticr9   k   s   € Øˆq‰Dˆ5€Lr   c                 ó:   — |                      | dz  dz   ¦  «         S )Nr0   r   ©Úsqrtr,   s     r   Úmultiquadricr=   o   s   € Ø�GŠG�A�q‘D˜1‘HÑÔÐÐr   c                 ó>   — d|                      | dz  dz   ¦  «        z  S ©Nç      ð?r0   r;   r,   s     r   Úinverse_multiquadricrA   s   s!   € Ø�—’˜˜A™ ™Ñ$Ô$Ñ$Ð$r   c                 ó   — d| dz  dz   z  S r?   r+   r,   s     r   Úinverse_quadraticrC   w   s   € Ø�!�Q‘$˜‘*ÑÐr   c                 ó4   — |                      | dz   ¦  «        S )Nr0   )Úexpr,   s     r   ÚgaussianrF   {   s   € Ø�6Š6�1�a‘4�%‰=Œ=Ðr   )r.   r3   r6   r9   r=   rA   rC   rF   c           
      óˆ   —  ||j                              | ddd…dd…f         | dd…ddd…f         z
  d¬¦  «        |¦  «        S )z+Evaluate RBFs, with centers at `x`, at `x`.Néÿÿÿÿ©Úaxis)r   Úvector_norm)ÚxÚkernel_funcr   s      r   Úkernel_matrixrN   ‹   sY   € àˆ;Ø
Œ	×Ò˜a  a a a¨¨¨ 
œm¨a°°°°4¸¸¸°
¬mÑ;À"ÐÑEÔEÀrñô ð r   c                 óP   — |                      | dd…ddd…f         |z  d¬¦  «        S )z9Evaluate monomials, with exponents from `powers`, at `x`.NrH   rI   )Úprod)rL   r   r   s      r   r   r   ’   s/   € à�7Š7�1�Q�Q�Q˜˜a˜a˜a�Z”= FÑ*°ˆ7Ñ4Ô4Ð4r   c           
      ó  — |j         d         }|j         d         }t          |         }	|                     | d¬¦  «        }
|                     | d¬¦  «        }||
z   dz  }||
z
  dz  }|                     |dk    d|¦  «        }| |z  }| |z
  |z  }t          ||	|¦  «        }t          |||¦  «        }|                     |                     ||fd¬¦  «        |                     |j        | 	                    ||f¦  «        fd¬¦  «        gd¬¦  «        | 
                    |                     || 	                    |¦  «        g¦  «        ¦  «        z   }|                     || 	                    ||f¦  «        gd¬¦  «        }||||fS )a=  Build the system used to solve for the RBF interpolant coefficients.

    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
    -------
    lhs : (P + R, P + R) float ndarray
        Left-hand side matrix.
    rhs : (P + R, S) float ndarray
        Right-hand side matrix.
    shift : (N,) float ndarray
        Domain shift used to create the polynomial matrix.
    scale : (N,) float ndarray
        Domain scaling used to create the polynomial matrix.

    r   r   rI   r0   g        r@   )r   ÚNAME_TO_FUNCÚminÚmaxr1   rN   r   ÚconcatÚTÚzerosÚdiag)r   r   r   r   r   r   r   Úsr-   rM   ÚminsÚmaxsr!   r"   ÚyepsÚyhatÚout_kernelsÚout_polyr   r    s                       r   r   r   —   sŽ  € ð< 	
Œ�Œ
€AØŒ�QŒ€AÝ˜vÔ&€Kð �6Š6�!˜!ˆ6ÑÔ€DØ�6Š6�!˜!ˆ6ÑÔ€DØ�D‰[˜!‰O€EØ�D‰[˜!‰O€Eð �HŠH�U˜c’\ 3¨Ñ.Ô.€EàˆW‰9€DØ�‰I�uÑ€Då   {°BÑ7Ô7€KÝ   v¨rÑ2Ô2€Hà
�)Š)à	�Š�K Ð*°ˆÑ	3Ô	3Ø	�Š�H”J §¢¨!¨Q¨Ñ 0Ô 0Ð1¸ˆÑ	:Ô	:ð	
ð ð ñ ô ð
 —’˜Ÿ	š	 9¨b¯hªh°q©k¬kÐ":Ñ;Ô;Ñ<Ô<ñ=€Cð �)Š)�Q˜Ÿš ! Q Ñ(Ô(Ð)°ˆ)Ñ
2Ô
2€Cà��U˜EÐ!Ð!r   c                 óB  — t           |         }||z  }	| |z  }
| |z
  |z  }|                      ||j                             |
dd…ddd…f         |	ddd…dd…f         z
  d¬¦  «        |¦  «        |                     |dd…ddd…f         |z  d¬¦  «        gd¬¦  «        }|S )a�  Construct the coefficients needed to evaluate
    the RBF.

    Parameters
    ----------
    x : (Q, N) float ndarray
        Evaluation point coordinates.
    y : (P, N) float ndarray
        Data point coordinates.
    kernel : str
        Name of the RBF.
    epsilon : float
        Shape parameter.
    powers : (R, N) int ndarray
        The exponents for each monomial in the polynomial.
    shift : (N,) float ndarray
        Shifts the polynomial domain for numerical stability.
    scale : (N,) float ndarray
        Scales the polynomial domain for numerical stability.

    Returns
    -------
    (Q, P + R) float ndarray

    NrH   rI   )rR   rU   r   rK   rP   )rL   r   r   r   r   r!   r"   r   rM   r\   ÚxepsÚxhatÚvecs                r   Ú_build_evaluation_coefficientsrd   Õ   sâ   € õ8 ˜vÔ&€KàˆW‰9€DØˆW‰9€DØ�‰I�uÑ€Dð �)Š)àˆKØ”	×%Ò%Ø˜˜˜˜D ! ! !˜Ô$ t¨D°!°!°!°Q°Q°Q¨JÔ'7Ñ7¸bð &ñ ô àñô ð
 �GŠG�D˜˜˜˜D ! ! !˜Ô$¨Ñ.°RˆGÑ8Ô8ð	
ð ð ñ 	ô 	€Cð €Jr   c	           
      ó8   — t          | |||||||¦  «        }	|	|z  S r*   )rd   )
rL   r   r   r   r   r!   r"   r#   r   rc   s
             r   Úcompute_interpolationrf     s.   € Ý
(Ø	ˆ1ˆf�g˜v u¨e°Rñô €Cð �‰<Ðr   N)Ú__doc__Únumpy.linalgr   Ú_rbfinterp_commonr   r   r(   r.   r3   r6   r9   r=   rA   rC   rF   rR   rN   r   r   rd   rf   r+   r   r   ú<module>rj      sI  ððð ð6 %Ð $Ð $Ð $Ð $Ð $Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4ðð ð ð3 ð 3 ð 3 ðlð ð ð1ð 1ð 1ð
ð ð ðð ð ðð ð ð%ð %ð %ðð ð ðð ð ð
 Ø)ØØØØ/Ø)Øð	ð 	€ðð ð ð5ð 5ð 5ð
;"ð ;"ð ;"ð|.ð .ð .ðbð ð ð ð r   