U
    ,dFd                     @   s  d dl Zd dlmZmZmZ d dlmZ d dlm	Z	m
Z
mZmZmZmZmZ 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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/d0 Z&d1d2 Z'd3d4 Z(d5d6 Z)d7d8 Z*d9d: Z+d;d< Z,d=d> Z-d?d@ Z.dAdB Z/dCdD Z0dEdF Z1dGdH Z2dIdJ Z3dKdL Z4dMdN Z5dOdP Z6dQdR Z7dSdT Z8G dUdV dVeZ9e:dWkre;  dS )X    N)unittestCUDATestCaseskip_on_cudasim)numpy_support)cudafloat32float64int32	vectorizevoidint64c                 C   s    t d}t| | ||< d S N   )r   gridmathacosABi r   E/tmp/pip-unpacked-wheel-eu7e0c37/numba/cuda/tests/cudapy/test_math.py	math_acos   s    
r   c                 C   s    t d}t| | ||< d S r   )r   r   r   asinr   r   r   r   	math_asin   s    
r   c                 C   s    t d}t| | ||< d S r   )r   r   r   atanr   r   r   r   	math_atan   s    
r   c                 C   s    t d}t| | ||< d S r   )r   r   r   acoshr   r   r   r   
math_acosh   s    
r   c                 C   s    t d}t| | ||< d S r   )r   r   r   asinhr   r   r   r   
math_asinh   s    
r    c                 C   s    t d}t| | ||< d S r   )r   r   r   atanhr   r   r   r   
math_atanh!   s    
r"   c                 C   s    t d}t| | ||< d S r   )r   r   r   cosr   r   r   r   math_cos&   s    
r$   c                 C   s    t d}t| | ||< d S r   )r   r   r   sinr   r   r   r   math_sin+   s    
r&   c                 C   s    t d}t| | ||< d S r   )r   r   r   tanr   r   r   r   math_tan0   s    
r(   c                 C   s    t d}t| | ||< d S r   )r   r   r   coshr   r   r   r   	math_cosh5   s    
r*   c                 C   s    t d}t| | ||< d S r   )r   r   r   sinhr   r   r   r   	math_sinh:   s    
r,   c                 C   s    t d}t| | ||< d S r   )r   r   r   tanhr   r   r   r   	math_tanh?   s    
r.   c                 C   s&   t d}t| | || ||< d S r   )r   r   r   atan2r   r   Cr   r   r   r   
math_atan2D   s    
r2   c                 C   s    t d}t| | ||< d S r   )r   r   r   expr   r   r   r   math_expI   s    
r4   c                 C   s    t d}t| | ||< d S r   )r   r   r   erfr   r   r   r   math_erfN   s    
r6   c                 C   s    t d}t| | ||< d S r   )r   r   r   erfcr   r   r   r   	math_erfcS   s    
r8   c                 C   s    t d}t| | ||< d S r   )r   r   r   expm1r   r   r   r   
math_expm1X   s    
r:   c                 C   s    t d}t| | ||< d S r   )r   r   r   fabsr   r   r   r   	math_fabs]   s    
r<   c                 C   s    t d}t| | ||< d S r   )r   r   r   gammar   r   r   r   
math_gammab   s    
r>   c                 C   s    t d}t| | ||< d S r   )r   r   r   lgammar   r   r   r   math_lgammag   s    
r@   c                 C   s    t d}t| | ||< d S r   )r   r   r   logr   r   r   r   math_logl   s    
rB   c                 C   s    t d}t| | ||< d S r   )r   r   r   log2r   r   r   r   	math_log2q   s    
rD   c                 C   s    t d}t| | ||< d S r   )r   r   r   log10r   r   r   r   
math_log10v   s    
rF   c                 C   s    t d}t| | ||< d S r   )r   r   r   log1pr   r   r   r   
math_log1p{   s    
rH   c                 C   s&   t d}t| | || ||< d S r   )r   r   r   	remainderr0   r   r   r   math_remainder   s    
rJ   c                 C   s    t d}t| | ||< d S r   )r   r   r   sqrtr   r   r   r   	math_sqrt   s    
rL   c                 C   s&   t d}t| | || ||< d S r   )r   r   r   hypotr0   r   r   r   
math_hypot   s    
rN   c                 C   s&   t d}t| | || ||< d S r   )r   r   r   powr0   r   r   r   math_pow   s    
rP   c                 C   s    t d}t| | ||< d S r   )r   r   r   ceilr   r   r   r   	math_ceil   s    
rR   c                 C   s    t d}t| | ||< d S r   )r   r   r   floorr   r   r   r   
math_floor   s    
rT   c                 C   s&   t d}t| | || ||< d S r   )r   r   r   copysignr0   r   r   r   math_copysign   s    
rV   c                 C   s&   t d}t| | || ||< d S r   )r   r   r   fmodr0   r   r   r   	math_fmod   s    
rX   c                 C   s(   t d}t| | \||< ||< d S r   )r   r   r   modfr0   r   r   r   	math_modf   s    
rZ   c                 C   s    t d}t| | ||< d S r   )r   r   r   isnanr   r   r   r   
math_isnan   s    
r\   c                 C   s    t d}t| | ||< d S r   )r   r   r   isinfr   r   r   r   
math_isinf   s    
r^   c                 C   s    t d}t| | ||< d S r   )r   r   r   isfiniter   r   r   r   math_isfinite   s    
r`   c                 C   s    t d}t| | ||< d S r   )r   r   r   degreesr   r   r   r   math_degrees   s    
rb   c                 C   s    t d}t| | ||< d S r   )r   r   r   radiansr   r   r   r   math_radians   s    
rd   c                 C   s"   t d}| | ||  ||< d S r   r   r   r0   r   r   r   math_pow_binop   s    
rf   c                 C   s"   t d}| | ||  ||< d S r   re   r0   r   r   r   math_mod_binop   s    
rg   c                   @   s  e Zd ZdddZdddZddd	Zdd
dZdd Zdd Zdd Z	dd Z
dddZdddZdddZdddZdd Zddd Zdd!d"Zdd#d$Zdd%d&Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ Z dAdB Z!dCdD Z"dEdF Z#dGdH Z$dIdJ Z%dKdL Z&dMdN Z'dOdP Z(dQdR Z)dSdT Z*dUdV Z+dWdX Z,dYdZ Z-e.d[d\d] Z/d^d_ Z0d`da Z1dbdc Z2ddde Z3dfdg Z4dhdi Z5djdk Z6dldm Z7dndo Z8dpdq Z9drds Z:dtdu Z;dvdw Z<dxdy Z=dzd{ Z>d|d} Z?d~S )TestCudaMathr   r   c                 C   s   |  ||tjtj|| d S Nunary_templatenpr   selffuncnpfuncstartstopr   r   r   unary_template_float32   s    z#TestCudaMath.unary_template_float32c                 C   s   |  ||tjtj|| d S ri   rk   rl   r   rm   r   r   r   unary_template_float64   s    z#TestCudaMath.unary_template_float642   c                 C   s   |  ||tjtj|| d S ri   )rk   rl   r   r   rm   r   r   r   unary_template_int64   s    z!TestCudaMath.unary_template_int64c                 C   s   |  ||tjtj|| d S ri   )rk   rl   uint64r   rm   r   r   r   unary_template_uint64   s    z"TestCudaMath.unary_template_uint64c                 C   s   d}t ||||}t ||}	t|d d d }
t|d d d }t|
|f|}|d|f ||	 |t jkrd}nd}t j	j
|||	|d d S )Nrv   r   gvIh%<=ư>rtol)rl   linspaceastype
empty_liker   
from_dtyper   jitr   testingassert_allclose)rn   ro   rp   npdtype	nprestyperq   rr   nelemr   r   arytyperestypecfuncr|   r   r   r   rk      s    
zTestCudaMath.unary_templatec           
   	   C   s   t |}|jd }t jd||jdd|jt jt jg|d}t j|t jd}t	
|d d d td d d f|}	|	d|jf || t j||| |	d|jf | | t j|||  d S )N   g        g      ?g      ?dtyper   )rl   ZfinfoZtinyarraymaxinfnanr   r	   r   r   sizer   Zassert_array_equal)
rn   ro   rp   r   npmtypefiZdenormr   r   r   r   r   r   unary_bool_special_values   s    

&z&TestCudaMath.unary_bool_special_valuesc                 C   s   |  ||tjt d S ri   )r   rl   r   rn   ro   rp   r   r   r   !unary_bool_special_values_float32   s    z.TestCudaMath.unary_bool_special_values_float32c                 C   s   |  ||tjt d S ri   )r   rl   r   r   r   r   r   !unary_bool_special_values_float64  s    z.TestCudaMath.unary_bool_special_values_float64c                 C   s   |  ||tjtj|| d S ri   rj   rm   r   r   r   unary_bool_template_float32  s    z(TestCudaMath.unary_bool_template_float32c                 C   s   |  ||tjtj|| d S ri   rt   rm   r   r   r   unary_bool_template_float64  s    z(TestCudaMath.unary_bool_template_float641   c                 C   s   |  ||tjtj|| d S ri   )rk   rl   r	   rm   r   r   r   unary_bool_template_int32
  s    z&TestCudaMath.unary_bool_template_int32c                 C   s   |  ||tjtj|| d S ri   )rk   rl   r   rm   r   r   r   unary_bool_template_int64  s    z&TestCudaMath.unary_bool_template_int64c                 C   s   d}t ||||}t j|jt jd}	|d d d }
td d d }t|
|f|}|d|f ||	 t j	|||	 d S )Nrv   r   r   )
rl   r}   r~   emptyshaper	   r   r   r   r   )rn   ro   rp   r   r   rq   rr   r   r   r   ZiarytypeZoarytyper   r   r   r   unary_bool_template  s    z TestCudaMath.unary_bool_templatec                 C   s   |  ||tjtj|| d S ri   )binary_templaterl   r   rm   r   r   r   binary_template_float32  s    z$TestCudaMath.binary_template_float32c                 C   s   |  ||tjtj|| d S ri   )r   rl   r   rm   r   r   r   binary_template_float64  s    z$TestCudaMath.binary_template_float64c                 C   s   |  ||tjtj|| d S ri   )r   rl   r   r   rm   r   r   r   binary_template_int64   s    z"TestCudaMath.binary_template_int64c                 C   s   |  ||tjtj|| d S ri   )r   rl   rx   r   rm   r   r   r   binary_template_uint64#  s    z#TestCudaMath.binary_template_uint64c                 C   s   d}t ||||}t ||}	t|d d d }
t|d d d }t|
|
|f|}|  |d|f |||	 t j	
||||	 d S )Nrv   r   )rl   r}   r~   r   r   r   r   r   bindr   r   )rn   ro   rp   r   r   rq   rr   r   r   r   r   r   r   r   r   r   r   &  s    zTestCudaMath.binary_templatec                 C   sH   |  ttj | ttj | jttjddd | jttjddd d S Nr   rq   rr   )rs   r   rl   Zarccosru   rw   ry   rn   r   r   r   test_math_acos4  s    zTestCudaMath.test_math_acosc                 C   sH   |  ttj | ttj | jttjddd | jttjddd d S r   )rs   r   rl   Zarcsinru   rw   ry   r   r   r   r   test_math_asin?  s    zTestCudaMath.test_math_asinc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r   rl   Zarctanru   rw   ry   r   r   r   r   test_math_atanJ  s    zTestCudaMath.test_math_atanc                 C   sT   | j ttjddd | jttjddd | jttjddd | jttjddd d S )Nr      r   )rs   r   rl   Zarccoshru   rw   ry   r   r   r   r   test_math_acoshS  s    zTestCudaMath.test_math_acoshc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r    rl   Zarcsinhru   rw   ry   r   r   r   r   test_math_asinh\  s    zTestCudaMath.test_math_asinhc                 C   sT   | j ttjddd | jttjddd | jttjddd | jttjddd d S )Nr   g?r   )rs   r"   rl   Zarctanhru   rw   ry   r   r   r   r   test_math_atanhe  s    zTestCudaMath.test_math_atanhc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r$   rl   r#   ru   rw   ry   r   r   r   r   test_math_cosn  s    zTestCudaMath.test_math_cosc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r&   rl   r%   ru   rw   ry   r   r   r   r   test_math_sinw  s    zTestCudaMath.test_math_sinc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r(   rl   r'   ru   rw   ry   r   r   r   r   test_math_tan  s    zTestCudaMath.test_math_tanc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r*   rl   r)   ru   rw   ry   r   r   r   r   test_math_cosh  s    zTestCudaMath.test_math_coshc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r,   rl   r+   ru   rw   ry   r   r   r   r   test_math_sinh  s    zTestCudaMath.test_math_sinhc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r.   rl   r-   ru   rw   ry   r   r   r   r   test_math_tanh  s    zTestCudaMath.test_math_tanhc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )r   r2   rl   Zarctan2r   r   r   r   r   r   r   test_math_atan2  s    zTestCudaMath.test_math_atan2c                 C   s@   t dd }| t| | t| | t| | t| d S )Nc                 S   s
   t | S ri   )r   r5   xr   r   r   ufunc  s    z)TestCudaMath.test_math_erf.<locals>.ufunc)r
   rs   r6   ru   rw   ry   rn   r   r   r   r   test_math_erf  s    
zTestCudaMath.test_math_erfc                 C   s@   t dd }| t| | t| | t| | t| d S )Nc                 S   s
   t | S ri   )r   r7   r   r   r   r   r     s    z*TestCudaMath.test_math_erfc.<locals>.ufunc)r
   rs   r8   ru   rw   ry   r   r   r   r   test_math_erfc  s    
zTestCudaMath.test_math_erfcc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r4   rl   r3   ru   rw   ry   r   r   r   r   test_math_exp  s    zTestCudaMath.test_math_expc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   r:   rl   r9   ru   rw   ry   r   r   r   r   test_math_expm1  s    zTestCudaMath.test_math_expm1c                 C   sL   | j ttjdd | jttjdd | jttjdd | jttjdd d S Nrq   )rs   r<   rl   r;   ru   rw   ry   r   r   r   r   test_math_fabs  s    zTestCudaMath.test_math_fabsc                 C   sP   t dd }| jt|dd | jt|dd | jt|dd | jt|dd d S )Nc                 S   s
   t | S ri   )r   r=   r   r   r   r   r     s    z+TestCudaMath.test_math_gamma.<locals>.ufunc皙?r   r   )r
   rs   r>   ru   rw   ry   r   r   r   r   test_math_gamma  s    
zTestCudaMath.test_math_gammac                 C   sP   t dd }| jt|dd | jt|dd | jt|dd | jt|dd d S )Nc                 S   s
   t | S ri   )r   r?   r   r   r   r   r     s    z,TestCudaMath.test_math_lgamma.<locals>.ufuncr   r   r   )r
   rs   r@   ru   rw   ry   r   r   r   r   test_math_lgamma  s    
zTestCudaMath.test_math_lgammac                 C   sL   | j ttjdd | jttjdd | jttjdd | jttjdd d S Nr   r   )rs   rB   rl   rA   ru   rw   ry   r   r   r   r   test_math_log  s    zTestCudaMath.test_math_logc                 C   sL   | j ttjdd | jttjdd | jttjdd | jttjdd d S r   )rs   rD   rl   rC   ru   rw   ry   r   r   r   r   test_math_log2  s    zTestCudaMath.test_math_log2c                 C   sL   | j ttjdd | jttjdd | jttjdd | jttjdd d S r   )rs   rF   rl   rE   ru   rw   ry   r   r   r   r   test_math_log10
  s    zTestCudaMath.test_math_log10c                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   rH   rl   rG   ru   rw   ry   r   r   r   r   test_math_log1p  s    zTestCudaMath.test_math_log1pc                 C   sL   | j ttjdd | jttjdd | jttjdd | jttjdd d S )Ngdy=r   r   )r   rJ   rl   rI   r   r   r   r   r   r   r   test_math_remainder  s    z TestCudaMath.test_math_remainderz3math.remainder(0, 0) raises a ValueError on CUDASimc                 C   sZ   t ttd d d ttdd }tdtj}|d |dd | t|d  d S )Nr   c                 S   s   t ||| d< d S )Nr   )r   rI   )rr   yr   r   r   test_0_0$  s    z6TestCudaMath.test_math_remainder_0_0.<locals>.test_0_0)r   r   r   )	r   r   r   r   r   rl   zeros
assertTruer[   )rn   r   r   r   r   r   test_math_remainder_0_0"  s
    
z$TestCudaMath.test_math_remainder_0_0c                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   rL   rl   rK   ru   rw   ry   r   r   r   r   test_math_sqrt.  s    zTestCudaMath.test_math_sqrtc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )r   rN   rl   rM   r   r   r   r   r   r   r   test_math_hypot7  s    zTestCudaMath.test_math_hypotc           
      C   s   d}t dd||}t j|t jd}t |}t|d d d }t	|td d d |ft
}|  |d|f ||| t |}tt|D ]}	t||	 ||	 ||	< qt jjt ||||dd d S )Nrv   r      r   r   rz   r{   )rl   r}   r~   Zaranger	   r   r   r   r   r   rP   r   rangelenr   rO   r   r   power)
rn   r   r   r   r   r1   r   r   ZCrefr   r   r   r   pow_template_int32@  s    

zTestCudaMath.pow_template_int32c                 C   s8   |  ttj | ttj | tj | tj d S ri   )r   rP   rl   r   r   r   r   r   r   r   r   r   test_math_powR  s    zTestCudaMath.test_math_powc                 C   s    |  ttj | ttj d S ri   )r   rf   rl   r   r   r   r   r   r   test_math_pow_binop[  s    z TestCudaMath.test_math_pow_binopc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   rR   rl   rQ   ru   rw   ry   r   r   r   r   test_math_ceilb  s    zTestCudaMath.test_math_ceilc                 C   s<   |  ttj | ttj | ttj | ttj d S ri   )rs   rT   rl   rS   ru   rw   ry   r   r   r   r   test_math_floork  s    zTestCudaMath.test_math_floorc                 C   s(   | j ttjdd | jttjdd d S r   )r   rV   rl   rU   r   r   r   r   r   test_math_copysignt  s    zTestCudaMath.test_math_copysignc              	      sd   fdd} fdd}d}  d( |tdd|tjtd d  d	 W 5 Q R X   d
. |ttjtj gtjtd d  d	 W 5 Q R X   d |tjtd d  d	 W 5 Q R X   d( |tdd|tjtd d  d	 W 5 Q R X   d. |ttjtj gtjtd d  d	 W 5 Q R X   d |tjtd d  d	 W 5 Q R X d S )Nc                    sv   t jt jg| d}t |}t |}t|||ft}|dt|f |||  t 	|  t 	| d S )Nr   r   )
rl   r   r   
zeros_liker   r   rZ   r   r   r[   )r   r   r   r   r1   r   r   r   r   modf_template_nan|  s    

z6TestCudaMath.test_math_modf.<locals>.modf_template_nanc                    s   |  |} t| }t| }t|||ft}|dt| f | || t| \}} t	||  t	|| d S r   )
r~   rl   r   r   r   rZ   r   rY   r   Zarray_equal)r   r   r   r   r1   r   DEr   r   r   modf_template_compare  s    


z:TestCudaMath.test_math_modf.<locals>.modf_template_comparerv   zfloat32 modf on simple floatr   
   )r   r   zfloat32 modf on +- infinityzfloat32 modf on nanzfloat64 modf on simple floatzfloat64 modf on +- infinityzfloat64 modf on nan)ZsubTestrl   r}   r   r   r   r   )rn   r   r   r   r   r   r   test_math_modf{  s.    	


 

zTestCudaMath.test_math_modfc                 C   s(   | j ttjdd | jttjdd d S r   )r   rX   rl   rW   r   r   r   r   r   test_math_fmod  s    zTestCudaMath.test_math_fmodc                 C   s(   | j ttjdd | jttjdd d S r   )r   rg   rl   rW   r   r   r   r   r   test_math_mod_binop  s    z TestCudaMath.test_math_mod_binopc                 C   sX   |  ttj | ttj | ttj | ttj | ttj | ttj d S ri   )	r   r\   rl   r[   r   r   r   r   r   r   r   r   r   test_math_isnan  s    zTestCudaMath.test_math_isnanc                 C   sX   |  ttj | ttj | ttj | ttj | ttj | ttj d S ri   )	r   r^   rl   r]   r   r   r   r   r   r   r   r   r   test_math_isinf  s    zTestCudaMath.test_math_isinfc                 C   sX   |  ttj | ttj | ttj | ttj | ttj | ttj d S ri   )	r   r`   rl   r_   r   r   r   r   r   r   r   r   r   test_math_isfinite  s    zTestCudaMath.test_math_isfinitec                 C   s    |  ttj | ttj d S ri   )r   rb   rl   ra   r   r   r   r   r   test_math_degrees  s    zTestCudaMath.test_math_degreesc                 C   s    |  ttj | ttj d S ri   )r   rd   rl   rc   r   r   r   r   r   test_math_radians  s    zTestCudaMath.test_math_radiansN)r   r   )r   r   )r   rv   )r   rv   )r   r   )r   r   )r   r   )r   r   )r   r   )r   r   )r   rv   )r   rv   )@__name__
__module____qualname__rs   ru   rw   ry   rk   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rh      sz   












																		
					,rh   __main__)<Znumpyrl   Znumba.cuda.testingr   r   r   Znumba.npr   Znumbar   r   r   r	   r
   r   r   r   r   r   r   r   r    r"   r$   r&   r(   r*   r,   r.   r2   r4   r6   r8   r:   r<   r>   r@   rB   rD   rF   rH   rJ   rL   rN   rP   rR   rT   rV   rX   rZ   r\   r^   r`   rb   rd   rf   rg   rh   r   mainr   r   r   r   <module>   sf   $    
