
    
9jT                    8   S SK Jr  S SKrS SKrS SKrS SKJr  S SKJr  S SKJ	r	  SS jr
SS jrSS jrSS	 jrSS
 jrSS jrSS jrSS jr\R$                  R&                  R)                  SSSS5      rSS jrSS jrSSS.S jrSS jrS S jrSS jrSS jrg)!    )annotationsN)_routines_math)_fusion_thread_local)internalc                    [         R                  " 5       (       aL  U(       a  [        S5      eUc  [        R                  nO[        R
                  n[         R                  " XPXUS9$ U R                  XX45      $ )a  Returns the sum of an array along given axes.

Args:
    a (cupy.ndarray): Array to take sum.
    axis (int or sequence of ints): Axes along which the sum is taken.
    dtype: Data type specifier.
    out (cupy.ndarray): Output array.
    keepdims (bool): If ``True``, the specified axes are remained as axes
        of length one.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.sum`

z3cupy.sum does not support `keepdims` in fusion yet.axisdtypeout)r   	is_fusingNotImplementedError_mathsum_auto_dtype_sum_keep_dtypecall_reductionsumar	   r
   r   keepdimsfuncs         L/home/wildlama/miniconda3/lib/python3.13/site-packages/cupy/_math/sumprod.pyr   r      sp    " %%''%EG G=''D((D#22$6 	6 55c,,    c                    [         R                  " 5       (       aL  U(       a  [        S5      eUc  [        R                  nO[        R
                  n[         R                  " XPXUS9$ U R                  XX45      $ )a  Returns the product of an array along given axes.

Args:
    a (cupy.ndarray): Array to take product.
    axis (int or sequence of ints): Axes along which the product is taken.
    dtype: Data type specifier.
    out (cupy.ndarray): Output array.
    keepdims (bool): If ``True``, the specified axes are remained as axes
        of length one.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.prod`

z4cupy.prod does not support `keepdims` in fusion yet.r   )r   r   r   r   _prod_auto_dtype_prod_keep_dtyper   prodr   s         r   r   r   -   sp    " %%''%FH H=))D))D#22$6 	6 66$s--r   c                T   [         R                  " 5       (       aw  U(       a  [        S5      eU R                  R                  S;   a  [
        R                  nO$Uc  [
        R                  nO[
        R                  n[         R                  " XPXUS9$ [
        R                  " XX#U5      $ )a  Returns the sum of an array along given axes treating Not a Numbers
(NaNs) as zero.

Args:
    a (cupy.ndarray): Array to take sum.
    axis (int or sequence of ints): Axes along which the sum is taken.
    dtype: Data type specifier.
    out (cupy.ndarray): Output array.
    keepdims (bool): If ``True``, the specified axes are remained as axes
        of length one.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.nansum`

z6cupy.nansum does not support `keepdims` in fusion yet.FDr   )r   r   r   r
   charr   _nansum_complex_dtype_nansum_auto_dtype_nansum_keep_dtyper   _nansumr   s         r   nansumr$   M   s    $ %%''%HJ J77<<4..D]++D++D#22$6 	6 ==%h77r   c                    [         R                  " 5       (       aL  U(       a  [        S5      eUc  [        R                  nO[        R
                  n[         R                  " XPXUS9$ [        R                  " XX#U5      $ )a  Returns the product of an array along given axes treating Not a Numbers
(NaNs) as zero.

Args:
    a (cupy.ndarray): Array to take product.
    axis (int or sequence of ints): Axes along which the product is taken.
    dtype: Data type specifier.
    out (cupy.ndarray): Output array.
    keepdims (bool): If ``True``, the specified axes are remained as axes
        of length one.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.nanprod`

z7cupy.nanprod does not support `keepdims` in fusion yet.r   )r   r   r   r   _nanprod_auto_dtype_nanprod_keep_dtyper   _nanprodr   s         r   nanprodr)   p   sr    $ %%''%IK K=,,D,,D#22$6 	6 >>!5x88r   c                b    [         R                  " X[         R                  R                  X#5      $ )as  Returns the cumulative sum of an array along a given axis.

Args:
    a (cupy.ndarray): Input array.
    axis (int): Axis along which the cumulative sum is taken. If it is not
        specified, the input is flattened.
    dtype: Data type specifier.
    out (cupy.ndarray): Output array.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.cumsum`

)r   	scan_corescan_opSCAN_SUMr   r	   r
   r   s       r   cumsumr/      s!      ??1EMM$:$:EGGr   c                b    [         R                  " X[         R                  R                  X#5      $ )a|  Returns the cumulative product of an array along a given axis.

Args:
    a (cupy.ndarray): Input array.
    axis (int): Axis along which the cumulative product is taken. If it is
        not specified, the input is flattened.
    dtype: Data type specifier.
    out (cupy.ndarray): Output array.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.cumprod`

)r   r+   r,   	SCAN_PRODr.   s       r   cumprodr2      s!      ??1EMM$;$;UHHr   c                ,    [        U SUS9n [        XX#S9$ )a  Returns the cumulative sum of an array along a given axis treating Not a
Numbers (NaNs) as zero.

Args:
    a (cupy.ndarray): Input array.
    axis (int): Axis along which the cumulative sum is taken. If it is not
        specified, the input is flattened.
    dtype: Data type specifier.
    out (cupy.ndarray): Output array.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.nancumsum`
r   r   r   )_replace_nanr/   r.   s       r   	nancumsumr6      s      	Qs#A!e55r   c                ,    [        U SUS9n [        XX#S9$ )a  Returns the cumulative product of an array along a given axis treating
Not a Numbers (NaNs) as one.

Args:
    a (cupy.ndarray): Input array.
    axis (int): Axis along which the cumulative product is taken. If it is
        not specified, the input is flattened.
    dtype: Data type specifier.
    out (cupy.ndarray): Output array.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.nancumprod`
   r4   r   )r5   r2   r.   s       r   
nancumprodr9      s      	Qs#A1u66r   z
T a, T valzT outz(if (a == a) {out = a;} else {out = val;}cupy_replace_nanc                    Ub  U R                   UR                   :w  a  [        R                  " U 5      n[        XU5        U$ N)r
   cupy
empty_like_replace_nan_kernel)r   valr   s      r   r5   r5      s3    
{agg*ooa $Jr   r8   c                V   US:X  a  U $ US:  a  [        S[        U5      -   5      e[        R                  " U 5      n U R                  n[
        R                  " X%5      n/ nUbp  [        R                  " U5      nUR                  S:X  a9  [        U R                  5      nSXr'   [        R                  " U[        U5      5      nUR                  U5        UR                  U 5        Ubp  [        R                  " U5      nUR                  S:X  a9  [        U R                  5      nSXr'   [        R                  " U[        U5      5      nUR                  U5        [        U5      S:  a  [        R                  " Xb5      n [        S5      /U-  n[        S5      /U-  n	[        SS5      X'   [        SS5      X'   [        U5      n[        U	5      n	U R                  [         R"                  :X  a  [        R$                  O[        R&                  n
[)        U5       H  nU
" X   X	   5      n M     U $ )a  Calculate the n-th discrete difference along the given axis.

Args:
    a (cupy.ndarray): Input array.
    n (int): The number of times values are differenced. If zero, the input
        is returned as-is.
    axis (int): The axis along which the difference is taken, default is
        the last axis.
    prepend (int, float, cupy.ndarray): Value to prepend to ``a``.
    append (int, float, cupy.ndarray): Value to append to ``a``.

Returns:
    cupy.ndarray: The result array.

.. seealso:: :func:`numpy.diff`
r   z#order must be non-negative but got Nr8   )
ValueErrorreprr=   
asanyarrayndimr   _normalize_axis_indexlistshapebroadcast_totupleappendlenconcatenateslicer
   numpybool_	not_equalsubtractrange)r   nr	   prependrL   ndcombinedrI   slice1slice2op_s               r   diffr]      s   $ 	Av1u1DG;= 	= 	A	
B))$3DH//'*<<1MEEK''u>G OOA(;;!MEEK&&vuU|<F
8}qX,Dk]RFDk]RFD>FLr?FL6]F6]F77ekk1t}}B1Xqy!)$  Hr   )r	   
edge_orderc                  [         R                  " U 5      n U R                  n[        R                  " XSS9n[        U5      n[        U5      nUS:X  a  S/U-  nGOPUS:X  a#  [         R                  " US   5      S:X  a  X6-  nGO'Xv:X  Ga  [        U5      n[        U5       H  u  p[         R                  " U
5      S:X  a  M!  [         R                  " U
5      S:w  a  [        S5      e[        U
5      U R                  XY      :w  a  [        S5      e[        R                  " U
R                  [        R                  5      (       a  U
R                  [        R                  5      n
[         R                   " U
5      nXS   :H  R#                  5       (       a  US   nXU	'   M     O[%        S5      eUS	:  a  [        S
5      e/ n['        S5      /U-  n['        S5      /U-  n['        S5      /U-  n['        S5      /U-  nU R                  n[        R                  " U[        R(                  5      (       a  OY[        R                  " U[        R                  5      (       a  U R                  [        R                  5      n [        R                  n[+        XX5       GHd  u  nnU R                  U   US-   :  a  [        S5      e[         R,                  " U US9n[         R                  " U5      S:H  n['        SS5      X'   ['        SS5      X'   ['        SS5      X'   ['        S	S5      UU'   U(       a0  U [/        U5         U [/        U5         -
  SU-  -  U[/        U5      '   OUSS nUSS nUU-   nU* UU-  -  nUU-
  UU-  -  nUUU-  -  nS/U-  nSUU'   [/        U5      =Ul	        =Ul	        Ul	        UU [/        U5         -  UU [/        U5         -  -   UU [/        U5         -  -   U[/        U5      '   US:X  a  SX'   SX'   SX'   U(       a  UOUS   nU [/        U5         U [/        U5         -
  U-  U[/        U5      '   SX'   SX'   SX'   U(       a  UOUS   nU [/        U5         U [/        U5         -
  U-  U[/        U5      '   GO/SX'   SX'   SX'   S	UU'   U(       a  SU-  nSU-  nSU-  nO/US   nUS   nUU-   nSU-  U-   * UU-  -  nUUU-  -  nU* UU-  -  nUU [/        U5         -  UU [/        U5         -  -   UU [/        U5         -  -   U[/        U5      '   SX'   SX'   SX'   SUU'   U(       a  SU-  nSU-  nSU-  nO.US   nUS   nUU-   nUUU-  -  nU* UU-  -  nSU-  U-   UU-  -  nUU [/        U5         -  UU [/        U5         -  -   UU [/        U5         -  -   U[/        U5      '   UR1                  U5        ['        S5      X'   ['        S5      X'   ['        S5      X'   ['        S5      UU'   GMg     US:X  a  US   $ U$ )am  Return the gradient of an N-dimensional array.

The gradient is computed using second order accurate central differences
in the interior points and either first or second order accurate one-sides
(forward or backwards) differences at the boundaries.
The returned gradient hence has the same shape as the input array.

Args:
    f (cupy.ndarray): An N-dimensional array containing samples of a scalar
        function.
    varargs (list of scalar or array, optional): Spacing between f values.
        Default unitary spacing for all dimensions. Spacing can be
        specified using:

        1. single scalar to specify a sample distance for all dimensions.
        2. N scalars to specify a constant sample distance for each
           dimension. i.e. `dx`, `dy`, `dz`, ...
        3. N arrays to specify the coordinates of the values along each
           dimension of F. The length of the array must match the size of
           the corresponding dimension
        4. Any combination of N scalars/arrays with the meaning of 2. and
           3.

        If `axis` is given, the number of varargs must equal the number of
        axes. Default: 1.
    edge_order ({1, 2}, optional): The gradient is calculated using N-th
        order accurate differences at the boundaries. Default: 1.
    axis (None or int or tuple of ints, optional): The gradient is
        calculated only along the given axis or axes. The default
        (axis = None) is to calculate the gradient for all the axes of the
        input array. axis may be negative, in which case it counts from the
        last to the first axis.

Returns:
    gradient (cupy.ndarray or list of cupy.ndarray): A set of ndarrays
    (or a single ndarray if there is only one dimension) corresponding
    to the derivatives of f with respect to each dimension. Each
    derivative has the same shape as f.

.. seealso:: :func:`numpy.gradient`
F)	sort_axesr         ?r8   z&distances must be either scalars or 1dzGwhen 1d, distances must match the length of the corresponding dimensionzinvalid number of arguments   z)'edge_order' greater than 2 not supportedNzlShape of array too small to calculate a numerical gradient, at least (edge_order + 1) elements are required.r
   rB          @g      g      g      ?g       g      ?)r=   rE   rF   r   _normalize_axis_indicesrM   rH   	enumeraterC   rI   rP   
issubdtyper
   integerastypefloat64r]   all	TypeErrorrO   inexactzipr>   rK   rL   )fr	   r^   varargsrF   axeslen_axesrU   dxi	distancesdiffxoutvalsrY   rZ   slice3slice4otypeax_dxr   uniform_spacingdx1dx2dx_sumr   bcrI   dx_0dx_ns                                 r   gradientr   ,  sQ   T 	A66D++D%HD4yHGAAvUX	
aDIIgaj)Q.	
']%bMLAyy#q(9%* !IJJ9~!11 @  	?? &,,U]];	IIi(E q!&&((aqE' ** 566A~DEE
 G Dk]T!FDk]T!FDk]T!FDk]T!FGGEu}}--
 E5==11'A4}e774=:>)C 
 ooau- ))E*a/ Q|T2Q|Q~t"#E&M"2QuV}5E"Ee"Cf "+C)C3YF#,'AssSy)AsV|$AC$JEE$K*/,6AG6ag"#af&6"6"#af&6"6#7"#af&6"6#7Cf
 ?FLFLFL+5qD"#E&M"2QuV}5E"E!MCfFLFLFL+5rD"#E&M"2QuV}5E"E!MCf FLFLFLF4L5L%K5LAhAhsCi#o&#.9cCi(DC6N+"#af&6"6"#af&6"6#7"#af&6"6#7Cf FLFLFLF4L%K5L%KBiBisSF^,GsSy)3Y_8"#af&6"6"#af&6"6#7"#af&6"6#7Cf 	s T{T{T{T{tK %N 1}qzr   c                \   [        U [        R                  5      (       d  [        S5      eU R	                  5       n U R
                  nUc  Uc  U SS U SS -
  $ Uc  SnOj[        U[        R                  5      (       d  [        S5      e[        R                  " X#SS9(       d  [        S	5      eUR	                  5       n[        U5      nUc  SnOj[        U[        R                  5      (       d  [        S
5      e[        R                  " XSS9(       d  [        S5      eUR	                  5       n[        U5      n[        [        U 5      S-
  S5      n[        R                  " Xd-   U-   U R
                  S9nUS:  a  X'SU& US:  a  XXF-   S& [        R                  " U SS U SS XtXF-    5        U$ )a  
Calculates the difference between consecutive elements of an array.

Args:
    arr (cupy.ndarray): Input array.
    to_end (cupy.ndarray, optional): Numbers to append at the end
        of the returned differences.
    to_begin (cupy.ndarray, optional): Numbers to prepend at the
        beginning of the returned differences.

Returns:
    cupy.ndarray: New array consisting differences among succeeding
    elements.

.. seealso:: :func:`numpy.ediff1d`
z$`arr` should be of type cupy.ndarrayNr8   rB   r   z)`to_begin` should be of type cupy.ndarray	same_kind)castingzSdtype of `to_begin` must be compatible with input `arr` under the `same_kind` rule.z'`to_end` should be of type cupy.ndarrayzQdtype of `to_end` must be compatible with input `arr` under the `same_kind` rule.rc   )
isinstancer=   ndarrayrn   ravelr
   can_castrM   maxemptyrS   )arrto_endto_begin	dtype_reql_beginl_endl_diffresults           r   ediff1dr     s   " c4<<((>?? ))+C 		I FN12wSb!!(DLL11GHH}}X+F K L L >>#h-~&$,,//EFF}}VD K L L F SAq!FZZ(50		BF {#xqy$*w !MM#ab'3s8VG4D%EFMr   c                   [        U [        R                  5      (       d  [        S5      eUc  UnO[        U[        R                  5      (       d  [        S5      eUR                  S:X  a>  [        U5      nS/U R                  -  nUR                  S   XS'   UR                  U5      nO	[        XS9nU R                  n[        S5      /U-  n[        S5      /U-  n[        SS5      Xs'   [        SS5      X'   X@[        U5         U [        U5         -   -  S-  n	 U	R                  U5      n
U
$ ! [         a#    [        R                  R                  X5      n
 U
$ f = f)	aL  
Integrate along the given axis using the composite trapezoidal rule.
Integrate `y` (`x`) along the given axis.

Args:
    y (cupy.ndarray): Input array to integrate.
    x (cupy.ndarray): Sample points over which to integrate. If None equal
        spacing `dx` is assumed.
    dx (float): Spacing between sample points, used if `x` is None, default
        is 1.
    axis (int): The axis along which the integral is taken, default is
        the last axis.

Returns:
    cupy.ndarray: Definite integral as approximated by the trapezoidal
    rule.

.. seealso:: :func:`numpy.trapezoid`
z"`y` should be of type cupy.ndarrayNz"`x` should be of type cupy.ndarrayr8   r   )r	   rB   re   )r   r=   r   rn   rF   r]   rI   reshaperO   rK   r   rC   addreduce)yxru   r	   drI   rW   rY   rZ   productrets              r   	trapezoidr   H  s;   ( a&&<==y!T\\**@AA66Q;QAC!&&LE''!*EK		% AQ"A	
BDk]RFDk]RFD>FLr?FLU6]#af&667#=G-kk$ J  -hhoog,J-s   D. .)EEc                R    [         R                  " S[        5        [        XX#U5      $ )NzPlease use `prod` instead.)warningswarnDeprecationWarningr   )r   r	   r
   r   r   s        r   r   r   y  s!    MM.0BCX..r   c                P    [         R                  " S[        5        [        XX#5      $ )NzPlease use `cumprod` instead.)r   r   r   r2   r.   s       r   
cumproductr   ~  s    MM13EF1E''r   )NNNF)NNNr<   )r8   rB   NN)NN)Nra   rB   )
__future__r   r   rP   r=   
cupy._corer   r   r   r   r   r   r$   r)   r/   r2   r6   r9   _core_kernelElementwiseKernelr?   r5   r]   r   r   r   r   r    r   r   <module>r      s    "    . + -@.@ 8F9BH&I&6(7( jj((::'E 
>B  $ Sl@L.b/
(r   