o
    Td                 	   @   s  d Z ddlZddlZddlZddlmZ ddlmZ ddlZ	ddl
mZmZ ddlmZmZmZmZmZ ddlmZmZmZmZmZmZmZ ddlmZmZmZ dd	lmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) dd
l*m+Z+ ddl,m-Z- ddl.m/Z/m0Z0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl.m<Z< ddl=m>Z?m@ZAmBZBmCZCmDZD dd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKd d! ZLd"d# ZM	d4d$d%ZNd&d' ZOd(d) ZPe0ePd*d+ ZQed,ejRd-d. ZSd/d0 ZTed1ejUe%e)jVd2d3 ZW	d4d4d5ZXd6d7 ZYd8d9 ZZeej[ejRej\eej[ejRej]d:d; Z^eej[ejRej_d<d= Z`eejaejRejbejbd>d? ZceedejRd@dA ZeedBejfdCdD ZgedEejfejbdFdG ZhG dHdI dIeiZjG dJdK dKejZkG dLdM dMejZlG dNdO dOejZmG dPdQ dQejZnG dRdS dSejZoG dTdU dUeiZpdVdW Zqeej[ejRejfdXdY ZrdZd[ Zsd\d] Ztd^d_ Zud`da Zvdbdc Zwddde Zxdfdg Zydhdi Zze2djdk Z{e2dldm Z|e/dndo Z}dpdq Z~e0e	jdrds Ze/dtdu Ze0e	jdvdw Ze0e	jdxdy Zdzd{ Zd|d} Zed~ejfdd Zdd Zed~ejfej_dd Zed~ejfeejbdd Ze0e	jd4ddZe ejfddd Ze0e	jd5ddZe0e	jd6ddZdd Zdd Zedejfej_dd Zedejfeejbdd Ze0e	jdd Ze0e	jd4ddZedejfdd Zee	jejfdd Zedejfdd Ze/dd Ze/dd Ze0e	jd4ddZe1ejfdd7ddZdd Ze0e	jdd Ze0e	jdd Ze0e	jdd Ze/dd Ze1ejfddd Ze2dd Zdd Ze0eddddĄ ZedejfejddǄ Ze ejfdȃddʄ Ze ejfd˃e ejd˃dd̈́ Ze ejfd΃e ejd΃ddЄ Ze ejfdуe ejdуddӄ Ze ejfdԃddք Ze ejfd׃e ejd׃ddل Ze ejfdڃe ejdڃdd܄ Ze ejd݃dd߄ Ze ejddd Ze ejddd Ze ejddd Ze ejfddd Ze ejddd Ze#ejeje#ejejÃdd Zdd Ze ejfddd Ze ejd݃e ejddd Ze ejddd Ze ejfddd Ze ejfddd Zːd d Ze1ejfde1ejfddd Z͐dd Ze ejϐde΃ e ejϐd	e΃ ed
ejejbdd Ze!ejfdd Zed
ejfej-dd Ze!ejԃdd Ze"ejԃdd Zed
ejej-dd Zed
ejej؃dd Zedejej-ejbdd Zedejejejbdd Ze$ejfdd Ze$ejԃd d! Ze$ejރd"d# Zeejejfejfd$d% Ze3ejfd&d'd( Zd)d* Zd+d, Z		d7d-d.Z	d4d/d0Zd1d2 Zd3d4 Zd5d6 Ze ejfd7d8d9 Zed1eje%e)jVd:d; Zeej[ejej\d<d= Zeejaejej\ejbd>d? Zeedejd@dA Zee	jejfdBdC Zed1eje%e)jVdDdE Zeeeej\ee	jeej\dFdG Zeeej_ee	jej_dHdI Zed1eje%e)jVdJdK Zee	jejbdLdM Zed1eje%e)jVdNdO ZdPdQ Ze<ejfdRdSdT ZdUdV Ze2dWdX Z dYdZ Zd[d\ Zd]d^ Zd_d` Ze2dadb Ze0e	jefdcddZe2dedf Z	e0e	j
d4dgdhZe2didj Ze1ejfdkdldm Ze0e	jefdndoZe0e	jd4dpdqZe0e	jd4drdsZe0e	jd4dtduZe0e	jd4dvdwZe0e	jd4dxdyZe0e	jd4dzd{Zd|d} Ze0ed~d Ze0e	jddefddZe0e	j d8ddZ!e0e	j"e1ejfddd Z#dd Z$e0e	j%d7ddZ&e0e	j'd5ddZ(dd Z)e2dd Z*edejfdd Z+e0e	j,dd Z-dd Z.e2dd Z/e0e	j0dd Z1e0e	j2dd Z3edejfejedejfej-dd Z4e2dd Z5e0e	j6efddZ7e0ed4ddZ8e0ed4ddZ9dd Z:dd Z;dd Z<dd Z=dd Z>dd Z?dd Z@dd ZAe2dd ZBe0e	jCd4ddZDdd ZEdd ZFdd ZGdÐdĄ ZHe2dŐdƄ ZIe0e	jHdǐdȄ ZJdɐdʄ ZKdːd̄ ZLe0e	jMd͐d΄ ZNe0e	jOdϐdЄ ZPe0e	jQdѐd҄ ZRdӐdԄ ZSdՐdք ZTdאd؄ ZUdِdڄ ZVe2dېd܄ ZWe0e	jXd8dݐdބZYdߐd ZZe2dd Z[e0e	j\dd Z]dd Z^e2dd Z_e0e	j`d8ddZadd Zbe2dd Zce0e	jddd Zee2dd Zfe0e	jgdd Zhe2dd Zie0e	jjdd Zke1ejfddd Zle1ejfddd Zme0e	jndd  Zoe0e	jpdd Zqe2dd Zre0e	jsdd Zte0e	jud8ddZve0e	jwd8d	d
Zxi Zydd Zzd9ddZ{edejfdd Z|e0e	j}dd Z~edejfej-ee	jejfej-dd Ze#ejfejfdd Ze#ejfeje#ejfeje#ejfej\e#ejfeje#ejfeje#ejfeje#ejfejdd Ze#ejfejdd Zdd Zeed d! Zeeejfej_ej_d"d# Ze0e	jjjd:d$d%Ze0e	jjjd4d&d'Ze0ed(d) Ze0e	jd*d+ Ze/d,d- Ze0e	jd.d/ Ze0e	jd;d2d3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)as_dtypecarrayfarrayis_contiguous
is_fortrancheck_is_integertype_is_scalar)type_can_asarrayis_nonelikenumpy_version)lower_builtinlower_getattrlower_getattr_genericlower_setattr_generic
lower_castlower_constantiternext_implimpl_ret_borrowedimpl_ret_new_refimpl_ret_untrackedRefType)	signature)StringLiteral)register_jitableoverloadoverload_method	intrinsicoverload_attribute)	quicksort	mergesort)slicing)tuple_setitembuild_full_slice_tuple)overload_classmethod)parse_dtypeparse_shape_parse_nested_sequence_sequence_of_arrays_choose_concatenation_layoutc                 C   s4   t |j|t |j|g}| j|}|d| dS )zz
    Set the "range" metadata on a load instruction.
    Note the interval is in the form [lower_bound, upper_bound).
    rangeN)r   typemoduleadd_metadataset_metadata)builderloadlower_boundupper_boundrange_operandsmd r=   d/home/ncw/WWW/www-new/content/articles/pi-bbp/venv/lib/python3.10/site-packages/numba/np/arrayobj.pyset_range_metadata+   s
   

r?   c                 C   s&   d|j jd > d }t| |d| dS )zF
    Mark the result of a load instruction as positive (or zero).
       r   N)r3   widthr?   )r7   r8   r:   r=   r=   r>   mark_positive6   s   rB   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_typer=   r>   rI   L   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)
rM   __getattr___get_ptr_by_namer2   r	   gep_inboundsr8   appendrB   
pack_array)rN   r7   ptrdimsidimptrr8   )basendimr=   r>   rY   `   s   


z%make_array.<locals>.ArrayStruct.shape)__name__
__module____qualname__rI   propertyrY   __classcell__r=   rU   rW   rc   rd   rX   )rV   r>   rU   J   s    rU   )as_arrayr	   create_struct_proxyrd   )rW   r=   rj   r>   
make_array>   s
   
-rm   c                 C   s   |  |j}| |S )zB
    Return the item size for the given array or buffer type.
    )get_data_typedtypeget_abi_sizeof)contextrW   lltyr=   r=   r>   get_itemsizez   s   
rs   c                 C   s"   |j rdnd}| j||j||dS )z3
    Load the item at the given array pointer.
    Nr@   align)alignedunpack_valuero   )rq   r7   arraytyr_   ru   r=   r=   r>   	load_item   s   ry   c                 C   s$   |j rdnd}| j||j|||dS )z4
    Store the item at the given array pointer.
    Nr@   rt   )rv   
pack_valuero   )rq   r7   rx   valr_   ru   r=   r=   r>   
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)rq   r7   idxtyidxsizeindr=   r=   r>   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   Arrayrd   ro   Integerrm   ry   data)rq   r7   r   r   idxaryidxvalr=   r=   r>   normalize_index   s   
r   c                    s2   t |rt fddt||D  \}}||fS )z^
    Same as normalize_index(), but operating on sequences of
    index types and values.
    c                    s   g | ]\}}t  ||qS r=   )r   ).0r   r   r7   rq   r=   r>   
<listcomp>   s
    z%normalize_indices.<locals>.<listcomp>)lenzip)rq   r7   index_typesindicesr=   r   r>   normalize_indices   s
   r   c                 C   s  | j }| j}| j}	ttjdd}
|j|
 }t|j}t|	j}||@ |k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.
    r@   CzThe datamodel for type z is missing fields  .Nmeminfo)rY   stridesr   itemsizer   parentnswflagsnitemszmissing {0}) rE   rM   
_datamodelr   r   float64rJ   set_fields_fe_typer   
ValueErrorr   get_value_typeget_typer   r   tuplelistr	   r^   intdictget_constantunpack_tupler3   countmulkeysformatitemssetattr)arrayr   rY   r   r   r   r   rq   r7   rR   standard_arraystandard_array_type_datamodelrequired_fieldsdatamodel_fieldsmissingmsgintp_tattrsr   unpacked_shapeaxlen
got_fieldskvr=   r=   r>   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.
    r@   r   r   N)rE   rM   r   r   r   r	   r   rY   rd   r   r   rs   r   )arytyr   rq   r7   r   r   r   r=   r=   r>   update_array_info   s   
r   c                 C   s   t  )z/Constrain axis values to valid positive values.rG   	func_namearg_namerd   axisr=   r=   r>   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   r=   r>   impl"  s
   z&normalize_axis_overloads.<locals>.impl)r   r!   r   TypingErrorliteral_value)r   r   rd   r   r   r=   r   r>   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   )rq   r7   rP   r   rx   r   iterobjzeroindexptrresoutr=   r=   r>   getiter_array-  s   r   c              	   C   s   t j||j|jd}t j||j|jd}||d |}t ||j|}	|dd }
|dd }t|t	j
rEt| |||||	|
|}| S |
rIJ t| |||	S )z1 Evaluate `ary[idx]`, where idx is a single int. r   r   r@   N)r	   r   rY   rd   r   r   pointer_addr   r   r   Buffer	make_viewr   ry   )rq   r7   r   r   aryr   shapesr   offsetdataptrview_shapesview_stridesretaryr=   r=   r>   _getitem_array_single_intE  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valuer@   r   <)r   rW   r   rm   r   r	   r   rY   r8   r   icmp_signed	set_validif_thenr   
yield_typeyield_increment_indexstore)rq   r7   rP   r   resultitertyiterrx   r   r   r   r   is_validr   nindexr=   r=   r>   iternext_arrayZ  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   r@   c                 S      g | ]}t |r|qS r=   r   r   r   r=   r=   r>   r         z"basic_indexing.<locals>.<listcomp>r   unexpected index type: %sF
wraparoundboundscheck)r   r   r   r	   r   rY   rd   r   r   r   ellipsisr2   r]   r   	SliceTyper   r)   guard_invalid_slice	fix_slicestartget_slice_length
fix_strider   r   do_boundscheckr   rG   get_item_pointer)rq   r7   r   r   r   r   r
  r   oner   r   output_indicesoutput_shapesoutput_stridesnum_newaxesaxindexvalr   	n_missingra   sliceshstr   r   r=   r=   r>   basic_indexingv  sf   










r  c           	   	   C   s.   t || |}t|||||j|j|jd |S )zF
    Build a view over the given array with the given parameters.
    r   rY   r   r   r   r   )rm   r   r   r   r   )	rq   r7   r   r   r   r   r   r   r   r=   r=   r>   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   ry   )rq   r7   r   r   r   r   r   r   r   r   r   r=   r=   r>   _getitem_array_generic  s   r#  c           	      C   sZ   |j \}}|\}}|jdksJ t|| ||}t| ||j|||f|f}t| ||j|S )z4
    Basic indexing with an integer or a slice.
    r@   )r   rd   rm   r#  r   r   	rq   r7   rP   r   r   r   r   r   r   r=   r=   r>   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   tyr=   r=   r>   	<genexpr>      z&getitem_array_tuple.<locals>.<genexpr>)r   rm   r   r	   r   r   r   anyfancy_getitemr#  r   r   )rq   r7   rP   r   r   tuptyr   tupr   r   r   r=   r=   r>   getitem_array_tuple  s    


r0  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   rm   r   r  r"  rG   boolfancy_setslicer~   ro   r|   )rq   r7   rP   r   r   r   valtyr   r   r{   r   r   r   r   r   use_fancy_indexingr=   r=   r>   setitem_array	  s4   

r6  c           	      C   sD   |j \}|\}t|}|| ||}|j}||d}t| ||j|S r   )r   rm   rY   extract_valuer   r   )	rq   r7   rP   r   r   r   arysttyshapearyr   r=   r=   r>   	array_len2  s   r:  z
array.itemc                 C   s   |j \}|\}t|| ||}|j}|j|d||ddd d}| j|t|f W d    n1 s7w   Y  t	| |||j
S )N!=r@   Flikelyz>item(): can only convert an array of size 1 to a Python scalar)r   rm   r   r   r   r3   	call_convreturn_user_excr   ry   r   )rq   r7   rP   r   r   r   r   r   r=   r=   r>   
array_item=  s   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;  r@   Fr<  z/itemset(): can only write to an array of size 1)r   ro   rm   r   r   r   r3   r>  r?  r   r|   r   get_dummy_value)
rq   r7   rP   r   r   r4  r   r{   r   r   r=   r=   r>   array_itemsetL  s   
rB  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   rN   r=   r=   r>   prepareg     zIndexer.preparec                 C   rE  )z=
        Return this dimension's size as an integer.
        r   rF  r=   r=   r>   get_sizen     zIndexer.get_sizec                 C   rE  )z;
        Return this dimension's shape as a tuple.
        r   rF  r=   r=   r>   	get_shapet  rJ  zIndexer.get_shapec                 C   rE  )z}
        Return a half-open [lower, upper) range of indices this dimension
        is guaranteed not to step out of.
        r   rF  r=   r=   r>   get_index_boundsz  rH  zIndexer.get_index_boundsc                 C   rE  )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   rF  r=   r=   r>   	loop_head  s   	zIndexer.loop_headc                 C   rE  )z)
        Finish indexation loop.
        r   rF  r=   r=   r>   	loop_tail  rJ  zIndexer.loop_tailN)
re   rf   rg   __doc__rG  rI  rK  rL  rM  rN  r=   r=   r=   r>   rD  a  s    rD  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'  )	rq   r7   r   r   dimr   r   r   ll_intp)rN   rq   r7   r   r   rR  r=   r=   r>   __init__  s   zEntireIndexer.__init__c                 C   sB   | j }|| jj| j| _t|| j| _	|
 | _|
 | _d S r'  )r7   r7  r   rY   rR  r   r	   rL   rS  r   append_basic_blockbb_startbb_endrN   r7   r=   r=   r>   rG    s
   
zEntireIndexer.preparec                 C      | j S r'  r   rF  r=   r=   r>   rI       zEntireIndexer.get_sizec                 C      | j fS r'  rZ  rF  r=   r=   r>   rK       zEntireIndexer.get_shapec                 C      |  d| jfS r   rS  r   rF  r=   r=   r>   rL       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<  )r7   r   r   rS  r   branchrV  position_at_endr8   r   r   r   rW  )rN   r7   	cur_indexr=   r=   r>   rM    s   
zEntireIndexer.loop_headc                 C   D   | j }t||| j}||| j || j || j	 d S r'  )
r7   r	   r   r8   r   r   rc  rV  rd  rW  rN   r7   
next_indexr=   r=   r>   rN    s
   zEntireIndexer.loop_tailNre   rf   rg   rO  rT  rG  rI  rK  rL  rM  rN  r=   r=   r=   r>   rQ    s    rQ  c                   @   rP  )IntegerIndexerz0
    Compute indices from a single integer.
    c                 C   s&   || _ || _|| _| j tj| _d S r'  )rq   r7   r   r   r   r   rS  )rN   rq   r7   r   r=   r=   r>   rT    s   zIntegerIndexer.__init__c                 C      d S r'  r=   rF  r=   r=   r>   rG       zIntegerIndexer.preparec                 C   s   t | jdS Nr@   )r   rS  rF  r=   r=   r>   rI       zIntegerIndexer.get_sizec                 C      dS Nr=   r=   rF  r=   r=   r>   rK    rl  zIntegerIndexer.get_shapec                 C   s   | j | j| j |  fS r'  )r   r7   addrI  rF  r=   r=   r>   rL    s   zIntegerIndexer.get_index_boundsc                 C   s
   | j d fS r'  )r   rF  r=   r=   r>   rM       
zIntegerIndexer.loop_headc                 C   rk  r'  r=   rF  r=   r=   r>   rN    rl  zIntegerIndexer.loop_tailNri  r=   r=   r=   r>   rj    s    rj  c                   @   rP  )IntegerArrayIndexerz;
    Compute indices from an array of integer indices.
    c                 C   s@   || _ || _|| _|| _|| _|jdksJ | j tj| _	d S rm  )
rq   r7   r   r   r   rd   r   r   r   rS  )rN   rq   r7   r   r   r   r=   r=   r>   rT    s   zIntegerArrayIndexer.__init__c                 C   sD   | j }t|| jjd | _t|| j| _|	 | _
|	 | _d S r   )r7   r	   r   r   rY   idx_sizerL   rS  	idx_indexrU  rV  rW  rX  r=   r=   r>   rG    s
   
zIntegerArrayIndexer.preparec                 C   rY  r'  rt  rF  r=   r=   r>   rI    r[  zIntegerArrayIndexer.get_sizec                 C   r\  r'  rv  rF  r=   r=   r>   rK    r]  zIntegerArrayIndexer.get_shapec                 C   r^  r   r_  rF  r=   r=   r>   rL    r`  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 ra  )r7   r   r   rS  ru  rc  rV  rd  r8   r   r   rt  rW  r   rq   r   ro   r   r   r   )rN   r7   re  r   r=   r=   r>   rM    s&   zIntegerArrayIndexer.loop_headc                 C   rf  r'  )
r7   r	   r   r8   ru  r   rc  rV  rd  rW  rg  r=   r=   r>   rN    s   
zIntegerArrayIndexer.loop_tailNri  r=   r=   r=   r>   rs    s    	rs  c                   @   rP  )BooleanArrayIndexerz>
    Compute indices from an array of boolean predicates.
    c                 C   sH   || _ || _|| _|| _|jdksJ | j tj| _t	| jd| _
d S Nr@   r   )rq   r7   r   r   rd   r   r   r   rS  r   r   )rN   rq   r7   r   r   r=   r=   r>   rT  &  s   zBooleanArrayIndexer.__init__c                 C   s^   | j }t|| jjd | _t|| j| _t|| j| _	|
 | _|
 | _|
 | _d S r   )r7   r	   r   r   rY   r   rL   rS  ru  r   rU  rV  bb_tailrW  rX  r=   r=   r>   rG  /  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'  )r7   r	   r   r   	for_ranger   r8   r   rq   r   ro   r   r   rq  zextr3   r   )rN   r7   r   loopcpredr=   r=   r>   rI  8  s   

	zBooleanArrayIndexer.get_sizec                 C   
   |   fS r'  rI  rF  r=   r=   r>   rK  G  rr  zBooleanArrayIndexer.get_shapec                 C   r^  r   r_  rF  r=   r=   r>   rL  J  s   z$BooleanArrayIndexer.get_index_boundsc                 C   s  | j }| j | j| j | j | j| j || j || j || j}|| j}|j	|
d|| jdd || j W d    n1 sKw   Y  t| j|| jj| j| j|}|	|| || j W d    n1 sww   Y  t||}||| j ||fS )Nrb  Fr<  )r7   r   r   ru  r   rc  rV  rd  r8   r   r   r   rW  r   rq   r   ro   r   not_ry  r	   r   )rN   r7   re  	cur_countr~  
next_countr=   r=   r>   rM  O  s,   zBooleanArrayIndexer.loop_headc                 C   s\   | j }|| j || j t||| j}||| j || j	 || j
 d S r'  )r7   rc  ry  rd  r	   r   r8   ru  r   rV  rW  rg  r=   r=   r>   rN  g  s   
zBooleanArrayIndexer.loop_tailNri  r=   r=   r=   r>   rw  !  s    		rw  c                   @   rP  )SliceIndexerz(
    Compute indices along a slice.
    c                 C   sL   || _ || _|| _|| _|| _|| _|| _| j tj	| _
t| j
d| _d S r   )rq   r7   r   r   rR  r   r  r   r   r   rS  r   r   )rN   rq   r7   r   r   rR  r   r  r=   r=   r>   rT  w  s   zSliceIndexer.__init__c                 C   s   | j }|| jj| j| _t| j|| j	| j
 t|| j
| j t|| j
j| _t|| j| _t|| j| _| | _| | _d S r'  )r7   r7  r   rY   rR  dim_sizer)   r  rq   r   r  r  r	   
is_neg_intstepis_step_negativerL   rS  r   r   rU  rV  rW  rX  r=   r=   r>   rG    s   
zSliceIndexer.preparec                 C   s   t | j| jS r'  )r)   r  r7   r  rF  r=   r=   r>   rI       zSliceIndexer.get_sizec                 C   r  r'  r  rF  r=   r=   r>   rK    rr  zSliceIndexer.get_shapec                 C   s   t | j| j\}}||fS r'  )r)   get_slice_boundsr7   r  )rN   lowerupperr=   r=   r>   rL    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<=rb  Fr<  )r7   r   r  r  r   r   r   rc  rV  rd  r8   selectr  r   stopr   rW  )rN   r7   re  r  is_finishedr=   r=   r>   rM    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   )r7   rq  r8   r   r  r  r   r	   r   r   rc  rV  rd  rW  )rN   r7   rh  r  r=   r=   r>   rN    s   zSliceIndexer.loop_tailNri  r=   r=   r=   r>   r  r  s    r  c                   @   rC  )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  r=   r  r  r=   r=   r>   r     r  z)FancyIndexer.__init__.<locals>.<listcomp>r   r@   r  )$rq   r7   r   r	   r   rY   rd   r   r   r   r   r   rS  newaxesr   r   r  r2   rQ  r]   r   r  r   r  r   r   rj  r   rm   ro   rs  Booleanrw  r   AssertionErrorindexers)rN   rq   r7   r   r   r   r   r  r  r  new_axr  r   r  ra   indexerr  r   r   r=   r=   r>   rT    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 )Nr@   c                 S      g | ]}|  qS r=   )rK  r   ra   r=   r=   r>   r         z(FancyIndexer.prepare.<locals>.<listcomp>r=   )
r  rG  rq   r   r   r   r  insertsumindexers_shape)rN   ra   r  	res_shaper=   r=   r>   rG    s   


zFancyIndexer.preparec                 C   rY  )z?
        Get the resulting data shape as Python tuple.
        )r  rF  r=   r=   r>   rK    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   r@   ==r   )r   r   rd   r7   r	   	false_bitrS  r   r  r  or_icmp_unsignedrL  r   subr   rq  r  )rN   r   r   r7   is_emptyr   r  r  r  r  rY   stridelower_indexupper_indexlower_offsetupper_offsetis_downwardsr=   r=   r>   get_offset_bounds  sB   


zFancyIndexer.get_offset_boundsc                 C   s    t dd | jD  \}}||fS )Nc                 s   s    | ]}|  V  qd S r'  )rM  r  r=   r=   r>   r*  >  s    z+FancyIndexer.begin_loops.<locals>.<genexpr>)r   r  )rN   r   countsr=   r=   r>   begin_loops=  s   zFancyIndexer.begin_loopsc                 C   s   t | jD ]}|  qd S r'  )reversedr  rN  )rN   ra   r=   r=   r>   	end_loopsA  s   
zFancyIndexer.end_loopsN)
re   rf   rg   rO  rT  rG  rK  r  r  r  r=   r=   r=   r>   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   rY   r   r   r  rG  r   rK  _empty_nd_implr   r   r   r   r  get_item_pointer2layoutr"  ry   r8   gepr|   r   r   r  r   r   )rq   r7   rP   r   r   r   r   r   r   r   r   r  out_ty
out_shapesr   out_dataout_idx_r_   r{   curnext_idxr=   r=   r>   r-  F  s6   

r-  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   rm   rd   r   r#  r   r   r-  r$  r=   r=   r>   fancy_getitem_arrayq  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   r@   CFrb  r  )r   r3   r  r   r   r2   rd   r  r   r  rq  )rq   r7   arrtyarrr   r   r   r   r  r  r  ra   max_axis_offset
is_upwardsr  r=   r=   r>   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.
    )ptrtointr3   rq  )rq   r7   r  r  r   data_ptr_as_intr  endr=   r=   r>   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  )	rq   r7   r  r  r   r   r   r  r  r=   r=   r>   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  )rq   r7   a_starta_endb_startb_endmay_overlapr=   r=   r>   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	   rL   if_elser   r  r8   r  ry   )source_indicessrc_ptrif_copy	otherwise)r7   rq   	copy_datacopy_layoutcopy_shapescopy_stridesptrtysrc_data
src_shapessrc_stridessrctyuse_copyr=   r>   src_getitem  s4   
z&maybe_copy_source.<locals>.src_getitemc                     sX    j dd  }  | } j |  W d    d S 1 s%w   Y  d S )NFr<  )r   r8   bitcastr   freer   )r7   rq   r  r  	voidptrtyr=   r>   src_cleanup  s
   
"z&maybe_copy_source.<locals>.src_cleanup)r3   r	   r   r   r   r   r   r   allocater  r   r   r   r   	loop_nestr  r  r8   )rq   r7   r  r  srcr  r  r  	allocsizer   r   r   r  dest_ptrr  r  r=   )r7   rq   r  r  r  r  r  r  r  r  r  r  r  r>   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   r@   c                       g | ]	}  d |qS r  )r  r   r  r7   r  r=   r>   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  r>  r?  r   )
rq   r7   r   r   target_shaper   nd_diff
dim_is_oneacceptedr   r=   r  r>   _bc_adjust_dimension  s,   	

r  c           
         s   g }g }|  tjd|  tjd fddt||D } fdd|D } fddt||D }	 fddt|	||D } fddt|	|D }||fS )	z
    Broadcast shapes and strides to target_shape given that their ndim already
    matches.  For each location where the shape is 1 and does not match the
    dim for target, it is set to the value at the target and the stride is
    set to zero.
    r   r@   c                    s   g | ]\}}  d ||qS )r;  r   )r   taroldr7   r=   r>   r   @      z,_bc_adjust_shape_strides.<locals>.<listcomp>c                    r  r  r  )r   r  r  r=   r>   r   B      c                    s   g | ]
\}}  ||qS r=   )r  )r   xyr  r=   r>   r   C      c                    s    g | ]\}}}  |||qS r=   r  )r   pr  r  r  r=   r>   r   D  s    c                    s   g | ]\}}  ||qS r=   r  )r   r   r  )r7   r   r=   r>   r   F  r  )r   r   r   r   )
rq   r7   r   r   r  	bc_shapes
bc_stridesmismatch
src_is_onepredsr=   )r7   r  r   r>   _bc_adjust_shape_strides4  s    


r  c           	   	   C   s   t ||j}t ||j}t| ||||\}}t| ||||\}}|jt|dd}t|| |}t	||j
t ||t |||j|j|jd ||fS )zX
    Broadcast the given array to the target_shape.
    Returns (array_type, array)
    Ard   r  r   )r	   r   rY   r   r  r  copyr   rm   r   r   r^   r   r   r   )	rq   r7   arrtyper  r  r   r   new_arrtypenew_arrr=   r=   r>   _broadcast_to_shapeK  s&   


r  c                 C   s,   |j |jddd}|||}dd }||fS )Nr  T)rd   r  readonlyc           
   	   S   sz   |\}}|j d }t|| ||}t||}t| ||||\}}t|dt| |j	dd  |
 }	t| ||j|	S )Nr   r   )r   rm   r	   r   r  r   r   r   r   r   r   r   r   )
rq   r7   rP   r   r  shape_r  r  destr   r=   r=   r>   codegenj  s   
z$_numpy_broadcast_to.<locals>.codegen)r	  r   )	typingctxr   rY   retrP   r  r=   r=   r>   _numpy_broadcast_toe  s   
r  c                 C   s$   |j dd}||}dd }||fS )NTr  c                 S   sH   |\}|j d }t|| ||}t|jj|_| }t| ||j|S r   )	r   rm   r	   get_null_valuer   r3   r   r   r   )rq   r7   rP   r   r  r  r  r   r=   r=   r>   r    s   
z#get_readonly_array.<locals>.codegen)r	  )r  r  r  rP   r  r=   r=   r>   get_readonly_array|  s   	r  c           
      C   s   | j }t|}t|}||krtd|D ]
}|dk rtdqd}|| }||k 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-negativer@   z=operands could not be broadcast together with remapped shapes)rY   r   r   )
r   
dest_shape	src_shapesrc_ndim	dest_ndimr   	src_index
dest_indexsrc_dimdest_dimr=   r=   r>   _can_broadcast  s&   
r   c                 C   s   t | } t| | t| |S r'  )npasarrayr   r  r   rY   r=   r=   r>   _default_broadcast_to_impl  s   


r$  c                 C   s   t | s	tdt|tjrdd }|S t|tj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#  r=   r=   r>   r        z numpy_broadcast_to.<locals>.implz7The second argument "shape" must be a tuple of integersr   c                 S      g | ]}t |tjqS r=   )r   r   IntegerLiteral)r   typr=   r=   r>   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#  r=   r=   r>   r     s   
z/Cannot broadcast a non-scalar to a scalar arrayz:The argument "shape" must be a tuple or an integer. Got %s)r   r   r   r   r   r   UniTuplero   r$  Tupler   allr   rd   r   )r   rY   r   r   is_scalar_arrayr=   r=   r>   numpy_broadcast_to  s4   




r/  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 allowedr@   z=shape mismatch: objects cannot be broadcast to a single shape)r2   r   r   )rmrY   ra   r   tmpr=   r=   r>   numpy_broadcast_shapes_list  s   
r4  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 ddlm   fd	d
}|S )Nr   z	Argument z* must be either an int or tuple[int]. Got r@   c                  W   ro  rp  r=   r   r=   r=   r>   <lambda>      z+ol_numpy_broadcast_shapes.<locals>.<lambda>r@   )_isinstance_no_warnc                     sz   dg }}t | D ] } |trt|dkrt|| q |tr+t||f qt|D ]
\}}t|||}q0|S rx  )r   r   r   r4  r   	enumerater*   )r   r1  r/  argr   elemr9  r2  tup_initr=   r>   r     s   

z'ol_numpy_broadcast_shapes.<locals>.impl)r:  r   r   r   r+  ro   r,  r   r   r   maxr1  numba.cpython.builtinsr9  )r   r   r;  is_intis_int_tupleis_empty_tupler   r   r=   r=  r>   ol_numpy_broadcast_shapes  s2   
rD  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   r@   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 rm  )	r   r4  r!  r"  rY   r2   r*   r]   r%  )r   rY   r   r/  ra   outsr2  r>  r=   r>   r   >  s   
z$numpy_broadcast_arrays.<locals>.impl)r:  r   r   r   r   r   r   r1  ro   ArrayCompatibler?  rd   Numberr  )r   r   r;  unified_dtypedtr   r=   rG  r>   numpy_broadcast_arrays  s6   rL  c           '         s:  |j \}}|\}}t| |}t |j}	t |j}
|j}t ||||}|  t	t
jrۈj| }t t |\t j}t j}j}tj}t|t|ksnJ t||D ]\}} | d||}qs j|dd d}j t|f 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 d}j t|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.
    r;  Fr<  z0cannot assign slice from input of different sizeNr@   r   c                    s,   | \} tjttj}| |fS r'  )rF   operatorgetitemr    r   r   )r  r   getitem_implr7   rq   r  	src_dtyper  r=   r>   r    s   z#fancy_setslice.<locals>.src_getitemc                   S   rk  r'  r=   r=   r=   r=   r>   r    rl  z#fancy_setslice.<locals>.src_cleanupc                    s    S r'  r=   )r  )r  r=   r>   r    rl  c                   S   rk  r'  r=   r=   r=   r=   r>   r    rl  c                 S   s   g | ]}|d ur|qS r'  r=   )r   r}  r=   r=   r>   r     r  z"fancy_setslice.<locals>.<listcomp>r  )/r   rm   r	   r   rY   r   r   r  rG  r   r   r   ro   rK  r  r  r   r   r  r   r   r>  r?  r   r  r  r   r  r  r  SequencerF   r    r   r   r   r  r   r  r  r~   r  r  r"  r|   r  rA  )'rq   r7   rP   r   r   r   r   r  r   dest_shapesdest_strides	dest_datar  index_shaper  r  r  shape_errorur   r   	src_startsrc_end
dest_lower
dest_upper
dest_startdest_endr  r  r  len_implseq_lenr   dest_indicesr  ra   r  r{   r  r=   rP  r>   r3  U  s   







r3  c                    s   |j d }|j dd  }|d }|dd  } fddt||D }t ||d j}tjtjt|d}	t	
|j||	}
||f}|
|fS )Nr   r@   c                    "   g | ]\}}  ||tjqS r=   r~   r   r   )r   r)  r{   r   r=   r>   r         z#vararg_to_tuple.<locals>.<listcomp>ro   r   )r   r   r	   r^   r3   r   r+  r   r   r   r    r   )rq   r7   rP   r   r   dimtysr   r`   rY   shapetynew_signew_argsr=   r   r>   vararg_to_tuple  s   
rj  zarray.transposec                 C   s   t | ||jd |d S r   )array_Tr   rq   r7   rP   r   r=   r=   r>   array_transpose  s   rm  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   rY   r   rR  r  r=   r=   r>   permute_arrays  s   ro  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   r@   c                       g | ]}t  qS r=   r	   rL   r   r  )r7   ll_ary_sizer=   r>   r     r  z)array_transpose_tuple.<locals>.<listcomp>r   ro   rd   r  c                    s   g | ]	}t  qS r=   rm   rr  )r7   rq   	np_ary_tyr=   r>   r      r  r   rY   r   r   r   c                 S   r  r=   r   r   ar=   r=   r>   r   0  r     r   ) r   rm   r   ro   r   r   r   r   	ArrayTyperY   r   r   r   r   r   rp   r   r  
as_pointercompile_internalro  r   r    voidr   r   r8   r   r   r   r   r   )rq   r7   rP   r   r   r   axistyr   num_axisro   rS  arysll_arysr  dstnp_itemsizenp_arysnp_aryll_aryr  r   r=   )r7   rq   rs  rv  r>   array_transpose_tuple  sP   
	r  c                 C       t | |||\}}t| |||S r'  )rj  r  rq   r7   rP   r   rh  ri  r=   r=   r>   array_transpose_vararg?     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rz  axesr=   r=   r>   np_transpose_implK  r]  z*numpy_transpose.<locals>.np_transpose_implc                 S   
   |  |S r'  r  r  r=   r=   r>   r  N  rr  r'  )r   r   r1  r   UnsupportedError)rz  r  r  r=   r=   r>   numpy_transposeE  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 )Nr@   r   )rd   rm   r	   r   rY   r   r   r   r^   r   r   r   r   r   )	rq   r7   r)  r   r   r   r  r   r   r=   r=   r>   rk  T  s    
rk  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  r=   r=   r>   r   q  s   znumpy_logspace.<locals>.implr  )r   r   rI  r   r   r   r   )r  r  r  r   r=   r=   r>   numpy_logspaceh  s   



r  r@   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 integer)The first argument "arr" must be an arrayr{  Input must be >= 2-d.r@   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   r@   r{     )r!  swapaxesfliplrflipudr  )r  r   r=   r=   r>   r     s   znumpy_rot90.<locals>.implr8  )	r   r   r   r   r   r   r   rd   NumbaValueError)r  r   r   r=   r=   r>   numpy_rot90x  s   




r  c                 C   s   |  tj}| }|  tj}	t|	||||||||	g}
t|j	|
d}||j
}t||ddd}t||ddd}||}t||dd}t||dd}|	d}|||||||||j|g}|S )a  
    Call into Numba_attempt_nocopy_reshape() for the given array type
    and instance, and the specified new shape.

    Return value is non-zero if successful, and the array pointed to
    by *newstrides* will be filled up with the computed results.
    numba_attempt_nocopy_reshaperY   r   r   )r   r   r   r}  intcr   FunctionTyper	   get_or_insert_functionr4   rd   r\   r[   callr   )rq   r7   r   r   newndnewshape
newstridesrS  ll_intp_starll_intcfntyfnndrY   r   
is_f_orderr   r=   r=   r>   _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   r@   z)total size of new array must be unchangedzmultiple negative shape values)r:  r   )	origsizerY   num_neg_value
known_sizer  r   neg_axinferredokr=   r=   r>   normalize_reshape_value  s*   

r  zarray.reshapec              	   C   s  |j d }|j}|j d }|d }| tj}t||j}	t|| ||d }
t	
||	}||| tj|jddd}t|| |}| tj| |}t|||| | tj|jg|g|d d |
j}| |tttjtj||| g |j}t	
||	}t| |||
|||}|d||d}|| d}| j |t!|f W d    n1 sw   Y  t|| |}t||
j"|#||#||
j$|
j%|
j&d | }t'| ||j|S )	Nr   r@   r   rt  rw  r  zincompatible shape for arrayr   )(r   r   r   r   r   r   r|  r   rm   r	   rL   r   r   ro   r   rp   r   r  r}  r   r~  r  r   r    r  r   r   r  r  r3   r   r>  r?  rG   r   r8   r   r   r   r   )rq   r7   rP   r   r   rettyrg  rY   rS  ll_shaper   r  shape_ary_ty	shape_aryshape_itemsizer   r  r  r  failr   r  r   r=   r=   r>   array_reshape  sb   


r  c                 C   r  r'  )rj  r  r  r=   r=   r>   array_reshape_vararg  r  r  c                 C      dd }|S )Nc                 S   r  r'  )reshape)rz  rY   r=   r=   r>   np_reshape_impl  rr  z#np_reshape.<locals>.np_reshape_implr=   )rz  rY   r  r=   r=   r>   
np_reshape  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!  ravelr"  concatenater  valuesr   r=   r=   r>   r   .  s   znp_append.<locals>.implz,The third argument "axis" must be an integerc                 S   s   t j| |f|dS Nr   r!  r  r  r=   r=   r>   r   8     r'  )r   r   r   r   r   r   r   )r  r  r   r   r=   r=   r>   	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   r=   r=   r>   
imp_nocopy@     zarray_ravel.<locals>.imp_nocopyc                 S   r  )zCopy version)flattenr  r=   r=   r>   imp_copyD     zarray_ravel.<locals>.imp_copyr   r   )r   r  r~  r   r   )rq   r7   rP   r   r  r  impr   r=   r=   r>   array_ravel=  s   r  c                 C   s   dd }|  ||||S )Nc                 S   r  r'  )r  rz  r=   r=   r>   np_ravel_implV  r]  znp_ravel.<locals>.np_ravel_impl)r~  )rq   r7   rP   r   r  r=   r=   r>   np_ravelT  s   r  zarray.flattenc                 C   s,   dd }|  ||||}t| ||j|}|S )Nc                 S   s   |   | jS r'  )r	  r  r   r  r=   r=   r>   r  _  r  zarray_flatten.<locals>.imp)r~  r   r   )rq   r7   rP   r   r  r   r=   r=   r>   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ndindexrY   minr?  )rz  a_mina_maxr   r  a_ba_min_ba_max_br   val_a	val_a_min	val_a_maxr=   r=   r>   _np_clip_implg  s   r  c                 C   sH   t | jD ]}| | }|| }|rt||||< qt||||< q|S r'  )r!  r  rY   r  r?  )rz  buse_minr   r   r  val_br=   r=   r>   _np_clip_impl_noneu  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 )N#The argument "a" must be array-likez6The argument "a_min" must be a number or an array-likez6The argument "a_max" must be a number or an array-likez5The argument "out" must be an array if it is providedc                 S   s   t d)Nz&array_clip: must set either max or minr   rz  r  r  r   r=   r=   r>   
np_clip_nn  r]  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  rY   r  r?  rz  r  r  r   r  r   r  r=   r=   r>   
np_clip_ss  s
   znp_clip.<locals>.np_clip_ssc                 S   sB   |d u r	t | n|}t | jD ]}| | }t||||< q|S r'  )r!  r  r  rY   r?  r  r=   r=   r>   
np_clip_sn  
   znp_clip.<locals>.np_clip_snc                 S   s   t | |}t| |||S r'  r!  	full_liker  )rz  r  r  r   
a_min_fullr=   r=   r>   
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  rY   r  r  r=   r=   r>   
np_clip_ns  r  znp_clip.<locals>.np_clip_nsc                 S   s   t | |}t| |||S r'  r  )rz  r  r  r   
a_max_fullr=   r=   r>   
np_clip_as  r  znp_clip.<locals>.np_clip_asc                 S   s4   |d u r	t | n|}t | |\}}t||d|S )NTr!  r  r  r  )rz  r  r  r   r  r  r  r=   r=   r>   
np_clip_na     znp_clip.<locals>.np_clip_nac                 S   s4   |d u r	t | n|}t | |\}}t||d|S )NFr  )rz  r  r  r   r  r  r  r=   r=   r>   
np_clip_an  r  znp_clip.<locals>.np_clip_anc                 S      t | |||S r'  )r  r  r=   r=   r>   
np_clip_aa  s   znp_clip.<locals>.np_clip_aar'  )	r   r   r   r   r   NoneTyper   r   rI  )rz  r  r  r   r   a_min_is_nonea_max_is_noner  a_min_is_scalara_max_is_scalarr  r  r  r  r  r   r  r  r=   r=   r>   np_clip  sV   












r
  clipc                 C   s   ddd}|S )Nc                 S   s   t | |||S r'  )r!  r  r  r=   r=   r>   r     r  zarray_clip.<locals>.implNNNr=   )rz  r  r  r   r   r=   r=   r>   
array_clip  s   
r  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   Fr@   Tr=   )	r  r`   r   old_itemsizenew_itemsizer  ra   newdim
bytelength
any_layoutc_layoutf_layoutint8r=   r>   r  	  s4   

z_change_dtype.<locals>.imprY   r   r   r@   rt  Nrw  )rd   r  ordr   r  r   r   rs   r	   r\   r[   r   rm   r^   rp   rn   r   r   r    booleanr~  r   r   r   )rq   r7   oldtynewtyr   
new_layoutr  r  r  r  
shape_datastrides_datashape_strides_array_typearyclsshape_constantsizeof_intpstrides_constantr  strides_aryrY   r   r   rP   r   r=   r  r>   _change_dtype	  s   	
0



	
r%  c                 C      t | s	tddd }|S )Nz+The argument to np.shape must be array-likec                 S   s   t | jS r'  )r!  r"  rY   r  r=   r=   r>   r   	  rn  znp_shape.<locals>.implr   r   r   rz  r   r=   r=   r>   np_shape	  s   
r)  c                 C      dd }|S )Nc                    sJ   t |   t d d } fddt dd  D }t || S )Nr@   c                    s    g | ]\}} | |kr|qS r=   r=   )r   ra   r  r  r=   r>   r   	       z5np_unique.<locals>.np_unique_impl.<locals>.<listcomp>)r!  sortr  r   r:  r   )rz  headtailr=   r+  r>   np_unique_impl	  s   z!np_unique.<locals>.np_unique_implr=   )rz  r0  r=   r=   r>   	np_unique	  s   r1  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ro   r   r@   r0  z(operands could not be broadcast together)r!  r"  int64rY   np_repeat_impl_repeats_scalerr,  r   r  emptyr  ro   r2   )	rz  repeatsrepeats_arrayasaaraveln	to_returnposra   r=   r=   r>   !np_repeat_impl_repeats_array_like	  s$   

z4np_repeat.<locals>.np_repeat_impl_repeats_array_likezIThe repeats argument must be an integer or an array-like of integer dtype)r   r   r   Listr1  rI  r  r   r4  ro   r   r   )rz  r6  r=  r=   r=   r>   	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   r0  r2  r@   )	r   r!  r"  r  rY   r5  ro   r	  r2   )rz  r6  r8  r9  r:  r;  ra   r=   r=   r>   r4  	  s   


r4  repeatc                 C   r  )Nc                 S      t | |S r'  )r!  r@  )rz  r6  r=   r=   r>   array_repeat_impl	  rn  z'array_repeat.<locals>.array_repeat_implr=   )rz  r6  rB  r=   r=   r>   array_repeat	     rC  c                 C      t |}dd }||fS )z"Computes the itemsize of the dtypec                 S   s,   |  |jd j}|  |j}|| |S r   )rn   r   ro   r   rp   )cgctxr7   rP   llargsrr   llintpr=   r=   r>   r  	  s   z%_intrin_get_itemsize.<locals>.codegen)r   r   )tyctxro   rP   r  r=   r=   r>   _intrin_get_itemsize	  s   
rJ  c                 C   rk  r'  r=   )rz  ro   r=   r=   r>   _compatible_view	  rl  rK  generic)targetc                 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 unchangedr@   zJTo change to a dtype of a different size, the last axis must be contiguouszZWhen changing to a smaller dtype, its size must be a divisor of the size of original dtypezuWhen changing to a larger dtype, its size must be a divisor of the total size in bytes of the last axis of the array.)rJ  r   rd   r   rY   r   r   )rz  ro   
dtype_sizemsg1r   p1p2p3msg2msg3r  msg4r=   r=   r>   r   	  s0   




z ol_compatible_view.<locals>.implr=   rz  ro   r   r=   r=   r>   ol_compatible_view	  s   #rW  z
array.viewc                 C   sD  |j d }|j}t|| ||d }t|| |}t|jj}t|D ]}	t||	}
|	dkr:|jj	}|
|
||_q#t||	|
 q#tdkrc| 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@      r  z"new type not compatible with array)r   r   rm   r   r   r   sortedgetattrr   r3   r  r   r   typing_contextresolve_value_typerK  get_call_typerF   r%  r  r   r   r>  r?  r   r   r   )rq   r7   rP   r   r   r  r   r  fieldsr   r{   r  rI  r  _compatible_view_sigr   r  r  r   r   r=   r=   r>   
array_view
  s2   



r`  ro   c                 C   s   |   }t| |||S r'  )rA  r   rq   r7   r)  r   r   r=   r=   r>   array_dtypeD
  s   rb  rY   c                 C   (   t |}|| ||}|j}t| |||S r'  )rm   rY   r   rq   r7   r)  r   rx   r   r   r=   r=   r>   array_shapeJ
     re  r   c                 C   rc  r'  )rm   r   r   rd  r=   r=   r>   array_stridesS
  rf  rg  rd   c                 C      |  tj|j}t| |||S r'  )r   r   r   rd   r   ra  r=   r=   r>   
array_ndim\
  s   ri  r   c                 C   rc  r'  )rm   r   r   rd  r=   r=   r>   
array_sizec
  s   rj  r   c                 C   rc  r'  )rm   r   r   rd  r=   r=   r>   array_itemsizek
  rf  rk  nbytesc                 C   s2   t |}|| ||}||j|j}t| |||S )z"
    nbytes = size * itemsize
    )rm   r   r   r   r   rd  r=   r=   r>   array_nbytest
  s   rm  
contiguousc                 C   rh  r'  )r   r   r  	is_contigr   ra  r=   r=   r>   array_contiguous
     rp  c_contiguousc                 C   rh  r'  )r   r   r  is_c_contigr   ra  r=   r=   r>   array_c_contiguous
  rq  rt  f_contiguousc                 C   rh  r'  )r   r   r  is_f_contigr   ra  r=   r=   r>   array_f_contiguous
  rq  rw  r  c                 C   s    |  tj|j }t| |||S r'  )r   r   r  mutabler   ra  r=   r=   r>   array_readonly
  r  ry  ctypesc           	      C   sP   t |}|| ||}t|}| ||}|j|_|j|_| }t| |||S r'  )rm   r   ArrayCTypesr   r   r   r   r   )	rq   r7   r)  r   rx   r   actctinfor   r=   r=   r>   array_ctypes
  s   
r~  r   c                 C   s8   | j |||d}|j}||| tj}t| |||S Nr   )r   r   r  r   r   r   r   )rq   r7   r)  r   r}  r   r=   r=   r>   array_ctypes_data
  s   r  c                 C   s6   | j |||d}|j}||| |}t| |||S r  )r   r   r  r   r   )rq   r7   fromtytotyr{   r}  r   r=   r=   r>   array_ctypes_to_pointer
  s   r  c           
      C   sr   t ||||d}ttj|j}|||j}ttj	||tj}|j
|j|tj|g}||| ||}	|	S )a)  Helper to invoke the contiguous checker function on an array

    Args
    ----
    checker :
        ``numba.numpy_supports.is_contiguous``, or
        ``numba.numpy_supports.is_fortran``.
    context : target context
    builder : llvm ir builder
    aryty : numba type
    ary : llvm value
    r   )rm   r   r+  r   rd   rp   r   ro   r    bool_rY   r   r   r~  )
checkerrq   r7   r   r   tup_intpr   	check_sig
check_argsro  r=   r=   r>   _call_contiguous_check
  s   
r  r   c                 C   s>   |  |t|}||_| }| j||| t| |||S r'  )r   r   
ArrayFlagsr   r   r   r   r   )rq   r7   r)  r   flagsobjr   r=   r=   r>   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   )
rW   r  r   r  r   r   r   r   r  r   )rq   r7   r)  r   r  r   r{   r=   r=   r>   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 )Nr  r   r@   r  )rW   r  r   r  r   r   rd   r   r   r  r   )rq   r7   r)  r   r  r   r  r{   r=   r=   r>   array_flags_f_contiguous
  s   r  realc                 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 {})	ro   r   complex_domainarray_complex_attrnumber_domainr   rG   r   r3   )rq   r7   r)  r   r=   r=   r>   array_real_part
  s
   r  imagc                 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 {})ro   r   r  r  r  r    r	  _parse_empty_like_argsr  r	   memsetr   r   r   r   r   r   r   rG   r   r3   )rq   r7   r)  r   rP   r
  r   r   r=   r=   r>   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      r@   r  ro   r  )r   r   )ro   r   r  rG   r   rm   underlying_floatrp   rn   r   r3   r   r}  r  r   r  r   IntTyper	  r   r	   copy_structr   r   )rq   r7   r)  r   r  rx   r   fltysizeof_fltyr   
llfltptrtyr   resulttyr   replr=   r=   r>   r    s    r  conj	conjugatec                 C   r*  )Nc                 S   s
   t | S r'  )r!  r  r  r=   r=   r>   r   Q  rr  zarray_conj.<locals>.implr=   r  r   r=   r=   r>   
array_conjN     r  c                 C   r  r'  )rA  )rq   r7   dtypetydtypevalr=   r=   r>   
dtype_typeY  r  r  r3   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)rq   r7   rP   r   r  r   r   r=   r=   r>   static_getitem_number_clazzf  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  rt  r  )r   c                       g | ]	}  tj|qS r=   r   r   r   r  rq   r=   r>   r     r  z(array_record_getattr.<locals>.<listcomp>c                    r  r=   r  r  r  r=   r>   r     r  r   )rm   ro   r   r   RecordrG   typeofr   NestedArrayr	  rd   r   r   r	   r   r   r3   r   rY   r   rp   rn   r   r   r   r   r   )rq   r7   r)  r   r  rx   r   rectypero   r   restyrarytyraryconstoffset
newdataptrrY   r   datasizer   r=   r  r>   array_record_getattr~  sN   


r  c                 C   s0   |d }t |tstt| ||jd |d |S rx  )r   strrG   r  r   )rq   r7   rP   r   r   r=   r=   r>   array_record_getitem  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  r=   r  r   r   r  r=   r>   r     r  z"record_getattr.<locals>.<listcomp>c                    r  r=   r  r  r  r=   r>   r     r  Nr   r@   )sentry_record_alignmentr   r  r   r   r  rm   ro   rY   r   r	   get_record_memberrn   r   r^   r   r   r   r   r   rv   rw   )rq   r7   r)  r   r  r   elemtyr   r   ro   r  r  newdatar   dptrru   r=   r  r>   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.
    r@   r   Nrt   )r   r  r   r  r   r   r  r	   rl   r   r  r3   pointeememcpyr   r   r   rn   r~   rv   rz   )rq   r7   rP   r   r  r)  r4  rM  r{   r   r  
val_structr  r  r  ru   r=   r=   r>   record_setattr  s,   





r  c                 C   s4   |  |jd |d }|| ||jd |d |d S )3
    Record.__getitem__ redirects to getattr()
    r   r@   )get_getattrr   )rq   r7   rP   r   r   r=   r=   r>   record_static_getitem_str  s   r  c                 C   s^   |j d j}t|j d j}| |j|| }| |j d |}|| ||j d |d || S )r  r@   r   )r   r   r   r^  insert_const_stringr4   r  )rq   r7   rP   r   r   r^  ll_fieldr   r=   r=   r>   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)rq   r7   rP   r   rectyr  r4  recr   r{   getattr_sigr   r=   r=   r>   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  )rq   r7   rP   r   r  r  r4  r  r   r{   r  r^  r   r=   r=   r>   record_static_setitem_int'  s   
r  c                 C   s   |  |||S )zB
    Create a constant array (mechanism is target-dependent).
    )make_constant_array)rq   r7   r)  pyvalr=   r=   r>   constant_array9  s   r  c                 C   s0   t t d|j}|t| }t||S )zG
    Create a record constant as a stack-allocated array of bytes.
       )r   r|  r  rl  	bytearraytostringr	   r   )rq   r7   r)  r  ltyr{   r=   r=   r>   constant_recordA  s   r  c                 C   s"   t jt|t jd}| |||S )zM
    Create a constant array from bytes (mechanism is target-dependent).
    r2  )r!  r   r  uint8r  )rq   r7   r)  r  bufr=   r=   r>   constant_bytesK  s   r  c                 C   s0   |j \}}||krtjS dd }| ||||S )Nc                 S   s(   | j |j ko| j|jko| jj|jjkS r'  )rY   r   rz  r   rz  r  r=   r=   r>   array_is_impl]  s
   
zarray_is.<locals>.array_is_impl)r   r	   r  r~  )rq   r7   rP   r   atybtyr  r=   r=   r>   array_isW  s
   
r  __hash__c                 C   s   dd S )Nc                 S   rk  r'  r=   r  r=   r=   r>   r6  j  r7  zol_array_hash.<locals>.<lambda>r=   r  r=   r=   r>   ol_array_hashh  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)
flatitertyr=   r=   r>   make_array_flat_clsp     
r  c                 C   r  )zv
    Return the Structure representation of the given *nditerty* (an
    instance of types.NumpyNdEnumerateType).
    ndenumerater  nditertyr=   r=   r>   make_array_ndenumerate_clsx  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   rU  r   r	   
false_byter  r2   r\   r   r8   r   	if_likelyrc  	true_byterd  )rq   r7   rd   rY   r   end_flagloop_continue
loop_breakr   bbendrR  idxptrr   r   	in_boundsr=   r=   r>   _increment_indices  s.   

r  c                 C   s*   t ||j|j}t| ||j||| d S r'  )r	   r   rY   rd   r  )rq   r7   r  r  r   r  rY   r=   r=   r>   _increment_indices_array  s   r  c                    s   j jtjjrnd	G dd dt}G dd d| G fddd|G dd	 d	|G d
d d|G dd d|G  	f
dddt}|S )zq
    Return the Structure representation of the given *nditerty* (an
    instance of types.NumpyNdIterType).
    r@   c                   @   sF   e Zd ZdZdd Zdd Zejdd Zdd	 Z	d
d Z
dd ZdS )z$make_nditer_cls.<locals>.BaseSubIterzF
        Base class for sub-iterators of a nditer() instance.
        c                 S   s&   || _ || _|| _|| _|| | _d S r'  )nditermember_name	start_dimend_dimrd   )rN   r  r  r  r  r=   r=   r>   rT    s
   z-make_nditer_cls.<locals>.BaseSubIter.__init__c                 S   s   t | j| j| d S r'  )r   r  r  )rN   r_   r=   r=   r>   set_member_ptr     z3make_nditer_cls.<locals>.BaseSubIter.set_member_ptrc                 S   s   t | j| jS r'  )rZ  r  r  rF  r=   r=   r>   
member_ptr     z/make_nditer_cls.<locals>.BaseSubIter.member_ptrc                 S   rk  r'  r=   rN   rq   r7   r=   r=   r>   init_specific  rl  z2make_nditer_cls.<locals>.BaseSubIter.init_specificc                 S   rk  r'  r=   rN   rq   r7   logical_dimr=   r=   r>   r    rl  z2make_nditer_cls.<locals>.BaseSubIter.loop_continuec                 S   rk  r'  r=   r  r=   r=   r>   r    rl  z/make_nditer_cls.<locals>.BaseSubIter.loop_breakN)re   rf   rg   rO  rT  r  r  cached_propertyr	  r  r  r  r=   r=   r=   r>   BaseSubIter  s    
r  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )z$make_nditer_cls.<locals>.FlatSubIterz
        Sub-iterator walking a contiguous array in physical order, with
        support for broadcasting (the index is reset on the outer dimension).
        c                 S   s$   | tjd}| t|| d S r   )r   r   r   r  r	   r   )rN   rq   r7   r   r=   r=   r>   r    s   z2make_nditer_cls.<locals>.FlatSubIter.init_specificc                 S   s   | | j}||j|gS r'  )r8   r	  r  r   )rN   rq   r7   r   r  r  r   r=   r=   r>   compute_pointer  s   z4make_nditer_cls.<locals>.FlatSubIter.compute_pointerc                 S   s<   || j d kr|| j}t||}||| j d S d S rm  )rd   r8   r	  r	   r   r   )rN   rq   r7   r  r   r=   r=   r>   r    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 Nr   r@   )	r   r   r   r   r	  rd   r8   r	   r   )rN   rq   r7   r  r   r   r=   r=   r>   r    s   z/make_nditer_cls.<locals>.FlatSubIter.loop_breakN)re   rf   rg   rO  r  r  r  r  r=   r=   r=   r>   FlatSubIter  s    r  c                       s$   e Zd ZdZ fddZdd ZdS )z+make_nditer_cls.<locals>.TrivialFlatSubIterzx
        Sub-iterator walking a contiguous array in physical order,
        *without* support for broadcasting.
        c                    s    j rJ d S r'  )need_shaped_indexingr  r  r=   r>   r    r&  z9make_nditer_cls.<locals>.TrivialFlatSubIter.init_specificc                 S   s&   t |dksJ t |||j|S rm  )r   r  r   rN   rq   r7   r   r  r  r=   r=   r>   r    s   z;make_nditer_cls.<locals>.TrivialFlatSubIter.compute_pointerN)re   rf   rg   rO  r  r  r=   r  r=   r>   TrivialFlatSubIter  s    r  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   rd   r	   r  r  r=   r=   r>   r    s   z7make_nditer_cls.<locals>.IndexedSubIter.compute_pointerNre   rf   rg   rO  r  r=   r=   r=   r>   IndexedSubIter      r  c                   @   r  )z'make_nditer_cls.<locals>.ZeroDimSubIterz5
        Sub-iterator "walking" a 0-d array.
        c                 S   s   |j S r'  r  r  r=   r=   r>   r  	  r[  z7make_nditer_cls.<locals>.ZeroDimSubIter.compute_pointerNr  r=   r=   r=   r>   ZeroDimSubIter  r  r  c                   @   r  )z&make_nditer_cls.<locals>.ScalarSubIterz8
        Sub-iterator "walking" a scalar value.
        c                 S   s   |S r'  r=   r  r=   r=   r>   r    rl  z6make_nditer_cls.<locals>.ScalarSubIter.compute_pointerNr  r=   r=   r=   r>   ScalarSubIter  r  r  c                       s|   e Zd ZdZej fddZ	fddZfddZdd	 Z	d
d Z
	fddZdd Zdd ZdS )zmake_nditer_cls.<locals>.NdIterz
        .nditer() implementation.

        Note: 'F' layout means the shape is iterated in reverse logical order,
        so indices and shapes arrays have to be reversed as well.
        c                    sd   g }j r nd}tjD ]\}}|\}}}}d| }	|| }
||
| |	|| q|S )N)r  indexed0dscalarzindex%d)r  r:  r  r]   )rN   l	factoriesra   r  r  r  r  r  r  factory)r  r  r  r  r  r  r=   r>   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%dNr=   r@   c                 S   s>   t | }t|D ]}| | |t || |  krtdqd S )Nz2nditer(): operands could not be broadcast together)r   r2   r   )rY   
main_shaper:  ra   r=   r=   r>   check_shapeO  s   zBmake_nditer_cls.<locals>.NdIter.init_specific.<locals>.check_shaper  r  r  rZ  )&r   r   r   
make_tupler,  arraysr:  r   r   r	   r   r   _arrays_or_scalarsr+  rd   rY   r   r   r    noner~  r   r   r  r  r  r  r   rL   r3   r2   r\   r   r   r^   	exhaustedr$  r  )rN   rq   r7   arrtysr)  r   ra   r)  r  slotmain_shape_tyr&  main_nitemsr  r'  r  rP   r   shape_is_emptyr,  r   rR  r  subiter)r  rd   r  nshapesr=   r>   r  ,  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  r=   rx  r   r   r=   r=   r>   r     r  zEmake_nditer_cls.<locals>.NdIter.iternext_specific.<locals>.<listcomp>r@   r   )rU  r	   as_bool_bitr8   r,  if_unlikelyr   rc  r)  r   r*  r   _make_viewsr   r   r(  r   rY   r  r  partial_loop_continue_loop_breakrd  )rN   rq   r7   r   r  r,  r-  r)  r   viewsrY   r  r=   r>   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'  )r$  r  r  r  rN   rq   r7   rR  r  r=   r=   r>   r9    
   
z.make_nditer_cls.<locals>.NdIter._loop_continuec                 S   r=  r'  )r$  r  r  r  r>  r=   r=   r>   r:    r?  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 r=   r8   r	   r\   r  r7   r   r=   r>   r     s    z?make_nditer_cls.<locals>.NdIter._make_views.<locals>.<listcomp>r  r  c                 s   s    | ]}|V  qd S r'  r=   r4  r=   r=   r>   r*    s    z>make_nditer_cls.<locals>.NdIter._make_views.<locals>.<genexpr>)r  r$  r   r   r   r1  r   r2   r   r  r  
_make_viewr-  )rN   rq   r7   r   r-  r)  r;  r  r$  rettysr  r2  r  array_indicessub_indicesra   )r  narraysr  r3  rB  r>   r7    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   r=   N)r   )r   r   r   rd   r  rm   rs   r(  r+  r   r   )rN   rq   r7   r   r  r  r  r2  r_   viewr   rY   r   r=   r=   r>   rC    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   r%  )r:  r   r   r   r   r]   rm   rZ  )	rN   rq   r7   r-  r)  r!  ra   r  r  r=   r=   r>   r*    s   
z2make_nditer_cls.<locals>.NdIter._arrays_or_scalarsN)re   rf   rg   rO  r  r  r$  r  r<  r9  r:  r7  rC  r*  r=   
r  r  r  r  r  r  rG  rd   r  r3  r=   r>   NdIter  s    O*rJ  )rd   r  r   r)  r  objectr	   rl   )r  r  rJ  r=   rI  r>   make_nditer_cls  s   
 
, YrL  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   rZ  r  )r   r   r   r	   rL   r3   r   r  r2   r\   r   r  r6  r  r   r,  r^   rY   )rN   rq   r7   r   r   r   r,  rR  r  r  dim_is_emptyrd   r=   r>   r    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 r=   )r8   r	   r\   r   r   rR  r7   rN   r=   r>   r     s    

zKmake_ndindex_cls.<locals>.NdIndexIter.iternext_specific.<locals>.<listcomp>T)r   r   r   rU  r	   r5  r8   r,  r6  r   rc  r2   rB   r   r^   r3   r   rY   r  r   rd  )
rN   rq   r7   r   r   r  r,  r   r8   rY   rO  rQ  r>   r<    s,   





z7make_ndindex_cls.<locals>.NdIndexIter.iternext_specificN)re   rf   rg   rO  r  r<  r=   rO  r=   r>   NdIndexIter  s    rR  )rd   r	   rl   )r  rR  r=   rO  r>   make_ndindex_cls  s   7rS  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  rZ  )r   r   r   r	   r   r   r   r  rL   r3   rd   r2   r\   r   r   )	rN   rq   r7   r  r  r   r   rR  r  r  r=   r>   r  ;  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              	      r@  r=   rA  rP  rB  r=   r>   r   d      

z\_make_flattening_iter_cls.<locals>.CContiguousFlatIter.iternext_specific.<locals>.<listcomp>)rd   r   r8   r   r   r   r	   r  r  r   ry   r   r   r2   r^   make_anonymous_structr  r   r   )rN   rq   r7   r  r  r   rd   r   r   r   r_   r   idxvalsidxtuplerT  rB  r>   r<  T  s4   
"zH_make_flattening_iter_cls.<locals>.CContiguousFlatIter.iternext_specificc                 S   s   | |j|g}t||||S r'  )r  r   ry   rN   rq   r7   r  r  r   r_   r=   r=   r>   rN  r  s   z>_make_flattening_iter_cls.<locals>.CContiguousFlatIter.getitemc                 S   s$   | |j|g}t||||| d S r'  )r  r   r|   rN   rq   r7   r  r  r   r   r_   r=   r=   r>   setitemv  s   z>_make_flattening_iter_cls.<locals>.CContiguousFlatIter.setitemN)re   rf   rg   rO  r  r<  rN  r[  r=   rT  r=   r>   CContiguousFlatIter6  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 rM  )r   r   r   r   rd   r	   r   rY   rL   r3   r   r  r2   r\   r   r  r6  r  r   pointersr,  )rN   rq   r7   r  r  r   r   rd   r   r   r]  r,  rR  r  ptrptrr  rN  r=   r=   r>   r    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  FTr@   r  c              	      r@  r=   rA  rP  rB  r=   r>   r     rU  zQ_make_flattening_iter_cls.<locals>.FlatIter.iternext_specific.<locals>.<listcomp>r   )rd   r	   r   rY   r   r   r]  r   r   r   rU  r5  r8   r,  r6  r   rc  r\   ry   r   r2   r^   rV  r  r   r   r  r   r   r  rd  )rN   rq   r7   r  r  r   rd   r   r   r]  r   r  is_exhaustedlast_ptrr_   r   rW  rX  rR  r  r   r   r  r   r^  	inner_dimrT  rB  r>   r<    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   )rd   r	   r   rY   r   r  r2   r]   uremudivreverser  r   r  )rN   rq   r7   r  r  r   rd   r   r   r   rR  r_   r=   r=   r>   _ptr_for_index  s   z:_make_flattening_iter_cls.<locals>.FlatIter._ptr_for_indexc                 S   s    |  |||||}t||||S r'  )re  ry   rY  r=   r=   r>   rN    s   z3_make_flattening_iter_cls.<locals>.FlatIter.getitemc                 S   s&   |  |||||}t||||| d S r'  )re  r|   rZ  r=   r=   r>   r[     s   z3_make_flattening_iter_cls.<locals>.FlatIter.setitemN)	re   rf   rg   rO  r  r<  re  rN  r[  r=   rT  r=   r>   FlatIter}  s    !Erf  )rW   r  r	   rl   )r  r  rW   r\  rf  r=   rT  r>   r  0  s   
D r  r  c                 C   sh   t t|}|| |}||_| |}|| ||dd}|| ||| | }t| |t||S Nr   )rO   )	r  r   NumpyFlatTyper   rm   r[   r  r   r   )rq   r7   r  r  flatiterclsflatiterarrclsr   r=   r=   r>   make_array_flatiter  s   

rl  c                 C   Z   |j \}|\}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
| d S r  )r   r  rW   rm   r   r<  )rq   r7   rP   r   r   r  rj  ri  r  rk  r  r=   r=   r>   iternext_numpy_flatiter     
rn  c                 C   sj   |j d }|\}}t|}|| ||d}|j}| |}	|	| ||jd}
|| |||
|}t| ||j|S Nr   r   )r   r  rW   rm   r   rN  r   r   )rq   r7   rP   r   r  rj  r   ri  r  rk  r  r   r=   r=   r>   iternext_numpy_getitem'  s   

rq  c                 C   sf   |j d }|\}}}t|}|| ||d}|j}	| |	}
|
| ||jd}|| ||	||| |  S rp  )r   r  rW   rm   r   r[  rA  )rq   r7   rP   r   r  rj  r   r   ri  r  rk  r  r=   r=   r>   iternext_numpy_getitem_any7  s   


rr  c           	      C   sF   |j d }t|}|| ||d d}| |j}|| ||jd}|jS rp  )r   r  rm   rW   r   r   )	rq   r7   rP   r   r  ri  rj  rk  r  r=   r=   r>   iternext_numpy_getitem_flatH  s   
rs  c           
      C   sr   |j \}|\}tt|}|| |}||_| |}|| ||dd}|| ||| | }	t	| ||j
|	S rg  )r   r  r   NumpyNdEnumerateTyper   rm   r[   r  r   r   r   )
rq   r7   rP   r   r  r  	nditerclsr  rk  r   r=   r=   r>   make_array_ndenumerateS  s   

rv  c                 C   rm  r  )r   r  rW   rm   r   r<  )rq   r7   rP   r   r   r  r  ru  r  rk  r  r=   r=   r>   iternext_numpy_nditere  ro  rw  c                    s^    fddt |j|D }ttt|}| }| | | }t |j	|S )zndindex(*shape)c                    rb  r=   rc  )r   argtyr;  r   r=   r>   r   y  rd  z&make_array_ndindex.<locals>.<listcomp>)
r   r   rS  r   NumpyNdIndexTyper   r  r   r   r   )rq   r7   rP   r   rY   ru  r  r   r=   r   r>   make_array_ndindexu  s   

rz  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 r=   rc  r  r7   rq   r   r=   r>   r         z,make_array_ndindex_tuple.<locals>.<listcomp>)r   rd   r   ro   r	   r   rS  r   ry  r   r  r   r   )
rq   r7   rP   r   rd   r/  rY   ru  r  r   r=   r{  r>   make_array_ndindex_tuple  s   
r}  c                 C   s6   |j \}|\}t|}|| ||d}|| || d S r  )r   rS  r<  )rq   r7   rP   r   r   r  r  ru  r=   r=   r>   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   r1  r	   r   rL  r  r   r   )	rq   r7   rP   r   r  r-  r)  r  r   r=   r=   r>   make_array_nditer  s   
r  c                 C   s2   |j \}|\}t|| ||d}|| || d S r  )r   rL  r<  )rq   r7   rP   r   r   r  r  r=   r=   r>   iternext_numpy_nditer2  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.
    r@   r   r=   r   Nr  r  z3Don't know how to allocate array with layout '{0}'.Fr<  )z[array is too big; `arr.size * arr.dtype.itemsize` is larger than the maximum possible size.)r)  rw  )+rm   rn   ro   r   r   r   rs   r   r   r  smul_with_overflowr7  r  rd   r  r  r]   r   r   rG   r   r   r>  r?  r   get_preferred_array_alignmentuint32rA  MemInfoPointervoidptrTypeRefr3   r    r~  _call_allocatorr   meminfo_datar   r	   r^   r   r  r}  )rq   r7   r
  r   r   r   datatyper   arrlenoverflowr   arrlen_multr   dimension_sizeallocsize_multr  ro   	align_valru   r   miparytypeclassargtypesr   r   r   shape_arraystrides_arrayr=   r=   r>   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  ru   r=   r=   r>   r     rr  z _ol_array_allocate.<locals>.implr=   )r  r  ru   r   r=   r=   r>   _ol_array_allocate  s   r  c                 C   s   |  ||S )z9Trampoline to call the intrinsic used for allocation
    )r  )r
  r   ru   r=   r=   r>   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)rq   r7   r    r   r  ru   r   r=   r=   r>   r  +  s   zintrin_alloc.<locals>.codegen)r   r  r  r    )r  r  ru   r  r  rP   r=   r=   r>   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 maintainedr@   >Fr<  )z#Cannot safely convert value to intpN)r   r   r   rA   r   r  r   sextr   r   r>  r?  r   trunc)
rq   r7   src_tr  r   
intp_widthintp_irmaxvalr   	is_largerr=   r=   r>   safecast_intp9  s(   



z#_parse_shape.<locals>.safecast_intpr@   r   r   r   )znegative dimensions not allowedN)r   r   r   r~   r   r1  r   r	   r   r]   r3   get_constant_genericr2   r   r6  r>  r?  r   )rq   r7   r)  r{   r  rd   passed_shapesr   r   r   rR  is_negr=   r=   r>   _parse_shape5  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  )rq   r7   rP   r   arrshapetypearrshaper
  r=   r=   r>   _parse_empty_argse  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   r=   )
r   r   r   r   rm   r	   r   rY   rd   r   )rq   r7   rP   r   arytyper   r   r=   r=   r>   r  o  s   


r  c                 C   s&   t |tjrd|  d}t|d S )NzIf np.z0 dtype is a string it must be a string constant.)r   r   UnicodeTyper   r   )fnamero   r   r=   r=   r>   _check_const_str_dtype}  s   
r  c                 C   "   |j }||||}dd }||fS )Nc                 S   (   t | |||\}}t| |||}| S r'  )r  r  r   rF  r7   rP   rG  r
  r   r   r=   r=   r>   r       znumpy_empty_nd.<locals>.codegeninstance_type)rI  ty_shapety_dtypety_retty_refty_rettyrP   r  r=   r=   r>   numpy_empty_nd     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|)	Nr5  r   rt  c                       t | | S r'  )r  )rY   ro   r  r=   r>   r     rn  zol_np_empty.<locals>.implz.Cannot parse input types to function np.empty(, ))r  floatr   r   Function
typing_keyr   doublety_parse_dtypety_parse_shaper   r   r   )rY   ro   nb_dtyperd   r   r   r=   r  r>   ol_np_empty  s"   


r  c                 C   r  )Nc                 S   r  r'  )r  r  r   r  r=   r=   r>   r    r  z$numpy_empty_like_nd.<locals>.codegenr  )rI  ty_prototyper  r  r  rP   r  r=   r=   r>   numpy_empty_like_nd  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)ro   r  r  r   z3Cannot parse input types to function np.empty_like(r  r  c                    r  r'  )r  )r  ro   r  r=   r>   r     rn  zol_np_empty_like.<locals>.implr'  )r  r   r  r   r   r   ro   r  r	  r   r   )r  ro   r  r  r   r   r=   r  r>   ol_np_empty_like  s&   


r  c                 C   rE  )Nc                 S   s<   t |jd | ||d }t||j||j|jd d S r   )rm   r   r	   r  r   r   r   r   )rF  r7   rP   rG  r   r=   r=   r>   r    s   z(_zero_fill_array_method.<locals>.codegen)r   r+  )rI  rN   rP   r  r=   r=   r>   _zero_fill_array_method  s   
r  
_zero_fillc                 C   r*  )z?Adds a `._zero_fill` method to zero fill an array using memset.c                 S   s   t |  d S r'  )r  rF  r=   r=   r>   r     rn  z ol_array_zero_fill.<locals>.implr=   )rN   r   r=   r=   r>   ol_array_zero_fill  r  r  c                 C   s   t d| tfdd}|S )Nzerosc                 S      t j| |d}|  |S Nr2  )r!  r5  r  )rY   ro   r  r=   r=   r>   r        zol_np_zeros.<locals>.impl)r  r  rY   ro   r   r=   r=   r>   ol_np_zeros  s   
r  c                 C      t d| ddd}|S )N
zeros_likec                 S   r  r  )r!  r  r  )rz  ro   r  r=   r=   r>   r     r  zol_np_zeros_like.<locals>.implr'  r  rV  r=   r=   r>   ol_np_zeros_like  s   

r  c                 C   r  )N	ones_likec                 S   2   t j| |d}|j}tt|D ]}d||< q|S Nr2  r@   r!  r  r  r2   r   )rz  ro   r  arr_flatr   r=   r=   r>   r     
   
zol_np_ones_like.<locals>.implr'  r  rV  r=   r=   r>   ol_np_ones_like  s   

r  c                    s2   t d| t|st| n| d fdd	}|S )Nfullc                    s0   t |  }|j}tt|D ]}|||< q|S r'  r!  r5  r  r2   r   )rY   
fill_valuero   r  r  r   r  r=   r>   r  
  
   
zimpl_np_full.<locals>.fullr'  )r  r   r  )rY   r  ro   r  r=   r  r>   impl_np_full  s   

r  c                 C   s   t d| ddd}|S )Nr  c                 S   s0   t | |}|j}tt|D ]}|||< q|S r'  r  )rz  r  ro   r  r  r   r=   r=   r>   r    r  z$impl_np_full_like.<locals>.full_liker'  r  )rz  r  ro   r  r=   r=   r>   impl_np_full_like  s   

r  c                 C   r  )Nonesc                 S   r  r  r  )rY   ro   r  r  r   r=   r=   r>   r   '  r  zol_np_ones.<locals>.implr'  r  r  r=   r=   r>   
ol_np_ones!  s   

r  c                    s4   t d| t|st| ntj d fdd	}|S )Nidentityc                    s.   t | | f }t| D ]}d|||f< q|S rm  )r!  r  r2   )r:  ro   r  ra   r  r=   r>   r  8  s   z"impl_np_identity.<locals>.identityr'  )r  r   r  r   r  )r:  ro   r  r=   r  r>   impl_np_identity0  s   

r  c                 C   rk  r'  r=   NMr=   r=   r>   _eye_none_handler@  rl  r  c                 C   s$   t |tjrdd }|S dd }|S )Nc                 S      | S r'  r=   r  r=   r=   r>   r   G  rl  z$_eye_none_handler_impl.<locals>.implc                 S      |S r'  r=   r  r=   r=   r>   r   J  rl  r   r   r  )r  r  r   r=   r=   r>   _eye_none_handler_implD  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 )Nro   r   c                    s   t | |}t| |f }|dkr)t| || }t|D ]
}d|||| f< q|S t| | |}t|D ]
}d||| |f< q4|S r  )r  r!  r  r  r2   )r  r  r   ro   _Mr  dra   rK  r=   r>   r   Z  s   
znumpy_eye.<locals>.impl)
r   r   r  r!  ro   r  	DTypeSpecrI  r   rZ  )r  r  r   ro   r   r=   r  r>   	numpy_eyeO  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 )Nz#The argument "v" must be array-like)r@   r{  zInput must be 1- or 2-d.r   c                 S   s*  | j dk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 rx  )
rd   rY   rn  r!  r  ro   r2   r?  r  r5  )r   r   r   r:  r  ra   rowscolsr=   r=   r>   	diag_implr  s2   

zimpl_np_diag.<locals>.diag_implrE  )r   r   r   r   r   r   rd   NumbaTypeError)r   r   r  r=   r=   r>   impl_np_diagi  s   



r  takec                    s   t | tjrt |tjrdd }|S tdd | |fD r*|jdk  fdd}|S t | tjr?t |tjtjfrAdd }|S d S d S )Nc                 S   s.   || j d ks|| j  k rtd|  | S )Nr@   Index out of bounds)r   
IndexErrorr  )rz  r   r=   r=   r>   	take_impl  s   znumpy_take.<locals>.take_implc                 s   r&  r'  r(  r   r;  r=   r=   r>   r*    r+  znumpy_take.<locals>.<genexpr>r  c                    s   t j|j| jd} r| }n|}t |}d}|  }|D ]}|| jd ks.|| j k r2td|| ||< |d }q||j	S Nr2  r   r@   r  )
r!  r5  r   ro   r	  r  r  r  r  rY   )rz  r   r  walkeritra   r  r  F_orderr=   r>   r    s   


c                 S   s~   t |}t j|j| jd}t |}d}|  }|D ]}|| jd ks*|| j k r.td|| ||< |d }q||j	S r  )
r!  r   r5  r   ro   r  r  r  r  rY   )rz  r   convertr  r  ra   r  r  r=   r=   r>   r    s   


)r   r   r   r   r-  r  r>  r1  )rz  r   r  r=   r  r>   
numpy_take  s   
r  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 r=   r  ry  r=   r=   r>   r     r  z!_arange_dtype.<locals>.<listcomp>c                 s   r&  r'  r   r   Complexry  r=   r=   r>   r*    r+  z _arange_dtype.<locals>.<genexpr>c                 s   r&  r'  )r   r   Floatry  r=   r=   r>   r*    r+  zint%sr  c                 S   s   g | ]}t |qS r=   )r   	unliteralr   r  r=   r=   r>   r         )
r,  r   
complex128r   rZ  r!  ro   r   r   r?  )r   boundsro   NPY_TYunliteral_boundsr=   r=   r>   _arange_dtype  s   r  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'  r=   r  r
  r=   r=   r>   r         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 )Nr@   r   zMaximum allowed size exceededT)r   r   mathceilr  r  r?  r  r!  r5  r2   )r  r  r  ro   	lit_startlit_stoplit_step_step_start_stopnitems_cnitems_rnitems_ir   r  r{   ra   start_value
step_value
stop_value
true_dtypeuse_complexr=   r>   r     s(   

znp_arange.<locals>.implr  )r   r   Optionalr3   r+  rI  r  r  r  ro   r,  rZ  )r  r  r  ro   r   r=   r  r>   	np_arange  s>   r$  c                    sn   t dd | |fD sd S t|ttjfsd}t|tdd | |fD r+tj ntj	 d fdd	}|S )	Nc                 s   r&  r'  )r   r   rI  r  r=   r=   r>   r*     r+  z!numpy_linspace.<locals>.<genexpr>z%The argument "num" must be an integerc                 s   r&  r'  r  r  r=   r=   r>   r*  '  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   r@   r  )r!  r5  divider2   )r  r  r  r  divdeltar  ra   r2  r=   r>   r  -  s    z numpy_linspace.<locals>.linspacer  )
r-  r   r   r   r   r   r   r,  r  r   )r  r  r  r   r  r=   r2  r>   numpy_linspace  s   
r(  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  r@   rt   N)r   rm   r	   r   rY   r   r  r   r  
raw_memcpyr   r   r   r   r   r   r  r  r   r8   r   r   )rq   r7   rP   r   r  r   r   rettyper  r  rU  r  rT  r   r   r  r  r=   r=   r>   _array_copyF  s6   


	r+  c                 C   s@   t |tjsJ |jdkrdnd}|j|dd}||}|tfS )Nr  r   F)r  r  )r   r   r   r  r	  r+  )r  rz  r  r  rP   r=   r=   r>   _array_copy_intrinsick  s
   r,  z
array.copyc                 C   r  r'  )r+  rl  r=   r=   r>   
array_copyt  r
  r-  c                 C   s   t | tjr
dd }|S )Nc                 S      t | S r'  )r,  r  r=   r=   r>   
numpy_copy|  r]  z#impl_numpy_copy.<locals>.numpy_copyr(  )rz  r/  r=   r=   r>   impl_numpy_copyy  s   r0  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 layoutr@   r   r8  r  r  r   N)r   r   r  rd   rm   r  r   r+  r   r(  r   r   r   r   r   r   r   r   r   r  r  blockr+  phir3   add_incoming)rq   r7   rP   r   output_layoutr  r   r   r  rY   r   
check_funcro  thenorelseout_thenthen_blk
out_orelse
orelse_blkret_phir=   r=   r>   _as_layout_array  sf   


r=  c                    sH   t  tjst |j jt|jdd}|| }| fddfS )Nr@   )r  rd   c                    s   t | ||| jdS )Nr4  )r=  r   )r}  r  r   rz  r>  r=   r>   r6    s    z,_as_layout_array_intrinsic.<locals>.<lambda>)	r   r   r!   r   RequireLiteralValuer	  r   r?  rd   )r  rz  r4  r  rP   r=   r>  r>   _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  r=   r=   r>   r     r  z%array_ascontiguousarray.<locals>.implc                 S   r  Nr   r@  r  r=   r=   r>   r     rr  r   r   r   r   r   rI  r  r   r(  r=   r=   r>   array_ascontiguousarray  s   
rF  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   rA  r'  )r!  asfortranarrayr   r  r=   r=   r>   r     r  z"array_asfortranarray.<locals>.implc                 S   r  )Nr  rD  r  r=   r=   r>   r     rr  rE  r(  r=   r=   r>   array_asfortranarray  s   
rH  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 rp  )r   rm   r	   r   rY   r   r  r   r   r   r   r   r  r  r  ry   r~   ro   r|   r   r   )rq   r7   rP   r   r  r   r   r*  r  r  rU  r  rT  r   r   r  r  itemr=   r=   r>   array_astype  s0   


rJ  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   rm   r   rs   r   r   r3   r   r   sremr	   is_not_nullr   r>  r?  r   r^   sdivr  r   r   r   r   r   r   r   r   )rq   r7   rP   r   buftyr   r  
out_ary_tyout_aryout_datamodelr   ll_itemsizerl  remis_incompatibler   rY   r   r   r   r=   r=   r>   r    s<   


znp_frombuffer.<locals>.codegenr  )r  bufferro   r  r)  rP   r  r=   r=   r>   np_frombuffer
  s   $rV  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 r@   )ro   rd   r  r  z3Cannot parse input types to function np.frombuffer(r  r  c                    r  r'  )rV  )rU  ro   r  r=   r>   r   M  rn  z impl_np_frombuffer.<locals>.impl)r  r   r   r   r  r   r   r  r  r  r   r  r  r   rx  )rU  ro   r   r  r   r=   r  r>   impl_np_frombuffer6  s2   





rX  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'  r=   r_   rY   ro   intrinsic_cfarrayr=   r>   r   W  rr  zimpl_carray.<locals>.implc                    rZ  r'  r=   r[  r\  r=   r>   r   ]  rr  r'  r   get_cfarray_intrinsicr   r   r  r_   rY   ro   r   r=   r\  r>   impl_carrayR     

ra  c                    rY  )Nr  c                    rZ  r'  r=   r[  r\  r=   r>   r   g  rr  zimpl_farray.<locals>.implc                    rZ  r'  r=   r[  r\  r=   r>   r   m  rr  r'  r^  r`  r=   r\  r>   impl_farrayb  rb  rc  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   CPointerro   r   r  r  r  r   r    
np_cfarray)	r  r_   rY   	ptr_dtyper   ro   rd   r  rP   dtype_r  r=   r>   r]  s  s4   





z0get_cfarray_intrinsic.<locals>.intrinsic_cfarrayr%   )r  ri  r]  r=   rh  r>   r_  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(...).
    Nr{  r  c                    rb  r=   rc  )r   r  r   r   r=   r>   r     rd  znp_cfarray.<locals>.<listcomp>r  rw  )r   r   r  rm   rs   r	   r   r   r   r1  r   r   r]   r   r  rd  r  rn   ro   r}  r   r   r   )rq   r7   rP   r   r  rg  r_   rY   r   rP  r   rR  r   offr   r   r   r   r=   r   r>   rf    sJ   




	rf  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   r1  r   r   r   rR  rF   r    )rq   r7   seqtyseqr_  r=   r=   r>   _get_seq_size  s   rn  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)r7   r   r  rq   rO  r  r=   r>   wrap  s   
z$_get_borrowing_getitem.<locals>.wrap)ro   rF   rM  rN  r    r   r   )rq   rl  rq  r=   rp  r>   _get_borrowing_getitem  s   rr  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   r1  r   r7  rr  ro   )rl  rm  rO  r7   rq   r   r=   r>   get_first_item  s   
z.compute_sequence_shape.<locals>.get_first_item)r   r   r   r   r2   r]   rn  r   )rq   r7   rd   rl  rm  r   ru  r   innertyinnerra   r=   rt  r>   compute_sequence_shape  s   

rx  c                    s0   fdd  fdd||| dS )z?
    Check the nested sequence matches the given *shapes*.
    c                      s   j  td d S )N)zincompatible sequence shape)r>  r?  r   r=   r   r=   r>   _fail  s   
z#check_sequence_shape.<locals>._failc                    s<  t |dkrd S t| |}|d }d||}j|dd    W 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<  r@   )r   rn  r   r   r   r   rR  rr  r	   rz  ro   r   r1  r2   r7  )rl  rm  r   r   expectedr  rO  r|  rv  rw  ra   ry  r7   check_seq_sizerq   r=   r>   r|    s0   
"z,check_sequence_shape.<locals>.check_seq_sizeNr=   )rq   r7   rl  rm  r   r=   r{  r>   check_sequence_shape  s   r}  c                    s:    fddfdd||d dS )zl
    Assign a nested sequence contents to an array.  The shape must match
    the sequence's structure.
    c              
      sB   t j j| dd}|| j}t || d S r  )r	   r  r  r~   ro   r|   )r   r4  r{   r_   )r  r7   rq   r   r   r   r=   r>   assign_item3  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 | r  )r   r   r   rR  r1  rr  r	   rz  ro   r   r2   r7  r   r   )rl  rm  r   r   r   rO  r|  rv  rw  ra   r   )assignr~  r7   rq   r=   r>   r  9  s(   
 "z(assign_sequence_to_array.<locals>.assignr=   Nr=   )rq   r7   r   r   r   r  rl  rm  r=   )r  r  r~  r7   rq   r   r   r   r>   assign_sequence_to_array,  s   r  c                 C   s>   t | |\}}t|r|}n
t|}|d u rd S t||dS rC  )r/   r   r  r   r   )r  rK  ro   rd   	seq_dtyper=   r=   r>   np_array_typerU  s   r  c                 C   s0   t d| t| ||}|||}dd }||fS )Nr   c           
   	   S   s   |j }|j}|jd }|d }t| ||||}t||ksJ t| |||| t| |||}	t| ||	j||	j	||| t
| ||j |	 S r   )r   rd   r   rx  r   r}  r  r  r   r   r   r   )
rq   r7   rP   r   r  rd   rl  rm  r   r  r=   r=   r>   r  f  s   


znp_array.<locals>.codegen)r  r  )r  objro   r  rP   r  r=   r=   r>   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  )rK  ro   r=   r=   r>   r     rr  zimpl_np_array.<locals>.implr'  )r  r   r   r   r   r  )rK  ro   r   r   r=   r=   r>   impl_np_arrayz  s   



r  c           
      C   s   | d}| |}|d||}||||||}||d|||d||}|j|dd d| }	| j|t|	f W d    |S 1 sLw   Y  |S )Nr   r   rb  Fr<  z%s(): axis out of bounds)	r3   r   r  rq  r  r   r>  r?  r  )
rq   r7   r   rd   r   r   ll_ndimis_neg_axisaxis_out_of_boundsr   r=   r=   r>   _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).
    r@   rb  r   r   r   r|  r	   r   rL   r2   r   r  rq  r   r\   r   r8   )rq   r7   
orig_shaperd   r   ll_shtyr   r  rR  ll_dim
after_axisr  r   r=   r=   r>   _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.
    r@   r   rb  r  )rq   r7   orig_stridesrd   r   r  r   r  r   rR  r  r  r   r=   r=   r>   _insert_axis_in_strides  s"   




r  c              	   C   s   |j }|j}|jd }t|| ||d d}t|| |}	t||j}
t||j}t| ||
||}t	| ||||}t
|	|j|||j|j|jd |	 S )z/
    np.expand_dims() with the given axis.
    r   r   r   )r   rd   r   rm   r	   r   rY   r   r  r  r   r   r   r   r   r   )rq   r7   rP   r   r   r  rd   r  r  r  r   r   
new_shapesnew_stridesr=   r=   r>   expand_dims  s$   
r  c                 C   sB   |j dkr|jnd}|j|j d |d}|||}dd }||fS )Nr@   r  r  c                 S   sP   |  ||d |jd tj}t| |d|jj|}t| ||||}t| ||j|S )Nr@   znp.expand_dims)	r~   r   r   r   r  r   rd   r  r   )rq   r7   rP   r   r   r  r=   r=   r>   r    s   znp_expand_dims.<locals>.codegen)rd   r  r	  )r  rz  r   r  r  rP   r  r=   r=   r>   np_expand_dims  s
   
r  c                 C   sL   t | tjsd|  }t|t |tjs d| }t|dd }|S )Nz)First argument "a" must be an array. Got z(Argument "axis" must be an integer. Got c                 S   r  r'  )r  )rz  r   r=   r=   r>   r     rr  z!impl_np_expand_dims.<locals>.impl)r   r   r   r   r   r   )rz  r   r   r   r=   r=   r>   impl_np_expand_dims  s   



r  c                    s   t  fddfddS )Nc                    sX   | fdd|D  fdd}t tdkr tnd tj||fS )Nc                    s    g | ]}|j t|j d qS )rO  )r	  r?  rd   r  )minimumr=   r>   r     r,  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 r=   r=   )r   r  r  r  r7   rq   	transformr=   r>   r     s    z>_atleast_nd.<locals>.impl.<locals>.codegen.<locals>.<listcomp>r@   )_atleast_nd_transformr	   r   r   r   r(  r   r   )rq   r7   rP   r   arrsretsr  )r-  r  r  rD  r  r>   r    s   

z*_atleast_nd.<locals>.impl.<locals>.codegenr@   r   )r    r   r   r,  StarArgTuple
from_types)typingcontextr   r  )r  r  )r-  rD  r>   r     s   
z_atleast_nd.<locals>.implc                     s    |  S r'  r=   r5  )r   r=   r>   r6  (  s    z_atleast_nd.<locals>.<lambda>rj  )r  r  r=   )r  r   r  r>   _atleast_nd  s   r  c                    s"   t  ksJ  fdd}|S )z`
    Return a callback successively inserting 1-sized dimensions at the
    following axes.
    c           	         sb   t D ]*}|d }|j|k r.t | }|j|jd d}t| |t|||f|}|}q|S )Nr@   rO  )r2   rd   r	   r   r	  r  r   r    )	rq   r7   r  r  r  ra   rd   r   newarrtyr  min_ndimr=   r>   r  2  s   
z(_atleast_nd_transform.<locals>.transform)r   )r  r  r  r=   r  r>   r  +  s   r  c                  G   s"   t dd | D rtddgS d S )Nc                 s   r&  r'  r(  r  r=   r=   r>   r*  D  r+  z np_atleast_1d.<locals>.<genexpr>r@   r   r-  r  r5  r=   r=   r>   np_atleast_1dB  s   r  c                  G   s$   t dd | D rtdddgS d S )Nc                 s   r&  r'  r(  r  r=   r=   r>   r*  J  r+  z np_atleast_2d.<locals>.<genexpr>r{  r   r  r5  r=   r=   r>   np_atleast_2dH     r  c                  G   s$   t dd | D rtdg dS d S )Nc                 s   r&  r'  r(  r  r=   r=   r>   r*  P  r+  z np_atleast_3d.<locals>.<genexpr>r  )r   r   r{  r  r5  r=   r=   r>   np_atleast_3dN  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   r3   r   r  rq  r]   r   r  r  r  ry   r~   ro   r|   r   )rq   r7   r   r-  r  
arr_shapesarr_stridesr  
ret_shapesr   r  ret_stridescopy_offsetsarr_shr   rR  r   r  is_axisaddendret_datar  r  arr_starr_datar  r   r  r{   r  r=   r=   r>   _do_concatenateT  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   ru  r   r  rz  r   r=   r>   r     rd  z#_np_concatenate.<locals>.<listcomp>np.concatenatec                       g | ]	}t  |jqS r=   )r	   r   rY   r   r  r  r=   r>   r     r  c                    r  r=   r	   r   r   r  r  r=   r>   r     r  c                    s   g | ]}t  |qS r=   )r	   r   r  r  r=   r>   r     r  r   r  c                    s   g | ]}|  qS r=   r=   r  )rR  r=   r>   r     r  r@   Fr<  z<np.concatenate(): input sizes over dimension %d do not matchc                    s   g | ]}  |qS r=   )r8   r  r  r=   r>   r     r  )rd   r   r  r2   r   r3   r8   r  r  r  rq  r   r	   r  r  r   r  r>  r?  r   r  r   r   )rq   r7   r-  r  r  r   rd   r  r  r  r  ret_shape_ptrret_shother_shapeson_axison_other_dimr  is_okr  r=   )r7   rq   rR  r>   _np_concatenate  s`   

r  c                    s  |j }td}td}tt|}	 fddt||D }t d||}t |d j}
|dd  D ]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   r@   c                    r  r  ru  r  r   r=   r>   r     rd  z_np_stack.<locals>.<listcomp>np.stackr  Fr<  )z5np.stack(): all input arrays must have the same shapec                    r  r=   r  r  r  r=   r>   r     r  rb  c                    rp  r=   rq  r  )r7   r  r=   r>   r     r  c                    s   g | ]}t   |qS r=   )r	   r   r8   )r   r  r  r=   r>   r     r|  )rd   r	   r   r   r   r  r   rY   r  r  r   r   r  r>  r?  r   r   r|  rL   r2   r  rq  r   r\   r8   r  r   r   )rq   r7   r-  r  r  r   rd   r   r  
ll_narraysr  r  r  r  orig_shr  input_shapesr  rR  r  r  r   input_stridesra   r  r=   )r7   rq   r  r>   	_np_stack  s   








r  c                 C   sN   |d urt |tjsd S t| d|\}}|dkrtdt|}t|||S )Nr  r   z.zero-dimensional arrays cannot be concatenated)r   r   r   r0   	TypeErrorr1   r   )r  r)  r   ro   rd   r  r=   r=   r>   np_concatenate_typer  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 rx  )	r~   r   r   r   r  r   r	   r   r   rq   r7   rP   r   r   r=   r=   r>   r  3  s   znp_concatenate.<locals>.codegen)r  r   r   r   )r  r)  r   r  rP   r  r=   r=   r>   np_concatenate-  s
   
r  c                 C      t | tjrddd}|S d S )Nr   c                 S   r  r'  )r  r)  r   r=   r=   r>   r   A  rr  z!impl_np_concatenate.<locals>.implrE  r   r   r1  r)  r   r   r=   r=   r>   impl_np_concatenate>     
r  c                 C   s*   |D ]}|j dk s|j dkrtdqdS )Nr@   r{  z7np.column_stack() is only defined on 1-d and 2-d arrays)rd   r  )rq   r   r)  rz  r=   r=   r>   _column_stack_dimsF  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   r@   r{  rO  )r   r   r	   r   r   r   r   r   rd   r]   r	  r   r    r  r  r   )rq   r7   rP   r   orig_arrtys	orig_arrsr-  r  r   r  r  r  
expand_signewarrr=   r=   r>   r  X  s$   



z np_column_stack.<locals>.codegen)r0   r  r1   r   r   )r  r/  ro   rd   r  r  rP   r  r=   r=   r>   np_column_stackO  s   
r  c                 C      t | tjrdd }|S d S )Nc                 S   r.  r'  )r  r/  r=   r=   r>   r   x  r]  zimpl_column_stack.<locals>.implr  r/  r   r=   r=   r>   impl_column_stacku     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  r=   r=   r>   _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 )r  N)r  ry  r=   r=   r>   r*    s    z"np_stack_common.<locals>.<genexpr>r  r   r@   c                 S   s,   |  ||d |jd tj}t| ||||S rm  )r~   r   r   r   r  r  r=   r=   r>   r    s   z np_stack_common.<locals>.codegen)r0   r-  r   r   )	r  r)  r   ro   rd   r  r  rP   r  r=   r=   r>   np_stack_common  s   
r  c                 C   r  )Nr   c                 S   r  r'  )r  r  r=   r=   r>   r     rr  zimpl_np_stack.<locals>.implrE  r  r  r=   r=   r>   impl_np_stack  r  r  c                 C   s4   t | ||\}}t||}t|}t|||}|S r'  )r0   r?  r1   r   r   )r  r   r)  ndim_minro   rd   r  r  r=   r=   r>   NdStack_typer  s
   
r  c                 C   &   t | d|d}||}dd }||fS )Nz	np.hstackr@   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   r@   c                    s   t j|  dS r  r  r)  r  r=   r>   np_hstack_impl  r&  z3_np_hstack.<locals>.codegen.<locals>.np_hstack_implr   rd   r   r   r   r  r~  )rq   r7   rP   r   r.  rd   r  r=   r  r>   r    s   

z_np_hstack.<locals>.codegenr  r  r/  r  rP   r  r=   r=   r>   
_np_hstack     r  c                 C   r  )Nc                 S   r.  r'  )r  r  r=   r=   r>   r     r]  zimpl_np_hstack.<locals>.implr  r  r=   r=   r>   impl_np_hstack  r  r  c                 C   r  )Nz	np.vstackr{  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 rm  )r!  r  hstackr  r=   r=   r>   np_vstack_impl  r  z3_np_vstack.<locals>.codegen.<locals>.np_vstack_implr@   c                 S      t j| ddS )Nr   r  r  r  r=   r=   r>   r    r&  r  )rq   r7   rP   r   r.  rd   r  r   r=   r=   r>   r    s   


z_np_vstack.<locals>.codegenr  r  r=   r=   r>   
_np_vstack  r  r  c                 C   r  )Nc                 S   r.  r'  )r  r  r=   r=   r>   r     r]  zimpl_np_vstack.<locals>.implr  r  r=   r=   r>   impl_np_vstack  r  r  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 )Nr@   r  )r!  r  r  r  r=   r=   r>   r    r  z3_np_dstack.<locals>.codegen.<locals>.np_vstack_implr@   rO  r{  c                 S   r  )Nr{  r  r  r  r=   r=   r>   r    r&  )r   r   rd   r~  r   r   r   r	  r   r    r  r  )rq   r7   rP   r   r.  r  rd   r  r   stack_retty	stack_sig	stack_retr  r=   r=   r>   r    s*   


z_np_dstack.<locals>.codegenr  r  r=   r=   r>   
_np_dstack  s   "r  c                 C   r  )Nc                 S   r.  r'  )r  r  r=   r=   r>   r     r]  zimpl_np_dstack.<locals>.implr  r  r=   r=   r>   impl_np_dstack  r  r  fillc                 C   r  )Nc                 S   s   || d d < d S r'  r=   )r  r{   r=   r=   r>   	fill_impl$  s   zarr_fill.<locals>.fill_implr=   )r  r{   r  r=   r=   r>   arr_fill!  s   r  dotc                 C   r  )Nc                 S   rA  r'  )r!  r  )r  otherr=   r=   r>   dot_impl-  rn  zarray_dot.<locals>.dot_implr=   )r  r  r  r=   r=   r>   	array_dot+  rD  r  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 )Nr{  r  r  .r!  r"  rd   r   rz  r  r=   r=   r>   r   9  s   

znp_flip_lr.<locals>.implr'  r(  r=   r=   r>   
np_flip_lr3     r  c                 C   r   )NzCannot np.flipud on %s typec                 S   s.   t | }|jdk rtd|d d ddf S )Nr@   zInput must be >= 1-d.r  .r  r  r=   r=   r>   r   J  s   

znp_flip_ud.<locals>.implr'  r(  r=   r=   r>   
np_flip_udD  r  r  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` re  c                    sZ   dd }t jg}tjg|R  }| t j}| }| |g}	| ||||	}
|
S )Nc                 S   s*   |}t | D ]}t||td d d}q|S )Nr  )r2   r*   r  )lengthempty_tupler   ra   r=   r=   r>   r   `  s   z6_build_flip_slice_tuple.<locals>.codegen.<locals>.impl)r   r   r   r    r   get_constant_undefr~  )rq   r7   r    r   r   inner_argtypes	inner_sigll_idx_typer  
inner_argsr   r   
tuple_typer=   r>   r  _  s   

z(_build_flip_slice_tuple.<locals>.codegen)	r   r   r(  r   r?  r   r   r+  slice3_type)rI  szrP   r  r=   r  r>   _build_flip_slice_tupleU  s   

r  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'  )r  rd   )rz  slr=   r=   r>   r   z  s   
znp_flip.<locals>.impl)r   r   r   r   r   r(  r=   r=   r>   np_flips  s   r  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   r=   r=   r>   r     s
   znp_array_split.<locals>.implc                 S   sP   t | j| |\}}tt|d g| |g|| d   }tj| ||dS )Nr@   r  )divmodrY   r!  cumsumr   r  )r   r  r   r!  rS  r   r=   r=   r>   r     s   c              	   S   sp   t | j}tdd| j|}g }d}|D ]}t||t||}|| |  |}q|| t||t|d   |S Nznp.splitr   r   )r+   rd   r   r*   r  r]   r   r  r   	slice_tupr   prevr  r   r=   r=   r>   r     s   
c                 s   r&  r'  r   r   r   )r   tr=   r=   r>   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+   rd   r   r   r*   r  r]   r  r=   r=   r>   r     s   
rE  )r   r   r+  ListTyper>  r   IterableTypeiterator_typer   r,  r-  r   r  r   r   r=   r=   r>   np_array_split  s,   





r$  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   r  r  )r!  splitr"  r  r=   r=   r>   r     s   znp_split.<locals>.implc                 S   s4   t | j| |\}}|dkrtdtj| ||dS )Nr   z0array split does not result in an equal divisionr  )r  rY   r   r!  r  )r   r  r   r  rS  r=   r=   r>   r     s   r  rE  )r   r   r+  r   r>  r   r$  r#  r=   r=   r>   np_split  s   


r&  c                 C   s   | |k pt |ot |  S r'  )r!  isnanr  r=   r=   r>   	lt_floats  s   r(  Fc                 C   s   | ||f}zt | W S  ty?   | dkr%tj|rtnd|dd}|j}n| dkr7tj|r/tnd|d}|j}|t |< | Y S w )z6
    Get a sort implementation of the given kind.
    r'   NT)lt
is_argsortis_np_arrayr(   )r)  r*  )	_sortsKeyErrorr'   make_jit_quicksortr(  run_quicksortr(   make_jit_mergesortrun_mergesort)r  is_floatr*  keyr-  funcr=   r=   r>   get_sort_func  s(   



r5  z
array.sortc                    s<   |j d }tdt|jtjd  fdd}| ||||S )Nr   r'   )r  r2  c                    s    |  d S r'  r=   r  	sort_funcr=   r>   array_sort_impl  r  z#array_sort.<locals>.array_sort_impl)r   r5  r   ro   r   r  r~  )rq   r7   rP   r   r  r8  r=   r6  r>   
array_sort   s   
r9  c                 C   r&  )NzArgument "a" must be array-likec                 S   s   |   }|  |S r'  )r	  r-  )rz  r   r=   r=   r>   np_sort_impl  s   z"impl_np_sort.<locals>.np_sort_implr'  )rz  r:  r=   r=   r>   impl_np_sort  s   
r;  zarray.argsortc           	         sb   |j \}}t|jt|jtjdd  fdd}|j|j d d d}|d d }| ||||S )NT)r  r2  r*  c                    s    | S r'  r=   r  r6  r=   r>   array_argsort_impl"  r]  z)array_argsort.<locals>.array_argsort_implr@   r5  )	r   r5  r   r   ro   r   r  replacer~  )	rq   r7   rP   r   r  r  r<  innersig	innerargsr=   r6  r>   array_argsort  s   
r@  c                 C   s   |j |j ks|jdksJ |S )Nr  )rx  r  )rq   r7   r  r  r{   r=   r=   r>   array_to_array.  s   rA  c                 C   4   dd }t ||}| ||||g}t| ||j|S )Nc                 S   s
   |  dS r   )r  r  r=   r=   r>   r   =  s   
zarray0d_to_scalar.<locals>.impl)r    r~  r   r   rq   r7   r  r  r{   r   rP   r   r=   r=   r>   array0d_to_scalar5  s   
rD  c                 C   rB  )Nc                 S   s   t | d S rp  )r  r  r=   r=   r>   r   H  rn  z array_to_unichrseq.<locals>.impl)r    r~  r   r   rC  r=   r=   r>   array_to_unichrseqF  s   
rE  c                 C   rE  )zR
    An intrinsic returning a derived array with the given shape and strides.
    r   rz  rY   r   r=   r=   r>   reshape_uncheckedS  rJ  rG  c                    s   dd   fdd}|S )Nc                 S   s   t | tjotdd | D S )Nc                 s   r&  r'  r  r4  r=   r=   r>   r*  ^  r+  z>type_reshape_unchecked.<locals>.check_shape.<locals>.<genexpr>)r   r   r1  r-  rY   r=   r=   r>   r'  \  s   z+type_reshape_unchecked.<locals>.check_shapec                    sJ   t | tjsd S  |r |sd S t|t|krd S | jt|ddS )Nr  r  )r   r   r   r   r	  rF  r'  r=   r>   typer`  s   z%type_reshape_unchecked.<locals>.typerr=   )rq   rJ  r=   rI  r>   type_reshape_uncheckedZ  s   	rK  c                 C   s   |j d }|j}t|| ||d }t|| |}t||d }t||d }	t||j||	|j|jd |	 }
t
| |||
S )Nr   r@   r{  rw  )r   r   rm   r	   r   r   r   r   r   r   r   )rq   r7   rP   r   r   r  r   r   rY   r   r   r=   r=   r>   impl_shape_uncheckedl  s   
rL  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   rY  r'  rH  r  rY   r=   r=   r>   rK    r   zas_strided.<locals>.get_shapec                 S   r  r'  r=   rM  r=   r=   r>   rK       z*as_strided() strides argument is mandatoryc                 S   r  r'  r=   )r  r   r=   r=   r>   get_strides  rN  zas_strided.<locals>.get_stridesc                    s   t |  | || |} | S r'  )rG  )r  rY   r   rK  rO  r=   r>   as_strided_impl  s   z#as_strided.<locals>.as_strided_implrs  )r   r+  r"   rG   )r  rY   r   rQ  r=   rP  r>   
as_strided  s   

rR  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 )Nr@   c                 S   s   | fS r'  r=   window_shaper=   r=   r>   get_window_shape  r   z-sliding_window_view.<locals>.get_window_shapec                 S   r  r'  r=   rS  r=   r=   r>   rU    rN  z4window_shape must be an integer or tuple of integersc                 S   s   t t|S r'  )r   r2   rT  r   rd   r=   r=   r>   get_axis  r  z%sliding_window_view.<locals>.get_axisc                 S   s   t dd||gS )Nsliding_window_viewr   r   rV  r=   r=   r>   rW    s   c                    s    fdd|D S )Nc                    s   g | ]	}t d d |qS )rX  r   rY  ry  rO  r=   r>   r     r  z9sliding_window_view.<locals>.get_axis.<locals>.<listcomp>r=   rV  r=   rO  r>   rW    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 shaper@   )	rd   r   r   r2   r*   rY   r   r   rG  )
r  rT  r   	out_shapeout_stridesra   r  rR  trimmedrH  rW  rU  shape_bufferstride_bufferr=   r>   sliding_window_view_impl  s8   
z5sliding_window_view.<locals>.sliding_window_view_implr'  )r   r   r   r   r2   rd   r"   r+  ro   r   r   r   r   )r  rT  r   r`  r=   r]  r>   rX    s>   'rX  c                 C   r  )Nc                 S   s2   | j dkrdS | j dkrt| dS d}t|)Nr   Fr@   z[The truth value of an array with more than one element is ambiguous. Use a.any() or a.all())r   r2  r  r   )r  r   r=   r=   r>   r     s   

zol_bool.<locals>.implr(  r  r=   r=   r>   ol_bool  s   	ra  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 integerr  c                    s`   t dd|}t dd|}|dk r|7 }|dk r|7 }t ||}t|||}t| |S )Nznp.swapaxesaxis1axis2r   )r   r*   r!  r  )r  rb  rc  
axes_tuple	axes_listrd   r=   r>   r     s   znumpy_swapaxes.<locals>.impl)
r   r   r   r   r   r   r   rd   r   r2   )r  rb  rc  r   r=   re  r>   numpy_swapaxes  s   


rg  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   r@   z*`arr` and `indices` dimensions don't matchr   )r   rd   r   rY   r:  r   r   r*   r!  r%  r   r2   r5  ro   r  r  )r  r   r   Ni_origNk_origindices_broadcast_shape	arr_shapera   d1d2new_valarr_broadcast_shapeNiNkJr   np_s_iikka_1d
indices_1dout_1djr=   r=   r>   _take_along_axis_impl-  sT   

rz  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 )Nr  z.The second argument "indices" must be an arrayz'The indices array must contain integersr@   z;`indices` and `arr` must have the same number of dimensionsc                    s   t |  |ddd S )Nr   r=   )rz  r  r  r   r   )rj  r=   r>   take_along_axis_impl~  s   z1arr_take_along_axis.<locals>.take_along_axis_implr   zaxis must be a literal valuer   zaxis is out of boundsc                    s   t | || S r'  )rz  r{  rp  rq  rj  r=   r>   r|    s   )r   r   r   r   r   ro   r   r   rd   r   r2   r   r(  r  r   )r  r   r   arr_ndimr|  r=   r}  r>   arr_take_along_axisi  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'  r=   r  r	  nanr=   r=   r>   r     rl  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!  r'  isneginffinfor3   r  isposinfr?  r  r=   r=   r>   r     s   


c                 S   s*   t j| j|d}t j| j|d}t||S )N)r  )r!  
nan_to_numr  r  complex)r  r	  r  r1  r}  r=   r=   r>   r     s   
z4Only Integer, Float, and Complex values are acceptedc                 S   r  r'  r=   r  r=   r=   r>   r     rl  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  ro   r  r?  r"  r	  r  r2   r   r'  r  r  )	r  r	  r  min_infmax_infx_outputoutput_flatra   r=   r=   r>   r     s   


c                 S   sD   t | }|rt |n|}t j|jd|d t j|jd|d |S )NF)r	  r  )r!  r"  r	  r  r  r  )r  r	  r  r  r  r=   r=   r>   r     s
   
z4The first argument must be a scalar or an array-likeTr  )
r   r   rI  r   r  r  r   r   r   ro   )r  r	  r  r   r=   r=   r>   nan_to_num_impl  s6   
;
7
.
"


r  r'  r  r8  r  rE  )Frs  r  (  rO  r  r  rM  llvmliter   llvmlite.irr   numpyr!  numbar   r   
numba.corer   r   r   r	   r
   numba.np.numpy_supportr   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+   r,   numba.core.typing.npydeclr-   r  r.   r  r/   r0   r1   r?   rB   rm   rs   ry   r|   r   r   r   r   r   r   r   r   r   r   ArrayIteratorBORROWEDr  r  r   r#  rN  r   r  r%  r1  r0  r[  Anyr6  r   r:  r   r@  rB  rK  rD  rQ  rj  rs  rw  r  r  r-  r  r  r  r  r  r  r  r  r  r  r  r   r$  r%  r/  r4  broadcast_shapesrD  r  rL  r3  rj  rm  ro  r  VarArgr  r  r  rk  logspacer  rot90r  r  r  r  r  r  r  r]   r  r  r  r  r  r  r  r  r
  r  r%  rY   r)  uniquer1  r@  r?  r4  rC  rJ  rK  rW  r  r`  rb  
MemoryViewre  rg  ri  rj  rk  rm  rp  rt  rw  ry  r~  r{  r  re  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  r  r  rL  rS  r  rl  rh  rn  rq  rr  rs  r  rv  rt  rw  r  rz  r}  ry  r~  r  r  NumpyNdIterTyper  r  r  r  r  r  r  r  r  r  r5  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  eyer  diagr  r  r  r  aranger$  r  r(  r+  r,  r-  r	  r0  r=  r@  rB  rF  rG  rH  rJ  rV  rW  rX  ra  rc  r_  rf  rn  rr  rx  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  r  r  r  vstackr  r  dstackr  r  r  r  r  r  r  r  flipr  r  r$  r%  r&  r,  r(  r5  r9  r-  r;  argsortr@  rA  UnicodeCharSeqr  r  r  NPTimedelta
NPDatetimerD  rE  rG  type_callablerK  rL  libstride_tricksrR  rX  r2  ra  r  rg  rz  take_along_axisr  r  r  r=   r=   r=   r>   <module>   s   $4<	E



D


(



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

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


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


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

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