U
    /eC                  /   @   s  d dl Z d dlZd dlmZ d dlmZ d dlZedZd dl	m
Z d dlmZ d dlmZ d dlmZ dZejd	d
ddgdd Zdd Zdddddddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4g$Zd5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKdLdMdNd
dOdPdQdRdSdTdUdVdWdXdYdZd[d\d]d^d_d`dadbg/Zejdceddde Zejdcedfdg Zdhdi Zejdcdjdkdldmgdndo Zejdcddgdpdq Zdrds Zdtdu Zdvdw Z ejdxdydzd{gd|d} Z!d~d Z"dd Z#dd Z$dd Z%dd Z&dd Z'dd Z(dd Z)dd Z*ejdddddgdd Z+ejdddgejdddgejdddgejdddddgdd Z,ejdddgejdddgejdddgdd Z-dd Z.dd Z/dS )    N)partial)addZnumpy)da_frompyfunc)	assert_eq)tokenizeze
This docstring was copied from numpy.{name}.

Some inconsistencies with the Dask version may exist.
namelogmodffrexpc                 C   s`   t j| d}d}tt| }|j| ks(t||jks6t|j|d|dtt| jks\td S )N)r   z  # doctest: +SKIP )	
DISCLAIMERformatgetattrda__name__AssertionError__doc__replacenp)r   Z
disclaimerZ	skip_testufunc r   ?/tmp/pip-unpacked-wheel-dbjnr7gq/dask/array/tests/test_ufunc.pytest_ufunc_meta   s    

r   c               	   C   s   dD ] } t tj| t tj| kstqtt tjj W 5 Q R X t	tjt	tjks\tdt
tjksntdt
tjkstd S )N)ninnargsZnoutntypesidentity	signaturetypesr   outer)r   r   r   r   r   pytestraisesAttributeErrornot_an_attributereprdir)attrr   r   r   
test_ufunc$   s    r'   r   Zarctan2copysigndivideequalZbitwise_andZ
bitwise_orZbitwise_xorZfloor_divideZfmaxZfminfmodZgreaterZgreater_equalhypotldexpZ
left_shiftZlessZ
less_equalZ	logaddexpZ
logaddexp2logical_and
logical_orlogical_xormaximumZminimummodmultiplyZ	nextafter	not_equalpower	remainderZright_shiftsubtractZtrue_divideZfloat_powerabsabsoluteZarccosZarccoshZarcsinZarcsinhZarctanZarctanhZbitwise_notZcbrtceilconjcoscoshZdeg2raddegreesexpexp2expm1fabsfixfloorinvertisfiniteisinfisnanlog10log1plog2Zlogical_notnegativeZpositiveZrad2degradiansZ
reciprocalZrintsignZsignbitsinsinhspacingsqrtZsquaretantanhtruncr   c              	   C   sP  | dkrt d tt| }tt| }tjjdddd}t|d}t	 > tj
dtd	 t||tjsptt||||d
d W 5 Q R X t	 ` tj
dtd	 t|tjrt||tjstnt||tjstt||||d
d W 5 Q R X t	 @ tj
dtd	 t||tjs,tt||||d
d W 5 Q R X d S )NrC   z3fix calls floor in a way that we do not yet support   d      rY   size   ignorecategoryTZ	equal_nan)r    skipr   r   r   randomrandint
from_arraywarningscatch_warningsfilterwarningsRuntimeWarning
isinstanceArrayr   r   r   ndarray)r   dafuncnpfuncarrdarrr   r   r   test_unary_ufunc   s(    



 
 
rp   c              	   C   s  t t| }t t| }tjjdddd}t|d}tjjdddd}t|d}t|||tjsftt	|||||| t|||tjstt	|||||| t|||tj
stt	|||||| t||dtjstt	||d||d t B tjdtd t|d|tjs0tt	|d||d| W 5 Q R X t||dtj
shtt	||d||d t B tjdtd t|d|tj
stt	|d||d| W 5 Q R X d S )	NrV   rW   rX   rZ   r\   
   r]   r^   )r   r   r   rb   rc   rd   ri   rj   r   r   rk   re   rf   rg   rh   )r   rl   rm   arr1darr1arr2darr2r   r   r   test_binary_ufunc   s0    


 
rv   c            	      C   s  t jjdddd} t| d}t jjdddd}t|d}ttj||tjsVt	ttj| |tjspt	ttj||tjst	ttj| |t j
st	||f| |ff||f|| ff||df|| dff||f| |ff|df| dffd|fd|ffd|fd|ffdd	dg|fdd	dg|ff| |f|  |fft d|ft d|ffg
}|D ]0\\}}\}}ttj||t j|| qdttjj||dd
t jj| |dd
 tt tjj||| d W 5 Q R X tt tj|| W 5 Q R X d S )NrV   rW   rY   rZ   r\   )rq   r\   f8g      ?   dtypeout)r   rb   rc   r   rd   ri   r   r   rj   r   rk   astypesumarrayr   r    r!   
ValueErrorrO   )	rr   rs   rt   ru   ZcasesZdAZdBABr   r   r   test_ufunc_outer   s:    " r   ZisrealZ	iscomplexrealimagc                 C   s  t t| }t t| }tjjdddd}tjjddddd }|| }t|d}t|d}t|d}t|j|j t|j|j t|	 |	  ||f||f||ffD ]h\}	}
t
||	tjstt||	||
 t||	||
 t
||
tjstt||
||
 qd S )NrV   rW   rX   rZ                 ?r\   )r   r   r   rb   rc   rd   r   r   r   r;   ri   rj   r   rk   )r   rl   rm   r   r   compZdarealZdaimagdacompro   rn   r   r   r   test_complex   s"    

r   c           	      C   s  t t| }t t| }tjjdddd}t|d}||\}}t|tjsNtt|tjs^t||\}}t	|| t	|| ||\}}t|tjstt|tjst||\}}t	|| t	|| ||\}}t|tjstt|tjst||\}}t	|| t	|| d S )NrV   rW   rX   rZ   r\   )
r   r   r   rb   rc   rd   ri   rj   r   r   )	r   rl   rm   rn   ro   Zres1Zres2Zexp1r@   r   r   r   test_ufunc_2results  s,    






r   c                  C   s   t jjdddd} tj| dd}t| d|d t| dd|dd t| jdd	|jdd	 t| jdd
|jdd
 t| jddd|jddd t| jddd|jddd d S )Nr   rq   rq   rq   rZ   r\      chunks   rV   )min)max)r   r   )r   r   )r   rb   normalr   rd   r   Zclip)xdr   r   r   	test_clip=  s    r   c                  C   s   t jjdddd} t jjddddd }| | }t|d}tt|t | ttj|ddt j|dd tt|t jst	tt|t | d S )	NrV   rW   rX   rZ   r   r\   T)deg)
r   rb   rc   r   rd   r   Zangleri   rk   r   )r   r   r   r   r   r   r   
test_angleI  s    r   c               	   C   sr   t jddd" t jjddddtd } W 5 Q R X t| d}tt 	| t	| tt 
| t
| d S )	Nr]   )invalidr)   rx   rX   rZ   r   r\   )r   Zerrstaterb   rc   r}   floatr   rd   r   ZisneginfZisposinf)rn   ro   r   r   r   test_issignedinfU  s
    &r   funcZi0ZsincZ
nan_to_numc                 C   sL   t jjdddd}t|d}tt| }tt | }t||||dd d S )NrV   rW   rX   rZ   r\   Tr`   )r   rb   rc   r   rd   r   r   )r   rn   ro   rl   rm   r   r   r   test_non_ufunc_others^  s
    

r   c               	   C   s   t tdd} ttdd}tjjdddd}t j|dd}tjjdddd}t j|dd}t| ||||| t| ||||| t	
t t d	d
 dd W 5 Q R X d S )Nrx   rV   r   rq   r   rZ   r   r   c                 S   s   | | | | fS )Nr   r   yr   r   r   <lambda>v      z!test_frompyfunc.<locals>.<lambda>)r   
frompyfuncr   r   rb   r   rd   r   r   r    r!   NotImplementedError)ZmyaddZnp_myaddr   Zdxr   Zdyr   r   r   test_frompyfunci  s    r   c               	   C   s  t tdd} ttdd}tdddg}tj| |d||d tt	| }tj||d||d | j
|j
ks~ttt | j W 5 Q R X dt| ksttj| ||||| | jdkstt| dksttt tddtt tddkstd S )Nrx   rV   r\   r   zfrompyfunc-addzda.frompyfunc<add, 2, 1>)r   r   r   r   r   ZtestingZassert_equalpickleloadsdumpsr   r   r    r!   r"   r#   r%   r   r   r$   r   )fZnp_fr   f2r   r   r   test_frompyfunc_wrappery  s    r   c                  C   sj   t dd} tj| dd}t jt jt jtt j	ddfD ]*}t
||tjsRtt||||  q:d S )N   )r      )rx   r\   r   r   )Zaxis)r   arangereshaper   rd   rO   r~   rL   r   prodri   rj   r   r   r   r   r   r   r   r   test_array_ufunc  s
     r   c                  C   s   t dd} tj| dd}t jt jfD ]b}t|||tjsDt	t
||||| |  t|||tjsrt	t
||||| |  q*d S )N   )r   r   )rx   rx   r   )r   r   r   r   rd   r   r3   ri   rj   r   r   r   r   r   r   r   test_array_ufunc_binop  s    r   c                  C   sJ   t jddd} tj| | d tj| d| d t| ttdd  d S )Nrq   r   r   r{   )r   r   r   rO   r   r   r   r   r   r   test_array_ufunc_out  s    r   c               	   C   s8   t jddd} tt tj| s*tW 5 Q R X d S )Nrq   r   r   )	r   r   r    r!   	TypeErrorr   r   reducer   r   r   r   r   test_unsupported_ufunc_methods  s    r   c               	   C   sp   t jddd} tjd| jd}tttf}tj	| d|d W 5 Q R X dt
|jksZtdt
|jksltd S )	Nrq   r   r   ry   rV   r{   rk   rj   )r   r   r   emptyrz   r    r!   r   r   r   strvaluer   )r   r   infor   r   r   test_out_numpy  s    r   c               	   C   sH   t jddd} t jddd}tt tj| |ds:tW 5 Q R X d S )Nrq   r   r      r{   )r   r   r    r!   r   r   r   r   r   r   r   r   test_out_shape_mismatch  s    r   c                  C   s(  t jjdddd} t jjdddd}t| d}t|d}t |d}t | d}t|d |d  t|d |d  t ||}t | |}t|d |d  t|d |d  t|d}t| d}t|d |d  t|d |d  t||}t| |}t|d |d  t|d |d  d S )NrV   rW   rX   rZ   r\   g       @r   )r   rb   rc   r   rd   divmodr   )rr   rt   rs   ru   resultexpectedr   r   r   test_divmod  s(    



r   dtZfloat64Zfloat32Zint32Zint64c                 C   s|   t dddg}t dddg}t|}t|}t j||| d}t j||| d}t|| tj||| d}t|| d S )NrV   rx   r\   r   r   r   ry   )r   r   r   rd   r   r   )r   rr   rt   rs   ru   r   r   r   r   r   test_dtype_kwarg  s    


r   rz   rw   
left_is_daFTright_is_da
where_kinddaskc                 C   s   t dd}t d}t j|| d}tj|| d}|dkrH| }}	n,t ddddg }}	|dkrttj|	d	d
}|rtj|d	d
n|}
|rtj|d	d
n|}t j|||	|| d}tj|
|||| d}||kstt	|| d S )N   r   r   ry   )TFFTr   rx   r   )wherer|   rz   )
r   r   r   Z
zeros_liker   r   rd   r   r   r   )rz   r   r   r   leftrightr|   d_outd_wherer   d_leftd_rightr   r   r   r   r   test_ufunc_where  s    

r   where_is_dac                 C   s   t d}t dd}t ddddgddddgddddggd}t j|j|jd}tj|j|jd}|rztj|ddn|}| rtj|ddn|}	|rtj|ddn|}
t j	||||d	}tj	|	|
||d	}||kst
t|| d S )
Nr      r   rV   boolry   rx   r   r   r|   )r   r   r   r}   zerosshaperz   r   rd   r   r   r   )r   r   r   r   r   r   r|   r   r   r   r   r   r   r   r   r   test_ufunc_where_broadcasts  s    
.r   c                  C   s   t d} t dd}t ddddgddddgddddggd}tj|dd}tj| dd}tj|dd}t j| ||d}tj|||d}t ||d}t ||d}	t||	 t | |}
t 	|
 |
 rtd S )	Nr   r   r   rV   r   rx   r   )r   )r   r   r   r}   r   rd   r   r   r   r*   Zcomputeallr   )r   r   r   r   r   r   r   r   Zexpected_maskedZresult_maskedZexpected_no_wherer   r   r   test_ufunc_where_no_out'  s    
.
r   c                  C   s   t jtjddddd} t jtjdddddd}t jtjddddgd	ddd}tjddd}t j|dd}t j| |||d
}||kstt|tjddddgdd t	|d
 stdS )zdDask array's are immutable, ensure that the backing numpy array for
    `out` isn't actually mutatedr   i8ry   rx   r   r   rV   r   r   r   rq   N)r   rd   r   r   r   r   r   r   r   r*   r   )r   r   r   Zout_npr|   r   r   r   r   "test_ufunc_where_doesnt_mutate_out@  s     r   )0r   re   	functoolsr   operatorr   r    Zimportorskipr   Z
dask.arrayr   r   Zdask.array.ufuncr   Zdask.array.utilsr   Z	dask.baser   r   markZparametrizer   r'   Zbinary_ufuncsZunary_ufuncsrp   rv   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   <module>   s  

(3

(*

!	

!	

