a
    
d                 	   @   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loadZlower_boundupper_boundZrange_operandsZmd r8   Z/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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   r7   r8   r8   r9   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y>   t | | Y S 0 | jj }| |}|d u rrt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_typer8   r9   rC   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)
rF   __getattr___get_ptr_by_namer2   r	   gep_inboundsr6   appendr=   
pack_array)rG   r5   ptrdimsiZdimptrr6   )basendimr8   r9   rO   `   s    


z%make_array.<locals>.ArrayStruct.shape)__name__
__module____qualname__rC   propertyrO   __classcell__r8   rK   rM   rX   rY   rN   )rL   r9   rK   J   s   rK   )Zas_arrayr	   create_struct_proxyrY   )rM   r8   r_   r9   
make_array>   s
    
-ra   c                 C   s   |  |j}| |S )zB
    Return the item size for the given array or buffer type.
    )get_data_typedtypeget_abi_sizeof)contextrM   lltyr8   r8   r9   get_itemsizez   s    rg   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_valuerc   )re   r5   arraytyrU   ri   r8   r8   r9   	load_item   s    rm   c                 C   s$   |j r
dnd}| j||j|||dS )z4
    Store the item at the given array pointer.
    Nr;   rh   )rj   
pack_valuerc   )re   r5   rl   valrU   ri   r8   r8   r9   
store_item   s    rp   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)re   r5   idxtyidxsizeindr8   r8   r9   fix_integer_index   s
    ry   c                 C   s^   t |tjrR|jdkrRt |jtjs(J 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   ArrayrY   rc   Integerra   rm   data)re   r5   ru   rv   idxaryZidxvalr8   r8   r9   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 r8   )r   ).0ru   rv   r5   re   r8   r9   
<listcomp>   s   z%normalize_indices.<locals>.<listcomp>)lenzip)re   r5   index_typesindicesr8   r   r9   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	u rt||	d
d	}|tj}t|ttfrt|||}t|ttfrt|||}t|tr||}t|||||d}|d	u 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)rO   stridesr}   itemsizer   parentnswflagsnitemszmissing {0}) r?   rF   
_datamodelr   r{   float64rD   set_fieldsZ_fe_typer   
ValueErrorr   get_value_typeget_typers   rz   tuplelistr	   rT   intdictget_constantunpack_tupler3   countmulkeysformatitemssetattr)arrayr}   rO   r   r   r   r   re   r5   rJ   Zstandard_arrayZstandard_array_type_datamodelZrequired_fieldsZdatamodel_fieldsmissingmsgintp_tattrsr   unpacked_shapeaxlenZ
got_fieldskvr8   r8   r9   populate_array   sh    







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)r?   rF   r   r   rs   r	   r   rO   rY   r   r   rg   r   )arytyr   re   r5   r   r   r   r8   r8   r9   update_array_info   s    r   c                 C   s
   t  dS )z/Constrain axis values to valid positive values.NrA   	func_namearg_namerY   axisr8   r8   r9   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   r8   r9   impl"  s
    z&normalize_axis_overloads.<locals>.impl)rz   r!   r   TypingErrorliteral_value)r   r   rY   r   r   r8   r   r9   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   rs   r	   alloca_once_valueindexr   
enable_nrtnrtincref	_getvaluer   )re   r5   rI   r   rl   r   iterobjzeroZindexptrresoutr8   r8   r9   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 |
rJ t| |||	S dS )z1 Evaluate `ary[idx]`, where idx is a single int. r   r   r;   N)r	   r   rO   rY   r   r   pointer_addr}   rz   r   Buffer	make_viewr   rm   )re   r5   r   r   aryrv   shapesr   offsetdataptrview_shapesview_stridesretaryr8   r8   r9   _getitem_array_single_intE  s    r   Ziternextc                 C   s   |j \}|\}|j}| j|||d}t|| ||jd}	tj||	jdd\}
||j	}|
d||
}|| ||H t| ||j||	|}|| t||}|||j	 W d    n1 s0    Y  d S )Nvaluer;   r   <)r   rM   r   ra   r   r	   r   rO   r6   r   icmp_signed	set_validif_thenr   
yield_typeyield_increment_indexstore)re   r5   rI   r   resultZitertyiterrl   r   r   r   r   is_validr   Znindexr8   r8   r9   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
u 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sJ ||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 r8   r   r   rv   r8   r8   r9   r         z"basic_indexing.<locals>.<listcomp>r   unexpected index type: %sF
wraparoundboundscheck)r   r   rs   r	   r   rO   rY   r   r   r   ellipsisr2   rS   rz   	SliceTyper   r)   guard_invalid_slice	fix_slicestartget_slice_lengthZ
fix_strider|   ry   Zdo_boundscheckr   rA   get_item_pointer)re   r5   r   r   r   r   r   r   oner   r   Zoutput_indicesZoutput_shapesZoutput_stridesnum_newaxesaxindexvalru   	n_missingrW   sliceshstrx   r   r8   r8   r9   basic_indexingv  sd    









r   c           	   	   C   s.   t || |}t|||||j|j|jd |S )zF
    Build a view over the given array with the given parameters.
    r}   rO   r   r   r   r   )ra   r   r   r   r   )	re   r5   r   r   r   r}   r   r   r   r8   r8   r9   r     s    r   c              	   C   sb   t | |||||| jd\}}}	t|tjrHt| |||||||	}
|
 S |rPJ 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_boundscheckrz   r   r   r   r   rm   )re   r5   r   r   r   r   r   r   r   r   r   r8   r8   r9   _getitem_array_generic  s    r  c           	      C   sZ   |j \}}|\}}|jdks J t|| ||}t| ||j|||f|f}t| ||j|S )z4
    Basic indexing with an integer or a slice.
    r;   )r   rY   ra   r  r   r   	re   r5   rI   r   r   ru   r   rv   r   r8   r8   r9   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rz   r   r{   )r   tyr8   r8   r9   	<genexpr>  r   z&getitem_array_tuple.<locals>.<genexpr>)r   ra   r   r	   r   r   r   anyfancy_getitemr  r   r   )re   r5   rI   r   r   tuptyr   tupr   r   r   r8   r8   r9   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y   d}Y n
0 t|}|rt| ||||
|S | ||	||j}	t| |||	| dS )zH
    array[a] = scalar_or_array
    array[a,..,b] = scalar_or_array
    r   r  TN)r   rz   r   	BaseTupler	   r   r   ra   r   r   r  rA   boolfancy_setslicerr   rc   rp   )re   r5   rI   r   r   ru   valtyr   rv   ro   r   r   r   r   r   Zuse_fancy_indexingr8   r8   r9   setitem_array	  s2    


r  c           	      C   sD   |j \}|\}t|}|| ||}|j}||d}t| ||j|S r   )r   ra   rO   extract_valuer   r   )	re   r5   rI   r   r   r   ZarysttyZshapearyr   r8   r8   r9   	array_len2  s    r  z
array.itemc                 C   s   |j \}|\}t|| ||}|j}|j|d||ddd& d}| j|t|f W d    n1 sn0    Y  t	| |||j
S )N!=r;   FZlikelyz>item(): can only convert an array of size 1 to a Python scalar)r   ra   r   r   r   r3   	call_convreturn_user_excr   rm   r}   )re   r5   rI   r   r   r   r   r   r8   r8   r9   
array_item=  s    0r  zarray.itemsetc           
      C   s   |j \}}|\}}||jks J t|| ||}|j}|j|d||ddd& d}	| j|t	|	f W d    n1 s0    Y  t
| ||||j |  S )Nr  r;   Fr  z/itemset(): can only write to an array of size 1)r   rc   ra   r   r   r   r3   r  r  r   rp   r}   get_dummy_value)
re   r5   rI   r   r   r  r   ro   r   r   r8   r8   r9   array_itemsetL  s    
0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   rG   r8   r8   r9   prepareg  s    zIndexer.preparec                 C   s   t dS )z=
        Return this dimension's size as an integer.
        Nr   r  r8   r8   r9   get_sizen  s    zIndexer.get_sizec                 C   s   t dS )z;
        Return this dimension's shape as a tuple.
        Nr   r  r8   r8   r9   	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  r8   r8   r9   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  r8   r8   r9   	loop_head  s    	zIndexer.loop_headc                 C   s   t dS )z)
        Finish indexation loop.
        Nr   r  r8   r8   r9   	loop_tail  s    zIndexer.loop_tailN)
rZ   r[   r\   __doc__r  r   r!  r"  r#  r$  r8   r8   r8   r9   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  )	re   r5   r   r   dimr   r   rs   ll_intp)rG   re   r5   r   r   r'  r8   r8   r9   __init__  s    zEntireIndexer.__init__c                 C   sB   | j }|| jj| j| _t|| j| _	|
 | _|
 | _d S r  )r5   r  r   rO   r'  rw   r	   rE   r(  r   append_basic_blockbb_startbb_endrG   r5   r8   r8   r9   r    s
    
zEntireIndexer.preparec                 C   s   | j S r  rw   r  r8   r8   r9   r     s    zEntireIndexer.get_sizec                 C   s   | j fS r  r.  r  r8   r8   r9   r!    s    zEntireIndexer.get_shapec                 C   s   |  d| jfS r   r(  rw   r  r8   r8   r9   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 d    n1 s~0    Y  ||fS Nr   >=Fr  )r5   r   r   r(  r   branchr+  position_at_endr6   r   r   rw   r,  )rG   r5   	cur_indexr8   r8   r9   r#    s    *zEntireIndexer.loop_headc                 C   sD   | j }t||| j}||| j || j || j	 d S r  )
r5   r	   r   r6   r   r   r2  r+  r3  r,  rG   r5   
next_indexr8   r8   r9   r$    s
    zEntireIndexer.loop_tailNrZ   r[   r\   r%  r)  r  r   r!  r"  r#  r$  r8   r8   r8   r9   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  )re   r5   rv   r   r   rs   r(  )rG   re   r5   rv   r8   r8   r9   r)    s    zIntegerIndexer.__init__c                 C   s   d S r  r8   r  r8   r8   r9   r    s    zIntegerIndexer.preparec                 C   s   t | jdS Nr;   )r   r(  r  r8   r8   r9   r     s    zIntegerIndexer.get_sizec                 C   s   dS Nr8   r8   r  r8   r8   r9   r!    s    zIntegerIndexer.get_shapec                 C   s   | j | j| j |  fS r  )rv   r5   addr   r  r8   r8   r9   r"    s    zIntegerIndexer.get_index_boundsc                 C   s
   | j d fS r  )rv   r  r8   r8   r9   r#    s    zIntegerIndexer.loop_headc                 C   s   d S r  r8   r  r8   r8   r9   r$    s    zIntegerIndexer.loop_tailNr7  r8   r8   r8   r9   r8    s   r8  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,J | j tj| _	d S r9  )
re   r5   ru   r~   rw   rY   r   r   rs   r(  )rG   re   r5   ru   r~   rw   r8   r8   r9   r)    s    zIntegerArrayIndexer.__init__c                 C   sD   | j }t|| jjd | _t|| j| _|	 | _
|	 | _d S r   )r5   r	   r   r~   rO   idx_sizerE   r(  	idx_indexr*  r+  r,  r-  r8   r8   r9   r    s
    
zIntegerArrayIndexer.preparec                 C   s   | j S r  r=  r  r8   r8   r9   r     s    zIntegerArrayIndexer.get_sizec                 C   s   | j fS r  r?  r  r8   r8   r9   r!    s    zIntegerArrayIndexer.get_shapec                 C   s   |  d| jfS r   r/  r  r8   r8   r9   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 d    n1 s~0    Y  t| j|| jj| j| j|}t| j|| jj|| j}||fS r0  )r5   r   r   r(  r>  r2  r+  r3  r6   r   r   r=  r,  r   re   ru   rc   r~   ry   rw   )rG   r5   r4  r   r8   r8   r9   r#    s$    *zIntegerArrayIndexer.loop_headc                 C   sD   | j }t||| j}||| j || j || j	 d S r  )
r5   r	   r   r6   r>  r   r2  r+  r3  r,  r5  r8   r8   r9   r$    s    
zIntegerArrayIndexer.loop_tailNr7  r8   r8   r8   r9   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&J | j tj| _t	| jd| _
d S Nr;   r   )re   r5   ru   r~   rY   r   r   rs   r(  r   r   )rG   re   r5   ru   r~   r8   r8   r9   r)  &  s    zBooleanArrayIndexer.__init__c                 C   s^   | j }t|| jjd | _t|| j| _t|| j| _	|
 | _|
 | _|
 | _d S r   )r5   r	   r   r~   rO   rw   rE   r(  r>  r   r*  r+  bb_tailr,  r-  r8   r8   r9   r  /  s    

zBooleanArrayIndexer.preparec                 C   s   | j }t|| j}t|| jZ}||}t| j|| j	j
| j	| j|j}|||||j}||| W d    n1 s0    Y  ||S r  )r5   r	   r   r   	for_rangerw   r6   r   re   ru   rc   r~   r   r;  Zzextr3   r   )rG   r5   r   loopcpredr8   r8   r9   r   8  s    
*zBooleanArrayIndexer.get_sizec                 C   s
   |   fS r  r   r  r8   r8   r9   r!  G  s    zBooleanArrayIndexer.get_shapec                 C   s   |  d| jfS r   r/  r  r8   r8   r9   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 d    n1 s0    Y  t| j|| jj| j| j|}|	|| || j W d    n1 s0    Y  t||}||| j ||fS )Nr1  Fr  )r5   r   r   r>  r   r2  r+  r3  r6   r   r   rw   r,  r   re   ru   rc   r~   not_rB  r	   r   )rG   r5   r4  	cur_countrF  
next_countr8   r8   r9   r#  O  s(    **zBooleanArrayIndexer.loop_headc                 C   s\   | j }|| j || j t||| j}||| j || j	 || j
 d S r  )r5   r2  rB  r3  r	   r   r6   r>  r   r+  r,  r5  r8   r8   r9   r$  g  s    
zBooleanArrayIndexer.loop_tailNr7  r8   r8   r8   r9   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 )SliceIndexerz(
    Compute indices along a slice.
    c                 C   sL   || _ || _|| _|| _|| _|| _|| _| j tj	| _
t| j
d| _d S r   )re   r5   r   r   r'  ru   r   r   r   rs   r(  r   r   )rG   re   r5   r   r   r'  ru   r   r8   r8   r9   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   rO   r'  dim_sizer)   r   re   ru   r   r   r	   Z
is_neg_intstepis_step_negativerE   r(  r   r   r*  r+  r,  r-  r8   r8   r9   r    s    
zSliceIndexer.preparec                 C   s   t | j| jS r  )r)   r   r5   r   r  r8   r8   r9   r     s    zSliceIndexer.get_sizec                 C   s
   |   fS r  rG  r  r8   r8   r9   r!    s    zSliceIndexer.get_shapec                 C   s   t | j| j\}}||fS r  )r)   Zget_slice_boundsr5   r   )rG   lowerupperr8   r8   r9   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 d    n1 s0    Y  ||fS )Nz<=r1  Fr  )r5   r   r   r   r   r   r   r2  r+  r3  r6   selectrN  r   stopr   r,  )rG   r5   r4  rI  Zis_finishedr8   r8   r9   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   rM  r   r	   r   r   r2  r+  r3  r,  )rG   r5   r6  rJ  r8   r8   r9   r$    s    zSliceIndexer.loop_tailNr7  r8   r8   r8   r9   rK  r  s   rK  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u 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sJ || n.t!|r| j|
 |	d8 }	nt"d|f |	d7 }	|
d7 }
qx|	|jks*J |	|jf|	|jk r\t|||||	}|| |	d7 }	q*t||jks~J t||jf|| _#d S )Nc                 S   s   g | ]}t |r|qS r8   r   r   r8   r8   r9   r     r   z)FancyIndexer.__init__.<locals>.<listcomp>r   r;   r   )$re   r5   r   r	   r   rO   rY   r   r   r   r   rs   r(  newaxesr   r   r   r2   r&  rS   rz   r   r   rK  r|   ry   r8  r{   ra   rc   r<  Booleanr@  r   AssertionErrorindexers)rG   re   r5   r   r   r   r   rW  r   r   Znew_axr   ru   r   rW   indexerr   rx   r~   r8   r8   r9   r)    st    








"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 r8   )r!  r   rW   r8   r8   r9   r     r   z(FancyIndexer.prepare.<locals>.<listcomp>r8   )
rW  r  re   r   r   rs   rT  insertsumindexers_shape)rG   rW   r   Z	res_shaper8   r8   r9   r    s    


zFancyIndexer.preparec                 C   s   | j S )z?
        Get the resulting data shape as Python tuple.
        )r\  r  r8   r8   r9   r!    s    zFancyIndexer.get_shapec              	   C   s  t || jjksJ | 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   rY   r5   r	   	false_bitr(  r   rW  r\  or_icmp_unsignedr"  r   subr   r;  rQ  )rG   r   r   r5   is_emptyr   r   rO  rP  rX  rO   strideZlower_indexZupper_indexZlower_offsetZupper_offsetZis_downwardsr8   r8   r9   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#  rY  r8   r8   r9   r	  >  r   z+FancyIndexer.begin_loops.<locals>.<genexpr>)r   rW  )rG   r   countsr8   r8   r9   begin_loops=  s    zFancyIndexer.begin_loopsc                 C   s   t | jD ]}|  q
d S r  )reversedrW  r$  )rG   rW   r8   r8   r9   	end_loopsA  s    zFancyIndexer.end_loopsN)
rZ   r[   r\   r%  r)  r  r!  rd  rf  rh  r8   r8   r8   r9   rS    s   B&rS  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   rO   r   r}   rS  r  r   r!  _empty_nd_implr   r   r   rs   rf  get_item_pointer2layoutr  rm   r6   geprp   r   r   rh  r   r   )re   r5   rI   r   r   r   r   r   r   r   r}   rX  Zout_tyZ
out_shapesr   Zout_dataZout_idx_rU   ro   curZnext_idxr8   r8   r9   r  F  s6    

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   ra   rY   r   r  r   r   r  r  r8   r8   r9   fancy_getitem_arrayq  s    



ro  c              	   C   s   |j }|d}|d}|jdv 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;   CFr1  r]  )r   r3   rk  r   r   r2   rY   ra  r   rQ  r;  )re   r5   arrtyarrr   r   r   r   r   rO  rP  rW   Zmax_axis_offsetZ
is_upwardsrb  r8   r8   r9   offset_bounds_from_strides  s0    



rs  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;  )re   r5   rO  rP  r}   Zdata_ptr_as_intr   endr8   r8   r9   compute_memory_extents  s    rv  c           	      C   s&   t | |||||\}}t| ||||S )zr
    Compute a half-open range [start, end) of pointer-sized integers
    which fully contain the array data.
    )rs  rv  )	re   r5   rq  rr  r   r   r}   rO  rP  r8   r8   r9   get_array_memory_extents  s    
rw  c                 C   s$   | |d|||d||}|S )z[
    Whether two memory extents [a_start, a_end) and [b_start, b_end)
    may overlap.
    r   )and_r`  )re   r5   Za_startZa_endZb_startZb_endZmay_overlapr8   r8   r9   extents_may_overlap  s
    ry  c                    s@  j dt d  jdd  |j|j}j |}	|	j  	|	}	 
|	 tj}
t |
P}t 	
j|}t |	|} 
 || W d    n1 s0    Y  W d    n1 s0    Y   	
fdd} fdd}||fS )Nr   Fr  c                    s   t  } jdd\}}|8  t j  | dd| W d    n1 sb0    Y  |4  t j 	
j| dd| W d    n1 s0    Y  W d    n1 s0    Y  t 
 |S )NFr  r   )r	   rE   if_elser   rj  r6   rk  rm   )source_indicessrc_ptrZif_copyZ	otherwise)r5   re   	copy_datacopy_layoutcopy_shapescopy_stridesptrtysrc_data
src_shapessrc_stridessrctyuse_copyr8   r9   src_getitem  s,    "
@z&maybe_copy_source.<locals>.src_getitemc                     sV    j dd4  }  | } j |  W d    n1 sH0    Y  d S )NFr  )r   r6   bitcastr   freer}   )r5   re   r~  r  	voidptrtyr8   r9   src_cleanup  s    
z&maybe_copy_source.<locals>.src_cleanup)r3   r	   r   r   r   r   r   r   allocater  r   r   r   rs   	loop_nestrj  rk  r6   )re   r5   r  r  srcr  r  r  	allocsizer}   r   r   r}  dest_ptrr  r  r8   )r5   re   r~  r  r  r  r  r  r  r  r  r  r  r9   maybe_copy_source  s2    

N"r  c           
         s  |  tjd}|  tjdt|t|krZt|t| }g| | }|g| | }nt|t|k rt|t| } fdd|d| D }t j|tj} j	 
|dd& d}	| j t|	f W d   n1 s0    Y  ||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]  )r`  r   r   r5   r   r8   r9   r   %  s   z(_bc_adjust_dimension.<locals>.<listcomp>NFr  z,cannot broadcast source array for assignment)r   r   rt   r   	functoolsreducerx  r	   true_bitr   rH  r  r  r   )
re   r5   r   r   target_shaper   Znd_diffZ
dim_is_oneacceptedr   r8   r  r9   _bc_adjust_dimension  s(    	

0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   r8   r9   r   @  s   z,_bc_adjust_shape_strides.<locals>.<listcomp>c                    s   g | ]}  d |qS r  r  )r   r  r  r8   r9   r   B  r   c                    s   g | ]\}}  ||qS r8   )rx  )r   xyr  r8   r9   r   C  r   c                    s    g | ]\}}}  |||qS r8   rQ  )r   pr  r  r  r8   r9   r   D  s   c                    s   g | ]\}}  ||qS r8   r  )r   r  r  )r5   r   r8   r9   r   F  s   )r   r   rt   r   )
re   r5   r   r   r  Z	bc_shapesZ
bc_stridesmismatchZ
src_is_onepredsr8   )r5   r   r   r9   _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rY   rk  r   )r	   r   rO   r   r  r  copyr   ra   r   r}   rT   r   r   r   )	re   r5   arrtyperr  r  r   r   Znew_arrtypeZnew_arrr8   r8   r9   _broadcast_to_shapeK  s&    


r  c                 C   s,   |j |jddd}|||}dd }||fS )Nr  T)rY   rk  readonlyc           
   	   S   sz   |\}}|j d }t|| ||}t||}t| ||||\}}t|dt| |j	dd  |
 }	t| ||j|	S )Nr   r   )r   ra   r	   r   r  r   r   r   r   r   r   r   r   )
re   r5   rI   r   r  Zshape_r  rm  destr   r8   r8   r9   codegenj  s    
z$_numpy_broadcast_to.<locals>.codegen)r  r   )	typingctxr   rO   retrI   r  r8   r8   r9   _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   ra   r	   Zget_null_valuer   r3   r   r   r   )re   r5   rI   r   r  r  r  r   r8   r8   r9   r    s    
z#get_readonly_array.<locals>.codegen)r  )r  rr  r  rI   r  r8   r8   r9   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)rO   r   r   )
r   Z
dest_shapeZ	src_shapeZsrc_ndimZ	dest_ndimrw   Z	src_indexZ
dest_indexZsrc_dimZdest_dimr8   r8   r9   _can_broadcast  s"    

r  c                 C   s   t | } t| | t| |S r  )npasarrayr  r  r   rO   r8   r8   r9   _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  r8   r8   r9   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 r8   )rz   r   IntegerLiteral)r   typr8   r8   r9   r     r   z&numpy_broadcast_to.<locals>.<listcomp>"z," object cannot be interpreted as an integerc                 S   s   t | } t| S r  )r  r  r  r  r8   r8   r9   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   rz   r   r|   UniTuplerc   r  Tupler   allr{   rY   r   )r   rO   r   r   Zis_scalar_arrayr8   r8   r9   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mrO   rW   r   tmpr8   r8   r9   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:  r8   r   r8   r8   r9   <lambda>  r   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 rA  )r   r   r   r  r   	enumerater*   )r   r  r  argrv   elemr  r  tup_initr8   r9   r     s    

z'ol_numpy_broadcast_shapes.<locals>.impl)r  rz   r   r|   r  rc   r  r   r   r   maxr  Znumba.cpython.builtinsr  )r   rv   r  Zis_intZis_int_tupleZis_empty_tupler   r   r8   r  r9   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 u 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 r9  )	r   r  r  r  rO   r2   r*   rS   r  )r   rO   r   r  rW   Zoutsr  r  r8   r9   r   >  s    
z$numpy_broadcast_arrays.<locals>.impl)r  r   r   r   rz   r   r{   r  rc   ZArrayCompatibler  rY   NumberrU  )r   rv   r  Zunified_dtypedtr   r8   r  r9   numpy_broadcast_arrays  s2    
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sJ t||D ]\}} | d||}q j|dd& d}j t|f W d   n1 sD0    Y  t |||\}}||
|j\}}t |||\}}t ||||}t ||||\}}nt	t
jr~j| }t|dksJ tt t
j!}| f} d|d |} j|dd& d}j t|f W d   n1 sV0    Y   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 sizeNr;   r   c                    s,   | \} tjttj}| |fS r  )r@   operatorgetitemr    r   rs   )r|  rv   getitem_implr5   re   r  Z	src_dtyper  r8   r9   r    s    z#fancy_setslice.<locals>.src_getitemc                   S   s   d S r  r8   r8   r8   r8   r9   r    s    z#fancy_setslice.<locals>.src_cleanupc                    s    S r  r8   )r|  )r  r8   r9   r    s    c                   S   s   d S r  r8   r8   r8   r8   r9   r    s    c                 S   s   g | ]}|d ur|qS r  r8   )r   rE  r8   r8   r9   r     r   z"fancy_setslice.<locals>.<listcomp>r   )/r   ra   r	   r   rO   r   r}   rS  r  rz   r   r   rc   r!  r  r^  r   r   r_  r   r   r  r  r   rw  rd  r   rv  ry  r  Sequencer@   r    rs   r   rt   rf  r   rT  rZ  rr   rj  rk  r  rp   rh  r  )'re   r5   rI   r   r   r   r   rm  r   Zdest_shapesdest_strides	dest_datarX  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_indicesre  rW   r|  ro   r  r8   r  r9   r  U  s    


2

2


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 r8   rr   r   rs   )r   r  ro   r   r8   r9   r     s   z#vararg_to_tuple.<locals>.<listcomp>rc   r   )r   r   r	   rT   r3   r   r  rs   r   r   r    r   )re   r5   rI   r   r   Zdimtysr   rV   rO   shapetynew_signew_argsr8   r   r9   vararg_to_tuple  s    
r  zarray.transposec                 C   s   t | ||jd |d S r   )array_Tr   re   r5   rI   r   r8   r8   r9   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   rO   r   r'  r  r8   r8   r9   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 r8   r	   rE   r   rm  )r5   ll_ary_sizer8   r9   r     r   z)array_transpose_tuple.<locals>.<listcomp>r   rc   rY   rk  c                    s   g | ]}t  qS r8   ra   r  )r5   re   	np_ary_tyr8   r9   r      r   r}   rO   r   r   r   c                 S   s   g | ]}|  qS r8   r   r   ar8   r8   r9   r   0  r      r   ) r   ra   r   rc   r   r   rs   r   	ArrayTyperO   r   r   r   r{   r   rd   r   r  
as_pointercompile_internalr  r   r    voidr   r}   r6   r   r   r   r   r   )re   r5   rI   r   r   r   Zaxistyr   Znum_axisrc   r(  ZarysZll_arysr  dstZnp_itemsizeZnp_arysZnp_aryZll_aryr  r   r8   )r5   re   r  r  r9   array_transpose_tuple  sP    
	r  c                 C   s    t | |||\}}t| |||S r  )r  r  re   r5   rI   r   r  r  r8   r8   r9   array_transpose_vararg?  s    r  c                 C   s8   t | tjrtd|d u r*ddd}n
ddd}|S )Nz#np.transpose does not accept tuplesc                 S   s   |   S r  	transposer  axesr8   r8   r9   np_transpose_implK  s    z*numpy_transpose.<locals>.np_transpose_implc                 S   s
   |  |S r  r  r  r8   r8   r9   r  N  s    )N)N)rz   r   r  r   ZUnsupportedError)r  r   r  r8   r8   r9   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   )rY   ra   r	   r   rO   r   r   r}   rT   r   r   r   r   r   )	re   r5   r  r   r   r   r  r   r   r8   r8   r9   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   rR  numr  r8   r8   r9   r   q  s    znumpy_logspace.<locals>.impl)r  )rz   r   r  r   r   r   r|   )r   rR  r  r   r8   r8   r9   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flipudrV  )rr  r   r8   r8   r9   r     s    znumpy_rot90.<locals>.impl)r;   )	rz   r   r   r|   r   r   r{   rY   NumbaValueError)rr  r   r   r8   r8   r9   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_reshaperO   r   r   )r   r   rs   r  Zintcr   FunctionTyper	   Zget_or_insert_functionr4   rY   rR   rQ   callr   )re   r5   r   r   newndnewshape
newstridesr(  Zll_intp_starZll_intcfntyfnndrO   r   Z
is_f_orderr   r8   r8   r9   _attempt_nocopy_reshape  s2    	

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   )	origsizerO   Znum_neg_valueZ
known_sizer   r   Zneg_axinferredokr8   r8   r9   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 d    n1 sj0    Y  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   rs   r   r  r   ra   r	   rE   r   r{   rc   r   rd   r   r  r  r   r  r  r   r    r  rt   r   r  r`  r3   r   r  r  rA   r}   r6   r   r   r   r   )re   r5   rI   r   r   rettyr  rO   r(  Zll_shaper   r  Zshape_ary_ty	shape_aryZshape_itemsizerw   r  r  r  failr   r  r   r8   r8   r9   array_reshape  s`    


2r#  c                 C   s    t | |||\}}t| |||S r  )r  r#  r  r8   r8   r9   array_reshape_vararg  s    r$  c                 C   s   dd }|S )Nc                 S   s
   |  |S r  )reshape)r  rO   r8   r8   r9   np_reshape_impl  s    z#np_reshape.<locals>.np_reshape_implr8   )r  rO   r&  r8   r8   r9   
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rr  valuesr   r8   r8   r9   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*  r8   r8   r9   r   8  s    )N)N)r   r   r   r   rz   r   r|   )rr  r+  r   r   r8   r8   r9   	np_append#  s    



r/  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%  rw   r   r8   r8   r9   
imp_nocopy@  s    zarray_ravel.<locals>.imp_nocopyc                 S   s   |   S )zCopy version)flattenr0  r8   r8   r9   imp_copyD  s    zarray_ravel.<locals>.imp_copyr   r   )r   rk  r  r   r   )re   r5   rI   r   r1  r3  impr   r8   r8   r9   array_ravel=  s    r5  c                 C   s   dd }|  ||||S )Nc                 S   s   |   S r  )r(  r  r8   r8   r9   np_ravel_implV  s    znp_ravel.<locals>.np_ravel_impl)r  )re   r5   rI   r   r7  r8   r8   r9   np_ravelT  s    r8  zarray.flattenc                 C   s,   dd }|  ||||}t| ||j|}|S )Nc                 S   s   |   | jS r  )r  r%  rw   r0  r8   r8   r9   r4  _  s    zarray_flatten.<locals>.imp)r  r   r   )re   r5   rI   r   r4  r   r8   r8   r9   array_flatten\  s    r9  c                 C   sl   |d u rt | n|}t | ||\}}}t |jD ]0}|| }	|| }
|| }tt|	|
|||< q6|S r  )r  
empty_likebroadcast_arraysndindexrO   minr  )r  a_mina_maxr   r  a_ba_min_ba_max_br   val_aZ	val_a_minZ	val_a_maxr8   r8   r9   _np_clip_implg  s    rD  c                 C   sH   t | jD ]6}| | }|| }|r4t||||< qt||||< q|S r  )r  r<  rO   r=  r  )r  bZuse_minr   r   rC  Zval_br8   r8   r9   _np_clip_impl_noneu  s    rF  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 u pt|tj}|d u 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  r>  r?  r   r8   r8   r9   
np_clip_nn  s    znp_clip.<locals>.np_clip_nnc                 S   sH   |d u rt | n|}t | jD ] }| | }tt|||||< q"|S r  )r  r:  r<  rO   r=  r  r  r>  r?  r   r  r   rC  r8   r8   r9   
np_clip_ss  s
    znp_clip.<locals>.np_clip_ssc                 S   sB   |d u rt | n|}t | jD ]}| | }t||||< q"|S r  )r  r:  r<  rO   r  rJ  r8   r8   r9   
np_clip_sn  s
    znp_clip.<locals>.np_clip_snc                 S   s   t | |}t| |||S r  r  	full_likerD  )r  r>  r?  r   Z
a_min_fullr8   r8   r9   
np_clip_sa  s    znp_clip.<locals>.np_clip_sac                 S   sB   |d u rt | n|}t | jD ]}| | }t||||< q"|S r  )r  r:  r<  rO   r=  rJ  r8   r8   r9   
np_clip_ns  s
    znp_clip.<locals>.np_clip_nsc                 S   s   t | |}t| |||S r  rM  )r  r>  r?  r   Z
a_max_fullr8   r8   r9   
np_clip_as  s    znp_clip.<locals>.np_clip_asc                 S   s4   |d u rt | n|}t | |\}}t||d|S )NTr  r:  r;  rF  )r  r>  r?  r   r  r@  rB  r8   r8   r9   
np_clip_na  s    znp_clip.<locals>.np_clip_nac                 S   s4   |d u rt | n|}t | |\}}t||d|S )NFrR  )r  r>  r?  r   r  r@  rA  r8   r8   r9   
np_clip_an  s    znp_clip.<locals>.np_clip_anc                 S   s   t | |||S r  )rD  rH  r8   r8   r9   
np_clip_aa  s    znp_clip.<locals>.np_clip_aa)N)N)N)N)N)N)N)N)N)	r   r   r   rz   r   NoneTyper{   r   r  )r  r>  r?  r   r   Za_min_is_noneZa_max_is_nonerI  Za_min_is_scalarZa_max_is_scalarrK  rL  rO  rP  rQ  rS  rT  rU  r8   r8   r9   np_clip  sV    












rW  clipc                 C   s   ddd}|S )Nc                 S   s   t | |||S r  )r  rX  rH  r8   r8   r9   r     s    zarray_clip.<locals>.impl)NNNr8   )r  r>  r?  r   r   r8   r8   r9   
array_clip  s    
rY  c              	      s  |j |j ksJ |j|jks J 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;   Tr8   )	r  rV   r   old_itemsizenew_itemsizerk  rW   newdimZ
bytelengthZ
any_layoutZc_layoutZf_layoutint8r8   r9   r4  	  s.    



z_change_dtype.<locals>.imprO   r   r   r;   r  Nr  )rY   rk  ordr   r_  r   rs   rg   r	   rR   rQ   r{   ra   rT   rd   rb   r   r   r    booleanr  r   r   r   )re   r5   Zoldtynewtyr   Z
new_layoutr4  r[  r\  r  Z
shape_dataZstrides_dataZshape_strides_array_typearyclsZshape_constantZsizeof_intpZstrides_constantr!  Zstrides_aryrO   r   r   rI   r   r8   r^  r9   _change_dtype	  s    	
0



	
rd  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  rO   r6  r8   r8   r9   r   	  s    znp_shape.<locals>.implr   r   r   r  r   r8   r8   r9   np_shape	  s    
rg  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 r8   r8   )r   rW   r  rE  r8   r9   r   	  r   z5np_unique.<locals>.np_unique_impl.<locals>.<listcomp>)r  sortr(  r   r  r   )r  headtailr8   rh  r9   np_unique_impl	  s    z!np_unique.<locals>.np_unique_implr8   )r  rl  r8   r8   r9   	np_unique	  s    rm  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rc   r   r;   r  z(operands could not be broadcast together)r  r  int64rO   np_repeat_impl_repeats_scalerr
  r   r(  emptyr[  rc   r2   )	r  repeatsZrepeats_arrayasaaraveln	to_returnposrW   r8   r8   r9   !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)rz   r   r{   Listr  r  rU  r|   rp  rc   r   r   )r  rr  rx  r8   r8   r9   	np_repeat	  s    rz  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  rn  r;   )	r   r  r  r(  rO   rq  rc   r  r2   )r  rr  rs  rt  ru  rv  rW   r8   r8   r9   rp  	  s    


rp  repeatc                 C   s   dd }|S )Nc                 S   s   t | |S r  )r  r{  )r  rr  r8   r8   r9   array_repeat_impl	  s    z'array_repeat.<locals>.array_repeat_implr8   )r  rr  r|  r8   r8   r9   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   )rb   r   rc   r   rd   )cgctxr5   rI   llargsrf   Zllintpr8   r8   r9   r  	  s    z%_intrin_get_itemsize.<locals>.codegen)r   rs   )tyctxrc   rI   r  r8   r8   r9   _intrin_get_itemsize	  s    
r  c                 C   s   d S r  r8   )r  rc   r8   r8   r9   _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   rY   r   rO   rw   r   )r  rc   Z
dtype_sizeZmsg1r   p1p2Zp3Zmsg2Zmsg3r]  Zmsg4r8   r8   r9   r   	  s(    






z ol_compatible_view.<locals>.implr8   r  rc   r   r8   r8   r9   ol_compatible_view	  s    #r  z
array.viewc                 C   sF  |j d }|j}t|| ||d }t|| |}t|jj}t|D ]:}	t||	}
|	dkrt|jj	}|
|
||_qFt||	|
 qFtdkr| j}|t}||g |j R i }| ||}||| t| ||||}|d|t|j	d}||& d}| j|t|f W d    n1 s$0    Y  | }t| ||j|S )Nr   r}   )r;      r]  z"new type not compatible with array)r   r   ra   r   r   r   sortedgetattrr}   r3   r  r   r   Ztyping_contextZresolve_value_typer  Zget_call_typer@   rd  r`  r   r   r  r  r   r   r   )re   r5   rI   r   r   r   r   r  fieldsr   ro   r  r  r  Z_compatible_view_sigr   r  r"  r   r   r8   r8   r9   
array_view
  s0    



2r  rc   c                 C   s   |   }t| |||S r  )r  r   re   r5   r  r   r   r8   r8   r9   array_dtypeD
  s    r  rO   c                 C   s(   t |}|| ||}|j}t| |||S r  )ra   rO   r   re   r5   r  r   rl   r   r   r8   r8   r9   array_shapeJ
  s    r  r   c                 C   s(   t |}|| ||}|j}t| |||S r  )ra   r   r   r  r8   r8   r9   array_stridesS
  s    r  rY   c                 C   s   |  tj|j}t| |||S r  )r   r   rs   rY   r   r  r8   r8   r9   
array_ndim\
  s    r  rw   c                 C   s(   t |}|| ||}|j}t| |||S r  )ra   r   r   r  r8   r8   r9   
array_sizec
  s    r  r   c                 C   s(   t |}|| ||}|j}t| |||S r  )ra   r   r   r  r8   r8   r9   array_itemsizek
  s    r  nbytesc                 C   s2   t |}|| ||}||j|j}t| |||S )z"
    nbytes = size * itemsize
    )ra   r   r   r   r   r  r8   r8   r9   array_nbytest
  s    r  
contiguousc                 C   s   |  tj|j}t| |||S r  )r   r   ra  	is_contigr   r  r8   r8   r9   array_contiguous
  s    r  c_contiguousc                 C   s   |  tj|j}t| |||S r  )r   r   ra  Zis_c_contigr   r  r8   r8   r9   array_c_contiguous
  s    r  f_contiguousc                 C   s   |  tj|j}t| |||S r  )r   r   ra  Zis_f_contigr   r  r8   r8   r9   array_f_contiguous
  s    r  r  c                 C   s    |  tj|j }t| |||S r  )r   r   ra  mutabler   r  r8   r8   r9   array_readonly
  s    r  ctypesc           	      C   sP   t |}|| ||}t|}| ||}|j|_|j|_| }t| |||S r  )ra   r   ArrayCTypesr   r}   r   r   r   )	re   r5   r  r   rl   r   Zactctinfor   r8   r8   r9   array_ctypes
  s    
r  r}   c                 C   s8   | j |||d}|j}||| tj}t| |||S Nr   )r   r}   rt  r   r   rs   r   )re   r5   r  r   r  r   r8   r8   r9   array_ctypes_data
  s    r  c                 C   s6   | j |||d}|j}||| |}t| |||S r  )r   r}   r  r   r   )re   r5   fromtytotyro   r  r   r8   r8   r9   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   )ra   r   r  rs   rY   rd   r   rc   r    Zbool_rO   r   r   r  )
checkerre   r5   r   r   Ztup_intpr   Z	check_sigZ
check_argsr  r8   r8   r9   _call_contiguous_check
  s    
r  r   c                 C   s>   |  |t|}||_| }| j||| t| |||S r  )r   r   
ArrayFlagsr   r   r   r   r   )re   r5   r  r   flagsobjr   r8   r8   r9   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   )
rM   rk  r   r  r   r   r   r   ra  r   )re   r5   r  r   r  r   ro   r8   r8   r9   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v }| tj	|}t
| |||S )NrZ  r   r;   rp  )rM   rk  r   r  r   r   rY   r   r   ra  r   )re   r5   r  r   r  r   rk  ro   r8   r8   r9   array_flags_f_contiguous
  s    r  realc                 C   sL   |j tjv rt| |||ddS |j tjv r8t| |||S tdtj d S )Nr  attrzunsupported .real for {})	rc   r   complex_domainarray_complex_attrnumber_domainr   rA   r   r3   )re   r5   r  r   r8   r8   r9   array_real_part
  s
    r  imagc                 C   s   |j tjv rt| |||ddS |j tjv 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 {})rc   r   r  r  r  r    r  _parse_empty_like_argsri  r	   memsetr}   r   r   r   r   r   r   rA   r   r3   )re   r5   r  r   rI   r  r   r   r8   r8   r9   array_imag_part  s    
r  c                 C   s   |dvs|j tjv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  rc   rk  )r}   r   )rc   r   r  rA   r   ra   Zunderlying_floatrd   rb   r   r3   r   r  r  r}   rl  r   IntTyper  r   r	   Zcopy_structr   r   )re   r5   r  r   r  rl   r   ZfltyZsizeof_fltyr   Z
llfltptrtyr   Zresulttyr   replr8   r8   r9   r    s     r  conj	conjugatec                 C   s   dd }|S )Nc                 S   s
   t | S r  )r  r  rr  r8   r8   r9   r   Q  s    zarray_conj.<locals>.implr8   rr  r   r8   r8   r9   
array_conjN  s    r  c                 C   s   |   S r  )r  )re   r5   ZdtypetyZdtypevalr8   r8   r9   
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   rz   r   r{   r   r   r   ZLoweringError)re   r5   rI   r   r   r   r   r8   r8   r9   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 r8   r   r   rs   rY  re   r8   r9   r     r   z(array_record_getattr.<locals>.<listcomp>c                    s   g | ]}  tj|qS r8   r  rY  r  r8   r9   r     r   r   )ra   rc   rz   r   RecordrA   typeofr   NestedArrayr  rY   r   rs   r	   r   r}   r3   r   rO   r   rd   rb   r   r   r   r   r   )re   r5   r  r   r  rl   r   Zrectyperc   r   ZrestyZrarytyZraryZconstoffsetZ
newdataptrrO   r   Zdatasizer   r8   r  r9   array_record_getattr~  sN    


r  c                 C   s0   |d }t |tstt| ||jd |d |S rA  )rz   strrA   r  r   )re   r5   rI   r   r   r8   r8   r9   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 r8   r  r   r   r  r8   r9   r     r   z"record_getattr.<locals>.<listcomp>c                    s   g | ]}  tj|qS r8   r  r  r  r8   r9   r     r   Nr   r;   )sentry_record_alignmentr   r  rz   r   r  ra   rc   rO   r   r	   get_record_memberrb   r   rT   r   rs   rw   r   r   rj   rk   )re   r5   r  r   r  r   elemtyr   r   rc   r  r  newdatar   dptrri   r8   r  r9   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   Nrh   )r   r  r   r  rz   r   r  r	   r`   r}   r  r3   ZpointeeZmemcpyr   rs   r   rb   rr   rj   rn   )re   r5   rI   r   r  r  r  r  ro   r   r  Z
val_structr  r  r  ri   r8   r8   r9   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   )re   r5   rI   r   r   r8   r8   r9   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  )re   r5   rI   r   rv   r  Zll_fieldr   r8   r8   r9   record_static_getitem_int  s
    r  Zstatic_setitemc                 C   sJ   |j \}}}|\}}}	t|j||}
| ||
}|dus<J ||||	fS )3
    Record.__setitem__ redirects to setattr()
    N)r   r    r   get_setattr)re   r5   rI   r   rectyrm  r  recrv   ro   getattr_sigr   r8   r8   r9   record_static_setitem_str  s    
r  c                 C   s^   |j \}}}|\}}}	t|j||}
t|j d j}| || |
}|dusPJ ||||	fS )r  r   N)r   r    r   r   r  r  )re   r5   rI   r   r  rm  r  r  rv   ro   r  r  r   r8   r8   r9   record_static_setitem_int'  s    
r  c                 C   s   |  |||S )zB
    Create a constant array (mechanism is target-dependent).
    )make_constant_array)re   r5   r  pyvalr8   r8   r9   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   )re   r5   r  r  Zltyro   r8   r8   r9   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).
    rn  )r  r   r  Zuint8r  )re   r5   r  r  bufr8   r8   r9   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  )rO   r   r  r}   r  rE  r8   r8   r9   array_is_impl]  s
    
zarray_is.<locals>.array_is_impl)r   r	   r^  r  )re   r5   rI   r   atyZbtyr  r8   r8   r9   array_isW  s
    
r  __hash__c                 C   s   dd S )Nc                 S   s   d S r  r8   r  r8   r8   r9   r  j  r   zol_array_hash.<locals>.<lambda>r8   r  r8   r8   r9   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)
flatitertyr8   r8   r9   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  nditertyr8   r8   r9   make_array_ndenumerate_clsx  s    r  c              	   C   s  |  tjd}|d}	|d ur.|tj| tt|D ]}
t	|||
}t
|||}||
 }|d||}t||6 ||| |d ur||
 ||	 W d    n1 s0    Y  ||| |d ur:||
 q:|d ur|tj| ||	 ||	 d S )Nr   Zend_incrementr   )r   r   rs   r*  r   r	   
false_byterg  r2   rR   r   r6   r   	if_likelyr2  	true_byter3  )re   r5   rY   rO   r   end_flagloop_continue
loop_breakr   bbendr'  idxptrrv   r   	in_boundsr8   r8   r9   _increment_indices  s*    
(

r  c                 C   s*   t ||j|j}t| ||j||| d S r  )r	   r   rO   rY   r  )re   r5   rq  rr  r   r  rO   r8   r8   r9   _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_dimrY   )rG   r  r  r  r  r8   r8   r9   r)    s
    z-make_nditer_cls.<locals>.BaseSubIter.__init__c                 S   s   t | j| j| d S r  )r   r  r  )rG   rU   r8   r8   r9   set_member_ptr  s    z3make_nditer_cls.<locals>.BaseSubIter.set_member_ptrc                 S   s   t | j| jS r  )r  r  r  r  r8   r8   r9   
member_ptr  s    z/make_nditer_cls.<locals>.BaseSubIter.member_ptrc                 S   s   d S r  r8   rG   re   r5   r8   r8   r9   init_specific  s    z2make_nditer_cls.<locals>.BaseSubIter.init_specificc                 S   s   d S r  r8   rG   re   r5   logical_dimr8   r8   r9   r    s    z2make_nditer_cls.<locals>.BaseSubIter.loop_continuec                 S   s   d S r  r8   r  r8   r8   r9   r    s    z/make_nditer_cls.<locals>.BaseSubIter.loop_breakN)rZ   r[   r\   r%  r)  r	  r  cached_propertyr
  r  r  r  r8   r8   r8   r9   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   rs   r	  r	   r   )rG   re   r5   r   r8   r8   r9   r    s    z2make_nditer_cls.<locals>.FlatSubIter.init_specificc                 S   s   | | j}||j|gS r  )r6   r
  rl  r}   )rG   re   r5   r   rq  rr  r   r8   r8   r9   compute_pointer  s    z4make_nditer_cls.<locals>.FlatSubIter.compute_pointerc                 S   s8   || j d kr4|| j}t||}||| j d S r9  )rY   r6   r
  r	   r   r   )rG   re   r5   r  r   r8   r8   r9   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   rs   r   r
  rY   r6   r	   r   )rG   re   r5   r  r   r   r8   r8   r9   r    s    z/make_nditer_cls.<locals>.FlatSubIter.loop_breakN)rZ   r[   r\   r%  r  r  r  r  r8   r8   r8   r9   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
J d S r  )need_shaped_indexingr  r  r8   r9   r    s    z9make_nditer_cls.<locals>.TrivialFlatSubIter.init_specificc                 S   s&   t |dksJ t |||j|S r9  )r   rl  r}   rG   re   r5   r   rq  rr  r8   r8   r9   r    s    z;make_nditer_cls.<locals>.TrivialFlatSubIter.compute_pointerN)rZ   r[   r\   r%  r  r  r8   r  r8   r9   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J tj|||||ddS NFrz  )r   rY   r	   r   r  r8   r8   r9   r    s    z7make_nditer_cls.<locals>.IndexedSubIter.compute_pointerNrZ   r[   r\   r%  r  r8   r8   r8   r9   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  r8   r8   r9   r  	  s    z7make_nditer_cls.<locals>.ZeroDimSubIter.compute_pointerNr  r8   r8   r8   r9   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  r8   r  r8   r8   r9   r    s    z6make_nditer_cls.<locals>.ScalarSubIter.compute_pointerNr  r8   r8   r8   r9   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  rW  rS   )rG   lZ	factoriesrW   ra  r  r  r  rm  r  factory)r  r  r  r  r  r  r8   r9   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sJ |||
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sJ 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%dNr8   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   )rO   
main_shaperu  rW   r8   r8   r9   check_shapeO  s    zBmake_nditer_cls.<locals>.NdIter.init_specific.<locals>.check_shaperZ  r  r]  r.  )&r   r   rs   
make_tupler  arraysr  rz   r{   r	   r   r   _arrays_or_scalarsr  rY   rO   r   r   r    noner  r   r   rQ  r  r  r  r   rE   r3   r2   rR   r   r   rT   	exhaustedr  r  )rG   re   r5   arrtysr#  r   rW   r  r  ZslotZmain_shape_tyr   Zmain_nitemsrq  r!  rr  rI   r   Zshape_is_emptyr&  r   r'  r  subiter)rk  rY   r  nshapesr8   r9   r  ,  sd    





z-make_nditer_cls.<locals>.NdIter.init_specificc                    s>  | d}t||| j}t||$ |d || W d   n1 sT0    Y   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.
            ru  FNTc                 S   s   g | ]}|  qS r8   r  r   r   r8   r8   r9   r     r   zEmake_nditer_cls.<locals>.NdIter.iternext_specific.<locals>.<listcomp>r;   r   )r*  r	   as_bool_bitr6   r&  if_unlikelyr   r2  r#  r   r$  r   _make_viewsr   r   r"  r   rO   r  r  partial_loop_continue_loop_breakr3  )rG   re   r5   r   r   r&  r'  r#  r   viewsrO   r  r8   r9   iternext_specific{  sB    


(


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  rG   re   r5   r'  ra  r8   r8   r9   r/    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  r3  r8   r8   r9   r0    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u sJ | | ||	| || || |||< qqZtdd |D sJ |S )z:
            Compute the views to be yielded.
            Nc              	      s    g | ]}  t |qS r8   r6   r	   rR   rY  r5   r   r8   r9   r     s   z?make_nditer_cls.<locals>.NdIter._make_views.<locals>.<listcomp>rZ  r  c                 s   s   | ]
}|V  qd S r  r8   r*  r8   r8   r9   r	    r   z>make_nditer_cls.<locals>.NdIter._make_views.<locals>.<genexpr>)rW  r  r   rz   r   r  r   r2   r   r  r  
_make_viewr  )rG   re   r5   r   r'  r#  r1  rW  r  rettysra  r(  rm  Zarray_indicesZsub_indicesrW   )rk  narraysr  r)  r5  r9   r-    s.    


z+make_nditer_cls.<locals>.NdIter._make_viewsc                 S   s   t |tjr|jdksJ ||||||}||||}	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   r8   N)r   )rz   r   r{   rY   r  ra   rg   r"  r  rs   r   )rG   re   r5   r   r   rq  rr  r(  rU   viewr   rO   r   r8   r8   r9   r6    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   rz   r   r{   rS   ra   r  )	rG   re   r5   r'  r#  r  rW   rq  rr  r8   r8   r9   r$    s    
z2make_nditer_cls.<locals>.NdIter._arrays_or_scalarsN)rZ   r[   r\   r%  r  r  r  r  r2  r/  r0  r-  r6  r$  r8   
r  r  r  r  r  rk  r8  rY   r  r)  r8   r9   NdIter  s   O*r;  )rY   rk  r   r#  r  objectr	   r`   )r  r  r;  r8   r:  r9   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 ]n}t	|||}|
|| || }	|d|	|}
t||
 |
tj| W d    q@1 s0    Y  q@|| _|| _t|||j| _d S Nr   r.  r]  )r   r   rs   r	   rE   r3   r   r  r2   rR   r   r`  r,  r  r   r&  rT   rO   )rG   re   r5   r   r   r   r&  r'  r  rL  dim_is_emptyrY   r8   r9   r    s"    
.z3make_ndindex_cls.<locals>.NdIndexIter.init_specificc           
         s   | tjd} d}t  j}t |$ |	d  
| W d    n1 sb0    Y   fddtD }|D ]}t | q|t ||j |	d t j}	t| |	jj  
|  | d S )Nr   ru  Fc              	      s"   g | ]}  t j|qS r8   )r6   r	   rR   r   r   r'  r5   rG   r8   r9   r     s
   
zKmake_ndindex_cls.<locals>.NdIndexIter.iternext_specific.<locals>.<listcomp>T)r   r   rs   r*  r	   r+  r6   r&  r,  r   r2  r2   r=   r   rT   r3   r   rO   r  r   r3  )
rG   re   r5   r   r   r   r&  r   r6   rO   r@  rB  r9   r2    s*    


(


z7make_ndindex_cls.<locals>.NdIndexIter.iternext_specificN)rZ   r[   r\   r%  r  r2  r8   r@  r8   r9   NdIndexIter  s   rC  )rY   r	   r`   )r  rC  r8   r@  r9   make_ndindex_cls  s    7rD  c                    s\    dv sJ | 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  r.  )r   r   rs   r	   r   r   r   rc  rE   r3   rY   r2   rR   r   r   )	rG   re   r5   rq  rr  r   r   r'  r  r  r8   r9   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 d    n1 s0    Y  d S )Nr   r  c              	      s    g | ]}  t |qS r8   r4  rA  r5  r8   r9   r   d  s
   
z\_make_flattening_iter_cls.<locals>.CContiguousFlatIter.iternext_specific.<locals>.<listcomp>)rY   r   r6   r   r   r   r	   r  rl  r}   rm   r   r   r2   rT   make_anonymous_structr  r   r   )rG   re   r5   rq  rr  r   rY   r   r   r   rU   r   idxvalsidxtuplerE  r5  r9   r2  T  s2    
zH_make_flattening_iter_cls.<locals>.CContiguousFlatIter.iternext_specificc                 S   s   | |j|g}t||||S r  )rl  r}   rm   rG   re   r5   rq  rr  r   rU   r8   r8   r9   r  r  s    z>_make_flattening_iter_cls.<locals>.CContiguousFlatIter.getitemc                 S   s$   | |j|g}t||||| d S r  )rl  r}   rp   rG   re   r5   rq  rr  r   r   rU   r8   r8   r9   setitemv  s    z>_make_flattening_iter_cls.<locals>.CContiguousFlatIter.setitemN)rZ   r[   r\   r%  r  r2  r  rK  r8   rE  r8   r9   CContiguousFlatIter6  s
   rL  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||
|}||| ||| || }|d||}t|| |tj| W d    q|1 s0    Y  q||	| _|
| _|| _d S r>  )r   r   rs   r}   rY   r	   r   rO   rE   r3   r   r  r2   rR   r   r`  r,  r  r   pointersr&  )rG   re   r5   rq  rr  r   r}   rY   r   r   rM  r&  r'  r  ptrptrrL  r?  r8   r8   r9   r    s:    .z9_make_flattening_iter_cls.<locals>.FlatIter.init_specificc              	      sL  |j }t |j|}t |j|}| j| j}	|tj	d}
 
d}t  | j}t |$ |d  | W d    n1 s0    Y  |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 d    n1 s
0    Y   |
| q, tj| j  |  | d S )
Nr   ru  FTr;   r  c              	      s    g | ]}  t |qS r8   r4  rA  r5  r8   r9   r     s
   
zQ_make_flattening_iter_cls.<locals>.FlatIter.iternext_specific.<locals>.<listcomp>r   )rY   r	   r   rO   r   r   rM  r   r   rs   r*  r+  r6   r&  r,  r   r2  rR   rm   r   r2   rT   rF  rg  r   r   r  r   r   r  r3  )rG   re   r5   rq  rr  r   rY   r   r   rM  r   r   is_exhaustedZlast_ptrrU   r   rG  rH  r'  r  rv   r   rc  r  rN  Z	inner_dimrE  r5  r9   r2    sj    

(


*
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   )rY   r	   r   rO   r   rg  r2   rS   ZuremZudivreverserj  r}   rk  )rG   re   r5   rq  rr  r   rY   r   r   r   r'  rU   r8   r8   r9   _ptr_for_index  s    z:_make_flattening_iter_cls.<locals>.FlatIter._ptr_for_indexc                 S   s    |  |||||}t||||S r  )rQ  rm   rI  r8   r8   r9   r    s    z3_make_flattening_iter_cls.<locals>.FlatIter.getitemc                 S   s&   |  |||||}t||||| d S r  )rQ  rp   rJ  r8   r8   r9   rK     s    z3_make_flattening_iter_cls.<locals>.FlatIter.setitemN)	rZ   r[   r\   r%  r  r2  rQ  r  rK  r8   rE  r8   r9   FlatIter}  s   !ErR  )rM   rk  r	   r`   )r  r  rM   rL  rR  r8   rE  r9   r  0  s    
D r  r  c                 C   sh   t t|}|| |}||_| |}|| ||dd}|| ||| | }t| |t||S Nr   )rH   )	r  r   NumpyFlatTyper   ra   rQ   r  r   r   )re   r5   rq  rr  flatiterclsflatiterarrclsr   r8   r8   r9   make_array_flatiter  s    

rX  c                 C   sZ   |j \}|\}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
| d S r  )r   r  rM   ra   r   r2  )re   r5   rI   r   r   r  rV  rU  rq  rW  rr  r8   r8   r9   iternext_numpy_flatiter  s    
rY  c                 C   sj   |j d }|\}}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
|}t| ||j|S Nr   r   )r   r  rM   ra   r   r  r   r   )re   r5   rI   r   r  rV  r   rU  rq  rW  rr  r   r8   r8   r9   iternext_numpy_getitem'  s    

r[  c                 C   sf   |j d }|\}}}t|}|| ||d}|j}	| |	}
|
| ||jd}|| ||	||| |  S rZ  )r   r  rM   ra   r   rK  r  )re   r5   rI   r   r  rV  r   r   rU  rq  rW  rr  r8   r8   r9   iternext_numpy_getitem_any7  s    


r\  c           	      C   sF   |j d }t|}|| ||d d}| |j}|| ||jd}|jS rZ  )r   r  ra   rM   r   r   )	re   r5   rI   r   r  rU  rV  rW  rr  r8   r8   r9   iternext_numpy_getitem_flatH  s    
r]  c           
      C   sr   |j \}|\}tt|}|| |}||_| |}|| ||dd}|| ||| | }	t	| ||j
|	S rS  )r   r  r   NumpyNdEnumerateTyper   ra   rQ   r  r   r   r   )
re   r5   rI   r   rq  rr  	nditerclsr  rW  r   r8   r8   r9   make_array_ndenumerateS  s    

r`  c                 C   sZ   |j \}|\}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
| d S r  )r   r  rM   ra   r   r2  )re   r5   rI   r   r   r  r  r_  rq  rW  rr  r8   r8   r9   iternext_numpy_nditere  s    
ra  c                    s^    fddt |j|D }ttt|}| }| | | }t |j	|S )zndindex(*shape)c                    s"   g | ]\}}  ||tjqS r8   r  )r   Zargtyr  r   r8   r9   r   y  s   z&make_array_ndindex.<locals>.<listcomp>)
r   r   rD  r   NumpyNdIndexTyper   r  r   r   r   )re   r5   rI   r   rO   r_  r  r   r8   r   r9   make_array_ndindexu  s    

rc  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 r8   r  r   r5   re   ru   r8   r9   r     s   z,make_array_ndindex_tuple.<locals>.<listcomp>)r   rY   r   rc   r	   r   rD  r   rb  r   r  r   r   )
re   r5   rI   r   rY   r  rO   r_  r  r   r8   rd  r9   make_array_ndindex_tuple  s    
re  c                 C   s6   |j \}|\}t|}|| ||d}|| || d S r  )r   rD  r2  )re   r5   rI   r   r   r  r  r_  r8   r8   r9   iternext_numpy_ndindex  s
    rf  c           	      C   sn   |j }|j}t|jd tjr0t||d }n
|d g}t|| |}|	| ||| |
 }t| |||S )z
    nditer(...)
    r   )r   r#  rz   r   r   r  r	   r   r=  r  r   r   )	re   r5   rI   r   r  r'  r#  r  r   r8   r8   r9   make_array_nditer  s    
rg  c                 C   s2   |j \}|\}t|| ||d}|| || d S r  )r   r=  r2  )re   r5   rI   r   r   r  r  r8   r8   r9   iternext_numpy_nditer2  s    rh  c                 C   sf  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 d   n1 s0    Y  |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   r8   r   Nr  rZ  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  )+ra   rb   rc   r   r   rs   rg   r   r   r  Zsmul_with_overflowr  r_  rY   rk  rg  rS   r   r   rA   r   r   r  r  r   Zget_preferred_array_alignmentZuint32r  MemInfoPointervoidptrZTypeRefr3   r    r  _call_allocatorr   Zmeminfo_datar   r	   rT   r   r  r  )re   r5   r  r   rc  r   datatyper   ZarrlenZoverflowr   Zarrlen_multr   Zdimension_sizeZallocsize_multr  rc   Z	align_valri   r   mipZarytypeclassargtypesr   r}   r   Zshape_arrayZstrides_arrayr8   r8   r9   ri    sr    



$
ri  	_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  ri   r8   r8   r9   r     s    z _ol_array_allocate.<locals>.implr8   )rq  r  ri   r   r8   r8   r9   _ol_array_allocate  s    rr  c                 C   s   |  ||S )z9Trampoline to call the intrinsic used for allocation
    )ro  )r  rw   ri   r8   r8   r9   rk  !  s    rk  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)re   r5   r    r   r  ri   r   r8   r8   r9   r  +  s    zintrin_alloc.<locals>.codegen)r   ri  rj  r    )r  r  ri   r  rm  rI   r8   r8   r9   rp  '  s    rp  c              	   C   s   dd }t |tjr.d}| |||tjg}n&t |tjs>J |j}tj|||d}g }|D ]}|	|| ||j
| q\| |tjd}	t|D ]R}
|d||
 |	}t||  | j|td W d   q1 s0    Y  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|||}nr|j|kr|d||}	|j	|	dd  | j
|td W d   n1 s0    Y  |j|kr|||}n|}|S )z0Cast src to intp only if value can be maintainedr;   >Fr  )z#Cannot safely convert value to intpN)r   r   rs   r<   r   r  r   Zsextr   r   r  r  r   trunc)
re   r5   Zsrc_tr  r   Z
intp_widthZintp_irmaxvalr   Z	is_largerr8   r8   r9   safecast_intp9  s"    


"
z#_parse_shape.<locals>.safecast_intpr;   r   r   r   )znegative dimensions not allowedN)rz   r   r|   rr   rs   r  r   r	   r   rS   r3   get_constant_genericr2   r   r,  r  r  r   )re   r5   r  ro   rv  rY   Zpassed_shapesr   r   r   r'  Zis_negr8   r8   r9   _parse_shape5  s$    $rx  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   rx  )re   r5   rI   r   ZarrshapetypeZarrshaper  r8   r8   r9   _parse_empty_argse  s    
ry  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   r8   N)
r   rz   r   r{   ra   r	   r   rO   rY   r   )re   r5   rI   r   arytyper   r   r8   r8   r9   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.)rz   r   ZUnicodeTyper   r   )fnamerc   r   r8   r8   r9   _check_const_str_dtype}  s    r|  c                 C   s"   |j }||||}dd }||fS )Nc                 S   s(   t | |||\}}t| |||}| S r  )ry  ri  r   r~  r5   rI   r  r  r   r   r8   r8   r9   r    s    znumpy_empty_nd.<locals>.codegenZinstance_type)r  Zty_shapety_dtypety_retty_refty_rettyrI   r  r8   r8   r9   numpy_empty_nd  s    r  c                    s   t d| |tu s0t|tjr(|jtu s0t|r8tj}nt|}t	| }|d ur||d ur|tj
||dd tf fdd	}|S d|  d| d}t|d S )	Nrq  r   r  c                    s   t | | S r  )r  )rO   rc   r   r8   r9   r     s    zol_np_empty.<locals>.implz.Cannot parse input types to function np.empty(, ))r|  floatrz   r   Function
typing_keyr   doublety_parse_dtypety_parse_shaper{   r   r   )rO   rc   nb_dtyperY   r   r   r8   r  r9   ol_np_empty  s"    

r  c                 C   s"   |j }||||}dd }||fS )Nc                 S   s(   t | |||\}}t| |||}| S r  )r  ri  r   r}  r8   r8   r9   r    s    z$numpy_empty_like_nd.<locals>.codegenr~  )r  Zty_prototyper  r  r  rI   r  r8   r8   r9   numpy_empty_like_nd  s    r  c                    s   t d| t|st|}nt| tjr0| j}n| }|d u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)rc   rk  r  r   z3Cannot parse input types to function np.empty_like(r  r  c                    s   t | | S r  )r  )rr  rc   r  r8   r9   r     s    zol_np_empty_like.<locals>.impl)N)r|  r   r  rz   r   r{   rc   rk  r  r   r   )rr  rc   r  rk  r   r   r8   r  r9   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   )ra   r   r	   r  r}   r   r   r   )r~  r5   rI   r  r   r8   r8   r9   r    s    z(_zero_fill_array_method.<locals>.codegen)r   r%  )r  rG   rI   r  r8   r8   r9   _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  r8   r8   r9   r     s    z ol_array_zero_fill.<locals>.implr8   )rG   r   r8   r8   r9   ol_array_zero_fill  s    r  c                 C   s   t d| tfdd}|S )Nzerosc                 S   s   t j| |d}|  |S Nrn  )r  rq  r  )rO   rc   rr  r8   r8   r9   r     s    zol_np_zeros.<locals>.impl)r|  r  rO   rc   r   r8   r8   r9   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  rc   rr  r8   r8   r9   r     s    zol_np_zeros_like.<locals>.impl)Nr|  r  r8   r8   r9   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 Nrn  r;   r  r:  r  r2   r   )r  rc   rr  arr_flatrv   r8   r8   r9   r     s
    
zol_np_ones_like.<locals>.impl)Nr  r  r8   r8   r9   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  rq  r  r2   r   )rO   
fill_valuerc   rr  r  rv   r  r8   r9   r  
  s
    
zimpl_np_full.<locals>.full)N)r|  r   r  )rO   r  rc   r  r8   r  r9   impl_np_full  s    

r  c                 C   s   t d| ddd}|S )NrN  c                 S   s0   t | |}|j}tt|D ]}|||< q|S r  r  )r  r  rc   rr  r  rv   r8   r8   r9   rN    s
    
z$impl_np_full_like.<locals>.full_like)Nr  )r  r  rc   rN  r8   r8   r9   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  )rO   rc   rr  r  rv   r8   r8   r9   r   '  s
    
zol_np_ones.<locals>.impl)Nr  r  r8   r8   r9   
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 r9  )r  r  r2   )ru  rc   rr  rW   r  r8   r9   r  8  s    z"impl_np_identity.<locals>.identity)N)r|  r   r  r   r  )ru  rc   r  r8   r  r9   impl_np_identity0  s    

r  c                 C   s   d S r  r8   NMr8   r8   r9   _eye_none_handler@  s    r  c                 C   s"   t |tjrdd }ndd }|S )Nc                 S   s   | S r  r8   r  r8   r8   r9   r   G  s    z$_eye_none_handler_impl.<locals>.implc                 S   s   |S r  r8   r  r8   r8   r9   r   J  s    rz   r   rV  )r  r  r   r8   r8   r9   _eye_none_handler_implD  s    
r  c                    sf   |d u 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 )Nrc   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   rc   Z_Mrr  drW   r  r8   r9   r   Z  s    
znumpy_eye.<locals>.impl)
rz   r   rV  r  rc   r  	DTypeSpecr  r   r  )r  r  r   rc   r   r8   r  r9   	numpy_eyeO  s    
r  c                 C   sD   t | stdt| tjr@| jdv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 rA  )
rY   rO   r  r  r  rc   r2   r  r=  rq  )r   r   r   ru  r  rW   rowscolsr8   r8   r9   	diag_implr  s.    


zimpl_np_diag.<locals>.diag_impl)r   )r   r   r   rz   r   r{   rY   NumbaTypeError)r   r   r  r8   r8   r9   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)rw   
IndexErrorr(  )r  r   r8   r8   r9   	take_impl  s    znumpy_take.<locals>.take_implc                 s   s   | ]}t |tjV  qd S r  r  r   r  r8   r8   r9   r	    r   znumpy_take.<locals>.<genexpr>rZ  c                    s   t j|j| jd} r | }n|}t |}d}|  }|D ]:}|| jd ks\|| j k rdtd|| ||< |d }q>||j	S Nrn  r   r;   r  )
r  rq  rw   rc   r  r  r(  r  r%  rO   )r  r   r  walkeritrW   r  r  ZF_orderr8   r9   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   rq  rw   rc   r  r(  r  r%  rO   )r  r   convertr  r  rW   r  r  r8   r8   r9   r    s    


)rz   r   r{   r|   r  rk  ry  r  )r  r   r  r8   r  r9   
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 r8   r  r  r8   r8   r9   r     r   z!_arange_dtype.<locals>.<listcomp>c                 s   s   | ]}t |tjV  qd S r  rz   r   Complexr  r8   r8   r9   r	    r   z _arange_dtype.<locals>.<genexpr>c                 s   s   | ]}t |tjV  qd S r  )rz   r   Floatr  r8   r8   r9   r	    r   zint%sr  c                 S   s   g | ]}t |qS r8   )r   Z	unliteralr   r  r8   r8   r9   r     r   )
r
  r   
complex128r   r  r  rc   r   r   r  )r   Zboundsrc   ZNPY_TYZunliteral_boundsr8   r8   r9   _arange_dtype  s    r  c                    s  t |tjr|j}t |tjr$|j}t |tjr6|j}|d u rDtj}|d u rRtj}|d u 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 r8   r  r  r8   r8   r9   r     s   znp_arange.<locals>.<listcomp>r   c                    s    d ur n| }d urn|}d ur,n|}|d ur<|nd}|d u rTd| }}	n
|| }}	|dkrnt d|	| | }
tt|
j}du 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  rq  r2   )r   rR  rM  rc   Z	lit_startZlit_stopZlit_stepZ_step_start_stopZnitems_cZnitems_rZnitems_ir   rr  ro   rW   Zstart_valueZ
step_valueZ
stop_valueZ
true_dtypeZuse_complexr8   r9   r     s(    

znp_arange.<locals>.impl)NNN)rz   r   Optionalr3   r%  r  rV  r  r  rc   r
  r  )r   rR  rM  rc   r   r8   r  r9   	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  )rz   r   r  r  r8   r8   r9   r	     r   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  r8   r8   r9   r	  '  r   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  rq  divider2   )r   rR  r  rr  divdeltarM  rW   rn  r8   r9   r  -  s    z numpy_linspace.<locals>.linspace)r  )
r  rz   r   r   r|   r   r   r
  r  r   )r   rR  r  r   r  r8   rn  r9   numpy_linspace  s    
r  c              
   C   s2  |j d }t|| ||d d}t||j}|j}t| |||}|j}	|j}
|jdv s\J |j|jkrtj	||
|	|j
|jdd nt||j}t||j}| tj}t|||R}t| ||	|||j|}t| ||
|||j|}|||| W d   n1 s0    Y  t| ||j| S )z
    Array copy.
    r   r   rp  r;   rh   N)r   ra   r	   r   rO   r   ri  r}   rk  Z
raw_memcpyr   r   r   r   r   rs   r  rj  r   r6   r   r   )re   r5   rI   r   rz  r   r   rettyper  r  r  r  r  r   r   r}  r  r8   r8   r9   _array_copyF  s4    


2r  c                 C   s@   t |tjsJ |jdkrdnd}|j|dd}||}|tfS )NrZ  r   F)rk  r  )rz   r   r{   rk  r  r  )r  r  rk  r  rI   r8   r8   r9   _array_copy_intrinsick  s
    r  z
array.copyc                 C   s   t | |||S r  )r  r  r8   r8   r9   
array_copyt  s    r  c                 C   s   t | tjrdd }|S )Nc                 S   s   t | S r  )r  r6  r8   r8   r9   
numpy_copy|  s    z#impl_numpy_copy.<locals>.numpy_copyr  )r  r  r8   r8   r9   impl_numpy_copyy  s    r  c              	   C   s  |j }|jd }|j|ks"J d|jdkr|jdks:J 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v rt| |||d S |jdkr|dv sJ |dkrtnt}t|| |||d }||\}}|( t| |||d }|j}W d	   n1 sv0    Y  |$ t| |||}|j}W d	   n1 s0    Y  W d	   n1 s0    Y  ||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  rp  r  r   N)r   r   rk  rY   ra   rw  r   r  rs   r"  r   r   r}   r   r   r   r   r   r   r  r{  blockr  phir3   Zadd_incoming)re   r5   rI   r   output_layoutr   r   r   r  rO   r   Z
check_funcr  ZthenorelseZout_thenZthen_blkZ
out_orelseZ
orelse_blkZret_phir8   r8   r9   _as_layout_array  s^    

&Fr  c                    sH   t  tjst |j jt|jdd}|| }| fddfS )Nr;   )rk  rY   c                    s   t | ||| jdS )Nr  )r  r   )rE  rE  r   r  r  r8   r9   r    s   z,_as_layout_array_intrinsic.<locals>.<lambda>)	rz   r   r!   r   RequireLiteralValuer  r   r  rY   )r  r  r  r  rI   r8   r  r9   _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 )NrG  c                 S   s   t t | S r  )r  ascontiguousarrayr   r6  r8   r8   r9   r     s    z%array_ascontiguousarray.<locals>.implc                 S   s
   t | dS Nr   r  r6  r8   r8   r9   r     s    r   r   r   rz   r   r  rU  r{   rf  r8   r8   r9   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 )NrG  c                 S   s   t t | S r  )r  asfortranarrayr   r6  r8   r8   r9   r     s    z"array_asfortranarray.<locals>.implc                 S   s
   t | dS )NrZ  r  r6  r8   r8   r9   r     s    r  rf  r8   r8   r9   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|||r}t| ||	|||j|}t| ||
|||j|}t| |||}| |||j|j}t| |||| W d    n1 s0    Y  t| ||j| S rZ  )r   ra   r	   r   rO   r   ri  r}   r   r   r   rs   r  rj  rk  rm   rr   rc   rp   r   r   )re   r5   rI   r   rz  r   r   r  r  r  r  r  r  r   r   r}  r  itemr8   r8   r9   array_astype  s.    


.r  c                 C   s"   |j }||||}dd }||fS )Nc              	   S   s2  |j d }|j}t|| ||d d}t|}|| |}|j}	t| |}
t|jj|
}||j	|j}|
||}t||}|j|dd& d}| j|t|f W d    n1 s0    Y  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   ra   r   rg   r   r   r3   r   r   Zsremr	   Zis_not_nullr   r  r  r   rT   Zsdivr  r}   r   r   r   r   r   r   r   )re   r5   rI   r   Zbuftyr   r  Z
out_ary_tyout_aryZout_datamodelr   ll_itemsizer  remZis_incompatibler   rO   r   r}   r   r8   r8   r9   r    s:    


0znp_frombuffer.<locals>.codegenr~  )r  bufferrc   r   r  rI   r  r8   r8   r9   np_frombuffer
  s    $r  c                    s   t d| t| tjr | jdkr4d|  }t||tu sZt|tjrR|j	tu sZt
|rbtj}nt|}|d u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;   )rc   rY   rk  r  z3Cannot parse input types to function np.frombuffer(r  r  c                    s   t | | S r  )r  )r  rc   r  r8   r9   r   M  s    z impl_np_frombuffer.<locals>.impl)r|  rz   r   r   rk  r   r   r  r  r  r   r  r  r{   r  )r  rc   r   r  r   r8   r  r9   impl_np_frombuffer6  s2    





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  r8   rU   rO   rc   intrinsic_cfarrayr8   r9   r   W  s    zimpl_carray.<locals>.implc                    s
    | |S r  r8   r  r  r8   r9   r   ]  s    )N)Nr   get_cfarray_intrinsicrz   r   r  rU   rO   rc   r   r8   r  r9   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 )NrZ  c                    s
    | |S r  r8   r  r  r8   r9   r   g  s    zimpl_farray.<locals>.implc                    s
    | |S r  r8   r  r  r8   r9   r   m  s    )N)Nr  r  r8   r  r9   impl_farrayb  s    

r  c                    s   t  fdd}|S )Nc           	         s   |t ju rd }n*t|t jr$|j}nd| d}t| d u r^|d u rXd}t||}nVt t jr j}|d ur||krd| d| d}t|nd  d}t|t|}|d u 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   rj  rz   CPointerrc   r   r  r  r  r{   r    
np_cfarray)	r  rU   rO   Z	ptr_dtyper   rc   rY   r   rI   dtype_rk  r8   r9   r  s  s2    




z0get_cfarray_intrinsic.<locals>.intrinsic_cfarrayr%   )rk  r  r  r8   r  r9   r  r  s    r  c                    s<  |j dd \}}|dd \}}|j}|jdv s6J 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  rp  c                    s"   g | ]\}}  ||tjqS r8   r  )r   r  r   r   r8   r9   r     s   znp_cfarray.<locals>.<listcomp>rZ  r  )r   r   rk  ra   rg   r	   r   rz   r   r  r   r   rS   r   rg  rP  r  rb   rc   r  r   r   r   )re   r5   rI   r   r  r  rU   rO   r   r  r   r  r   offr   r   r}   r   r8   r   r9   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sRJ d S r   )	rz   r   r  r   rs   r   r  r@   r    )re   r5   seqtyseqr  r8   r8   r9   _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  re   r  r   r8   r9   wrap  s    
z$_get_borrowing_getitem.<locals>.wrap)rc   r@   r  r  r    r   rs   )re   r  r  r8   r  r9   _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)rz   r   r  r   r  r  rc   )r  r  r  r5   re   r   r8   r9   get_first_item  s    
z.compute_sequence_shape.<locals>.get_first_item)r   r   rs   r   r2   rS   r  r   )re   r5   rY   r  r  r   r  r   innertyinnerrW   r8   r  r9   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   r8   r   r8   r9   _fail  s    
z#check_sequence_shape.<locals>._failc                    sD  t |dkrd S t| |}|d }d||}j|dd    W d    n1 s^0    Y  t |dkrxd S t| tjrt| }t	|:}| j
}|||jf}	||	|dd   W d    n1 s0    Y  nZt| tjr2tt | D ].}
| |
 }||
}	||	|dd   q nds@J | d S )Nr   r  Fr  r;   )r   r  r   r   rz   r   r  r  r	   rC  rc   r   r  r2   r  )r  r  r   rw   expectedr  r  rD  r	  r
  rW   r  r5   check_seq_sizere   r8   r9   r    s*    $
4z,check_sequence_shape.<locals>.check_seq_sizeNr8   )re   r5   r  r  r   r8   r  r9   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	   rj  rk  rr   rc   rp   )r   r  ro   rU   )rq  r5   re   r}   r   r   r8   r9   assign_item3  s
    z-assign_sequence_to_array.<locals>.assign_itemc                    s$  t |dkr2t| tjtjfr"J || | d S |d }t| tjrt| }t|D}| j}|||j	f} |||dd  ||j	f  W d    n1 s0    Y  nnt| tjrt
t | D ]B}	| |	 }||	}tj|	}
 |||dd  ||
f  qnds J | d S r  )r   rz   r   r  r  r  r	   rC  rc   r   r2   r  r   rs   )r  r  r   r   rw   r  rD  r	  r
  rW   r   )assignr  r5   re   r8   r9   r  9  s$    
> z(assign_sequence_to_array.<locals>.assignr8   Nr8   )re   r5   r}   r   r   rq  r  r  r8   )rq  r  r  r5   re   r}   r   r   r9   assign_sequence_to_array,  s    r  c                 C   s>   t | |\}}t|r|}nt|}|d u r0d S t||dS r  )r/   r   r  r   r{   )r  r<  rc   rY   Z	seq_dtyper8   r8   r9   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>J t| |||| t| |||}	t| ||	j||	j	||| t
| ||j |	 S r   )r   rY   r   r  r   r  ri  r  r}   r   r   r   )
re   r5   rI   r   rq  rY   r  r  r   rr  r8   r8   r9   r  f  s    


znp_array.<locals>.codegen)r|  r  )r  objrc   r  rI   r  r8   r8   r9   np_array`  s
    

r  c                 C   sL   t d| t| stdt|s>t|d u 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<  rc   r8   r8   r9   r     s    zimpl_np_array.<locals>.impl)N)r|  r   r   r   r   r  )r<  rc   r   r   r8   r8   r9   impl_np_arrayz  s    



r  c           
      C   s   | d}| |}|d||}||||||}||d|||d||}|j|dd* d| }	| j|t|	f W d    n1 s0    Y  |S )Nr   r   r1  Fr  z%s(): axis out of bounds)	r3   r   rQ  r;  r_  r   r  r  r  )
re   r5   r   rY   r   r   Zll_ndimZis_neg_axisZaxis_out_of_boundsr   r8   r8   r9   _normalize_axis  s    

0r  c              
   C   s   t ||d ksJ 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;   r1  r   r   r   r  r	   r   rE   r2   r   rQ  r;  r   rR   r   r6   )re   r5   
orig_shaperY   r   ll_shtyr   r   r'  ll_dim
after_axisr   rv   r8   r8   r9   _insert_axis_in_shape  s    


r  c              
   C   s   t ||d ksJ 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   r1  r  )re   r5   orig_stridesrY   r   r  r   r   r   r'  r  r  rv   r8   r8   r9   _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   rY   r   ra   r	   r   rO   r   r  r   r   r}   r   r   r   r   )re   r5   rI   r   r   r   rY   rq  rr  r  r   r   Z
new_shapesZnew_stridesr8   r8   r9   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)	rr   r   r   rs   r  r   rY   r!  r   )re   r5   rI   r   r   r  r8   r8   r9   r    s    znp_expand_dims.<locals>.codegen)rY   rk  r  )r  r  r   rk  r  rI   r  r8   r8   r9   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   r8   r8   r9   r     s    z!impl_np_expand_dims.<locals>.impl)rz   r   r{   r   r   r|   )r  r   r   r   r8   r8   r9   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 )r@  )r  r  rY   r  )minimumr8   r9   r     r   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 r8   r8   )r   rr  rq  r   r5   re   	transformr8   r9   r     s   z>_atleast_nd.<locals>.impl.<locals>.codegen.<locals>.<listcomp>r;   )_atleast_nd_transformr	   r   r   r   r"  r   r   )re   r5   rI   r   arrsZretsr  )r'  r   r$  r7  r%  r9   r    s    

z*_atleast_nd.<locals>.impl.<locals>.codegenr;   r   )r    r   r   r  ZStarArgTupleZ
from_types)Ztypingcontextr   r  )r   r$  )r'  r7  r9   r     s    
z_atleast_nd.<locals>.implc                     s    |  S r  r8   r  )r   r8   r9   r  (  r   z_atleast_nd.<locals>.<lambda>r  )r$  r   r8   )r   r   r$  r9   _atleast_nd  s    r)  c                    s"   t  ksJ  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;   r@  )r2   rY   r	   r   r  r!  r   r    )	re   r5   rr  rq  r   rW   rY   r   Znewarrtyr   min_ndimr8   r9   r&  2  s    
z(_atleast_nd_transform.<locals>.transform)r   )r+  r   r&  r8   r*  r9   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  r8   r8   r9   r	  D  r   z np_atleast_1d.<locals>.<genexpr>r;   r   r  r)  r  r8   r8   r9   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  r8   r8   r9   r	  J  r   z np_atleast_2d.<locals>.<genexpr>r  r   r,  r  r8   r8   r9   np_atleast_2dH  s    r.  c                  G   s$   t dd | D r tdg dS d S )Nc                 s   s   | ]}t |tjV  qd S r  r  r  r8   r8   r9   r	  P  r   z np_atleast_3d.<locals>.<genexpr>r  )r   r   r  r,  r  r8   r8   r9   np_atleast_3dN  s    r/  c	                 C   s  t |t |  kr.t |  kr.t |ks4n J 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}|r}t| |||||j|}t| |||}| |||j|j}t| |||||j|}t| |||| W d   n1 s0    Y  t|||}q|
S )z2
    Concatenate arrays along the given axis.
    r   r]  )orderN)r   r	   r   ri  r   r   r  r   r   r3   r   rQ  r;  rS   r}   r  rk  rj  rm   rr   rc   rp   r   )re   r5   r   r'  r(  
arr_shapesarr_stridesr   
ret_shapesr   r  Zret_stridesZcopy_offsetsZarr_shr   r'  rw   rc  is_axisaddendZret_datarq  rr  Zarr_stZarr_datar  r   r}  ro   r  r8   r8   r9   _do_concatenateT  sJ    4



0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 ]T d||}
|	 } |}fd	d|d
d  D } |
\}}|0 t	 j
||g } || W d    n1 s0    Y  | tj}|D ]} | d||}q j |dd& j td f W d    n1 s~0    Y  W d    n1 s0    Y  W d    qr1 s0    Y  qr fdd|	D }	t |||||||		}t || S )Nc                    s"   g | ]\}}t | |d qS r   r  r   r  r  r   r8   r9   r     s   z#_np_concatenate.<locals>.<listcomp>np.concatenatec                    s   g | ]}t  |jqS r8   )r	   r   rO   r   rr  r  r8   r9   r     r   c                    s   g | ]}t  |jqS r8   r	   r   r   r:  r  r8   r9   r     r   c                    s   g | ]}t  |qS r8   )r	   r   r  r  r8   r9   r     s   r   r]  c                    s   g | ]}|  qS r8   r8   r  )r'  r8   r9   r     r   r;   Fr  z<np.concatenate(): input sizes over dimension %d do not matchc                    s   g | ]}  |qS r8   )r6   r  r  r8   r9   r     r   )rY   r   r  r2   r   r3   r6   r{  r  r  r;  r   r	   r  rx  r   rH  r  r  r   r6  r   r   )re   r5   r'  r(  r   r   rY   r1  r2  r3  r4  Zret_shape_ptrZret_shZother_shapesZon_axisZon_other_dimr   is_okr  r8   )r5   re   r'  r9   _np_concatenate  sR    

,fr=  c                    s
  |j }td}td}tt|}	 fddt||D }t d||}t |d j}
|dd  D ]}tj}tt |j|
D ]b\}} 	| 
d||} j |dd  j td	 W d    q1 s0    Y  qqp fd
d|D }ttj|t }t }t|d D ]l}t|} 
d||}|
| } | |||} |t |d|  |t |d| q8 |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| qnq0t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   r8   r9   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 r8   r;  r:  r  r8   r9   r     r   r1  c                    s   g | ]}t  qS r8   r  rY  )r5   r  r8   r9   r     s   c                    s   g | ]}t   |qS r8   )r	   r   r6   )r   r   r  r8   r9   r     s   )rY   r	   r   r   r   r  r   rO   r  rx  r   r   rH  r  r  r   r   r  rE   r2   rQ  r;  r   rR   r6   r6  r   r   )re   r5   r'  r(  r   r   rY   r   r   Z
ll_narraysr  rr  r<  r   Zorig_shr  Zinput_shapesr3  r'  r  r  rv   Zinput_stridesrW   r  r8   )r5   re   r  r9   	_np_stack  s    

&






r?  c                 C   sN   |d u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)rz   r   r|   r0   	TypeErrorr1   r{   )r  r#  r   rc   rY   rk  r8   r8   r9   np_concatenate_typer  s    rA  c                 C   s6   t | ||}t|tjsJ |||}dd }||fS )Nc              	   S   sF   |  ||d |jd tj}t| |t|jd t||d |j|S rA  )	rr   r   r   rs   r=  r   r	   r   r   re   r5   rI   r   r   r8   r8   r9   r  3  s    znp_concatenate.<locals>.codegen)rA  rz   r   r{   )r  r#  r   r  rI   r  r8   r8   r9   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   r8   r8   r9   r   A  s    z!impl_np_concatenate.<locals>.impl)r   rz   r   r  r#  r   r   r8   r8   r9   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)rY   r@  )re   r   r#  r  r8   r8   r9   _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stJ |	j
dd}t||	}t| |||
f|}|	| |	| q>t| ||||j|S )Nr   r;   r  r@  )r   r   r	   r   r   r   rs   r   rY   rS   r  r   r    r!  r=  r   )re   r5   rI   r   Zorig_arrtysZ	orig_arrsr'  r(  r   rq  rr  rb  
expand_sigZnewarrr8   r8   r9   r  X  s$    



z np_column_stack.<locals>.codegen)r0   rH  r1   r   r{   )r  r  rc   rY   rk  r  rI   r  r8   r8   r9   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  r8   r8   r9   r   x  s    zimpl_column_stack.<locals>.implrE  r  r   r8   r8   r9   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  r8   r8   r9   _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 )rZ  N)rk  r  r8   r8   r9   r	    r   z"np_stack_common.<locals>.<genexpr>rZ  r   r;   c                 S   s,   |  ||d |jd tj}t| ||||S r9  )rr   r   r   rs   rN  rB  r8   r8   r9   r    s    z np_stack_common.<locals>.codegen)r0   r  r   r{   )	r  r#  r   rc   rY   rk  r  rI   r  r8   r8   r9   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  r8   r8   r9   r     s    zimpl_np_stack.<locals>.impl)r   rE  rF  r8   r8   r9   impl_np_stack  s    
rP  c                 C   s4   t | ||\}}t||}t|}t|||}|S r  )r0   r  r1   r   r{   )r  r   r#  Zndim_minrc   rY   rk  r  r8   r8   r9   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,  r.  r#  r-  r8   r9   np_hstack_impl  s    z3_np_hstack.<locals>.codegen.<locals>.np_hstack_implr   rY   r   r   rs   rN  r  )re   r5   rI   r   r  rY   rS  r8   r-  r9   r    s    

z_np_hstack.<locals>.codegenrQ  r  r  r  rI   r  r8   r8   r9   
_np_hstack  s    rW  c                 C   s   t | tjrdd }|S d S )Nc                 S   s   t | S r  )rW  rK  r8   r8   r9   r     s    zimpl_np_hstack.<locals>.implrE  rL  r8   r8   r9   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 r9  )r  r!  hstackrR  r8   r8   r9   np_vstack_impl  s    z3_np_vstack.<locals>.codegen.<locals>.np_vstack_implr;   c                 S   s   t j| ddS )Nr   r-  r.  rR  r8   r8   r9   rZ    s    rT  )re   r5   rI   r   r  rY   rZ  r   r8   r8   r9   r    s    


z_np_vstack.<locals>.codegenrU  rV  r8   r8   r9   
_np_vstack  s    r[  c                 C   s   t | tjrdd }|S d S )Nc                 S   s   t | S r  )r[  rK  r8   r8   r9   r     s    zimpl_np_vstack.<locals>.implrE  rL  r8   r8   r9   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	|	g|j R  }
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  r8   r8   r9   rZ    s    z3_np_dstack.<locals>.codegen.<locals>.np_vstack_implr;   r@  r  c                 S   s   t j| ddS )Nr  r-  r.  rR  r8   r8   r9   rZ    s    )r   r   rY   r  r   r   rs   r  r   r    rN  r!  )re   r5   rI   r   r  r   rY   rZ  r   Zstack_rettyZ	stack_sigZ	stack_retrI  r8   r8   r9   r    s*    


z_np_dstack.<locals>.codegenrU  rV  r8   r8   r9   
_np_dstack  s    "r]  c                 C   s   t | tjrdd }|S d S )Nc                 S   s   t | S r  )r]  rK  r8   r8   r9   r     s    zimpl_np_dstack.<locals>.implrE  rL  r8   r8   r9   impl_np_dstack  s    r^  fillc                 C   s   dd }|S )Nc                 S   s   || d d < d S r  r8   )rr  ro   r8   r8   r9   	fill_impl$  s    zarr_fill.<locals>.fill_implr8   )rr  ro   r`  r8   r8   r9   arr_fill!  s    ra  dotc                 C   s   dd }|S )Nc                 S   s   t | |S r  )r  rb  )rr  otherr8   r8   r9   dot_impl-  s    zarray_dot.<locals>.dot_implr8   )rr  rc  rd  r8   r8   r9   	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  rY   r   r  r  r8   r8   r9   r   9  s    

znp_flip_lr.<locals>.implre  rf  r8   r8   r9   
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  r8   r8   r9   r   J  s    

znp_flip_ud.<locals>.implre  rf  r8   r8   r9   
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                    sZ   dd }t jg}tjg|R  }| t j}| }| |g}	| ||||	}
|
S )Nc                 S   s*   |}t | D ]}t||td d d}q|S )Nr  )r2   r*   r   )lengthempty_tupler   rW   r8   r8   r9   r   `  s    z6_build_flip_slice_tuple.<locals>.codegen.<locals>.impl)r   rs   r   r    r   Zget_constant_undefr  )re   r5   r    r   r   Zinner_argtypesZ	inner_sigZll_idx_typerk  Z
inner_argsr   rw   Z
tuple_typer8   r9   r  _  s    

z(_build_flip_slice_tuple.<locals>.codegen)	rz   r   r  r   r  r   r   r  Zslice3_type)r  szrI   r  r8   rl  r9   _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  rY   )r  slr8   r8   r9   r   z  s    
znp_flip.<locals>.impl)rz   r   r{   r   r   rf  r8   r8   r9   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   r8   r8   r9   r     s
    znp_array_split.<locals>.implc                 S   sP   t | j| |\}}tt|d g| |g|| d   }tj| ||dS )Nr;   r-  )divmodrO   r  Zcumsumr   rq  )r   rs  r   r  r  r   r8   r8   r9   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+   rY   r   r*   r   rS   r   rs  r   Z	slice_tupr   prevrn  rv   r8   r8   r9   r     s    
c                 s   s   | ]}t |tjV  qd S r  rz   r   r|   )r   tr8   r8   r9   r	    r   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+   rY   r   r   r*   r   rS   rv  r8   r8   r9   r     s    
)r   )r   )r   )r   )rz   r   r  ListTypery  r|   ZIterableTypeZiterator_typer   r  r  r   rs  r   r   r8   r8   r9   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  r8   r8   r9   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 divisionr-  )rt  rO   r   r  rq  )r   rs  r   rm  r  r8   r8   r9   r     s    r-  )r   )r   )rz   r   r  rz  ry  r|   r|  r{  r8   r8   r9   np_split  s    


r~  c                 C   s   | |k pt |ot |  S r  )r  isnanr  r8   r8   r9   	lt_floats  s    r  Fc                 C   s   | ||f}z
t | W S  ty~   | dkrJtj|r6tnd|dd}|j}n$| dkrntj|r^tnd|d}|j}|t |< | Y S 0 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  keyri  funcr8   r8   r9   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  r8   r  Z	sort_funcr8   r9   array_sort_impl  s    z#array_sort.<locals>.array_sort_impl)r   r  rz   rc   r   r  r  )re   r5   rI   r   rz  r  r8   r  r9   
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  ri  )r  r   r8   r8   r9   np_sort_impl  s    z"impl_np_sort.<locals>.np_sort_implre  )r  r  r8   r8   r9   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  r8   r  r  r8   r9   array_argsort_impl"  s    z)array_argsort.<locals>.array_argsort_implr;   r  )	r   r  r   rz   rc   r   r  replacer  )	re   r5   rI   r   rz  r  r  ZinnersigZ	innerargsr8   r  r9   array_argsort  s    
r  c                 C   s   |j |j ks|jdksJ |S )Nr  )r  rk  )re   r5   r  r  ro   r8   r8   r9   array_to_array.  s    r  c                 C   s4   dd }t ||}| ||||g}t| ||j|S )Nc                 S   s
   |  dS r   )r  r6  r8   r8   r9   r   =  s    zarray0d_to_scalar.<locals>.impl)r    r  r   r   re   r5   r  r  ro   r   rI   r   r8   r8   r9   array0d_to_scalar5  s    
r  c                 C   s4   dd }t ||}| ||||g}t| ||j|S )Nc                 S   s   t | d S r:  )r  r6  r8   r8   r9   r   H  s    z array_to_unichrseq.<locals>.impl)r    r  r   r   r  r8   r8   r9   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  rO   r   r8   r8   r9   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*  r8   r8   r9   r	  ^  r   z>type_reshape_unchecked.<locals>.check_shape.<locals>.<genexpr>)rz   r   r  r  rO   r8   r8   r9   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  )rz   r   r{   r   r  r  r!  r8   r9   typer`  s    z%type_reshape_unchecked.<locals>.typerr8   )re   r  r8   r  r9   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   ra   r	   r   r   r}   r   r   r   r   )re   r5   rI   r   r   r   r   r   rO   r   r   r8   r8   r9   impl_shape_uncheckedl  s    
r  c                    s`   |d t jfv rtdd  ntdd  |d t jfv r@tdntdd d	 fdd	}|S )
Nc                 S   s   | j S r  r  r  rO   r8   r8   r9   r!    s    zas_strided.<locals>.get_shapec                 S   s   |S r  r8   r  r8   r8   r9   r!    s    z*as_strided() strides argument is mandatoryc                 S   s   |S r  r8   )r  r   r8   r8   r9   get_strides  s    zas_strided.<locals>.get_stridesc                    s   t |  | || |} | S r  )r  )r  rO   r   r!  r  r8   r9   as_strided_impl  s    z#as_strided.<locals>.as_strided_impl)NN)r   r%  r"   rA   )r  rO   r   r  r8   r  r9   
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  r8   window_shaper8   r8   r9   get_window_shape  s    z-sliding_window_view.<locals>.get_window_shapec                 S   s   | S r  r8   r  r8   r8   r9   r    s    z4window_shape must be an integer or tuple of integersc                 S   s   t t|S r  )r   r2   r  r   rY   r8   r8   r9   get_axis  s    z%sliding_window_view.<locals>.get_axisc                 S   s   t dd||gS )Nsliding_window_viewr   r   r  r8   r8   r9   r    s    c                    s    fdd|D S )Nc                    s   g | ]}t d d |qS )r  r   r  r  r@  r8   r9   r     s   z9sliding_window_view.<locals>.get_axis.<locals>.<listcomp>r8   r  r8   r@  r9   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;   )	rY   r   r   r2   r*   rO   r   r   r  )
r  r  r   Z	out_shapeZout_stridesrW   r   r'  Ztrimmedr9  r  r  Zshape_bufferZstride_bufferr8   r9   sliding_window_view_impl  s8    
z5sliding_window_view.<locals>.sliding_window_view_impl)N)rz   r   r|   r   r2   rY   r"   r  rc   r   r   r   r   )r  r  r   r  r8   r  r9   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())rw   r  r  r   )rr  r   r8   r8   r9   r     s    

zol_bool.<locals>.implr  r  r8   r8   r9   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  )rr  r  r  Z
axes_tupleZ	axes_listrY   r8   r9   r     s    znumpy_swapaxes.<locals>.impl)
rz   r   r   r|   r   r   r{   rY   r   r2   )rr  r  r  r   r8   r  r9   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   rY   r   rO   r  r   r   r*   r  r  r   r2   rq  rc   r   r<  )rr  r   r   ZNi_origZNk_origindices_broadcast_shapeZ	arr_shaperW   d1Zd2new_valZarr_broadcast_shapeNiNkJr   Znp_s_iikkZa_1dZ
indices_1dZout_1djr8   r8   r9   _take_along_axis_impl-  sP    

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   r8   )r  r2  rr  r   r   )r  r8   r9   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  r8   r9   r    s    )rz   r   r{   r   r   rc   r|   r   rY   r   r2   r   r  r  r   )rr  r   r   Zarr_ndimr  r8   r  r9   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  r8   r  r  nanr8   r8   r9   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  r8   r8   r9   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  rE  r8   r8   r9   r     s    z4Only Integer, Float, and Complex values are acceptedc                 S   s   | S r  r8   r  r8   r8   r9   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  rc   r=  r  r  r  r  r2   rw   r  r  r  )	r  r  r  Zmin_infZmax_infx_outputZoutput_flatrW   r8   r8   r9   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  r8   r8   r9   r     s
    
z4The first argument must be a scalar or an array-like)Tr  )Tr  )Tr  )Tr  )Tr  )Tr  )
rz   r   r  r|   r  r  r   r   r   rc   )r  r  r  r   r8   r8   r9   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=   ra   rg   rm   rp   ry   r   r   r   r   r   r   r   r   r   ZArrayIteratorZBORROWEDr   r   r   r  r  r|   r   r  r  r  rK  Anyr  r   r  r{   r  r  r<  r  r&  r8  r<  r@  rK  rS  r  ro  rs  rv  rw  ry  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'  rS   r/  r5  r(  r8  r9  rD  rF  rX  rW  rY  rd  rO   rg  uniquerm  r{  rz  rp  r}  r  r  r  r  r  r  Z
MemoryViewr  r  r  r  r  r  r  r  r  r  r  r  r  r  rj  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=  rD  r  rX  rT  rY  r[  r\  r]  r  r`  r^  ra  r<  rc  re  rb  rf  r  rg  ZNumpyNdIterTyperh  ri  rr  rk  rp  rx  ry  r  r|  r  rq  r  r  r  r:  r  r  r  r  r  r  r  r  r  r  r  rN  r  r  r  r  r  r  r  Z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  Zfliprp  rq  r|  r}  r~  r  r  r  r  ri  r  Zargsortr  r  ZUnicodeCharSeqr  r  rU  ZNPTimedeltaZ
NPDatetimer  r  r  Ztype_callabler  r  libZstride_tricksr  r  r  r  r  r  r  Ztake_along_axisr  r  r  r8   r8   r8   r9   <module>   s  $4<	
E




D

(



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

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

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


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

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