B
     \I                 @   s6  d dl mZmZmZ d dlZd dlZd dlZd dlZddl	m
Z
mZmZmZmZ eeeejddd ZejdkZedkZed	ejed
ejedejedejedejedejedejedej edej!edej"edej#edej$edej%iZ&e'dej(Z)e'dej(Z*edj+Z,dd Z-dd Z.dd Z/ej0dej1d ej2d!ej3d"iZ4d#d$ Z5d%d& Z6d'd( Z7d)d* Z8d+d, Z9d-d. Z:d/d0 Z;d1d2 Z<d3d4 Z=G d5d6 d6e>d7d8Z?dNd:d;Z@d<d= ZAd>d? ZBd@dA ZCdBdC ZDdDdE ZEdOdFdGZFdPdHdIZGdJdK ZHdLdM ZIdS )Q    )print_functiondivisionabsolute_importN   )errorstypesconfig
npdatetimeutils.   )   r   )r   
   boolint8int16int32int64uint8uint16uint32uint64float32float64	complex64
complex128z[<>=\|]([a-z])(\d+)?$z[<>=\|]([mM])8?(\[([a-z]+)\])?$ZU1c             C   s   t | j}|st| | }|d }|dkrn| jdkrBtd| jt }|t|d ksdt	dt
|S |dkr| j}|t|d kst	dt
|S t| d S )	Nr   Uz=|z%Does not support non-native byteorderr   zUnicode char size mismatchSzChar size mismatch)
re_typestrmatchstrNotImplementedErrorgroups	byteorderitemsizesizeof_unicode_charintAssertionErrorr   UnicodeCharSeqCharSeq)dtypemr"   typecodecount r.   2lib/python3.7/site-packages/numba/numpy_support.py_from_str_dtype-   s     



r0   c             C   sd   t | j}|st| | }|d }|d p2d}|dkrFt|S |dkrXt|S t| d S )Nr   r    r+   M)re_datetimestrr   r    r!   r"   r   NPTimedelta
NPDatetime)r*   r+   r"   r,   unitr.   r.   r/   _from_datetime_dtypeF   s    

r7   c             C   s   t | t kr$t| tjr$t| } nt| dddk	r<t| S yt|  S  tk
r   | j	}|dkrjt
| S |dkrzt| S |dkrt| jd }t|| jS Y nX t| dS )z
    Return a Numba Type instance corresponding to the given Numpy *dtype*.
    NotImplementedError is raised on unsupported Numpy dtypes.
    fieldsNZSUmMVr   )type
issubclassnpgenericr*   getattrfrom_struct_dtype
FROM_DTYPEKeyErrorcharr0   r7   
from_dtypeZsubdtyper   NestedArrayshaper!   )r*   rC   Zsubtyper.   r.   r/   rD   U   s     rD   ZM8Zm8r   r   c             C   sN  t | } t| t jt jt jfr.tt| S | t j	krBtdS t| t j
t jfrtt|  }| jrztd|| jf S t|S t| t jt jfrtt|  }td|| jf S t| t jrt| S t| t jrt| jS t| t jjrt| jS t| t jrt| jS t| t jr<t| jt| jf}t|S td| f dS )z
    Return a numpy dtype instance corresponding to the given Numba type.
    NotImplementedError is if no correspondence is known.
    ?z%s[%s]z%s%dz)%r cannot be represented as a Numpy dtypeN)r   Z	unliteral
isinstanceZComplexZIntegerZFloatr=   r*   r    Zbool_r5   r4   _as_dtype_lettersr;   r6   r)   r(   r-   Recordas_struct_dtypeZ
EnumMemberas_dtypeZnpytypesZDTypeZNumberClassrE   tuplerF   r!   )Znbtypeletterspecr.   r.   r/   rL   w   s4    







rL   c                sZ   t  tjstdd  jD }|dd  jD  fdd|D  jd}tj| jdS )z8Convert Numba Record type to NumPy structured dtype
    c             S   s   g | ]\}}|qS r.   r.   ).0k_r.   r.   r/   
<listcomp>   s    z#as_struct_dtype.<locals>.<listcomp>c             S   s   g | ]\}}t |qS r.   )rL   )rP   rR   tr.   r.   r/   rS      s    c                s   g | ]}  |qS r.   )offset)rP   rQ   )recr.   r/   rS      s    )namesZformatsZoffsetsr$   )Zalign)	rH   r   rJ   r'   memberssizer=   r*   aligned)rV   rW   r8   r.   )rV   r/   rK      s    
rK   c             C   s   t t| tkS )N)r=   r*   r;   rA   )valr.   r.   r/   is_arrayscalar   s    r\   c             C   sT   t | tjr| j}n8ytt| }W n$ tk
rJ   tdt|  Y nX t|S )Nz#no corresponding numpy dtype for %r)rH   r=   r>   r*   r;   	TypeErrorr!   rD   )r[   r*   r.   r.   r/   map_arrayscalar_type   s    r^   c             C   s   t | tjS )N)rH   r=   Zndarray)r[   r.   r.   r/   is_array   s    r_   c             C   s(   | j d rd}n| j d r d}nd}|S )NZC_CONTIGUOUSCZF_CONTIGUOUSFA)flags)r[   Zlayoutr.   r.   r/   
map_layout   s    

rd   c             C   sZ   t d}d}d}x8t| D ],\}}t|tjr|j|kr|}|}|j}qW |dk	sVt|S )z
    Given the array-compatible input types to an operation (e.g. ufunc),
    select the appropriate input for wrapping the operation output,
    according to each input's __array_priority__.

    An index into *inputs* is returned.
    z-infN)float	enumeraterH   r   ZArrayCompatiblearray_priorityr'   )inputsZmax_prioselected_inputZselected_indexindextyr.   r.   r/   select_array_wrapper   s    
rl   c             C   sR   |t | }||f}| d|i }|dkrL|jtjjkr<|S td|f |jS )a.  
    Given the array-compatible input types to an operation (e.g. ufunc),
    and the operation's formal output type (a types.Array instance),
    resolve the actual output type using the typing *context*.

    This uses a mechanism compatible with Numpy's __array_priority__ /
    __array_wrap__.
    Z__array_wrap__Nz__array_wrap__ failed for %s)rl   Zresolve_function_typerg   r   ZArrayr   ZTypingErrorZreturn_type)contextrh   Zformal_outputri   argsZsigr.   r.   r/   resolve_output_type   s    	ro   c                sn   ddl m} |j}y||| k}W nD tk
rh   dd |j|j D }d t fdd|D }Y nX |S )aR  Return whether the *loop* for the *ufunc* is supported -in nopython-.

    *loop* should be a UFuncLoopSpec instance, and *ufunc* a numpy ufunc.

    For ufuncs implemented using the ufunc_db, it is supported if the ufunc_db
    contains a lowering definition for 'loop' in the 'ufunc' entry.

    For other ufuncs, it is type based. The loop will be considered valid if it
    only contains the following letter types: '?bBhHiIlLqQfd'. Note this is
    legacy and when implementing new ufuncs the ufunc_db should be preferred,
    as it allows for a more fine-grained incremental support.
    r   )ufunc_dbc             S   s   g | ]
}|j qS r.   )rC   )rP   xr.   r.   r/   rS   
  s    z(supported_ufunc_loop.<locals>.<listcomp>z?bBhHiIlLqQfdc             3   s   | ]}| kV  qd S )Nr.   )rP   rT   )supported_typesr.   r/   	<genexpr>  s    z'supported_ufunc_loop.<locals>.<genexpr>)Ztargetsrp   	ufunc_sigZget_ufunc_inforB   numpy_inputsnumpy_outputsall)ufuncZlooprp   Zloop_sigZsupported_loopZ
loop_typesr.   )rr   r/   supported_ufunc_loop   s    ry   c               @   s,   e Zd ZdZdZedd Zedd ZdS )UFuncLoopSpecz
    An object describing a ufunc loop's inner types.  Properties:
    - inputs: the inputs' Numba types
    - outputs: the outputs' Numba types
    - ufunc_sig: the string representing the ufunc's type signature, in
      Numpy format (e.g. "ii->i")
    r.   c             C   s   dd | j D S )Nc             S   s   g | ]}t |qS r.   )rL   )rP   rq   r.   r.   r/   rS   !  s    z.UFuncLoopSpec.numpy_inputs.<locals>.<listcomp>)rh   )selfr.   r.   r/   ru     s    zUFuncLoopSpec.numpy_inputsc             C   s   dd | j D S )Nc             S   s   g | ]}t |qS r.   )rL   )rP   rq   r.   r.   r/   rS   %  s    z/UFuncLoopSpec.numpy_outputs.<locals>.<listcomp>)outputs)r{   r.   r.   r/   rv   #  s    zUFuncLoopSpec.numpy_outputsN)__name__
__module____qualname____doc__	__slots__propertyru   rv   r.   r.   r.   r/   rz     s   rz   Z_UFuncLoopSpec)rh   r|   rt   safec             C   s>   t | } t |}|r0| jdkr0|jdkr0dS t | ||S )aW  
    A variant of np.can_cast() that can allow casting any integer to
    any real or complex type, in case the operation has mixed-kind
    inputs.

    For example we want `np.power(float32, int32)` to be computed using
    SP arithmetic and return `float32`.
    However, `np.sqrt(int32)` should use DP arithmetic and return `float64`.
    iucfT)r=   r*   kindZcan_cast)Zfrom_tohas_mixed_inputsZcastingr.   r.   r/   ufunc_can_cast(  s
    


r   c          	   C   s  |d| j  }|| j d }t|| j ks.tydd |D }W n tk
rT   dS X ydd |D }W n tk
r|   dS X tdd |D otdd |D }dd	 }x | jD ]}|d| j  }	|| j d }
d
|	krqd}xVt||	D ]H\}}|jdks|dkr$|j|kr:d}P qt	|j||dsd}P qW |rt
rx<t||
D ].\}}|jdkrVt	||j|dsVd}P qVW |ry|||	}|||
}W n tk
r   wY qX t|||S qW dS )aj  Find the appropriate loop to be used for a ufunc based on the types
    of the operands

    ufunc        - The ufunc we want to check
    arg_types    - The tuple of arguments to the ufunc, including any
                   explicit output(s).
    return value - A UFuncLoopSpec identifying the loop, or None
                   if no matching loop is found.
    Nc             S   s   g | ]}t |qS r.   )rL   )rP   rq   r.   r.   r/   rS   K  s    z,ufunc_find_matching_loop.<locals>.<listcomp>c             S   s   g | ]}t |qS r.   )rL   )rP   rq   r.   r.   r/   rS   O  s    c             s   s   | ]}|j d kV  qdS )r   N)r   )rP   dtr.   r.   r/   rs   U  s    z+ufunc_find_matching_loop.<locals>.<genexpr>c             s   s   | ]}|j d kV  qdS )r   N)r   )rP   r   r.   r.   r/   rs   V  s    c             S   sJ   t |t | kstdd t| |D }|dd |t | d D 7 }|S )z
        Return a list of Numba types representing *ufunc_letters*,
        except when the letter designates a datetime64 or timedelta64,
        in which case the type is taken from *numba_types*.
        c             S   s*   g | ]"\}}|d kr|nt t|qS )r9   )rD   r=   r*   )rP   tprN   r.   r.   r/   rS   _  s   zBufunc_find_matching_loop.<locals>.choose_types.<locals>.<listcomp>c             S   s   g | ]}t t|qS r.   )rD   r=   r*   )rP   rN   r.   r.   r/   rS   b  s   N)lenr'   zip)Znumba_typesZufunc_lettersr   r.   r.   r/   choose_typesX  s    z.ufunc_find_matching_loop.<locals>.choose_typesOTr9   Fr   Z	same_kind)Zninr   r'   r!   anyr   Znoutr   rC   r   strict_ufunc_typingrz   )rx   Z	arg_typesZinput_typesZoutput_typesZnp_input_typesZnp_output_typesr   r   	candidateZufunc_inputsZufunc_outputsfoundZouterinnerrh   r|   r.   r.   r/   ufunc_find_matching_loop:  sX    

r   c             C   s   | j S )N)Zisalignedstruct)Zstructr.   r.   r/   _is_aligned_struct  s    r   c       
      C   sv   | j rtdg }xF| j D ]8\}}|dd \}}t|}||d}|||f qW | j}t| }	t	|||	S )z:Convert a NumPy structured dtype to Numba Record type
    z&Do not support dtype containing objectNr   )r;   rU   )
Z	hasobjectr]   r8   itemsrD   appendr$   r   r   rJ   )
r*   r8   nameinfoZ	elemdtyperU   rk   ZinfosrY   rZ   r.   r.   r/   r@     s    r@   c             C   s&   t | tjr| j} tj| }|| S )z;
    Get a ctypes array of *nbytes* starting at *ptr*.
    )rH   ctypesc_void_pvalueZc_byteZfrom_address)ptrnbytesZarrtyr.   r.   r/   _get_bytes_buffer  s    
r   c             C   s   t t| ||S )N)r=   Z
frombufferr   )r   r   r*   r.   r.   r/   _get_array_from_ptr  s    r   c             C   s   ddl m} y
| j} W n tk
r*   Y nX |dk	r>t|}t| tjr`|dkrZt	d| }nrt| tj
r|| j}t|tjstt|j}|dk	r||krt	d|| f |}t| tj}nt	d| f |jtj|tjd }t||||S )z
    Return a Numpy array view over the data pointed to by *ptr* with the
    given *shape*, in C order.  If *dtype* is given, it is used as the
    array's dtype, otherwise the array's dtype is inferred from *ptr*'s type.
    r   )from_ctypesNz*explicit dtype required for void* argumentz%mismatching dtype '%s' for pointer %sz!expected a ctypes pointer, got %r)r*   )Ztyping.ctypes_utilsr   Z_as_parameter_AttributeErrorr=   r*   rH   r   r   r]   Z_Pointer	__class__r   ZCPointerr'   rL   castr$   productZintpr   Zreshape)r   rF   r*   r   pZptrtyZ	ptr_dtyper   r.   r.   r/   carray  s.    



r   c             C   s(   t |tjs|ddd }t| ||jS )z
    Return a Numpy array view over the data pointed to by *ptr* with the
    given *shape*, in Fortran order.  If *dtype* is given, it is used as the
    array's dtype, otherwise the array's dtype is inferred from *ptr*'s type.
    N)rH   r
   Z	INT_TYPESr   T)r   rF   r*   r.   r.   r/   farray  s    r   c             C   s   t | }|d }x |dkr0| | dkr0|d8 }qW |dk r>dS ||| krNdS d}x ||k rr| | dkrr|d7 }qTW |}x4||kr|| | |  ||d  krdS |d8 }qzW dS )zvIs the given shape, strides, and itemsize of C layout?

    Note: The code is usable as a numba-compiled function
    r   r   r   TF)r   )dimsstridesr$   ndZinneraxZouteraxaxr.   r.   r/   is_contiguous  s"    
r   c             C   s   t | }d}x ||k r,| | dkr,|d7 }qW ||kr:dS ||| krJdS |d }x ||krr| | dkrr|d8 }qTW |}x4||k r|| | |  ||d  krdS |d7 }qzW dS )zvIs the given shape, strides, and itemsize of F layout?

    Note: The code is usable as a numba-compiled function
    r   r   TF)r   )r   r   r$   r   ZfirstaxZlastaxr   r.   r.   r/   
is_fortran  s"    
r   )r   )N)N)JZ
__future__r   r   r   collectionsr   reZnumpyr=   r1   r   r   r   r	   r
   rM   mapr&   __version__splitversionZ	PYVERSIONZint_divbyzero_returns_zeror   r*   Zbooleanr   r   r   r   r   r   r   r   r   r   r   r   rA   compileIr   r3   r$   r%   r0   r7   rD   r5   r4   r)   r(   rI   rL   rK   r\   r^   r_   rd   rl   ro   ry   
namedtuplerz   r   r   r   r@   r   r   r   r   r   r   r.   r.   r.   r/   <module>   sh   

!
"
_	
&
!