o
    Y۷i                 	   @   s  d Z ddl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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 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/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7m8Z8 ddl9m:Z:m;Z; ddl<m=Z= ddl>m?Z?m@Z@ ddl3mAZA ddlBmCZDmEZFmGZGmHZHmIZI dd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPd d! ZQd"d# ZR	dTd$d%ZSd&d' ZTd(d) ZUe5eUd*d+ ZVe$d,ejWd-d. ZXd/d0 ZYe$d1ejZe*e.j[d2d3 Z\	dTd4d5Z]d6d7 Z^d8d9 Z_e$ej`ejWejae$ej`ejWejbd:d; Zce$ej`ejWejdd<d= Zee$ejfejWejgejgd>d? Zhe$eiejWd@dA Zje$dBejkdCdD Zle"dEk rxe$dFejkejgdGdH ZmG dIdJ dJenZoG dKdL dLeoZpG dMdN dNeoZqG dOdP dPeoZrG dQdR dReoZsG dSdT dTeoZtG dUdV dVenZudWdX Zve$ej`ejWejkdYdZ Zwd[d\ Zxd]d^ Zyd_d` Zzdadb Z{dcdd Z|dedf Z}dgdh Z~didj Ze7dkdl Ze7dmdn Ze4dodp Zdqdr Ze5e
jdsdt Ze4dudv Ze5e
jdwdx Ze5e
jdydz Zd{d| Ze5ed}d~dd Ze5edd~dd Zdd Zdd Ze$dejkdd Zdd Ze$dejkejddd Ze$dejkeejgdd Ze5e
jdTddZe%ejkddd Ze5e
jdUddZe5e
jdUddZe5e
jdVddZdd Zdd Ze$dejkejddd Ze$dejkeejgdd Ze"dk re5e
jdd Zn	e5e
jdd Ze5e
jdd Ze5e
jdTddZe$dejkdd Ze$e
jejkdd Ze$dejkdd Ze4dd Ze4dd Ze5e
jdTddZe6ejkddWddZdd Ze5e
jddÄ Ze5e
jddń Ze5e
jddǄ Ze5e
jddɄ Ze4dd˄ Ze6ejkd̡dd΄ Ze7ddЄ Zdd҄ Ze5ed}d~ddԄ Ze$dejkejddׄ Ze%ejkd؃ddڄ Ze%ejkdۃe%ejdۃdd݄ Ze%ejkdރe%ejdރdd Ze%ejkde%ejddd Ze%ejkddd Ze%ejkde%ejddd Ze%ejkde%ejddd Ze%ejddd Ze%ejddd Ze%ejddd Ze%ejddd Ze%ejkddd Ze%ejddd Ze(ejejЃe(ejejуdd  ZҐdd Ze%ejkddd Ze%ejde%ejddd Ze%ejddd	 Ze%ejkd
dd Ze%ejkddd Zِdd Ze6ejkde6ejkddd Zېdd Ze%ejݐde܃ e%ejݐde܃ e$dejejgdd Ze&ejkdd Ze$dejkej2dd  Ze&ejd!d" Ze'ejd#d$ Ze$dejej2d%d& Ze$dejejd'd( Ze$d)ejej2ejgd*d+ Ze$d)ejejejgd,d- Ze)ejkd.d/ Ze)ejd0d1 Ze)ejd2d3 Ze$ejejkejkd4d5 Ze8ejkd6d7d8 Zd9d: Zd;d< Z		dWd=d>Z	dTd?d@ZdAdB ZdCdD ZdEdF Ze%ejkdGdHdI Ze$d1eje*e.j[dJdK Ze$ej`ejejadLdM Ze$ejfejejaejgdNdO Ze$eiejdPdQ Ze$e
jejkdRdS Ze$d1ej e*e.j[dTdU Ze$eeejae$e
jeejadVdW Ze$eejde$e
jejddXdY Ze$d1eje*e.j[dZd[ Ze$e
jejgd\d] Ze$d1ej	e*e.j[d^d_ Z
e$ejejej݃d`da Zdbdc ZeAejkdddedf Zdgdh Ze7didj Zdkdl Zdmdn Zdodp Zdqdr Ze7dsdt Ze5e
jefdudvZe7dwdx Ze5e
jdTdydzZe7d{d| Ze6ejkd}d~d Ze5e
jefddZe5e
j dTddZ!e5e
j"dTddZ#e5e
j$dTddZ%e5e
j&dTddZ'e5e
j(dTddZ)e5e
j*dTddZ+dd Z,e5e,dd Z-e5e
j.ddefddZ/e5e
j0dXddZ1e5e
j2dd Z3e5e
j4dXddZ5dd Z6e5e
j7e6ejkddTddZ8dd Z9e5e
j:dWddZ;e5e
j<dUddZ=dd Z>e7dd Z?e$dejkdd Z@e5e
jAdd ZBdd ZCe7dd ZDe5e
jEdd ZFe5e
jGdd ZHe$dejkeje$dejkej2dd ZIe7dd ZJe5e
jKefddZLe5edTddZMe5edTddZNdd ZOdÐdĄ ZPdŐdƄ ZQdǐdȄ ZRdɐdʄ ZSdːd̄ ZTd͐d΄ ZUdϐdЄ ZVe7dѐd҄ ZWe5e
jXdTdӐdԄZYdՐdք ZZdאd؄ Z[dِdڄ Z\dېd܄ Z]e7dݐdބ Z^e5e
j]dߐd Z_dd Z`dd Zae5e
jbdd Zce5e
jddd Zee5e
jfdd Zgdd Zhdd Zidd Zjdd Zke7dd Zle5e
jmdXddZndd Zoe7dd Zpe5e
jqdd Zrdd Zse7dd  Zte5e
judXddZvdd Zwe7dd Zxe5e
jydd Zze7d	d
 Z{e5e
j|dd Z}e"dEk
re5e
j~e} e7dd Ze5e
jdd Ze6ejkddd Ze6ejkddd Ze5e
jdd Ze5e
jdd Ze7dd Ze5e
jdd Ze5e
jdXdd Ze5e
jdXd!d"Ze5e
jd#d$ Ze5e
jd%d& Ze5e
jd'd( Zi Zd)d* ZdYd,d-Zd.d/ Ze$d0ejkd1d2 Ze5e
jd3d4 Ze$d5ejkej2e$e
jejkej2d6d7 Ze(ejkejkd8d9 Ze(ejkeje(ejkeje(ejkejae(ejkeje(ejkeje(ejkeje(ejkejd:d; Ze(ejkejd<d= Zd>d? Zeed@dA Ze$eejkejdejddBdC Ze5e
jjjdZdDdEZe5e
jjjdTdFdGZe5edHdI Ze5e
jdJdK Ze4dLdM Ze5e
jdNdO Ze5e
jd[dRdSZd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config)
as_dtype
from_dtypecarrayfarrayis_contiguous
is_fortrancheck_is_integertype_is_scalar
lt_complex	lt_floats)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moduleadd_metadataset_metadata)builderloadlower_boundupper_boundrange_operandsmd rA   G/home/ubuntu/vllm_env/lib/python3.10/site-packages/numba/np/arrayobj.pyset_range_metadata-   s
   

rC   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)r7   widthrC   )r;   r<   r>   rA   rA   rB   mark_positive8   s   rF   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 w | jj }| |}|d u r9tj	| j
|dd}n|}|| j
|f}||fS )N	__array__T)zfill)r$   _contextget_functionNotImplementedErrorsuper
_make_refsdata_model_manager_get_be_typer	   alloca_once_builder)selfrefsig
array_impl	datamodelbe_type	outer_ref)ArrayStruct	__class__
array_typereal_array_typerA   rB   rM   N   s   


z*make_array.<locals>.ArrayStruct._make_refsc                    sp   | j }dkr | dS | d}g }tD ]}t||d|}||}|| t|| qt	||S )z\
            Override .shape to inform LLVM that its elements are all positive.
            r   shape)
rQ   __getattr___get_ptr_by_namer6   r	   gep_inboundsr<   appendrF   
pack_array)rR   r;   ptrdimsidimptrr<   )basendimrA   rB   r]   b   s   


z%make_array.<locals>.ArrayStruct.shape)__name__
__module____qualname__rM   propertyr]   __classcell__rA   rY   r[   rg   rh   r\   )rZ   rB   rY   L   s    rY   )as_arrayr	   create_struct_proxyrh   )r[   rA   rn   rB   
make_array@   s
   
-rq   c                 C   s   |  |j}| |S )zB
    Return the item size for the given array or buffer type.
    )get_data_typedtypeget_abi_sizeof)contextr[   lltyrA   rA   rB   get_itemsize|   s   
rw   c                 C   s"   |j rdnd}| j||j||dS )z3
    Load the item at the given array pointer.
    NrD   align)alignedunpack_valuers   )ru   r;   arraytyrc   ry   rA   rA   rB   	load_item   s   r}   c                 C   s$   |j rdnd}| j||j|||dS )z4
    Store the item at the given array pointer.
    NrD   rx   )rz   
pack_valuers   )ru   r;   r|   valrc   ry   rA   rA   rB   
store_item   s   r   c                 C   s@   |j r| |||tj}t|||}|S | |||tj}|S )zM
    Fix the integer index' type and value for the given dimension size.
    )signedcastr   intpr-   	fix_indexuintp)ru   r;   idxtyidxsizeindrA   rA   rB   fix_integer_index   s   r   c                 C   sZ   t |tjr)|jdkr)t |jtjsJ t|| ||}t| |||j}|j|fS ||fS )zS
    Normalize the index type and value.  0-d arrays are converted to scalars.
    r   )	
isinstancer   Arrayrh   rs   Integerrq   r}   data)ru   r;   r   r   idxaryidxvalrA   rA   rB   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 rA   )r   ).0r   r   r;   ru   rA   rB   
<listcomp>   s
    z%normalize_indices.<locals>.<listcomp>)lenzip)ru   r;   index_typesindicesrA   r   rB   normalize_indices   s
   r   c                 C   s  | j }| j}| j}	ttjdd}
|j|
 }t|j}t|	j}||@ |krC|| }d| j	 dt
|dkr7dnd d| d}t||d	u rRt||	d
d	}|tj}t|ttfrft|||}t|ttfrtt|||}t|tr}||}t|||||d}|d	u rt||	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.
    rD   CzThe datamodel for type z is missing fields  .Nmeminfo)r]   stridesr   itemsizer   parentnswflagsnitemszmissing {0}) rI   rQ   
_datamodelr   r   float64rN   set_fields_fe_typer   
ValueErrorr   get_value_typeget_typer   r   tuplelistr	   rb   intdictget_constantunpack_tupler7   countmulkeysformatitemssetattr)arrayr   r]   r   r   r   r   ru   r;   rV   standard_arraystandard_array_type_datamodelrequired_fieldsdatamodel_fieldsmissingmsgintp_tattrsr   unpacked_shapeaxlen
got_fieldskvrA   rA   rB   populate_array   sh   





r   c                 C   sh   |j }|j}|tjd}t||j| j}|D ]}|j	||dgd}q||_
|tjt|| |_dS )z
    Update some auxiliary information in *array* after some of its fields
    were changed.  `itemsize` and `nitems` are updated.
    rD   r   r   N)rI   rQ   r   r   r   r	   r   r]   rh   r   r   rw   r   )arytyr   ru   r;   r   r   r   rA   rA   rB   update_array_info   s   
r   c                 C   s   t  )z/Constrain axis values to valid positive values.rK   	func_namearg_namerh   axisrA   rA   rB   normalize_axis     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   rA   rB   impl$  s
   z&normalize_axis_overloads.<locals>.impl)r   r%   r   TypingErrorliteral_value)r   r   rh   r   r   rA   r   rB   normalize_axis_overloads  s   



r   getiterc                 C   st   |j \}|\}| ||j}| tjd}t||}||_||_	| j
r,| j||| | }	t| ||j|	}
|
S r   )argsmake_helperreturn_typer   r   r   r	   alloca_once_valueindexr   
enable_nrtnrtincref	_getvaluer!   )ru   r;   rT   r   r|   r   iterobjzeroindexptrresoutrA   rA   rB   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
rEt| |||||	|
|}| S |
rIJ t| |||	S )z1 Evaluate `ary[idx]`, where idx is a single int. r   r   rD   N)r	   r   r]   rh   r   r   pointer_addr   r   r   Buffer	make_viewr   r}   )ru   r;   r   r   aryr   shapesr   offsetdataptrview_shapesview_stridesretaryrA   rA   rB   _getitem_array_single_intG  s   r   iternextc                 C   s   |j \}|\}|j}| j|||d}t|| ||jd}	tj||	jdd\}
||j	}|
d||
}|| ||% t| ||j||	|}|| t||}|||j	 W d    d S 1 sew   Y  d S )NvaluerD   r   <)r   r[   r   rq   r   r	   r   r]   r<   r   icmp_signed	set_validif_thenr   
yield_typeyield_increment_indexstore)ru   r;   rT   r   resultitertyiterr|   r   r   r   r   is_validr   nindexrA   rA   rB   iternext_array\  s"   

"r  c              	   C   s>  |  tjd}|  tjd}t||j|j}	t||j|j}
g }g }g }tdd |D }d}t	||D ]\}}|tj
u ri|jt| d | }t|D ]}|| ||	|  ||
|  |d7 }qNq6t|tjr| j|||d}t| ||| t|||	|  ||j t||}t|||
| }|| || n=t|tjrt| ||||	| }|rt| |||	| | || nt|r|| || |d8 }ntd|f |d7 }q6||jksJ ||jk r||	|  ||
|  |d7 }||jk st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   rD   c                 S      g | ]}t |r|qS rA   r   r   r   rA   rA   rB   r         z"basic_indexing.<locals>.<listcomp>r   unexpected index type: %sF
wraparoundboundscheck)r   r   r   r	   r   r]   rh   r   r   r   ellipsisr6   ra   r   	SliceTyper   r-   guard_invalid_slice	fix_slicestartget_slice_length
fix_strider   r   do_boundscheckr   rK   get_item_pointer)ru   r;   r   r   r   r   r  r   oner   r   output_indicesoutput_shapesoutput_stridesnum_newaxesaxindexvalr   	n_missingre   sliceshstr   r   rA   rA   rB   basic_indexingx  sf   










r#  c           	   	   C   s.   t || |}t|||||j|j|jd |S )zF
    Build a view over the given array with the given parameters.
    r   r]   r   r   r   r   )rq   r   r   r   r   )	ru   r;   r   r   r   r   r   r   r   rA   rA   rB   r     s   r   c              	   C   s^   t | |||||| jd\}}}	t|tjr$t| |||||||	}
|
 S |r(J t| |||S )zp
    Return the result of indexing *ary* with the given *indices*,
    returning either a scalar or a view.
    r  )r#  enable_boundscheckr   r   r   r   r   r}   )ru   r;   r   r   r   r   r   r   r   r   r   rA   rA   rB   _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.
    rD   )r   rh   rq   r'  r   r    	ru   r;   rT   r   r   r   r   r   r   rA   rA   rB   getitem_arraynd_intp  s   

r)  c              	   C   s   |j \}}|\}}t|| ||}|j}tj||t|d}	t| |||	\}}	tdd |D r;t| |||||||	S t	| ||j
||||	}
t| ||j
|
S )z2
    Basic or advanced indexing with a tuple.
    r   c                 s       | ]	}t |tjV  qd S Nr   r   r   )r   tyrA   rA   rB   	<genexpr>      z&getitem_array_tuple.<locals>.<genexpr>)r   rq   r   r	   r   r   r   anyfancy_getitemr'  r   r    )ru   r;   rT   r   r   tuptyr   tupr   r   r   rA   rA   rB   getitem_array_tuple  s    


r4  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yR   d}Y nw t|}|rbt| ||||
|S | ||	||j}	t| |||	| dS )zH
    array[a] = scalar_or_array
    array[a,..,b] = scalar_or_array
    r   r%  TN)r   r   r   	BaseTupler	   r   r   rq   r   r#  r&  rK   boolfancy_setslicer   rs   r   )ru   r;   rT   r   r   r   valtyr   r   r   r   r   r   r   r   use_fancy_indexingrA   rA   rB   setitem_array  s4   

r:  c           	      C   sD   |j \}|\}t|}|| ||}|j}||d}t| ||j|S r   )r   rq   r]   extract_valuer"   r   )	ru   r;   rT   r   r   r   arysttyshapearyr   rA   rA   rB   	array_len4  s   r>  z
array.itemc                 C   s   |j \}|\}t|| ||}|j}|j|d||ddd d}| j|t|f W d    n1 s7w   Y  t	| |||j
S )N!=rD   Flikelyz>item(): can only convert an array of size 1 to a Python scalar)r   rq   r   r   r   r7   	call_convreturn_user_excr   r}   r   )ru   r;   rT   r   r   r   r   r   rA   rA   rB   
array_item?  s   rD     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 s@w   Y  t
| ||||j |  S )Nr?  rD   Fr@  z/itemset(): can only write to an array of size 1)r   rs   rq   r   r   r   r7   rB  rC  r   r   r   get_dummy_value)
ru   r;   rT   r   r   r8  r   r   r   r   rA   rA   rB   array_itemsetO  s   
rH  c                   @   @   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )Indexerzq
    Generic indexer interface, for generating indices over a fancy indexed
    array on a single dimension.
    c                 C      t )ze
        Prepare the indexer by initializing any required variables, basic
        blocks...
        r   rR   rA   rA   rB   preparej     zIndexer.preparec                 C   rK  )z=
        Return this dimension's size as an integer.
        r   rL  rA   rA   rB   get_sizeq     zIndexer.get_sizec                 C   rK  )z;
        Return this dimension's shape as a tuple.
        r   rL  rA   rA   rB   	get_shapew  rP  zIndexer.get_shapec                 C   rK  )z}
        Return a half-open [lower, upper) range of indices this dimension
        is guaranteed not to step out of.
        r   rL  rA   rA   rB   get_index_bounds}  rN  zIndexer.get_index_boundsc                 C   rK  )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.
        r   rL  rA   rA   rB   	loop_head  s   	zIndexer.loop_headc                 C   rK  )z)
        Finish indexation loop.
        r   rL  rA   rA   rB   	loop_tail  rP  zIndexer.loop_tailN)
ri   rj   rk   __doc__rM  rO  rQ  rR  rS  rT  rA   rA   rA   rB   rJ  d  s    rJ  c                   @   H   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+  )	ru   r;   r   r   dimr   r   r   ll_intp)rR   ru   r;   r   r   rX  rA   rA   rB   __init__  s   zEntireIndexer.__init__c                 C   sB   | j }|| jj| j| _t|| j| _	|
 | _|
 | _d S r+  )r;   r;  r   r]   rX  r   r	   rP   rY  r   append_basic_blockbb_startbb_endrR   r;   rA   rA   rB   rM    s
   
zEntireIndexer.preparec                 C      | j S r+  r   rL  rA   rA   rB   rO       zEntireIndexer.get_sizec                 C      | j fS r+  r`  rL  rA   rA   rB   rQ       zEntireIndexer.get_shapec                 C      |  d| jfS r   rY  r   rL  rA   rA   rB   rR       zEntireIndexer.get_index_boundsc                 C   s   | j }| j t| jd| j || j || j || j}|j	|
d|| jdd || j W d    ||fS 1 sBw   Y  ||fS Nr   >=Fr@  )r;   r   r   rY  r   branchr\  position_at_endr<   r   r   r   r]  )rR   r;   	cur_indexrA   rA   rB   rS    s   
zEntireIndexer.loop_headc                 C   D   | j }t||| j}||| j || j || j	 d S r+  )
r;   r	   r   r<   r   r   ri  r\  rj  r]  rR   r;   
next_indexrA   rA   rB   rT    s
   zEntireIndexer.loop_tailNri   rj   rk   rU  rZ  rM  rO  rQ  rR  rS  rT  rA   rA   rA   rB   rW    s    rW  c                   @   rV  )IntegerIndexerz0
    Compute indices from a single integer.
    c                 C   s&   || _ || _|| _| j tj| _d S r+  )ru   r;   r   r   r   r   rY  )rR   ru   r;   r   rA   rA   rB   rZ    s   zIntegerIndexer.__init__c                 C      d S r+  rA   rL  rA   rA   rB   rM       zIntegerIndexer.preparec                 C   s   t | jdS NrD   )r   rY  rL  rA   rA   rB   rO       zIntegerIndexer.get_sizec                 C      dS NrA   rA   rL  rA   rA   rB   rQ    rr  zIntegerIndexer.get_shapec                 C   s   | j | j| j |  fS r+  )r   r;   addrO  rL  rA   rA   rB   rR    s   zIntegerIndexer.get_index_boundsc                 C   s
   | j d fS r+  )r   rL  rA   rA   rB   rS       
zIntegerIndexer.loop_headc                 C   rq  r+  rA   rL  rA   rA   rB   rT    rr  zIntegerIndexer.loop_tailNro  rA   rA   rA   rB   rp    s    rp  c                   @   rV  )IntegerArrayIndexerz;
    Compute indices from an array of integer indices.
    c                 C   s@   || _ || _|| _|| _|| _|jdksJ | j tj| _	d S rs  )
ru   r;   r   r   r   rh   r   r   r   rY  )rR   ru   r;   r   r   r   rA   rA   rB   rZ    s   zIntegerArrayIndexer.__init__c                 C   sD   | j }t|| jjd | _t|| j| _|	 | _
|	 | _d S r   )r;   r	   r   r   r]   idx_sizerP   rY  	idx_indexr[  r\  r]  r^  rA   rA   rB   rM    s
   
zIntegerArrayIndexer.preparec                 C   r_  r+  rz  rL  rA   rA   rB   rO    ra  zIntegerArrayIndexer.get_sizec                 C   rb  r+  r|  rL  rA   rA   rB   rQ    rc  zIntegerArrayIndexer.get_shapec                 C   rd  r   re  rL  rA   rA   rB   rR    rf  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?w   Y  t| j|| jj| j| j|}t| j|| jj|| j}||fS rg  )r;   r   r   rY  r{  ri  r\  rj  r<   r   r   rz  r]  r   ru   r   rs   r   r   r   )rR   r;   rk  r   rA   rA   rB   rS    s&   zIntegerArrayIndexer.loop_headc                 C   rl  r+  )
r;   r	   r   r<   r{  r   ri  r\  rj  r]  rm  rA   rA   rB   rT    s   
zIntegerArrayIndexer.loop_tailNro  rA   rA   rA   rB   ry    s    	ry  c                   @   rV  )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 NrD   r   )ru   r;   r   r   rh   r   r   r   rY  r   r   )rR   ru   r;   r   r   rA   rA   rB   rZ  )  s   zBooleanArrayIndexer.__init__c                 C   s^   | j }t|| jjd | _t|| j| _t|| j| _	|
 | _|
 | _|
 | _d S r   )r;   r	   r   r   r]   r   rP   rY  r{  r   r[  r\  bb_tailr]  r^  rA   rA   rB   rM  2  s   

zBooleanArrayIndexer.preparec                 C   s   | j }t|| j}t|| j-}||}t| j|| j	j
| j	| j|j}|||||j}||| W d    n1 sAw   Y  ||S r+  )r;   r	   r   r   	for_ranger   r<   r   ru   r   rs   r   r   rw  zextr7   r   )rR   r;   r   loopcpredrA   rA   rB   rO  ;  s   

	zBooleanArrayIndexer.get_sizec                 C   
   |   fS r+  rO  rL  rA   rA   rB   rQ  J  rx  zBooleanArrayIndexer.get_shapec                 C   rd  r   re  rL  rA   rA   rB   rR  M  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 sKw   Y  t| j|| jj| j| j|}|	|| || j W d    n1 sww   Y  t||}||| j ||fS )Nrh  Fr@  )r;   r   r   r{  r   ri  r\  rj  r<   r   r   r   r]  r   ru   r   rs   r   not_r  r	   r   )rR   r;   rk  	cur_countr  
next_countrA   rA   rB   rS  R  s,   zBooleanArrayIndexer.loop_headc                 C   s\   | j }|| j || j t||| j}||| j || j	 || j
 d S r+  )r;   ri  r  rj  r	   r   r<   r{  r   r\  r]  rm  rA   rA   rB   rT  j  s   
zBooleanArrayIndexer.loop_tailNro  rA   rA   rA   rB   r}  $  s    		r}  c                   @   rV  )SliceIndexerz(
    Compute indices along a slice.
    c                 C   sL   || _ || _|| _|| _|| _|| _|| _| j tj	| _
t| j
d| _d S r   )ru   r;   r   r   rX  r   r   r   r   r   rY  r   r   )rR   ru   r;   r   r   rX  r   r   rA   rA   rB   rZ  z  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+  )r;   r;  r   r]   rX  dim_sizer-   r  ru   r   r   r  r	   
is_neg_intstepis_step_negativerP   rY  r   r   r[  r\  r]  r^  rA   rA   rB   rM    s   
zSliceIndexer.preparec                 C   s   t | j| jS r+  )r-   r  r;   r   rL  rA   rA   rB   rO       zSliceIndexer.get_sizec                 C   r  r+  r  rL  rA   rA   rB   rQ    rx  zSliceIndexer.get_shapec                 C   s   t | j| j\}}||fS r+  )r-   get_slice_boundsr;   r   )rR   lowerupperrA   rA   rB   rR    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    ||fS 1 s_w   Y  ||fS )Nz<=rh  Fr@  )r;   r   r   r  r   r   r   ri  r\  rj  r<   selectr  r   stopr   r]  )rR   r;   rk  r  is_finishedrA   rA   rB   rS    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   )r;   rw  r<   r   r   r  r   r	   r   r   ri  r\  rj  r]  )rR   r;   rn  r  rA   rA   rB   rT    s   zSliceIndexer.loop_tailNro  rA   rA   rA   rB   r  u  s    r  c                   @   rI  )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 rm|jt| d | }t|D ]}t|||||	}|| |	d7 }	|
d7 }
qTq<t|t
jr||||}t|||||	||}|| nkt|t
jrt||||| j|	 }t|||}|| nNt|t
jrt||||}t|jt
jrt||||| j|	 }nt|jt
jrt ||||}nJ || nt!|r| j|
 |	d8 }	nt"d|f |	d7 }	|
d7 }
q<|	|jksJ |	|jf|	|jk r(t|||||	}|| |	d7 }	|	|jk st||jks9J t||jf|| _#d S )Nc                 S   r  rA   r  r	  rA   rA   rB   r     r
  z)FancyIndexer.__init__.<locals>.<listcomp>r   rD   r  )$ru   r;   r   r	   r   r]   rh   r   r   r   r   r   rY  newaxesr   r   r  r6   rW  ra   r   r  r   r  r   r   rp  r   rq   rs   ry  Booleanr}  r   AssertionErrorindexers)rR   ru   r;   r   r   r   r   r  r  r  new_axr  r   r  re   indexerr   r   r   rA   rA   rB   rZ    sv   






"
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 )NrD   c                 S      g | ]}|  qS rA   )rQ  r   re   rA   rA   rB   r         z(FancyIndexer.prepare.<locals>.<listcomp>rA   )
r  rM  ru   r   r   r   r  insertsumindexers_shape)rR   re   r  	res_shaperA   rA   rB   rM    s   


zFancyIndexer.preparec                 C   r_  )z?
        Get the resulting data shape as Python tuple.
        )r  rL  rA   rA   rB   rQ    s   zFancyIndexer.get_shapec              	   C   s  t || jjks
J | j}tj}| d}| d}|}|}t| j| j	|D ]C\}	}
}|
||d|
|}|	 \}}|||}|||||}|d||}||||||}||||||}q&|||}||||}||||}||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   rD   ==r   )r   r   rh   r;   r	   	false_bitrY  r   r  r  or_icmp_unsignedrR  r   subr   rw  r  )rR   r   r   r;   is_emptyr   r  r  r  r  r]   stridelower_indexupper_indexlower_offsetupper_offsetis_downwardsrA   rA   rB   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+  )rS  r  rA   rA   rB   r.  A  s    z+FancyIndexer.begin_loops.<locals>.<genexpr>)r   r  )rR   r   countsrA   rA   rB   begin_loops@  s   zFancyIndexer.begin_loopsc                 C   s   t | jD ]}|  qd S r+  )reversedr  rT  )rR   re   rA   rA   rB   	end_loopsD  s   
zFancyIndexer.end_loopsN)
ri   rj   rk   rU  rZ  rM  rQ  r  r  r  rA   rA   rA   rB   r    s    B&r  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   r]   r   r   r  rM  r   rQ  _empty_nd_implr   r   r   r   r  get_item_pointer2layoutr&  r}   r<   gepr   r   r   r  r!   r   )ru   r;   rT   r   r   r   r   r   r   r   r   r  out_ty
out_shapesr   out_dataout_idx_rc   r   curnext_idxrA   rA   rB   r1  I  s6   

r1  c           	   	   C   s   |j \}}|\}}t|| ||}|jdkr4t| |||\}}t| ||j|||f|f}t| ||j|S t| ||||||f|fS )z3
    Advanced or basic indexing with an array.
    r   )r   rq   rh   r   r'  r   r    r1  r(  rA   rA   rB   fancy_getitem_arrayt  s   



r  c              	   C   s   |j }|d}|d}|jdv r|}	|||j}
|	|
fS |}	|}
t|jD ]-}||| ||| |}|d||}|	||
|
||
}
|	||	|
|	|}	q(|
|
|}
|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   rD   CFrh  r  )r   r7   r  r   r   r6   rh   r  r   r  rw  )ru   r;   arrtyarrr   r   r   r   r  r  r  re   max_axis_offset
is_upwardsr  rA   rA   rB   offset_bounds_from_strides  s2   



r  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.
    )ptrtointr7   rw  )ru   r;   r  r  r   data_ptr_as_intr  endrA   rA   rB   compute_memory_extents  s   r  c           	      C   s&   t | |||||\}}t| ||||S )zr
    Compute a half-open range [start, end) of pointer-sized integers
    which fully contain the array data.
    )r  r  )	ru   r;   r  r  r   r   r   r  r  rA   rA   rB   get_array_memory_extents  s   
r  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  )ru   r;   a_starta_endb_startb_endmay_overlaprA   rA   rB   extents_may_overlap  s
   r  c                    s@  j dt d  jddc  |j|j}j |}	|	j  	|	}	 
|	 tj}
t |
(}t 	
j|}t |	|} 
 || W d    n1 smw   Y  W d    n1 s|w   Y   	
fdd} fdd}||fS )Nr   Fr@  c                    s   t  } jddT\}}|  t j  | dd| W d    n1 s1w   Y  |  t j 	
j| dd| W d    n1 sUw   Y  W d    n1 sdw   Y  t 
 |S )NFr@  r  )r	   rP   if_elser   r  r<   r  r}   )source_indicessrc_ptrif_copy	otherwise)r;   ru   	copy_datacopy_layoutcopy_shapescopy_stridesptrtysrc_data
src_shapessrc_stridessrctyuse_copyrA   rB   src_getitem  s4   
z&maybe_copy_source.<locals>.src_getitemc                     sX    j dd  }  | } j |  W d    d S 1 s%w   Y  d S )NFr@  )r   r<   bitcastr   freer   )r;   ru   r  r  	voidptrtyrA   rB   src_cleanup  s
   
"z&maybe_copy_source.<locals>.src_cleanup)r7   r	   r   r   r   r   r   r   allocater  r   r   r   r   	loop_nestr  r  r<   )ru   r;   r  r  srcr  r  r  	allocsizer   r   r   r  dest_ptrr  r  rA   )r;   ru   r  r  r  r  r  r  r  r  r  r  r  rB   maybe_copy_source  s8   

"r  c           
         s  |  tjd}|  tjdt|t|kr0t|t| }g| | }|g| | }||fS t|t|k rt|t| } fdd|d| D }t j|tj} j	 
|dd d}	| j t|	f W d   n1 sww   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   rD   c                       g | ]	}  d |qS r  )r  r   r!  r;   r  rA   rB   r   (      z(_bc_adjust_dimension.<locals>.<listcomp>NFr@  z,cannot broadcast source array for assignment)r   r   r   r   	functoolsreducer  r	   true_bitr   r  rB  rC  r   )
ru   r;   r   r   target_shaper   nd_diff
dim_is_oneacceptedr   rA   r  rB   _bc_adjust_dimension  s,   	

r  c           
         s   g }g }|  tjd|  tjd fddt||D } fdd|D } fddt||D }	 fddt|	||D } fddt|	|D }||fS )	z
    Broadcast shapes and strides to target_shape given that their ndim already
    matches.  For each location where the shape is 1 and does not match the
    dim for target, it is set to the value at the target and the stride is
    set to zero.
    r   rD   c                    s   g | ]\}}  d ||qS )r?  r   )r   taroldr;   rA   rB   r   C      z,_bc_adjust_shape_strides.<locals>.<listcomp>c                    r  r  r  )r   r  r  rA   rB   r   E      c                    s   g | ]
\}}  ||qS rA   )r  )r   xyr  rA   rB   r   F      c                    s    g | ]\}}}  |||qS rA   r  )r   pr  r  r  rA   rB   r   G  s    c                    s   g | ]\}}  ||qS rA   r  )r   r  r  )r;   r   rA   rB   r   I  r   )r   r   r   r   )
ru   r;   r   r   r  	bc_shapes
bc_stridesmismatch
src_is_onepredsrA   )r;   r  r   rB   _bc_adjust_shape_strides7  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rh   r  r$  )r	   r   r]   r   r  r  copyr   rq   r   r   rb   r   r   r   )	ru   r;   arrtyper  r  r   r   new_arrtypenew_arrrA   rA   rB   _broadcast_to_shapeN  s&   


r  c                 C   s,   |j |jddd}|||}dd }||fS )Nr  T)rh   r  readonlyc           
   	   S   sz   |\}}|j d }t|| ||}t||}t| ||||\}}t|dt| |j	dd  |
 }	t| ||j|	S )Nr   r   )r   rq   r	   r   r  r   r   r   r   r   r   r    r   )
ru   r;   rT   r   r  shape_r  r  destr   rA   rA   rB   codegenm  s   
z$_numpy_broadcast_to.<locals>.codegen)r  r   )	typingctxr   r]   retrT   r  rA   rA   rB   _numpy_broadcast_toh  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   rq   r	   get_null_valuer   r7   r   r    r   )ru   r;   rT   r   r  r  r  r   rA   rA   rB   r    s   
z#get_readonly_array.<locals>.codegen)r  )r  r  r  rT   r  rA   rA   rB   get_readonly_array  s   	r  c           
      C   s   | j }t|}t|}||krtd|D ]
}|dk rtdqd}|| }||k rM|| }|| }	||	ks:|dkrC|d7 }|d7 }ntd||k s*d S d S )NzDinput operand has more dimensions than allowed by the axis remappingr   z4all elements of broadcast shape must be non-negativerD   z=operands could not be broadcast together with remapped shapes)r]   r   r   )
r   
dest_shape	src_shapesrc_ndim	dest_ndimr   	src_index
dest_indexsrc_dimdest_dimrA   rA   rB   _can_broadcast  s&   
r&  c                 C   s   t | } t| | t| |S r+  )npasarrayr&  r  r   r]   rA   rA   rB   _default_broadcast_to_impl  s   


r*  c                 C   s   t | s	tdt|tjrdd }|S t|tjr+t|jtjs)d}t|tS t|tj	rL|j
dkrLtdd |D sJd| d	}t|tS t|tj	ru|j
dkrut| tjoa| jdk}t| sh|rnd
d }|S d}t|d| }t|)Nz-The first argument "array" must be array-likec                 S   s   t | |fS r+  )r'  broadcast_tor)  rA   rA   rB   r        z numpy_broadcast_to.<locals>.implz7The second argument "shape" must be a tuple of integersr   c                 S      g | ]}t |tjqS rA   )r   r   IntegerLiteral)r   typrA   rA   rB   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)  rA   rA   rB   r     s   
z/Cannot broadcast a non-scalar to a scalar arrayz:The argument "shape" must be a tuple or an integer. Got %s)r   r   r   r   r   r   UniTuplers   r*  Tupler   allr   rh   r   )r   r]   r   r   is_scalar_arrayrA   rA   rB   numpy_broadcast_to  s4   




r5  c                 C   sr   t t|D ]0}|t| | }|| }|dk rtd|dkr!q| | dkr,|| |< q| | |kr6tdqd S )Nr   #negative dimensions are not allowedrD   z=shape mismatch: objects cannot be broadcast to a single shape)r6   r   r   )rmr]   re   r   tmprA   rA   rB   numpy_broadcast_shapes_list  s   
r:  c                     s   t | D ]7\}}t|tj}t|tjot|jtj}t|tjo't|jdk}|s;|s;|s;d| d| }t	|qd | D ]}t|tjrNt
 d q@t|tjr[t
 t| q@ dkrddd S d   fdd	}|S )
Nr   z	Argument z* must be either an int or tuple[int]. Got rD   c                  W   ru  rv  rA   r   rA   rA   rB   <lambda>	      z+ol_numpy_broadcast_shapes.<locals>.<lambda>rD   c                     sz   dg  }}t | D ] }t|trt|dkrt| | qt|tr+t| |f qt|D ]
\}}t|||}q0|S r~  )r   r   r   r   r:  r   	enumerater.   )r   r7  r3  argr   elemr8  tup_initrA   rB   r     s   

z'ol_numpy_broadcast_shapes.<locals>.impl)r?  r   r   r   r1  rs   r2  r   r   r   maxr5  )r   r   r@  is_intis_int_tupleis_empty_tupler   r   rA   rB  rB   ol_numpy_broadcast_shapes  s0   
rH  c                     s   t | D ]\}}t|std| dqd }d }| D ]$}t|tjtjfr+|j}n|}|d u r4|}q||kr@td|  qd t | D ])\}}t|tj	rXt
 |j qGt|tjtjtjfrit
 d qGtd| d   fdd	}|S )
Nz
Argument "z" must be array-likezTMismatch of argument types. Numba cannot broadcast arrays with different types. Got r   rD   zUnhandled type r   c                     s|   dg  }t | D ]}t| t|j q	}t D ]
}t|||| }qg }t | D ]}|tt|| q-|S rs  )	r   r:  r'  r(  r]   r6   r.   ra   r+  )r   r]   r   r3  re   outsrB  rA   rB   r   ?  s   
z$numpy_broadcast_arrays.<locals>.impl)r?  r   r   r   r   r   r   r5  rs   ArrayCompatiblerD  rh   Numberr  )r   r   r@  unified_dtypedtr   rA   rB  rB   numpy_broadcast_arrays  s6   rO  c                 C   rK  )zTargets should implement this if they wish to specialize the error
    handling/messages. The overload implementation takes two tuples as arguments
    and should raise a ValueError.r   r  index_shaperA   rA   rB   raise_with_shape_contextV  rP  rR  generic)targetc                 C   N   t | tjrt |tjr!| j|jkr#t | jtjr%dd }|S d S d S d S d S )Nc                 S      t d)Nz0cannot assign slice from input of different sizer   rP  rA   rA   rB   r   f  rc  z1ol_raise_with_shape_context_generic.<locals>.implr   r   r1  rs   r   r  rQ  r   rA   rA   rB   #ol_raise_with_shape_context_generic]  s   
rY  CPUc                 C   rU  )Nc                 S   s   t | dkrd| d  d}ndddd | D  d}t |dkr,d|d  d}nddd	d |D  d}d
| d| }t|)NrD   (r   z,), c                 S      g | ]}t |qS rA   strr   r  rA   rA   rB   r   v  r  zAol_raise_with_shape_context_cpu.<locals>.impl.<locals>.<listcomp>)c                 S   r]  rA   r^  r`  rA   rA   rB   r   z  r  zcannot assign slice of shape z from input of shape )r   joinr   )r  rQ  	shape_str	index_strr   rA   rA   rB   r   r  s   z-ol_raise_with_shape_context_cpu.<locals>.implrW  rX  rA   rA   rB   ol_raise_with_shape_context_cpuk  s   
re  c           '         s:  |j \}}|\}}t| |}t |j}	t |j}
|j}t ||||}|  dd }t	t
jrۈj| }t t |\t j}t j}j}tj}t|t|ksrJ t||D ]\}} | d||}qw j|dd | || W d   n1 sw   Y  t |||\}}||
|j\}}t |||\}}t ||||}t ||||\}}nmt	t
jr<j| }t|dksJ ttt
j}| f} d|d |} j|dd | |f|d f W d   n	1 s(w   Y   fd	d
}dd }nfdd
}dd }t
j d} |! \}!}"t"|"}"|j#D ]	}#|"$|#|  q\d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.
    c                 S   s~   | j t}ttjt|ttjt|f}|| j |i }| ||}||| 	||j
d || 	||j
d |f d S Nr   rD   )typing_contextresolve_value_typerR  r   r1  int64r   get_call_typerJ   
make_tupler   )ru   r;   r  rQ  fntyargtys	raise_sigfuncrA   rA   rB   raise_shape_mismatch_error  s   
z2fancy_setslice.<locals>.raise_shape_mismatch_errorr?  Fr@  NrD   r   c                    s,   | \} tjttj}| |fS r+  )rJ   operatorgetitemr$   r   r   )r  r   getitem_implr;   ru   r  	src_dtyper  rA   rB   r    s   z#fancy_setslice.<locals>.src_getitemc                   S   rq  r+  rA   rA   rA   rA   rB   r    rr  z#fancy_setslice.<locals>.src_cleanupc                    s    S r+  rA   )r  )r  rA   rB   r    rr  c                   S   rq  r+  rA   rA   rA   rA   rB   r    rr  c                 S   s   g | ]}|d ur|qS r+  rA   )r   r  rA   rA   rB   r     r
  z"fancy_setslice.<locals>.<listcomp>r  ),r   rq   r	   r   r]   r   r   r  rM  r   r   r   rs   rQ  r  r  r   r   r  r   r   r  r  r   r  r  r  SequencerJ   r$   r   r   r   r  r   r  r  r   r  r  r&  r   r  rG  )'ru   r;   rT   r   r   r   r   r  r   dest_shapesdest_strides	dest_datar  rp  rQ  r  r  r  shape_errorur   	src_startsrc_end
dest_lower
dest_upper
dest_startdest_endr  r  r  len_implseq_lenr   dest_indicesr  re   r  r   r  rA   rt  rB   r7    s   








r7  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   rD   c                    "   g | ]\}}  ||tjqS rA   r   r   r   )r   r-  r   r   rA   rB   r   #      z#vararg_to_tuple.<locals>.<listcomp>rs   r   )r   r   r	   rb   r7   r   r1  r   r   r   r$   r   )ru   r;   rT   r   r   dimtysr   rd   r]   shapetynew_signew_argsrA   r   rB   vararg_to_tuple  s   
r  zarray.transposec                 C   s   t | ||jd |d S r   )array_Tr   ru   r;   rT   r   rA   rA   rB   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   r]   r   rX  r  rA   rA   rB   permute_arrays4  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 ]
\}} || qBtj|	dddtj|
} fdd|D }t||D ]\}}t| ||
 tj|g|g|d d qo tttjd	d |D  t|j }t||j |d  |d
 |j|j|jd | }t |j|S )Nr   rD   c                       g | ]}t  qS rA   r	   rP   r   r  )r;   ll_ary_sizerA   rB   r   P  r
  z)array_transpose_tuple.<locals>.<listcomp>r   rs   rh   r  c                    s   g | ]	}t  qS rA   rq   r  )r;   ru   	np_ary_tyrA   rB   r   [  r  r   r]   r   r   r   c                 S   r  rA   r   r   arA   rA   rB   r   k  r  rF  r$  ) r   rq   r   rs   r   r   r   r   	ArrayTyper]   r   r   r   r   r   rt   r   r  
as_pointercompile_internalr  r   r$   voidr   r   r<   r   r   r   r   r    )ru   r;   rT   r   r   r   axistyr   num_axisrs   rY  arysll_arysr  dstnp_itemsizenp_arysnp_aryll_aryr  r   rA   )r;   ru   r  r  rB   array_transpose_tupleC  sP   
	r  c                 C       t | |||\}}t| |||S r+  )r  r  ru   r;   rT   r   r  r  rA   rA   rB   array_transpose_varargz     r  c                 C   s:   t | tjrtd|d u rddd}|S ddd}|S )Nz#np.transpose does not accept tuplesc                 S      |   S r+  	transposer  axesrA   rA   rB   np_transpose_impl  rc  z*numpy_transpose.<locals>.np_transpose_implc                 S   
   |  |S r+  r  r  rA   rA   rB   r    rx  r+  )r   r   r5  r   r   )r  r  r  rA   rA   rB   numpy_transpose  s   


r  Tc           	      C   s   |j dkr|}nFt|| ||}t|| |}t||j|j }t||j|j }t||jt||d d d t||d d d |j	|j
|jd | }t| |||S )NrD   r$  )rh   rq   r	   r   r]   r   r   r   rb   r   r   r   r   r    )	ru   r;   r/  r   r   r   r  r   r   rA   rA   rB   r    s    
r  2   c                 C   sT   t | tjstdt |tjstdt |ttjfs#t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  r  numr  rA   rA   rB   r     s   znumpy_logspace.<locals>.implr  )r   r   rL  r   r   r   r   )r  r  r  r   rA   rA   rB   numpy_logspace  s   



r  c                    s   t | tjsd}t|t |tjsd}t|t |ttjfs)d}t|tdd | |fD rJtt	
t| t|d  d
 fdd	}|S d
d	d}|S )Nz%The argument "start" must be a numberz$The argument "stop" must be a number%The argument "num" must be an integerc                 s   r*  r+  r   r   Complexr   r@  rA   rA   rB   r.    r/  z"numpy_geomspace.<locals>.<genexpr>r  c           	         s   | dks|dkrt d | }  |}tdk rH| jdk|jdk@ }t| dkt|dk@ }d}|r<| j} |j}d}|rG|  } | }| }nt| }| | } || }t| }t|}t|||}|dkrv| |d< |dkrv||d< || S )Nr   &Geometric sequence cannot include zerorE  r  rD   y              ?)r   r   realr'  signimaglog10logspace)	r  r  r  both_imaginaryboth_negativeout_signlogstartlogstopr  result_dtyperA   rB   r     s8   


znumpy_geomspace.<locals>.implc                 S   s   | dks|dkrt dt| dkt|dk@ }d}|r'|  } | }| }t| }t|}t|||}|dkrH| |d< |dkrH||d< || S )Nr   r  r  rD   )r   r'  r  r  r  )r  r  r  r  r  r  r  r  rA   rA   rB   r     s    

r  )r   r   rL  r   r   r   r   r0  r   r'  result_typer   )r  r  r  r   r   rA   r  rB   numpy_geomspace  s"   


8
r  rD   c                 C   sR   t |ttjfstdt | tjstd| jdk r"tdddd}|S )	Nz*The second argument "k" must be an integerz'The first argument "m" must be an arrayrF  Input must be >= 2-d.rD   c                 S   sp   |d }|dkr| d d  S |dkrt t | ddS |dkr(t t | S |dkr6t t | ddS t)N   r   rD   rF     )r'  swapaxesfliplrflipudr  )r8  r   rA   rA   rB   r     s   znumpy_rot90.<locals>.implr>  )	r   r   r   r   r   r   r   rh   NumbaValueError)r8  r   r   rA   rA   rB   numpy_rot90   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.
    numba_attempt_nocopy_reshaper]   r   r   )r   r   r   r  intcr   FunctionTyper	   get_or_insert_functionr8   rh   r`   r_   callr   )ru   r;   r   r   newndnewshape
newstridesrY  ll_intp_starll_intcrl  fnndr]   r   
is_f_orderr   rA   rA   rB   _attempt_nocopy_reshape  s2   	

r  c           	      C   s   d}d}t |D ]\}}|dk r|d7 }|}q||9 }q|dkr*| |kr(tdd S |dkrO|dkr9d}| dk}n
| | }| | dk}|sItd|||< d S td)Nr   rD   z)total size of new array must be unchangedzmultiple negative shape values)r?  r   )	origsizer]   num_neg_value
known_sizer  r   neg_axinferredokrA   rA   rB   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 sw   Y  t|| |}t||
j"|#||#||
j$|
j%|
j&d | }t'| ||j|S )	Nr   rD   r   r  r  r  zincompatible shape for arrayr$  )(r   r   r   r   r   r   r  r   rq   r	   rP   r   r   rs   r   rt   r   r  r  r   r  r  r   r$   r  r   r   r  r  r7   r   rB  rC  rK   r   r<   r   r   r   r    )ru   r;   rT   r   r   rettyr  r]   rY  ll_shaper   r  shape_ary_ty	shape_aryshape_itemsizer   r  r  r  failr   r  r   rA   rA   rB   array_reshape]  sb   


r  c                 C   r  r+  )r  r  r  rA   rA   rB   array_reshape_vararg  r  r  )rF  rD   c                 C      dd }|S )Nc                 S   r  r+  reshape)r  r  rA   rA   rB   np_reshape_impl  rx  #np_reshape.<locals>.np_reshape_implrA   )r  r  r  rA   rA   rB   
np_reshape     r  c                 C   r  )Nc                 S   r  r+  r  )r  r]   rA   rA   rB   r    rx  r  rA   )r  r]   r  rA   rA   rB   r    r  c                 C   sV   t | sd}t|t|tjrt|jtjs%t|tjs%d}t|dd }|S )N#The argument "a" must be array-likezBThe argument "new_shape" must be an integer or a tuple of integersc           	      S   s   t | } t | } t|tr(d}t |D ]}||9 }|dk r&d}t|qn|dk r2d}t||}| jdkrBt || j	S | | j  }| }t
|d D ]	}t || f}qQ|d | }t ||S )NrD   r   z0All elements of `new_shape` must be non-negative)r'  r(  ravelr   r   r   r   zerosastypers   r6   concatenater  )	r  	new_shapenew_size
dim_lengthr   msg2repeatsr   re   rA   rA   rB   r     s.   



znumpy_resize.<locals>.impl)r   r   r   r   r   r1  rs   r   )r  r  r   r   rA   rA   rB   numpy_resize  s   


r	  c                 C   s^   t | s	tdt |stdt|rddd}|S t|tjs(t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'  r   r(  r  r  valuesr   rA   rA   rB   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
  rA   rA   rB   r        r+  )r   r   r   r   r   r   r   )r  r  r   r   rA   rA   rB   	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  r   r   rA   rA   rB   
imp_nocopy     zarray_ravel.<locals>.imp_nocopyc                 S   r  )zCopy version)flattenr  rA   rA   rB   imp_copy	     zarray_ravel.<locals>.imp_copyr   r   )r   r  r  r!   r   )ru   r;   rT   r   r  r  impr   rA   rA   rB   array_ravel  s   r  c                 C   s   dd }|  ||||S )Nc                 S   r  r+  )r   r  rA   rA   rB   np_ravel_impl	  rc  znp_ravel.<locals>.np_ravel_impl)r  )ru   r;   rT   r   r  rA   rA   rB   np_ravel	  s   r  zarray.flattenc                 C   s,   dd }|  ||||}t| ||j|}|S )Nc                 S   s   |   | jS r+  )r  r  r   r  rA   rA   rB   r  	  r  zarray_flatten.<locals>.imp)r  r!   r   )ru   r;   rT   r   r  r   rA   rA   rB   array_flatten	  s   r  c                 C   sl   |d u r	t | n|}t | ||\}}}t |jD ]}|| }	|| }
|| }tt|	|
|||< q|S r+  )r'  
empty_likebroadcast_arraysndindexr]   minrD  )r  a_mina_maxr   r  a_ba_min_ba_max_br   val_a	val_a_min	val_a_maxrA   rA   rB   _np_clip_impl%	  s   r)  c                 C   sH   t | jD ]}| | }|| }|rt||||< qt||||< q|S r+  )r'  r  r]   r   rD  )r  buse_minr   r   r&  val_brA   rA   rB   _np_clip_impl_none3	  s   r-  c                 C   s^  t | s	tdt|tjst |stdt|tjs't |s'tdt|tjs8t|s8d}t||d u pAt|tj}|d u pKt|tj}|rW|rWddd}|S t|tj}t|tj}	|rn|	rnddd}
|
S |r|	s|r{dd	d
}|S ddd}|S |s|	r|rddd}|S ddd}|S |rddd}|S |rddd}|S ddd}|S )Nr  z6The 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   rV  )Nz&array_clip: must set either max or minr   r  r!  r"  r   rA   rA   rB   
np_clip_nnX	  rc  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  r]   r   rD  r  r!  r"  r   r  r   r&  rA   rA   rB   
np_clip_ssa	  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  r]   rD  r0  rA   rA   rB   
np_clip_sno	  
   znp_clip.<locals>.np_clip_snc                 S   s   t | |}t| |||S r+  r'  	full_liker)  )r  r!  r"  r   
a_min_fullrA   rA   rB   
np_clip_sa|	     znp_clip.<locals>.np_clip_sac                 S   sB   |d u r	t | n|}t | jD ]}| | }t||||< q|S r+  )r'  r  r  r]   r   r0  rA   rA   rB   
np_clip_ns	  r3  znp_clip.<locals>.np_clip_nsc                 S   s   t | |}t| |||S r+  r4  )r  r!  r"  r   
a_max_fullrA   rA   rB   
np_clip_as	  r8  znp_clip.<locals>.np_clip_asc                 S   s4   |d u r	t | n|}t | |\}}t||d|S )NTr'  r  r  r-  )r  r!  r"  r   r  r#  r%  rA   rA   rB   
np_clip_na	     znp_clip.<locals>.np_clip_nac                 S   s4   |d u r	t | n|}t | |\}}t||d|S )NFr<  )r  r!  r"  r   r  r#  r$  rA   rA   rB   
np_clip_an	  r>  znp_clip.<locals>.np_clip_anc                 S      t | |||S r+  )r)  r.  rA   rA   rB   
np_clip_aa	  s   znp_clip.<locals>.np_clip_aar+  )	r   r   r   r   r   NoneTyper   r   rL  )r  r!  r"  r   r   a_min_is_nonea_max_is_noner/  a_min_is_scalara_max_is_scalarr1  r2  r7  r9  r;  r=  r?  rA  rA   rA   rB   np_clip?	  sV   












rG  clipc                 C   s   ddd}|S )Nc                 S   s   t | |||S r+  )r'  rH  r.  rA   rA   rB   r   	  r  zarray_clip.<locals>.implNNNrA   )r  r!  r"  r   r   rA   rA   rB   
array_clip	  s   
rJ  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r|d |kr}n
|d |kr}||kr'| ks%| dkr'dS |kr0| d }nd}||k rP|| dkr>dS || }||  |9  < |||< dS ||krn|| | }|| dkrbdS || ||< |||< dS 	 dS )Nr  r   FrD   TrA   )	r  rd   r   old_itemsizenew_itemsizer  re   newdim
bytelength
any_layoutc_layoutf_layoutint8rA   rB   r  	  s4   

z_change_dtype.<locals>.impr]   r   r   rD   r  Nr  )rh   r  ordr   rT  r   r   rw   r	   r`   r_   r   rq   rb   rt   rr   r   r   r$   booleanr  r   r    r   )ru   r;   oldtynewtyr   
new_layoutr  rL  rM  r  
shape_datastrides_datashape_strides_array_typearyclsshape_constantsizeof_intpstrides_constantr  strides_aryr]   r   r   rT   r   rA   rP  rB   _change_dtype	  s   	
0



	
rb  c                 C      t | s	tddd }|S )Nz+The argument to np.shape must be array-likec                 S      t | jS r+  )r'  r(  r]   r  rA   rA   rB   r   C
  rt  znp_shape.<locals>.implr   r   r   r  r   rA   rA   rB   np_shape>
     
rg  c                 C   rc  )Nz*The argument to np.size must be array-likec                 S   rd  r+  )r'  r(  r   r  rA   rA   rB   r   M
  rt  znp_size.<locals>.implre  rf  rA   rA   rB   np_sizeH
  rh  ri  c                 C      dd }|S )Nc                    sJ   t |   t d d } fddt dd  D }t || S )NrD   c                    s    g | ]\}} | |kr|qS rA   rA   )r   re   r  r*  rA   rB   r   Y
       z5np_unique.<locals>.np_unique_impl.<locals>.<listcomp>)r'  sortr   r   r?  r   )arheadtailrA   rk  rB   np_unique_implV
  s   z!np_unique.<locals>.np_unique_implrA   )rn  rq  rA   rA   rB   	np_uniqueT
  s   rr  c                 C   sh   dd }t | tjtjtjtjtjfr2t |tjrtS t |tjtjfr-t |j	tjr-|S t
dd S )Nc           	      S   s   t j|t jd}|jd dkrt| |d S t |dk r!tdt | }| }|jd }|j|jkr9tdt jt 	||j
d}d}t|D ]}|| |||||  < ||| 7 }qJ|S )Nrs   r   rD   r6  z(operands could not be broadcast together)r'  r(  ri  r]   np_repeat_impl_repeats_scalerr0  r   r   emptyr  rs   r6   )	r  r  repeats_arrayasaaraveln	to_returnposre   rA   rA   rB   !np_repeat_impl_repeats_array_likec
  s$   

z4np_repeat.<locals>.np_repeat_impl_repeats_array_likezIThe repeats argument must be an integer or an array-like of integer dtype)r   r   r   Listr5  rL  r  r   rt  rs   r   r   )r  r  r|  rA   rA   rB   	np_repeat^
  s    r~  c                 C   s   |dk rt dt| }| }|jd }|dkr"tjd|jdS |dkr+t|S tj|| |jd}t|D ]}|| ||| |d | < q9|S )Nr   r6  rs  rD   )	r   r'  r(  r   r]   ru  rs   r  r6   )r  r  rw  rx  ry  rz  re   rA   rA   rB   rt  
  s   


rt  repeatc                 C   r  )Nc                 S      t | |S r+  )r'  r  )r  r  rA   rA   rB   array_repeat_impl
  rt  z'array_repeat.<locals>.array_repeat_implrA   )r  r  r  rA   rA   rB   array_repeat
     r  c                 C      t |}dd }||fS )z"Computes the itemsize of the dtypec                 S   s,   |  |jd j}|  |j}|| |S r   )rr   r   rs   r   rt   )cgctxr;   rT   llargsrv   llintprA   rA   rB   r  
  s   z%_intrin_get_itemsize.<locals>.codegen)r   r   )tyctxrs   rT   r  rA   rA   rB   _intrin_get_itemsize
  s   
r  c                 C   rq  r+  rA   )r  rs   rA   rA   rB   _compatible_view
  rr  r  c                 C   r  )zDDetermines if the array and dtype are compatible for forming a view.c                 S   s   t |}|| jkrf| jdkrd}t|	 | jd }| j| dk}| jdk}| j| | jk}|r:|r:|r:d}t||| jk rR|dksJ| j| dkrPd}	t|	d S | j| | j }
|
| dkrhd}t|d S d S )Nr   zOChanging the dtype of a 0d array is only supported if the itemsize is unchangedrD   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   rh   r   r]   r   r   )r  rs   
dtype_sizemsg1r   p1p2p3r  msg3rN  msg4rA   rA   rB   r   
  s0   




z ol_compatible_view.<locals>.implrA   r  rs   r   rA   rA   rB   ol_compatible_view
  s   #r  z
array.viewc                 C   s<  |j d }|j}t|| ||d }t|| |}t|jj}t|D ]}	t||	}
|	dkr:|jj	}|
|
||_q#t||	|
 q#| j}|t}||g |j R i }| ||}||| t| ||||}|d|t|j	d}|| d}| j|t|f W d    n1 sw   Y  | }t| ||j|S )Nr   r   r  z"new type not compatible with array)r   r   rq   r   r   r   sortedgetattrr   r7   r  r   rg  rh  r  rj  rJ   rb  r  r   r   rB  rC  r   r   r    )ru   r;   rT   r   r   r  r   r  fieldsr   r   r  r  rl  _compatible_view_sigr   r  r  r   r   rA   rA   rB   
array_view
  s0   



r  rs   c                 C   s   |   }t| |||S r+  )rG  r"   ru   r;   r/  r   r   rA   rA   rB   array_dtype  s   r  r]   c                 C   (   t |}|| ||}|j}t| |||S r+  )rq   r]   r"   ru   r;   r/  r   r|   r   r   rA   rA   rB   array_shape     r  r   c                 C   r  r+  )rq   r   r"   r  rA   rA   rB   array_strides  r  r  rh   c                 C      |  tj|j}t| |||S r+  )r   r   r   rh   r"   r  rA   rA   rB   
array_ndim  s   r  r   c                 C   r  r+  )rq   r   r"   r  rA   rA   rB   
array_size%  s   r  r   c                 C   r  r+  )rq   r   r"   r  rA   rA   rB   array_itemsize-  r  r  nbytesc                 C   s2   t |}|| ||}||j|j}t| |||S )z"
    nbytes = size * itemsize
    )rq   r   r   r   r"   r  rA   rA   rB   array_nbytes6  s   r  
contiguousc                 C   r  r+  )r   r   rV  	is_contigr"   r  rA   rA   rB   array_contiguousB     r  c_contiguousc                 C   r  r+  )r   r   rV  is_c_contigr"   r  rA   rA   rB   array_c_contiguousH  r  r  f_contiguousc                 C   r  r+  )r   r   rV  is_f_contigr"   r  rA   rA   rB   array_f_contiguousN  r  r  r  c                 C   s    |  tj|j }t| |||S r+  )r   r   rV  mutabler"   r  rA   rA   rB   array_readonlyT  r  r  ctypesc           	      C   sP   t |}|| ||}t|}| ||}|j|_|j|_| }t| |||S r+  )rq   r   ArrayCTypesr   r   r   r   r    )	ru   r;   r/  r   r|   r   actctinfor   rA   rA   rB   array_ctypes\  s   
r  r   c                 C   s8   | j |||d}|j}||| tj}t| |||S Nr   )r   r   r  r   r   r   r"   )ru   r;   r/  r   r  r   rA   rA   rB   array_ctypes_datai  s   r  c                 C   s6   | j |||d}|j}||| |}t| |||S r  )r   r   r  r   r"   )ru   r;   fromtytotyr   r  r   rA   rA   rB   array_ctypes_to_pointerr  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   )rq   r   r1  r   rh   rt   r   rs   r$   bool_r]   r   r   r  )
checkerru   r;   r   r   tup_intpr   	check_sig
check_argsr  rA   rA   rB   _call_contiguous_check{  s   
r  r   c                 C   s>   |  |t|}||_| }| j||| t| |||S r+  )r   r   
ArrayFlagsr   r   r   r   r!   )ru   r;   r/  r   flagsobjr   rA   rA   rB   array_flags  s
   r  c                 C   sZ   |j jdkr| j|||d}tt| ||j |j}n|j jdk}| tj|}t	| |||S )Nr   r   )
r[   r  r   r  r   r   r   r   rV  r"   )ru   r;   r/  r   r  r   r   rA   rA   rB   array_flags_c_contiguous  s   r  c                 C   sr   |j jdkr| j|||d}tt| ||j |j}n|j j}|j jdkr'|dkn|dv }| tj	|}t
| |||S )NrK  r   rD   r  )r[   r  r   r  r   r   rh   r   r   rV  r"   )ru   r;   r/  r   r  r   r  r   rA   rA   rB   array_flags_f_contiguous  s   r  r  c                 C   sH   |j tjv rt| |||ddS |j tjv rt| |||S tdtj )Nr  attrzunsupported .real for {})	rs   r   complex_domainarray_complex_attrnumber_domainr    rK   r   r7   )ru   r;   r/  r   rA   rA   rB   array_real_part  s
   r  r  c                 C   s   |j tjv rt| |||ddS |j tjv rHt|jdd|}t| |||g\}}t| |||}t	
||j||j|jd t| ||j| S tdtj )Nr  r  Tr  r   zunsupported .imag for {})rs   r   r  r  r  r$   r  _parse_empty_like_argsr  r	   memsetr   r   r   r   r!   r   r   rK   r   r7   )ru   r;   r/  r   rT   r  r   r   rA   rA   rB   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rK||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      rD   r  rs   r  )r   r   )rs   r   r  rK   r   rq   underlying_floatrt   rr   r   r7   r   r  r  r   r  r   IntTyper  r   r	   copy_structr    r   )ru   r;   r/  r   r  r|   r   fltysizeof_fltyr   
llfltptrtyr   resulttyr  replrA   rA   rB   r    s    r  conj	conjugatec                 C   rj  )Nc                 S   s
   t | S r+  )r'  r  r  rA   rA   rB   r     rx  zarray_conj.<locals>.implrA   r  r   rA   rA   rB   
array_conj     r  c                 C   r  r+  )rG  )ru   r;   dtypetydtypevalrA   rA   rB   
dtype_type  r  r  r7   kindstatic_getitemc                 C   s<   |j }t|tjr| |d}t| |||S d}t|)zThis handles the "static_getitem" when a Numba type is subscripted e.g:
    var = typed.List.empty_list(float64[::1, :])
    It only allows this on simple numerical types. Compound types, like
    records, are not supported.
    NzlUnreachable; the definition of __getitem__ on the numba.types.abstract.Type metaclass should prevent access.)r   r   r   r   r   r"   r   LoweringError)ru   r;   rT   r   r  r   r   rA   rA   rB   static_getitem_number_clazz(  s   
r  c              	      s  t |}| ||}|j}t|tjstd||f ||}||}	t|tjr9|j	|j|j
|j
 dd}
n|j	|dd}
t |
}| |} tj|	}tj||j||jjd}t|tjrt||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                       g | ]	}  tj|qS rA   r   r   r   r  ru   rA   rB   r   b  r  z(array_record_getattr.<locals>.<listcomp>c                    r  rA   r  r  r  rA   rB   r   e  r  r$  )rq   rs   r   r   RecordrK   typeofr   NestedArrayr  rh   r   r   r	   r   r   r7   r   r]   r   rt   rr   r   r   r   r   r    )ru   r;   r/  r   r  r|   r   rectypers   r   restyrarytyraryconstoffset
newdataptrr]   r   datasizer   rA   r  rB   array_record_getattr@  sN   


r  c                 C   s0   |d }t |tstt| ||jd |d |S r~  )r   r_  rK   r  r   )ru   r;   rT   r   r   rA   rA   rB   array_record_getitemx  s   
r  c              	      s    || ||}||}t|tjret|}| |}|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rudnd} ||||}t |||S )zT
    Generic getattr() implementation for records: get the given record member.
    c                    r  rA   r  r   r   r  rA   rB   r     r  z"record_getattr.<locals>.<listcomp>c                    r  rA   r  r  r  rA   rB   r     r  Nr$  rD   )sentry_record_alignmentr   r  r   r   r  rq   rs   r]   r   r	   get_record_memberrr   r   rb   r   r   r   r   r    rz   r{   )ru   r;   r/  r   r  r   elemtyr   r   rs   r  r  newdatar   dptrry   rA   r  rB   record_getattr  sB   







	
r  c              	   C   s   |j \}}|\}}| || ||}	||}
t|
tjrHt|| ||d d}|j	}t
|||	|jj}t|||| tj|
j dS t
|||	| |
}| ||||
}|jr`dnd}| j||
|||d dS )zT
    Generic setattr() implementation for records: set the given record member.
    rD   r   Nrx   )r   r  r   r  r   r   r  r	   rp   r   r  r7   pointeememcpyr   r   r   rr   r   rz   r~   )ru   r;   rT   r   r  r/  r8  rT  r   r   r  
val_structr  r  r  ry   rA   rA   rB   record_setattr  s,   





r   c                 C   s4   |  |jd |d }|| ||jd |d |d S )3
    Record.__getitem__ redirects to getattr()
    r   rD   )get_getattrr   )ru   r;   rT   r   r   rA   rA   rB   record_static_getitem_str  s   r  c                 C   s^   |j d j}t|j d j}| |j|| }| |j d |}|| ||j d |d || S )r  rD   r   )r   r   r   r  insert_const_stringr8   r  )ru   r;   rT   r   r   r  ll_fieldr   rA   rA   rB   record_static_getitem_int  s
   r  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)ru   r;   rT   r   rectyr  r8  recr   r   getattr_sigr   rA   rA   rB   record_static_setitem_str  s   
r  c                 C   s^   |j \}}}|\}}}	t|j||}
t|j d j}| || |
}|dus(J ||||	fS )r  r   N)r   r$   r   r   r  r	  )ru   r;   rT   r   r
  r  r8  r  r   r   r  r  r   rA   rA   rB   record_static_setitem_int  s   
r  c                 C   s   |  |||S )zB
    Create a constant array (mechanism is target-dependent).
    )make_constant_array)ru   r;   r-  pyvalrA   rA   rB   constant_array  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   )ru   r;   r-  r  ltyr   rA   rA   rB   constant_record  s   r  c                 C   s"   t jt|t jd}| |||S )zM
    Create a constant array from bytes (mechanism is target-dependent).
    rs  )r'  r   r  uint8r  )ru   r;   r-  r  bufrA   rA   rB   constant_bytes  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+  )r]   r   r  r   r  r*  rA   rA   rB   array_is_impl  s
   
zarray_is.<locals>.array_is_impl)r   r	   r  r  )ru   r;   rT   r   atybtyr  rA   rA   rB   array_is  s
   
r  __hash__c                 C   s   dd S )Nc                 S   rq  r+  rA   r  rA   rA   rB   r<  ,  r=  zol_array_hash.<locals>.<lambda>rA   r  rA   rA   rB   ol_array_hash*  r  r   c                 C   
   t | dS )zq
    Return the Structure representation of the given *flatiterty* (an
    instance of types.NumpyFlatType).
    flat_make_flattening_iter_cls)
flatitertyrA   rA   rB   make_array_flat_cls2     
r&  c                 C   r!  )zv
    Return the Structure representation of the given *nditerty* (an
    instance of types.NumpyNdEnumerateType).
    ndenumerater#  nditertyrA   rA   rB   make_array_ndenumerate_cls:  r'  r+  c              	   C   s  |  tjd}|d}	|d ur|tj| tt|D ]T}
t	|||
}t
|||}||
 }|d||}t|| ||| |d urO||
 ||	 W d    n1 s^w   Y  ||| |d urq||
 q|d ur}|tj| ||	 ||	 d S )Nr   end_incrementr   )r   r   r   r[  r   r	   
false_byter  r6   r`   r   r<   r   	if_likelyri  	true_byterj  )ru   r;   rh   r]   r   end_flagloop_continue
loop_breakr   bbendrX  idxptrr   r   	in_boundsrA   rA   rB   _increment_indicesB  s.   

r6  c                 C   s*   t ||j|j}t| ||j||| d S r+  )r	   r   r]   rh   r6  )ru   r;   r  r  r   r0  r]   rA   rA   rB   _increment_indices_arrayc  s   r7  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).
    rD   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_dimrh   )rR   r8  r9  r:  r;  rA   rA   rB   rZ  x  s
   z-make_nditer_cls.<locals>.BaseSubIter.__init__c                 S   s   t | j| j| d S r+  )r   r8  r9  )rR   rc   rA   rA   rB   set_member_ptr     z3make_nditer_cls.<locals>.BaseSubIter.set_member_ptrc                 S   s   t | j| jS r+  )r  r8  r9  rL  rA   rA   rB   
member_ptr     z/make_nditer_cls.<locals>.BaseSubIter.member_ptrc                 S   rq  r+  rA   rR   ru   r;   rA   rA   rB   init_specific  rr  z2make_nditer_cls.<locals>.BaseSubIter.init_specificc                 S   rq  r+  rA   rR   ru   r;   logical_dimrA   rA   rB   r1    rr  z2make_nditer_cls.<locals>.BaseSubIter.loop_continuec                 S   rq  r+  rA   rB  rA   rA   rB   r2    rr  z/make_nditer_cls.<locals>.BaseSubIter.loop_breakN)ri   rj   rk   rU  rZ  r<  r  cached_propertyr>  rA  r1  r2  rA   rA   rA   rB   BaseSubIters  s    
rE  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   r   r<  r	   r   )rR   ru   r;   r   rA   rA   rB   rA    s   z2make_nditer_cls.<locals>.FlatSubIter.init_specificc                 S   s   | | j}||j|gS r+  )r<   r>  r  r   )rR   ru   r;   r   r  r  r   rA   rA   rB   compute_pointer  s   z4make_nditer_cls.<locals>.FlatSubIter.compute_pointerc                 S   s<   || j d kr|| j}t||}||| j d S d S rs  )rh   r<   r>  r	   r   r   )rR   ru   r;   rC  r   rA   rA   rB   r1    s
   z2make_nditer_cls.<locals>.FlatSubIter.loop_continuec                 S   sd   |dkr| tjd}||| j d S || jd kr0|| j}t||}||| j d S d S rf  )	r   r   r   r   r>  rh   r<   r	   r   )rR   ru   r;   rC  r   r   rA   rA   rB   r2    s   z/make_nditer_cls.<locals>.FlatSubIter.loop_breakN)ri   rj   rk   rU  rA  rF  r1  r2  rA   rA   rA   rB   FlatSubIter  s    rG  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)  rA   rB   rA    r,  z9make_nditer_cls.<locals>.TrivialFlatSubIter.init_specificc                 S   s&   t |dksJ t |||j|S rs  )r   r  r   rR   ru   r;   r   r  r  rA   rA   rB   rF    s   z;make_nditer_cls.<locals>.TrivialFlatSubIter.compute_pointerN)ri   rj   rk   rU  rA  rF  rA   r)  rA   rB   TrivialFlatSubIter  s    rJ  c                   @      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 NFr  )r   rh   r	   r  rI  rA   rA   rB   rF    s   z7make_nditer_cls.<locals>.IndexedSubIter.compute_pointerNri   rj   rk   rU  rF  rA   rA   rA   rB   IndexedSubIter      rN  c                   @   rK  )z'make_nditer_cls.<locals>.ZeroDimSubIterz5
        Sub-iterator "walking" a 0-d array.
        c                 S   s   |j S r+  r  rI  rA   rA   rB   rF    ra  z7make_nditer_cls.<locals>.ZeroDimSubIter.compute_pointerNrM  rA   rA   rA   rB   ZeroDimSubIter  rO  rP  c                   @   rK  )z&make_nditer_cls.<locals>.ScalarSubIterz8
        Sub-iterator "walking" a scalar value.
        c                 S   s   |S r+  rA   rI  rA   rA   rB   rF    rr  z6make_nditer_cls.<locals>.ScalarSubIter.compute_pointerNrM  rA   rA   rA   rB   ScalarSubIter  rO  rQ  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 ]\}}|\}}}}d| }	|| }
||
| |	|| q|S )N)r"  indexed0dscalarzindex%d)rH  r?  r  ra   )rR   l	factoriesre   r  r  r:  r;  r  r9  factory)rG  rN  rQ  rJ  rP  r*  rA   rB   subiters  s   
z(make_nditer_cls.<locals>.NdIter.subitersc                    s2  | tjd}||t||| _t|D ]\}}t|tjs2d| }t	
||| }	t| ||	 q| ||||}ttj}
d}d}t|D ]\}}t|tjre|jkre|| j}|| j} nqJdkslJ |||
d}| tjd}dd }t||D ]'\}}t|tjr|jdkrttjttj|j|
}|||||j|f qt	||} 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%dNrA   rD   c                 S   s>   t | }t|D ]}| | |t || |  krtdqd S )Nz2nditer(): operands could not be broadcast together)r   r6   r   )r]   
main_shapery  re   rA   rA   rB   check_shape  s   zBmake_nditer_cls.<locals>.NdIter.init_specific.<locals>.check_shaperK  r  r  r`  )&r   r   r   rk  r2  arraysr?  r   r   r	   r   r   _arrays_or_scalarsr1  rh   r]   r   r   r$   noner  r   r   r  r/  r-  rH  r   rP   r7   r6   r`   r   r   rb   	exhaustedrX  rA  )rR   ru   r;   arrtysr\  r   re   r-  r9  slotmain_shape_tyrZ  main_nitemsr  r[  r  rT   r   shape_is_emptyr_  r   rX  r4  subiter)r  rh   r*  nshapesrA   rB   rA    sl   




z-make_nditer_cls.<locals>.NdIter.init_specificc                    s>  | d}t||| j}t|| |d || W d   n1 s*w   Y   j}t	|| j}| 
||||}| j}|d | |||||}	dd |	D }	t|	dkrg||	d  n||| j|	 t	|| j}
t||t|
|
|| jt| j||t| j|| || || dS )	zJ
            Compute next iteration of the nditer() instance.
            r  FNTc                 S   r  rA   r  r   r   rA   rA   rB   r   R  r  zEmake_nditer_cls.<locals>.NdIter.iternext_specific.<locals>.<listcomp>rD   r   )r[  r	   as_bool_bitr<   r_  if_unlikelyr   ri  r\  r   r]  r   _make_viewsr   r   rk  r   r]   r6  r  partial_loop_continue_loop_breakrj  )rR   ru   r;   r  r3  r_  r`  r\  r   viewsr]   r)  rA   rB   iternext_specific=  sD   





z1make_nditer_cls.<locals>.NdIter.iternext_specificc                 S   @   | j D ]}|j|  kr|jk rn q|||||j  qd S r+  )rX  r:  r;  r1  rR   ru   r;   rX  r  rA   rA   rB   rl  g  
   
z.make_nditer_cls.<locals>.NdIter._loop_continuec                 S   rp  r+  )rX  r:  r;  r2  rq  rA   rA   rB   rm  l  rr  z+make_nditer_cls.<locals>.NdIter._loop_breakc                    s   dg }j }| j}j}	t|	tjrt|	}	n|	g}	 fddtD t||D ]=\}
}|
\}}}}|j	|j
 }dkrJ|ddd }|D ]}|| du sVJ | | ||	| || || |||< qLq-tdd |D svJ |S )z:
            Compute the views to be yielded.
            Nc              	          g | ]}  t |qS rA   r<   r	   r`   r  r;   r   rA   rB   r   }  s    z?make_nditer_cls.<locals>.NdIter._make_views.<locals>.<listcomp>rK  r  c                 s   s    | ]}|V  qd S r+  rA   rg  rA   rA   rB   r.    s    z>make_nditer_cls.<locals>.NdIter._make_views.<locals>.<genexpr>)r  rX  r   r   r   r5  r   r6   r   r:  r;  
_make_viewr3  )rR   ru   r;   r   r`  r\  rn  r  rX  rettysr  re  r  array_indicessub_indicesre   )r  narraysr*  rf  ru  rB   rj  q  s0   



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   rA   N)r   )r   r   r   rh   rF  rq   rw   rk  r1  r   r   )rR   ru   r;   r   r  r  r  re  rc   viewr   r]   r   rA   rA   rB   rv    s   
z*make_nditer_cls.<locals>.NdIter._make_viewc           	      S   s`   g }t t||D ]$\}\}}t|tjr#||||||d q	|t| d|  q	|S )Nr   rY  )r?  r   r   r   r   ra   rq   r  )	rR   ru   r;   r`  r\  rU  re   r  r  rA   rA   rB   r]    s   
z2make_nditer_cls.<locals>.NdIter._arrays_or_scalarsN)ri   rj   rk   rU  r  rD  rX  rA  ro  rl  rm  rj  rv  r]  rA   
rG  rN  rQ  rJ  rP  r  rz  rh   r*  rf  rA   rB   NdIter  s    O*r}  )rh   r  r   r\  rH  objectr	   rp   )r*  rE  r}  rA   r|  rB   make_nditer_clsi  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 ]7}t	|||}|
|| || }	|d|	|}
t||
 |
tj| W d    n1 sRw   Y  q || _|| _t|||j| _d S Nr   r`  r  )r   r   r   r	   rP   r7   r   r-  r6   r`   r   r  ri  r/  r   r_  rb   r]   )rR   ru   r;   r   r   r   r_  rX  r4  r  dim_is_emptyrh   rA   rB   rA    s&   
z3make_ndindex_cls.<locals>.NdIndexIter.init_specificc           
         s   | tjd} d}t  j}t | |	d  
| W d    n1 s1w   Y   fddtD }|D ]}t | qD|t ||j |	d t j}	t| |	jj  
|  | d S )Nr   r  Fc              	      s"   g | ]}  t j|qS rA   )r<   r	   r`   r   r   rX  r;   rR   rA   rB   r     s    

zKmake_ndindex_cls.<locals>.NdIndexIter.iternext_specific.<locals>.<listcomp>T)r   r   r   r[  r	   rh  r<   r_  ri  r   ri  r6   rF   r   rb   r7   r   r]   r6  r   rj  )
rR   ru   r;   r  r   r3  r_  r   r<   r]   r  r  rB   ro    s,   





z7make_ndindex_cls.<locals>.NdIndexIter.iternext_specificN)ri   rj   rk   rU  rA  ro  rA   r  rA   rB   NdIndexIter  s    r  )rh   r	   rp   )r*  r  rA   r  rB   make_ndindex_cls  s   7r  c                    sX    dv sJ | j }|jdkrG  fdddt| }|S G  fdddt| }|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r?tj||j	| tj|j
d}t|j
D ]}t|||}||| q*|| _d S d S )Nr   r(  r`  )r   r   r   r	   r   r   r   r  rP   r7   rh   r6   r`   r   r   )	rR   ru   r;   r  r  r   r   rX  r4  r  rA   rB   rA    s   
zD_make_flattening_iter_cls.<locals>.CContiguousFlatIter.init_specificc                    s   |j }|j} | j} d||}	||	 t |	W  |j	|g}
t
| ||
}dkr8|| n(| j fddt|D }t |}|t ||g t| || t |} || j W d    d S 1 sxw   Y  d S )Nr   r"  c              	      rs  rA   rt  r  ru  rA   rB   r   &      

z\_make_flattening_iter_cls.<locals>.CContiguousFlatIter.iternext_specific.<locals>.<listcomp>)rh   r   r<   r   r   r   r	   r.  r  r   r}   r   r   r6   rb   make_anonymous_structr7  r   r   )rR   ru   r;   r  r  r  rh   r   r   r  rc   r   idxvalsidxtupler  ru  rB   ro    s4   
"zH_make_flattening_iter_cls.<locals>.CContiguousFlatIter.iternext_specificc                 S   s   | |j|g}t||||S r+  )r  r   r}   rR   ru   r;   r  r  r   rc   rA   rA   rB   rr  4  s   z>_make_flattening_iter_cls.<locals>.CContiguousFlatIter.getitemc                 S   s$   | |j|g}t||||| d S r+  )r  r   r   rR   ru   r;   r  r  r   r   rc   rA   rA   rB   setitem8  s   z>_make_flattening_iter_cls.<locals>.CContiguousFlatIter.setitemN)ri   rj   rk   rU  rA  ro  rr  r  rA   r  rA   rB   CContiguousFlatIter  s    r  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 ]D}t||	|}t||
|}||| ||| || }|d||}t|| |tj| W d    n1 s}w   Y  q>|	| _|
| _|| _d S r  )r   r   r   r   rh   r	   r   r]   rP   r7   r   r-  r6   r`   r   r  ri  r/  r   pointersr_  )rR   ru   r;   r  r  r   r   rh   r   r   r  r_  rX  r4  ptrptrr  r  rA   rA   rB   rA  G  s>   
z9_make_flattening_iter_cls.<locals>.FlatIter.init_specificc              	      sH  |j }t |j|}t |j|}| j| j}	|tj	d}
 
d}t  | j}t | |d  | W d    n1 sJw   Y  |d t |	|d } |}t| ||}dkrs|| n fddt|D }t |}|t ||g tt|D ]y}t |}t  |}|| }|| } d	||}t |C  || t |	|} |}t ||} || t|d |D ]}t |	|} || q | W d    n	1 sw   Y   |
| q tj| j  |  | d S )
Nr   r  FTrD   r"  c              	      rs  rA   rt  r  ru  rA   rB   r     r  zQ_make_flattening_iter_cls.<locals>.FlatIter.iternext_specific.<locals>.<listcomp>r   )rh   r	   r   r]   r   r   r  r   r   r   r[  rh  r<   r_  ri  r   ri  r`   r}   r   r6   rb   r  r  r   r   r.  r   r   r/  rj  )rR   ru   r;   r  r  r  rh   r   r   r  r   r3  is_exhaustedlast_ptrrc   r   r  r  rX  r4  r   r   r  r5  r  	inner_dimr  ru  rB   ro  h  sn   





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   )rh   r	   r   r]   r   r  r6   ra   uremudivreverser  r   r  )rR   ru   r;   r  r  r   rh   r   r   r   rX  rc   rA   rA   rB   _ptr_for_index  s   z:_make_flattening_iter_cls.<locals>.FlatIter._ptr_for_indexc                 S   s    |  |||||}t||||S r+  )r  r}   r  rA   rA   rB   rr    s   z3_make_flattening_iter_cls.<locals>.FlatIter.getitemc                 S   s&   |  |||||}t||||| d S r+  )r  r   r  rA   rA   rB   r    s   z3_make_flattening_iter_cls.<locals>.FlatIter.setitemN)	ri   rj   rk   rU  rA  ro  r  rr  r  rA   r  rA   rB   FlatIter?  s    !Er  )r[   r  r	   rp   )r%  r  r[   r  r  rA   r  rB   r$    s   
D r$  r"  c                 C   sh   t t|}|| |}||_| |}|| ||dd}|| ||| | }t| |t||S Nr   )rS   )	r&  r   NumpyFlatTyper   rq   r_   rA  r   r    )ru   r;   r  r  flatiterclsflatiterarrclsr   rA   rA   rB   make_array_flatiter  s   

r  c                 C   Z   |j \}|\}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
| d S r  )r   r&  r[   rq   r   ro  )ru   r;   rT   r   r  r%  r  r  r  r  r  rA   rA   rB   iternext_numpy_flatiter     
r  c                 C   sj   |j d }|\}}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
|}t| ||j|S Nr   r   )r   r&  r[   rq   r   rr  r    r   )ru   r;   rT   r   r%  r  r   r  r  r  r  r   rA   rA   rB   iternext_numpy_getitem  s   

r  c                 C   sf   |j d }|\}}}t|}|| ||d}|j}	| |	}
|
| ||jd}|| ||	||| |  S r  )r   r&  r[   rq   r   r  rG  )ru   r;   rT   r   r%  r  r   r   r  r  r  r  rA   rA   rB   iternext_numpy_getitem_any  s   


r  c           	      C   sF   |j d }t|}|| ||d d}| |j}|| ||jd}|jS r  )r   r&  rq   r[   r   r   )	ru   r;   rT   r   r%  r  r  r  r  rA   rA   rB   iternext_numpy_getitem_flat
  s   
r  c           
      C   sr   |j \}|\}tt|}|| |}||_| |}|| ||dd}|| ||| | }	t	| ||j
|	S r  )r   r+  r   NumpyNdEnumerateTyper   rq   r_   rA  r   r    r   )
ru   r;   rT   r   r  r  	nditerclsr8  r  r   rA   rA   rB   make_array_ndenumerate  s   

r  c                 C   r  r  )r   r+  r[   rq   r   ro  )ru   r;   rT   r   r  r*  r8  r  r  r  r  rA   rA   rB   iternext_numpy_nditer'  r  r  c                    s^    fddt |j|D }ttt|}| }| | | }t |j	|S )zndindex(*shape)c                    r  rA   r  )r   argtyr@  r   rA   rB   r   ;  r  z&make_array_ndindex.<locals>.<listcomp>)
r   r   r  r   NumpyNdIndexTyper   rA  r   r    r   )ru   r;   rT   r   r]   r  r8  r   rA   r   rB   make_array_ndindex7  s   

r  c           
         s   |j j}|dkr%|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 rA   r  r	  r;   ru   r   rA   rB   r   P      z,make_array_ndindex_tuple.<locals>.<listcomp>)r   rh   r   rs   r	   r   r  r   r  r   rA  r   r    )
ru   r;   rT   r   rh   r3  r]   r  r8  r   rA   r  rB   make_array_ndindex_tupleF  s   
r  c                 C   s6   |j \}|\}t|}|| ||d}|| || d S r  )r   r  ro  )ru   r;   rT   r   r  r*  r8  r  rA   rA   rB   iternext_numpy_ndindex]  s
   r  c           	      C   sn   |j }|j}t|jd tjrt||d }n|d g}t|| |}|	| ||| |
 }t| |||S )z
    nditer(...)
    r   )r   r\  r   r   r   r5  r	   r   r  rA  r   r    )	ru   r;   rT   r   r*  r`  r\  r8  r   rA   rA   rB   make_array_nditeri  s   
r  c                 C   s2   |j \}|\}t|| ||d}|| || d S r  )r   r  ro  )ru   r;   rT   r   r  r*  r8  rA   rA   rB   iternext_numpy_nditer2}  s   r  c                 C   s4   |j \}}ttdt||k}t| ||j|S rs  )r   r   r   r  r   r"   r   )ru   r;   rT   r   arg1arg2r   rA   rA   rB   dtype_eq_impl  s   
r  c                 C   sb  t |}|| |}| |j}| tjt| |}| tjd}tt	dd}	|D ]}
|
||
}||d}||	||d}	q*|jdkrKd}nN|jdkrp|g}t|dd D ]}|||d | q[tt|}n)|jdkr|g}|dd D ]}|||d | q~t|}ntd|j|
||}||d}||	||d}	|j|	d	d
 | j|td W d   n1 sw   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.
    rD   r   rA   r   Nr  rK  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  )+rq   rr   rs   r   r   r   rw   r   r   r  smul_with_overflowr;  r  rh   r  r  ra   r   r   rK   r   r   rB  rC  r   get_preferred_array_alignmentuint32rG  MemInfoPointervoidptrTypeRefr7   r$   r  _call_allocatorr   meminfo_datar   r	   rb   r   r  r  )ru   r;   r  r   r]  r   datatyper   arrlenoverflowr   arrlen_multr   dimension_sizeallocsize_multr  rs   	align_valry   r   miparytypeclassargtypesr   r   r   shape_arraystrides_arrayrA   rA   rB   r    st   




	
r  	_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  ry   rA   rA   rB   r     rx  z _ol_array_allocate.<locals>.implrA   )r  r  ry   r   rA   rA   rB   _ol_array_allocate  s   r  c                 C   s   |  ||S )z9Trampoline to call the intrinsic used for allocation
    )r  )r  r   ry   rA   rA   rB   r    s   r  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   meminfo_alloc_aligned)ru   r;   r$   r   r  ry   r   rA   rA   rB   r    s   zintrin_alloc.<locals>.codegen)r   r  r  r$   )r  r  ry   r  r  rT   rA   rA   rB   r    s   r  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 ])}
|d||
 |	}t|| | j|td W d   n1 slw   Y  qH|S )	z;
    Parse the shape argument to an array constructor.
    c           
      S   s   |  tj}|j}t|}t|d|d > d }|j|k r&|||}|S |j|kr`|d||}	|j	|	dd | j
|td W d   n1 sLw   Y  |j|kr^|||}|S |}|S )z0Cast src to intp only if value can be maintainedrD   >Fr@  )z#Cannot safely convert value to intpN)r   r   r   rE   r   r  r   sextr   r   rB  rC  r   trunc)
ru   r;   src_tr  r   
intp_widthintp_irmaxvalr   	is_largerrA   rA   rB   safecast_intp  s(   



z#_parse_shape.<locals>.safecast_intprD   r   r   r   )znegative dimensions not allowedN)r   r   r   r   r   r5  r   r	   r   ra   r7   get_constant_genericr6   r   ri  rB  rC  r   )ru   r;   r-  r   r  rh   passed_shapesr   r   r   rX  is_negrA   rA   rB   _parse_shape  s(   r  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   r  )ru   r;   rT   r   arrshapetypearrshaper  rA   rA   rB   _parse_empty_args.  s   
r  c                 C   sT   |j d }t|tjr%t|| ||d d}tj||j|jd}|j	|fS |j	dfS )z_
    Parse the arguments of a np.empty_like(), np.zeros_like() or
    np.ones_like() call.
    r   r   r   rA   )
r   r   r   r   rq   r	   r   r]   rh   r   )ru   r;   rT   r   arytyper   r   rA   rA   rB   r  8  s   


r  c                 C   s&   t |tjrd|  d}t|d S )NzIf np.z0 dtype is a string it must be a string constant.)r   r   UnicodeTyper   r   )fnamers   r   rA   rA   rB   _check_const_str_dtypeF  s   
r  c                 C   "   |j }||||}dd }||fS )Nc                 S   (   t | |||\}}t| |||}| S r+  )r  r  r   r  r;   rT   r  r  r   r   rA   rA   rB   r  Q     znumpy_empty_nd.<locals>.codegeninstance_type)r  ty_shapety_dtypety_retty_refty_rettyrT   r  rA   rA   rB   numpy_empty_ndL     r  c                    s   t d| |tu st|tjr|jtu st|rtj}nt|}t	| }|d ur>|d ur>tj
||dd tf fdd	}|S d|  d| d}t|)	Nru  r   r  c                       t | | S r+  )r  )r]   rs   r  rA   rB   r   f  rt  zol_np_empty.<locals>.implz.Cannot parse input types to function np.empty(r\  ra  )r  floatr   r   Function
typing_keyr   doublety_parse_dtypety_parse_shaper   r   r   )r]   rs   nb_dtyperh   r   r   rA   r  rB   ol_np_emptyX  s    



r  c                 C   r  )Nc                 S   r  r+  )r  r  r   r  rA   rA   rB   r  s  r  z$numpy_empty_like_nd.<locals>.codegenr  )r  ty_prototyper  r  r  rT   r  rA   rA   rB   numpy_empty_like_ndn  r  r  c                    s   t d| t|st|}nt| tjr| j}n| }|d ur?t| tjr7| jdkr,| jnd}| j||dd nt|dd nd|  d| d	}t	
|d fd
d	}|S )Nr  r  r   F)rs   r  r  r   z3Cannot parse input types to function np.empty_like(r\  ra  c                    r  r+  )r  )r  rs   r  rA   rB   r     rt  zol_np_empty_like.<locals>.implr+  )r  r   r  r   r   r   rs   r  r  r   r   )r  rs   r  r  r   r   rA   r  rB   ol_np_empty_likez  s&   


r  c                 C   r  )Nc                 S   s<   t |jd | ||d }t||j||j|jd d S r   )rq   r   r	   r  r   r   r   r   )r  r;   rT   r  r   rA   rA   rB   r    s   z(_zero_fill_array_method.<locals>.codegen)r   r^  )r  rR   rT   r  rA   rA   rB   _zero_fill_array_method  s   
r  
_zero_fillc                 C   rj  )z?Adds a `._zero_fill` method to zero fill an array using memset.c                 S   s   t |  d S r+  )r  rL  rA   rA   rB   r     rt  z ol_array_zero_fill.<locals>.implrA   )rR   r   rA   rA   rB   ol_array_zero_fill  r  r  c                 C   s   t d| tfdd}|S )Nr  c                 S      t j| |d}|  |S Nrs  )r'  ru  r  )r]   rs   r  rA   rA   rB   r        zol_np_zeros.<locals>.impl)r  r  r]   rs   r   rA   rA   rB   ol_np_zeros  s   
r  c                 C      t d| ddd}|S )N
zeros_likec                 S   r   r  )r'  r  r  )r  rs   r  rA   rA   rB   r     r  zol_np_zeros_like.<locals>.implr+  r  r  rA   rA   rB   ol_np_zeros_like  s   

r  c                 C   r  )N	ones_likec                 S   2   t j| |d}|j}tt|D ]}d||< q|S Nrs  rD   r'  r  r"  r6   r   )r  rs   r  arr_flatr   rA   rA   rB   r     
   
zol_np_ones_like.<locals>.implr+  r  r  rA   rA   rB   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'  ru  r"  r6   r   )r]   
fill_valuers   r  r  r   r  rA   rB   r    
   
zimpl_np_full.<locals>.fullr+  )r  r   r  )r]   r  rs   r  rA   r  rB   impl_np_full  s   

r  c                 C   s   t d| ddd}|S )Nr5  c                 S   s0   t | |}|j}tt|D ]}|||< q|S r+  r  )r  r  rs   r  r  r   rA   rA   rB   r5    r  z$impl_np_full_like.<locals>.full_liker+  r  )r  r  rs   r5  rA   rA   rB   impl_np_full_like  s   

r  c                 C   r  )Nonesc                 S   r
  r  r  )r]   rs   r  r  r   rA   rA   rB   r     r  zol_np_ones.<locals>.implr+  r  r  rA   rA   rB   
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 rs  )r'  r  r6   )ry  rs   r  re   r  rA   rB   r    s   z"impl_np_identity.<locals>.identityr+  )r  r   r  r   r  )ry  rs   r  rA   r  rB   impl_np_identity  s   

r  c                 C   rq  r+  rA   NMrA   rA   rB   _eye_none_handler	  rr  r  c                 C   s$   t |tjrdd }|S dd }|S )Nc                 S      | S r+  rA   r  rA   rA   rB   r     rr  z$_eye_none_handler_impl.<locals>.implc                 S      |S r+  rA   r  rA   rA   rB   r     rr  r   r   rB  )r  r  r   rA   rA   rB   _eye_none_handler_impl  s
   r"  c                    sf   |d u s
t |tjrtt nt |tjtjfr"tt	|d| nt| d dtf fdd	}|S )Nrs   r   c                    s   t | |}t| |f }|dkr)t| || }t|D ]
}d|||| f< q|S t| | |}t|D ]
}d||| |f< q4|S rf  )r  r'  r  r   r6   )r  r  r   rs   _Mr  dre   rN  rA   rB   r   #  s   
znumpy_eye.<locals>.impl)
r   r   rB  r'  rs   r  	DTypeSpecrL  r   r  )r  r  r   rs   r   rA   r%  rB   	numpy_eye  s   
r'  c                 C   sD   t | s	tdt| tjr | jdvrtdddd}|S d S )N#The argument "v" must be array-like)rD   rF  zInput must be 1- or 2-d.r   c                 S   s*  | j dkrG| j}|d t| }t||f| j}|dkr2t|| D ]}| | |||| f< q#|S t|| D ]}| | ||| |f< q8|S | j\}}|dk rT|| }|dkr\|| }tt||d}t	|| j}|dkrt|D ]}| ||| f ||< qs|S t|D ]}| || |f ||< q|S r~  )
rh   r]   r  r'  r  rs   r6   rD  r   ru  )r   r   r   ry  r  re   rowscolsrA   rA   rB   	diag_impl;  s2   

zimpl_np_diag.<locals>.diag_implrI  )r   r   r   r   r   r   rh   NumbaTypeError)r   r   r+  rA   rA   rB   impl_np_diag2  s   



r-  c                    sX   t | tjsd}t|t | jtjsd}t|t|  d   fdd}|S )Nz5The argument "dimensions" must be a tuple of integersr>  c                    sV   t j f|  t jd}d}| D ]}t j|t jdt||}|||< |d7 }q|S )Nrs  r   rD   )r'  ru  ri  aranger  r.   )
dimensionsr   re   rX  r   r  r]   rA   rB   r   f  s   

znumpy_indices.<locals>.impl)r   r   r1  r   r   rs   r   r   )r/  r   r   rA   r0  rB   numpy_indicesY  s   

r1  c                 C   sB   t | sd}t|t|ttjfsd}t|ddd}|S )Nr(  z#The argument "k" must be an integerr   c           	      S   s   t | } |  } t| }t|}|| }t ||f| j}t d| }t d|}t|D ]}| | ||| || f< q/|S r   )	r'  r(  r   r   r  r  rs   maximumr6   )	r   r   r   abs_kry  r   re   jtrA   rA   rB   r     s   
znumpy_diagflat.<locals>.implrI  )r   r   r   r   r   r   r   )r   r   r   r   rA   rA   rB   numpy_diagflatu  s   


r6  c                 C   s   |dv sJ |dkr'd}t d| D ]}d| }|d| dd| d	7 }qnd
}t d| D ]}d| }|d| dd| d7 }q.t|}t|t  t d|  }t|S )N)rr  r  rr  zv
            def _getitem(a, idx, axis):
                if axis == 0:
                    return a[idx, ...]
        rD   ):z
                elif axis == z:
                    return a[r\  z, idx, ...]
            z|
            def _setitem(a, idx, axis, vals):
                if axis == 0:
                    a[idx, ...] = vals
        z:
                    a[z, idx, ...] = vals
            r  )r6   rb  textwrapdedentexecglobalsr&   )rh   r  r  re   lstrA   rA   rB   "generate_getitem_setitem_with_axis  s.   
r=  takec                    s8  t |rLt| tjrt|tjrddd}|S t| tjr2t|tjr2|jdk d fdd	}|S t| tjrHt|tjtjfrJddd}|S d S d S t| tjrpt|tjrpd| j	d  t
fdd	dfd
d	}|S t| tjrt|tjtjtjfr| j	}t|dt|ddfdd	}|S d S d S )Nc                 S   s.   || j d ks|| j  k rtd|  | S )NrD   Index out of bounds)r   
IndexErrorr   )r  r   r   rA   rA   rB   	take_impl  s   znumpy_take.<locals>.take_implrK  c           	         s   t j|j| jd} r| }n|}t |}d}|  }|D ]}|| jd ks.|| j k r2td|| ||< |d }q||j	S )Nrs  r   rD   r?  )
r'  ru  r   rs   r  r8  r   r@  r  r]   )	r  r   r   r  walkeritre   r"  r  )F_orderrA   rB   rA    s   


c                 S   s   t |}t | |S r+  )r'  r   r>  )r  r   r   convertrA   rA   rB   rA    s   
rI  rD   c                    sv   t  }d}|t| jk r| j| dksJ | jtt| jD ]}| j| }||kr5t|||}|d7 }q | |S rf  )r   r   r]   r6   r.   r  )r7  r   r3  r4  r   r   )r5  rA   rB   _squeeze  s   &

znumpy_take.<locals>._squeezec                    s@   t j| |f|d}| jdkr|d S |dk r|| j7 } ||S )Nr  rD   r   )r'  r>  rh   )r  r   r   r7  )rF  rA   rB   rA    s   


rr  r  c                    s   |dk r	|| j 7 }|dk s|| j krd| d| j  }t|t| j|t|}tj|| jd}tt|D ]} | || |}|||| q6|S )Nr   zaxis z) is out of bounds for array of dimension rs  )	rh   r   r.   r]   r   r'  ru  rs   r6   )r  r   r   r   r]   r   re   r  )_getitem_setitemrA   rB   rA    s   
r+  )r	   r   r   r   r   r   r  r}  r5  rh   r&   r=  )r  r   r   rA  rh   rA   )rD  rG  rH  rF  r5  rB   
numpy_take  s:   





rI  c                  G   s   dd | D }t dd |D rtj}|S t dd |D r#tj}|S ttddttj  }dd |D }t	||g }|S )	Nc                 S   s   g | ]
}t |tjs|qS rA   r!  r  rA   rA   rB   r     r  z!_arange_dtype.<locals>.<listcomp>c                 s   r*  r+  r  r  rA   rA   rB   r.    r/  z _arange_dtype.<locals>.<genexpr>c                 s   r*  r+  )r   r   Floatr  rA   rA   rB   r.    r/  zint%sr  c                 S   s   g | ]}t |qS rA   )r   	unliteralr`  rA   rA   rB   r   #      )
r0  r   
complex128r   r  r'  rs   r   r   rD  )r   boundsrs   NPY_TYunliteral_boundsrA   rA   rB   _arange_dtype  s   rQ  c                   s  t |tjr	|j}t |tjr|j}t |tjr|j}|d u r"tj}|d u r)tj}|d u r0tj}t | tjrQt |tjtjfrQt |tjtjfrQt |tjtjfsSd S t |tjr`t| ||n|j	t
dd | ||fD t| dd  t|dd t|dd d fdd	}|S )Nc                 S   r-  rA   r  r`  rA   rA   rB   r   E      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 r*d|}}	n||}}	|dkr7t d|	| | }
tt|
j}du rZtt|
j}tt||d}nt|d}t	|}|}t
|D ]
}|||  ||< qk|S )NrD   r   zMaximum allowed size exceededT)r   r   mathceilr  r  rD  r   r'  ru  r6   )r  r  r  rs   	lit_startlit_stoplit_step_step_start_stopnitems_cnitems_rnitems_ir   r  r   re   start_value
step_value
stop_value
true_dtypeuse_complexrA   rB   r   L  s(   

znp_arange.<locals>.implrI  )r   r   Optionalr7   r^  rL  rB  r&  rQ  rs   r0  r  )r  r  r  rs   r   rA   r^  rB   	np_arange)  s>   re  c                    s|   t dd | |fD sd S t|ttjfsd}t|tdd | |fD r2tj	r.tj
 ntj ntj d fdd	}|S )	Nc                 s   r*  r+  )r   r   rL  r  rA   rA   rB   r.  o  r/  z!numpy_linspace.<locals>.<genexpr>r  c                 s   r*  r+  r  r  rA   rA   rB   r.  v  r/  r  c                    s   t | }| d } |d }|dkr|S |d }|dkr7||  }t ||}td|D ]
}| ||  ||< q+n| |d< |dkrC||d< |S )Ng      ?r   rD   r  )r'  ru  divider6   )r  r  r  r  divdeltar  re   rs  rA   rB   r    s    z numpy_linspace.<locals>.linspacer  )r3  r   r   r   r   r   r   r0  r   USE_LEGACY_TYPE_SYSTEMrM  np_complex128r   )r  r  r  r   r  rA   rs  rB   numpy_linspacem  s   
rk  c              
   C   s0  |j d }t|| ||d d}t||j}|j}t| |||}|j}	|j}
|jdv s.J |j|jkrBtj	||
|	|j
|jdd nLt||j}t||j}| tj}t|||)}t| ||	|||j|}t| ||
|||j|}|||| W d   n1 sw   Y  t| ||j| S )z
    Array copy.
    r   r   r  rD   rx   N)r   rq   r	   r   r]   r   r  r   r  
raw_memcpyr   r   r   r   r   r   r  r  r   r<   r!   r   )ru   r;   rT   r   r  r   r   rettyper  r  ry  r  rx  r   r   r  r  rA   rA   rB   _array_copy  s6   


	rn  c                 C   s@   t |tjsJ |jdkrdnd}|j|dd}||}|tfS )NrK  r   F)r  r  )r   r   r   r  r  rn  )r  r  r  r  rT   rA   rA   rB   _array_copy_intrinsic  s
   ro  z
array.copyc                 C   r@  r+  )rn  r  rA   rA   rB   
array_copy  r?  rp  c                 C   s   t | tjr
dd }|S )Nc                 S      t | S r+  )ro  r  rA   rA   rB   
numpy_copy  rc  z#impl_numpy_copy.<locals>.numpy_copyr,  )r  rr  rA   rA   rB   impl_numpy_copy  s   rs  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sp|jdkry|jdv ryt| |||d S |jdkr|dv sJ |dkrtnt}t|| |||d }||D\}}| t| |||d }|j}W d	   n1 sw   Y  | t| |||}|j}W d	   n1 sw   Y  W d	   n1 sw   Y  ||j}||| ||| |S t| |||S )
zd
    Common logic for layout conversion function;
    e.g. ascontiguousarray and asfortranarray
    r   z return-type has incorrect layoutrD   r   r>  r  r  r   N)r   r   r  rh   rq   r  r   r1  r   rk  r   r   r   r   r   r    r   r   r   r  r  blockrn  phir7   add_incoming)ru   r;   rT   r   output_layoutr  r   r   r  r]   r   
check_funcr  thenorelseout_thenthen_blk
out_orelse
orelse_blkret_phirA   rA   rB   _as_layout_array  s`   


r  c                    sH   t  tjst |j jt|jdd}|| }| fddfS )NrD   )r  rh   c                    s   t | ||| jdS )Nrw  )r  r   )r  r*  r   r  r  rA   rB   r<    s    z,_as_layout_array_intrinsic.<locals>.<lambda>)	r   r   r%   r   RequireLiteralValuer  r   rD  rh   )r  r  rw  r  rT   rA   r  rB   _as_layout_array_intrinsic  s
   

r  c                 C   sH   t | s	tdt| tjtjfrdd }|S t| tjr"dd }|S )Nr  c                 S      t t | S r+  )r'  ascontiguousarrayr   r  rA   rA   rB   r   &  r  z%array_ascontiguousarray.<locals>.implc                 S   r!  Nr   r  r  rA   rA   rB   r   )  rx  r   r   r   r   r   rL  r  r   rf  rA   rA   rB   array_ascontiguousarray   s   
r  c                 C   sL   t | s	tdt| tjtjfrdd }|S t| tjr$dd }|S d S )Nr  c                 S   r  r+  )r'  asfortranarrayr   r  rA   rA   rB   r   4  r  z"array_asfortranarray.<locals>.implc                 S   r!  )NrK  r  r  rA   rA   rB   r   8  rx  r  rf  rA   rA   rB   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|||9}t| ||	|||j|}t| ||
|||j|}t| |||}| |||j|j}t| |||| W d    n1 s~w   Y  t| ||j| S r  )r   rq   r	   r   r]   r   r  r   r   r   r   r   r  r  r  r}   r   rs   r   r!   r   )ru   r;   rT   r   r  r   r   rm  r  r  ry  r  rx  r   r   r  r  itemrA   rA   rB   array_astype=  s0   


r  c                 C   r  )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 s\w   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   rq   r   rw   r   r   r7   r   r   sremr	   is_not_nullr   rB  rC  r   rb   sdivr  r   r   r   r   r   r   r   r    )ru   r;   rT   r   buftyr   r  
out_ary_tyout_aryout_datamodelr   ll_itemsizer  remis_incompatibler   r]   r   r   r   rA   rA   rB   r  a  s<   


znp_frombuffer.<locals>.codegenr  )r  bufferrs   r  r-  rT   r  rA   rA   rB   np_frombuffer\  s   $r  c                    s   t d| t| tjr| jdkrd|  }t||tu s-t|tjr)|j	tu s-t
|r1tj}nt|}|d urEt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 rD   )rs   rh   r  r  z3Cannot parse input types to function np.frombuffer(r\  ra  c                    r  r+  )r  )r  rs   r  rA   rB   r     rt  z impl_np_frombuffer.<locals>.impl)r  r   r   r   r  r   r   r  r  r  r   r  r  r   r  )r  rs   r   r  r   rA   r  rB   impl_np_frombuffer  s0   






r  c                    P   t |rtdd  d fdd	}|S t|tjr&td| d fdd	}|S d S )Nr   c                    
    | |S r+  rA   rc   r]   rs   intrinsic_cfarrayrA   rB   r     rx  zimpl_carray.<locals>.implc                    r  r+  rA   r  r  rA   rB   r     rx  r+  r   get_cfarray_intrinsicr   r   r&  rc   r]   rs   r   rA   r  rB   impl_carray     

r  c                    r  )NrK  c                    r  r+  rA   r  r  rA   rB   r     rx  zimpl_farray.<locals>.implc                    r  r+  rA   r  r  rA   rB   r     rx  r+  r  r  rA   r  rB   impl_farray  r  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 r,d}t||}n+t t jrO j}|d urN||krNd| d| d}t|nd  d}t|t|}|d u rmd| 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   r  r   CPointerrs   r   r,  r&  r  r   r$   
np_cfarray)	r  rc   r]   	ptr_dtyper   rs   rh   r  rT   dtype_r  rA   rB   r    s4   





z0get_cfarray_intrinsic.<locals>.intrinsic_cfarrayr)   )r  r  r  rA   r  rB   r    s   r  c                    s<  |j dd \}}|dd \}}|j}|jdv sJ t| }	t|}
t|
}t|tj	r9t
 |}n|f}|f} fddt||D }|}g }|jdkrf|D ]}||  ||}qWnt|D ]}||  ||}qj|   ||j }t|	||||dd |	 }t |j|S )zR
    numba.numpy_support.carray(...) and
    numba.numpy_support.farray(...).
    NrF  r  c                    r  rA   r  )r   r  r   r   rA   rB   r     r  znp_cfarray.<locals>.<listcomp>rK  r  )r   r   r  rq   rw   r	   r   r   r   r5  r   r   ra   r   r  r  r  rr   rs   r  r   r   r!   )ru   r;   rT   r   r  r  rc   r]   r   r  r   r  r   offr   r   r   r   rA   r   rB   r    sJ   




	r  c                 C   sN   t |tjr| tjt|S t |tjr%| tttj|}|||fS J r+  )	r   r   r5  r   r   r   rv  rJ   r$   )ru   r;   seqtyseqr  rA   rA   rB   _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   decref)r;   r   r  ru   rs  r  rA   rB   wrap.  s   
z$_get_borrowing_getitem.<locals>.wrap)rs   rJ   rq  rr  r$   r   r   )ru   r  r  rA   r  rB   _get_borrowing_getitem&  s   r  c                    st    tj}t|d fdd}g }||}}	t|D ]}
|
dkr+|||	\}}	|t ||	 qt|S )zF
    Compute the likely shape of a nested sequence (possibly 0d).
    r   c                    sN   t | tjrt| dkrdS | d  |dfS t| }| j| |ffS )Nr   NN)r   r   r5  r   r;  r  rs   )r  r  rs  r;   ru   r   rA   rB   get_first_item>  s   
z.compute_sequence_shape.<locals>.get_first_item)r   r   r   r   r6   ra   r  r   )ru   r;   rh   r  r  r   r  r   innertyinnerre   rA   r  rB   compute_sequence_shape7  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)rB  rC  r   rA   r   rA   rB   _failZ  s   
z#check_sequence_shape.<locals>._failc                    s<  t |dkrd S t| |}|d }d||}j|dd    W d    n1 s/w   Y  t |dkr<d S t| tjrut| }t	|}| j
}|||jf}	||	|dd   W d    d S 1 snw   Y  d S t| tjrtt | D ]}
| |
 }||
}	||	|dd   qd S J | )Nr   r?  Fr@  rD   )r   r  r   r   r   r   rv  r  r	   r  rs   r   r5  r6   r;  )r  r  r   r   expectedr	  rs  r  r  r  re   r  r;   check_seq_sizeru   rA   rB   r  ^  s0   
"z,check_sequence_shape.<locals>.check_seq_sizeNrA   )ru   r;   r  r  r   rA   r  rB   check_sequence_shapeU  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 rL  )r	   r  r  r   rs   r   )r   r8  r   rc   )r  r;   ru   r   r   r   rA   rB   assign_item  s
   z-assign_sequence_to_array.<locals>.assign_itemc                    s  t |dkrt| tjtjfrJ || | d S |d }t| tjr[t| }t|#}| j}|||j	f} |||dd  ||j	f  W d    d S 1 sTw   Y  d S t| tjrt
t | D ]!}	| |	 }||	}tj|	}
 |||dd  ||
f  qgd S J | rf  )r   r   r   rv  r5  r  r	   r  rs   r   r6   r;  r   r   )r  r  r   r   r   rs  r  r  r  re   r   )assignr  r;   ru   rA   rB   r    s(   
 "z(assign_sequence_to_array.<locals>.assignrA   NrA   )ru   r;   r   r   r   r  r  r  rA   )r  r  r  r;   ru   r   r   r   rB   assign_sequence_to_array~  s   r  c                 C   s>   t | |\}}t|r|}n
t|}|d u rd S t||dS r  )r3   r   r  r   r   )r  r~  rs   rh   	seq_dtyperA   rA   rB   np_array_typer  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   rh   r   r  r   r  r  r  r   r   r!   r   )
ru   r;   rT   r   r  rh   r  r  r   r  rA   rA   rB   r    s   


znp_array.<locals>.codegen)r  r  )r  objrs   r  rT   r  rA   rA   rB   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   
   t | |S r+  )r  )r~  rs   rA   rA   rB   r     rx  zimpl_np_array.<locals>.implr+  )r  r   r   r   r   r  )r~  rs   r   r   rA   rA   rB   impl_np_array  s   



r  c           
      C   s   | d}| |}|d||}||||||}||d|||d||}|j|dd d| }	| j|t|	f W d    |S 1 sLw   Y  |S )Nr   r   rh  Fr@  z%s(): axis out of bounds)	r7   r   r  rw  r  r   rB  rC  r@  )
ru   r;   r   rh   r   r   ll_ndimis_neg_axisaxis_out_of_boundsr   rA   rA   rB   _normalize_axis  s   


r  c              
   C   s   t ||d ks
J ttj|}t||}td}t|d D ])}t|}	|d|	|}
|| }||
|	|	||	}|
|t||d| q"|
|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).
    rD   rh  r   r   r   r  r	   r   rP   r6   r   r  rw  r   r`   r   r<   )ru   r;   
orig_shaperh   r   ll_shtyr   r  rX  ll_dim
after_axisr!  r   rA   rA   rB   _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 ]'}	t|	}
|d|
|}|||	|
||
}|
||	 t||d| q'|
|t||d| t|||S )zD
    Same as _insert_axis_in_shape(), but with a strides array.
    rD   r   rh  r  )ru   r;   orig_stridesrh   r   r  r   r  r   rX  r  r  r   rA   rA   rB   _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   rh   r   rq   r	   r   r]   r   r  r  r   r   r   r   r   r   )ru   r;   rT   r   r   r  rh   r  r  r  r   r   
new_shapesnew_stridesrA   rA   rB   expand_dims(  s$   
r  c                 C   sB   |j dkr|jnd}|j|j d |d}|||}dd }||fS )NrD   r  r  c                 S   sP   |  ||d |jd tj}t| |d|jj|}t| ||||}t| ||j|S )NrD   znp.expand_dims)	r   r   r   r   r  r   rh   r  r    )ru   r;   rT   r   r   r  rA   rA   rB   r  J  s   znp_expand_dims.<locals>.codegen)rh   r  r  )r  r  r   r  r  rT   r  rA   rA   rB   np_expand_dimsD  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   r  r+  )r  )r  r   rA   rA   rB   r   _  rx  z!impl_np_expand_dims.<locals>.impl)r   r   r   r   r   r   )r  r   r   r   rA   rA   rB   impl_np_expand_dimsU  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  rD  rh   r  )minimumrA   rB   r   h  rl  z-_atleast_nd.<locals>.impl.<locals>.<listcomp>c                    sn   t t |d } fddt|D }t|dkr+ |j|}n|d }t |j|S )Nr   c                    s"   g | ]\}}} |||qS rA   rA   )r   r  r  r  r;   ru   	transformrA   rB   r   n  s    z>_atleast_nd.<locals>.impl.<locals>.codegen.<locals>.<listcomp>rD   )_atleast_nd_transformr	   r   r   r   rk  r   r    )ru   r;   rT   r   arrsretsr  )r`  r  r  rw  r  rB   r  j  s   

z*_atleast_nd.<locals>.impl.<locals>.codegenrD   r   )r$   r   r   r2  StarArgTuple
from_types)typingcontextr   r  )r  r  )r`  rw  rB   r   e  s   
z_atleast_nd.<locals>.implc                     s    |  S r+  rA   r;  )r   rA   rB   r<  z  s    z_atleast_nd.<locals>.<lambda>r  )r  r  rA   )r  r   r  rB   _atleast_ndd  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 ]*}|d }|j|k r.t | }|j|jd d}t| |t|||f|}|}q|S )NrD   r  )r6   rh   r	   r   r  r  r   r$   )	ru   r;   r  r  r  re   rh   r   newarrtyr  min_ndimrA   rB   r    s   
z(_atleast_nd_transform.<locals>.transform)r   )r  r  r  rA   r  rB   r  }  s   r  c                  G   s"   t dd | D rtddgS d S )Nc                 s   r*  r+  r,  r  rA   rA   rB   r.    r/  z np_atleast_1d.<locals>.<genexpr>rD   r   r3  r  r;  rA   rA   rB   np_atleast_1d  s   r  c                  G   s$   t dd | D rtdddgS d S )Nc                 s   r*  r+  r,  r  rA   rA   rB   r.    r/  z np_atleast_2d.<locals>.<genexpr>rF  r   r  r;  rA   rA   rB   np_atleast_2d     r  c                  G   s$   t dd | D rtdg dS d S )Nc                 s   r*  r+  r,  r  rA   rA   rB   r.    r/  z np_atleast_3d.<locals>.<genexpr>r  )r   r   rF  r  r;  rA   rA   rB   np_atleast_3d  r  r  c	                 C   s  t |t |  krt |  krt |ksJ  J td}	t| |||}
t||
j}g }|D ]2}|	}tt||D ]!\}\}}|d|	||}|
||}||||||}q=|| q2|
j}t|||||D ]_\}}}}}|j}tj||tj|jd}|9}t| |||||j|}t| |||}| |||j|j}t| |||||j|}t| |||| W d   n1 sw   Y  t|||}qp|
S )z2
    Concatenate arrays along the given axis.
    r   r  )orderN)r   r	   r   r  r   r   r?  r   r   r7   r   r  rw  ra   r   r  r  r  r}   r   rs   r   r   )ru   r;   r   r`  r  
arr_shapesarr_stridesr  
ret_shapesr   r  ret_stridescopy_offsetsarr_shr   rX  r   r  is_axisaddendret_datar  r  arr_starr_datar  r   r  r   r  rA   rA   rB   _do_concatenate  sL   6



r  c                    s  |j } fddt||D }t d||} fdd|D } fdd|D } fdd|d D }	t|D ] d||}
|	 } |}fd	d|d
d  D } |
v\}}| t	 j
||g } || W d    n1 sw   Y  |@ tj}|D ]} | d||}q j |dd j td f W d    n1 sw   Y  W d    n1 sw   Y  W d    n1 sw   Y  q9 fdd|	D }	t |||||||		}t || S )Nc                    "   g | ]\}}t | |d qS r   r  r   r  r  r   rA   rB   r     r  z#_np_concatenate.<locals>.<listcomp>np.concatenatec                       g | ]	}t  |jqS rA   )r	   r   r]   r   r  r  rA   rB   r     r  c                    r  rA   r	   r   r   r  r  rA   rB   r     r  c                    s   g | ]}t  |qS rA   )r	   r   r  r  rA   rB   r     rR  r   r  c                    s   g | ]}|  qS rA   rA   r  )rX  rA   rB   r     r  rD   Fr@  z<np.concatenate(): input sizes over dimension %d do not matchc                    s   g | ]}  |qS rA   )r<   r  r  rA   rB   r     rL  )rh   r   r  r6   r   r7   r<   r  r  r  rw  r   r	   r  r  r   r  rB  rC  r   r  r!   r   )ru   r;   r`  r  r  r   rh   r  r  r  r  ret_shape_ptrret_shother_shapeson_axison_other_dimr!  is_okr  rA   )r;   ru   rX  rB   _np_concatenate  s`   

r  c                    s  |j }td}td}tt|}	 fddt||D }t d||}t |d j}
|dd  D ]A}tj}tt |j|
D ]1\}} 	| 
d||} j |dd j td	 W d    n1 ssw   Y  qGq8 fd
d|D }ttj|t }t }t|d D ]5}t|} 
d||}|
| } | |||} |t |d|  |t |d| q |t |d|  |	t |d| t  |}|gt| }t  |} fddtt|D }t|d D ]6}t|} 
d||} | |||}tt|D ]} || | t || d| q6qtt|D ]} |t || d| qT fdd|D }t |||||||	}t || S )Nr   rD   c                    r  r  r  r  r   rA   rB   r     r  z_np_stack.<locals>.<listcomp>np.stackr  Fr@  )z5np.stack(): all input arrays must have the same shapec                    r  rA   r   r  r  rA   rB   r   .  r  rh  c                    r  rA   r  r  )r;   r  rA   rB   r   O  rR  c                    s   g | ]}t   |qS rA   )r	   r   r<   )r   r"  r  rA   rB   r   d  r  )rh   r	   r   r   r   r  r   r]   r  r  r   r   r  rB  rC  r   r   r  rP   r6   r  rw  r   r`   r<   r  r!   r   )ru   r;   r`  r  r  r   rh   r   r  
ll_narraysr  r  r  r!  orig_shr  input_shapesr  rX  r  r  r   input_stridesre   r  rA   )r;   ru   r  rB   	_np_stack  s   








r  c                 C   sT   |d urt |tjsd S t| d|\}}|dkrd}t|t|}t|||S )Nr  r   z.zero-dimensional arrays cannot be concatenated)r   r   r   r4   r   r,  r5   r   )r  r\  r   rs   rh   r   r  rA   rA   rB   np_concatenate_typern  s   
r  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 r~  )	r   r   r   r   r  r   r	   r   r   ru   r;   rT   r   r   rA   rA   rB   r    s   znp_concatenate.<locals>.codegen)r  r   r   r   )r  r\  r   r  rT   r  rA   rA   rB   np_concatenate  s
   
r  c                 C      t | tjrddd}|S d S )Nr   c                 S   r  r+  )r  r\  r   rA   rA   rB   r     rx  z!impl_np_concatenate.<locals>.implrI  r   r   r5  r\  r   r   rA   rA   rB   impl_np_concatenate     
r  c                 C   s0   |D ]}|j dk s|j dkrd}t|qdS )NrD   rF  z7np.column_stack() is only defined on 1-d and 2-d arrays)rh   r   r,  )ru   r   r\  r  r   rA   rA   rB   _column_stack_dims  s   
r  c                 C   sB   t | d|td\}}t|}t|||}||}dd }||fS )Nznp.column_stack)dim_chooserc                 S   s   t |jd }t||d }g }g }| tjd}t||D ]:\}	}
|	jdkr3|	|	 |	|
 q|	jdks:J |	j
dd}t||	}t| |||
f|}|	| |	| qt| ||||j|S )Nr   rD   rF  r  )r   r   r	   r   r   r   r   r   rh   ra   r  r   r$   r  r  r   )ru   r;   rT   r   orig_arrtys	orig_arrsr`  r  r   r  r  rX  
expand_signewarrrA   rA   rB   r    s$   



z np_column_stack.<locals>.codegen)r4   r  r5   r   r   )r  r3  rs   rh   r  r  rT   r  rA   rA   rB   np_column_stack  s   
r  c                 C      t | tjrdd }|S d S )Nc                 S   rq  r+  )r  r3  rA   rA   rB   r     rc  zimpl_column_stack.<locals>.implr  r3  r   rA   rA   rB   impl_column_stack     r!  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   r  rA   rA   rB   _np_stack_common  s   r#  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 )rK  N)r  r  rA   rA   rB   r.    s    z"np_stack_common.<locals>.<genexpr>rK  r   rD   c                 S   s,   |  ||d |jd tj}t| ||||S rs  )r   r   r   r   r#  r  rA   rA   rB   r    s   z np_stack_common.<locals>.codegen)r4   r3  r   r   )	r  r\  r   rs   rh   r  r  rT   r  rA   rA   rB   np_stack_common  s   
r$  c                 C   r  )Nr   c                 S   r  r+  )r$  r  rA   rA   rB   r     rx  zimpl_np_stack.<locals>.implrI  r  r  rA   rA   rB   impl_np_stack  r  r%  c                 C   s4   t | ||\}}t||}t|}t|||}|S r+  )r4   rD  r5   r   r   )r  r   r\  ndim_minrs   rh   r  r  rA   rA   rB   NdStack_typer  s
   
r'  c                 C   &   t | d|d}||}dd }||fS )Nz	np.hstackrD   c                    sf   |j d }|d j}|dkr| tjd t| ||| S |dkr#dnd  fdd}| ||||S )Nr   rD   c                    s   t j|  dS r  r  r\  r  rA   rB   np_hstack_impl  r,  z3_np_hstack.<locals>.codegen.<locals>.np_hstack_implr   rh   r   r   r   r#  r  )ru   r;   rT   r   r2  rh   r*  rA   r  rB   r    s   

z_np_hstack.<locals>.codegenr'  r  r3  r  rT   r  rA   rA   rB   
_np_hstack     r.  c                 C   r  )Nc                 S   rq  r+  )r.  r  rA   rA   rB   r     rc  zimpl_np_hstack.<locals>.implr  r   rA   rA   rB   impl_np_hstack  r"  r0  c                 C   r(  )Nz	np.vstackrF  c                 S   sd   |j d }|d j}|dkrdd }n|dkr&| tjd}t| ||||S dd }| ||||S )Nr   c                 S   s   t t | dS rs  )r'  r  hstackr)  rA   rA   rB   np_vstack_impl)  r  z3_np_vstack.<locals>.codegen.<locals>.np_vstack_implrD   c                 S      t j| ddS )Nr   r  r  r)  rA   rA   rB   r2  2  r,  r+  )ru   r;   rT   r   r2  rh   r2  r   rA   rA   rB   r  $  s   


z_np_vstack.<locals>.codegenr,  r-  rA   rA   rB   
_np_vstack  r/  r4  c                 C   r  )Nc                 S   rq  r+  )r4  r  rA   rA   rB   r   =  rc  zimpl_np_vstack.<locals>.implr  r   rA   rA   rB   impl_np_vstack:  r"  r5  c                 C   r(  )Nz	np.dstackr  c                 S   s   |j d }|j}|d j}|dkrdd }| ||||S |dkrY| tjd}|j|jd d}	tj	|	g|j R  }
t
| ||
||}| tjd}t	||	}t| |||f|S |dkrl| tjd}t
| ||||S dd }| ||||S )Nr   c                 S   s   t | dddS )NrD   r  )r'  r1  r  r)  rA   rA   rB   r2  Q  r=  z3_np_dstack.<locals>.codegen.<locals>.np_vstack_implrD   r  rF  c                 S   r3  )NrF  r  r  r)  rA   rA   rB   r2  h  r,  )r   r   rh   r  r   r   r   r  r   r$   r#  r  )ru   r;   rT   r   r2  r  rh   r2  r   stack_retty	stack_sig	stack_retr  rA   rA   rB   r  K  s*   


z_np_dstack.<locals>.codegenr,  r-  rA   rA   rB   
_np_dstackF  s   "r9  c                 C   r  )Nc                 S   rq  r+  )r9  r  rA   rA   rB   r   s  rc  zimpl_np_dstack.<locals>.implr  r   rA   rA   rB   impl_np_dstackp  r"  r:  fillc                 C   r  )Nc                 S   s   || d d < d S r+  rA   )r  r   rA   rA   rB   	fill_impl{  s   zarr_fill.<locals>.fill_implrA   )r  r   r<  rA   rA   rB   arr_fillx  s   r=  dotc                 C   r  )Nc                 S   r  r+  )r'  r>  )r  otherrA   rA   rB   dot_impl  rt  zarray_dot.<locals>.dot_implrA   )r  r?  r@  rA   rA   rB   	array_dot  r  rA  c                 C   "   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 )NrF  r  r  .r'  r(  rh   r   r8  r  rA   rA   rB   r     s   

znp_flip_lr.<locals>.implre  r8  r   rA   rA   rB   
np_flip_lr     rF  c                 C   rB  )NzCannot np.flipud on %s typec                 S   s.   t | }|jdk rtd|d d ddf S )NrD   zInput must be >= 1-d.r  .rC  rD  rA   rA   rB   r     s   

znp_flip_ud.<locals>.implre  rE  rA   rA   rB   
np_flip_ud  rG  rH  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  )r6   r.   r   )lengthempty_tupler   re   rA   rA   rB   r     s   z6_build_flip_slice_tuple.<locals>.codegen.<locals>.impl)r   r   r   r$   r   get_constant_undefr  )ru   r;   r$   r   r   inner_argtypes	inner_sigll_idx_typerJ  
inner_argsr   r   
tuple_typerA   rB   r    s   

z(_build_flip_slice_tuple.<locals>.codegen)	r   r   r.  r   r  r   r   r1  slice3_type)r  szrT   r  rA   rP  rB   _build_flip_slice_tuple  s   

rT  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+  )rT  rh   )r8  slrA   rA   rB   r     s   
znp_flip.<locals>.impl)r   r   r   r   r   rE  rA   rA   rB   np_flip  s   rV  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r4t |jjtjr4d	dd}|S t |tj	rKt
dd |jD rMd	dd}|S d S d S )
Nr   c                 S      t jt | ||dS r  )r'  array_splitr(  r   indices_or_sectionsr   rA   rA   rB   r     s
   znp_array_split.<locals>.implc                 S   sP   t | j| |\}}tt|d g| |g|| d   }tj| ||dS )NrD   r  )divmodr]   r'  cumsumr   rX  )r   rZ  r   rU  r  r   rA   rA   rB   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/   rh   r   r.   r   ra   r   rZ  r   	slice_tupr   prevr  r   rA   rA   rB   r     s   
c                 s   r*  r+  r   r   r   )r   r5  rA   rA   rB   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 r]  )r/   rh   r   r   r.   r   ra   r^  rA   rA   rB   r     s   
rI  )r   r   r1  ListTyper}  r   IterableTypeiterator_typer   r2  r3  r   rZ  r   r   rA   rA   rB   np_array_split  s,   





rf  c                 C   sL   t | tjtjtjfrddd}|S t |tjrddd}|S t| ||dS )Nr   c                 S   rW  r  )r'  splitr(  rY  rA   rA   rB   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  )r[  r]   r   r'  rX  )r   rZ  r   r  r  rA   rA   rB   r   "  s   r  rI  )r   r   r1  rb  r}  r   rf  re  rA   rA   rB   np_split  s   


rh  c                 C   N   t | tjsd}t|t |tjtjtjtjfs!d}t|dd }|S )N#The argument "ary" must be an array:The argument "indices_or_sections" must be int or 1d-arrayc                 S   "   | j dk r	tdtj| |ddS )NrF  z3vsplit only works on arrays of 2 or more dimensionsr   r  rh   r   r'  rg  r   rZ  rA   rA   rB   r   =     
znumpy_vsplit.<locals>.implr   r   r   r   r   r   r}  r1  r   rZ  r   r   rA   rA   rB   numpy_vsplit2     

rr  c                 C   ri  )Nrj  rk  c                 S   s<   | j dkr	td| j dkrtj| |ddS tj| |ddS )Nr   z3hsplit only works on arrays of 1 or more dimensionsrD   r  rm  rn  rA   rA   rB   r   Q  s
   

znumpy_hsplit.<locals>.implrp  rq  rA   rA   rB   numpy_hsplitF  s   

rt  c                 C   ri  )Nrj  rk  c                 S   rl  )Nr  z3dsplit only works on arrays of 3 or more dimensionsrF  r  rm  rn  rA   rA   rB   r   g  ro  znumpy_dsplit.<locals>.implrp  rq  rA   rA   rB   numpy_dsplit\  rs  ru  c                 C   s   | |k S )z7
    Trivial comparison function between two keys.
    rA   r  rA   rA   rB   
default_ltv  s   rv  Fc                 C   sr   | |j |f}zt| W S  ty8   | dkr"tj||dd}|j}n| dkr0tj||d}|j}|t|< | Y S w )z6
    Get a sort implementation of the given kind.
    r+   T)lt
is_argsortis_np_arrayr,   )rw  rx  )	ri   _sortsKeyErrorr+   make_jit_quicksortrun_quicksortr,   make_jit_mergesortrun_mergesort)r  lt_implrx  keyrm  ro  rA   rA   rB   get_sort_func}  s(   
r  c                 C   s$   t | tjrtS t | tjrtS tS r+  )r   r   rJ  r   r  r   rv  rs  rA   rA   rB   lt_implementation  s
   r  z
array.sortc                    s8   |j d }tdt|jd  fdd}| ||||S )Nr   r+   )r  r  c                    s    |  d S r+  rA   r  	sort_funcrA   rB   array_sort_impl  r  z#array_sort.<locals>.array_sort_impl)r   r  r  rs   r  )ru   r;   rT   r   r  r  rA   r  rB   
array_sort  s   
r  c                 C   rc  )NzArgument "a" must be array-likec                 S   s   |   }|  |S r+  )r  rm  )r  r   rA   rA   rB   np_sort_impl  s   z"impl_np_sort.<locals>.np_sort_implre  )r  r  rA   rA   rB   impl_np_sort  s   
r  zarray.argsortc           	         s^   |j \}}t|jt|jdd  fdd}|j|j d d d}|d d }| ||||S )NT)r  r  rx  c                    s    | S r+  rA   r  r  rA   rB   array_argsort_impl  rc  z)array_argsort.<locals>.array_argsort_implrD   r;  )r   r  r   r  rs   replacer  )	ru   r;   rT   r   r  r  r  innersig	innerargsrA   r  rB   array_argsort  s   
r  c                 C   s   |j |j ks|jdksJ |S )Nr  )r  r  )ru   r;   r  r  r   rA   rA   rB   array_to_array  s   r  c                 C   4   dd }t ||}| ||||g}t| ||j|S )Nc                 S   s
   |  dS r   )r>  r  rA   rA   rB   r     s   
zarray0d_to_scalar.<locals>.impl)r$   r  r"   r   ru   r;   r  r  r   r   rT   r   rA   rA   rB   array0d_to_scalar  s   
r  c                 C   r  )Nc                 S   s   t | d S rv  r^  r  rA   rA   rB   r     rt  z array_to_unichrseq.<locals>.impl)r$   r  r    r   r  rA   rA   rB   array_to_unichrseq  s   
r  c                 C   rK  )zR
    An intrinsic returning a derived array with the given shape and strides.
    r   r  r]   r   rA   rA   rB   reshape_unchecked  rP  r  c                    s   dd   fdd}|S )Nc                 S   s   t | tjotdd | D S )Nc                 s   r*  r+  ra  rg  rA   rA   rB   r.    r/  z>type_reshape_unchecked.<locals>.check_shape.<locals>.<genexpr>)r   r   r5  r3  r]   rA   rA   rB   r[    s   z+type_reshape_unchecked.<locals>.check_shapec                    sJ   t | tjsd S  |r |sd S t|t|krd S | jt|ddS )Nr  r  )r   r   r   r   r  r  r[  rA   rB   typer   s   z%type_reshape_unchecked.<locals>.typerrA   )ru   r  rA   r  rB   type_reshape_unchecked  s   	r  c                 C   s   |j d }|j}t|| ||d }t|| |}t||d }t||d }	t||j||	|j|jd |	 }
t
| |||
S )Nr   rD   rF  r  )r   r   rq   r	   r   r   r   r   r   r   r    )ru   r;   rT   r   r   r  r   r   r]   r   r   rA   rA   rB   impl_shape_unchecked  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tdd d	 fdd	}|S )
Nc                 S   r_  r+  r  r  r]   rA   rA   rB   rQ  %  r   zas_strided.<locals>.get_shapec                 S   r   r+  rA   r  rA   rA   rB   rQ  )     z,as_strided() strides argument cannot be Nonec                 S   r   r+  rA   )r  r   rA   rA   rB   get_strides4  r  zas_strided.<locals>.get_stridesc                    s   t |  | || |} | S r+  )r  )r  r]   r   rQ  r  rA   rB   as_strided_impl8  s   z#as_strided.<locals>.as_strided_implr  )r   r^  r&   r   r   )r  r]   r   r  rA   r  rB   
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rIt |jtjrItt| jt	| tt| jt	| tdd nt
dt|rYtdd  n&t |tjrftdd  nt |tjrzt |jtjrztd	d  nt
d
d fdd	}|S )NrD   c                 S   s   | fS r+  rA   window_shaperA   rA   rB   get_window_shapeI  r   z-sliding_window_view.<locals>.get_window_shapec                 S   r  r+  rA   r  rA   rA   rB   r  R  r  z4window_shape must be an integer or tuple of integersc                 S   s   t t|S r+  )r   r6   r  r   rh   rA   rA   rB   get_axis]  r  z%sliding_window_view.<locals>.get_axisc                 S   s   t dd||gS )Nsliding_window_viewr   r   r  rA   rA   rB   r  b  s   c                    s    fdd|D S )Nc                    s   g | ]	}t d d |qS )r  r   r  r  r  rA   rB   r   l  r  z9sliding_window_view.<locals>.get_axis.<locals>.<listcomp>rA   r  rA   r  rB   r  j  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 ]7\}}|dk rItd|| |k rStd|| | d }t|||}t|||}t||| j| }|d7 }q=t| ||}	|	S )Nz2Must provide matching length window_shape and axisr   z-`window_shape` cannot contain negative valuesz4window_shape cannot be larger than input array shaperD   )	rh   r   r   r6   r.   r]   r   r   r  )
r  r  r   	out_shapeout_stridesre   r  rX  trimmedr{  r  r  shape_bufferstride_bufferrA   rB   sliding_window_view_implt  s8   
z5sliding_window_view.<locals>.sliding_window_view_implr+  )r   r   r   r   r6   rh   r&   r1  rs   r   r   r   r   )r  r  r   r  rA   r  rB   r  ?  s>   'r  c                 C   r  )Nc                 S   s>   | j dkrtdk rdS td| j dkrt| dS td)Nr   )rF  rF  FziThe truth value of an empty array is ambiguous. Use `array.size > 0` to check that an array is not empty.rD   z[The truth value of an array with more than one element is ambiguous. Use a.any() or a.all())r   r   r   r6  r>  r  rA   rA   rB   r     s   

zol_bool.<locals>.implr,  r  rA   rA   rB   ol_bool  s   r  c                    sn   t |ttjfstdt |ttjfstdt | tjs%td| jtt	  fdd}|S )Nz.The second argument "axis1" must be an integerz-The third argument "axis2" must be an integerz'The first argument "a" must be an arrayc                    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  )r  r  r  
axes_tuple	axes_listrh   rA   rB   r     s   znumpy_swapaxes.<locals>.impl)
r   r   r   r   r   r   r   rh   r   r6   )r  r  r  r   rA   r  rB   numpy_swapaxes  s   


r  c                 C   s  t dd| j|}t| j}d||< tt||jD ]$\}\}}	|dkr&|	}
n|	dkr-|}
n
||	kr5td|}
t|||
}qt||| j| }t	| |} t	||}|}t
|dkrmtt
|D ]}t||| j| }qa|}t
|dkrtt
|D ]}t||| j|d |  }q{|j| }t||f | | j}td d d f}t|D ]1}t|D ])}| || |  }||| |  }||| |  }t|D ]
}|||  ||< qqq|S )Nznp.take_along_axisr   rD   z*`arr` and `indices` dimensions don't matchr   )r   rh   r   r]   r?  r   r   r.   r'  r+  r   r6   ru  rs   r   r  )r  r   r   Ni_origNk_origindices_broadcast_shape	arr_shapere   d1d2new_valarr_broadcast_shapeNiNkJr   np_s_iikka_1d
indices_1dout_1dr4  rA   rA   rB   _take_along_axis_impl  sT   

r  c                    s  t | tjstdt |tjstdt |jtjs"tdt|r)d}n| j}||jkr6tdt	t
|jt|rIfdd}|S t|d t |tjsYtd	|j}|d
k re| j| }|d
k sn|| jkrstdt	t
| t	t
|d | j fdd}|S )Nz)The first argument "arr" must be an arrayz.The second argument "indices" must be an arrayz'The indices array must contain integersrD   z;`indices` and `arr` must have the same number of dimensionsc                    s   t |  |ddd S )Nr   rA   )r  r  r  r   r   )r  rA   rB   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  rA   rB   r  4  s   )r   r   r   r   r   rs   r   r   rh   r   r6   r   r.  r  r   )r  r   r   arr_ndimr  rA   r  rB   arr_take_along_axis  s>   






r  T        c                 C   s   t | tjr2t | tjrddd}|S t | tjr ddd}|S t | tjr-ddd}|S tdt| ret | j	tjrDddd}|S t | j	tjrRdd	d}|S t | j	tjr`dd
d}|S tdtd)NTr  c                 S   r  r+  rA   r  r  nanrA   rA   rB   r   ?  rr  znan_to_num_impl.<locals>.implc                 S   sF   t | r|S t | rt t| jS t | r!t t| jS | S r+  )r'  isnanisneginffinfor7   r   isposinfrD  r  rA   rA   rB   r   C  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  r7  r  rA   rA   rB   r   L  s   
z4Only Integer, Float, and Complex values are acceptedc                 S   r  r+  rA   r  rA   rA   rB   r   X  rr  c           	      S   s   t | jj}t | jj}t | }|rt |n|}|j}t|j	D ]%}t 
|| r2|||< q$t || r>|||< q$t || rI|||< q$|S r+  )r'  r  rs   r   rD  r(  r  r"  r6   r   r  r  r  )	r  r  r  min_infmax_infx_outputoutput_flatre   rA   rA   rB   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  rA   rA   rB   r   l  s
   
z4The first argument must be a scalar or an array-likeTr  )
r   r   rL  r   rJ  r  r   r   r   rs   )r  r  r  r   rA   rA   rB   nan_to_num_impl:  s6   
;
7
.
"


r  r+  r  r>  rI  rI  )Fr  r  (  rU  r  rS  rq  r8  llvmliter   llvmlite.irr   numpyr'  numbar   r   
numba.corer   r   r   r	   r
   r   numba.np.numpy_supportr   r   r   r   r   r   r   r   r   r   r   r   r   numba.core.imputilsr   r   r   r   r   r   r   r    r!   r"   r#   numba.core.typingr$   numba.core.typesr%   numba.core.extendingr&   r'   r(   r)   r*   
numba.miscr+   r,   numba.cpythonr-   numba.cpython.unsafe.tupler.   r/   r0   numba.core.typing.npydeclr1   r  r2   r  r3   r4   r5   rC   rF   rq   rw   r}   r   r   r   r   r   r   r   r   r   r   r   ArrayIteratorBORROWEDr  r#  r   r'  rr  r   r  r)  r5  r4  r  Anyr:  r   r>  r   rD  rH  r~  rJ  rW  rp  ry  r}  r  r  r1  r  r  r  r  r  r  r  r  r  r  r  r&  r*  r+  r5  r:  broadcast_shapesrH  r  rO  rR  rY  re  r7  r  r  r  r  VarArgr  r  r  r  r  r  	geomspacer  rot90r  r  r  r  r  r  r  resizer	  ra   r  r  r   r  r  r)  r-  rH  rG  rJ  rb  r]   rg  r   ri  uniquerr  r  r~  rt  r  r  r  r  r&  r  r  
MemoryViewr  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  DTypeNumberClassr  r  r  r  r  r   r  r.  r  r  r  r  r  Bytesr  is_r  r   r&  r+  r6  r7  r  r  r$  r  r  r  r  r  r  r(  r  r  r  r  r  r  r  r  r8  r  NumpyNdIterTyper  eqr  r  r  r  r  r  r  r  r  r  ru  r  r  r  r  r  r  r  r  r  r  r  r	  r  r  r  r5  r  r  r  r  r  r  r"  eyer'  diagr-  r   r1  diagflatr6  r=  r>  rI  rQ  r.  re  r  rk  rn  ro  rp  r  rs  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  
atleast_1dr  
atleast_2dr  
atleast_3dr  r  r  r  r  r  r  r  r  r  column_stackr!  r#  r$  stackr%  r'  r.  r1  r0  r4  vstackr5  	row_stackr9  dstackr:  r=  rA  r  rF  r  rH  rT  fliprV  rX  rf  rg  rh  vsplitrr  hsplitrt  dsplitru  rz  rv  r  r  r  rm  r  argsortr  r  UnicodeCharSeqrJ  r  r  NPTimedelta
NPDatetimer  r  r  type_callabler  r  libstride_tricksr  r  r6  r  r  r  r  take_along_axisr  r  r  rA   rA   rA   rB   <module>   s    04<	E



D


(



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

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


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

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