
    sh1                         S SK Jr  S SKJr  S SKJr  S SKJr  S SKJ	r	J
r
  S SKJr  S SKJrJr  S SKJr  S S	KJrJr  S S
KrS r " S S\
5      r " S S\R.                  \	5      rS rg
)    )typeof)types)GUFuncBuilder)parse_signature)	UfuncBaseUfuncLowererBase)ufunc_find_matching_loop)	serializeerrors)npydecl)	signatureAbstractTemplateNc                    ^  SSK Jn   " U 4S jSUR                  5      nU=R                  T R                  -  sl        U$ )Nr   npyimplc                   L   >^  \ rS rSrSr YrU 4S jrU 4S jrU 4S jrSr	U =r
$ )(make_gufunc_kernel.<locals>.GUFuncKernel   z
npyimpl._Kernel subclass responsible for lowering a gufunc kernel
(element-wise function) inside a broadcast loop (which is
generated by npyimpl.numpy_gufunc_kernel()).
c                    > [         TU ]  XU5        U R                  R                  UR                  5      nU R                  R                  U5      u  U l        U l        g N)super__init__dufunc_get_ewise_dtypesargsfind_ewise_function	inner_sigcres)selfcontextbuilder	outer_sigewise_types	__class__s        i/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/numba/np/ufunc/gufunc.pyr   1make_gufunc_kernel.<locals>.GUFuncKernel.__init__   sJ    GWy9++77	GK(,(G(G)%DNDI    c                    > [        U[        R                  5      (       a9  [        U[        R                  5      (       d  [        TU ]  XR
                  U5      $ [        TU ]  XU5      $ r   )
isinstancer   Arrayr   castdtype)r   valfromtytotyr$   s       r%   r+   -make_gufunc_kernel.<locals>.GUFuncKernel.cast   sK    &%++..tU[[11w|Ct<<7<T22r'   c                    > U R                   R                  (       a  Sn[        R                  " U5      eU R                  R                  U R                   R                  45        [        TU ]   " U6 $ )NzCCalling a guvectorize function in object mode is not supported yet.)	r   
objectmoder   NumbaRuntimeErrorr    add_linking_libslibraryr   generate)r   r   msgr$   s      r%   r6   1make_gufunc_kernel.<locals>.GUFuncKernel.generate%   sW    yy##(..s33LL))499+<+<*>?7#T**r'   )r   r   )__name__
__module____qualname____firstlineno____doc__r   r   r+   r6   __static_attributes____classcell__)r$   _dufuncs   @r%   GUFuncKernelr      s#    	
 		3	+ 	+r'   rA   )numba.npr   _Kernelr9   )r@   r   rA   s   `  r%   make_gufunc_kernelrD      s5     +w +: W---r'   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )GUFuncLowerer1   zHCallable class responsible for lowering calls to a specific gufunc.
    c                 P   > SSK Jn  [        TU ]  U[        UR
                  5        g )Nr   r   )rB   r   r   r   rD   numpy_gufunc_kernel)r   gufuncr   r$   s      r%   r   GUFuncLowerer.__init__4   s!    $+ 44	6r'    )r9   r:   r;   r<   r=   r   r>   r?   )r$   s   @r%   rF   rF   1   s    6 6r'   rF   c                       \ rS rSrSrSSS0 S4S jrS rS r\S	 5       r	S
 r
SS jrS rS rS rS rSS jrS r\S 5       rS rS rS rS rSrg)GUFunc;   z
Dynamic generalized universal function (GUFunc)
intended to act like a normal Numpy gufunc, but capable
of call-time (just-in-time) compilation of fast loops
specialized to inputs.
NFrL   c                    S U l         SU l        XPl        X0l        [	        XX4Xg5      U l        U R
                  R                  R                  U l        U R
                  R                  R                  U l        U R
                  R                  U l
        U R                  U R                  5        [        R                  " X5        g )NF)ufunc_frozen_is_dynamic	_identityr   gufunc_builderpy_funcr9   r=   nb_func_dispatcher_initialize	functoolsupdate_wrapper)r   rV   r   identitycache
is_dynamictargetoptionswritable_argss           r%   r   GUFunc.__init__C   s    
%!
 ,O ++33<<**22::..66))*  /r'   c                     U R                  5         U R                  5         [        U 5      U l        U R	                  5         g r   )build_ufunc_install_typerF   	_lower_me_install_cg)r   
dispatchers     r%   rY   GUFunc._initializeV   s2    &t,r'   c                     U R                   n[        UR                  UR                  U R                  UR
                  U R                  UR                  UR                  UR                  U R                  S9	nU$ )N)	rV   r   r\   r]   r^   r_   r`   typesigsfrozen)rU   dictrV   r   rT   r]   rS   r_   r`   _sigsrR   )r   gbdcts      r%   _reduce_statesGUFunc._reduce_states\   sc      JJll^^((''****XX<<

 
r'   c
           
      x    U " XUXEXgS9n
U H  nU
R                  U5        M     U
R                  5         Xl        U
$ )N)rV   r   r\   r]   r^   r_   r`   )addrc   rR   )clsrV   r   r\   r]   r^   r_   r`   rj   rk   r   sigs               r%   _rebuildGUFunc._rebuildk   sF     7(!.M CHHSM r'   c                 "    SU R                    S3$ )Nz<numba._GUFunc 'z'>)r9   r   s    r%   __repr__GUFunc.__repr__w   s    !$--33r'   c           	          Uc   U R                   R                  R                  n[        SU R                  -   [
        4[        X R                  S95      nUR                  X5        g)a  Constructs and installs a typing class for a gufunc 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GUFuncTyping_)keygeneric)	rX   targetdescrtyping_contexttyper9   r   rl   _type_meinsert_user_function)r   	typingctx_ty_clss      r%   rd   GUFunc._install_typez   sX     ((44CCI6(*mm<> 	&&t5r'   c                 :    U R                   R                  U5        g r   )rU   rs   )r   ftys     r%   rs   
GUFunc.add   s    $r'   c                 D    U R                   R                  5       U l        U $ r   )rU   rc   rQ   ry   s    r%   rc   GUFunc.build_ufunc   s    ((446
r'   c                     [        U R                  R                  5      n[        [	        [
        US   5      5      [        [	        [
        US   5      5      4$ Nr      )r   rU   r   tuplemaplen)r   
parsed_sigs     r%   expected_ndimsGUFunc.expected_ndims   sC    $T%8%8%B%BC
c#z!}-.c#z!}6M0NOOr'   c                    U(       a   eU R                   nU R                  R                  nU R                  5       u  pVXV-   n[	        U5      (       d   [	        U5      5       e[        U5       H  u  p[        U	[        R                  5      (       d  M&  U	R                  Xx   :  d  M:  U[	        U5      :  a  SOSn
U[	        U5      :  a  UOU[	        U5      -
  nU R                   SU
 SU SU	R                   SU SXx    S3n[        R                  " U5      e   [        R                  R                  X1U5      nUu  n    nU R!                  U5      u  pOUc  U R"                  (       a  S	U  S
U 3n[        R                  " U5      eU R%                  U5        U R!                  U5      u  pOUS:X  a(  SU R                   S
U 3n[        R                  " U5      eUc   e[        [        R&                  /UQ76 $ )z
Implement AbstractTemplate.generic() for the typing class
built by gufunc._install_type().

Return the call-site signature after either validating the
element-wise signature or compiling for it.
InputOutputz: z	 operand z& does not have enough dimensions (has z, gufunc core with signature z
 requires )zcannot call z with types NNzFail to compile )rQ   rU   r   r   r   	enumerater)   r   r*   ndimr9   r   TypingErrorr   Numpy_rules_ufunc_handle_inputsr   rR   _compile_for_argtysnone)r   argtyskwsrQ   ru   	inp_ndims	out_ndimsndimsidxargkindir7   _handle_inputs_resultr#   _s                   r%   r   GUFunc._type_me   s    w

!!++#224	%6{{&CJ&{!&)HC#u{{++5:0E"%I"6wHY/CS3y>5I}}oRvYqc :..1hhZ 8!!$Z
|1>  ((-- * !( 9 9 H H3! 4Q1))+6; ||$TF,vh?((--$$[1--k:FCl"(|F8L((--?"?-f--r'   c                 X    U R                   " U6 nU R                  R                  U5        g r   )_get_function_typerU   rs   )r   r   return_typefntys       r%   r   GUFunc._compile_for_argtys   s(     &&/%r'   c                 f    U R                  UR                  5      n[        U5      [        U5      :H  $ r   )r   r   r   )r   r#   ru   dtypess       r%   match_signatureGUFunc.match_signature   s*    ''1V}k 222r'   c                     U R                   $ r   )rS   ry   s    r%   r^   GUFunc.is_dynamic   s    r'   c                     [        S U5      n/ nU HP  n[        U[        R                  5      (       a  UR	                  UR
                  5        M?  UR	                  U5        MR     U$ )Nc                 Z    [        U [        R                  5      (       a  U $ [        U 5      $ r   )r)   r   Typer   )r   s    r%   <lambda>*GUFunc._get_ewise_dtypes.<locals>.<lambda>   s%    
3

(C(C "!C["!r'   )r   r)   r   r*   appendr,   )r   r   r   tysargtys        r%   r   GUFunc._get_ewise_dtypes   sW     !"&(E%--

5;;'

5!	 
 
r'   c                     [        U R                  R                  5      n[        U5      [        US   5      [        US   5      -   :H  $ r   )r   rU   r   r   )r   r   r   s      r%   _num_args_matchGUFunc._num_args_match   s>    $T%8%8%B%BC
4yC
1.Z]1CCCCr'   c                 $   [        U R                  R                  5      nU R                  U5      n/ n[	        US   5       HT  u  pV[        U5      nUS:X  a  UR                  X5   5        M+  UR                  [        R                  " X5   US5      5        MV     [        US   5      n[	        US   5       HF  u  pVX5U-      n	[        U5      =(       d    Sn
UR                  [        R                  " XS5      5        MH     [        R                  " U6 $ )Nr   Ar   )
r   rU   r   r   r   r   r   r   r*   r   )r   r   r   r#   lr   sig_dimr   offsetrettyret_ndims              r%   r   GUFunc._get_function_type   s    $T%8%8%B%BC
,,T2 %jm4LCw<Dqy)*[%5tSAB 5 Z]#%jm4LCf-E7|(qHHHU[[#67 5
 zz1~r'   c                    U R                   (       d  U R                  (       dF  U(       a-  [        US   5      (       a  US   R                  " U S/UQ70 UD6$ U R                  " U0 UD6$ SU;   a  XR                  S5      4-  nU R                  " U6 SL a  SU R                   S3n[        U5      eU R                  U5      nU R                  (       a  [        U R                  U5      (       dE  U R                  U5      S:w  d   U R                  " U6 nU R                  U5        U R                  5         U R                  " U0 UD6$ )Nr   __call__outFz Too few arguments for function 'z'. Note that the pattern `out = gufunc(Arg1, Arg2, ..., ArgN)` is not allowed. Use `gufunc(Arg1, Arg2, ..., ArgN, out) instead.r   )rR   r^   _is_array_wrapper__array_ufunc__rQ   popr   r9   	TypeErrorr   r	   r   r   rs   rc   )r   r   kwargsr7   ewiseru   s         r%   r   GUFunc.__call__   s8    <<t
 )$q'22Aw..*'+/5  zz42622f_ZZ&((D&%/
 34==/ B  
 C.  &&t,

7

EJJ ++E2lB--t4zz4*6**r'   )	r=   r9   rX   rR   rT   rS   re   rU   rQ   r   )r9   r:   r;   r<   r=   r   rY   rp   classmethodrv   rz   rd   rs   rc   r   r   r   r   propertyr^   r   r   r   r   r>   rL   r'   r%   rN   rN   ;   s     59!20& 	 	46%P-.^&3    	D0(+r'   rN   c                 x    [        U [        5      (       + =(       a    [        U S5      =(       a    [        U S5      $ )a  Return True if obj wraps around numpy or another numpy-like library
and is likely going to apply the ufunc to the wrapped array; False
otherwise.

At the moment, this returns True for

- dask.array.Array
- dask.dataframe.DataFrame
- dask.dataframe.Series
- xarray.DataArray
- xarray.Dataset
- xarray.Variable
- pint.Quantity
- other potential wrappers around dask array or dask dataframe

We may need to add other libraries that pickle ufuncs from their
__array_ufunc__ method in the future.

Note that the below test is a lot more naive than
`dask.base.is_dask_collection`
(https://github.com/dask/dask/blob/5949e54bc04158d215814586a44d51e0eb4a964d/dask/base.py#L209-L249),  # noqa: E501
because it doesn't need to find out if we're actually dealing with
a dask collection, only that we're dealing with a wrapper.
Namely, it will return True for a pint.Quantity wrapping around a plain float, a
numpy.ndarray, or a dask.array.Array, and it's OK because in all cases
Quantity.__array_ufunc__ is going to forward the ufunc call inwards.
__dask_graph__r   )r)   r   hasattr)objs    r%   r   r   #  s6    : sD!! 	,C)*	,C*+r'   )numbar   
numba.corer   numba.np.ufunc.ufuncbuilderr   numba.np.ufunc.sigparser   numba.np.ufunc.ufunc_baser   r   numba.np.numpy_supportr	   r
   r   numba.core.typingr   numba.core.typing.templatesr   r   rZ   rD   rF   ReduceMixinrN   r   rL   r'   r%   <module>r      sR      5 3 A ; ( % C !H6$ 6e+Y""I e+P r'   