
    \ci              	           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       o/var/www/html/mdtn/previsions/meteo_cartes/venv/lib/python3.11/site-packages/scipy/interpolate/_rbfinterp_xp.py_monomial_powersr       sM    
f
-
-C
**S//C
y|qjj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 +	1i&"
 
CeUc**   
  aA::$a%i%62FFFD9((..Df}}/!/ /$*/ / / 
 #!$ %s
   8 A0B(c                     |  S N rr   s     r   linearr.   ^   s	    2Ir   c                 j    |                     | dk    d| dz  |                    |           z            S )Nr      )wherelogr,   s     r   thin_plate_spliner3   b   s/    88AFAq!tbffQii/000r   c                     | dz  S )N   r+   r,   s     r   cubicr6   g   s    a4Kr   c                     | dz   S )N   r+   r,   s     r   quinticr9   k   s    qD5Lr   c                 :    |                     | dz  dz              S )Nr0   r   sqrtr,   s     r   multiquadricr=   o   s    GGAqD1H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    661a4%==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aaa
ma4
m;"EEr  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/    771QQQaaaZ=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 66!!6D66!!6DD[!OED[!OE HHUc\3..EW9DIuD {B77K vr22H
))	K*	3	3	HJ!Q 0 01	:	:	
    
 		9bhhqkk":;;<<=C ))Q!Q(())
2
2CUE!!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W9DW9DIuD ))K	%%D!!!$tD!!!QQQJ'77b &   
 GGDD!!!$.RG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.    
(	1fgvueR 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   