U
    hd                 	   @   s
  d Z 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Z	ddl
mZmZ ddlmZmZmZmZmZ ddlmZmZmZmZmZmZmZ ddlmZmZmZ dd	lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) dd
l*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl.m<Z< ddl=m>Z?m@ZAmBZBmCZCmDZD dd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKd d! ZLd"d# ZMd4d$d%ZNd&d' ZOd(d) ZPe0ePd*d+ ZQed,ejRd-d. ZSd/d0 ZTed1ejUe%e)jVd2d3 ZWd5d4d5ZXd6d7 ZYd8d9 ZZeej[ejRej\eej[ejRej]d:d; Z^eej[ejRej_d<d= Z`eejaejRejbejbd>d? ZceedejRd@dA ZeedBejfdCdD ZgedEejfejbdFdG ZhG dHdI dIeiZjG dJdK dKejZkG dLdM dMejZlG dNdO dOejZmG dPdQ dQejZnG dRdS dSejZoG dTdU dUeiZpdVdW Zqeej[ejRejfdXdY ZrdZd[ Zsd\d] Ztd^d_ Zud`da Zvdbdc Zwddde Zxdfdg Zydhdi Zze2djdk Z{e2dldm Z|e/dndo Z}dpdq Z~e0e	jdrds Ze/dtdu Ze0e	jdvdw Ze0e	jdxdy Zdzd{ Zd|d} Zed~ejfdd Zdd Zed~ejfej_dd Zed~ejfeejbdd Ze0e	jd6ddZe ejfddd Ze0e	jd7ddZe0e	jd8ddZdd Zdd Zedejfej_dd Zedejfeejbdd Ze0e	jdd Ze0e	jd9ddZedejfdd Zee	jejfdd Zedejfdd Ze/dd Ze/dd Ze0e	jd:ddZe1ejfdd;ddZdd Ze0e	jdd Ze0e	jdd Ze0e	jdd Ze/dd Ze1ejfddd Ze2dd Zdd Ze0eddddĄ ZedejfejddǄ Ze ejfdȃddʄ Ze ejfd˃e ejd˃dd̈́ Ze ejfd΃e ejd΃ddЄ Ze ejfdуe ejdуddӄ Ze ejfdԃddք Ze ejfd׃e ejd׃ddل Ze ejfdڃe ejdڃdd܄ Ze ejd݃dd߄ Ze ejddd Ze ejddd Ze ejddd Ze ejfddd Ze ejddd Ze#ejeje#ejejÃdd Zdd Ze ejfddd Ze ejd݃e ejddd Ze ejddd Ze ejfddd Ze ejfddd Zːd d Ze1ejfde1ejfddd Z͐dd Ze ejϐde΃ e ejϐd	e΃ ed
ejejbdd Ze!ejfdd Zed
ejfej-dd Ze!ejԃdd Ze"ejԃdd Zed
ejej-dd Zed
ejej؃dd Zedejej-ejbdd Zedejejejbdd Ze$ejfdd Ze$ejԃd d! Ze$ejރd"d# Zeejejfejfd$d% Ze3ejfd&d'd( Zd)d* Zd+d, Zd<d-d.Zd=d/d0Zd1d2 Zd3d4 Zd5d6 Ze ejfd7d8d9 Zed1eje%e)jVd:d; Zeej[ejej\d<d= Zeejaejej\ejbd>d? Zeedejd@dA Zee	jejfdBdC Zed1eje%e)jVdDdE Zeeeej\ee	jeej\dFdG Zeeej_ee	jej_dHdI Zed1eje%e)jVdJdK Zee	jejbdLdM Zed1eje%e)jVdNdO ZdPdQ Ze<ejfdRdSdT ZdUdV Ze2dWdX Z dYdZ Zd[d\ Zd]d^ Zd_d` Ze2dadb Ze0e	jefdcddZe2dedf Z	e0e	j
d>dgdhZe2didj Ze1ejfdkdldm Ze0e	jefdndoZe0e	jd?dpdqZe0e	jd@drdsZe0e	jdAdtduZe0e	jdBdvdwZe0e	jdCdxdyZe0e	jdDdzd{Zd|d} Ze0ed~d Ze0e	jddefddZe0e	j dEddZ!e0e	j"e1ejfddd Z#dd Z$e0e	j%dFddZ&e0e	j'dGddZ(dd Z)e2dd Z*edejfdd Z+e0e	j,dd Z-dd Z.e2dd Z/e0e	j0dd Z1e0e	j2dd Z3edejfejedejfej-dd Z4e2dd Z5e0e	j6efddZ7e0edHddZ8e0edIddZ9dd Z:dd Z;dd Z<dd Z=dd Z>dd Z?dd Z@dd ZAe2dd ZBe0e	jCdJddZDdd ZEdd ZFdd ZGdÐdĄ ZHe2dŐdƄ ZIe0e	jHdǐdȄ ZJdɐdʄ ZKdːd̄ ZLe0e	jMd͐d΄ ZNe0e	jOdϐdЄ ZPe0e	jQdѐd҄ ZRdӐdԄ ZSdՐdք ZTdאd؄ ZUdِdڄ ZVe2dېd܄ ZWe0e	jXdKdݐdބZYdߐd ZZe2dd Z[e0e	j\dd Z]dd Z^e2dd Z_e0e	j`dLddZadd Zbe2dd Zce0e	jddd Zee2dd Zfe0e	jgdd Zhe2dd Zie0e	jjdd Zke1ejfddd Zle1ejfddd Zme0e	jndd  Zoe0e	jpdd Zqe2dd Zre0e	jsdd Zte0e	judMddZve0e	jwdNd	d
Zxi Zydd ZzdOddZ{edejfdd Z|e0e	j}dd Z~edejfej-ee	jejfej-dd Ze#ejfejfdd Ze#ejfeje#ejfeje#ejfej\e#ejfeje#ejfeje#ejfeje#ejfejdd Ze#ejfejdd Zdd Zeed d! Zeeejfej_ej_d"d# Ze0e	jjjdPd$d%Ze0e	jjjdQd&d'Ze0ed(d) Ze0e	jd*d+ Ze/d,d- Ze0e	jd.d/ Ze0e	jdRd2d3ZdS (S  z[
Implementation of operations on Array objects and objects supporting
the buffer protocol.
    N)ir)Constant)pndindexliteral_unroll)typestypingerrorscgutils	extending)as_dtypecarrayfarrayis_contiguous
is_fortrancheck_is_integertype_is_scalar)type_can_asarrayis_nonelikenumpy_version)lower_builtinlower_getattrlower_getattr_genericlower_setattr_generic
lower_castlower_constantiternext_implimpl_ret_borrowedimpl_ret_new_refimpl_ret_untrackedRefType)	signature)StringLiteral)register_jitableoverloadoverload_method	intrinsicoverload_attribute)	quicksort	mergesort)slicing)tuple_setitembuild_full_slice_tuple)overload_classmethod)parse_dtypeparse_shape_parse_nested_sequence_sequence_of_arrays_choose_concatenation_layoutc                 C   s4   t |j|t |j|g}| j|}|d| dS )zz
    Set the "range" metadata on a load instruction.
    Note the interval is in the form [lower_bound, upper_bound).
    rangeN)r   typemoduleZadd_metadataZset_metadata)builderloadlower_boundupper_boundZrange_operandsmd r:   J/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/np/arrayobj.pyset_range_metadata+   s
    

r<   c                 C   s&   d|j jd > d }t| |d| dS )zF
    Mark the result of a load instruction as positive (or zero).
       r   N)r3   widthr<   )r5   r6   r8   r:   r:   r;   mark_positive6   s    r?   c                    s6   j tjG  fddd  S )z
    Return the Structure representation of the given *array_type*
    (an instance of types.ArrayCompatible).

    Note this does not call __array_wrap__ in case a new array structure
    is being created (rather than populated).
    c                       s4   e Zd Z fddZefddZ  ZS )zmake_array.<locals>.ArrayStructc                    s   t }z| jd|}W n$ tk
r@   t | | Y S X | jj }| |}|d krttj	| j
|dd}n|}|| j
|f}||fS )NZ	__array__T)zfill)r    _contextget_functionNotImplementedErrorsuper
_make_refsdata_model_managerZ_get_be_typer	   alloca_once_builder)selfrefsigZ
array_impl	datamodelZbe_typeZ	outer_ref)ArrayStruct	__class__
array_typereal_array_typer:   r;   rE   L   s    


z*make_array.<locals>.ArrayStruct._make_refsc                    sp   | j }dkr | dS | d}g }tD ]2}t||d|}||}|| t|| q0t	||S )z\
            Override .shape to inform LLVM that its elements are all positive.
            r   shape)
rH   __getattr___get_ptr_by_namer2   r	   gep_inboundsr6   appendr?   
pack_array)rI   r5   ptrdimsiZdimptrr6   )basendimr:   r;   rQ   `   s    


z%make_array.<locals>.ArrayStruct.shape)__name__
__module____qualname__rE   propertyrQ   __classcell__r:   rM   rO   rZ   r[   rP   )rN   r;   rM   J   s   rM   )Zas_arrayr	   create_struct_proxyr[   )rO   r:   ra   r;   
make_array>   s
    
-rc   c                 C   s   |  |j}| |S )zB
    Return the item size for the given array or buffer type.
    )get_data_typedtypeget_abi_sizeof)contextrO   lltyr:   r:   r;   get_itemsizez   s    ri   c                 C   s"   |j r
dnd}| j||j||dS )z3
    Load the item at the given array pointer.
    Nr=   align)alignedunpack_valuere   )rg   r5   arraytyrW   rk   r:   r:   r;   	load_item   s    ro   c                 C   s$   |j r
dnd}| j||j|||dS )z4
    Store the item at the given array pointer.
    Nr=   rj   )rl   
pack_valuere   )rg   r5   rn   valrW   rk   r:   r:   r;   
store_item   s    rr   c                 C   s>   |j r(| |||tj}t|||}n| |||tj}|S )zM
    Fix the integer index' type and value for the given dimension size.
    )signedcastr   intpr)   Z	fix_indexuintp)rg   r5   idxtyidxsizeindr:   r:   r;   fix_integer_index   s
    r{   c                 C   s^   t |tjrR|jdkrRt |jtjs(tt|| ||}t| |||j	}|j|fS ||fS dS )zS
    Normalize the index type and value.  0-d arrays are converted to scalars.
    r   N)

isinstancer   Arrayr[   re   IntegerAssertionErrorrc   ro   data)rg   r5   rw   rx   idxaryZidxvalr:   r:   r;   normalize_index   s    
r   c                    s2   t |r*t fddt||D  \}}||fS )z^
    Same as normalize_index(), but operating on sequences of
    index types and values.
    c                    s   g | ]\}}t  ||qS r:   )r   ).0rw   rx   r5   rg   r:   r;   
<listcomp>   s   z%normalize_indices.<locals>.<listcomp>)lenzip)rg   r5   index_typesindicesr:   r   r;   normalize_indices   s
    r   c                 C   s  | j }| j}| j}	ttjdd}
|j|
 }t|j}t|	j}||@ |kr|| }d| j	 dt
|dkrndnd d| d}t||d	krt||	d
d	}|tj}t|ttfrt|||}t|ttfrt|||}t|tr||}t|||||d}|d	kr2t||	dd	|d< n||d< |tjd}t|||jj}|D ]}|j||dgd}q^||d< t| }||krtd|| | D ]\}}t| || q| S )z
    Helper function for populating array structures.
    This avoids forgetting to set fields.

    *shape* and *strides* can be Python tuples or LLVM arrays.
    r=   CzThe datamodel for type z is missing fields  .Nmeminfo)rQ   stridesr   itemsizer   parentnswflagsnitemszmissing {0}) rA   rH   
_datamodelr   r}   float64rF   set_fieldsZ_fe_typer   
ValueErrorr   get_value_typeget_typeru   r|   tuplelistr	   rV   intdictget_constantunpack_tupler3   countmulkeysformatitemssetattr)arrayr   rQ   r   r   r   r   rg   r5   rL   Zstandard_arrayZstandard_array_type_datamodelZrequired_fieldsZdatamodel_fieldsmissingmsgintp_tattrsr   unpacked_shapeaxlenZ
got_fieldskvr:   r:   r;   populate_array   s`    


*



r   c                 C   sh   |j }|j}|tjd}t||j| j}|D ]}|j	||dgd}q0||_
|tjt|| |_dS )z
    Update some auxiliary information in *array* after some of its fields
    were changed.  `itemsize` and `nitems` are updated.
    r=   r   r   N)rA   rH   r   r   ru   r	   r   rQ   r[   r   r   ri   r   )arytyr   rg   r5   r   r   r   r:   r:   r;   update_array_info   s    r   c                 C   s
   t  dS )z/Constrain axis values to valid positive values.NrC   	func_namearg_namer[   axisr:   r:   r;   normalize_axis  s    r   c                    sL   t | tstdt |ts(td| j d|j d  fdd}|S )Nz func_name must be a str literal.zarg_name must be a str literal.z: Argument z* out of bounds for dimensions of the arrayc                    s,   |dk r||7 }|dk s ||kr(t  |S Nr   r   r   r   r:   r;   impl"  s
    z&normalize_axis_overloads.<locals>.impl)r|   r!   r   TypingErrorliteral_value)r   r   r[   r   r   r:   r   r;   normalize_axis_overloads  s    



r   Zgetiterc                 C   st   |j \}|\}| ||j}| tjd}t||}||_||_	| j
rX| j||| | }	t| ||j|	}
|
S r   )argsmake_helperreturn_typer   r   ru   r	   alloca_once_valueindexr   
enable_nrtnrtincref	_getvaluer   )rg   r5   rK   r   rn   r   iterobjzeroZindexptrresoutr:   r:   r;   getiter_array-  s    r   c              	   C   s   t j||j|jd}t j||j|jd}||d |}t ||j|}	|dd }
|dd }t|t	j
rt| |||||	|
|}| S |
rtt| |||	S dS )z1 Evaluate `ary[idx]`, where idx is a single int. r   r   r=   N)r	   r   rQ   r[   r   r   pointer_addr   r|   r   Buffer	make_viewr   r   ro   )rg   r5   r   r   aryrx   shapesr   offsetdataptrview_shapesview_stridesretaryr:   r:   r;   _getitem_array_single_intE  s      r   Ziternextc              	   C   s   |j \}|\}|j}| j|||d}t|| ||jd}	tj||	jdd\}
||j	}|
d||
}|| ||> t| ||j||	|}|| t||}|||j	 W 5 Q R X d S )Nvaluer=   r   <)r   rO   r   rc   r   r	   r   rQ   r6   r   icmp_signed	set_validif_thenr   
yield_typeyield_increment_indexstore)rg   r5   rK   r   resultZitertyiterrn   r   r   r   r   is_validr   Znindexr:   r:   r;   iternext_arrayZ  s*    
     
r   c              	   C   sD  |  tjd}|  tjd}t||j|j}	t||j|j}
g }g }g }tdd |D }d}t	||D ]n\}}|tj
kr|jt| d | }t|D ]2}|| ||	|  ||
|  |d7 }qqlt|tjrT| j|||d}t| ||| t|||	|  ||j t||}t|||
| }|| || nt|tjrt| ||||	| }|rt| |||	| | || n6t|r|| || |d8 }ntd|f |d7 }ql||jkst||jk r"||	|  ||
|  |d7 }qtj| ||||ddd}|||fS )	z
    Perform basic indexing on the given array.
    A (data pointer, shapes, strides) tuple is returned describing
    the corresponding view.
    r   r=   c                 S   s   g | ]}t |r|qS r:   r   r   rx   r:   r:   r;   r     s      z"basic_indexing.<locals>.<listcomp>r   unexpected index type: %sF
wraparoundboundscheck)r   r   ru   r	   r   rQ   r[   r   r   r   ellipsisr2   rU   r|   	SliceTyper   r)   guard_invalid_slice	fix_slicestartget_slice_lengthZ
fix_strider~   r{   Zdo_boundscheckr   rC   r   get_item_pointer)rg   r5   r   r   r   r   r   r   oner   r   Zoutput_indicesZoutput_shapesZoutput_stridesnum_newaxesaxindexvalrw   	n_missingrY   sliceshstrz   r   r:   r:   r;   basic_indexingv  sf    









 r  c           	   	   C   s.   t || |}t|||||j|j|jd |S )zF
    Build a view over the given array with the given parameters.
    r   rQ   r   r   r   r   )rc   r   r   r   r   )	rg   r5   r   r   r   r   r   r   r   r:   r:   r;   r     s    r   c              	   C   sb   t | |||||| jd\}}}	t|tjrHt| |||||||	}
|
 S |rPtt| |||S dS )zp
    Return the result of indexing *ary* with the given *indices*,
    returning either a scalar or a view.
    r   N)	r  enable_boundscheckr|   r   r   r   r   r   ro   )rg   r5   r   r   r   r   r   r   r   r   r   r:   r:   r;   _getitem_array_generic  s      r  c           	      C   sZ   |j \}}|\}}|jdks tt|| ||}t| ||j|||f|f}t| ||j|S )z4
    Basic indexing with an integer or a slice.
    r=   )r   r[   r   rc   r  r   r   	rg   r5   rK   r   r   rw   r   rx   r   r:   r:   r;   getitem_arraynd_intp  s    

   r  c              	   C   s   |j \}}|\}}t|| ||}|j}tj||t|d}	t| |||	\}}	tdd |D rvt| |||||||	S t	| ||j
||||	}
t| ||j
|
S )z2
    Basic or advanced indexing with a tuple.
    r   c                 s   s   | ]}t |tjV  qd S Nr|   r   r}   )r   tyr:   r:   r;   	<genexpr>  s     z&getitem_array_tuple.<locals>.<genexpr>)r   rc   r   r	   r   r   r   anyfancy_getitemr  r   r   )rg   r5   rK   r   r   tuptyr   tupr   r   r   r:   r:   r;   getitem_array_tuple  s.    
 
   
   r  c              	   C   s   |j \}}}|\}}}	t|tjr>|j}
tj||t|d}n|f}
|f}t|| ||}t| ||
|\}
}z"t	| ||||
|| j
d\}}}W n tk
r   d}Y n
X t|}|rt| ||||
|S | ||	||j}	t| |||	| dS )zH
    array[a] = scalar_or_array
    array[a,..,b] = scalar_or_array
    r   r  TN)r   r|   r   	BaseTupler	   r   r   rc   r   r  r  rC   boolfancy_setslicert   re   rr   )rg   r5   rK   r   r   rw   valtyr   rx   rq   r   r   r   r   r   Zuse_fancy_indexingr:   r:   r;   setitem_array	  s6    
 

 r  c           	      C   sD   |j \}|\}t|}|| ||}|j}||d}t| ||j|S r   )r   rc   rQ   extract_valuer   r   )	rg   r5   rK   r   r   r   ZarysttyZshapearyr   r:   r:   r;   	array_len2  s    r  z
array.itemc              	   C   st   |j \}|\}t|| ||}|j}|j|d||ddd d}| j|t|f W 5 Q R X t	| |||j
S )N!=r=   FZlikelyz>item(): can only convert an array of size 1 to a Python scalar)r   rc   r   r   r   r3   	call_convreturn_user_excr   ro   r   )rg   r5   rK   r   r   r   r   r   r:   r:   r;   
array_item=  s    r  zarray.itemsetc           
   	   C   s   |j \}}|\}}||jks tt|| ||}|j}|j|d||ddd d}	| j	|t
|	f W 5 Q R X t| ||||j |  S )Nr  r=   Fr  z/itemset(): can only write to an array of size 1)r   re   r   rc   r   r   r   r3   r  r  r   rr   r   get_dummy_value)
rg   r5   rK   r   r   r  r   rq   r   r   r:   r:   r;   array_itemsetL  s    
r  c                   @   s@   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 )Indexerzq
    Generic indexer interface, for generating indices over a fancy indexed
    array on a single dimension.
    c                 C   s   t dS )ze
        Prepare the indexer by initializing any required variables, basic
        blocks...
        Nr   rI   r:   r:   r;   prepareg  s    zIndexer.preparec                 C   s   t dS )z=
        Return this dimension's size as an integer.
        Nr   r   r:   r:   r;   get_sizen  s    zIndexer.get_sizec                 C   s   t dS )z;
        Return this dimension's shape as a tuple.
        Nr   r   r:   r:   r;   	get_shapet  s    zIndexer.get_shapec                 C   s   t dS )z}
        Return a half-open [lower, upper) range of indices this dimension
        is guaranteed not to step out of.
        Nr   r   r:   r:   r;   get_index_boundsz  s    zIndexer.get_index_boundsc                 C   s   t dS )aW  
        Start indexation loop.  Return a (index, count) tuple.
        *index* is an integer LLVM value representing the index over this
        dimension.
        *count* is either an integer LLVM value representing the current
        iteration count, or None if this dimension should be omitted from
        the indexation result.
        Nr   r   r:   r:   r;   	loop_head  s    	zIndexer.loop_headc                 C   s   t dS )z)
        Finish indexation loop.
        Nr   r   r:   r:   r;   	loop_tail  s    zIndexer.loop_tailN)
r\   r]   r^   __doc__r!  r"  r#  r$  r%  r&  r:   r:   r:   r;   r  a  s   r  c                   @   sH   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d Z
dS )EntireIndexerz:
    Compute indices along an entire array dimension.
    c                 C   s2   || _ || _|| _|| _|| _| j tj| _d S r  )	rg   r5   r   r   dimr   r   ru   ll_intp)rI   rg   r5   r   r   r)  r:   r:   r;   __init__  s    zEntireIndexer.__init__c                 C   sB   | j }|| jj| j| _t|| j| _	|
 | _|
 | _d S r  )r5   r  r   rQ   r)  ry   r	   rG   r*  r   append_basic_blockbb_startbb_endrI   r5   r:   r:   r;   r!    s
    
zEntireIndexer.preparec                 C   s   | j S r  ry   r   r:   r:   r;   r"    s    zEntireIndexer.get_sizec                 C   s   | j fS r  r0  r   r:   r:   r;   r#    s    zEntireIndexer.get_shapec                 C   s   |  d| jfS r   r*  ry   r   r:   r:   r;   r$    s    zEntireIndexer.get_index_boundsc              	   C   s|   | j }| j t| jd| j || j || j || j}|j	|
d|| jdd || j W 5 Q R X ||fS Nr   >=Fr  )r5   r   r   r*  r   branchr-  position_at_endr6   r   r   ry   r.  )rI   r5   	cur_indexr:   r:   r;   r%    s    zEntireIndexer.loop_headc                 C   sD   | j }t||| j}||| j || j || j	 d S r  )
r5   r	   r   r6   r   r   r4  r-  r5  r.  rI   r5   
next_indexr:   r:   r;   r&    s
    zEntireIndexer.loop_tailNr\   r]   r^   r'  r+  r!  r"  r#  r$  r%  r&  r:   r:   r:   r;   r(    s   r(  c                   @   sH   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d Z
dS )IntegerIndexerz0
    Compute indices from a single integer.
    c                 C   s&   || _ || _|| _| j tj| _d S r  )rg   r5   rx   r   r   ru   r*  )rI   rg   r5   rx   r:   r:   r;   r+    s    zIntegerIndexer.__init__c                 C   s   d S r  r:   r   r:   r:   r;   r!    s    zIntegerIndexer.preparec                 C   s   t | jdS Nr=   )r   r*  r   r:   r:   r;   r"    s    zIntegerIndexer.get_sizec                 C   s   dS Nr:   r:   r   r:   r:   r;   r#    s    zIntegerIndexer.get_shapec                 C   s   | j | j| j |  fS r  )rx   r5   addr"  r   r:   r:   r;   r$    s    zIntegerIndexer.get_index_boundsc                 C   s
   | j d fS r  )rx   r   r:   r:   r;   r%    s    zIntegerIndexer.loop_headc                 C   s   d S r  r:   r   r:   r:   r;   r&    s    zIntegerIndexer.loop_tailNr9  r:   r:   r:   r;   r:    s   r:  c                   @   sH   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d Z
dS )IntegerArrayIndexerz;
    Compute indices from an array of integer indices.
    c                 C   s@   || _ || _|| _|| _|| _|jdks,t| j tj	| _
d S r;  )rg   r5   rw   r   ry   r[   r   r   r   ru   r*  )rI   rg   r5   rw   r   ry   r:   r:   r;   r+    s    zIntegerArrayIndexer.__init__c                 C   sD   | j }t|| jjd | _t|| j| _|	 | _
|	 | _d S r   )r5   r	   r   r   rQ   idx_sizerG   r*  	idx_indexr,  r-  r.  r/  r:   r:   r;   r!    s
    
zIntegerArrayIndexer.preparec                 C   s   | j S r  r?  r   r:   r:   r;   r"    s    zIntegerArrayIndexer.get_sizec                 C   s   | j fS r  rA  r   r:   r:   r;   r#    s    zIntegerArrayIndexer.get_shapec                 C   s   |  d| jfS r   r1  r   r:   r:   r;   r$    s    z$IntegerArrayIndexer.get_index_boundsc              	   C   s   | j }| j t| jd| j || j || j || j}|j	|
d|| jdd || j W 5 Q R X t| j|| jj| j| j|}t| j|| jj|| j}||fS r2  )r5   r   r   r*  r@  r4  r-  r5  r6   r   r   r?  r.  r   rg   rw   re   r   r{   ry   )rI   r5   r6  r   r:   r:   r;   r%    s0          zIntegerArrayIndexer.loop_headc                 C   sD   | j }t||| j}||| j || j || j	 d S r  )
r5   r	   r   r6   r@  r   r4  r-  r5  r.  r7  r:   r:   r;   r&    s    
zIntegerArrayIndexer.loop_tailNr9  r:   r:   r:   r;   r>    s   	r>  c                   @   sH   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d Z
dS )BooleanArrayIndexerz>
    Compute indices from an array of boolean predicates.
    c                 C   sH   || _ || _|| _|| _|jdks&t| j tj| _	t
| j	d| _d S Nr=   r   )rg   r5   rw   r   r[   r   r   r   ru   r*  r   r   )rI   rg   r5   rw   r   r:   r:   r;   r+  &  s    zBooleanArrayIndexer.__init__c                 C   s^   | j }t|| jjd | _t|| j| _t|| j| _	|
 | _|
 | _|
 | _d S r   )r5   r	   r   r   rQ   ry   rG   r*  r@  r   r,  r-  bb_tailr.  r/  r:   r:   r;   r!  /  s    

zBooleanArrayIndexer.preparec              	   C   s   | j }t|| j}t|| jP}||}t| j|| j	j
| j	| j|j}|||||j}||| W 5 Q R X ||S r  )r5   r	   r   r   	for_rangery   r6   r   rg   rw   re   r   r   r=  Zzextr3   r   )rI   r5   r   loopcpredr:   r:   r;   r"  8  s    
    zBooleanArrayIndexer.get_sizec                 C   s
   |   fS r  r"  r   r:   r:   r;   r#  G  s    zBooleanArrayIndexer.get_shapec                 C   s   |  d| jfS r   r1  r   r:   r:   r;   r$  J  s    z$BooleanArrayIndexer.get_index_boundsc              	   C   s   | j }| j | j| j | j | j| j || j || j || j}|| j}|j	|
d|| jdd || j W 5 Q R X t| j|| jj| j| j|}|	|| || j W 5 Q R X t||}||| j ||fS )Nr3  Fr  )r5   r   r   r@  r   r4  r-  r5  r6   r   r   ry   r.  r   rg   rw   re   r   not_rD  r	   r   )rI   r5   r6  	cur_countrH  
next_countr:   r:   r;   r%  O  s0        zBooleanArrayIndexer.loop_headc                 C   s\   | j }|| j || j t||| j}||| j || j	 || j
 d S r  )r5   r4  rD  r5  r	   r   r6   r@  r   r-  r.  r7  r:   r:   r;   r&  g  s    
zBooleanArrayIndexer.loop_tailNr9  r:   r:   r:   r;   rB  !  s   		rB  c                   @   sH   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d Z
dS )SliceIndexerz(
    Compute indices along a slice.
    c                 C   sL   || _ || _|| _|| _|| _|| _|| _| j tj	| _
t| j
d| _d S r   )rg   r5   r   r   r)  rw   r   r   r   ru   r*  r   r   )rI   rg   r5   r   r   r)  rw   r   r:   r:   r;   r+  w  s    zSliceIndexer.__init__c                 C   s   | j }|| jj| j| _t| j|| j	| j
 t|| j
| j t|| j
j| _t|| j| _t|| j| _| | _| | _d S r  )r5   r  r   rQ   r)  dim_sizer)   r   rg   rw   r   r   r	   Z
is_neg_intstepis_step_negativerG   r*  r   r   r,  r-  r.  r/  r:   r:   r;   r!    s    
zSliceIndexer.preparec                 C   s   t | j| jS r  )r)   r   r5   r   r   r:   r:   r;   r"    s    zSliceIndexer.get_sizec                 C   s
   |   fS r  rI  r   r:   r:   r;   r#    s    zSliceIndexer.get_shapec                 C   s   t | j| j\}}||fS r  )r)   Zget_slice_boundsr5   r   )rI   lowerupperr:   r:   r;   r$    s    zSliceIndexer.get_index_boundsc              	   C   s   | j }| j | jj| j | j | j| j || j |	| j |
| j}|
| j}|| j|d|| jj|d|| jj}|j|dd || j W 5 Q R X ||fS )Nz<=r3  Fr  )r5   r   r   r   r   r   r   r4  r-  r5  r6   selectrP  r   stopr   r.  )rI   r5   r6  rK  is_finishedr:   r:   r;   r%    s$    zSliceIndexer.loop_headc                 C   sp   | j }|j|| j| jjdgd}||| j t||| j	}||| j	 |
| j || j d S )Nr   r   )r5   r=  r6   r   r   rO  r   r	   r   r   r4  r-  r5  r.  )rI   r5   r8  rL  r:   r:   r;   r&    s    zSliceIndexer.loop_tailNr9  r:   r:   r:   r;   rM  r  s   rM  c                   @   s@   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 )FancyIndexerz4
    Perform fancy indexing on the given array.
    c              	   C   s  || _ || _|| _t||j|j| _t||j|j| _| j 	t
j| _g | _g }tdd |D }d}	d}
t||D ]\}}|t
jkr|jt| d | }t|D ].}t|||||	}|| |	d7 }	|
d7 }
qqxt|t
jr||||}t|||||	||}|| nt|t
jrTt||||| j|	 }t|||}|| nt|t
jrt||||}t|jt
jrt||||| j|	 }n*t|jt
jrt ||||}n
dst!|| n.t"|r| j|
 |	d8 }	nt!d|f |	d7 }	|
d7 }
qx|	|jks*t!|	|jf|	|jk r\t|||||	}|| |	d7 }	q*t||jks~t!t||jf|| _#d S )Nc                 S   s   g | ]}t |r|qS r:   r   r   r:   r:   r;   r     s      z)FancyIndexer.__init__.<locals>.<listcomp>r   r=   r   )$rg   r5   r   r	   r   rQ   r[   r   r   r   r   ru   r*  newaxesr   r   r   r2   r(  rU   r|   r   r   rM  r~   r{   r:  r}   rc   re   r>  BooleanrB  r   r   indexers)rI   rg   r5   r   r   r   r   rY  r   r   Znew_axr   rw   r   rY   indexerr   rz   r   r:   r:   r;   r+    sz    


 
  




"zFancyIndexer.__init__c                 C   s^   | j D ]}|  q| jtjd}dd | j D }| jD ]}|||f q:t|d| _	d S )Nr=   c                 S   s   g | ]}|  qS r:   )r#  r   rY   r:   r:   r;   r     s     z(FancyIndexer.prepare.<locals>.<listcomp>r:   )
rY  r!  rg   r   r   ru   rW  insertsumindexers_shape)rI   rY   r   Z	res_shaper:   r:   r;   r!    s    


zFancyIndexer.preparec                 C   s   | j S )z?
        Get the resulting data shape as Python tuple.
        )r^  r   r:   r:   r;   r#    s    zFancyIndexer.get_shapec              	   C   s  t || jjkst| j}tj}| d}| d}|}|}t| j	| j
|D ]\}	}
}|||d|
|}|	 \}}|||}|||||}|d||}||||||}||||||}qL|||}||||}||||}||fS )z
        Get a half-open [lower, upper) range of byte offsets spanned by
        the indexer with the given strides and itemsize.  The indexer is
        guaranteed to not go past those bounds.
        r   r=   ==r   )r   r   r[   r   r5   r	   	false_bitr*  r   rY  r^  or_icmp_unsignedr$  r   subr   r=  rS  )rI   r   r   r5   is_emptyr   r   rQ  rR  rZ  rQ   strideZlower_indexZupper_indexZlower_offsetZupper_offsetZis_downwardsr:   r:   r;   get_offset_bounds  sB    


zFancyIndexer.get_offset_boundsc                 C   s    t dd | jD  \}}||fS )Nc                 s   s   | ]}|  V  qd S r  )r%  r[  r:   r:   r;   r  >  s     z+FancyIndexer.begin_loops.<locals>.<genexpr>)r   rY  )rI   r   countsr:   r:   r;   begin_loops=  s    zFancyIndexer.begin_loopsc                 C   s   t | jD ]}|  q
d S r  )reversedrY  r&  )rI   rY   r:   r:   r;   	end_loopsA  s    zFancyIndexer.end_loopsN)
r\   r]   r^   r'  r+  r!  r#  rf  rh  rj  r:   r:   r:   r;   rV    s   B&rV  c                 C   s  t ||j}t ||j}	|j}
t| |||||}|  |j}| }t	| |||}|j}t 
|| tjd}| \}}t j| ||
||	|j|d| jd	}t| |||}||}|||g}t| |||| t ||}||| |  t| ||| S )Nr   Fr   )r	   r   rQ   r   r   rV  r!  r   r#  _empty_nd_implr   r   r   ru   rh  get_item_pointer2layoutr  ro   r6   geprr   r   r   rj  r   r   )rg   r5   rK   r   r   r   r   r   r   r   r   rZ  Zout_tyZ
out_shapesr   Zout_dataZout_idx_rW   rq   curZnext_idxr:   r:   r;   r  F  s<    
   
r  c           	   	   C   s   |j \}}|\}}t|| ||}|jdkrht| |||\}}t| ||j|||f|f}t| ||j|S t| ||||||f|fS dS )z3
    Advanced or basic indexing with an array.
    r   N)r   rc   r[   r   r  r   r   r  r  r:   r:   r;   fancy_getitem_arrayq  s$    


   
   rq  c              	   C   s   |j }|d}|d}|jdkr8|}	|||j}
n|}	|}
t|jD ]Z}||| ||| |}|d||}|	||
|
||
}
|	||	|
|	|}	qJ|
|
|}
|d|j|}|	|||
}
|	|||	}	|	|
fS )z
    Compute a half-open range [lower, upper) of byte offsets from the
    array's data pointer, that bound the in-memory extent of the array.

    This mimics offset_bounds_from_strides() from
    numpy/core/src/private/mem_overlap.c
    r   r=   CFr3  r_  )r   r3   rm  r   r   r2   r[   rc  r   rS  r=  )rg   r5   arrtyarrr   r   r   r   r   rQ  rR  rY   Zmax_axis_offsetZ
is_upwardsrd  r:   r:   r;   offset_bounds_from_strides  s4    




  
ru  c                 C   s.   | ||j}|||}|||}||fS )z
    Given [lower, upper) byte offsets and a base data pointer,
    compute the memory pointer bounds as pointer-sized integers.
    )ptrtointr3   r=  )rg   r5   rQ  rR  r   Zdata_ptr_as_intr   endr:   r:   r;   compute_memory_extents  s    rx  c           	      C   s&   t | |||||\}}t| ||||S )zr
    Compute a half-open range [start, end) of pointer-sized integers
    which fully contain the array data.
    )ru  rx  )	rg   r5   rs  rt  r   r   r   rQ  rR  r:   r:   r;   get_array_memory_extents  s
    
 ry  c                 C   s$   | |d|||d||}|S )z[
    Whether two memory extents [a_start, a_end) and [b_start, b_end)
    may overlap.
    r   )and_rb  )rg   r5   Za_startZa_endZb_startZb_endZmay_overlapr:   r:   r;   extents_may_overlap  s
    r{  c                    s  j dt d  jdd  |j|j}j |}	|	j  	|	}	 
|	 tj}
t |
F}t 	
j|}t |	|} 
 || W 5 Q R X W 5 Q R X  	
fdd} fdd}||fS )Nr   Fr  c                    s   t  } jddv\}}|.  t j  | dd| W 5 Q R X |*  t j 	
j| dd| W 5 Q R X W 5 Q R X t 
 |S )NFr  r   )r	   rG   if_elser   rl  r6   rm  ro   )source_indicessrc_ptrZif_copyZ	otherwise)r5   rg   	copy_datacopy_layoutcopy_shapescopy_stridesptrtysrc_data
src_shapessrc_stridessrctyuse_copyr:   r;   src_getitem  s4      
  z&maybe_copy_source.<locals>.src_getitemc               	      sB    j dd*  }  | } j |  W 5 Q R X d S )NFr  )r   r6   bitcastr   freer   )r5   rg   r  r  	voidptrtyr:   r;   src_cleanup  s    
z&maybe_copy_source.<locals>.src_cleanup)r3   r	   r   r   r   r   r   r   allocater  r   r   r   ru   	loop_nestrl  rm  r6   )rg   r5   r  r  srcr  r  r  	allocsizer   r   r   r  dest_ptrr  r  r:   )r5   rg   r  r  r  r  r  r  r  r  r  r  r  r;   maybe_copy_source  s:    
  
  &"r  c           
   	      s   |  tjd}|  tjdt|t|krZt|t| }g| | }|g| | }nt|t|k rt|t| } fdd|d| D }t j|tj} j	 
|dd d}	| j t|	f W 5 Q R X ||d }||d }||fS )	ae  
    Preprocess dimension for broadcasting.
    Returns (shapes, strides) such that the ndim match *target_shape*.
    When expanding to higher ndim, the returning shapes and strides are
    prepended with ones and zeros, respectively.
    When truncating to lower ndim, the shapes are checked (in runtime).
    All extra dimension must have size of 1.
    r   r=   c                    s   g | ]}  d |qS r_  )rb  r   r   r5   r   r:   r;   r   %  s   z(_bc_adjust_dimension.<locals>.<listcomp>NFr  z,cannot broadcast source array for assignment)r   r   rv   r   	functoolsreducerz  r	   true_bitr   rJ  r  r  r   )
rg   r5   r   r   target_shaper   Znd_diffZ
dim_is_oneacceptedr   r:   r  r;   _bc_adjust_dimension  s(    	

r  c           
         s   g }g }|  tjd|  tjd fddt||D } fdd|D } fddt||D }	 fddt|	||D } fddt|	|D }||fS )	z
    Broadcast shapes and strides to target_shape given that their ndim already
    matches.  For each location where the shape is 1 and does not match the
    dim for target, it is set to the value at the target and the stride is
    set to zero.
    r   r=   c                    s   g | ]\}}  d ||qS )r  r   )r   taroldr5   r:   r;   r   @  s   z,_bc_adjust_shape_strides.<locals>.<listcomp>c                    s   g | ]}  d |qS r  r  )r   r  r  r:   r;   r   B  s     c                    s   g | ]\}}  ||qS r:   )rz  )r   xyr  r:   r;   r   C  s     c                    s    g | ]\}}}  |||qS r:   rS  )r   pr  r  r  r:   r;   r   D  s   c                    s   g | ]\}}  ||qS r:   r  )r   r  r  )r5   r   r:   r;   r   F  s   )r   r   rv   r   )
rg   r5   r   r   r  Z	bc_shapesZ
bc_stridesmismatchZ
src_is_onepredsr:   )r5   r   r   r;   _bc_adjust_shape_strides4  s     


r  c           	   	   C   s   t ||j}t ||j}t| ||||\}}t| ||||\}}|jt|dd}t|| |}t	||j
t ||t |||j|j|jd ||fS )zX
    Broadcast the given array to the target_shape.
    Returns (array_type, array)
    Ar[   rm  r  )r	   r   rQ   r   r  r  copyr   rc   r   r   rV   r   r   r   )	rg   r5   arrtypert  r  r   r   Znew_arrtypeZnew_arrr:   r:   r;   _broadcast_to_shapeK  s(    
 

r  c                 C   s,   |j |jddd}|||}dd }||fS )Nr  T)r[   rm  readonlyc           
   	   S   sz   |\}}|j d }t|| ||}t||}t| ||||\}}t|dt| |j	dd  |
 }	t| ||j|	S )Nr   r   )r   rc   r	   r   r  r   r   r   r   r   r   r   r   )
rg   r5   rK   r   r  Zshape_r  ro  destr   r:   r:   r;   codegenj  s    
z$_numpy_broadcast_to.<locals>.codegen)r  r   )	typingctxr   rQ   retrK   r  r:   r:   r;   _numpy_broadcast_toe  s    
r  c                 C   s$   |j dd}||}dd }||fS )NTr  c                 S   sH   |\}|j d }t|| ||}t|jj|_| }t| ||j|S r   )	r   rc   r	   Zget_null_valuer   r3   r   r   r   )rg   r5   rK   r   r  r  r  r   r:   r:   r;   r    s    
z#get_readonly_array.<locals>.codegen)r  )r  rt  r  rK   r  r:   r:   r;   get_readonly_array|  s    	r  c           
      C   s   | j }t|}t|}||kr&td|D ]}|dk r*tdq*d}|| }||k r|| }|| }	||	kst|dkr|d7 }|d7 }qLtdqLd S )NzDinput operand has more dimensions than allowed by the axis remappingr   z4all elements of broadcast shape must be non-negativer=   z=operands could not be broadcast together with remapped shapes)rQ   r   r   )
r   Z
dest_shapeZ	src_shapeZsrc_ndimZ	dest_ndimry   Z	src_indexZ
dest_indexZsrc_dimZdest_dimr:   r:   r;   _can_broadcast  s"    

r  c                 C   s   t | } t| | t| |S r  )npasarrayr  r  r   rQ   r:   r:   r;   _default_broadcast_to_impl  s    

r  c                 C   s  t | stdt|tjr*dd }|S t|tjrVt|jtjsRd}t|tS t|tj	r|j
dkrtdd |D sd| d	}t|tS t|tj	r|j
dkrt| tjo| jdk}t| s|rd
d }|S d}t|nd| }t|d S )Nz-The first argument "array" must be array-likec                 S   s   t | |fS r  )r  broadcast_tor  r:   r:   r;   r     s    z numpy_broadcast_to.<locals>.implz7The second argument "shape" must be a tuple of integersr   c                 S   s   g | ]}t |tjqS r:   )r|   r   IntegerLiteral)r   typr:   r:   r;   r     s     z&numpy_broadcast_to.<locals>.<listcomp>"z," object cannot be interpreted as an integerc                 S   s   t | } t| S r  )r  r  r  r  r:   r:   r;   r     s    
z/Cannot broadcast a non-scalar to a scalar arrayz:The argument "shape" must be a tuple or an integer. Got %s)r   r   r   r|   r   r~   UniTuplere   r  Tupler   allr}   r[   r   )r   rQ   r   r   Zis_scalar_arrayr:   r:   r;   numpy_broadcast_to  s4    


r  c                 C   sr   t t|D ]`}|t| | }|| }|dk r8td|dkrBq| | dkrX|| |< q| | |krtdqd S )Nr   #negative dimensions are not allowedr=   z=shape mismatch: objects cannot be broadcast to a single shape)r2   r   r   )rmrQ   rY   r   tmpr:   r:   r;   numpy_broadcast_shapes_list  s    
r  c                     s   t | D ]n\}}t|tj}t|tjo4t|jtj}t|tjoNt|jdk}|s|s|sd| d| }t	|qd| D ]6}t|tjrt
dqt|tjrt
t|qdkrdd S d ddlm   fd	d
}|S d S )Nr   z	Argument z* must be either an int or tuple[int]. Got r=   c                  W   s   dS r<  r:   r   r:   r:   r;   <lambda>      z+ol_numpy_broadcast_shapes.<locals>.<lambda>r=   )_isinstance_no_warnc                     sz   dg }}t | D ]@} |tr>t|dkr>t|| q |trt||f qt|D ]\}}t|||}q`|S rC  )r   r   r   r  r   	enumerater*   )r   r  r  argrx   elemr  r  tup_initr:   r;   r     s    

z'ol_numpy_broadcast_shapes.<locals>.impl)r  r|   r   r~   r  re   r  r   r   r   maxr  Znumba.cpython.builtinsr  )r   rx   r  Zis_intZis_int_tupleZis_empty_tupler   r   r:   r  r;   ol_numpy_broadcast_shapes  s*    r  c                     s   t | D ]"\}}t|std| dqd }d }| D ]H}t|tjtjfrV|j}n|}|d krh|}q8||kr8td|  q8d t | D ]T\}}t|tj	rt
 |j qt|tjtjtjfrt
 d qtd| qd   fdd	}|S )
Nz
Argument "z" must be array-likezTMismatch of argument types. Numba cannot broadcast arrays with different types. Got r   r=   zUnhandled type )r   c                     s|   dg  }t | D ]}t| t|j q}t D ]}t|||| }q8g }t | D ]}|tt|| qZ|S r;  )	r   r  r  r  rQ   r2   r*   rU   r  )r   rQ   r   r  rY   Zoutsr  r  r:   r;   r   >  s    
z$numpy_broadcast_arrays.<locals>.impl)r  r   r   r   r|   r   r}   r  re   ZArrayCompatibler  r[   NumberrX  )r   rx   r  Zunified_dtypedtr   r:   r  r;   numpy_broadcast_arrays  s.    r  c           '         s  |j \}}|\}}t| |}t |j}	t |j}
|j}t ||||}|  t	t
jrj| }t t |\t j}t j}j}tj}t|t|kstt||D ]\}} | d||}q j|dd d}j t|f W 5 Q R X t |||\}}||
|j\}}t |||\}}t ||||}t ||||\}}nt	t
jrRj| }t|dkst tt!t
j"}| f} d|d |} j|dd d}j t|f W 5 Q R X  fdd}d	d
 }nfdd}dd
 }#t
j$d} |% \}!}"t&|"}"|j'D ]}#|"(|#|  qdd |"D }$||$}%) |%|j}%tj* ||	|
|j+|!dj,d	}&t- ||%|& |.  |  / S )z
    Implement slice assignment for arrays.  This implementation works for
    basic as well as fancy indexing, since there's no functional difference
    between the two for indexed assignment.
    r  Fr  z0cannot assign slice from input of different sizer=   r   c                    s,   | \} tjttj}| |fS r  )rB   operatorgetitemr    r   ru   )r~  rx   getitem_implr5   rg   r  Z	src_dtyper  r:   r;   r    s    z#fancy_setslice.<locals>.src_getitemc                   S   s   d S r  r:   r:   r:   r:   r;   r    s    z#fancy_setslice.<locals>.src_cleanupc                    s    S r  r:   )r~  )r  r:   r;   r    s    c                   S   s   d S r  r:   r:   r:   r:   r;   r    s    c                 S   s   g | ]}|d k	r|qS r  r:   )r   rG  r:   r:   r;   r     s      z"fancy_setslice.<locals>.<listcomp>r   )0r   rc   r	   r   rQ   r   r   rV  r!  r|   r   r   re   r#  r  r`  r   r   r   ra  r   r   r  r  r   ry  rf  r   rx  r{  r  SequencerB   r    ru   r   rv   rh  r   rW  r\  rt   rl  rm  r  rr   rj  r  )'rg   r5   rK   r   r   r   r   ro  r   Zdest_shapesdest_strides	dest_datarZ  Zindex_shaper  r  r  Zshape_errorur   r   Z	src_startZsrc_endZ
dest_lowerZ
dest_upperZ
dest_startZdest_endr  r  r  len_implZseq_lenr   Zdest_indicesrg  rY   r~  rq   r  r:   r  r;   r  U  s    

 
   
    



  r  c                    s   |j d }|j dd  }|d }|dd  } fddt||D }t ||d j}tjtjt|d}	t	
|j||	}
||f}|
|fS )Nr   r=   c                    s"   g | ]\}}  ||tjqS r:   rt   r   ru   )r   r
  rq   r   r:   r;   r     s   z#vararg_to_tuple.<locals>.<listcomp>re   r   )r   r   r	   rV   r3   r   r  ru   r   r   r    r   )rg   r5   rK   r   r   Zdimtysr   rX   rQ   shapetynew_signew_argsr:   r   r;   vararg_to_tuple  s    
r  zarray.transposec                 C   s   t | ||jd |d S r   )array_Tr   rg   r5   rK   r   r:   r:   r;   array_transpose  s    r  c                 C   sn   t | t t| krtdt |}| D ] }||ks@t||kr(tdq(||  |d d < ||  |d d < d S )Nzrepeated axis in transposez2axis is out of bounds for array of given dimension)r   r   r   abs)r   rQ   r   r)  r  r:   r:   r;   permute_arrays  s    
r  c              
      s  |j d }t| |d }|j d |d  }}|j|j }}	tj}
t|
|||j	|j
g} fdd|D }t||D ]\}} || qtj|	dddtj|
} fdd|D }t||D ]6\}}t| ||
 tj|g|g|d d qވ tttjd	d |D  t|j }t||j |d  |d
 |j|j|jd | }t |j|S )Nr   r=   c                    s   g | ]}t  qS r:   r	   rG   r   ro  )r5   ll_ary_sizer:   r;   r     s     z)array_transpose_tuple.<locals>.<listcomp>r   re   r[   rm  c                    s   g | ]}t  qS r:   rc   r  )r5   rg   	np_ary_tyr:   r;   r      s     r   rQ   r   r   r   c                 S   s   g | ]}|  qS r:   r   r   ar:   r:   r;   r   0  s        r  ) r   rc   r   re   r   r   ru   r   	ArrayTyperQ   r   r   r   r}   r   rf   r   r  
as_pointercompile_internalr  r   r    voidr   r   r6   r   r   r   r   r   )rg   r5   rK   r   r   r   Zaxistyr   Znum_axisre   r*  ZarysZll_arysr  dstZnp_itemsizeZnp_arysZnp_aryZll_aryr  r   r:   )r5   rg   r  r  r;   array_transpose_tuple  sT    
	  r  c                 C   s    t | |||\}}t| |||S r  )r  r  rg   r5   rK   r   r  r  r:   r:   r;   array_transpose_vararg?  s    r  c                 C   s8   t | tjrtd|d kr*ddd}n
ddd}|S )Nz#np.transpose does not accept tuplesc                 S   s   |   S r  	transposer  axesr:   r:   r;   np_transpose_implK  s    z*numpy_transpose.<locals>.np_transpose_implc                 S   s
   |  |S r  r   r  r:   r:   r;   r  N  s    )N)N)r|   r   r  r   ZUnsupportedError)r  r  r  r:   r:   r;   numpy_transposeE  s    

r  Tc           	      C   s   |j dkr|}nt|| ||}t|| |}t||j|j }t||j|j }t||jt||d d d t||d d d |j	|j
|jd | }t| |||S )Nr=   r  )r[   rc   r	   r   rQ   r   r   r   rV   r   r   r   r   r   )	rg   r5   r  r   r   r   r  r   r   r:   r:   r;   r  T  s     
r  2   c                 C   sT   t | tjstdt |tjs,tdt |ttjfsFtdddd}|S )Nz+The first argument "start" must be a numberz+The second argument "stop" must be a numberz+The third argument "num" must be an integerr  c                 S   s   t | ||}t d|S )Ng      $@)r  linspacepower)r   rT  numr  r:   r:   r;   r   q  s    znumpy_logspace.<locals>.impl)r  )r|   r   r  r   r   r   r~   )r   rT  r  r   r:   r:   r;   numpy_logspaceh  s    



r  r=   c                 C   sR   t |ttjfstdt | tjs0td| jdk rDtdddd}|S )	Nz*The second argument "k" must be an integer)The first argument "arr" must be an arrayr  Input must be >= 2-d.r=   c                 S   st   |d }|dkr| d d  S |dkr8t t | ddS |dkrPt t | S |dkrlt t | ddS td S )N   r   r=   r     )r  swapaxesfliplrflipudr   )rt  r   r:   r:   r;   r     s    znumpy_rot90.<locals>.impl)r=   )	r|   r   r   r~   r   r   r}   r[   NumbaValueError)rt  r   r   r:   r:   r;   numpy_rot90x  s    




r  c                 C   s   |  tj}| }|  tj}	t|	||||||||	g}
t|j	|
d}||j
}t||ddd}t||ddd}||}t||dd}t||dd}|	d}|||||||||j|g}|S )a  
    Call into Numba_attempt_nocopy_reshape() for the given array type
    and instance, and the specified new shape.

    Return value is non-zero if successful, and the array pointed to
    by *newstrides* will be filled up with the computed results.
    Znumba_attempt_nocopy_reshaperQ   r   r   )r   r   ru   r  Zintcr   FunctionTyper	   Zget_or_insert_functionr4   r[   rT   rS   callr   )rg   r5   r   r   newndnewshape
newstridesr*  Zll_intp_starZll_intcfntyfnndrQ   r   Z
is_f_orderr   r:   r:   r;   _attempt_nocopy_reshape  sD    	     

    r  c           	      C   s   d}d}t |D ]&\}}|dk r.|d7 }|}q||9 }q|dkrR| |krtdnP|dkr|dkrpd}| dk}n| | }| | dk}|std|||< ntdd S )Nr   r=   z)total size of new array must be unchangedzmultiple negative shape values)r  r   )	origsizerQ   Znum_neg_valueZ
known_sizer   r   Zneg_axinferredokr:   r:   r;   normalize_reshape_value  s(    



r"  zarray.reshapec              	   C   s  |j d }|j}|j d }|d }| tj}t||j}	t|| ||d }
t	
||	}||| tj|jddd}t|| |}| tj| |}t|||| | tj|jg|g|d d |
j}| |tttjtj||| g |j}t	
||	}t| |||
|||}|d||d}|| d}| j |t!|f W 5 Q R X t|| |}t||
j"|#||#||
j$|
j%|
j&d | }t'| ||j|S )	Nr   r=   r   r  r  r_  zincompatible shape for arrayr  )(r   r   r   r   ru   r   r  r   rc   r	   rG   r   r}   re   r   rf   r   r  r  r   r  r"  r   r    r  rv   r   r  rb  r3   r   r  r  rC   r   r6   r   r   r   r   )rg   r5   rK   r   r   rettyr  rQ   r*  Zll_shaper   r  Zshape_ary_ty	shape_aryZshape_itemsizery   r  r  r!  failr   r  r   r:   r:   r;   array_reshape  sd    

 
 r&  c                 C   s    t | |||\}}t| |||S r  )r  r&  r  r:   r:   r;   array_reshape_vararg  s    r'  c                 C   s   dd }|S )Nc                 S   s
   |  |S r  )reshape)r  rQ   r:   r:   r;   np_reshape_impl  s    z#np_reshape.<locals>.np_reshape_implr:   )r  rQ   r)  r:   r:   r;   
np_reshape  s    r*  c                 C   s\   t | stdt |s$tdt|r8ddd}n t|tjsNtdddd}|S )	Nz+The first argument "arr" must be array-likez/The second argument "values" must be array-likec                 S   s.   t t | } t t |}t | |fS r  )r  ravelr  concatenatert  valuesr   r:   r:   r;   r   .  s    znp_append.<locals>.implz,The third argument "axis" must be an integerc                 S   s   t j| |f|dS Nr   r  r,  r-  r:   r:   r;   r   8  s    )N)N)r   r   r   r   r|   r   r~   )rt  r.  r   r   r:   r:   r;   	np_append#  s    



r2  zarray.ravelc                 C   sN   dd }dd }|j d jdkr&|}n|}| ||||}t| ||j|}|S )Nc                 S   s   |  | jS )zNo copy version)r(  ry   r   r:   r:   r;   
imp_nocopy@  s    zarray_ravel.<locals>.imp_nocopyc                 S   s   |   S )zCopy version)flattenr3  r:   r:   r;   imp_copyD  s    zarray_ravel.<locals>.imp_copyr   r   )r   rm  r  r   r   )rg   r5   rK   r   r4  r6  impr   r:   r:   r;   array_ravel=  s    r8  c                 C   s   dd }|  ||||S )Nc                 S   s   |   S r  )r+  r  r:   r:   r;   np_ravel_implV  s    znp_ravel.<locals>.np_ravel_impl)r  )rg   r5   rK   r   r:  r:   r:   r;   np_ravelT  s    r;  zarray.flattenc                 C   s,   dd }|  ||||}t| ||j|}|S )Nc                 S   s   |   | jS r  )r  r(  ry   r3  r:   r:   r;   r7  _  s    zarray_flatten.<locals>.imp)r  r   r   )rg   r5   rK   r   r7  r   r:   r:   r;   array_flatten\  s    r<  c                 C   sl   |d krt | n|}t | ||\}}}t |jD ]0}|| }	|| }
|| }tt|	|
|||< q6|S r  )r  
empty_likebroadcast_arraysndindexrQ   minr  )r  a_mina_maxr   r  a_ba_min_ba_max_br   val_aZ	val_a_minZ	val_a_maxr:   r:   r;   _np_clip_implg  s    rG  c                 C   sH   t | jD ]6}| | }|| }|r4t||||< qt||||< q|S r  )r  r?  rQ   r@  r  )r  bZuse_minr   r   rF  Zval_br:   r:   r;   _np_clip_impl_noneu  s    rI  c                 C   sv  t | stdt|tjs0t |s0tdt|tjsNt |sNtdt|tjspt|spd}t||d kpt|tj}|d kpt|tj}|r|rddd}|S t|tj}t|tj}	|r|	rddd}
|
S |r|	s| rdd	d
}|S ddd}|S nf|s<|	r<|r,ddd}|S ddd}|S n6|rPddd}|S |rdddd}|S ddd}|S d S ) N#The argument "a" must be array-likez6The argument "a_min" must be a number or an array-likez6The argument "a_max" must be a number or an array-likez5The argument "out" must be an array if it is providedc                 S   s   t dd S )Nz&array_clip: must set either max or minr   r  rA  rB  r   r:   r:   r;   
np_clip_nn  s    znp_clip.<locals>.np_clip_nnc                 S   sH   |d krt | n|}t | jD ] }| | }tt|||||< q"|S r  )r  r=  r?  rQ   r@  r  r  rA  rB  r   r  r   rF  r:   r:   r;   
np_clip_ss  s
    znp_clip.<locals>.np_clip_ssc                 S   sB   |d krt | n|}t | jD ]}| | }t||||< q"|S r  )r  r=  r?  rQ   r  rM  r:   r:   r;   
np_clip_sn  s
    znp_clip.<locals>.np_clip_snc                 S   s   t | |}t| |||S r  r  	full_likerG  )r  rA  rB  r   Z
a_min_fullr:   r:   r;   
np_clip_sa  s    znp_clip.<locals>.np_clip_sac                 S   sB   |d krt | n|}t | jD ]}| | }t||||< q"|S r  )r  r=  r?  rQ   r@  rM  r:   r:   r;   
np_clip_ns  s
    znp_clip.<locals>.np_clip_nsc                 S   s   t | |}t| |||S r  rP  )r  rA  rB  r   Z
a_max_fullr:   r:   r;   
np_clip_as  s    znp_clip.<locals>.np_clip_asc                 S   s4   |d krt | n|}t | |\}}t||d|S )NTr  r=  r>  rI  )r  rA  rB  r   r  rC  rE  r:   r:   r;   
np_clip_na  s    znp_clip.<locals>.np_clip_nac                 S   s4   |d krt | n|}t | |\}}t||d|S )NFrU  )r  rA  rB  r   r  rC  rD  r:   r:   r;   
np_clip_an  s    znp_clip.<locals>.np_clip_anc                 S   s   t | |||S r  )rG  rK  r:   r:   r;   
np_clip_aa  s    znp_clip.<locals>.np_clip_aa)N)N)N)N)N)N)N)N)N)	r   r   r   r|   r   NoneTyper}   r   r  )r  rA  rB  r   r   Za_min_is_noneZa_max_is_nonerL  Za_min_is_scalarZa_max_is_scalarrN  rO  rR  rS  rT  rV  rW  rX  r:   r:   r;   np_clip  sV    












rZ  clipc                 C   s   ddd}|S )Nc                 S   s   t | |||S r  )r  r[  rK  r:   r:   r;   r     s    zarray_clip.<locals>.impl)NNNr:   )r  rA  rB  r   r   r:   r:   r;   
array_clip  s    
r\  c              	      s  |j |j kst|j|jks tt|j}td tdtdtj fdd}| tjt| |}| tjt| |}| tj|j }	t	
||ddd}
t	
||ddd}tjtjd	dd
}| |}t	|| tj|j g}| | tj}| tj|}t	||g}|| |}t||
|||dd || |}t|||||dd | }| }|	||||| tj|g}ttjtj||tjtjtj}| ||||}t|| t| ||j|}|S )a  
    Attempt to fix up *ary* for switching from *oldty* to *newty*.

    See Numpy's array_descr_set()
    (np/core/src/multiarray/getset.c).
    Attempt to fix the array's shape and strides for a new dtype.
    False is returned on failure, True on success.
    r  r   Fc           	         s   | kr2|d |kr}n|d |kr2}||krN| ksJ| dkrNdS |kr`| d }nd}||k r|| dkr|dS || }||  |9  < |||< n:||kr|| | }|| dkrdS || ||< |||< n dS )Nr  r   Fr=   Tr:   )	r  rX   r   old_itemsizenew_itemsizerm  rY   newdimZ
bytelengthZ
any_layoutZc_layoutZf_layoutint8r:   r;   r7  	  s.    



z_change_dtype.<locals>.imprQ   r   r   r=   r  Nr  )r[   r   rm  ordr   rb  r   ru   ri   r	   rT   rS   r}   rc   rV   rf   rd   r   r   r    booleanr  r   r   r   )rg   r5   Zoldtynewtyr   Z
new_layoutr7  r^  r_  r  Z
shape_dataZstrides_dataZshape_strides_array_typearyclsZshape_constantZsizeof_intpZstrides_constantr$  Zstrides_aryrQ   r   r   rK   r   r:   ra  r;   _change_dtype	  s    	
0   



	
rg  c                 C   s   t | stddd }|S )Nz+The argument to np.shape must be array-likec                 S   s   t | jS r  )r  r  rQ   r9  r:   r:   r;   r   	  s    znp_shape.<locals>.implr   r   r   r  r   r:   r:   r;   np_shape	  s    
rj  c                 C   s   dd }|S )Nc                    sJ   t |   t d d } fddt dd  D }t || S )Nr=   c                    s    g | ]\}} | |kr|qS r:   r:   )r   rY   r  rH  r:   r;   r   	  s      z5np_unique.<locals>.np_unique_impl.<locals>.<listcomp>)r  sortr+  r   r  r   )r  headtailr:   rk  r;   np_unique_impl	  s    z!np_unique.<locals>.np_unique_implr:   )r  ro  r:   r:   r;   	np_unique	  s    rp  c                 C   sh   dd }t | tjtjtjtjtjfrdt |tjr6tS t |tjtjfrZt |j	tjrZ|S t
dd S )Nc           	      S   s   t j|t jd}|jd dkr,t| |d S t |dk rBtdt | }| }|jd }|j|jkrrtdt jt 	||j
d}d}t|D ](}|| |||||  < ||| 7 }q|S )Nre   r   r=   r  z(operands could not be broadcast together)r  r  Zint64rQ   np_repeat_impl_repeats_scalerr  r   r+  emptyr]  re   r2   )	r  repeatsZrepeats_arrayasaaraveln	to_returnposrY   r:   r:   r;   !np_repeat_impl_repeats_array_like	  s$    

z4np_repeat.<locals>.np_repeat_impl_repeats_array_likezIThe repeats argument must be an integer or an array-like of integer dtype)r|   r   r}   Listr  r  rX  r~   rr  re   r   r   )r  rt  rz  r:   r:   r;   	np_repeat	  s    r|  c                 C   s   |dk rt dt| }| }|jd }|dkrDtjd|jdS |dkrVt|S tj|| |jd}t|D ] }|| ||| |d | < qr|S d S )Nr   r  rq  r=   )	r   r  r  r+  rQ   rs  re   r  r2   )r  rt  ru  rv  rw  rx  rY   r:   r:   r;   rr  	  s    


rr  repeatc                 C   s   dd }|S )Nc                 S   s   t | |S r  )r  r}  )r  rt  r:   r:   r;   array_repeat_impl	  s    z'array_repeat.<locals>.array_repeat_implr:   )r  rt  r~  r:   r:   r;   array_repeat	  s    r  c                 C   s   t |}dd }||fS )z"Computes the itemsize of the dtypec                 S   s,   |  |jd j}|  |j}|| |S r   )rd   r   re   r   rf   )cgctxr5   rK   llargsrh   Zllintpr:   r:   r;   r  	  s    z%_intrin_get_itemsize.<locals>.codegen)r   ru   )tyctxre   rK   r  r:   r:   r;   _intrin_get_itemsize	  s    
r  c                 C   s   d S r  r:   )r  re   r:   r:   r;   _compatible_view	  s    r  Zgeneric)targetc                 C   s   dd }|S )zDDetermines if the array and dtype are compatible for forming a view.c                 S   s   t |}|| jkr| jdkr*d}t|n | jd }| j| dk}| jdk}| j| | jk}|rt|rt|rtd}t||| jk r|dks| j| dkrd}	t|	n(| j| | j }
|
| dkrd}t|d S )Nr   zOChanging the dtype of a 0d array is only supported if the itemsize is unchangedr=   zJTo change to a dtype of a different size, the last axis must be contiguouszZWhen changing to a smaller dtype, its size must be a divisor of the size of original dtypezuWhen changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.)r  r   r[   r   rQ   ry   r   )r  re   Z
dtype_sizeZmsg1r   p1p2Zp3Zmsg2Zmsg3r`  Zmsg4r:   r:   r;   r   	  s(    






z ol_compatible_view.<locals>.implr:   r  re   r   r:   r:   r;   ol_compatible_view	  s    #r  z
array.viewc              	   C   s,  |j d }|j}t|| ||d }t|| |}t|jj}t|D ]:}	t||	}
|	dkrt|jj	}|
|
||_qFt||	|
 qFtdkr| j}|t}|||j i }| ||}||| t| ||||}|d|t|j	d}|| d}| j|t|f W 5 Q R X | }t| ||j|S )Nr   r   )r=      r_  z"new type not compatible with array)r   r   rc   r   r   r   sortedgetattrr   r3   r  r   r   Ztyping_contextZresolve_value_typer  Zget_call_typerB   rg  rb  r   r   r  r  r   r   r   )rg   r5   rK   r   r   r#  r   r  fieldsr   rq   r  r  r  Z_compatible_view_sigr   r!  r%  r   r   r:   r:   r;   
array_view
  s0    



r  re   c                 C   s   |   }t| |||S r  )r  r   rg   r5   r  r   r   r:   r:   r;   array_dtypeD
  s    r  rQ   c                 C   s(   t |}|| ||}|j}t| |||S r  )rc   rQ   r   rg   r5   r  r   rn   r   r   r:   r:   r;   array_shapeJ
  s    r  r   c                 C   s(   t |}|| ||}|j}t| |||S r  )rc   r   r   r  r:   r:   r;   array_stridesS
  s    r  r[   c                 C   s   |  tj|j}t| |||S r  )r   r   ru   r[   r   r  r:   r:   r;   
array_ndim\
  s    r  ry   c                 C   s(   t |}|| ||}|j}t| |||S r  )rc   r   r   r  r:   r:   r;   
array_sizec
  s    r  r   c                 C   s(   t |}|| ||}|j}t| |||S r  )rc   r   r   r  r:   r:   r;   array_itemsizek
  s    r  nbytesc                 C   s2   t |}|| ||}||j|j}t| |||S )z"
    nbytes = size * itemsize
    )rc   r   r   r   r   r  r:   r:   r;   array_nbytest
  s    r  
contiguousc                 C   s   |  tj|j}t| |||S r  )r   r   rd  	is_contigr   r  r:   r:   r;   array_contiguous
  s    r  c_contiguousc                 C   s   |  tj|j}t| |||S r  )r   r   rd  Zis_c_contigr   r  r:   r:   r;   array_c_contiguous
  s    r  f_contiguousc                 C   s   |  tj|j}t| |||S r  )r   r   rd  Zis_f_contigr   r  r:   r:   r;   array_f_contiguous
  s    r  r  c                 C   s    |  tj|j }t| |||S r  )r   r   rd  mutabler   r  r:   r:   r;   array_readonly
  s    r  ctypesc           	      C   sP   t |}|| ||}t|}| ||}|j|_|j|_| }t| |||S r  )rc   r   ArrayCTypesr   r   r   r   r   )	rg   r5   r  r   rn   r   Zactctinfor   r:   r:   r;   array_ctypes
  s    
r  r   c                 C   s8   | j |||d}|j}||| tj}t| |||S Nr   )r   r   rv  r   r   ru   r   )rg   r5   r  r   r  r   r:   r:   r;   array_ctypes_data
  s    r  c                 C   s6   | j |||d}|j}||| |}t| |||S r  )r   r   r  r   r   )rg   r5   fromtytotyrq   r  r   r:   r:   r;   array_ctypes_to_pointer
  s    r  c           
      C   sr   t ||||d}ttj|j}|||j}ttj	||tj}|j
|j|tj|g}||| ||}	|	S )a)  Helper to invoke the contiguous checker function on an array

    Args
    ----
    checker :
        ``numba.numpy_supports.is_contiguous``, or
        ``numba.numpy_supports.is_fortran``.
    context : target context
    builder : llvm ir builder
    aryty : numba type
    ary : llvm value
    r   )rc   r   r  ru   r[   rf   r   re   r    Zbool_rQ   r   r   r  )
checkerrg   r5   r   r   Ztup_intpr   Z	check_sigZ
check_argsr  r:   r:   r;   _call_contiguous_check
  s    
r  r   c                 C   s>   |  |t|}||_| }| j||| t| |||S r  )r   r   
ArrayFlagsr   r   r   r   r   )rg   r5   r  r   flagsobjr   r:   r:   r;   array_flags
  s
    r  c                 C   sZ   |j jdkr2| j|||d}tt| ||j |j}n|j jdk}| tj|}t	| |||S )Nr   r   )
rO   rm  r   r  r   r   r   r   rd  r   )rg   r5   r  r   r  r   rq   r:   r:   r;   array_flags_c_contiguous
  s     r  c                 C   sr   |j jdkr2| j|||d}tt| ||j |j}n2|j j}|j jdkrN|dkn|dk}| tj	|}t
| |||S )Nr]  r   r=   rr  )rO   rm  r   r  r   r   r[   r   r   rd  r   )rg   r5   r  r   r  r   rm  rq   r:   r:   r;   array_flags_f_contiguous
  s     r  realc                 C   sL   |j tjkrt| |||ddS |j tjkr8t| |||S tdtj d S )Nr  attrzunsupported .real for {})	re   r   complex_domainarray_complex_attrnumber_domainr   rC   r   r3   )rg   r5   r  r   r:   r:   r;   array_real_part
  s
    r  imagc                 C   s   |j tjkrt| |||ddS |j tjkrt|jdd|}t| |||g\}}t| |||}t	
||j||j|jd t| ||j| S tdtj d S )Nr  r  Tr  r   zunsupported .imag for {})re   r   r  r  r  r    r  _parse_empty_like_argsrk  r	   memsetr   r   r   r   r   r   r   rC   r   r3   )rg   r5   r  r   rK   r  r   r   r:   r:   r;   array_imag_part  s    
r  c                 C   s   |dks|j tjkr"td|t|}|| ||}|j j}| | |}|j	
|}	| | }
||j|
}|dkr||tddg}|j|dd}t|| |}t||	d}t||| t| ||| S )	a  
    Given a complex array, it's memory layout is:

        R C R C R C
        ^   ^   ^

    (`R` indicates a float for the real part;
     `C` indicates a float for the imaginary part;
     the `^` indicates the start of each element)

    To get the real part, we can simply change the dtype and itemsize to that
    of the underlying float type.  The new layout is:

        R x R x R x
        ^   ^   ^

    (`x` indicates unused)

    A load operation will use the dtype to determine the number of bytes to
    load.

    To get the imaginary part, we shift the pointer by 1 float offset and
    change the dtype and itemsize.  The new layout is:

        x C x C x C
          ^   ^   ^
    )r  r  zcannot get attribute `{}`r      r=   r  re   rm  )r   r   )re   r   r  rC   r   rc   Zunderlying_floatrf   rd   r   r3   r   r  r  r   rn  r   IntTyper  r   r	   Zcopy_structr   r   )rg   r5   r  r   r  rn   r   ZfltyZsizeof_fltyr   Z
llfltptrtyr   Zresulttyr   replr:   r:   r;   r    s     r  conj	conjugatec                 C   s   dd }|S )Nc                 S   s
   t | S r  )r  r  rt  r:   r:   r;   r   Q  s    zarray_conj.<locals>.implr:   rt  r   r:   r:   r;   
array_conjN  s    r  c                 C   s   |   S r  )r  )rg   r5   ZdtypetyZdtypevalr:   r:   r;   
dtype_typeY  s    r  r3   kindZstatic_getitemc                 C   s@   |j }t|tjr.| |d}t| |||S d}t|dS )zThis handles the "static_getitem" when a Numba type is subscripted e.g:
    var = typed.List.empty_list(float64[::1, :])
    It only allows this on simple numerical types. Compound types, like
    records, are not supported.
    NzlUnreachable; the definition of __getitem__ on the numba.types.abstract.Type metaclass should prevent access.)r   r|   r   r}   r   r   r   ZLoweringError)rg   r5   rK   r   r#  r   r   r:   r:   r;   static_getitem_number_clazzf  s    r  c              	      s  t |}| ||}|j}t|tjs6td||f ||}||}	t|tjrr|j	|j|j
|j
 dd}
n|j	|dd}
t |
}| |} tj|	}tj||j||jjd}t|tjr.t||j|j
}| fdd|jD 7 }t||j|j
}| fdd|jD 7 }  |j}n|j}|j}  |}t|||| tj||j|jd	 | }t ||
|S )
zq
    Generic getattr() implementation for record arrays: fetch the given
    record member, i.e. a subarray.
    zattribute %r of %s not definedr  r  r  )r   c                    s   g | ]}  tj|qS r:   r   r   ru   r[  rg   r:   r;   r     s     z(array_record_getattr.<locals>.<listcomp>c                    s   g | ]}  tj|qS r:   r  r[  r  r:   r;   r     s     r  )rc   re   r|   r   RecordrC   typeofr   NestedArrayr  r[   r   ru   r	   r   r   r3   r   rQ   r   rf   rd   r   r   r   r   r   )rg   r5   r  r   r  rn   r   Zrectypere   r   ZrestyZrarytyZraryZconstoffsetZ
newdataptrrQ   r   Zdatasizer   r:   r  r;   array_record_getattr~  sX    

 
 
   r  c                 C   s0   |d }t |tstt| ||jd |d |S rC  )r|   strrC   r  r   )rg   r5   rK   r   r   r:   r:   r;   array_record_getitem  s    
r  c              	      s    || ||}||}t|tjrt|}| |}|j}	 fdd|jD }
 fdd|j	D }t
||| |	}t||t
||
t
|| tj|jddd | }t |||S t
||| |}|jrdnd} ||||}t |||S dS )zT
    Generic getattr() implementation for records: get the given record member.
    c                    s   g | ]}  tj|qS r:   r  r   r   r  r:   r;   r     s     z"record_getattr.<locals>.<listcomp>c                    s   g | ]}  tj|qS r:   r  r  r  r:   r;   r     s     Nr  r=   )sentry_record_alignmentr   r  r|   r   r  rc   re   rQ   r   r	   get_record_memberrd   r   rV   r   ru   ry   r   r   rl   rm   )rg   r5   r  r   r  r   elemtyr   r   re   r  r  newdatar   dptrrk   r:   r  r;   record_getattr  sB    







	
r  c              	   C   s   |j \}}|\}}| || ||}	||}
t|
tjrt|| ||d d}|j	}t
|||	|jj}t|||| tj|
j nHt
|||	| |
}| ||||
}|jrdnd}| j||
|||d dS )zT
    Generic setattr() implementation for records: set the given record member.
    r=   r   Nrj   )r   r  r   r  r|   r   r  r	   rb   r   r  r3   ZpointeeZmemcpyr   ru   r   rd   rt   rl   rp   )rg   r5   rK   r   r  r  r  r  rq   r   r  Z
val_structr  r  r  rk   r:   r:   r;   record_setattr  s,    





r  c                 C   s4   |  |jd |d }|| ||jd |d |d S )3
    Record.__getitem__ redirects to getattr()
    r   r=   )get_getattrr   )rg   r5   rK   r   r   r:   r:   r;   record_static_getitem_str  s    r  c                 C   s^   |j d j}t|j d j}| |j|| }| |j d |}|| ||j d |d || S )r  r=   r   )r   r   r   r  Zinsert_const_stringr4   r  )rg   r5   rK   r   rx   r  Zll_fieldr   r:   r:   r;   record_static_getitem_int  s
    r  Zstatic_setitemc                 C   sJ   |j \}}}|\}}}	t|j||}
| ||
}|dk	s<t||||	fS )3
    Record.__setitem__ redirects to setattr()
    N)r   r    r   get_setattrr   )rg   r5   rK   r   rectyro  r  recrx   rq   getattr_sigr   r:   r:   r;   record_static_setitem_str  s    
r  c                 C   s^   |j \}}}|\}}}	t|j||}
t|j d j}| || |
}|dk	sPt||||	fS )r  r   N)r   r    r   r   r  r  r   )rg   r5   rK   r   r  ro  r  r  rx   rq   r  r  r   r:   r:   r;   record_static_setitem_int'  s    
r  c                 C   s   |  |||S )zB
    Create a constant array (mechanism is target-dependent).
    )make_constant_array)rg   r5   r
  pyvalr:   r:   r;   constant_array9  s    r  c                 C   s0   t t d|j}|t| }t||S )zG
    Create a record constant as a stack-allocated array of bytes.
       )r   r  r  r  	bytearraytostringr	   r   )rg   r5   r
  r  Zltyrq   r:   r:   r;   constant_recordA  s    r  c                 C   s"   t jt|t jd}| |||S )zM
    Create a constant array from bytes (mechanism is target-dependent).
    rq  )r  r   r  Zuint8r  )rg   r5   r
  r  bufr:   r:   r;   constant_bytesK  s    r  c                 C   s0   |j \}}||krtjS dd }| ||||S )Nc                 S   s(   | j |j ko&| j|jko&| jj|jjkS r  )rQ   r   r  r   r  rH  r:   r:   r;   array_is_impl]  s
    
zarray_is.<locals>.array_is_impl)r   r	   r`  r  )rg   r5   rK   r   atyZbtyr  r:   r:   r;   array_isW  s
    
r  __hash__c                 C   s   dd S )Nc                 S   s   d S r  r:   r  r:   r:   r;   r  j  r  zol_array_hash.<locals>.<lambda>r:   r  r:   r:   r;   ol_array_hashh  s    r  c                 C   s
   t | dS )zq
    Return the Structure representation of the given *flatiterty* (an
    instance of types.NumpyFlatType).
    flat_make_flattening_iter_cls)
flatitertyr:   r:   r;   make_array_flat_clsp  s    r  c                 C   s
   t | dS )zv
    Return the Structure representation of the given *nditerty* (an
    instance of types.NumpyNdEnumerateType).
    ndenumerater  nditertyr:   r:   r;   make_array_ndenumerate_clsx  s    r  c              
   C   s   |  tjd}|d}	|d k	r.|tj| tt|D ]}
t	|||
}t
|||}||
 }|d||}t||, ||| |d k	r||
 ||	 W 5 Q R X ||| |d k	r:||
 q:|d k	r|tj| ||	 ||	 d S )Nr   Zend_incrementr   )r   r   ru   r,  r   r	   
false_byteri  r2   rT   r   r6   r   	if_likelyr4  	true_byter5  )rg   r5   r[   rQ   r   end_flagloop_continue
loop_breakr   bbendr)  idxptrrx   r   	in_boundsr:   r:   r;   _increment_indices  s*    


r  c                 C   s*   t ||j|j}t| ||j||| d S r  )r	   r   rQ   r[   r  )rg   r5   rs  rt  r   r  rQ   r:   r:   r;   _increment_indices_array  s    r  c                    s   j jtjjr nd	G dd dt}G dd d| G fddd|G dd	 d	|G d
d d|G dd d|G  	f
dddt}|S )zq
    Return the Structure representation of the given *nditerty* (an
    instance of types.NumpyNdIterType).
    r=   c                   @   sF   e Zd ZdZdd Zdd Zejdd Zdd	 Z	d
d Z
dd ZdS )z$make_nditer_cls.<locals>.BaseSubIterzF
        Base class for sub-iterators of a nditer() instance.
        c                 S   s&   || _ || _|| _|| _|| | _d S r  )nditermember_name	start_dimend_dimr[   )rI   r  r  r	  r
  r:   r:   r;   r+    s
    z-make_nditer_cls.<locals>.BaseSubIter.__init__c                 S   s   t | j| j| d S r  )r   r  r  )rI   rW   r:   r:   r;   set_member_ptr  s    z3make_nditer_cls.<locals>.BaseSubIter.set_member_ptrc                 S   s   t | j| jS r  )r  r  r  r   r:   r:   r;   
member_ptr  s    z/make_nditer_cls.<locals>.BaseSubIter.member_ptrc                 S   s   d S r  r:   rI   rg   r5   r:   r:   r;   init_specific  s    z2make_nditer_cls.<locals>.BaseSubIter.init_specificc                 S   s   d S r  r:   rI   rg   r5   logical_dimr:   r:   r;   r     s    z2make_nditer_cls.<locals>.BaseSubIter.loop_continuec                 S   s   d S r  r:   r  r:   r:   r;   r    s    z/make_nditer_cls.<locals>.BaseSubIter.loop_breakN)r\   r]   r^   r'  r+  r  r  cached_propertyr  r  r   r  r:   r:   r:   r;   BaseSubIter  s   
r  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )z$make_nditer_cls.<locals>.FlatSubIterz
        Sub-iterator walking a contiguous array in physical order, with
        support for broadcasting (the index is reset on the outer dimension).
        c                 S   s$   | tjd}| t|| d S r   )r   r   ru   r  r	   r   )rI   rg   r5   r   r:   r:   r;   r    s    z2make_nditer_cls.<locals>.FlatSubIter.init_specificc                 S   s   | | j}||j|gS r  )r6   r  rn  r   )rI   rg   r5   r   rs  rt  r   r:   r:   r;   compute_pointer  s    z4make_nditer_cls.<locals>.FlatSubIter.compute_pointerc                 S   s8   || j d kr4|| j}t||}||| j d S r;  )r[   r6   r  r	   r   r   )rI   rg   r5   r  r   r:   r:   r;   r     s    z2make_nditer_cls.<locals>.FlatSubIter.loop_continuec                 S   s^   |dkr&| tjd}||| j n4|| jd krZ|| j}t||}||| j d S Nr   r=   )	r   r   ru   r   r  r[   r6   r	   r   )rI   rg   r5   r  r   r   r:   r:   r;   r    s    z/make_nditer_cls.<locals>.FlatSubIter.loop_breakN)r\   r]   r^   r'  r  r  r   r  r:   r:   r:   r;   FlatSubIter  s
   r  c                       s$   e Zd ZdZ fddZdd ZdS )z+make_nditer_cls.<locals>.TrivialFlatSubIterzx
        Sub-iterator walking a contiguous array in physical order,
        *without* support for broadcasting.
        c                    s    j r
td S r  )need_shaped_indexingr   r  r  r:   r;   r    s    z9make_nditer_cls.<locals>.TrivialFlatSubIter.init_specificc                 S   s&   t |dkstt |||j|S r;  )r   r   rn  r   rI   rg   r5   r   rs  rt  r:   r:   r;   r    s    z;make_nditer_cls.<locals>.TrivialFlatSubIter.compute_pointerN)r\   r]   r^   r'  r  r  r:   r  r:   r;   TrivialFlatSubIter  s   r  c                   @   s   e Zd ZdZdd ZdS )z'make_nditer_cls.<locals>.IndexedSubIterzA
        Sub-iterator walking an array in logical order.
        c                 S   s(   t || jksttj|||||ddS NFr|  )r   r[   r   r	   r   r  r:   r:   r;   r    s
     z7make_nditer_cls.<locals>.IndexedSubIter.compute_pointerNr\   r]   r^   r'  r  r:   r:   r:   r;   IndexedSubIter  s   r  c                   @   s   e Zd ZdZdd ZdS )z'make_nditer_cls.<locals>.ZeroDimSubIterz5
        Sub-iterator "walking" a 0-d array.
        c                 S   s   |j S r  r  r  r:   r:   r;   r  	  s    z7make_nditer_cls.<locals>.ZeroDimSubIter.compute_pointerNr  r:   r:   r:   r;   ZeroDimSubIter  s   r  c                   @   s   e Zd ZdZdd ZdS )z&make_nditer_cls.<locals>.ScalarSubIterz8
        Sub-iterator "walking" a scalar value.
        c                 S   s   |S r  r:   r  r:   r:   r;   r    s    z6make_nditer_cls.<locals>.ScalarSubIter.compute_pointerNr  r:   r:   r:   r;   ScalarSubIter  s   r  c                       s|   e Zd ZdZej fddZ	fddZfddZdd	 Z	d
d Z
	fddZdd Zdd ZdS )zmake_nditer_cls.<locals>.NdIterz
        .nditer() implementation.

        Note: 'F' layout means the shape is iterated in reverse logical order,
        so indices and shapes arrays have to be reversed as well.
        c                    sd   g }j r nd}tjD ]8\}}|\}}}}d| }	|| }
||
| |	|| q&|S )N)r  ZindexedZ0dZscalarzindex%d)r  r  rY  rU   )rI   lZ	factoriesrY   rc  r  r	  r
  ro  r  factory)r  r  r  r  r  r  r:   r;   subiters  s    
z(make_nditer_cls.<locals>.NdIter.subitersc                    s@  | tjd}||t||| _t|D ]8\}}t|tjs,d| }t	
||| }	t| ||	 q,| ||||}ttj}
d}d}t|D ]6\}}t|tjr|jkr|| j}|| j} qqdkst|||
d}| tjd}dd }t||D ]T\}}t|tjr|jdkrttjttj|j|
}|||||j|f qt	||} dkr|ddd	 }|d
||}||t	jt	j}js|f}t|kstt	j||jd}tD ] }t	 |||}|!|| q|| _"t	#|||j| _t	
||| _$| j%D ]}|&|| q(dS )zY
            Initialize the nditer() instance for the specific array inputs.
            r   scalar%dNr:   r=   c                 S   s>   t | }t|D ](}| | |t || |  krtdqd S )Nz2nditer(): operands could not be broadcast together)r   r2   r   )rQ   
main_shaperw  rY   r:   r:   r;   check_shapeO  s    zBmake_nditer_cls.<locals>.NdIter.init_specific.<locals>.check_shaper]  r  r_  r0  )'r   r   ru   
make_tupler  arraysr  r|   r}   r	   r   r   _arrays_or_scalarsr  r[   rQ   r   r   r   r    noner  r   r   rS  r  r  r  r   rG   r3   r2   rT   r   r   rV   	exhaustedr   r  )rI   rg   r5   arrtysr%  r   rY   r
  r  ZslotZmain_shape_tyr"  Zmain_nitemsrs  r#  rt  rK   r   Zshape_is_emptyr(  r   r)  r  subiter)rm  r[   r  nshapesr:   r;   r  ,  sf    

 


z-make_nditer_cls.<locals>.NdIter.init_specificc                    s*  | d}t||| j}t|| |d || W 5 Q R X  j}t	|| j}| 
||||}| j}|d | |||||}	dd |	D }	t|	dkr||	d  n||| j|	 t	|| j}
t||t|
|
|| jt| j||t| j|| || || dS )	zJ
            Compute next iteration of the nditer() instance.
            rw  FTc                 S   s   g | ]}|  qS r:   r  r   r   r:   r:   r;   r     s     zEmake_nditer_cls.<locals>.NdIter.iternext_specific.<locals>.<listcomp>r=   r   N)r,  r	   as_bool_bitr6   r(  if_unlikelyr   r4  r%  r   r&  r   _make_viewsr   r   r$  r   rQ   r  r  partial_loop_continue_loop_breakr5  )rI   rg   r5   r   r  r(  r)  r%  r   viewsrQ   r  r:   r;   iternext_specific{  sD    



 

z1make_nditer_cls.<locals>.NdIter.iternext_specificc                 S   s@   | j D ]4}|j|  kr"|jk rn q|||||j  qd S r  )r   r	  r
  r   rI   rg   r5   r)  rc  r:   r:   r;   r1    s    
z.make_nditer_cls.<locals>.NdIter._loop_continuec                 S   s@   | j D ]4}|j|  kr"|jk rn q|||||j  qd S r  )r   r	  r
  r  r5  r:   r:   r;   r2    s    
z+make_nditer_cls.<locals>.NdIter._loop_breakc                    s   dg }j }| j}j}	t|	tjr2t|	}	n|	g}	 fddtD t||D ]z\}
}|
\}}}}|j	|j
 }dkr|ddd }|D ]:}|| dkst| | ||	| || || |||< qqZtdd |D st|S )z:
            Compute the views to be yielded.
            Nc              	      s    g | ]}  t |qS r:   r6   r	   rT   r[  r5   r   r:   r;   r     s   z?make_nditer_cls.<locals>.NdIter._make_views.<locals>.<listcomp>r]  r  c                 s   s   | ]
}|V  qd S r  r:   r,  r:   r:   r;   r    s     z>make_nditer_cls.<locals>.NdIter._make_views.<locals>.<genexpr>)rY  r   r   r|   r   r  r   r2   r   r	  r
  r   
_make_viewr  )rI   rg   r5   r   r)  r%  r3  rY  r   rettysrc  r*  ro  Zarray_indicesZsub_indicesrY   )rm  narraysr  r+  r7  r;   r/    s2    


  z+make_nditer_cls.<locals>.NdIter._make_viewsc                 S   s   t |tjr|jdkst||||||}||||}	t||}
||t	tj
dd}||t	tj
dd}t|	||||
dd |	S )zD
            Compute a 0d view for a given input array.
            r   r:   N)r   )r|   r   r}   r[   r   r  rc   ri   r$  r  ru   r   )rI   rg   r5   r   r#  rs  rt  r*  rW   viewr   rQ   r   r:   r:   r;   r8    s    
z*make_nditer_cls.<locals>.NdIter._make_viewc           	      S   s`   g }t t||D ]H\}\}}t|tjrF||||||d q|t| d|  q|S )Nr   r!  )r  r   r|   r   r}   rU   rc   r  )	rI   rg   r5   r)  r%  r  rY   rs  rt  r:   r:   r;   r&    s    
z2make_nditer_cls.<locals>.NdIter._arrays_or_scalarsN)r\   r]   r^   r'  r  r  r   r  r4  r1  r2  r/  r8  r&  r:   
r  r  r  r  r  rm  r:  r[   r  r+  r:   r;   NdIter  s   O*r=  )r[   rm  r   r%  r  objectr	   rb   )r  r  r=  r:   r<  r;   make_nditer_cls  s    
 
, Yr?  c                    s$   | j  G  fdddt| }|S )zr
    Return the Structure representation of the given *nditerty* (an
    instance of types.NumpyNdIndexType).
    c                       s(   e Zd ZdZ fddZ fddZdS )z%make_ndindex_cls.<locals>.NdIndexIterz,
        .ndindex() implementation.
        c              
      s   | tjd}tj||j| tj d}t|tj}t D ]Z}t	|||}|
|| || }	|d|	|}
t||
 |
tj| W 5 Q R X q@|| _|| _t|||j| _d S Nr   r0  r_  )r   r   ru   r	   rG   r3   r   r  r2   rT   r   rb  r.  r  r   r(  rV   rQ   )rI   rg   r5   r   r   r   r(  r)  r  rN  dim_is_emptyr[   r:   r;   r    s"    
z3make_ndindex_cls.<locals>.NdIndexIter.init_specificc           
   	      s   | tjd} d}t  j}t | |	d  
| W 5 Q R X  fddtD }|D ]}t | qt|t ||j |	d t j}	t| |	jj  
|  | d S )Nr   rw  Fc              	      s"   g | ]}  t j|qS r:   )r6   r	   rT   r   r   r)  r5   rI   r:   r;   r     s
   
zKmake_ndindex_cls.<locals>.NdIndexIter.iternext_specific.<locals>.<listcomp>T)r   r   ru   r,  r	   r-  r6   r(  r.  r   r4  r2   r?   r   rV   r3   r   rQ   r  r   r5  )
rI   rg   r5   r   r   r  r(  r   r6   rQ   rB  rD  r;   r4    s,    




 
z7make_ndindex_cls.<locals>.NdIndexIter.iternext_specificN)r\   r]   r^   r'  r  r4  r:   rB  r:   r;   NdIndexIter  s   rE  )r[   r	   rb   )r  rE  r:   rB  r;   make_ndindex_cls  s    7rF  c                    s\    dkst | j}|jdkr:G  fdddt| }|S G  fdddt| }|S d S )N)r  r  r   c                       s8   e Zd ZdZ fddZ fddZdd Zdd	 Zd
S )z6_make_flattening_iter_cls.<locals>.CContiguousFlatIterzZ
            .flat() / .ndenumerate() implementation for C-contiguous arrays.
            c           	         s~   | tjd}t||| _|j| _ dkrztj||j	| tj|j
d}t|j
D ]}t|||}||| qT|| _d S )Nr   r  r0  )r   r   ru   r	   r   r   r   re  rG   r3   r[   r2   rT   r   r   )	rI   rg   r5   rs  rt  r   r   r)  r  r  r:   r;   r  ;  s     zD_make_flattening_iter_cls.<locals>.CContiguousFlatIter.init_specificc              	      s   |j }|j} | j} d||}	||	 t |	  |j	|g}
t
| ||
}dkrp|| nP| j fddt|D }t |}|t ||g t| || t |} || j W 5 Q R X d S )Nr   r  c              	      s    g | ]}  t |qS r:   r6  rC  r7  r:   r;   r   d  s
   
z\_make_flattening_iter_cls.<locals>.CContiguousFlatIter.iternext_specific.<locals>.<listcomp>)r[   r   r6   r   r   r   r	   r  rn  r   ro   r   r   r2   rV   make_anonymous_structr  r   r   )rI   rg   r5   rs  rt  r   r[   r   r   r   rW   r   idxvalsidxtuplerG  r7  r;   r4  T  s4    
 zH_make_flattening_iter_cls.<locals>.CContiguousFlatIter.iternext_specificc                 S   s   | |j|g}t||||S r  )rn  r   ro   rI   rg   r5   rs  rt  r   rW   r:   r:   r;   r  r  s    z>_make_flattening_iter_cls.<locals>.CContiguousFlatIter.getitemc                 S   s$   | |j|g}t||||| d S r  )rn  r   rr   rI   rg   r5   rs  rt  r   r   rW   r:   r:   r;   setitemv  s    z>_make_flattening_iter_cls.<locals>.CContiguousFlatIter.setitemN)r\   r]   r^   r'  r  r4  r  rM  r:   rG  r:   r;   CContiguousFlatIter6  s
   rN  c                       s<   e Zd ZdZdd Z fddZdd Zdd	 Zd
d ZdS )z+_make_flattening_iter_cls.<locals>.FlatIterz
            Generic .flat() / .ndenumerate() implementation for
            non-contiguous arrays.
            It keeps track of pointers along each dimension in order to
            minimize computations.
            c              
   S   s  | tjd}|j}|j}t||j|}tj||j	| tj|jd}	tj||j	| tj|jd}
t
|tj}t|D ]t}t||	|}t||
|}||| ||| || }|d||}t|| |tj| W 5 Q R X q||	| _|
| _|| _d S r@  )r   r   ru   r   r[   r	   r   rQ   rG   r3   r   r  r2   rT   r   rb  r.  r  r   pointersr(  )rI   rg   r5   rs  rt  r   r   r[   r   r   rO  r(  r)  r  ptrptrrN  rA  r:   r:   r;   r    sB        z9_make_flattening_iter_cls.<locals>.FlatIter.init_specificc              
      s"  |j }t |j|}t |j|}| j| j}	|tj	d}
 
d}t  | j}t | |d  | W 5 Q R X |d t |	|d } |}t| ||}dkr|| n: fddt|D }t |}|t ||g tt|D ]}t |}t  |}|| }|| } d	||}t |~  || t |	|} |}t ||} || t|d |D ] }t |	|} || q | W 5 Q R X  |
| q tj| j  |  | d S )
Nr   rw  FTr=   r  c              	      s    g | ]}  t |qS r:   r6  rC  r7  r:   r;   r     s
   
zQ_make_flattening_iter_cls.<locals>.FlatIter.iternext_specific.<locals>.<listcomp>r   )r[   r	   r   rQ   r   r   rO  r   r   ru   r,  r-  r6   r(  r.  r   r4  rT   ro   r   r2   rV   rH  ri  r   r   r  r   r   r  r5  )rI   rg   r5   rs  rt  r   r[   r   r   rO  r   r  is_exhaustedZlast_ptrrW   r   rI  rJ  r)  r  rx   r   re  r  rP  Z	inner_dimrG  r7  r;   r4    sl    
 





z=_make_flattening_iter_cls.<locals>.FlatIter.iternext_specificc              	   S   s   |j }tj||j|d}tj||j|d}g }	tt|D ]*}
|	||||
  |	|||
 }q:|	
  t|||j|||j|	}|S )Nr   )r[   r	   r   rQ   r   ri  r2   rU   ZuremZudivreverserl  r   rm  )rI   rg   r5   rs  rt  r   r[   r   r   r   r)  rW   r:   r:   r;   _ptr_for_index  s      z:_make_flattening_iter_cls.<locals>.FlatIter._ptr_for_indexc                 S   s    |  |||||}t||||S r  )rS  ro   rK  r:   r:   r;   r    s    z3_make_flattening_iter_cls.<locals>.FlatIter.getitemc                 S   s&   |  |||||}t||||| d S r  )rS  rr   rL  r:   r:   r;   rM     s    z3_make_flattening_iter_cls.<locals>.FlatIter.setitemN)	r\   r]   r^   r'  r  r4  rS  r  rM  r:   rG  r:   r;   FlatIter}  s   !ErT  )r   rO   rm  r	   rb   )r  r  rO   rN  rT  r:   rG  r;   r  0  s    
D r  r  c                 C   sh   t t|}|| |}||_| |}|| ||dd}|| ||| | }t| |t||S Nr   )rJ   )	r  r   NumpyFlatTyper   rc   rS   r  r   r   )rg   r5   rs  rt  flatiterclsflatiterarrclsr   r:   r:   r;   make_array_flatiter  s    

rZ  c                 C   sZ   |j \}|\}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
| d S r  )r   r  rO   rc   r   r4  )rg   r5   rK   r   r   r  rX  rW  rs  rY  rt  r:   r:   r;   iternext_numpy_flatiter  s    
r[  c                 C   sj   |j d }|\}}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
|}t| ||j|S Nr   r   )r   r  rO   rc   r   r  r   r   )rg   r5   rK   r   r  rX  r   rW  rs  rY  rt  r   r:   r:   r;   iternext_numpy_getitem'  s    

r]  c                 C   sf   |j d }|\}}}t|}|| ||d}|j}	| |	}
|
| ||jd}|| ||	||| |  S r\  )r   r  rO   rc   r   rM  r  )rg   r5   rK   r   r  rX  r   r   rW  rs  rY  rt  r:   r:   r;   iternext_numpy_getitem_any7  s    


r^  c           	      C   sF   |j d }t|}|| ||d d}| |j}|| ||jd}|jS r\  )r   r  rc   rO   r   r   )	rg   r5   rK   r   r  rW  rX  rY  rt  r:   r:   r;   iternext_numpy_getitem_flatH  s    
r_  c           
      C   sr   |j \}|\}tt|}|| |}||_| |}|| ||dd}|| ||| | }	t	| ||j
|	S rU  )r   r  r   NumpyNdEnumerateTyper   rc   rS   r  r   r   r   )
rg   r5   rK   r   rs  rt  	nditerclsr  rY  r   r:   r:   r;   make_array_ndenumerateS  s    

rb  c                 C   sZ   |j \}|\}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
| d S r  )r   r  rO   rc   r   r4  )rg   r5   rK   r   r   r  r  ra  rs  rY  rt  r:   r:   r;   iternext_numpy_nditere  s    
rc  c                    s^    fddt |j|D }ttt|}| }| | | }t |j	|S )zndindex(*shape)c                    s"   g | ]\}}  ||tjqS r:   r  )r   Zargtyr  r   r:   r;   r   y  s   z&make_array_ndindex.<locals>.<listcomp>)
r   r   rF  r   NumpyNdIndexTyper   r  r   r   r   )rg   r5   rK   r   rQ   ra  r  r   r:   r   r;   make_array_ndindexu  s    

re  c           
         s   |j j}|dkrJ|jd j|d }t ||} fdd|D }ng }ttt	|}| }|
 | | }	t |j |	S )zndindex(shape)r   c                    s   g | ]}  |tjqS r:   r  r   r5   rg   rw   r:   r;   r     s   z,make_array_ndindex_tuple.<locals>.<listcomp>)r   r[   r   re   r	   r   rF  r   rd  r   r  r   r   )
rg   r5   rK   r   r[   r  rQ   ra  r  r   r:   rf  r;   make_array_ndindex_tuple  s    
rg  c                 C   s6   |j \}|\}t|}|| ||d}|| || d S r  )r   rF  r4  )rg   r5   rK   r   r   r  r  ra  r:   r:   r;   iternext_numpy_ndindex  s
    rh  c           	      C   sn   |j }|j}t|jd tjr0t||d }n
|d g}t|| |}|	| ||| |
 }t| |||S )z
    nditer(...)
    r   )r   r%  r|   r   r   r  r	   r   r?  r  r   r   )	rg   r5   rK   r   r  r)  r%  r  r   r:   r:   r;   make_array_nditer  s    
ri  c                 C   s2   |j \}|\}t|| ||d}|| || d S r  )r   r?  r4  )rg   r5   rK   r   r   r  r  r:   r:   r;   iternext_numpy_nditer2  s    rj  c              	   C   sP  t |}|| |}| |j}| tjt| |}| tjd}tt	dd}	|D ]0}
|
||
}||d}||	||d}	qT|jdkrd}n|jdkr|g}t|dd D ]}|||d | qtt|}nT|jdkr$|g}|dd D ]}|||d | qt|}ntd|j|
||}||d}||	||d}	|j|	d	d
 | j|td W 5 Q R X |j}| |}| tj|}|  ||f}ttj}tt|}t ||tjtj}| !|t"||}| j#$||}| %tj}t&j'|||d}t&j'|||d}t(||)||* ||||d |S )a	  Utility function used for allocating a new array during LLVM code
    generation (lowering).  Given a target context, builder, array
    type, and a tuple or list of lowered dimension sizes, returns a
    LLVM value pointing at a Numba runtime allocated array.
    r=   r   r:   r   Nr  r]  z3Don't know how to allocate array with layout '{0}'.Fr  )z[array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size.)r
  r  )+rc   rd   re   r   r   ru   ri   r   r   r  Zsmul_with_overflowr  ra  r[   rm  ri  rU   r   r   rC   r   r   r  r  r   Zget_preferred_array_alignmentZuint32r  MemInfoPointervoidptrZTypeRefr3   r    r  _call_allocatorr   Zmeminfo_datar   r	   rV   r   r  r  )rg   r5   r  r   rf  r   datatyper   Zarrlenoverflowr   Zarrlen_multr   Zdimension_sizeZallocsize_multr  re   Z	align_valrk   r   mipZarytypeclassZargtypesr   r   r   Zshape_arrayZstrides_arrayr:   r:   r;   rk    sv    
 



 
rk  	_allocatec                 C   s   dd }|S )zTImplements a Numba-only default target (cpu) classmethod on the array
    type.
    c                 S   s
   t ||S r  )intrin_alloc)clsr  rk   r:   r:   r;   r     s    z _ol_array_allocate.<locals>.implr:   )rs  r  rk   r   r:   r:   r;   _ol_array_allocate  s    rt  c                 C   s   |  ||S )z9Trampoline to call the intrinsic used for allocation
    )rq  )r  ry   rk   r:   r:   r;   rm  !  s    rm  c                 C   s(   dd }t t j}t|||}||fS )z3Intrinsic to call into the allocator for Array
    c                 S   s   |\}}| j |||}|S r  )r   Zmeminfo_alloc_aligned)rg   r5   r    r   r  rk   r   r:   r:   r;   r  +  s    zintrin_alloc.<locals>.codegen)r   rk  rl  r    )r  r  rk   r  rp  rK   r:   r:   r;   rr  '  s    rr  c              
   C   s   dd }t |tjr.d}| |||tjg}n&t |tjs>t|j}tj	|||d}g }|D ]}|
|| ||j| q\| |tjd}	t|D ]>}
|d||
 |	}t|| | j|td W 5 Q R X q|S )z;
    Parse the shape argument to an array constructor.
    c           
   	   S   s   |  tj}|j}t|}t|d|d > d }|j|k rJ|||}n^|j|kr|d||}	|j	|	dd | j
|td W 5 Q R X |j|kr|||}n|}|S )z0Cast src to intp only if value can be maintainedr=   >Fr  )z#Cannot safely convert value to intp)r   r   ru   r>   r   r  r   Zsextr   r   r  r  r   trunc)
rg   r5   Zsrc_tr  r   Z
intp_widthZintp_irmaxvalr   Z	is_largerr:   r:   r;   safecast_intp9  s$    


 
z#_parse_shape.<locals>.safecast_intpr=   r   r   r   )znegative dimensions not allowed)r|   r   r~   rt   ru   r  r   r   r	   r   rU   r3   get_constant_genericr2   r   r.  r  r  r   )rg   r5   r
  rq   rx  r[   Zpassed_shapesr   r   r   r)  Zis_negr:   r:   r;   _parse_shape5  s(      rz  c                 C   s*   |j d }|d }|j}|t| |||fS )zL
    Parse the arguments of a np.empty(), np.zeros() or np.ones() call.
    r   )r   r   rz  )rg   r5   rK   r   ZarrshapetypeZarrshaper  r:   r:   r;   _parse_empty_argse  s    
r{  c                 C   sX   |j d }t|tjrJt|| ||d d}tj||j|jd}|j	|fS |j	dfS dS )z_
    Parse the arguments of a np.empty_like(), np.zeros_like() or
    np.ones_like() call.
    r   r   r   r:   N)
r   r|   r   r}   rc   r	   r   rQ   r[   r   )rg   r5   rK   r   arytyper   r   r:   r:   r;   r  o  s    

r  c                 C   s&   t |tjr"d|  d}t|d S )NzIf np.z0 dtype is a string it must be a string constant.)r|   r   ZUnicodeTyper   r   )fnamere   r   r:   r:   r;   _check_const_str_dtype}  s    r~  c                 C   s"   |j }||||}dd }||fS )Nc                 S   s(   t | |||\}}t| |||}| S r  )r{  rk  r   r  r5   rK   r  r  r   r   r:   r:   r;   r    s    znumpy_empty_nd.<locals>.codegenZinstance_type)r  Zty_shapety_dtypety_retty_refty_rettyrK   r  r:   r:   r;   numpy_empty_nd  s    r  c                    s   t d| |tks0t|tjr(|jtks0t|r8tj}nt|}t	| }|d k	r||d k	r|tj
||dd tf fdd	}|S d|  d| d}t|d S )	Nrs  r   r  c                    s   t | | S r  )r  )rQ   re   r#  r:   r;   r     s    zol_np_empty.<locals>.implz.Cannot parse input types to function np.empty(, ))r~  floatr|   r   Function
typing_keyr   doublety_parse_dtypety_parse_shaper}   r   r   )rQ   re   nb_dtyper[   r   r   r:   r  r;   ol_np_empty  s"    

r  c                 C   s"   |j }||||}dd }||fS )Nc                 S   s(   t | |||\}}t| |||}| S r  )r  rk  r   r  r:   r:   r;   r    s    z$numpy_empty_like_nd.<locals>.codegenr  )r  Zty_prototyper  r  r  rK   r  r:   r:   r;   numpy_empty_like_nd  s    r  c                    s   t d| t|st|}nt| tjr0| j}n| }|d k	r~t| tjrn| jdkrX| jnd}| j||dd qt|dd nd|  d| d	}t	
|d fd
d	}|S )Nr=  r  r   F)re   rm  r  r   z3Cannot parse input types to function np.empty_like(r  r  c                    s   t | | S r  )r  )rt  re   r  r:   r;   r     s    zol_np_empty_like.<locals>.impl)N)r~  r   r  r|   r   r}   re   rm  r  r   r   )rt  re   r  rm  r   r   r:   r  r;   ol_np_empty_like  s    


r  c                 C   s   t |}dd }||fS )Nc                 S   s<   t |jd | ||d }t||j||j|jd d S r   )rc   r   r	   r  r   r   r   r   )r  r5   rK   r  r   r:   r:   r;   r    s    z(_zero_fill_array_method.<locals>.codegen)r   r'  )r  rI   rK   r  r:   r:   r;   _zero_fill_array_method  s    
r  
_zero_fillc                 C   s   dd }|S )z?Adds a `._zero_fill` method to zero fill an array using memset.c                 S   s   t |  d S r  )r  r   r:   r:   r;   r     s    z ol_array_zero_fill.<locals>.implr:   )rI   r   r:   r:   r;   ol_array_zero_fill  s    r  c                 C   s   t d| tfdd}|S )Nzerosc                 S   s   t j| |d}|  |S Nrq  )r  rs  r  )rQ   re   rt  r:   r:   r;   r     s    zol_np_zeros.<locals>.impl)r~  r  rQ   re   r   r:   r:   r;   ol_np_zeros  s    
r  c                 C   s   t d| ddd}|S )N
zeros_likec                 S   s   t j| |d}|  |S r  )r  r=  r  )r  re   rt  r:   r:   r;   r     s    zol_np_zeros_like.<locals>.impl)Nr~  r  r:   r:   r;   ol_np_zeros_like  s    

r  c                 C   s   t d| ddd}|S )N	ones_likec                 S   s2   t j| |d}|j}tt|D ]}d||< q |S Nrq  r=   r  r=  r  r2   r   )r  re   rt  arr_flatrx   r:   r:   r;   r     s
    
zol_np_ones_like.<locals>.impl)Nr  r  r:   r:   r;   ol_np_ones_like  s    

r  c                    s2   t d| t|st| n| d fdd	}|S )Nfullc                    s0   t |  }|j}tt|D ]}|||< q|S r  r  rs  r  r2   r   )rQ   
fill_valuere   rt  r  rx   r  r:   r;   r  
  s
    
zimpl_np_full.<locals>.full)N)r~  r   r  )rQ   r  re   r  r:   r  r;   impl_np_full  s    

r  c                 C   s   t d| ddd}|S )NrQ  c                 S   s0   t | |}|j}tt|D ]}|||< q|S r  r  )r  r  re   rt  r  rx   r:   r:   r;   rQ    s
    
z$impl_np_full_like.<locals>.full_like)Nr  )r  r  re   rQ  r:   r:   r;   impl_np_full_like  s    

r  c                 C   s   t d| ddd}|S )Nonesc                 S   s2   t j| |d}|j}tt|D ]}d||< q |S r  r  )rQ   re   rt  r  rx   r:   r:   r;   r   '  s
    
zol_np_ones.<locals>.impl)Nr  r  r:   r:   r;   
ol_np_ones!  s    

r  c                    s4   t d| t|st| ntj d fdd	}|S )Nidentityc                    s.   t | | f }t| D ]}d|||f< q|S r;  )r  r  r2   )rw  re   rt  rY   r  r:   r;   r  8  s    z"impl_np_identity.<locals>.identity)N)r~  r   r  r   r  )rw  re   r  r:   r  r;   impl_np_identity0  s    

r  c                 C   s   d S r  r:   NMr:   r:   r;   _eye_none_handler@  s    r  c                 C   s"   t |tjrdd }ndd }|S )Nc                 S   s   | S r  r:   r  r:   r:   r;   r   G  s    z$_eye_none_handler_impl.<locals>.implc                 S   s   |S r  r:   r  r:   r:   r;   r   J  s    r|   r   rY  )r  r  r   r:   r:   r;   _eye_none_handler_implD  s    
r  c                    sf   |d kst |tjr tt n.t |tjtjfrDtt	|d| n
t| d dtf fdd	}|S )Nre   r   c                    s   t | |}t| |f }|dkrPt| || }t|D ]}d|||| f< q8n,t| | |}t|D ]}d||| |f< qf|S r  )r  r  r  r@  r2   )r  r  r   re   Z_Mrt  drY   r  r:   r;   r   Z  s    
znumpy_eye.<locals>.impl)
r|   r   rY  r  re   r  	DTypeSpecr  r   r  )r  r  r   re   r   r:   r  r;   	numpy_eyeO  s    
r  c                 C   sD   t | stdt| tjr@| jdkr2tdddd}|S d S )Nz#The argument "v" must be array-like)r=   r  zInput must be 1- or 2-d.r   c                 S   s.  | j dkr| j}|d t| }t||f| j}|dkrbt|| D ]}| | |||| f< qFn&t|| D ]}| | ||| |f< qn|S | j\}}|dk r|| }|dkr|| }tt||d}t	|| j}|dkrt|D ]}| ||| f ||< qn$t|D ]}| || |f ||< q
|S d S rC  )
r[   rQ   r  r  r  re   r2   r  r@  rs  )r   r   r   rw  r  rY   rowscolsr:   r:   r;   	diag_implr  s.    


zimpl_np_diag.<locals>.diag_impl)r   )r   r   r   r|   r   r}   r[   NumbaTypeError)r   r   r  r:   r:   r;   impl_np_diagi  s    



r  takec                    s   t | tjr$t |tjr$dd }|S tdd | |fD rT|jdk  fdd}|S t | tjr~t |tjtjfr~dd }|S d S )Nc                 S   s.   || j d ks|| j  k r"td|  | S )Nr=   Index out of bounds)ry   
IndexErrorr+  )r  r   r:   r:   r;   	take_impl  s    znumpy_take.<locals>.take_implc                 s   s   | ]}t |tjV  qd S r  r	  r   r  r:   r:   r;   r    s     znumpy_take.<locals>.<genexpr>r]  c                    s   t j|j| jd} r | }n|}t |}d}|  }|D ]:}|| jd ks\|| j k rdtd|| ||< |d }q>||j	S Nrq  r   r=   r  )
r  rs  ry   re   r  r  r+  r  r(  rQ   )r  r   r  ZwalkeritrY   r  r  ZF_orderr:   r;   r    s    


c                 S   s~   t |}t j|j| jd}t |}d}|  }|D ]:}|| jd ksT|| j k r\td|| ||< |d }q6||j	S r  )
r  r   rs  ry   re   r  r+  r  r(  rQ   )r  r   convertr  r  rY   r  r  r:   r:   r;   r    s    


)r|   r   r}   r~   r  rm  r{  r  )r  r   r  r:   r  r;   
numpy_take  s    
r  c                  G   s|   dd | D }t dd |D r(tj}nPt dd |D rBtj}n6ttddttj  }dd |D }t	||g }|S )	Nc                 S   s   g | ]}t |tjs|qS r:   r  r  r:   r:   r;   r     s      z!_arange_dtype.<locals>.<listcomp>c                 s   s   | ]}t |tjV  qd S r  r|   r   Complexr  r:   r:   r;   r    s     z _arange_dtype.<locals>.<genexpr>c                 s   s   | ]}t |tjV  qd S r  )r|   r   Floatr  r:   r:   r;   r    s     zint%sr  c                 S   s   g | ]}t |qS r:   )r   Z	unliteralr   r  r:   r:   r;   r     s     )
r  r   
complex128r   r  r  re   r   r   r  )r   Zboundsre   ZNPY_TYZunliteral_boundsr:   r:   r;   _arange_dtype  s    r  c                    s  t |tjr|j}t |tjr$|j}t |tjr6|j}|d krDtj}|d krRtj}|d kr`tj}t | tjrt |tjtjfrt |tjtjfrt |tjtjfsd S t |tjrt| ||n|j	t
dd | ||fD t| dd  t|dd t|dd d fdd	}|S )Nc                 S   s   g | ]}t |tjqS r:   r  r  r:   r:   r;   r     s   znp_arange.<locals>.<listcomp>r   c                    s    d k	r n| }d k	rn|}d k	r,n|}|d k	r<|nd}|d krTd| }}	n
|| }}	|dkrnt d|	| | }
tt|
j}dkrtt|
j}tt||d}n
t|d}t	|}|}t
|D ]}|||  ||< q|S )Nr=   r   zMaximum allowed size exceededT)r   r   mathceilr  r  r  r@  r  rs  r2   )r   rT  rO  re   Z	lit_startZlit_stopZlit_stepZ_step_start_stopZnitems_cZnitems_rZnitems_ir   rt  rq   rY   Zstart_valueZ
step_valueZ
stop_valueZ
true_dtypeZuse_complexr:   r;   r     s(    

znp_arange.<locals>.impl)NNN)r|   r   Optionalr3   r'  r  rY  r  r  re   r  r  )r   rT  rO  re   r   r:   r  r;   	np_arange  s>    r  c                    sn   t dd | |fD sd S t|ttjfs8d}t|tdd | |fD rVtj ntj	 d fdd	}|S )	Nc                 s   s   | ]}t |tjV  qd S r  )r|   r   r  r  r:   r:   r;   r     s     z!numpy_linspace.<locals>.<genexpr>z%The argument "num" must be an integerc                 s   s   | ]}t |tjV  qd S r  r  r  r:   r:   r;   r  '  s     r  c                    s   t | }| d } |d }|dkr(|S |d }|dkrn||  }t ||}td|D ]}| ||  ||< qVn| |d< |dkr||d< |S )Ng      ?r   r=   r  )r  rs  divider2   )r   rT  r  rt  divdeltarO  rY   rq  r:   r;   r	  -  s    z numpy_linspace.<locals>.linspace)r  )
r  r|   r   r   r~   r   r   r  r  r   )r   rT  r  r   r	  r:   rq  r;   numpy_linspace  s    
r  c              
   C   s  |j d }t|| ||d d}t||j}|j}t| |||}|j}	|j}
|jdks\t	|j|jkrtj
||
|	|j|jdd nt||j}t||j}| tj}t|||H}t| ||	|||j|}t| ||
|||j|}|||| W 5 Q R X t| ||j| S )z
    Array copy.
    r   r   rr  r=   rj   )r   rc   r	   r   rQ   r   rk  r   rm  r   Z
raw_memcpyr   r   r   r   r   ru   r  rl  r   r6   r   r   )rg   r5   rK   r   r|  r   r   rettyper  r  r  r  r  r   r   r  r  r:   r:   r;   _array_copyF  s>    
 
  
  r  c                 C   s@   t |tjst|jdkrdnd}|j|dd}||}|tfS )Nr]  r   F)rm  r  )r|   r   r}   r   rm  r  r  )r  r  rm  r  rK   r:   r:   r;   _array_copy_intrinsick  s
    r  z
array.copyc                 C   s   t | |||S r  )r  r  r:   r:   r;   
array_copyt  s    r  c                 C   s   t | tjrdd }|S )Nc                 S   s   t | S r  )r  r9  r:   r:   r;   
numpy_copy|  s    z#impl_numpy_copy.<locals>.numpy_copyr	  )r  r  r:   r:   r;   impl_numpy_copyy  s    r  c              
   C   s  |j }|jd }|j|ks"td|jdkr|jdks:tt|| ||d d}t|| |}| |ttj	dd}	| 
|ttj	d|jf}
t||j|	|
|j|j|j t| ||| S |j|jks|jdkr|jdkrt| |||d S |jdkr|dkst|dkrtnt}t|| |||d }||V\}}| t| |||d }|j}W 5 Q R X | t| |||}|j}W 5 Q R X W 5 Q R X ||j}||| ||| |S t| |||S d	S )
zd
    Common logic for layout conversion function;
    e.g. ascontiguousarray and asfortranarray
    r   z return-type has incorrect layoutr=   r   r  rr  r  r   N)r   r   rm  r   r[   rc   ry  r   r  ru   r$  r   r   r   r   r   r   r   r   r   r  r}  blockr  phir3   Zadd_incoming)rg   r5   rK   r   output_layoutr#  r   r   r  rQ   r   Z
check_funcr  ZthenZorelseZout_thenZthen_blkZ
out_orelseZ
orelse_blkZret_phir:   r:   r;   _as_layout_array  sd    

   r  c                    sH   t  tjst |j jt|jdd}|| }| fddfS )Nr=   )rm  r[   c                    s   t | ||| jdS )Nr  )r  r   )rG  rH  r   r  r  r:   r;   r    s       z,_as_layout_array_intrinsic.<locals>.<lambda>)	r|   r   r!   r   RequireLiteralValuer  r   r  r[   )r  r  r  r  rK   r:   r  r;   _as_layout_array_intrinsic  s
    

r  c                 C   sF   t | stdt| tjtjfr.dd }nt| tjrBdd }|S )NrJ  c                 S   s   t t | S r  )r  ascontiguousarrayr   r9  r:   r:   r;   r     s    z%array_ascontiguousarray.<locals>.implc                 S   s
   t | dS Nr   r  r9  r:   r:   r;   r     s    r   r   r   r|   r   r  rX  r}   ri  r:   r:   r;   array_ascontiguousarray  s    

r  c                 C   sL   t | stdt| tjtjfr0dd }|S t| tjrHdd }|S d S )NrJ  c                 S   s   t t | S r  )r  asfortranarrayr   r9  r:   r:   r;   r     s    z"array_asfortranarray.<locals>.implc                 S   s
   t | dS )Nr]  r  r9  r:   r:   r;   r     s    r  ri  r:   r:   r;   array_asfortranarray  s    
r  zarray.astypec              
   C   s  |j d }t|| ||d d}t||j}|j}t| |||}|j}	|j}
t||j}t||j}| 	t
j}t|||h}t| ||	|||j|}t| ||
|||j|}t| |||}| |||j|j}t| |||| W 5 Q R X t| ||j| S r\  )r   rc   r	   r   rQ   r   rk  r   r   r   r   ru   r  rl  rm  ro   rt   re   rr   r   r   )rg   r5   rK   r   r|  r   r   r  r  r  r  r  r  r   r   r  r  itemr:   r:   r;   array_astype  s6    

  
  r  c                 C   s"   |j }||||}dd }||fS )Nc              	   S   s  |j d }|j}t|| ||d d}t|}|| |}|j}	t| |}
t|jj|
}||j	|j}|
||}t||}|j|dd d}| j|t|f W 5 Q R X t||||g}t||g}||j| |	d}t||||||j|jd | }t| ||j|S )Nr   r   Fr  z.buffer size must be a multiple of element sizer   r  )r   r   rc   r   ri   r   r   r3   r   r   Zsremr	   Zis_not_nullr   r  r  r   rV   Zsdivr  r   r   r   r   r   r   r   r   )rg   r5   rK   r   Zbuftyr   r  Z
out_ary_tyout_aryZout_datamodelr   ll_itemsizer  remZis_incompatibler   rQ   r   r   r   r:   r:   r;   r    s<    


 znp_frombuffer.<locals>.codegenr  )r  bufferre   r#  r
  rK   r  r:   r:   r;   np_frombuffer
  s    $r  c                    s   t d| t| tjr | jdkr4d|  }t||tksZt|tjrR|j	tksZt
|rbtj}nt|}|d k	rtj|dd| j d nd|  d| d}t|tf fd	d
	}|S )N
frombufferr   z+Argument "buffer" must be buffer-like. Got r=   )re   r[   rm  r  z3Cannot parse input types to function np.frombuffer(r  r  c                    s   t | | S r  )r  )r  re   r  r:   r;   r   M  s    z impl_np_frombuffer.<locals>.impl)r~  r|   r   r   rm  r   r   r  r  r  r   r  r  r}   r  )r  re   r   r  r   r:   r  r;   impl_np_frombuffer6  s*    





r  c                    sP   t |r$tdd  d fdd	}|S t|tjrLtd| d fdd	}|S d S )Nr   c                    s
    | |S r  r:   rW   rQ   re   intrinsic_cfarrayr:   r;   r   W  s    zimpl_carray.<locals>.implc                    s
    | |S r  r:   r  r  r:   r;   r   ]  s    )N)Nr   get_cfarray_intrinsicr|   r   r  rW   rQ   re   r   r:   r  r;   impl_carrayR  s    

r  c                    sP   t |r$tdd  d fdd	}|S t|tjrLtd| d fdd	}|S d S )Nr]  c                    s
    | |S r  r:   r  r  r:   r;   r   g  s    zimpl_farray.<locals>.implc                    s
    | |S r  r:   r  r  r:   r;   r   m  s    )N)Nr  r  r:   r  r;   impl_farrayb  s    

r  c                    s   t  fdd}|S )Nc           	         s   |t jkrd }n*t|t jr$|j}nd| d}t| d kr^|d krXd}t||}nVt t jr j}|d k	r||krd| d| d}t|nd  d}t|t|}|d krd| d}t|t 	||}t
|||}|tfS )Nz pointer argument expected, got ''z*explicit dtype required for void* argumentzmismatching dtype 'z' for pointer type 'zinvalid dtype spec 'zinvalid shape ')r   rl  r|   CPointerre   r   r  r  r  r}   r    
np_cfarray)	r  rW   rQ   Z	ptr_dtyper   re   r[   r#  rK   dtype_rm  r:   r;   r  s  s2    




z0get_cfarray_intrinsic.<locals>.intrinsic_cfarrayr%   )rm  r  r  r:   r  r;   r  r  s    r  c                    s<  |j dd \}}|dd \}}|j}|jdks6tt| }	t|}
t|
}t|t	j
rrt |}n|f}|f} fddt||D }|}g }|jdkr|D ]}||  ||}qn,t|D ]}||  ||}q|   ||j }t|	||||dd |	 }t |j|S )zR
    numba.numpy_support.carray(...) and
    numba.numpy_support.farray(...).
    Nr  rr  c                    s"   g | ]\}}  ||tjqS r:   r  )r   r  r   r   r:   r;   r     s   znp_cfarray.<locals>.<listcomp>r]  r  )r   r   rm  r   rc   ri   r	   r   r|   r   r  r   r   rU   r   ri  rR  r  rd   re   r  r   r   r   )rg   r5   rK   r   r  r  rW   rQ   r   r  r   r  r   offr   r   r   r   r:   r   r;   r    sH    




	r  c                 C   sV   t |tjr| tjt|S t |tjrJ| tttj|}|||fS dsRt	d S r   )
r|   r   r  r   ru   r   r  rB   r    r   )rg   r5   seqtyseqr  r:   r:   r;   _get_seq_size  s    r  c                    s2   |j  tjt|tj fdd}|S )zK
    Return a getitem() implementation that doesn't incref its result.
    c                    s$   | |} j r  j| | |S r  )r   r   Zdecref)r5   r   r  rg   r  r#  r:   r;   wrap  s    
z$_get_borrowing_getitem.<locals>.wrap)re   rB   r  r  r    r   ru   )rg   r  r  r:   r  r;   _get_borrowing_getitem  s    r  c                    st    tj}t|d fdd}g }|| }}	t|D ].}
|
dkrV|||	\}}	|t ||	 q<t|S )zF
    Compute the likely shape of a nested sequence (possibly 0d).
    r   c                    sT   t | tjr2t| dkrdS | d  |dfS nt| }| j| |ffS d S )Nr   )NN)r|   r   r  r   r  r  re   )r  r  r  r5   rg   r   r:   r;   get_first_item  s    
z.compute_sequence_shape.<locals>.get_first_item)r   r   ru   r   r2   rU   r  r   )rg   r5   r[   r  r  r   r  r   innertyinnerrY   r:   r  r;   compute_sequence_shape  s    

r  c                    s0   fdd  fdd||| dS )z?
    Check the nested sequence matches the given *shapes*.
    c                      s   j  td d S )N)zincompatible sequence shape)r  r  r   r:   r   r:   r;   _fail  s    
z#check_sequence_shape.<locals>._failc              	      s  t |dkrd S t| |}|d }d||}j|dd    W 5 Q R X t |dkrdd S t| tjrt| }t	|0}| j
}|||jf}	||	|dd   W 5 Q R X nXt| tjrtt | D ],}
| |
 }||
}	||	|dd   qndst| d S )Nr   r  Fr  r=   )r   r  r   r   r|   r   r  r  r	   rE  re   r   r  r2   r  r   )r  r  r   ry   expectedr  r  rF  r	  r
  rY   r  r5   check_seq_sizerg   r:   r;   r    s*    
 z,check_sequence_shape.<locals>.check_seq_sizeNr:   )rg   r5   r  r  r   r:   r  r;   check_sequence_shape  s    r  c                    s:    fddfdd||d dS )zl
    Assign a nested sequence contents to an array.  The shape must match
    the sequence's structure.
    c              
      sB   t j j| dd}|| j}t || d S r  )r	   rl  rm  rt   re   rr   )r   r  rq   rW   )rs  r5   rg   r   r   r   r:   r;   assign_item3  s      z-assign_sequence_to_array.<locals>.assign_itemc              	      s  t |dkr2t| tjtjfr"t|| | d S |d }t| tjrt| }t|:}| j	}|||j
f} |||dd  ||j
f  W 5 Q R X nlt| tjrtt | D ]B}	| |	 }||	}tj|	}
 |||dd  ||
f  qnds
t| d S r  )r   r|   r   r  r  r   r  r	   rE  re   r   r2   r  r   ru   )r  r  r   r   ry   r  rF  r	  r
  rY   r   )assignr  r5   rg   r:   r;   r  9  s$    
* z(assign_sequence_to_array.<locals>.assignr:   Nr:   )rg   r5   r   r   r   rs  r  r  r:   )rs  r  r  r5   rg   r   r   r   r;   assign_sequence_to_array,  s    r  c                 C   s>   t | |\}}t|r|}nt|}|d kr0d S t||dS r  )r/   r   r  r   r}   )r  r>  re   r[   Z	seq_dtyper:   r:   r;   np_array_typerU  s    r  c                 C   s0   t d| t| ||}|||}dd }||fS )Nr   c           
   	   S   s   |j }|j}|jd }|d }t| ||||}t||ks>tt| |||| t| |||}	t| ||	j	||	j
||| t| ||j |	 S r   )r   r[   r   r  r   r   r  rk  r  r   r   r   r   )
rg   r5   rK   r   rs  r[   r  r  r   rt  r:   r:   r;   r  f  s"    
   
znp_array.<locals>.codegen)r~  r  )r  objre   r  rK   r  r:   r:   r;   np_array`  s
    

r  c                 C   sL   t d| t| stdt|s>t|d kr>d}t|ddd}|S )Nr   z(The argument "object" must be array-likez:The argument "dtype" must be a data-type if it is providedc                 S   s
   t | |S r  )r  )r>  re   r:   r:   r;   r     s    zimpl_np_array.<locals>.impl)N)r~  r   r   r   r   r  )r>  re   r   r   r:   r:   r;   impl_np_arrayz  s    



r  c           
   	   C   s   | d}| |}|d||}||||||}||d|||d||}|j|dd  d| }	| j|t|	f W 5 Q R X |S )Nr   r   r3  Fr  z%s(): axis out of bounds)	r3   r   rS  r=  ra  r   r  r  r  )
rg   r5   r   r[   r   r   Zll_ndimZis_neg_axisZaxis_out_of_boundsr   r:   r:   r;   _normalize_axis  s    

r  c              
   C   s   t ||d kstttj|}t||}td}t|d D ]R}t|}	|d|	|}
|| }|	|
|
|	||	}||t||d| qD||t||d| t|||S )z
    Compute shape with the new axis inserted
    e.g. given original shape (2, 3, 4) and axis=2,
    the returned new shape is (2, 3, 1, 4).
    r=   r3  r   r   r   r   r  r	   r   rG   r2   r   rS  r=  r   rT   r   r6   )rg   r5   
orig_shaper[   r   ll_shtyr   r   r)  ll_dim
after_axisr   rx   r:   r:   r;   _insert_axis_in_shape  s    


r  c              
   C   s   t ||d kstttj|}t||}td}td}t|d D ]N}	t|	}
|d|
|}|	||
|
||
}|||	 t||d| qN||t||d| t|||S )zD
    Same as _insert_axis_in_shape(), but with a strides array.
    r=   r   r3  r  )rg   r5   orig_stridesr[   r   r  r   r   r   r)  r  r  rx   r:   r:   r;   _insert_axis_in_strides  s"    




r   c              	   C   s   |j }|j}|jd }t|| ||d d}t|| |}	t||j}
t||j}t| ||
||}t	| ||||}t
|	|j|||j|j|jd |	 S )z/
    np.expand_dims() with the given axis.
    r   r   r  )r   r[   r   rc   r	   r   rQ   r   r  r   r   r   r   r   r   r   )rg   r5   rK   r   r   r#  r[   rs  rt  r  r   r   Z
new_shapesZnew_stridesr:   r:   r;   expand_dims  s$    
r!  c                 C   sB   |j dkr|jnd}|j|j d |d}|||}dd }||fS )Nr=   r  r  c                 S   sP   |  ||d |jd tj}t| |d|jj|}t| ||||}t| ||j|S )Nr=   znp.expand_dims)	rt   r   r   ru   r  r   r[   r!  r   )rg   r5   rK   r   r   r  r:   r:   r;   r    s     znp_expand_dims.<locals>.codegen)r[   rm  r  )r  r  r   rm  r  rK   r  r:   r:   r;   np_expand_dims  s
    
r"  c                 C   sL   t | tjs d|  }t|t |tjs@d| }t|dd }|S )Nz)First argument "a" must be an array. Got z(Argument "axis" must be an integer. Got c                 S   s
   t | |S r  )r"  )r  r   r:   r:   r;   r     s    z!impl_np_expand_dims.<locals>.impl)r|   r   r}   r   r   r~   )r  r   r   r   r:   r:   r;   impl_np_expand_dims  s    



r#  c                    s   t  fddfddS )Nc                    sX   | fdd|D  fdd}t tdkr@tnd tj||fS )Nc                    s    g | ]}|j t|j d qS )rB  )r  r  r[   r  )minimumr:   r;   r     s     z-_atleast_nd.<locals>.impl.<locals>.<listcomp>c                    sn   t t |d } fddt|D }t|dkrV |j|}n|d }t |j|S )Nr   c                    s"   g | ]\}}} |||qS r:   r:   )r   rt  rs  r#  r5   rg   	transformr:   r;   r     s   z>_atleast_nd.<locals>.impl.<locals>.codegen.<locals>.<listcomp>r=   )_atleast_nd_transformr	   r   r   r   r$  r   r   )rg   r5   rK   r   arrsZretsr  )r)  r  r$  r9  r%  r;   r    s    

z*_atleast_nd.<locals>.impl.<locals>.codegenr=   r   )r    r   r   r  ZStarArgTupleZ
from_types)Ztypingcontextr   r  )r  r$  )r)  r9  r;   r     s    
z_atleast_nd.<locals>.implc                     s    |  S r  r:   r  )r   r:   r;   r  (  r  z_atleast_nd.<locals>.<lambda>r  )r$  r  r:   )r  r   r$  r;   _atleast_nd  s    r)  c                    s"   t  kst fdd}|S )z`
    Return a callback successively inserting 1-sized dimensions at the
    following axes.
    c           	         sb   t D ]T}|d }|j|k rt | }|j|jd d}t| |t|||f|}|}q|S )Nr=   rB  )r2   r[   r	   r   r  r!  r   r    )	rg   r5   rt  rs  r#  rY   r[   r   Znewarrtyr  min_ndimr:   r;   r&  2  s    

 z(_atleast_nd_transform.<locals>.transform)r   r   )r+  r  r&  r:   r*  r;   r'  +  s    r'  c                  G   s"   t dd | D rtddgS d S )Nc                 s   s   | ]}t |tjV  qd S r  r	  r  r:   r:   r;   r  D  s     z np_atleast_1d.<locals>.<genexpr>r=   r   r  r)  r  r:   r:   r;   np_atleast_1dB  s    r-  c                  G   s$   t dd | D r tdddgS d S )Nc                 s   s   | ]}t |tjV  qd S r  r	  r  r:   r:   r;   r  J  s     z np_atleast_2d.<locals>.<genexpr>r  r   r,  r  r:   r:   r;   np_atleast_2dH  s    r.  c                  G   s&   t dd | D r"tddddgS d S )Nc                 s   s   | ]}t |tjV  qd S r  r	  r  r:   r:   r;   r  P  s     z np_atleast_3d.<locals>.<genexpr>r  r   r  r,  r  r:   r:   r;   np_atleast_3dN  s    r/  c	                 C   s  t |t |  kr.t |  kr.t |ks4n ttd}	t| |||}
t||
j}g }|D ]d}|	}tt||D ]B\}\}}|	d|
||}|||}||||||}qx|| qb|
j}t|||||D ]\}}}}}|j}tj||tj|jd}|h}t| |||||j|}t| |||}| |||j|j}t| |||||j|}t| |||| W 5 Q R X t|||}q|
S )z2
    Concatenate arrays along the given axis.
    r   r_  )order)r   r   r	   r   rk  r   r   r  r   r   r3   r   rS  r=  rU   r   r  rm  rl  ro   rt   re   rr   r   )rg   r5   r   r)  r(  
arr_shapesarr_stridesr#  
ret_shapesr   r  Zret_stridesZcopy_offsetsZarr_shr   r)  ry   re  is_axisaddendZret_datars  rt  Zarr_stZarr_datar  r   r  rq   r  r:   r:   r;   _do_concatenateT  sT    4

 
  
  r6  c                    s  |j } fddt||D }t d||} fdd|D } fdd|D } fdd|d D }	t|D ] d||}
|	 } |}fd	d|d
d  D } |
\}}|& t	 j
||g } || W 5 Q R X |d tj}|D ]} | d||}q j |dd j td f W 5 Q R X W 5 Q R X W 5 Q R X qr fdd|	D }	t |||||||		}t || S )Nc                    s"   g | ]\}}t | |d qS r   r  r   r  r  r   r:   r;   r     s   z#_np_concatenate.<locals>.<listcomp>np.concatenatec                    s   g | ]}t  |jqS r:   )r	   r   rQ   r   rt  r  r:   r;   r     s     c                    s   g | ]}t  |jqS r:   r	   r   r   r:  r  r:   r;   r     s     c                    s   g | ]}t  |qS r:   )r	   r   r  r  r:   r;   r     s   r   r_  c                    s   g | ]}|  qS r:   r:   r  )r)  r:   r;   r     s     r=   Fr  z<np.concatenate(): input sizes over dimension %d do not matchc                    s   g | ]}  |qS r:   )r6   r  r  r:   r;   r     s     )r[   r   r  r2   r   r3   r6   r}  r  r  r=  r   r	   r  rz  r   rJ  r  r  r   r6  r   r   )rg   r5   r)  r(  r#  r   r[   r1  r2  r3  r4  Zret_shape_ptrZret_shZother_shapesZon_axisZon_other_dimr   is_okr  r:   )r5   rg   r)  r;   _np_concatenate  s\    

 $    r=  c                    s  |j }td}td}tt|}	 fddt||D }t d||}t |d j}
|dd  D ]n}tj}tt |j|
D ]N\}} 	| 
d||} j |dd j td	 W 5 Q R X qqp fd
d|D }ttj|t }t }t|d D ]l}t|} 
d||}|
| } | |||} |t |d|  |t |d| q$ |t |d|  |	t |d| t  |}|gt| }t  |} fddtt|D }t|d D ]l}t|} 
d||} | |||}tt|D ]*} || | t || d| qZqtt|D ]"} |t || d| q fdd|D }t |||||||	}t || S )Nr   r=   c                    s"   g | ]\}}t | |d qS r7  r  r8  r   r:   r;   r     s   z_np_stack.<locals>.<listcomp>np.stackr_  Fr  )z5np.stack(): all input arrays must have the same shapec                    s   g | ]}t  |jqS r:   r;  r:  r  r:   r;   r     s     r3  c                    s   g | ]}t  qS r:   r  r[  )r5   r  r:   r;   r     s   c                    s   g | ]}t   |qS r:   )r	   r   r6   )r   r   r  r:   r;   r     s   )r[   r	   r   r   r   r  r   rQ   r  rz  r   r   rJ  r  r  r   r   r  rG   r2   rS  r=  r   rT   r6   r6  r   r   )rg   r5   r)  r(  r#  r   r[   r   r   Z
ll_narraysr  rt  r<  r   Zorig_shr  Zinput_shapesr3  r)  r  r  rx   Zinput_stridesrY   r  r:   )r5   rg   r  r;   	_np_stack  s    

 






    r?  c                 C   sN   |d k	rt |tjsd S t| d|\}}|dkr8tdt|}t|||S )Nr9  r   z.zero-dimensional arrays cannot be concatenated)r|   r   r~   r0   	TypeErrorr1   r}   )r  r%  r   re   r[   rm  r:   r:   r;   np_concatenate_typer  s     rA  c                 C   s6   t | ||}t|tjst|||}dd }||fS )Nc              	   S   sF   |  ||d |jd tj}t| |t|jd t||d |j|S rC  )	rt   r   r   ru   r=  r   r	   r   r   rg   r5   rK   r   r   r:   r:   r;   r  3  s    znp_concatenate.<locals>.codegen)rA  r|   r   r}   r   )r  r%  r   r  rK   r  r:   r:   r;   np_concatenate-  s
    
rC  c                 C   s   t | tjrddd}|S d S )Nr   c                 S   s
   t | |S r  )rC  r%  r   r:   r:   r;   r   A  s    z!impl_np_concatenate.<locals>.impl)r   r|   r   r  r%  r   r   r:   r:   r;   impl_np_concatenate>  s    
rG  c                 C   s*   |D ] }|j dk s|j dkrtdqdS )Nr=   r  z7np.column_stack() is only defined on 1-d and 2-d arrays)r[   r@  )rg   r   r%  r  r:   r:   r;   _column_stack_dimsF  s    
rH  c                 C   sB   t | d|td\}}t|}t|||}||}dd }||fS )Nznp.column_stack)Zdim_chooserc                 S   s   t |jd }t||d }g }g }| tjd}t||D ]t\}	}
|	jdkrf|	|	 |	|
 q>|	jdkstt
|	jdd}t||	}t| |||
f|}|	| |	| q>t| ||||j|S )Nr   r=   r  rB  )r   r   r	   r   r   r   ru   r   r[   rU   r   r  r   r    r!  r=  r   )rg   r5   rK   r   Zorig_arrtysZ	orig_arrsr)  r(  r   rs  rt  re  
expand_sigZnewarrr:   r:   r;   r  X  s&    



 z np_column_stack.<locals>.codegen)r0   rH  r1   r   r}   )r  r  re   r[   rm  r  rK   r  r:   r:   r;   np_column_stackO  s     
rJ  c                 C   s   t | tjrdd }|S d S )Nc                 S   s   t | S r  )rJ  r  r:   r:   r;   r   x  s    zimpl_column_stack.<locals>.implrE  r  r   r:   r:   r;   impl_column_stacku  s    rM  c              	   C   s*   t | |t|jd t||d |j|S )z/
    np.stack() with the given axis value.
    r   )r?  r   r   r	   r   r   rB  r:   r:   r;   _np_stack_common}  s    rN  c           	      C   sV   t | d|\}}tdd |D r&dnd}t||d |}|||}dd }||fS )	Nr>  c                 s   s   | ]}|j d kV  qdS )r]  N)rm  r  r:   r:   r;   r    s     z"np_stack_common.<locals>.<genexpr>r]  r   r=   c                 S   s,   |  ||d |jd tj}t| ||||S r;  )rt   r   r   ru   rN  rB  r:   r:   r;   r    s    z np_stack_common.<locals>.codegen)r0   r  r   r}   )	r  r%  r   re   r[   rm  r  rK   r  r:   r:   r;   np_stack_common  s     
rO  c                 C   s   t | tjrddd}|S d S )Nr   c                 S   s
   t | |S r  )rO  rD  r:   r:   r;   r     s    zimpl_np_stack.<locals>.impl)r   rE  rF  r:   r:   r;   impl_np_stack  s    
rP  c                 C   s4   t | ||\}}t||}t|}t|||}|S r  )r0   r  r1   r   r}   )r  r   r%  Zndim_minre   r[   rm  r  r:   r:   r;   NdStack_typer  s
    
rQ  c                 C   s&   t | d|d}||}dd }||fS )Nz	np.hstackr=   c                    sj   |j d }|d j}|dkr:| tjd t| ||| S |dkrFdnd  fdd}| ||||S d S )Nr   r=   c                    s   t j|  dS r/  r1  r%  r0  r:   r;   np_hstack_impl  s    z3_np_hstack.<locals>.codegen.<locals>.np_hstack_implr   r[   r   r   ru   rN  r  )rg   r5   rK   r   r  r[   rS  r:   r0  r;   r    s    

z_np_hstack.<locals>.codegenrQ  r  r  r  rK   r  r:   r:   r;   
_np_hstack  s    rW  c                 C   s   t | tjrdd }|S d S )Nc                 S   s   t | S r  )rW  rK  r:   r:   r;   r     s    zimpl_np_hstack.<locals>.implrE  rL  r:   r:   r;   impl_np_hstack  s    rX  c                 C   s&   t | d|d}||}dd }||fS )Nz	np.vstackr  c                 S   sd   |j d }|d j}|dkr&dd }n.|dkrL| tjd}t| ||||S dd }| ||||S )Nr   c                 S   s   t t | dS r;  )r  r!  hstackrR  r:   r:   r;   np_vstack_impl  s    z3_np_vstack.<locals>.codegen.<locals>.np_vstack_implr=   c                 S   s   t j| ddS )Nr   r0  r1  rR  r:   r:   r;   rZ    s    rT  )rg   r5   rK   r   r  r[   rZ  r   r:   r:   r;   r    s    


z_np_vstack.<locals>.codegenrU  rV  r:   r:   r;   
_np_vstack  s    r[  c                 C   s   t | tjrdd }|S d S )Nc                 S   s   t | S r  )r[  rK  r:   r:   r;   r     s    zimpl_np_vstack.<locals>.implrE  rL  r:   r:   r;   impl_np_vstack  s    r\  c                 C   s&   t | d|d}||}dd }||fS )Nz	np.dstackr  c                 S   s   |j d }|j}|d j}|dkr:dd }| ||||S |dkr| tjd}|j|jd d}	tj	|	f|j  }
t
| ||
||}| tjd}t	||	}t| |||f|S |dkr| tjd}t
| ||||S dd }| ||||S d S )Nr   c                 S   s   t | dddS )Nr=   r  )r  rY  r(  rR  r:   r:   r;   rZ    s    z3_np_dstack.<locals>.codegen.<locals>.np_vstack_implr=   rB  r  c                 S   s   t j| ddS )Nr  r0  r1  rR  r:   r:   r;   rZ    s    )r   r   r[   r  r   r   ru   r  r   r    rN  r!  )rg   r5   rK   r   r  r#  r[   rZ  r   Zstack_rettyZ	stack_sigZ	stack_retrI  r:   r:   r;   r    s*    


z_np_dstack.<locals>.codegenrU  rV  r:   r:   r;   
_np_dstack  s    "r]  c                 C   s   t | tjrdd }|S d S )Nc                 S   s   t | S r  )r]  rK  r:   r:   r;   r     s    zimpl_np_dstack.<locals>.implrE  rL  r:   r:   r;   impl_np_dstack  s    r^  fillc                 C   s   dd }|S )Nc                 S   s   || d d < d S r  r:   )rt  rq   r:   r:   r;   	fill_impl$  s    zarr_fill.<locals>.fill_implr:   )rt  rq   r`  r:   r:   r;   arr_fill!  s    ra  dotc                 C   s   dd }|S )Nc                 S   s   t | |S r  )r  rb  )rt  otherr:   r:   r;   dot_impl-  s    zarray_dot.<locals>.dot_implr:   )rt  rc  rd  r:   r:   r;   	array_dot+  s    re  c                 C   s"   t | std|  dd }|S )NzCannot np.fliplr on %s typec                 S   s4   t | }|jdk rtd|d d d d ddf S )Nr  r  r  .r  r  r[   r   r  r  r:   r:   r;   r   9  s    

znp_flip_lr.<locals>.implrh  ri  r:   r:   r;   
np_flip_lr3  s    rh  c                 C   s"   t | std|  dd }|S )NzCannot np.flipud on %s typec                 S   s.   t | }|jdk rtd|d d ddf S )Nr=   zInput must be >= 1-d.r  .rf  rg  r:   r:   r;   r   J  s    

znp_flip_ud.<locals>.implrh  ri  r:   r:   r;   
np_flip_udD  s    ri  c                    sN   t |tjst|t|j tjtj d|} fdd}||fS )zY Creates a tuple of slices for np.flip indexing like
    `(slice(None, None, -1),) * sz` r  c                    sX   dd }t jg}tjf| }| t j}| }| |g}	| ||||	}
|
S )Nc                 S   s*   |}t | D ]}t||td d d}q|S )Nr  )r2   r*   r   )lengthempty_tupler   rY   r:   r:   r;   r   `  s    z6_build_flip_slice_tuple.<locals>.codegen.<locals>.impl)r   ru   r   r    r   Zget_constant_undefr  )rg   r5   r    r   r   Zinner_argtypesZ	inner_sigZll_idx_typerk  Z
inner_argsr   ry   Z
tuple_typer:   r;   r  _  s    

z(_build_flip_slice_tuple.<locals>.codegen)	r|   r   r  r   r  r   r   r  Zslice3_type)r  szrK   r  r:   rl  r;   _build_flip_slice_tupleU  s    

rn  c                 C   s&   t | tjstd|  dd }|S )NzCannot np.flip on %s typec                 S   s   t | j}| | S r  )rn  r[   )r  slr:   r:   r;   r   z  s    
znp_flip.<locals>.impl)r|   r   r}   r   r   ri  r:   r:   r;   np_flips  s    rp  c                 C   s   t | tjtjtjfr$d	dd}|S t |tjr>d
dd}|S t |tjrht |jjtjrhddd}|S t |tj	rt
dd |jD rddd}|S d S )Nr   c                 S   s   t jt | ||dS r/  )r  array_splitr  r   indices_or_sectionsr   r:   r:   r;   r     s
    znp_array_split.<locals>.implc                 S   sP   t | j| |\}}tt|d g| |g|| d   }tj| ||dS )Nr=   r0  )divmodrQ   r  Zcumsumr   rq  )r   rs  r   r  r  r   r:   r:   r;   r     s    c              	   S   sp   t | j}tdd| j|}g }d}|D ](}t||t||}|| |  |}q&|| t||t|d   |S Nznp.splitr   r   )r+   r[   r   r*   r   rU   r   rs  r   Z	slice_tupr   prevrp  rx   r:   r:   r;   r     s    
c                 s   s   | ]}t |tjV  qd S r  r|   r   r~   )r   tr:   r:   r;   r    s     z!np_array_split.<locals>.<genexpr>c              	   S   st   t | j}tdd| j|}g }d}t|D ](}t||t||}|| |  |}q*|| t||t|d   |S ru  )r+   r[   r   r   r*   r   rU   rv  r:   r:   r;   r     s    
)r   )r   )r   )r   )r|   r   r  ListTyper{  r~   ZIterableTypeZiterator_typer   r  r  r   rs  r   r   r:   r:   r;   np_array_split  s*    





r|  c                 C   sP   t | tjtjtjfr$ddd}|S t |tjr>ddd}|S t| ||dS d S )Nr   c                 S   s   t jt | ||dS r/  )r  splitr  rr  r:   r:   r;   r     s    znp_split.<locals>.implc                 S   s4   t | j| |\}}|dkr$tdtj| ||dS )Nr   z0array split does not result in an equal divisionr0  )rt  rQ   r   r  rq  )r   rs  r   ro  r  r:   r:   r;   r     s      r0  )r   )r   )r|   r   r  rz  r{  r~   r|  r{  r:   r:   r;   np_split  s    


r~  c                 C   s   | |k pt |ot |  S r  )r  isnanr  r:   r:   r;   	lt_floats  s    r  Fc                 C   s   | ||f}z
t | W S  tk
r   | dkrLtj|r8tnd|dd}|j}n$| dkrptj|r`tnd|d}|j}|t |< | Y S X dS )z6
    Get a sort implementation of the given kind.
    r'   NT)lt
is_argsortZis_np_arrayr(   )r  r  )	_sortsKeyErrorr'   Zmake_jit_quicksortr  Zrun_quicksortr(   Zmake_jit_mergesortZrun_mergesort)r  is_floatr  keyrl  funcr:   r:   r;   get_sort_func  s&    



r  z
array.sortc                    s<   |j d }tdt|jtjd  fdd}| ||||S )Nr   r'   )r  r  c                    s    |  d S r  r:   r  Z	sort_funcr:   r;   array_sort_impl  s    z#array_sort.<locals>.array_sort_impl)r   r  r|   re   r   r  r  )rg   r5   rK   r   r|  r  r:   r  r;   
array_sort   s    
r  c                 C   s   t | stddd }|S )NzArgument "a" must be array-likec                 S   s   |   }|  |S r  )r  rl  )r  r   r:   r:   r;   np_sort_impl  s    z"impl_np_sort.<locals>.np_sort_implrh  )r  r  r:   r:   r;   impl_np_sort  s    
r  zarray.argsortc           	         sb   |j \}}t|jt|jtjdd  fdd}|j|j d d d}|d d }| ||||S )NT)r  r  r  c                    s    | S r  r:   r  r  r:   r;   array_argsort_impl"  s    z)array_argsort.<locals>.array_argsort_implr=   r  )	r   r  r   r|   re   r   r  replacer  )	rg   r5   rK   r   r|  r  r  ZinnersigZ	innerargsr:   r  r;   array_argsort  s    
 r  c                 C   s   |j |j ks|jdkst|S )Nr  )r  rm  r   )rg   r5   r  r  rq   r:   r:   r;   array_to_array.  s    r  c                 C   s4   dd }t ||}| ||||g}t| ||j|S )Nc                 S   s
   |  dS r   )r  r9  r:   r:   r;   r   =  s    zarray0d_to_scalar.<locals>.impl)r    r  r   r   rg   r5   r  r  rq   r   rK   r   r:   r:   r;   array0d_to_scalar5  s    
r  c                 C   s4   dd }t ||}| ||||g}t| ||j|S )Nc                 S   s   t | d S r<  )r  r9  r:   r:   r;   r   H  s    z array_to_unichrseq.<locals>.impl)r    r  r   r   r  r:   r:   r;   array_to_unichrseqF  s    
r  c                 C   s   t dS )zR
    An intrinsic returning a derived array with the given shape and strides.
    Nr   r  rQ   r   r:   r:   r;   reshape_uncheckedS  s    r  c                    s   dd   fdd}|S )Nc                 S   s   t | tjotdd | D S )Nc                 s   s   | ]}t |tjV  qd S r  rx  r,  r:   r:   r;   r  ^  s     z>type_reshape_unchecked.<locals>.check_shape.<locals>.<genexpr>)r|   r   r  r  rQ   r:   r:   r;   r#  \  s    z+type_reshape_unchecked.<locals>.check_shapec                    sJ   t | tjsd S  |r  |s$d S t|t|kr8d S | jt|ddS )Nr  r  )r|   r   r}   r   r  r  r#  r:   r;   typer`  s    z%type_reshape_unchecked.<locals>.typerr:   )rg   r  r:   r  r;   type_reshape_uncheckedZ  s    	r  c                 C   s   |j d }|j}t|| ||d }t|| |}t||d }t||d }	t||j||	|j|jd |	 }
t
| |||
S )Nr   r=   r  r  )r   r   rc   r	   r   r   r   r   r   r   r   )rg   r5   rK   r   r   r#  r   r   rQ   r   r   r:   r:   r;   impl_shape_uncheckedl  s    
r  c                    s`   |d t jfkrtdd  ntdd  |d t jfkr@tdntdd d	 fdd	}|S )
Nc                 S   s   | j S r  r  r  rQ   r:   r:   r;   r#    s    zas_strided.<locals>.get_shapec                 S   s   |S r  r:   r  r:   r:   r;   r#    s    z*as_strided() strides argument is mandatoryc                 S   s   |S r  r:   )r  r   r:   r:   r;   get_strides  s    zas_strided.<locals>.get_stridesc                    s   t |  | || |} | S r  )r  )r  rQ   r   r#  r  r:   r;   as_strided_impl  s    z#as_strided.<locals>.as_strided_impl)NN)r   r'  r"   rC   )r  rQ   r   r  r:   r  r;   
as_strided  s    


r  c                    s  t |tjr>tt| jd tt| jd tdd n^t |tjrt |jtjrtt| jt	| tt| jt	| tdd n
t
dt|rtdd  nLt |tjrtdd  n2t |tjrt |jtjrtd	d  n
t
d
d fdd	}|S )Nr=   c                 S   s   | fS r  r:   window_shaper:   r:   r;   get_window_shape  s    z-sliding_window_view.<locals>.get_window_shapec                 S   s   | S r  r:   r  r:   r:   r;   r    s    z4window_shape must be an integer or tuple of integersc                 S   s   t t|S r  )r   r2   r  r   r[   r:   r:   r;   get_axis  s    z%sliding_window_view.<locals>.get_axisc                 S   s   t dd||gS )Nsliding_window_viewr   r   r  r:   r:   r;   r    s    c                    s    fdd|D S )Nc                    s   g | ]}t d d |qS )r  r   r  r  rB  r:   r;   r     s   z9sliding_window_view.<locals>.get_axis.<locals>.<listcomp>r:   r  r:   rB  r;   r    s    
z2axis must be None, an integer or tuple of integersc           
         s   |} ||| j }t|t|kr.td}}t| j D ](}t||| j| }t||| j| }q@| j }t||D ]n\}}|dk rtd|| |k rtd|| | d }t|||}t|||}t||| j| }|d7 }qzt| ||}	|	S )Nz2Must provide matching length window_shape and axisr   z-`window_shape` cannot contain negative valuesz4window_shape cannot be larger than input array shaper=   )	r[   r   r   r2   r*   rQ   r   r   r  )
r  r  r   Z	out_shapeZout_stridesrY   r   r)  Ztrimmedr;  r  r  Zshape_bufferZstride_bufferr:   r;   sliding_window_view_impl  s8    
z5sliding_window_view.<locals>.sliding_window_view_impl)N)r|   r   r~   r   r2   r[   r"   r  re   r   r   r   r   )r  r  r   r  r:   r  r;   r    s>    'r  c                 C   s   t | tjrdd }|S d S )Nc                 S   s6   | j dkrdS | j dkr&t| dS d}t|d S )Nr   Fr=   z[The truth value of an array with more than one element is ambiguous. Use a.any() or a.all())ry   r  r  r   )rt  r   r:   r:   r;   r     s    

zol_bool.<locals>.implr	  r  r:   r:   r;   ol_bool  s    	r  c                    sn   t |ttjfstdt |ttjfs4tdt | tjsJtd| jtt	  fdd}|S )Nz.The second argument "axis1" must be an integerz-The third argument "axis2" must be an integerr  c                    s`   t dd|}t dd|}|dk r,|7 }|dk r<|7 }t ||}t|||}t| |S )Nznp.swapaxesaxis1axis2r   )r   r*   r  r  )rt  r  r  Z
axes_tupleZ	axes_listr[   r:   r;   r     s    znumpy_swapaxes.<locals>.impl)
r|   r   r   r~   r   r   r}   r[   r   r2   )rt  r  r  r   r:   r  r;   numpy_swapaxes  s    


r  c                 C   s  t dd| j|}t| j}d||< tt||jD ]H\}\}}	|dkrL|	}
n"|	dkrZ|}
n||	krjtd|}
t|||
}q2t||| j| }t	| |} t	||}|}t
|dkrtt
|D ]}t||| j| }q|}t
|dkrtt
|D ]}t||| j|d |  }q|j| }t||f | | j}td d d f}t|D ]h}t|D ]V}| || |  }||| |  }||| |  }t|D ]}|||  ||< qq`qR|S )Nznp.take_along_axisr   r=   z*`arr` and `indices` dimensions don't matchr   )r   r[   r   rQ   r  r   r   r*   r  r  r   r2   rs  re   r   r?  )rt  r   r   ZNi_origZNk_origindices_broadcast_shapeZ	arr_shaperY   Zd1Zd2new_valZarr_broadcast_shapeNiNkJr   Znp_s_iikkZa_1dZ
indices_1dZout_1djr:   r:   r;   _take_along_axis_impl-  sX    
    
r  c                    s  t | tjstdt |tjs,tdt |jtjsDtdt|rRd}n| j}||jkrltdt	t
|jt|rfdd}nt|d t |tjstd	|j}|d
k r| j| }|d
k s|| jkrtdt	t
| t	t
|d | j fdd}|S )Nr  z.The second argument "indices" must be an arrayz'The indices array must contain integersr=   z;`indices` and `arr` must have the same number of dimensionsc                    s   t |  |ddd S )Nr   r:   )r  r5  rt  r   r   )r  r:   r;   take_along_axis_impl~  s    z1arr_take_along_axis.<locals>.take_along_axis_implr   zaxis must be a literal valuer   zaxis is out of boundsc                    s   t | || S r  )r  r  r  r  r  r:   r;   r    s    )r|   r   r}   r   r   re   r~   r   r[   r   r2   r   r  r  r   )rt  r   r   Zarr_ndimr  r:   r  r;   arr_take_along_axisi  s<    






r  T        c                 C   s   t | tjr`t | tjr$ddd}qt | tjr<ddd}qt | tjrTddd}qtdnlt| rt | j	tjrddd}qt | j	tjrdd	d}qt | j	tjrdd
d}qtdn
td|S )NTr  c                 S   s   | S r  r:   r  r  nanr:   r:   r;   r     s    znan_to_num_impl.<locals>.implc                 S   sF   t | r|S t | r(t t| jS t | rBt t| jS | S r  )r  r  isneginffinfor3   r@  isposinfr  r  r:   r:   r;   r     s    


c                 S   s*   t j| j|d}t j| j|d}t||S )N)r  )r  
nan_to_numr  r  complex)r  r  r  r  rG  r:   r:   r;   r     s    z4Only Integer, Float, and Complex values are acceptedc                 S   s   | S r  r:   r  r:   r:   r;   r     s    c           	      S   s   t | jj}t | jj}t | }|r4t |n|}|j}t|j	D ]J}t 
|| rd|||< qHt || r||||< qHt || rH|||< qH|S r  )r  r  re   r@  r  r  r  r  r2   ry   r  r  r  )	r  r  r  Zmin_infZmax_infx_outputZoutput_flatrY   r:   r:   r;   r     s    



c                 S   sD   t | }|rt |n|}t j|jd|d t j|jd|d |S )NF)r  r  )r  r  r  r  r  r  )r  r  r  r  r  r:   r:   r;   r     s
    
z4The first argument must be a scalar or an array-like)Tr  )Tr  )Tr  )Tr  )Tr  )Tr  )
r|   r   r  r~   r  r  r   r   r   re   )r  r  r  r   r:   r:   r;   nan_to_num_impl  s,    
r  )N)N)N)r  )r=   )N)N)NNN)NNN)N)N)N)N)N)N)N)N)r   )NNN)r  )N)N)N)r   )r   )r   )r   )F)NN)N)Tr  (  r'  r  r  r  Zllvmliter   Zllvmlite.irr   numpyr  Znumbar   r   Z
numba.corer   r   r   r	   r
   Znumba.np.numpy_supportr   r   r   r   r   r   r   r   r   r   Znumba.core.imputilsr   r   r   r   r   r   r   r   r   r   r   Znumba.core.typingr    Znumba.core.typesr!   Znumba.core.extendingr"   r#   r$   r%   r&   Z
numba.miscr'   r(   Znumba.cpythonr)   Znumba.cpython.unsafe.tupler*   r+   r,   Znumba.core.typing.npydeclr-   r  r.   r  r/   r0   r1   r<   r?   rc   ri   ro   rr   r{   r   r   r   r   r   r   r   r   r   ZArrayIteratorZBORROWEDr   r  r   r  r  r~   r   r  r  r  rM  Anyr  r   r  r}   r  r  r>  r  r(  r:  r>  rB  rM  rV  r  rq  ru  rx  ry  r{  r  r  r  r  r  r  r  r  r  r  r  Zbroadcast_shapesr  r>  r  r  r  r  r  r  ZVarArgr  r  r  r  Zlogspacer  Zrot90r  r  r"  r&  r'  r(  r*  rU   r2  r8  r+  r;  r<  rG  rI  r[  rZ  r\  rg  rQ   rj  uniquerp  r}  r|  rr  r  r  r  r  r  r  r  Z
MemoryViewr  r  r  r  r  r  r  r  r  r  r  r  r  r  rl  r  r  r  r  r  r  r  r  r  r  r  ZDTypeZNumberClassr  r  r  r  r  r  r  r  r  r  r  r  r  Bytesr  is_r  r  r  r  r  r  r?  rF  r  rZ  rV  r[  r]  r^  r_  r  rb  r`  rc  r?  re  rg  rd  rh  r  ri  ZNumpyNdIterTyperj  rk  rt  rm  rr  rz  r{  r  r~  r  rs  r  r  r  r=  r  r  r  r  r  r  r  r  r  r  r  rQ  r  r  r  r  r  r  r  eyer  Zdiagr  r  r  r  Zaranger  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  r  r  r  r   r  r  r  r   r!  r"  r#  r)  r'  Z
atleast_1dr-  Z
atleast_2dr.  Z
atleast_3dr/  r6  r=  r?  rA  rC  r,  rG  rH  rJ  Zcolumn_stackrM  rN  rO  stackrP  rQ  rW  rY  rX  r[  Zvstackr\  r]  Zdstackr^  ra  re  r  rh  r  ri  rn  fliprp  rq  r|  r}  r~  r  r  r  r  rl  r  Zargsortr  r  ZUnicodeCharSeqr  r  rX  ZNPTimedeltaZ
NPDatetimer  r  r  Ztype_callabler  r  libZstride_tricksr  r  r  r  r  r  r  Ztake_along_axisr  r  r  r:   r:   r:   r;   <module>   s  $4<	
E




D

(





22=QE +
*=%



*

*
9 


6


$
@









y
~

	
,




+
*





































7	7'	   "  JA X







O	0














&
.
C
'%
A

+
$4
))




=2Y
	%

	

)
	



>


^
;
+
