o
    ۾iA                     @   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	m
Z
 d dlmZ d dlmZ d dlmZmZ d dlmZ G d	d
 d
eZG dd deZG dd deZG dd deZG dd deZG dd dZG dd dZG dd deZG dd dejZG dd deZedkre   dS dS )    N)assert_array_equal)GUFuncBuilder)	vectorizeguvectorize)PyUFunc_One)DUFunc)tagTestCase)configc                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestUfuncBuildingc                    s   ddl m} t|}|d} |j |d} |j |  fdd}tjddd	}|| |d d d
 }|| |d}||  	j
d  dj d S )Nr   addint32(int32, int32)zint64(int64, int64)c                    .   | | }  | |  |  |j| j d S NassertPreciseEqualassertEqualdtypeabselfufunc [/home/ubuntu/.local/lib/python3.10/site-packages/numba/tests/npyufunc/test_ufuncbuilding.pycheck      
z1TestUfuncBuilding.test_basic_ufunc.<locals>.check   int32r      r"      r   zAn addition)+numba.tests.npyufunc.ufuncbuilding_usecasesr   UFuncBuilderassertFalse
objectmodebuild_ufuncnparangereshaper   __name__assertIn__doc__r   r   ufbcresr   r   r   r   r   test_basic_ufunc   s    


z"TestUfuncBuilding.test_basic_ufuncc                    s   ddl m} t|}|d} |j |  fdd}tjdddd	 }|| |d d d
 }|| |d}|| d S )Nr   r   zcomplex64(complex64, complex64)c                    r   r   r   r   r   r   r   r   2   r   z2TestUfuncBuilding.test_ufunc_struct.<locals>.checkr   	complex64r!                 ?r"   r#   )	r%   r   r&   r'   r(   r)   r*   r+   r,   r0   r   r   r   test_ufunc_struct+   s   

z#TestUfuncBuilding.test_ufunc_structc                 C   sf   ddl m} t|ddid}|d}| |j | }tjddd	}|||}| || | d S )
Nr   r   forceobjTtargetoptionsr   
   r    r!   )	r%   r   r&   
assertTruer(   r)   r*   r+   r   )r   r   r1   r2   r   r   r   r   r   r   test_ufunc_forceobj?   s   

z%TestUfuncBuilding.test_ufunc_forceobjc                 C   D   ddl m} t|ddid}|d | }| |ddd	 d
S )zA
        Check nested call to an implicitly-typed ufunc.
        r   )outernopythonTr8   (int64, int64)r$   r"   N)r%   r>   r&   r   r)   r   )r   r>   builderr   r   r   r   test_nested_callJ      
z"TestUfuncBuilding.test_nested_callc                 C   r=   )zA
        Check nested call to an explicitly-typed ufunc.
        r   )outer_explicitr?   Tr8   r@   rA   r$   r"   N)r%   rE   r&   r   r)   r   )r   rE   rB   r   r   r   r   test_nested_call_explicitU   rD   z+TestUfuncBuilding.test_nested_call_explicitN)r-   
__module____qualname__r3   r6   r<   rC   rF   r   r   r   r   r      s    r   c                   @      e Zd Zdd Zdd ZdS )TestUfuncBuildingJitDisabledc                 C      t j| _dt _d S NFr
   DISABLE_JITold_disable_jitr   r   r   r   setUpc      
z"TestUfuncBuildingJitDisabled.setUpc                 C      | j t_d S r   rO   r
   rN   rP   r   r   r   tearDowng      z%TestUfuncBuildingJitDisabled.tearDownNr-   rG   rH   rQ   rU   r   r   r   r   rJ   a       rJ   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestGUfuncBuildingc                 C   s   ddl m} t|d}|d}| |j | }tjddd	dd	}|||}| 
|| | | |jtd | |jd
 | d|j d S )Nr   guadd(x, y),(x, y)->(x, y)z(void(int32[:,:], int32[:,:], int32[:,:])r:   r    r!   r"      r[   zA generalized addition)r%   r[   r   r   r'   r(   r)   r*   r+   r,   r   r   r   r-   r.   r/   r   r[   gufbr2   r   r   r   r   r   r   test_basic_gufuncm   s   


z$TestGUfuncBuilding.test_basic_gufuncc                 C   sl   ddl m} t|d}|d}| |j | }tjddd	dd	d
 }|||}| 
|| | d S )Nr   rZ   r\   4void(complex64[:,:], complex64[:,:], complex64[:,:])r:   r4   r!   r"   r]   r5   )r%   r[   r   r   r'   r(   r)   r*   r+   r,   r   r^   r   r   r   test_gufunc_struct~   s   


z%TestGUfuncBuilding.test_gufunc_structc                 C   sv   ddl m} t|dtddd}|d}| |j | }tj	dd	d

ddd }|||}| || | d S )Nr   rZ   r\   Tr7   r8   ra   r:   r4   r!   r"   r]   r5   )r%   r[   r   dictr   r;   r(   r)   r*   r+   r,   r   r^   r   r   r   test_gufunc_struct_forceobj   s   

z.TestGUfuncBuilding.test_gufunc_struct_forceobjN)r-   rG   rH   r`   rb   re   r   r   r   r   rY   k   s    rY   c                   @   rI   )TestGUfuncBuildingJitDisabledc                 C   rK   rL   rM   rP   r   r   r   rQ      rR   z#TestGUfuncBuildingJitDisabled.setUpc                 C   rS   r   rT   rP   r   r   r   rU      rV   z&TestGUfuncBuildingJitDisabled.tearDownNrW   r   r   r   r   rf      rX   rf   c                   @   s   e Zd Zg 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S )TestVectorizeDecor)r      Nreorderablec                 C   sF   ddl m} tdg|}tjddd}|||}| || | d S )Nr   r   r   r:   r    r!   r%   r   r   r*   r+   r   r   r   r   r   r   r   r   r   test_vectorize   s
   
z!TestVectorizeDecor.test_vectorizec                 C   sJ   ddl m} tdgdd|}tjddd}|||}| || | d S )	Nr   r   r   Trc   r:   r    r!   rj   rk   r   r   r   test_vectorize_objmode   s
   
z)TestVectorizeDecor.test_vectorize_objmodec                 C   sP   ddl m} tdg|}tjddd}|||}| |tj|jtjd d S )Nr   )equalszbool_(int32, int32)r:   r    r!   )	r%   rn   r   r*   r+   r   onesshapebool_)r   rn   r   r   rr   r   r   test_vectorize_bool_return   s
   
z-TestVectorizeDecor.test_vectorize_bool_returnc                 C   s   ddl m} d}| jD ]}t|g|d|}|dkrd n|}| |j| qt|g|}| |jd  | t t|gdd| W d    n1 sMw   Y  | t t|gdd| W d    d S 1 slw   Y  d S )Nr   r   r   )identityri   noner"   )	r%   r   _supported_identitiesr   r   rt   assertIsassertRaises
ValueError)r   r   sigrt   r   expectedr   r   r   test_vectorize_identity   s   
"z*TestVectorizeDecor.test_vectorize_identityc                 C   sx   ddl m} tddd}tddd}t|}| |||||  t|}td}|||| | |||  d S )Nr   r   rh   r:   r"   )r%   r   r*   linspacer   r   empty)r   r   r   r   r   ufunc2cr   r   r   test_vectorize_no_args   s   
z)TestVectorizeDecor.test_vectorize_no_argsc                 C   sR   ddl m} tddd}tddd}ttdd|}| |||||  d S )Nr   )mulrh   r:   r"   T)rt   r?   )r%   r   r*   r}   r   r   r   )r   r   r   r   r   r   r   r   test_vectorize_only_kws   s
   z*TestVectorizeDecor.test_vectorize_only_kwsc                    sT    fdd}ddl m} tdgdd|}|| tdd|}|| || dS )	zO
        Passing the output array as a keyword argument (issue #1867).
        c                    s   t jdddd}t |}| |||d} ||  |||   t | |||d W d    d S 1 s:w   Y  d S )Nr:      r    r!   )out)zzz)r*   r+   
zeros_likerw   r   rx   	TypeError)r   r   r   gotrP   r   r   r      s   
"z=TestVectorizeDecor.test_vectorize_output_kwarg.<locals>.checkr   r   r   Tr?   N)r%   r   r   )r   r   r   r   r   rP   r   test_vectorize_output_kwarg   s   
z.TestVectorizeDecor.test_vectorize_output_kwargc                 C   sP   ddl m} tdgd|}tjddddd	}|||}| || | d S )
Nr   rZ   $(int32[:,:], int32[:,:], int32[:,:])(x,y),(x,y)->(x,y)r:   r    r!   r"   r]   )r%   r[   r   r*   r+   r,   r   )r   r[   r   r   r   r   r   r   test_guvectorize   s   
z#TestVectorizeDecor.test_guvectorizec                 C   s\   ddl m} tdgd|}tjddddd	}t|}|||| | || | d S )
Nr   rZ   r   z(x,y),(x,y),(x,y)r:   r    r!   r"   r]   )r%   r[   r   r*   r+   r,   r   r   )r   r[   r   r   r   r   r   r   test_guvectorize_no_output   s   
z-TestVectorizeDecor.test_guvectorize_no_outputc                 C   sT   ddl m} tdgddd|}tjddd	d
d}|||}| || | d S )Nr   )	guadd_objr   r   Trc   r:   r    r!   r"   r]   )r%   r   r   r*   r+   r,   r   )r   r   r   r   r   r   r   r   test_guvectorize_objectmode  s   
z.TestVectorizeDecor.test_guvectorize_objectmodec                 C   sT   ddl m} tdgddd|}tjddd	d
d}||d}| |d | dS )zA
        Test passing of scalars to object mode gufuncs.
        r   )guadd_scalar_objz(int32[:,:], int32, int32[:,:])z(x,y),()->(x,y)Trc   r:   r    r!   r"   r]   r$   N)r%   r   r   r*   r+   r,   r   )r   r   r   r   r   r   r   r   "test_guvectorize_scalar_objectmode  s   
z5TestVectorizeDecor.test_guvectorize_scalar_objectmodec                 C   st   ddl m}m} tdgddd|}tjddd	d
d}| | ||| W d    d S 1 s3w   Y  d S )Nr   )guerrorMyExceptionr   r   Trc   r:   r    r!   r"   r]   )r%   r   r   r   r*   r+   r,   rx   )r   r   r   r   r   r   r   r   $test_guvectorize_error_in_objectmode  s   "z7TestVectorizeDecor.test_guvectorize_error_in_objectmodec                 C   s   ddl m}m} dgdf}| jD ]}t|d|i|}|dkr!d n|}| |j| qt| |}| |jd  | t	 t|ddi| W d    n1 sQw   Y  | t	 t|ddi| W d    d S 1 spw   Y  d S )	Nr   )r   r[   r   r   rt   ri   ru   r"   )
r%   r   r[   rv   r   r   rt   rw   rx   ry   )r   r   r[   argsrt   r   r{   r   r   r   test_guvectorize_identity!  s   

"z,TestVectorizeDecor.test_guvectorize_identityc                 C   s   ddl m} dg}| t}t|d| W d    n1 s w   Y  | dt|j | t}t|d| W d    n1 sEw   Y  | 	t|jd | t}t|d| W d    d S 1 skw   Y  d S )	Nr   rZ   r   z)-:zbad token in signaturez(x,y),(x,y)->(x,z,v)zundefined output symbols: v,zz(x,y),(x,y),(x,y)->)
r%   r[   rx   ry   r   r.   str	exception	NameErrorr   )r   r[   sigsraisesr   r   r   test_guvectorize_invalid_layout1  s   "z2TestVectorizeDecor.test_guvectorize_invalid_layoutN)r-   rG   rH   rv   rl   rm   rs   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rg      s     		rg   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )
NEP13Arrayz4https://numpy.org/neps/nep-0013-ufunc-overrides.htmlc                 C   
   || _ d S r   arrayr   r   r   r   r   __init__E     
zNEP13Array.__init__c                 C   s   | j S r   r   rP   r   r   r   	__array__H  s   zNEP13Array.__array__c                 C   s
   | j  S r   )r   tolistrP   r   r   r   r   K  r   zNEP13Array.tolistc                 O   s(   |dkrt S t|dd |D i |S )N__call__c                 S   s   g | ]}t |qS r   )r*   asarray.0xr   r   r   
<listcomp>R  s    z.NEP13Array.__array_ufunc__.<locals>.<listcomp>)NotImplementedr   r   r   methodr   kwargsr   r   r   __array_ufunc__N  s   zNEP13Array.__array_ufunc__N)r-   rG   rH   r/   r   r   r   r   r   r   r   r   r   C  s    r   c                   @   sT   e Zd ZdZdd Zdd Zdd ZeZeZeZ	eZ
eZeZeZeZeZeZeZdS )	FakeDaskArrayzThis class defines both the NEP13 protocol and the dask collection protocol
    (https://docs.dask.org/en/stable/custom-collections.html). This is a stand-in for
    dask array, dask dataframe, and for any wrapper around them (e.g. xarray or pint).
    c                 C   r   r   r   r   r   r   r   r   [  r   zFakeDaskArray.__init__c                 O   s<   |dkrt S tt|}dd |D }t||i |S )Nr   c                 S   s    g | ]}t |tr|jn|qS r   )
isinstancer   r   r   r   r   r   r   d  s     z1FakeDaskArray.__array_ufunc__.<locals>.<listcomp>)r   pickleloadsdumpsr   r   r   r   r   r   ^  s
   zFakeDaskArray.__array_ufunc__c                 O   s   t d)Nz#called potentially expensive method)AssertionError)r   r   r   r   r   r   _dask_methodg  s   zFakeDaskArray._dask_methodN)r-   rG   rH   r/   r   r   r   r   __dask_graph____dask_keys____dask_optimize____dask_postcompute____dask_postpersist____dask_scheduler____dask_tokenize__computepersist	visualizer   r   r   r   r   U  s     	r   c                   @   s   e Zd Zdd ZdS )TestNEP13WithoutSignaturec           	      C   s
  t dddd }tjg dtjd}tjg dtjd}tjg dtjd}||||}| |tj | | g d	 |t|||}| |t | | g d	 ||t||}| |t | | g d	 |||t|}| |t | | g d	 tjg d
tj	d}tjg dtj	d}tjg dtj	d}||||}| |tj | | g d |t|||}| |t | | g d ||t||}| |t | | g d |||t|}| |t | | g d d S )NTr   c                 S   s   d|  d|  | S )Nd   r:   r   )hundredstensro   r   r   r   	new_ufunc|     z5TestNEP13WithoutSignature.test_all.<locals>.new_ufunc)rh   r"   r$   r!   )   r]      )      	   )   i  iq  )g?g@gffffff
@)g@g      @gffffff@)g@g!@g#@)gfffff6d@g̼q@gfffff^y@)
r   r*   r   int64assertIsInstancendarrayr   r   r   float64)	r   r   r   r   r   all_npnep13_1nep13_2nep13_3r   r   r   test_ally  s@   
z"TestNEP13WithoutSignature.test_allN)r-   rG   rH   r   r   r   r   r   r   w  s    r   c                   @   s   e Zd ZdZdd ZdS )TestDaskzTest that numba ufuncs are compatible with dask collections and wrappers around
    dask (e.g. xarray or pint) and that they can be serialized, sent over the network,
    deserialized on a different host and applied remotely.
    c              	   C   s   t tjdtjd}tjdtjdd }tdgdd }t dd }td	gd
dd }|||fD ](}| | ||}t|t sFJ t|j	| W d    n1 sVw   Y  q3d S )Nr   r!   r"   zf8(f8)c                 S      | d S Nr"   r   r   r   r   r   double_static_vectorize     z9TestDask.test_dask_array.<locals>.double_static_vectorizec                 S   r   r   r   r   r   r   r   double_dynamic_vectorize  r   z:TestDask.test_dask_array.<locals>.double_dynamic_vectorizezf8,f8[:]z()->()c                 S   s   | d |d d < d S r   r   )r   r   r   r   r   double_guvectorize  r   z4TestDask.test_dask_array.<locals>.double_guvectorize)
r   r*   r+   r   r   r   subTestr   r   r   )r   r   expectr   r   r   funcr   r   r   r   test_dask_array  s&   



zTestDask.test_dask_arrayN)r-   rG   rH   r/   r   r   r   r   r   r     s    r   c                   @   rI   )TestVectorizeDecorJitDisabledc                 C   rK   rL   rM   rP   r   r   r   rQ     rR   z#TestVectorizeDecorJitDisabled.setUpc                 C   rS   r   rT   rP   r   r   r   rU     rV   z&TestVectorizeDecorJitDisabled.tearDownNrW   r   r   r   r   r     rX   r   __main__)!r   unittestnumpyr*   numpy.testingr   numba.np.ufunc.ufuncbuilderr   numbar   r   numba.np.ufuncr   numba.np.ufunc.dufuncr   r&   numba.tests.supportr   r	   
numba.corer
   r   rJ   rY   rf   rg   r   r   r   r   r   r-   mainr   r   r   r   <module>   s0    R
.
 !"4!
