o
    8ήc?                     @   s   d dl Z d dlZd dlZd dlm  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mZ dd Zdd 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eZedkrse   dS dS )    N)voidfloat32int64jitguvectorize)GUVectorize)tagTestCasec           	   
   C   sv   | j \}}|j \}}t|D ]*}t|D ]#}d|||f< t|D ]}|||f  | ||f |||f  7  < q qqdS )zdocstring for matmulcorer   N)shaperange)	ABCmnpijk r   G/tmp/pip-target-vg8gfxp4/lib/python/numba/tests/npyufunc/test_gufunc.py
matmulcore   s   

*r   c                 C   s   | | | |d< d S Nr   r   )axyoutr   r   r   axpy   s   r   c                   @   @   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )
TestGUFunccpuc                 C   sv   d}t j|d d t jd|dd}t j|d d t jd|dd}|||}t||}t jj||ddd d S )	Ni        dtype   h㈵>:0yE>rtolatol)nparanger   reshapeutmatrix_multiplytestingassert_allclose)selfgufunc	matrix_ctr   r   r   Goldr   r   r   check_matmul_gufunc   s   ""
zTestGUFunc.check_matmul_gufuncc                 C   sf   t td| jd}|td d d d f td d d d f td d d d f f | }| | d S N(m,n),(n,p)->(m,p)target)r   r   r:   addr   build_ufuncr6   r2   r3   r   r   r   test_gufunc(   s   @zTestGUFunc.test_gufuncc                 C   s\   t ttd d d d f td d d d f td d d d f gd| jdt}| | d S r7   )r   r   r   r:   r   r6   r=   r   r   r   test_guvectorize_decor0   s   >z!TestGUFunc.test_guvectorize_decorc                 C   sX   t td| jd}|d | }tjdtjd}||||}tj	||| |  d S )N(), (), () -> ()r9   z(intp, intp, intp, intp[:])
   r#   )
r   r   r:   r;   r<   r+   r,   intpr0   assert_equal)r2   r3   r   r   r   r   r   test_ufunc_like7   s   
zTestGUFunc.test_ufunc_likec                 C   sp   t dgddd }td}||dd}tj|dd}tj|| t|}|||dd tj|| d S )	Nzf8[:],f8[:](n)->(n)c                 S   0   d}t | jd D ]}|| | 7 }|||< q	d S r   r   r
   r   resaccr   r   r   r   	my_cumsumF   
   
z'TestGUFunc.test_axis.<locals>.my_cumsum      r   axisr   rQ   )r   r+   onescumsumr0   rC   
zeros_like)r2   rK   r   r   expectedout_kwr   r   r   	test_axisD   s   



zTestGUFunc.test_axisc                 C   sf   t td d  ttd d  fgddd }| d|j | d|j | d|j | d|j d S )N(n),()->(n)c                 S   s(   t | jd D ]
}| | | ||< qdS )docstring for gufuncr   NrG   )r   r   rI   r   r   r   r   r3   X   s   z)TestGUFunc.test_docstring.<locals>.gufuncz numba.tests.npyufunc.test_gufuncr3   rZ   )r   r   assertEqual
__module____name____qualname____doc__r=   r   r   r   test_docstringW   s    
zTestGUFunc.test_docstringN)
r]   r\   r^   r:   r6   r>   r?   rD   rX   r`   r   r   r   r   r      s    
r   c                   @      e Zd ZdZdZdS )TestGUFuncParallelFparallelNr]   r\   r^   _numba_parallel_test_r:   r   r   r   r   rb   d       rb   c                   @   r   )TestDynamicGUFuncr    c           
      C   s  dd }t td| jdd}d}tj|d d tjd	|dd}tj|d d
 tjd	|dd
}tj|d d
 tjd	|dd
}||||| tj|d d tjd	|dd}tj|d d
 tjd	|dd
}tj|d d
 tjd	|dd
}	|||||	 | t	|j
d d S )Nc                 S   s0   t ||}| ||| tjj||ddd d S )Nr&   r'   r(   )r.   r/   r+   r0   r1   )r3   r   r   r   r5   r   r   r   r6   n   s   zBTestDynamicGUFunc.test_dynamic_matmul.<locals>.check_matmul_gufuncr8   Tr:   
is_dynamicrA   r!   r"   r#   r%   )r   r   r:   r+   r,   r   r-   r   r[   lentypes)
r2   r6   r3   r4   Ai64Bi64Ci64r   r   r   r   r   r   test_dynamic_matmull   s   """"""z%TestDynamicGUFunc.test_dynamic_matmulc                 C   s8   dd }t td| jdd}tjdtjd}||| d S )Nc                 S   sj   t jd|jd}t jd|jd}| |||| | ||||d || | }t j|| t j|| d S )NrA   r#   r   )r+   zerosr$   r0   rC   )r3   r   r   rW   goldenr   r   r   check_ufunc_output   s   zETestDynamicGUFunc.test_dynamic_ufunc_like.<locals>.check_ufunc_outputr@   Trh   rA   r#   )r   r   r:   r+   r,   rB   )r2   rs   r3   r   r   r   r   test_dynamic_ufunc_like   s   
z)TestDynamicGUFunc.test_dynamic_ufunc_likec                 C   s   t d| jdddd }| |j tjdtjddd	}tjdtjd}||| t	|j
d
 D ]}| || ||   q2d}| t| || W d   dS 1 sYw   Y  dS )w
        Note that scalar output is a 0-dimension array that acts as
        a pointer to the output location.
        (n)->()Tr:   nopythonc                 S   0   d}t | jd D ]}|| | 7 }q	||d< d S Ng        r   r   rG   inpr   tmpr   r   r   r   sum_row   s   z=TestDynamicGUFunc.test_dynamic_scalar_output.<locals>.sum_row0u  r#   '     r   z)Too few arguments for function 'sum_row'.N)r   r:   
assertTrueri   r+   r,   int32r-   rq   r   r
   r[   sumassertRaisesRegex	TypeError)r2   r~   r|   r   r   msgr   r   r   test_dynamic_scalar_output   s   



"z,TestDynamicGUFunc.test_dynamic_scalar_outputc                 C   sx   t ddd }td}tj|dd}t|}|||dd tj|| t|}|||dd tj|| d S )NrE   c                 S   rF   r   rG   rH   r   r   r   rK      rL   z.TestDynamicGUFunc.test_axis.<locals>.my_cumsumrM   r   rP   rR   )r   r+   rS   rT   rU   r0   rC   )r2   rK   r   rV   r   rW   r   r   r   rX      s   



zTestDynamicGUFunc.test_axisc                 C   s  t ddd }g d}|D ]}t||}| |d|f  qtg d}tg d}||| | |tg d | |jd | t	}|
| W d    n1 sYw   Y  | t|jd	 | t	}|| W d    n1 s|w   Y  | t|jd	 | t	}||d
dg W d    n1 sw   Y  | t|jd	 | t}||| W d    n1 sw   Y  | t|jd d S )NrE   c                 S   rF   r   rG   rH   r   r   r   r3      rL   z8TestDynamicGUFunc.test_gufunc_attributes.<locals>.gufunc)	signature
accumulateatouterreducereduceatz!dynamic gufunc not exporting "%s"   r!   r   r"   r   r   r   r   r   r      rA   z-Reduction not defined on ufunc with signaturer   r!   z?method outer is not allowed in ufunc with non-trivial signature)r   hasattrr   r+   arrayassertPreciseEqualr[   r   assertRaisesRuntimeErrorr   str	exceptionr   r   r   r   )r2   r3   attrsattrcontainsr   rI   raisesr   r   r   test_gufunc_attributes   s6   


z(TestDynamicGUFunc.test_gufunc_attributesc                 C   s:  t ddd }| |j tg d}tg d}tg d}|||| | |tg d | |j | ||d | ||tg d	 | |	d
dgddgtddgddgg | |
|d
dgtddg tg d}tddg}||d
dg| | |tg d d S )Nz	(),()->()c                 S   s   | | |d< d S r   r   )r   r   rI   r   r   r   r;      s   z6TestDynamicGUFunc.test_gufunc_attributes2.<locals>.addr   )r"   r   r!   r   r   )r%   r%   r%   r%   rA   r   r   r   r!   r      )r!   r"   r   r"   )r   assertIsNoner   r+   r   r   r[   r   r   r   r   r   )r2   r;   r   brI   r   r   r   r   r   test_gufunc_attributes2   s"   
."z)TestDynamicGUFunc.test_gufunc_attributes2N)
r]   r\   r^   r:   ro   rt   r   rX   r   r   r   r   r   r   rg   i   s    (rg   c                   @   s<   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dS )TestGUVectorizeScalarz<
    Nothing keeps user from out-of-bound memory access
    r    c                 C   sl   t dgd| jdddd }tjdtjdd	d
}||}t|jd D ]}| || || 	  q%dS )ru   zvoid(int32[:], int32[:])rv   Trw   c                 S   ry   rz   rG   r{   r   r   r   r~     s   z9TestGUVectorizeScalar.test_scalar_output.<locals>.sum_rowr   r#   r   r   r   N)
r   r:   r+   r,   r   r-   r   r
   r[   r   )r2   r~   r|   r   r   r   r   r   test_scalar_output  s   

z(TestGUVectorizeScalar.test_scalar_outputc                 C   sR   t dgd| jdddd }tjdtjdd	d
}||d}| |d | d S )Nzint32[:], int32[:], int32[:]rY   Trw   c                 S   s,   t | jd D ]}| | |d  ||< qd S r   rG   r|   r   r   r   r   r   r   foo-  s   z4TestGUVectorizeScalar.test_scalar_input.<locals>.foorO   r#   rA   r   r!   )r   r:   r+   r,   r   r-   r   )r2   r   r|   r   r   r   r   test_scalar_input+  s   

z'TestGUVectorizeScalar.test_scalar_inputc                 C   s   dd }t dgd| jd|}tdtj}||d}t|}||d| tj|| tdtj	dd}||d}t|}t
|jd	 D ]}||| d||  qMtj|| d S )
Nc                 S   s(   t | jD ]}|| | d  ||< qd S )Nr   )r   sizer   r   r   r   pyfunc;  s   zATestGUVectorizeScalar.test_scalar_input_core_type.<locals>.pyfuncint32[:], int32, int32[:]rY   r9   rA   r!   rN   r   )r   r:   r+   r,   astyper   rU   r0   rC   r-   r   r
   )r2   r   	my_gufuncarrgotrV   axr   r   r   test_scalar_input_core_type:  s$   



z1TestGUVectorizeScalar.test_scalar_input_core_typec                 C   Z   |  t}tdgd| jddd }W d    n1 sw   Y  | dt|j d S )Nr   z(n),(n)->(n)r9   c                 S      d S Nr   )r   r   cr   r   r   r   X     zGTestGUVectorizeScalar.test_scalar_input_core_type_error.<locals>.pyfuncz2scalar type int32 given for non scalar argument #2r   r   r   r:   r[   r   r   r2   r   r   r   r   r   !test_scalar_input_core_type_errorV     z7TestGUVectorizeScalar.test_scalar_input_core_type_errorc                 C   r   )Nzint32[:], int32[:]z
(m,n)->(n)r9   c                 S   r   r   r   )r   r   r   r   r   r   a  r   z8TestGUVectorizeScalar.test_ndim_mismatch.<locals>.pyfuncz,type and shape signature mismatch for arg #1r   r   r   r   r   test_ndim_mismatch_  r   z(TestGUVectorizeScalar.test_ndim_mismatchN)
r]   r\   r^   r_   r:   r   r   r   r   r   r   r   r   r   r     s    	r   c                   @   ra   )TestGUVectorizeScalarParallelFrc   Nrd   r   r   r   r   r   i  rf   r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestGUVectorizePicklingc                 C   s   t dgddd }t|}t|}| |j|j | |j|j | |j|j | |jj	|jj	 | 
|j |  | 
|j | |d|d td}| |||| dS )zNon-dynamic gufunc.
        zf8,f8[:]()->()c                 S      | d |d d < d S Nr!   r   r   r   r   r   r   doubler     zFTestGUVectorizePickling.test_pickle_gufunc_non_dyanmic.<locals>.double      ?rA   N)r   pickledumpsloadsr[   _frozenidentityri   gufunc_builder_sigsr   disable_compiler   r+   r,   )r2   r   serclonedr   r   r   r   test_pickle_gufunc_non_dyanmico  s    





z6TestGUVectorizePickling.test_pickle_gufunc_non_dyanmicc                 C   s   t ddddd }t|}t|}| |j|j | |j|j | |j|j | |jj	|jj	 | 
|j td}td}|d|d |d|d | || td}t|}t|}|||d |||d | || d	S )
z:Dynamic gufunc w/o prepopulating before pickling.
        r   r   r   c                 S   r   r   r   r   r   r   r   r     r   zLTestGUVectorizePickling.test_pickle_gufunc_dyanmic_null_init.<locals>.doubler   rp   rA   N)r   r   r   r   r[   r   r   ri   r   r   assertFalser+   rq   r   r,   rU   )r2   r   r   r   expectr   r   r   r   r   $test_pickle_gufunc_dyanmic_null_init  s,   









z<TestGUVectorizePickling.test_pickle_gufunc_dyanmic_null_initc                 C   sJ  t ddddd }td}td}|d|d td}t|}t|}|||d t|}t|}| |j	|j	 | |j
|j
 | |j|j | |jj|jj | |j	 |  | |j	 td}td}|d|d |d|d | || t|}t|}|||d |||d | || d	S )
zDynamic gufunc prepopulated before pickling.

        Once unpickled, we disable compilation to verify that the gufunc
        compilation state is carried over.
        r   r   r   c                 S   r   r   r   r   r   r   r   r     r   zNTestGUVectorizePickling.test_pickle_gufunc_dynamic_initialized.<locals>.doubler   rp   rA   N)r   r+   rq   r,   rU   r   r   r   r[   r   r   ri   r   r   r   r   r   r   )r2   r   r   r   r   r   r   r   r   r   &test_pickle_gufunc_dynamic_initialized  s<   













z>TestGUVectorizePickling.test_pickle_gufunc_dynamic_initializedN)r]   r\   r^   r   r   r   r   r   r   r   r   n  s    "r   __main__)unittestr   numpyr+   numpy.core.umath_testscoreumath_testsr.   numbar   r   r   r   r   numba.np.ufuncr   numba.tests.supportr   r	   r   r   r   rb   rg   r   r   r   r]   mainr   r   r   r   <module>   s&    I $]r