
    ܙd/                        d dl Z d dlmZmZ d dlmZmZ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 Z G d de          Z G d dej        ej                  Zej                            e           dS )    N)jittypeof)cgutilstypes	serializesigutils)	is_jitted)npydecl)AbstractTemplate	signature)	_internal)array_analysis)ufuncbuilder)numpy_supportc                 v     ddl m}  G  fdd|j                  xj         j        j        z  c_        S )Nr   npyimplc                   0     e Zd ZdZZ fdZd Z xZS )(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                     t          |                               |||           | j                            |j                  \  | _        | _        d S N)super__init__dufuncfind_ewise_functionargs	inner_sigcres)selfcontextbuilder	outer_sigDUFuncKernel	__class__s       5lib/python3.11/site-packages/numba/np/ufunc/dufunc.pyr   z1make_dufunc_kernel.<locals>.DUFuncKernel.__init__   sN    ,%%..wKKK(,(G(G)  ) %DNDIII    c                      j         } j        } fdt          ||j        |j                  D             } j        j        rK j        j                            t          j
        t          j
        gt          |j                  z            }n* j        j                            |j        |j                  } j        j        j        j        }t#          j        || j        j        j                  }|j                            d            j        j                             j        ||j        |j        |          \  }}	                     |	|j        |j                  S )Nc                 F    g | ]\  }}}                     |||          S  )cast).0valintyouttyr   s       r%   
<listcomp>zEmake_dufunc_kernel.<locals>.DUFuncKernel.generate.<locals>.<listcomp>!   s?     : : :-S$ 3e44 : : :r&   alwaysinline)r   r"   zipr   r   
objectmoder    	call_convget_function_typer   pyobjectlenreturn_typer!   blockfunctionmoduler   get_or_insert_functionfndescllvm_func_name
attributesaddcall_functionr*   )
r   r   isigosig	cast_args	func_typer:   entry_point_ress
   `         r%   generatez1make_dufunc_kernel.<locals>.DUFuncKernel.generate   sO   >D>D: : : :T49di88: : :I y# 1 L2DDNU^$4s49~~$EG G		 !L2DD$di1 1	\'07F!8		 /1 1K "&&~666\+99k4+;TY FAs 99S$"2D4DEEEr&   )__name__
__module____qualname____doc__r   r   rH   __classcell__)r$   r#   _dufuncs   @r%   r#   r      se        	 	
 	  	  	  	  	  	 
	F 	F 	F 	F 	F 	F 	Fr&   r#   )numba.npr   _KernelrI   ufunc)rN   r   r#   s   ` @r%   make_dufunc_kernelrR      s~          "F "F "F "F "F "F "F "Fw "F "F "FH W]33r&   c                       e Zd ZdZd Zd ZdS )DUFuncLowererzHCallable class responsible for lowering calls to a specific DUFunc.
    c                 <    t          |          | _        g | _        d S r   )rR   kernellibs)r   r   s     r%   r   zDUFuncLowerer.__init__<   s    (00			r&   c                 j    ddl m} |                    ||||| j        j        j        | j                  S )Nr   r   )rO   r   numpy_ufunc_kernelrV   r   rQ   )r   r    r!   sigr   r   s         r%   __call__zDUFuncLowerer.__call__@   sD    $$$$$$))'7C*.+*<*B*.+7 7 	7r&   N)rI   rJ   rK   rL   r   r[   r)   r&   r%   rT   rT   9   s<           7 7 7 7 7r&   rT   c                   8    e Zd ZdZ ed          Zddi fdZ fdZd Ze	d             Z
d	 Zed
             Zed             Zed             Zed             Zed             Zed             Zed             Zd Zd Zd ZddZddZd Zd ZddZ xZS )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                    t          |          r|j        }t          j                    5   t	          dd|d||          }d d d            n# 1 swxY w Y   |                     ||           t          j        | |           d S )Nnpyufunc)_targetcacher)   )r	   py_funcr   3_suppress_deprecation_warning_nopython_not_suppliedr   _initialize	functoolsupdate_wrapper)r   rf   r^   re   targetoptions
dispatchers         r%   r   zDUFunc.__init__Q   s    W 	&oGMOO 	7 	7. .Z#(. .,. ..57 7J	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	X... w/////s   AAAc                 @   t          j        |          }t          t          |                               ||           |                                  t          |           | _        |                                  |j	        j
        | _
        |j	        j        | _        d S )N)r^   )r   parse_identityr   r]   r   _install_typerT   	_lower_me_install_cgrf   rI   rL   )r   rl   r^   r$   s      r%   rh   zDUFunc._initialize[   s    .x88fd$$Z($CCC 	&t,,"*3!)1r&   c                     t          | j        j                                                  }t	          | j        | j        | j        |          S )4
        NOTE: part of ReduceMixin protocol
        )rl   r^   frozensiglist)list_dispatcher	overloadskeysdictr^   _frozen)r   ru   s     r%   _reduce_stateszDUFunc._reduce_statesf   sM     t'1668899']<	
 
 
 	
r&   c                     t           j                            |           }|                    ||           |D ]}|                    |           |r|                                 |S )rs   )r   _DUFunc__new__rh   r?   disable_compile)clsrl   r^   rt   ru   r   rZ   s          r%   _rebuildzDUFunc._rebuildr   so    
  ((--X... 	 	CHHSMMMM 	#  """r&   c                     | S )zK
        For compatibility with the various *UFuncBuilder classes.
        r)   r   s    r%   build_ufunczDUFunc.build_ufunc   s	     r&   c                     | j         j        S r   )rw   rk   r   s    r%   rk   zDUFunc.targetoptions   s    --r&   c                     | j         j        S r   )rQ   r`   r   s    r%   r`   z
DUFunc.nin   s    z~r&   c                     | j         j        S r   )rQ   ra   r   s    r%   ra   zDUFunc.nout   s    zr&   c                     | j         j        S r   )rQ   nargsr   s    r%   r   zDUFunc.nargs       zr&   c                     | j         j        S r   )rQ   ntypesr   s    r%   r   zDUFunc.ntypes   s    z  r&   c                     | j         j        S r   )rQ   r   r   s    r%   r   zDUFunc.types   r   r&   c                     | j         j        S r   )rQ   r^   r   s    r%   r^   zDUFunc.identity   s    z""r&   c                 R    t          | j        j                  dk    sJ d| _        dS )zI
        Disable the compilation of new signatures at call time.
        r   TN)r6   rw   rx   r{   r   s    r%   r   zDUFunc.disable_compile   s.    
 4#-..2222r&   c                 \    t          j        |          \  }}|                     ||          S )z=
        Compile the DUFunc for the given signature.
        )r   normalize_signature_compile_for_argtys)r   rZ   r   r7   s       r%   r?   z
DUFunc.add   s/     %8==k''k:::r&   c           	      J   | j         j        }|r]d|v r|                    d          }||fz  }|r<t          dd                    d t          |          D                       z            t          |          }||k    s||| j         j        z   k    sJ |rJ g }|d |         D ]\}t          |          }t          |t          j                  r|j        }nt          j        |          }|                    |           ]|                     t#          |                    S )Noutz)unexpected keyword arguments to ufunc: %sz, c              3   4   K   | ]}t          |          V  d S r   )repr)r+   ks     r%   	<genexpr>z+DUFunc._compile_for_args.<locals>.<genexpr>   s(      +I+IDGG+I+I+I+I+I+Ir&   )rQ   r`   pop	TypeErrorjoinsortedr6   ra   r   
isinstancer   Arraydtyper   map_arrayscalar_typeappendr   tuple)	r   r   kwsr`   r   args_lenargtysargargtys	            r%   _compile_for_argszDUFunc._compile_for_args   s@   jn 	K||ggenn K K"&))+I+IVC[[+I+I+I"I"I!J K K K t99CXtz1F%F%F%F%F: 	! 	!C3KKE%-- @ &:3??MM%    ''f666r&   c                    | j         rt          d|           t          |t                    sJ ||}n || }t	          j        | j        | j        |          \  }}}t	          j        |||          }t	          j	        ||          \  }}}| 
                    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 )r{   RuntimeErrorr   r   r   _compile_element_wise_functionrw   rk   _finalize_ufunc_signature!_build_element_wise_ufunc_wrapper	_add_loopintr_   r   libraryrp   rW   )	r   r   r7   rZ   r   
actual_sig	dtypenumsptrenvs	            r%   r   zDUFunc._compile_for_argtys   s	    < 	H,FGGG&%(((((CC+v&C$0$Od0#%7 %7!fk!;&+' '
*L* 	3s3xx+++T\37888""4<000r&   c                     || j         j        j        }t          d| j        j        z   t          ft          | | j                            }|	                    | |           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()).
        NDUFuncTyping_)keygeneric)
rw   targetdescrtyping_contexttyperQ   rI   r   rz   _type_meinsert_user_function)r   	typingctx_ty_clss      r%   ro   zDUFunc._install_type   si     (4CI)<<(*dm<<<> > 	&&tW55555r&   c                    | j         rJt          j        | |          }|dS t          |j        |j        z             dt          |                   }| j        j        	                                D ]\  }}|j
        |k    r||fc S 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.
        N)NN)r{   r   ufunc_find_matching_loopr   inputsoutputsr6   rw   rx   itemsr   )r   ewise_typeslooprZ   r   s        r%   r   zDUFunc.find_ewise_function   s     < 	O 9$LLD|!zdl :;;<MS=M=M<MNK)399;; 	! 	!ICx;&&Dy    'zr&   c                    |rJ | j         }t          j                            |||          }|\  }}}}t	          |          }	|	dk    r&t          |dt	          |                              }
nt          |          }
|                     |
          \  }}|M| j        rt          d| d|          | 	                    |
           |                     |
          \  }}|J |	dk    rt          |          }nF|j        dk    r,|dk    rt          j        |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 z with types    ztyping gufuncs (nout > 1))rQ   r
   Numpy_rules_ufunc_handle_inputsr6   r   r   r{   r   r   rv   ra   r   r   r7   NotImplementedErrorextendr   )r   r   kwtysrQ   _handle_inputs_result
base_typesexplicit_outputsndimslayoutexplicit_output_countr   rZ   r   outtyss                 r%   r   zDUFunc._type_me  s    
 ' 9 H H65!" !"6K3
$eV #$4 5 5 1$$
+BS1A-B-B,B+B CDDKK
++K,,[99	T; | 2i#'44!1 2 2 2$$[11100==IC??? 1$$*++FFZ1__qyy+coufEEF/*%&ABBBf&!!r&   c                     | j         j        j        }t          j        }t          j        }|f j        j        z  |f j        j        z  z   }|f j        j        z  }|	                     fd||fD                        dS )a-  
        Install an implementation function for a DUFunc object in the
        given target context.  If no target context is given, then
        _install_cg() installs into the target context of the
        dispatcher object (should be same default context used by
        jit() and njit()).
        Nc                 $    g | ]}j         |fS r)   )rp   )r+   rZ   r   s     r%   r/   z&DUFunc._install_cg.<locals>.<listcomp>D  s"    AAASdndC(AAAr&   )
rw   r   target_contextr   Anyr   rQ   r`   ra   insert_func_defn)r   	targetctx_any_arrsig0sig1s   `     r%   rq   zDUFunc._install_cg4  s     (4CIy{w'4'DJO*CCw'""AAAAT4LAAA	C 	C 	C 	C 	Cr&   r   )rI   rJ   rK   rL   set_DUFunc__base_kwargsr   rh   r|   classmethodr   r   propertyrk   r`   ra   r   r   r   r^   r   r?   r   r   ro   r   r   rq   rM   )r$   s   @r%   r]   r]   G   s         CABBM)-U" 0 0 0 0	2 	2 	2 	2 	2

 

 

   [   . . X.   X   X     X  ! ! X!     X  # # X#  ; ; ;7 7 7>   46 6 6 6  (&" &" &"PC C C C C C C Cr&   r]   )ri   numbar   r   
numba.corer   r   r   r   numba.core.extendingr	   numba.core.typingr
   numba.core.typing.templatesr   r   numba.np.ufuncr   numba.parforsr   r   rO   r   rR   objectrT   ReduceMixinr~   r]   	MAP_TYPESr   r)   r&   r%   <module>r      sm               : : : : : : : : : : : : * * * * * * % % % % % % C C C C C C C C $ $ $ $ $ $ ( ( ( ( ( ( ' ' ' ' ' ' " " " " " "( ( (V7 7 7 7 7F 7 7 7}C }C }C }C }CY"I$5 }C }C }C@     ' ' ' ' 'r&   