U
    ,dl                     @   sf   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlZG dd	 d	e
jZdS )
    typeof)types)GUFuncBuilder)parse_signature)ufunc_find_matching_loop)	serializeNc                   @   s  e Zd ZdZdddi fddZdd Zedd	 Zd
d Zdd Z	dd Z
dd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 ZdS )4GUFuncz
    Dynamic generalized universal function (GUFunc)
    intended to act like a normal Numpy gufunc, but capable
    of call-time (just-in-time) compilation of fast loops
    specialized to inputs.
    NFc                 C   sF   d | _ d| _|| _|| _t|||||| _| jjj| _t	| | d S )NF)
ufunc_frozen_is_dynamic	_identityr   gufunc_builderpy_func__name__	functoolsupdate_wrapper)selfr   	signatureidentitycache
is_dynamictargetoptions r   9/tmp/pip-unpacked-wheel-eu7e0c37/numba/np/ufunc/gufunc.py__init__   s        zGUFunc.__init__c              
   C   s2   | j }t|j|j| j|j| j|j|j| j	d}|S )N)r   r   r   r   r   r   typesigsfrozen)
r   dictr   r   r   r   r   r   _sigsr   )r   gbdctr   r   r   _reduce_states!   s    
zGUFunc._reduce_statesc	                 C   s:   | ||||||d}	|D ]}
|	 |
 q|	  ||	_|	S )N)r   r   r   r   r   r   )addbuild_ufuncr   )clsr   r   r   r   r   r   r   r   r   sigr   r   r   _rebuild/   s     zGUFunc._rebuildc                 C   s   d| j  dS )Nz<numba._GUFunc 'z'>)r   r   r   r   r   __repr__;   s    zGUFunc.__repr__c                 C   s   | j | d S N)r   r#   )r   Zftyr   r   r   r#   >   s    z
GUFunc.addc                 C   s   | j  | _| S r*   )r   r$   r
   r(   r   r   r   r$   A   s    zGUFunc.build_ufuncc                 C   s   t | jjdkstd| _dS )zI
        Disable the compilation of new signatures at call time.
        r   TN)lenr   r   AssertionErrorr   r(   r   r   r   disable_compileE   s    zGUFunc.disable_compilec                 C   s   | j S r*   )r   r(   r   r   r   r   M   s    zGUFunc.is_dynamicc                 C   s   | j jS r*   )r
   ninr(   r   r   r   r.   Q   s    z
GUFunc.ninc                 C   s   | j jS r*   )r
   noutr(   r   r   r   r/   U   s    zGUFunc.noutc                 C   s   | j jS r*   )r
   nargsr(   r   r   r   r0   Y   s    zGUFunc.nargsc                 C   s   | j jS r*   )r
   ntypesr(   r   r   r   r1   ]   s    zGUFunc.ntypesc                 C   s   | j jS r*   )r
   r   r(   r   r   r   r   a   s    zGUFunc.typesc                 C   s   | j jS r*   )r
   r   r(   r   r   r   r   e   s    zGUFunc.identityc                 C   s   | j jS r*   )r
   r   r(   r   r   r   r   i   s    zGUFunc.signaturec                 C   s   | j jS r*   )r
   
accumulater(   r   r   r   r2   m   s    zGUFunc.accumulatec                 C   s   | j jS r*   )r
   atr(   r   r   r   r3   q   s    z	GUFunc.atc                 C   s   | j jS r*   )r
   outerr(   r   r   r   r4   u   s    zGUFunc.outerc                 C   s   | j jS r*   )r
   reducer(   r   r   r   r5   y   s    zGUFunc.reducec                 C   s   | j jS r*   )r
   reduceatr(   r   r   r   r6   }   s    zGUFunc.reduceatc                 C   sD   t dd |}g }|D ](}t|tjr4||j q|| q|S )Nc                 S   s   t | S r*   r   )xr   r   r   <lambda>       z*GUFunc._get_ewise_dtypes.<locals>.<lambda>)map
isinstancer   ArrayappendZdtype)r   argsZargtysZtysZargtyr   r   r   _get_ewise_dtypes   s    zGUFunc._get_ewise_dtypesc                 G   s$   t | jj}t|t|d d kS )Nr      )r   r   r   r+   )r   r>   
parsed_sigr   r   r   _num_args_match   s    zGUFunc._num_args_matchc           
      G   s   t | jj}| |}g }t|d D ]@\}}t|}|dkrN|||  q&|t|| |d q&|d }t|d d pd}	|t||	d tj	| S )Nr   Ar@   )
r   r   r   r?   	enumerater+   r=   r   r<   none)
r   r>   rA   Zewise_typeslidxZsig_dimndimZrettyZret_ndimr   r   r   _get_signature   s    
zGUFunc._get_signaturec                 O   s   | j s| js| j||S d|kr0||df7 }| j| dkrTd| j d}t|| |}| jrpt| j|s| j	| }| 
| |   | j||S )NoutFz Too few arguments for function 'z'. Note that the pattern `out = gufunc(Arg1, Arg2, ..., ArgN)` is not allowed. Use `gufunc(Arg1, Arg2, ..., ArgN, out) instead.)r   r   r
   poprB   r   	TypeErrorr?   r   rJ   r#   r$   )r   r>   kwargsmsgZewiser&   r   r   r   __call__   s    


zGUFunc.__call__)r   
__module____qualname____doc__r   r"   classmethodr'   r)   r#   r$   r-   propertyr   r.   r/   r0   r1   r   r   r   r2   r3   r4   r5   r6   r?   rB   rJ   rP   r   r   r   r   r	   
   sT    















r	   )Znumbar   Z
numba.corer   Znumba.np.ufunc.ufuncbuilderr   Znumba.np.ufunc.sigparser   Znumba.np.numpy_supportr   r   r   ZReduceMixinr	   r   r   r   r   <module>   s   