o
    EbmB                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ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	 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)assert_allcloseassert_array_equal)Halton)cKDTree)
_AVAILABLE_SCALE_INVARIANT_NAME_TO_MIN_DEGREE_monomial_powersRBFInterpolator)_rbfinterp_pythranc                 C   s   t | jd |}t| |S )N   )r
   shaper   Z_polynomial_matrix)xdegreeZpowers r   H/usr/lib/python3/dist-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=
@      npZ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   Zx1Zx2Zterm1Zterm2Zterm3Z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ZscrambleZseedr   r   Zcomplete)modeT)r   r   randomRandomStateranger   Z_kernel_matrixr   linalgZqrr   TdotZcholeskyr   )kernelmZnxZntestsndimseq_r   APQRZQ2Br   r   r   #_is_conditionally_positive_definite*   s$   
r6   r,   c                 C   s"   t | dd }t| |sJ d S )Nr   )r	   getr6   )r,   r-   r   r   r   $test_conditionally_positive_definiteI   s   r9   c                   @   s(  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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d&d' Zd(d) Zd*d+ Zd,d- Zd.S )/_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>Zatol)r   r   r&   r'   r   buildr   selfr,   r/   r   r   xitpyitp1yitp2r   r   r   test_scale_invariance_1dS   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"   r<   r=   r?   r@   rA   )r   r   r&   r'   r!   rB   r   rC   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,         @r=   r@   rA   )r   r   r&   r'   r!   r   rB   r   )
rD   r,   r/   Zscaleshiftr   r   rE   rF   rG   r   r   r   test_extreme_domainsk   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   r;           r<   r   )r   r@   rA   )
r   r&   r'   r   r   normalr   r+   rB   r   )rD   rngr/   r   r   rE   r2   Pitppoly_coeffsr   rF   rG   r   r   r   test_polynomial_reproduction   s   





z1_TestRBFInterpolator.test_polynomial_reproductionc              	   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"   r7   r   r   )	r   r   r&   r'   arrayr!   r*   rB   r   rD   r/   r   rE   r   rF   rG   Z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              ?r7   )	r   r   r&   r'   r!   rB   realimagr   rU   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   r;   rK   r=   r   -C6?)r   r   r&   r'   r   rB   mean	rD   r,   r/   r   rE   r   ytrueyitpZ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"   rK   r=   g-C6*?)r   r   r&   r'   r!   rB   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"   rN   )r"   r<   )r>   	smoothingr,   r   T)
r   r&   r'   r   linspacer   rO   rB   Zsqrtr[   )rD   r,   rP   r/   ZnoiseZrmse_tolZsmoothing_ranger   r   r]   Zrmse_within_tolrc   ZysmoothZ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   r;   rN   r<   r   )r;   g     @@rc   rZ   rA   )r   r&   r'   r   r   rO   r   r+   copyzerosrB   r   )rD   rP   r/   r   r   r2   rR   r   Zy_with_outlierrc   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   r&   r'   r!   pytestraises
ValueErrorrB   )rD   r   dr   rk   r   r   r   $test_inconsistent_x_dimensions_error  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`rj   r   rd   rh   rl   rm   rn   rB   rD   r   ro   rk   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.rj   rq   rr   r   r   r   test_y_not_2d_error   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 berj   rf   )r   rd   rh   onesrl   rm   rn   rB   )rD   r   ro   rc   rk   r   r   r   (test_inconsistent_smoothing_length_error'  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 ofrj   testrJ   rq   rr   r   r   r   test_invalid_kernel_name_error/     
"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 specifiedrj   rJ   )	r   rd   rh   r   r   rl   rm   rn   rB   )rD   r   ro   r,   rk   r   r   r    test_epsilon_not_specified_error6  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.rj   rq   )rD   r   r   ro   rk   r   r   r   test_x_not_2d_errorA  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 requiredrj   thin_plate_splinerJ   rq   rr   r   r   r   "test_not_enough_observations_errorI  ry   z7_TestRBFInterpolator.test_not_enough_observations_errorc              	   C   s   t dddd d d f }t d}t D ],\}}d| }tjt|d | j||d||d d W d    n1 s=w   Y  qd S )Nr   r   r#   z`degree` should not be below rj   r<   )r>   r,   r   )	r   rd   rh   r	   itemsrl   ZwarnsWarningrB   )rD   r   ro   r,   Zdegrk   r   r   r   test_degree_warningP  s   

z(_TestRBFInterpolator.test_degree_warningc                 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 )	Nr?   rN   r<   )rN   rN   rN   zdoes not have full column rankrj   r|   rJ   )r   rT   rl   rm   r   rB   rr   r   r   r   test_rank_errorX  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   ZlinearrJ   )r   rh   ru   rB   r   )rD   Zdimr   ro   fr   r   r   test_single_pointa  s   
z&_TestRBFInterpolator.test_single_pointc                 C   sp   t ddtjdd}d|d }d|d }t|}| ||}||}tt||}t	|| d S )Nr   Fl   e r$   r   r;   )
r   r   r&   r'   r   rB   pickleloadsdumpsr   )rD   r/   r   rE   r   ZinterprF   rG   r   r   r   test_pickleablej  s   z$_TestRBFInterpolator.test_pickleableN)__name__
__module____qualname__rl   markparametrizesortedr   rH   rI   r   rM   rS   rV   rY   r_   r`   re   ri   rp   rs   rt   rv   rx   rz   r{   r}   r   r   r   r   r   r   r   r   r:   R   s:    






		r:   c                   @   s$   e Zd Zdd Zdd Zdd ZdS ) TestRBFInterpolatorNeighborsNonec                 O   s   t |i |S )Nr   rD   argskwargsr   r   r   rB   }  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       חAr;   r   rc   Zrcondr   r@   rA   )r   r   r&   r'   r   rB   r   r+   r)   lstsqr   rD   r/   r   rc   r   rE   r   rF   r2   rQ   rG   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   r@   rA   )r   r   r&   r'   r!   rB   r   r+   r)   r   r   r   r   r   r   test_smoothing_limit_2d  s"   



z8TestRBFInterpolatorNeighborsNone.test_smoothing_limit_2dN)r   r   r   rB   r   r   r   r   r   r   r   |  s    r   c                   @      e Zd Zdd Zdd ZdS )TestRBFInterpolatorNeighbors20c                 O   s   t |i |ddiS )N	neighborsrb   r   r   r   r   r   rB     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"   rb   r   r@   rA   )r   r   r&   r'   r!   rB   r   Zqueryappendr   r   )rD   r/   r   rE   r   rF   rG   ZtreeZxir0   Znbrr   r   r   #test_equivalent_to_rbf_interpolator  s   

&zBTestRBFInterpolatorNeighbors20.test_equivalent_to_rbf_interpolatorNr   r   r   rB   r   r   r   r   r   r     s    r   c                   @   r   )TestRBFInterpolatorNeighborsInfc                 O   s   t |i |dtjiS )Nr   )r   r   infr   r   r   r   rB     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   r;   r@   rA   )r   r   r&   r'   r   rB   r   r   )rD   r/   r   rE   r   rF   rG   r   r   r   r     s   zCTestRBFInterpolatorNeighborsInf.test_equivalent_to_rbf_interpolatorNr   r   r   r   r   r     s    r   )!r   rl   Znumpyr   Znumpy.linalgr   Znumpy.testingr   r   Zscipy.stats.qmcr   Zscipy.spatialr   Zscipy.interpolate._rbfinterpr   r   r	   r
   r   Zscipy.interpolater   r   r   r!   r6   r   r   r   r9   r:   r   r   r   r   r   r   r   <module>   s*    
  ,7