
    sh.                    J   S r SSKJr  SSKrSSKJrJr  SSKrSSKJ	r	J
r
  \(       a  SSKJrJrJrJrJrJr  S rS	 r\S!S
S.S"S jjj5       r\  S#S
S.       S$S jjj5       r\  S#S
S.       S$S jjj5       r\   S%SS.         S&S jjj5       r\   S%SS.         S&S jjj5       r\     S'           S(S jj5       r\r\     S'           S(S jj5       r\r\   S%       S$S jj5       r\      S)             S*S jj5       r\      S)             S*S jj5       r\r \    S+SS.           S,S jjj5       r!\     S-SS.           S,S jjj5       r"\     S-SS.           S,S jjj5       r#   S.       S/S jjr$   S.       S/S jjr%\%r&   S%S
S.   S0S jjjr'     S1SS.           S2S jjjr(     S1SS.           S2S jjjr)    S3     S4S  jjr*g)5zImplementation of reduction operations, to be wrapped into arrays, dtypes etc
in the 'public' layer.

Anything here only deals with torch objects, e.g. "dtype" is a torch.dtype instance etc
    )annotationsN)OptionalTYPE_CHECKING   )_dtypes_impl_util)	ArrayLikeAxisLike	DTypeLikeKeepDimsNotImplementedTypeOutArrayc                J   ^  [         R                  " T 5      SU 4S jj5       nU$ )z
Generically handle axis arguments in reductions.
axis is *always* the 2nd arg in the function so no need to have a look at its signature
c                   > Ub   [         R                  " XR                  5      nUS:X  a2  [         R                  " U R                  SS9nU R                  U5      n SnT" X/UQ70 UD6$ )N r   axisr   )r   normalize_axis_tuplendimexpand_shapeshapereshape)ar   argskwdsnewshapefuncs        q/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/_numpy/_reductions_impl.pywrapped"_deco_axis_expand.<locals>.wrapped#   sd    --dFF;D2: ))!'':H		(#ADA+d+d++    N)	functoolswraps)r   r    s   ` r   _deco_axis_expandr&      s'     __T, , Nr"   c                    U c  Un U R                   (       d0  U R                  (       d  [        R                  " 5       R                  $ U $ )a  Return a dtype that is real or complex floating-point.

For inputs that are boolean or integer dtypes, this returns the default
float dtype; inputs that are complex get converted to the default complex
dtype; real floating-point dtypes (`float*`) get passed through unchanged
)is_floating_point
is_complexr   default_dtypesfloat_dtype)dtypeother_dtypes     r   _atleast_floatr.   4   s9     }##u'7'7**,888Lr"   F)keepdimsc               $    U R                  U5      $ r#   )count_nonzero)r   r   r/   s      r   r1   r1   B   s    ??4  r"   c               0   U R                  5       (       a  [        SU R                   S35      e[        R                  " U5      nU R                  [
        R                  :X  a  U R                  [
        R                  5      n [
        R                  " X5      $ )Nzargmax with dtype=.)
r)   NotImplementedErrorr,   r   allow_only_single_axistorchbooltouint8argmaxr   r   outr/   s       r   r:   r:   G   i     	||~~!$6qwwiq"ABB''-Dww%**DD<<  r"   c               0   U R                  5       (       a  [        SU R                   S35      e[        R                  " U5      nU R                  [
        R                  :X  a  U R                  [
        R                  5      n [
        R                  " X5      $ )Nzargmin with dtype=r3   )
r)   r4   r,   r   r5   r6   r7   r8   r9   argminr;   s       r   r?   r?   [   r=   r"   )wherec               n    [         R                  " U5      nUc  0 OSU0n[        R                  " U 40 UD6$ Ndim)r   r5   r6   anyr   r   r<   r/   r@   axis_kws         r   rD   rD   o   7     ''-DLbudmG99Q"'""r"   c               n    [         R                  " U5      nUc  0 OSU0n[        R                  " U 40 UD6$ rB   )r   r5   r6   allrE   s         r   rI   rI   }   rG   r"   c                ~    U R                  5       (       a  [        SU R                   35      eU R                  U5      $ )Nzamax with dtype=)r)   r4   r,   amaxr   r   r<   r/   initialr@   s         r   rK   rK      4     	||~~!$4QWWI">??66$<r"   c                ~    U R                  5       (       a  [        SU R                   35      eU R                  U5      $ )Nzamin with dtype=)r)   r4   r,   aminrL   s         r   rP   rP      rN   r"   c                H    U R                  U5      U R                  U5      -
  $ r#   )rK   rP   r;   s       r   ptprR      s     66$<!&&,&&r"   c                    Ub!  [        U[        R                  5      (       d   eU[        R                  :X  a  [        R
                  " 5       R                  nUc  0 OSU0nU R                  " SSU0UD6$ NrC   r,   r   )
isinstancer6   r,   r7   r   r*   	int_dtypesumr   r   r,   r<   r/   rM   r@   rF   s           r   rW   rW      se     =Juekk::::

++-77LbudmG55(u(((r"   c                    [         R                  " U5      nU[        R                  :X  a  [        R
                  " 5       R                  nUc  0 OSU0nU R                  " SSU0UD6$ rT   )r   r5   r6   r7   r   r*   rV   prodrX   s           r   rZ   rZ      sZ     ''-D

++-77LbudmG66))))r"   c               j    [        X R                  5      nUc  0 OSU0nU R                  " SSU0UD6nU$ rT   )r.   r,   mean)r   r   r,   r<   r/   r@   rF   results           r   r\   r\      s<     5''*ELbudmGVV+%+7+FMr"   c                   Un[        X R                  5      n[        R                  " X5      nUR	                  XS9n	[        R                  " X5      $ N)rC   
correction)r.   r,   r   cast_if_neededstd
r   r   r,   r<   ddofr/   r@   in_dtypetensorr]   s
             r   rb   rb      H     H5''*E!!!+FZZDZ2F11r"   c                   Un[        X R                  5      n[        R                  " X5      nUR	                  XS9n	[        R                  " X5      $ r_   )r.   r,   r   ra   varrc   s
             r   ri   ri     rg   r"   c                   U[         R                  :X  a  [        R                  " 5       R                  nUc  U R
                  n[        R                  " XS9u  u  p[        R                  " XR                  5      nU R                  XS9$ Nr   )r   r,   )r6   r7   r   r*   rV   r,   r   axis_none_flattennormalize_axis_indexr   cumsumr   r   r,   r<   s       r   rn   rn     sk     

++-77}((6JDQ%%dFF3D888++r"   c                   U[         R                  :X  a  [        R                  " 5       R                  nUc  U R
                  n[        R                  " XS9u  u  p[        R                  " XR                  5      nU R                  XS9$ rk   )r6   r7   r   r*   rV   r,   r   rl   rm   r   cumprodro   s       r   rq   rq   .  sk     

++-77}((6JDQ%%dFF3D99$9,,r"   c                  UcI  [        XS9n[        R                  " U R                  5       UR                  5       -  UR                  S9nGOU R                  R
                  (       d  U R                  5       n U R                  UR                  :w  a  Uc  [        S5      eUR                  S:w  a  [        S5      eUR                  S   U R                  U   :w  a  [        S5      e[        R                  " X R                  S-
  S-  UR                  -   5      nUR                  S	U5      n[        R                  " X5      n[        X-  XS9n[        X!US9nX-  nU(       a!  [         R"                  " XQU R                  5      nU(       aK  UR                  UR                  :w  a.  [        R                  " XeR                  5      R%                  5       nXV4$ U$ )
Nr   )r,   z;Axis must be specified when shapes of a and weights differ.r   z81D weights expected when shapes of a and weights differ.r   z5Length of weights not compatible with specified axis.)r   )r\   r6   	as_tensornumelr,   r(   doubler   	TypeErrorr   
ValueErrorbroadcast_toswapaxesr   result_type_implrW   r   apply_keepdimsclone)	r   r   weightsreturnedr/   r]   wsumresult_dtype	numerators	            r   averager   B  s    a#qwwy6<<>9Nww((
A 77gmm#|Q  ||q N  }}Q1774=0 K 
 ((66A:2E2UVG&&r40G $44Q@T>	75! %%fAFF;::%%%dLL9??AD|r"   )interpolationc               D   U(       a   U R                   R                  (       d0  [        R                  " 5       R                  nU R                  U5      n U R                   [        R                  :X  a  U R                  [        R                  5      n Uc#  U R                  5       n UR                  5       nSnO [        R                  " X R                  5      n[        R                  " U5      n[        R                  " XR                   5      n[        R                  " XX%S9$ )Nr   )r   r   )r,   r(   r   r*   r+   r8   r6   float16float32flattenr   r   r   r5   ra   quantile)	r   qr   r<   overwrite_inputmethodr/   r   r,   s	            r   r   r   w  s     
 	77$$++-99DDK 	ww%--DD|IIKIIK))$7
 ''-DQ(A>>!T@@r"   c          
         [         R                  " UR                  5      [        :X  a.  UR	                  [         R
                  " 5       R                  5      nUS-  n[        U UUUUUUS9$ )Ng      Y@)r   r   r   r/   r   )r   python_type_for_torchr,   intr8   r*   r+   r   )	r   r   r   r<   r   r   r/   r   qqs	            r   
percentiler     sc     ))!''2c9DD,,.::;	
UB	
'# r"   c           	     F    [        U [        R                  " S5      UUUUS9$ )Ng      ?)r   r   r<   r/   )r   r6   rt   )r   r   r<   r   r/   s        r   medianr     s,     	' r"   r#   )r   r	   r   r
   r/   r   )NN)r   r	   r   r
   r<   Optional[OutArray]r/   r   )NNF)
r   r	   r   r
   r<   r   r/   r   r@   r   )NNFNN)r   r	   r   r
   r<   r   r/   r   rM   r   r@   r   )NNNFNN)r   r	   r   r
   r,   Optional[DTypeLike]r<   r   r/   r   rM   r   r@   r   )NNNF)r   r	   r   r
   r,   r   r<   r   r/   r   r@   r   )NNNr   F)NNN)r   r	   r   r
   r,   r   r<   r   )r   r	   r~   r	   )NNFlinearF)r   r	   r   r	   r   r
   r<   r   r/   r   r   r   )NNFF)r   r	   r<   r   r/   r   )+__doc__
__future__r   r$   typingr   r   r6    r   r   _normalizationsr	   r
   r   r   r   r   r&   r.   r1   r:   r?   rD   rI   rK   maxrP   minrR   rW   rZ   productr\   rb   ri   rn   rq   
cumproductr   r   r   r   r   r"   r   <module>r      s-  
 #  *  !  . !u ! !  "!
 !!
! 
!
 ! !&  "!
 !!
! 
!
 ! !&  "	
# !%
#
#

# 

# 	
# 
# 
#  "	
# !%
#
#

# 

# 	
# 
# 
#  ""& $
 
 	
       ""& $
 
 	
       "	''
' 
' 	' '  !%""& $))
) ) 
	)
 )  ) ) )$  !%""& $**
* * 
	*
 *  * * *$   !%" !%
  
	
   "  !%"	
2 !%22
2 2 
	2 2 2 2"  !%"	
2 !%22
2 2 
	2 2 2 20 !%"	,,
, , 
	,& !%"	--
- - 
	-" 

 
	1 11 1p "(A )-(A(A(A (A 
	(A (A &(A\ " )-  
	  &: 
" 

 r"   