o
    ôjÈž  ã                   @   sB  d dl Z d dlZd dl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 d dlmZ d dlmZmZ d dlmZ d dlmZ d d	lmZmZ d d
l m!Z! d dlm"Z" d dl#m$Z$ d dl%m&Z& d dl'm(Z( d dl)m*Z* G dd„ dƒZ+dd„ Z,G dd„ deƒZ-G dd„ dej.ej/eƒZ0e!j1 2e0¡ dS )é    N)ÚjitÚtypeof)ÚcgutilsÚtypesÚ	serializeÚsigutilsÚerrors)Ú	is_jittedÚoverload_attributeÚoverload_methodÚregister_jitableÚ	intrinsic)Únpydecl)ÚAbstractTemplateÚ	signature)Útuple_setitem)Ú	_internal)Ú	UfuncBaseÚUfuncLowererBase)Úarray_analysis)Úufuncbuilder)Únumpy_support)ÚCallable)Úir)Úglobal_compiler_lockc                   @   sV   e Zd Zddd„Zd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 )ÚUfuncAtIteratorNc                 C   s.   || _ || _|| _|| _|| _|| _|| _d S ©N)ÚufuncÚaÚa_tyÚindicesÚ
indices_tyÚbÚb_ty)Úselfr   r   r   r    r!   r"   r#   © r%   úh/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/numba/np/ufunc/dufunc.pyÚ__init__   s   
zUfuncAtIterator.__init__c                 C   s2   |   ||¡ | j ¡ }|  |||¡ | j ¡  d S r   )Ú_prepareÚindexerZbegin_loopsÚ_call_ufuncZ	end_loops)r$   ÚcontextÚbuilderÚloop_indicesr%   r%   r&   Úrun$   s   
zUfuncAtIterator.runc                 C   s   t | jtjƒS r   )Ú
isinstancer!   r   Ú	BaseTuple©r$   r%   r%   r&   Úneed_advanced_indexing*   s   z&UfuncAtIterator.need_advanced_indexingc                 C   sD  ddl m}m}m}m} | j| j}}| j| j}	}
| 	t
j¡dƒ}| jd ur.t ||¡| _|  ¡ rItj||t|
ƒd�}|
j
}|||||ƒ\}}n|f}|
f}|||||ƒ\}}g }t|ƒD ]\}}|| }t|t
jƒr}||ƒ|||ƒ}| ||||f¡ q^|r‡||||ƒ}nd}||||	||||ƒ| _| j ¡  |  ||¡| _d S )Nr   )Únormalize_indicesÚFancyIndexerÚ
make_arrayÚget_subspace_shape)Úcountr%   )Únumba.np.arrayobjr3   r4   r5   r6   r   r    r   r!   Úget_value_typer   Úintpr"   r   Úalloca_once_valueÚb_indicer2   Úunpack_tupleÚlenÚ	enumerater/   ÚArrayÚappendr)   ÚprepareÚ_compile_ufuncÚcres)r$   r+   r,   r3   r4   r5   r6   r   r    r   r!   ÚzeroZindex_typesZarray_indicesÚiÚidxtyÚidxZidx_makeZsubspace_shape_tupler%   r%   r&   r(   -   sF   
ÿ
ÿÿ€
ÿ
ÿ
zUfuncAtIterator._preparec              	   C   s\   ddl m} t ||j¡}t ||j¡}|j}	t |||	|||j|¡}
|||||
ƒ}|
|fS )Nr   )Ú	load_item)	r8   rI   r   r=   ÚshapeÚstridesÚdataÚget_item_pointer2Úlayout)r$   r+   r,   r-   ÚarrayÚarray_tyrI   ÚshapesrK   rL   ÚptrÚvalr%   r%   r&   Ú	_load_valV   s   ÿzUfuncAtIterator._load_valc                 C   sd   |  |¡}| |tj¡}| tj|¡}||||fƒ}	| tj¡dƒ}
| ||
¡}| 	||¡ d |	fS )Né   )
ÚloadÚdtyper   r:   Úget_functionÚoperatorÚgetitemr9   ÚaddÚstore)r$   r+   r,   r    rO   rP   rH   ÚsigÚimplrS   Úoner%   r%   r&   Ú
_load_flata   s   
zUfuncAtIterator._load_flatc           
      C   s@   ddl m} | jjj}|j}	| ||||	¡}||||||ƒ d S )Nr   )Ú
store_item)r8   ra   rD   r   Úreturn_typerW   Úcast)
r$   r+   r,   rO   rP   rR   rS   ra   ÚfromtyÚtotyr%   r%   r&   Ú
_store_valn   s
   
zUfuncAtIterator._store_valc                 C   sN   | j jd }| jd u r| jjf}n| jj| jjf}| |¡}| |jf¡ |S ©Nr   )	r   Úkeyr"   r   rW   r#   r[   Úadd_linking_libsÚlibrary)r$   r+   r,   r   r]   rD   r%   r%   r&   rC   u   s   

zUfuncAtIterator._compile_ufuncc                 C   sœ   | j }| j| j}}|  |||||¡\}}| jd u r|f}	n| j| j| j}
}}|  ||||
|¡\}}||f}	| ||j	|j
|	¡}|  ||||||¡ d S r   )rD   r   r   rT   r"   r#   r<   r`   Úcall_internalÚfndescr   rf   )r$   r+   r,   r-   rD   r   r   rR   rS   Úargsr"   r#   Zb_idxÚ_Zval_bÚresr%   r%   r&   r*   �   s   
ÿzUfuncAtIterator._call_ufunc©NN)Ú__name__Ú
__module__Ú__qualname__r'   r.   r2   r(   rT   r`   rf   rC   r*   r%   r%   r%   r&   r      s    
	)r   c                    s8   ddl m} G ‡ fdd„d|jƒ}| jˆ jj7  _|S )Nr   ©Únpyimplc                       s$   e Zd ZdZ”Z‡ fdd„Z‡  ZS )z(make_dufunc_kernel.<locals>.DUFuncKernelzÉ
        npyimpl._Kernel subclass responsible for lowering a DUFunc kernel
        (element-wise function) inside a broadcast loop (which is
        generated by npyimpl.numpy_ufunc_kernel()).
        c                    s*   t ƒ  |||¡ | j |j¡\| _| _d S r   )Úsuperr'   ÚdufuncÚfind_ewise_functionrm   Ú	inner_sigrD   )r$   r+   r,   Ú	outer_sig©Ú	__class__r%   r&   r'   ž   s   ÿz1make_dufunc_kernel.<locals>.DUFuncKernel.__init__)rq   rr   rs   Ú__doc__rw   r'   Ú__classcell__r%   ©Ú_dufuncr{   r&   ÚDUFuncKernel–   s    r�   )Únumba.npru   Z_Kernelrq   r   )r€   ru   r�   r%   r   r&   Úmake_dufunc_kernel“   s   rƒ   c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚDUFuncLowererzHCallable class responsible for lowering calls to a specific DUFunc.
    c                    s"   ddl m} tƒ  |t|j¡ d S )Nr   rt   )r‚   ru   rv   r'   rƒ   Znumpy_ufunc_kernel)r$   rw   ru   r{   r%   r&   r'   ª   s
   þzDUFuncLowerer.__init__)rq   rr   rs   r}   r'   r~   r%   r%   r{   r&   r„   §   s    r„   c                       sB  e Zd ZdZedƒZd>dd„Z‡ fdd„Zd	d
„ Ze	dd„ ƒZ
dd„ Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZdd „ Zd!d"„ Z‡ fd#d$„Zd%d&„ Zed?d'd(„ƒZd)d*„ Zd@d,d-„Zd@d.d/„Zd@d0d1„Zd@d2d3„Zd@d4d5„Z d?‡ fd6d7„	Z!d?d8d9„Z"d:d;„ Z#d<d=„ Z$‡  Z%S )AÚDUFuncz¹
    Dynamic universal function (DUFunc) intended to act like a normal
    Numpy ufunc, but capable of call-time (just-in-time) compilation
    of fast loops specialized to inputs.
    )ÚidentityÚ
_keepaliveÚninÚnoutNFc                 C   sv   |d u ri }t |ƒr|j}t ¡ � tdd|dœ|¤Ž|ƒ}W d   ƒ n1 s(w   Y  |  ||¡ t | |¡ d S )NÚnpyufunc)Ú_targetÚcacher%   )r	   Úpy_funcr   Ú3_suppress_deprecation_warning_nopython_not_suppliedr   Ú_initializeÚ	functoolsÚupdate_wrapper)r$   r�   r†   rŒ   ÚtargetoptionsÚ
dispatcherr%   r%   r&   r'   »   s   
ÿþþÿzDUFunc.__init__c                    s\   t  |¡}tt| ƒj||d� |tjk| _|jj	| _	|jj
| _
t| ƒ| _|  ¡  |  ¡  d S )N)r†   )r   Úparse_identityrv   r…   r'   r   ÚPyUFunc_NoneÚreorderabler�   rq   r}   r„   Ú	_lower_meZ_install_cgÚ_install_type)r$   r“   r†   r{   r%   r&   r�   Ç   s   



zDUFunc._initializec                 C   s&   t | jj ¡ ƒ}t| j| j| j|d�S )ú4
        NOTE: part of ReduceMixin protocol
        )r“   r†   ÚfrozenÚsiglist)ÚlistÚ_dispatcherÚ	overloadsÚkeysÚdictr†   Ú_frozen)r$   r›   r%   r%   r&   Ú_reduce_statesÓ   s   üzDUFunc._reduce_statesc                 C   s<   t j | ¡}| ||¡ |D ]}| |¡ q|r| ¡  |S )r™   )r   Ú_DUFuncÚ__new__r�   r[   Údisable_compile)Úclsr“   r†   rš   r›   r$   r]   r%   r%   r&   Ú_rebuildß   s   zDUFunc._rebuildc                 C   s   | S )zK
        For compatibility with the various *UFuncBuilder classes.
        r%   r1   r%   r%   r&   Úbuild_ufuncí   s   zDUFunc.build_ufuncc                 C   ó   | j jS r   )r�   r’   r1   r%   r%   r&   r’   ó   ó   zDUFunc.targetoptionsc                 C   r©   r   )r   rˆ   r1   r%   r%   r&   rˆ   ÷   rª   z
DUFunc.ninc                 C   r©   r   )r   r‰   r1   r%   r%   r&   r‰   û   rª   zDUFunc.noutc                 C   r©   r   )r   Únargsr1   r%   r%   r&   r«   ÿ   rª   zDUFunc.nargsc                 C   r©   r   )r   Úntypesr1   r%   r%   r&   r¬     rª   zDUFunc.ntypesc                 C   r©   r   )r   r   r1   r%   r%   r&   r     rª   zDUFunc.typesc                 C   r©   r   )r   r†   r1   r%   r%   r&   r†     rª   zDUFunc.identityc                 C   r©   r   )r   r   r1   r%   r%   r&   r     rª   zDUFunc.signaturec                 C   s   t | jjƒdks
J ‚d| _dS )zI
        Disable the compilation of new signatures at call time.
        r   TN)r>   r�   rž   r¡   r1   r%   r%   r&   r¥     s   
zDUFunc.disable_compilec                 C   s   t  |¡\}}|  ||¡S )z=
        Compile the DUFunc for the given signature.
        )r   Únormalize_signatureÚ_compile_for_argtys)r$   r]   rm   rb   r%   r%   r&   r[     s   z
DUFunc.addc                    sp   t jjj}|t| ¡ ƒ D ]!}tt|ƒd|ƒ|ur.|j| dg|¢R i |¤Ž}|tur.|  S qt	ƒ j
|i |¤ŽS )z}
        Allow any argument that has overridden __array_ufunc__ (NEP-18)
        to take control of DUFunc.__call__.
        Ú__array_ufunc__Ú__call__)r   ÚnpÚndarrayr¯   ÚtupleÚvaluesÚgetattrÚtypeÚNotImplementedrv   r°   )r$   rm   ÚkwsÚdefaultÚargÚoutputr{   r%   r&   r°   "  s   
€zDUFunc.__call__c           	      O   sÌ   | j j}|r&d|v r| d¡}||f7 }|r&tdd dd„ t|ƒD ƒ¡ ƒ‚t|ƒ}||ks8||| j j ks8J ‚|r<J ‚g }|d |… D ]}t|ƒ}t	|t
jƒrT|j}nt |¡}| |¡ qD|  t|ƒ¡S )NÚoutz)unexpected keyword arguments to ufunc: %sz, c                 s   ó   � | ]}t |ƒV  qd S r   )Úrepr)Ú.0Úkr%   r%   r&   Ú	<genexpr>9  ó   € z+DUFunc._compile_for_args.<locals>.<genexpr>)r   rˆ   ÚpopÚ	TypeErrorÚjoinÚsortedr>   r‰   r   r/   r   r@   rW   r   Úmap_arrayscalar_typerA   r®   r³   )	r$   rm   r¸   rˆ   r¼   Zargs_lenÚargtysrº   Úargtyr%   r%   r&   Ú_compile_for_args1  s(   

ÿ

zDUFunc._compile_for_argsc                 C   sê   | j r
td| f ƒ‚t|tƒsJ ‚|du r|}n||Ž }| jj ¡ D ]\}}||jkr<d|› d�}t 	|t
j¡ |  S q"t | j| j|¡\}}}t |||¡}t ||¡\}}	}
|  t|	ƒ|¡ | j |	|j|
f¡ | jj |j¡ |S )a/  
        Given a tuple of argument types (these should be the array
        dtypes, and not the array types themselves), compile the
        element-wise function for those inputs, generate a UFunc loop
        wrapper, and register the loop with the Numpy ufunc object for
        this DUFunc.
        zcompilation disabled for %sNz>Compilation requested for previously compiled argument types (z~). This has no effect and perhaps indicates a bug in the calling code (compiling a ufunc more than once for the same signature)r¡   ÚRuntimeErrorr/   r³   r�   rž   Úitemsrm   ÚwarningsÚwarnr   ÚNumbaWarningr   Ú_compile_element_wise_functionr’   Ú_finalize_ufunc_signatureÚ!_build_element_wise_ufunc_wrapperÚ	_add_loopÚintr‡   rA   rj   r—   Úlibs)r$   rÈ   rb   r]   rÀ   rD   ÚmsgZ
actual_sigÚ	dtypenumsrR   Úenvr%   r%   r&   r®   P  s6   	
ÿú

ÿÿ
ÿzDUFunc._compile_for_argtysc                 C   s
   |j |kS r   )rm   )r$   Úewise_typesr]   r%   r%   r&   Úmatch_signatureu  s   
zDUFunc.match_signatureÚreturnc                 C   sD   dt dtfdd„}t |¡}d}|D ]}||ƒ}t||ƒ|ƒ qd S )NÚattrrÛ   c                    s   ‡ fdd„}|S )Nc                    s   t | jd ˆƒ‰ ‡ fdd„S )Nr   c                    s   ˆ S r   r%   ©r   ©rS   r%   r&   Ú<lambda>~  s    zUDUFunc._install_ufunc_attributes.<locals>.get_attr_fn.<locals>.impl.<locals>.<lambda>)rµ   rh   rÝ   ©rÜ   rÞ   r&   r^   |  s   zCDUFunc._install_ufunc_attributes.<locals>.get_attr_fn.<locals>.implr%   )rÜ   r^   r%   rà   r&   Úget_attr_fnz  s   z5DUFunc._install_ufunc_attributes.<locals>.get_attr_fn)rˆ   r‰   r«   r†   r   )Ústrr   r   ÚFunctionr
   )r$   Útemplaterá   ÚatÚ
attributesrÜ   Zattr_fnr%   r%   r&   Ú_install_ufunc_attributesx  s   
þz DUFunc._install_ufunc_attributesc                 C   s"   |   |¡ |  |¡ |  |¡ d S r   )Ú_install_ufunc_reduceÚ_install_ufunc_reduceatÚ_install_ufunc_at)r$   rä   r%   r%   r&   Ú_install_ufunc_methods�  s   

zDUFunc._install_ufunc_methodsc                    s&   t  |¡}t|dƒd‡ fdd„	ƒ}d S )Nrå   c                    s¾  t jdtjd� t|tjƒsd}t |¡‚t|tjƒ}t|tjƒ}t|tj	ƒ}t|tj
ƒ}|p2|p2| }	|o;t|ƒdk}
t|tjtjtjtj	fƒ}t |¡‰|pQˆ }t|gƒ}ˆjj}|dkrjt |¡rjt d¡‚|dkrzt |¡szd}t |¡‚t |¡r‡ˆ |jf¡ n|r’ˆ |j|f¡ n	ˆ |j|jf¡ ‡fd	d
„‰t‡fdd„ƒ‰ t‡fdd„ƒ‰d‡fdd„	}d‡ ‡‡fdd„	}ddd„}d‡fdd„	}|rÑ|S |
r×|r×|S |	rÝ|rÝ|S |S )Nz ufunc.at feature is experimental©Úcategoryz)The first argument "a" must be array-liker   é   zsecond operand needed for ufuncrU   z+second operand provided when ufunc is unaryc                    sŠ   ddl m} t|ƒdkr|j\}}}}|\}}	}
}n|jd \}}}}|d \}}	}
}||ƒ| ||	ƒ}	tˆ |	||
|||ƒ}| | |¡ d S )Nr   )r5   é   r   )r8   r5   r>   rm   r   r.   )r+   r,   r]   rm   r5   rn   ÚatyrG   Úbtyr   r    r"   Zat_iterrÝ   r%   r&   Úapply_ufunc_codegen¼  s   zDDUFunc._install_ufunc_at.<locals>.ol_at.<locals>.apply_ufunc_codegenc                    s   t  ||||¡}|ˆ fS r   ©r   Únone)Ú	typingctxr   r   r    r"   r]   ©rò   r%   r&   Úapply_a_b_ufuncÊ  s   z@DUFunc._install_ufunc_at.<locals>.ol_at.<locals>.apply_a_b_ufuncc                    s   t  |||¡}|ˆ fS r   ró   )rõ   r   r   r    r]   rö   r%   r&   Úapply_a_ufuncÏ  s   z>DUFunc._install_ufunc_at.<locals>.ol_at.<locals>.apply_a_ufuncc                    s0   ˆ r|   |t |¡¡S |   |t |¡t |¡¡S r   )rå   r±   Úasarray©r   r   r    r"   ©Úb_noner%   r&   Ú	impl_castÔ  s   þz:DUFunc._install_ufunc_at.<locals>.ol_at.<locals>.impl_castc                    sH   ˆr
ˆ| ||ƒ d S t  |¡}|| }t  ||j¡}ˆ | |||jƒ d S r   )r±   rù   Úbroadcast_torJ   Úflat)r   r   r    r"   Zb_Úa_)r÷   rø   rü   r%   r&   Úimpl_genericÜ  s   
z=DUFunc._install_ufunc_at.<locals>.ol_at.<locals>.impl_genericc                 S   s   | |d |ƒ|d< d S )Nr%   r%   rú   r%   r%   r&   Úimpl_indices_empty_b_scalarå  s   zLDUFunc._install_ufunc_at.<locals>.ol_at.<locals>.impl_indices_empty_b_scalarc                    s.   ˆ r| || ƒ||< d S | || |ƒ||< d S r   r%   rú   rû   r%   r&   Úimpl_scalar_scalarè  s   zCDUFunc._install_ufunc_at.<locals>.ol_at.<locals>.impl_scalar_scalarr   )rÍ   rÎ   r   ÚNumbaExperimentalFeatureWarningr/   r   r@   ÚNumbaTypeErrorÚListÚTupleÚ	SliceTyper>   ÚSequencer   Úis_nonelikeÚanyr   rˆ   ÚTypingErrorr[   rW   r   )r   r   r    r"   rÖ   Úindices_arrZindices_listZindices_tupleZindices_sliceZindices_scalarZindices_empty_tupleZb_arrayZb_scalarÚ	need_castrˆ   rý   r  r  r  r1   )r÷   rø   rò   rü   r   r&   Úol_at•  sZ   ÿ
ÿ






	z'DUFunc._install_ufunc_at.<locals>.ol_atr   ©r   rã   r   )r$   rä   rå   r  r%   r1   r&   rê   ’  ó   
zDUFunc._install_ufunc_atc                    ó&   t  |¡}t|dƒd‡ fdd„	ƒ}d S )NÚreducer   c                    s  t jdtjd� t|tjƒsd}t |¡‚t|tjƒo!t|j	tj
ƒ}t|tjƒo-t|ƒdk}t |¡}ˆjjd u ‰ˆjj‰ˆjsS|rSt|ƒdkrSdˆ› d�}t |¡‚d|j ‰	d|jd  ‰
t |¡rg|j	n|‰ˆj‰t ˆ¡‰t |¡‰t‡
fd	d
„ƒ‰t‡	fdd„ƒ‰tdd„ ƒ‰tdd„ ƒ‰tdd„ ƒ‰d‡‡‡‡‡fdd„	}			 	 d‡‡‡‡‡‡‡fdd„	}
		 	 d‡ ‡‡fdd„	}		 	 ddd„}		 	 d‡ fdd„	}|jdkrÚ|sÚ|	S |rÞ|S |rétt|jƒƒ‰ |S |rõdt|ƒd  ‰ |S |dk�st|tj
tjtjfƒ�r|
S d S )Nz$ufunc.reduce feature is experimentalrì   ú-The first argument "array" must be array-liker   rU   zreduction operation 'z:' is not reorderable, so at most one axis may be specified©r   c                    s<   ˆ }d}t | ƒD ]\}}||krqt|||ƒ}|d7 }q|S ©Nr   rU   )r?   r   )ÚtupÚposÚsrF   ÚjÚe)Útup_init_m1r%   r&   Útuple_slice  s   
zDDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.tuple_slicec                    sf   ˆ }ddt |ƒ}}}||k r1||krt|||ƒ}n| | }t|||ƒ}|d7 }|d7 }||k s|S r  ©r>   r   )r  r  rS   r  rF   r  Úszr  )Útup_initr%   r&   Útuple_slice_append(  s   ùzKDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.tuple_slice_appendc                    s:   t  ||||¡}t|ƒ‰‡fdd„‰ ‡ ‡fdd„}||fS )Nc                    s¤  |\}}}}| j |d�}|  |¡�µ t d¡dƒ}	|	}
|dkrBtdˆ ƒD ]}|  ||d ¡}|  ||¡}|  ||¡}|  |
|¡}
q$nud|  k rNˆ d k r•n nEtd|ƒD ]}|  ||¡}|  ||¡}|  ||¡}|  |
|¡}
qUt|d ˆ ƒD ]}|  ||d ¡}|  ||¡}|  ||¡}|  |
|¡}
qwn"tdˆ d ƒD ]}|  ||¡}|  ||¡}|  ||¡}|  |
|¡}
qœ|  |¡ W d   ƒ ||
fS 1 sÉw   Y  ||
fS )N©Únameé@   r   rU   )	Úappend_basic_blockÚ
goto_blockr   ÚIntTypeÚrangeÚextract_valueÚmulr[   Úbranch)r,   Z	block_posÚ
block_nameÚbb_endrm   rK   rn   rH   ÚbbrE   Úflat_idxrF   ÚstrideZidx_iÚm)Úlen_idxr%   r&   Ú	gen_block=  sD   üü
ãáz\DUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.compute_flat_idx.<locals>.gen_blockc                    sü   |\}}}}|j }|jdd�}	g }
tˆƒD ]}ˆ ||d|› �|	|ƒ\}}|
 ||f¡ q| |¡�# | ||
d d ¡}tˆƒD ]}| ||
| d ¡ q@W d   ƒ n1 sWw   Y  | |	¡ | |
d d j	¡}|
D ]
\}}| 
||¡ qm| ||¡S )NZaxis_endr"  Úaxis_éÿÿÿÿr   rU   )Úbasic_blockr%  r(  rA   r&  ÚswitchÚadd_caseÚposition_at_endÚphir¶   Úadd_incomingÚsdiv)r+   r,   r]   rm   rK   ÚitemsizerH   Úaxisr.  Z
switch_endÚlrF   Úblockr/  r7  r:  Úvalue©r3  r2  r%   r&   Úcodegenb  s(   ÿÿþ
zZDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.compute_flat_idx.<locals>.codegen)r   r:   r>   )rõ   rK   r=  rH   r>  r]   rC  r%   rB  r&   Úcompute_flat_idx8  s
   %zIDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.compute_flat_idxc                 S   sF   | }t t| ƒƒD ]}| | dk r| | | n| | }t|||ƒ}q|S rg   )r(  r>   r   )r>  ÚndimÚaxrF   rS   r%   r%   r&   Ú
fixup_axisz  s
    zCDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.fixup_axisc                 S   sB   d| d }}t t| ƒƒD ]}| | |k r|| | }}q||fS rg   )r(  r>   )r  rH   r  rF   r%   r%   r&   Úfind_min‚  s   €zADUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.find_minc           
         s€   ˆr|d u rt |ƒdkrdˆ› d�}t|ƒ‚d}ˆr#ˆ r#d}|d }nˆr(ˆ}n|}|jd }t||ƒD ]	}	| |||	 ƒ}q4|S )Nr   ú'zero-size array to reduction operation ú which has no identityrU   )r>   Ú
ValueErrorrJ   r(  )
r   rO   r>  rW   ÚinitialrÖ   ÚstartÚrr  rF   )Úid_noner†   Úidentity_noneÚ	init_noneÚ
ufunc_namer%   r&   Úimpl_1dŠ  s    ÿ

z@DUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.impl_1dc                    sÄ  |d u rt dƒ‚|dk r||j7 }|dk s||jkrt dƒ‚ˆr5|d u r5|j| dkr5dˆ› d�}t |ƒ‚ˆ|j|ƒ}|d u raˆd u ratj|ˆd�}t |¡D ]\}}	ˆ||dƒ}
||
 ||< qOn|d u rrˆd urrtj|ˆˆd�}ntj||ˆd�}| ¡ }|d u r°ˆd u r°t |¡D ]"\}}|| dkr–q‹ˆ |j|j	||ƒ}|| |}}| ||ƒ||< q‹|S t |¡D ]*\}}|d u rÈˆd u rÈ|| dkrÈqµˆ |j|j	||ƒ}|| |}}| ||ƒ||< qµ|S )Nz'axis' must be specifiedr   úInvalid axisrI  rJ  ©rW   )Ú
fill_valuerW   )
rK  rE  rJ   r±   ÚemptyÚndenumerateÚfullÚravelrK   r=  )r   rO   r>  rW   rL  rÖ   rJ   rN  rH   rn   Z
result_idxÚviewrS   Zflat_posÚlhsÚrhs)rD  r†   rP  Únb_dtyper  r!  rR  r%   r&   Úimpl_nd_axis_intž  sT   
ÿý
ÿ
ø
ÿzIDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.impl_nd_axis_intc                    s  ˆ||j ƒ}tdt|ƒƒD ]+}|| dk s|| |j kr tdƒ‚t|d t|ƒƒD ]}|| || kr7tdƒ‚q)qˆ|ƒ\}}	| j||	||d�}
t|ƒdkrP|
S t|ƒdkre| j|
||d d  d d�S ˆ }tt|ƒƒD ]}||kr{t|||| ƒ}qm| j|
|d�S )Nr   rT  rU   zduplicate value in 'axis')r>  rW   rL  rî   ©r>  )rE  r(  r>   rK  r  r   )r   rO   r>  rW   rL  r4  rF   r  Úmin_idxZmin_elemrN  rF  )Úaxis_tuprH  rG  r%   r&   Úimpl_nd_axis_tupleÜ  s2   ÿÿý€zKDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.impl_nd_axis_tuplec                 S   s   |S r   r%   ©r   rO   r>  rW   rL  r%   r%   r&   Úimpl_axis_empty_tupleú  s   zNDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.impl_axis_empty_tuplec                    s   |   |ˆ ||¡S r   )r  rd  )rb  r%   r&   Úimpl_axis_none  s   zGDUFunc._install_ufunc_reduce.<locals>.ol_reduce.<locals>.impl_axis_none©r   NN)rÍ   rÎ   r   r  r/   r   r@   r  ÚUniTuplerW   ÚIntegerr  r>   r   r
  r   r†   rq   r–   rE  r   r   r³   r(  ÚOmittedÚIntegerLiteral)r   rO   r>  rW   rL  rÖ   Zaxis_int_tupleZaxis_empty_tupleZ	axis_nonerS  r_  rc  re  rf  r1   )rb  rD  rH  rG  rO  r†   rP  rQ  r^  r   r  r  r!  rR  r&   Ú	ol_reduceú  s‚   ÿ
ÿ





A

ü@ü 
ü	üþûz/DUFunc._install_ufunc_reduce.<locals>.ol_reducerg  r  )r$   rä   rå   rl  r%   r1   r&   rè   ÷  r  zDUFunc._install_ufunc_reducec                    r  )NÚreduceatr   c                    sl  t jdtjd� ˆjjdkrd}t |¡‚t |¡s!d}t |¡‚t |¡s-d}t |¡‚t	|t
jt
jt
jtfƒs@d}t |¡‚t |¡‰ˆsRt	|t
jƒsRt d¡‚t	|t
jƒ}|r]|jnd	‰ d
ˆ d  ‰t	|t
jƒ}t |¡rs|jn|‰|ox| }	|rŒ|jdkrŒd|j› �}t |¡‚|	r•ddd„}
|
S t‡fdd„ƒ‰d	dlm} ||jdƒ‰d‡ ‡‡‡‡fdd„	}|S )Nz&ufunc.reduceat feature is experimentalrì   rî   z,reduceat only supported for binary functionsr  z0The second argument "indices" must be array-likez"axis" must be an integerzoutput must be an arrayr   r  rU   z8Expect "indices" array to have at most 1 dimension. Got c                 S   s    | j t |¡t |¡|||d�S )N)rW   r¼   )rm  r±   rù   )r   rO   r    r>  rW   r¼   r%   r%   r&   rý   I  s   üzFDUFunc._install_ufunc_reduceat.<locals>.ol_reduceat.<locals>.impl_castc                    sP   ˆ }d\}}|t | ƒk r&||krt||| | ƒ}|d7 }|d7 }|t | ƒk s|S )N)r   r   rU   r  )r  r  r  rF   r  )Útup_m1r%   r&   r  Q  s   üzHDUFunc._install_ufunc_reduceat.<locals>.ol_reduceat.<locals>.tuple_slice)Ú"generate_getitem_setitem_with_axisÚsetitemc                    sä  |j d }|}|dk r|ˆ 7 }|dk s|ˆ kr!td|› dˆ › �ƒ‚t|j ||ƒ}ˆs5|j |kr5d}	t|	ƒ‚ˆr>tj|ˆd�}t|ƒdkrF|S d}
tt|ƒd ƒD ]w}|| ||d  k r‡t || ||d  ¡}ˆ dkrtt |||¡}n|| }| j	||d�}ˆ||
||ƒ n<|| ||d  kr³|| }t |||¡}ˆ dkr«ˆ|j |ƒ}| 
|¡}ˆ||
||ƒ n|| |ks¿|| dk rÃtdƒ‚|
d7 }
qPt |d	 |j | ¡}ˆ dkrßt |||¡}n|| }|  	||¡}ˆ||
||ƒ |S )
Nr   zaxis z) is out of bounds for array of dimension z=operands could not be broadcast together with remmaped shapesrU  rU   r`  zInvalid value for indicesr5  )rJ   rK  r   r±   Úzerosr>   r(  ÚarangeÚtaker  Úreshape)r   rO   r    r>  rW   r¼   r  rF  rJ   rÖ   r  rF   rH   Z	arr_sliceZ
arr_reduceÚ_slice)Ú
array_ndimÚdtÚout_nonerp  r  r%   r&   r^   a  sT   

ÿ

zADUFunc._install_ufunc_reduceat.<locals>.ol_reduceat.<locals>.implrg  )rÍ   rÎ   r   r  r   rˆ   r  r   Útype_can_asarrayr/   r   ri  rj  rk  rÔ   r   r
  r@   rE  rW   ÚNumbaValueErrorr   r8   ro  )r   rO   r    r>  rW   r¼   rÖ   Z	array_arrr  r  rý   ro  r^   r1   )rv  rw  rx  rp  rn  r  r&   Úol_reduceat   sP   ÿ




ÿ



ÿ

:z3DUFunc._install_ufunc_reduceat.<locals>.ol_reduceatrg  r  )r$   rä   rå   r{  r%   r1   r&   ré     r  zDUFunc._install_ufunc_reduceatc                    s’   t  |¡r|fn||f}dd„ |D ƒ}tdd„ |D ƒƒ}|  |¡dkr4| jr/d| f }t|ƒ‚| j|Ž  t  |¡r@tƒ  ||¡S tƒ j|||fŽ S )Nc                 s   r½   r   )r   ©r¿   rº   r%   r%   r&   rÁ      rÂ   zDUFunc.at.<locals>.<genexpr>c                 s   s&   � | ]}t |tjƒr|jn|V  qd S r   )r/   r   r@   rW   r|  r%   r%   r&   rÁ   ¡  s   € ÿrp   z#compilation disabled for %s.at(...))	r   r
  r³   rx   r¡   rË   rÊ   rv   rå   )r$   r   r    r"   rm   rÈ   rÙ   rÖ   r{   r%   r&   rå   �  s   ÿ


z	DUFunc.atc                 C   sV   |du r	| j jj}td| jj tft| | jd�ƒ}| 	| |¡ |  
|¡ |  |¡ dS )a*  Constructs and installs a typing class for a DUFunc object in the
        input typing context.  If no typing context is given, then
        _install_type() installs into the typing context of the
        dispatcher object (should be same default context used by
        jit() and njit()).
        NZDUFuncTyping_)rh   Úgeneric)r�   ÚtargetdescrÚtyping_contextr¶   r   rq   r   r    Ú_type_meÚinsert_user_functionrç   rë   )r$   rõ   Ú_ty_clsr%   r%   r&   r˜   ²  s   
þ
zDUFunc._install_typec                 C   sj   | j rt | |¡}|du rdS t|j|j ƒdt|ƒ… }| jj 	¡ D ]\}}|j
|kr2||f  S q#dS )a  
        Given a tuple of element-wise argument types, find a matching
        signature in the dispatcher.

        Return a 2-tuple containing the matching signature, and
        compilation result.  Will return two None's if no matching
        signature was found.
        Nrp   )r¡   r   Úufunc_find_matching_loopr³   ÚinputsÚoutputsr>   r�   rž   rÌ   rm   )r$   rÙ   Úloopr]   rD   r%   r%   r&   rx   Â  s   	
ÿzDUFunc.find_ewise_functionc                 C   s
  |rJ ‚| j }tj |||¡}|\}}}}t|ƒ}	|	dkr)t|dt|ƒ … ƒ}
nt|ƒ}
|  |
¡\}}|du rV| jrDt 	d| |f ¡‚|  
|
¡ |  |
¡\}}|dusVJ ‚|	dkr_t|ƒ}n|jdkrw|dkrrt |j||¡g}n
|jg}nt d¡‚| |¡ t|Ž S )zì
        Implement AbstractTemplate.generic() for the typing class
        built by DUFunc._install_type().

        Return the call-site signature after either validating the
        element-wise signature or compiling for it.
        r   Nzcannot call %s with types %srU   ztyping gufuncs (nout > 1))r   r   ÚNumpy_rules_ufuncÚ_handle_inputsr>   r³   rx   r¡   r   r  r®   rœ   r‰   r   r@   rb   ÚNumbaNotImplementedErrorÚextendr   )r$   rÈ   Úkwtysr   Z_handle_inputs_resultÚ
base_typesÚexplicit_outputsÚndimsrN   Zexplicit_output_countrÙ   r]   rD   Zouttysr%   r%   r&   r€  Ö  s8   ÿÿ





zDUFunc._type_me)NFNr   )rÛ   N)&rq   rr   rs   r}   ÚsetZ_DUFunc__base_kwargsr'   r�   r¢   Úclassmethodr§   r¨   Úpropertyr’   rˆ   r‰   r«   r¬   r   r†   r   r¥   r[   r°   rÊ   r   r®   rÚ   rç   rë   rê   rè   ré   rå   r˜   rx   r€  r~   r%   r%   r{   r&   r…   ±   sX    









$



e  
( 
r…   )3r�   rY   rÍ   Únumpyr±   Únumbar   r   Ú
numba.corer   r   r   r   r   Únumba.core.extendingr	   r
   r   r   r   Únumba.core.typingr   Únumba.core.typing.templatesr   r   Únumba.cpython.unsafe.tupler   Únumba.np.ufuncr   Znumba.np.ufunc.ufunc_baser   r   Únumba.parforsr   r   r‚   r   Útypingr   Úllvmliter   Únumba.core.compiler_lockr   r   rƒ   r„   ÚReduceMixinr£   r…   Ú	MAP_TYPESrA   r%   r%   r%   r&   Ú<module>   s:    z
      T