o
    Ebb                  	   @   s   d dl Zdd Zdd Zdd Zdd	 Zd
d Zdd Zdd Zdd Z	eeeeeeee	dZ
dd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdS )#    Nc                 C   s   |  S )N rr   r   F/usr/lib/python3/dist-packages/scipy/interpolate/_rbfinterp_pythran.pylinear   s   r   c                 C   s   | dkrdS | d t |  S )Nr              )nplogr   r   r   r   thin_plate_spline   s   r   c                 C   s   | d S )N   r   r   r   r   r   cubic   s   r   c                 C   s
   | d  S )N   r   r   r   r   r   quintic   s   
r   c                 C   s   t | d d  S )Nr      r	   Zsqrtr   r   r   r   multiquadric   s   r   c                 C   s   dt | d d  S Nr   r   r   r   r   r   r   inverse_multiquadric   s   r   c                 C   s   d| d d  S r   r   r   r   r   r   inverse_quadratic      r   c                 C   s   t | d  S )Nr   )r	   Zexpr   r   r   r   gaussian#   r   r   )r   r   r   r   r   r   r   r   c                 C   s4   t |jd D ]}|tj| ||  ||< qdS )z5Evaluate RBFs, with centers at `y`, at the point `x`.r   Nrangeshaper	   ZlinalgZnorm)xykernel_funcoutir   r   r   kernel_vector3   s   r    c                 C   s.   t |jd D ]}t| ||  ||< qdS )zCEvaluate monomials, with exponents from `powers`, at the point `x`.r   Nr   r   r	   Zprod)r   powersr   r   r   r   r   polynomial_vector9   s   r#   c                 C   sb   t | jd D ]'}t |d D ]}|tj| | | |  |||f< |||f |||f< qqdS )z+Evaluate RBFs, with centers at `x`, at `x`.r   r   Nr   )r   r   r   r   jr   r   r   kernel_matrix?   s   $r%   c                 C   sJ   t | jd D ]}t |jd D ]}t| | ||  |||f< qqdS )z9Evaluate monomials, with exponents from `powers`, at `x`.r   Nr!   )r   r"   r   r   r$   r   r   r   polynomial_matrixG   s
    r&   c                 C   s6   t j| jd | jd ftd}t| }t| || |S )z3Return RBFs, with centers at `x`, evaluated at `x`.r   Zdtype)r	   emptyr   floatNAME_TO_FUNCr%   )r   kernelr   r   r   r   r   _kernel_matrixO   s   r,   c                 C   s.   t j| jd |jd ftd}t| || |S )zAReturn monomials, with exponents from `powers`, evaluated at `x`.r   r'   )r	   r(   r   r)   r&   )r   r"   r   r   r   r   _polynomial_matrixX   s   r-   c                 C   sj  |j d }|j d }|j d }t| }	tj| dd}
tj| dd}||
 d }||
 d }d||dk< | | }| | | }tj|| || ftdj}t||	|d|d|f  t	|||d||df  |d||df j||dd|f< d||d|df< t
|D ]}|||f  || 7  < qtj||| ftdj}||d|< d||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   )Zaxisr   g      ?r   r'   N)r   r*   r	   minmaxr(   r)   Tr%   r&   r   )r   dZ	smoothingr+   epsilonr"   psr   r   ZminsZmaxsshiftscaleyepsZyhatZlhsr   Zrhsr   r   r   _build_systeme   s,   


&r8   c              	   C   s   | j d }|j d }	|j d }
|j d }t| }|| }| | }| | | }tj||ftd}tj|	|
 ftd}t|D ]=}t|| |||d|	  t|| |||	d  t|D ]}t|	|
 D ]}|||f  |||f ||  7  < qdq\q=|S )a  Evaluate the RBF interpolant at `x`.

    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.
    coeffs : (P + R, S) float ndarray
        Coefficients for each RBF and monomial.

    Returns
    -------
    (Q, S) float ndarray

    r   r   r'   N)	r   r*   r	   Zzerosr)   r(   r   r    r#   )r   r   r+   r2   r"   r5   r6   Zcoeffsqr3   r   r4   r   r7   ZxepsZxhatr   Zvecr   r$   kr   r   r   	_evaluate   s&   



&r;   )Znumpyr	   r   r   r   r   r   r   r   r   r*   r    r#   r%   r&   r,   r-   r8   r;   r   r   r   r   <module>   s4    	J