o
    ?HhJ                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZmZmZmZmZ d dlmZ d dlmZ d	d
 Zdd Zdd Zdd Zejdeedd ZG dd dZG dd deZG dd deZ G dd deZ!dS )    N)LinAlgError)xp_assert_close)Halton)cKDTree)
_AVAILABLE_SCALE_INVARIANT_NAME_TO_MIN_DEGREE_monomial_powersRBFInterpolator)_rbfinterp_pythran)_run_concurrent_barrierc                 C   s   t | jd |}t| |S )N   )r	   shaper   _polynomial_matrix)xdegreepowers r   f/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/scipy/interpolate/tests/test_rbfinterp.py_vandermonde   s   r   c                 C   sH   | d d df } dt |  dt d|    dt d|     }|S )Nr   g
ףp=
@      npexp)r   yr   r   r   _1d_test_function   s   4r   c                 C   s   | d d df | d d df }}dt d| d d  d d| d d d   }dt d| d d  d d| d d   }d	t d| d
 d  d d| d d d   }dt d| d d  d| d
 d   }|| | | }|S )Nr   r   g      ?	      r   1   
   g      ?   r   gɿr   )r   x1x2term1term2term3term4r   r   r   r   _2d_test_function   s   "404,r*   c              
   C   s   d}d}dD ]_}t |dtj d}t|D ]N}d|| d }t|| }t||d }	tjj	|	dd	\}
}|
d d |	j
d d f }|j||}ztj| W q tjjyd   Y   dS w qd
S )Nr"   d   )r   r    r   r      Fscrambleseedr    r   complete)modeT)r   r   randomRandomStateranger   _kernel_matrixr   linalgqrr   Tdotcholeskyr   )kernelmnxntestsndimseq_r   APQRQ2Br   r   r   #_is_conditionally_positive_definite+   s$   
rH   r;   c                 C   s"   t | dd }t| |sJ d S )Nr   )r   getrH   )r;   r<   r   r   r   $test_conditionally_positive_definiteJ   s   rK   c                   @   sH  e Zd Zejdeedd Zejdeedd Z	ejdee
dd Zdd	 Zejjd
d Zdd Zdd Zejdee
dd Zejdee
dd Zejdee
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$d% Zd&d' Zejjd(d) Zd*d+ Zd,d- Z d.d/ Z!d0d1 Z"d2S )3_TestRBFInterpolatorr;   c                 C   sv   t ddtj d}d|d }t|}d|d }| j||d|d|}| j||d|d|}t||d	d
 d S )Nr   Fr-   r   2         ?epsilonr;          @:0yE>atol)r   r   r2   r3   r   buildr   selfr;   r@   r   r   xitpyitp1yitp2r   r   r   test_scale_invariance_1dT   s   z-_TestRBFInterpolator.test_scale_invariance_1dc                 C   sn   t ddtj d}|d}t|}|d}| j||d|d|}| j||d|d|}t||dd	 d S )
Nr    Fr-   r+   rN   rO   rQ   rR   rS   )r   r   r2   r3   r*   rU   r   rV   r   r   r   test_scale_invariance_2d`   s   

z-_TestRBFInterpolator.test_scale_invariance_2dc           
      C   s   t ddtj d}d}d}|d}t|}|d}|tv r=| j|||d|}| j|| | ||d|| | }	n | j||d|d	|}| j|| | |d| |d	|| | }	t||	d
d d S )Nr    Fr-   gd~QJgAjZKr+   r;         @rO   rR   rS   )r   r   r2   r3   r*   r   rU   r   )
rW   r;   r@   scaleshiftr   r   rX   rY   rZ   r   r   r   test_extreme_domainsl   s.   



z)_TestRBFInterpolator.test_extreme_domainsc                 C   s   t jd}tdd|d}d}|d}|d}t||}t||}|dd|jd	 }||}	||}
| j||	|d
|}t	|
|dd d S )Nr   r    Fr-   r   rM           rN   r   r   rR   rS   )
r   r2   r3   r   r   normalr   r9   rU   r   )rW   rngr@   r   r   rX   rC   Pitppoly_coeffsr   rY   rZ   r   r   r   test_polynomial_reproduction   s   





z1_TestRBFInterpolator.test_polynomial_reproductionc                    s   t jd}tdd|d}d}d}|d}||}t||}t||}	|dd	|jd
 }
||
}|	|
}| j|||d}|j	  fdd}|
|d| ||}t||dd d S )Nr   r    Fr-   r   i	  rM   rb   rN   r   rc   c                     s   |j dd  | i |S )Nr+   )memory_budget)update)argskwargsce_realr   r   _chunk_evaluator   s   z<_TestRBFInterpolator.test_chunking.<locals>._chunk_evaluatorro   rR   rS   )r   r2   r3   r   r   rd   r   r9   rU   ro   setattrr   )rW   monkeypatchre   r@   r   largeNr   rX   rC   rf   rg   r   rY   interpro   rZ   r   rm   r   test_chunking   s"   





z"_TestRBFInterpolator.test_chunkingc              	   C   s   t ddtj d}|d}|d}tt|t|d d d d df gj}| |||}| ||d d df |}| ||d d df |}t|d d df | t|d d df | d S )Nr    Fr-   r+   rI   r   r   )	r   r   r2   r3   arrayr*   r8   rU   r   rW   r@   r   rX   r   rY   rZ   yitp3r   r   r   test_vector_data   s   


z%_TestRBFInterpolator.test_vector_datac                 C   s   t ddtj d}|d}|d}t|dt|d d d d df   }| |||}| ||j|}| ||j|}t|j| t|j| d S )Nr    Fr-   r+   y              ?rI   )	r   r   r2   r3   r*   rU   realimagr   rv   r   r   r   test_complex_data   s   

&z&_TestRBFInterpolator.test_complex_datac           	      C   sx   t ddtj d}d|d }d|d }t|}t|}| j||d|d|}t|| d }|d	k s:J d S )
Nr   Fr-   r   rM   r^   rO   r    -C6?)r   r   r2   r3   r   rU   mean	rW   r;   r@   r   rX   r   ytrueyitpmser   r   r   test_interpolation_misfit_1d   s   z1_TestRBFInterpolator.test_interpolation_misfit_1dc           	      C   sp   t ddtj d}|d}|d}t|}t|}| j||d|d|}t|| d }|dk s6J d S )Nr    Fr-   r+   r^   rO   g-C6*?)r   r   r2   r3   r*   rU   r}   r~   r   r   r   test_interpolation_misfit_2d   s   

z1_TestRBFInterpolator.test_interpolation_misfit_2dc                 C   s   t jd}tdd|d}d}d}dt ddd	 }d
|d }t||d|d }t|}	d}
|D ]"}| j||d||d|}t t 	||	 d }||k rVd}
 nq4|
s[J d S )Nr   r   Fr-   g?g?r"      r   r+   rb   )r+   rN   )rP   	smoothingr;   r    T)
r   r2   r3   r   linspacer   rd   rU   sqrtr}   )rW   r;   re   r@   noisermse_tolsmoothing_ranger   r   r   rmse_within_tolr   ysmoothrmser   r   r   test_smoothing_misfit  s0   z*_TestRBFInterpolator.test_smoothing_misfitc                 C   s   t jd}tdd|d}d}|d}t||}|dd|jd }||}t |}|d	  d7  < t 	d
}	d|	d	< | j
|||	d|}
t|
|dd d S )Nr   r   Fr-   r    rM   rb   rN   r"   )rM   g     @@r   r|   rS   )r   r2   r3   r   r   rd   r   r9   copyzerosrU   r   )rW   re   r@   r   r   rC   rg   r   y_with_outlierr   r   r   r   r   test_array_smoothing  s   




z)_TestRBFInterpolator.test_array_smoothingc                 C   s   t ddtj dd}t|}t ddtj dd}d}tjt|d | ||| W d    d S 1 s;w   Y  d S )Nr    Fr-   r"   r   zExpected the second axis of `x`match)	r   r   r2   r3   r*   pytestraises
ValueErrorrU   )rW   r   dr   r   r   r   r   $test_inconsistent_x_dimensions_error0  s   "z9_TestRBFInterpolator.test_inconsistent_x_dimensions_errorc                 C   sh   t dddd d d f }t d}d}tjt|d | || W d    d S 1 s-w   Y  d S )Nr   r   r,   zExpected the first axis of `d`r   r   r   r   r   r   r   rU   rW   r   r   r   r   r   r    test_inconsistent_d_length_error:  s   
"z5_TestRBFInterpolator.test_inconsistent_d_length_errorc                 C   s\   t ddd}t d}d}tjt|d | || W d    d S 1 s'w   Y  d S )Nr   r   r,   z"`y` must be a 2-dimensional array.r   r   r   r   r   r   test_y_not_2d_errorA  s   
"z(_TestRBFInterpolator.test_y_not_2d_errorc                 C   sv   t dddd d d f }t d}t d}d}tjt|d | j|||d W d    d S 1 s4w   Y  d S )Nr   r   r,   zExpected `smoothing` to ber   r   )r   r   r   onesr   r   r   rU   )rW   r   r   r   r   r   r   r   (test_inconsistent_smoothing_length_errorH  s   

"z=_TestRBFInterpolator.test_inconsistent_smoothing_length_errorc                 C   sl   t dddd d d f }t d}d}tjt|d | j||dd W d    d S 1 s/w   Y  d S )Nr   r   r,   z`kernel` must be one ofr   testr]   r   r   r   r   r   test_invalid_kernel_name_errorP     
"z3_TestRBFInterpolator.test_invalid_kernel_name_errorc              	   C   s~   t dddd d d f }t d}tD ](}|tv rqd}tjt|d | j|||d W d    n1 s7w   Y  qd S )Nr   r   r,   z`epsilon` must be specifiedr   r]   )	r   r   r   r   r   r   r   r   rU   )rW   r   r   r;   r   r   r   r    test_epsilon_not_specified_errorW  s   
z5_TestRBFInterpolator.test_epsilon_not_specified_errorc                 C   sz   t dddd d d f }t ddd}t d}d}tjt|d | ||| W d    d S 1 s6w   Y  d S )Nr   r   r,   z"`x` must be a 2-dimensional array.r   r   )rW   r   r   r   r   r   r   r   test_x_not_2d_errorb  s   
"z(_TestRBFInterpolator.test_x_not_2d_errorc                 C   sl   t dddd d d f }t d}d}tjt|d | j||dd W d    d S 1 s/w   Y  d S )Nr   r   z#At least 2 data points are requiredr   thin_plate_spliner]   r   r   r   r   r   "test_not_enough_observations_errorj  r   z7_TestRBFInterpolator.test_not_enough_observations_errorc              	   C   s   t dddd d d f }t d}t D ]0\}}|dkrFd| }tjt|d | j||d||d d W d    n1 sAw   Y  qd S )Nr   r   r,   z`degree` should not be below r   rN   rP   r;   r   )	r   r   r   r   itemsr   warnsWarningrU   )rW   r   r   r;   degr   r   r   r   test_degree_warningq  s   

z(_TestRBFInterpolator.test_degree_warningc                 C   sN   t dddd d d f }t d}t D ]\}}| j||d|dd qd S )Nr   r   r,   rN   rI   r   )r   r   r   r   r   rU   )rW   r   r   r;   rA   r   r   r   test_minus_one_degree|  s
   
z*_TestRBFInterpolator.test_minus_one_degreec                 C   sv   t ddgddgddgg}t g d}d}tjt|d | j||dd| W d    d S 1 s4w   Y  d S )	NrQ   rb   rN   )rb   rb   rb   zdoes not have full column rankr   r   r]   )r   ru   r   r   r   rU   r   r   r   r   test_rank_error  s   "z$_TestRBFInterpolator.test_rank_errorc                 C   sD   dD ]}t d|f}t d}| j||dd|}t|| qd S )N)r   r    r   r   )r   linearr]   )r   r   r   rU   r   )rW   dimr   r   fr   r   r   test_single_point  s   
z&_TestRBFInterpolator.test_single_pointc                 C   st   t ddtjdd}d|d }d|d }t|}| ||}||}tt||}t	||dd d S )	Nr   Fl   e r-   r   rM   gؗҜ<rS   )
r   r   r2   r3   r   rU   pickleloadsdumpsr   )rW   r@   r   rX   r   rs   rY   rZ   r   r   r   test_pickleable  s   z$_TestRBFInterpolator.test_pickleableN)#__name__
__module____qualname__r   markparametrizesortedr   r[   r\   r   ra   rh   slowrt   rx   r{   r   r   r   r   r   r   r   r   r   r   r   r   thread_unsafer   r   r   r   r   r   r   r   r   rL   S   sB    









		rL   c                   @   $   e Zd Zdd Zdd Zdd ZdS ) TestRBFInterpolatorNeighborsNonec                 O   s   t |i |S Nr
   rW   rk   rl   r   r   r   rU     s   z&TestRBFInterpolatorNeighborsNone.buildc                 C   s   t ddtj d}d}d}d|d }d|d }t|}| j||||d|}t||}t||}	|	tjj	||d dd	 }
t
||
d
d d S )Nr   Fr-   r       חArM   r   r   rcondr   rR   rS   )r   r   r2   r3   r   rU   r   r9   r6   lstsqr   rW   r@   r   r   r   rX   r   rY   rC   rf   rZ   r   r   r   test_smoothing_limit_1d  s"   

z8TestRBFInterpolatorNeighborsNone.test_smoothing_limit_1dc                 C   s   t ddtj d}d}d}|d}|d}t|}| j||||d|}t||}t||}	|	tjj	||d dd	 }
t
||
d
d d S )Nr    Fr-   r   r   r+   r   r   r   rR   rS   )r   r   r2   r3   r*   rU   r   r9   r6   r   r   r   r   r   r   test_smoothing_limit_2d  s"   



z8TestRBFInterpolatorNeighborsNone.test_smoothing_limit_2dN)r   r   r   rU   r   r   r   r   r   r   r     s    r   c                   @   r   )TestRBFInterpolatorNeighbors20c                 O   s   t |i |ddiS )N	neighborsr   r   r   r   r   r   rU     s   z$TestRBFInterpolatorNeighbors20.buildc                 C   s   t ddtj d}|d}|d}t|}| |||}g }t|}|D ]}||d\}	}
|t	||
 ||
 |d  d  q(t
||dd d S )	Nr    Fr-   r+   r   r   rR   rS   )r   r   r2   r3   r*   rU   r   queryappendr
   r   )rW   r@   r   rX   r   rY   rZ   treexirA   nbrr   r   r   #test_equivalent_to_rbf_interpolator  s   

&zBTestRBFInterpolatorNeighbors20.test_equivalent_to_rbf_interpolatorc                 C   sX   t ddtjdd}|d}|d}t|}| ||}dd }td||| d S )	Nr    Fr   r-   r+   c                 S   s   || d S r   r   )rA   rs   xpr   r   r   	worker_fn   s   zBTestRBFInterpolatorNeighbors20.test_concurrency.<locals>.worker_fnr"   )r   r   r2   r3   r*   rU   r   )rW   r@   r   rX   r   rs   r   r   r   r   test_concurrency  s   

z/TestRBFInterpolatorNeighbors20.test_concurrencyN)r   r   r   rU   r   r   r   r   r   r   r     s    r   c                   @   s   e Zd Zdd Zdd ZdS )TestRBFInterpolatorNeighborsInfc                 O   s   t |i |dtjiS )Nr   )r
   r   infr   r   r   r   rU   	  s   z%TestRBFInterpolatorNeighborsInf.buildc                 C   sh   t ddtj d}d|d }d|d }t|}| |||}t|||}t||dd d S )Nr   Fr-   r   rM   rR   rS   )r   r   r2   r3   r   rU   r
   r   )rW   r@   r   rX   r   rY   rZ   r   r   r   r     s   zCTestRBFInterpolatorNeighborsInf.test_equivalent_to_rbf_interpolatorN)r   r   r   rU   r   r   r   r   r   r     s    r   )"r   r   numpyr   numpy.linalgr   scipy._lib._array_apir   scipy.stats.qmcr   scipy.spatialr   scipy.interpolate._rbfinterpr   r   r   r	   r
   scipy.interpolater   scipy._lib._testutilsr   r   r   r*   rH   r   r   r   rK   rL   r   r   r   r   r   r   r   <module>   s,    
  V7(