
    ёif                       % S SK Jr  S SKrS SKJrJr  S SKJrJr  S SK	r	S SK	J
r
  S SKJrJr  S SKJrJrJr  SS	KJrJr  SS
KJr  SSKJrJrJr  SSKJr  SSKJr  \(       a  S SKJ r   S SK	J!r!  S SK"J#r#  \S   r$S\%S'   / r&\" SS/SS/5         S-SSS.             S.S jjj5       r'\" S/S/S.5          S/SSS.               S0S jjj5       r(    S1           S2S jjr)S3S4S jjr*\   S5           S6S" jj5       r+\    S7           S8S# jj5       r+    S9S$ jr+\    S7S S%.             S:S& jjj5       r,\    S7           S;S' jj5       r,\" SS/SS/S!S(/5          S9SS%.S) jj5       r,     S<               S=S* jjr-\" SS/SS/5          S>SS%.               S?S+ jjj5       r.   S@           SAS, jjr/g)B    )annotationsN)TYPE_CHECKINGLiteral)	TypeAliasoverload)_C_ops)in_dynamic_modein_dynamic_or_pir_mode)ParamAliasDecoratorparam_two_aliasparam_two_alias_one_default   )
check_typecheck_variable_and_dtype)Variable)LayerHelperconvert_np_dtype_to_dtype_core   )cast)_get_reduce_axis_with_tensor)Sequence)Tensor)	DTypeLike)linearhigherlowermidpointnearestr   _Interpolationxinputaxisdim)dtypeoutc                  Ub_  [        U[        R                  R                  [        R                  45      (       d  [        U5      nU R                  U:w  a  [        X5      n [        5       (       a  [        R                  " XX%S9$ [        X5      u  pa[        U S/ SQS5        [        US[        [        [         ["        4S5        [        U[        [         45      (       a!  U H  n[        US[        ["        4S5        M     [%        S0 ['        5       D6nXUS.n	UR)                  U R                  5      n
UR+                  SS	U 0S
U
0U	S9  U
$ )ap
  
Computes the mean of the input tensor's elements along ``axis``.

Args:
    x (Tensor): The input Tensor with data type bool, bfloat16, float16, float32,
        float64, int32, int64, complex64, complex128.
        alias: ``input``
    axis (int|list|tuple|None, optional): The axis along which to perform mean
        calculations. ``axis`` should be int, list(int) or tuple(int). If
        ``axis`` is a list/tuple of dimension(s), mean is calculated along
        all element(s) of ``axis`` . ``axis`` or element(s) of ``axis``
        should be in range [-D, D), where D is the dimensions of ``x`` . If
        ``axis`` or element(s) of ``axis`` is less than 0, it works the
        same way as :math:`axis + D` . If ``axis`` is None, mean is
        calculated over all elements of ``x``. Default is None.
        alias: ``dim``
    keepdim (bool, optional): Whether to reserve the reduced dimension(s)
        in the output Tensor. If ``keepdim`` is True, the dimensions of
        the output Tensor is the same as ``x`` except in the reduced
        dimensions(it is of size 1 in this case). Otherwise, the shape of
        the output Tensor is squeezed in ``axis`` . Default is False.
    name (str|None, optional): Name for the operation (optional, default is None).
        For more information, please refer to :ref:`api_guide_Name`.
    dtype (str): The desired data type of returned tensor. Default: None.
    out(Tensor|None, optional): The output tensor. Default: None.

Returns:
    Tensor, results of average along ``axis`` of ``x``, with the same data
    type as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[[1., 2., 3., 4.],
        ...                        [5., 6., 7., 8.],
        ...                        [9., 10., 11., 12.]],
        ...                       [[13., 14., 15., 16.],
        ...                        [17., 18., 19., 20.],
        ...                        [21., 22., 23., 24.]]])
        >>> out1 = paddle.mean(x)
        >>> print(out1.numpy())
        12.5
        >>> out2 = paddle.mean(x, axis=-1)
        >>> print(out2.numpy())
        [[ 2.5  6.5 10.5]
         [14.5 18.5 22.5]]
        >>> out3 = paddle.mean(x, axis=-1, keepdim=True)
        >>> print(out3.numpy())
        [[[ 2.5]
          [ 6.5]
          [10.5]]
         [[14.5]
          [18.5]
          [22.5]]]
        >>> out4 = paddle.mean(x, axis=[0, 2])
        >>> print(out4.numpy())
        [ 8.5 12.5 16.5]
        >>> out5 = paddle.mean(x, dtype='float64')
        >>> out5
        Tensor(shape=[], dtype=float64, place=Place(gpu:0), stop_gradient=True,
            12.50000000)
r&   zx/input)	booluint16float16float32float64int32int64	complex64
complex128zmean/reduce_meanzaxis/dimzelements of axis/dim)r$   keep_dim
reduce_allreduce_meanXOuttypeinputsoutputsattrs)mean)
isinstancer   VarDescVarTypeDataTyper   r%   r   r
   r   r<   r   r   r   intlisttupler   r   locals"create_variable_for_type_inference	append_op)r!   r#   keepdimnamer%   r&   r3   itemhelperr;   
out_tensors              R/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/tensor/stat.pyr<   r<   4   s:   T %$,,"6"6!FGG.u5E77eQA{{1G557@
 
 	
  	*sD%:<N	
 dT5M***(O&	  0vx0L>>qwwG
8J'	 	 	
     )r!   r#   )
correctionr&   c                  Ub  US:w  a  [        S5      eUb  U(       a  SOSnO[        U5      n[        5       (       d  [        U S/ SQS5        [	        XSU5      nU R
                  [        R                  :X  a  [        R                  OU R
                  n	[        R                  " [        R                  " X-
  S	5      XXIS
9n
[        R                  " [        R                  " U 5      S5      [        R                  " [        R                  " U
5      S5      -  nUR                  U	5      nUS:w  a}  X-
  n[        R                  " U[        R                  " U5      5      n[        R                  " 5       (       a3  [        R                   " US:*  5      (       a  ["        R$                  " SS	S9  OUnSUl        X-  n
S nSU R(                  ;   a  U" U
5      n
[+        U R(                  5      S:X  a%  US:X  a  [        R,                  " SU
R&                  S9n
U
R
                  U R
                  :w  a  U
R                  U R
                  5      nOU
nUb  [        R.                  " X5        U$ U$ )a  
Computes the variance of ``x`` along ``axis`` .

.. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x``, and ``dim`` can be used as an alias for ``axis``.
    For example, ``var(input=tensor_x, dim=1, ...)`` is equivalent to ``var(x=tensor_x, axis=1, ...)``.

Args:
    x (Tensor): The input Tensor with data type float16, float32, float64.
        alias: ``input``.
    axis (int|list|tuple|None, optional): The axis along which to perform variance calculations. ``axis`` should be int, list(int) or tuple(int).
        alias: ``dim``.

        - If ``axis`` is a list/tuple of dimension(s), variance is calculated along all element(s) of ``axis`` . ``axis`` or element(s) of ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
        - If ``axis`` or element(s) of ``axis`` is less than 0, it works the same way as :math:`axis + D` .
        - If ``axis`` is None, variance is calculated over all elements of ``x``. Default is None.

    unbiased (bool, optional): Whether to use the unbiased estimation. If ``unbiased`` is True, the divisor used in the computation is :math:`N - 1`, where :math:`N` represents the number of elements along ``axis`` , otherwise the divisor is :math:`N`. Default is True.
    keep_dim (bool, optional): Whether to reserve the reduced dimension in the output Tensor. The result tensor will have one fewer dimension than the input unless keep_dim is true. Default is False.
    name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
    correction (int|float, optional): Difference between the sample size and sample degrees of freedom.
        Defaults to 1 (Bessel's correction). If unbiased is specified, this parameter is ignored.
    out (Tensor|None, optional): Output tensor. Default is None.

Returns:
    Tensor, results of variance along ``axis`` of ``x``, with the same data type as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[1.0, 2.0, 3.0], [1.0, 4.0, 5.0]])
        >>> out1 = paddle.var(x)
        >>> print(out1.numpy())
        2.6666667
        >>> out2 = paddle.var(x, axis=1)
        >>> print(out2.numpy())
        [1.         4.3333335]
r   z0Only one of unbiased and correction may be giveng      ?g        r!   r+   r,   r-   varTr   )rG   rH   r%   r/   r   zDegrees of freedom is <= 0.)
stacklevelc                    [         R                  " U R                  5       SS9n[         R                  " U R	                  5       US[        S5      5      R                  U R                  5      nU$ )Nr/   r%   r   nan)paddlearangenumel
index_fillflattenfloatreshapeshape)r&   indicesout_nans      rL   _replace_nanvar.<locals>._replace_nan  sS    --		7;##KKM7AuU|

'#))
 	 rM   )stop_gradient)
ValueErrorr[   r	   r   r<   r%   rV   r+   r,   sumpowr   rX   astypemaximum
zeros_likeanywarningswarnrb   r]   len	to_tensorassign)r!   r#   unbiasedrG   rH   rN   r&   actual_correctionur%   rK   ncorrected_nr`   results                  rL   rQ   rQ      s   f 
aKLL#+C!*- s5u	
 	QdD!Agg7FNNQWWE

AEADJ 	FLLOW-Z '1 	A 	
AA+nn**;7
 !!##

;!3C(D(DMM7AF $KJ 	AGG|!*-

177|q.!3%%az7O7OP
177"""177+
f"
MrM   c                    [        5       (       d  [        U S/ SQS5        [        S0 [        5       D6n[        R
                  " U5      $ )a  
Computes the standard-deviation of ``x`` along ``axis`` .

Args:
    x (Tensor): The input Tensor with data type float16, float32, float64.
    axis (int|list|tuple|None, optional): The axis along which to perform
        standard-deviation calculations. ``axis`` should be int, list(int)
        or tuple(int). If ``axis`` is a list/tuple of dimension(s),
        standard-deviation is calculated along all element(s) of ``axis`` .
        ``axis`` or element(s) of ``axis`` should be in range [-D, D),
        where D is the dimensions of ``x`` . If ``axis`` or element(s) of
        ``axis`` is less than 0, it works the same way as :math:`axis + D` .
        If ``axis`` is None, standard-deviation is calculated over all
        elements of ``x``. Default is None.
    unbiased (bool, optional): Whether to use the unbiased estimation. If
        ``unbiased`` is True, the standard-deviation is calculated via the
        unbiased estimator. If ``unbiased`` is True,  the divisor used in
        the computation is :math:`N - 1`, where :math:`N` represents the
        number of elements along ``axis`` , otherwise the divisor is
        :math:`N`. Default is True.
    keepdim (bool, optional): Whether to reserve the reduced dimension(s)
        in the output Tensor. If ``keepdim`` is True, the dimensions of
        the output Tensor is the same as ``x`` except in the reduced
        dimensions(it is of size 1 in this case). Otherwise, the shape of
        the output Tensor is squeezed in ``axis`` . Default is False.
    name (str|None, optional): Name for the operation (optional, default is None).
        For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, results of standard-deviation along ``axis`` of ``x``, with the
    same data type as ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor([[1.0, 2.0, 3.0], [1.0, 4.0, 5.0]])
        >>> out1 = paddle.std(x)
        >>> print(out1.numpy())
        1.6329932
        >>> out2 = paddle.std(x, unbiased=False)
        >>> print(out2.numpy())
        1.490712
        >>> out3 = paddle.std(x, axis=1)
        >>> print(out3.numpy())
        [1.       2.081666]

r!   rP   std )r
   r   rQ   rD   rV   sqrt)r!   r#   ro   rG   rH   r&   s         rL   rv   rv      s>    p "## s5u	
 //C;;srM   c                B   [        5       (       a  [        R                  " U 5      $ [        U [        5      (       d  [        S5      e[        S0 [        5       D6nUR                  [        R                  R                  R                  S9nUR                  SSU 0SU0S9  U$ )a  
Returns the number of elements for a tensor, which is a 0-D int64 Tensor with shape [].

Args:
    x (Tensor): The input Tensor, it's data type can be bool, float16, float32, float64, uint8, int8, int32, int64, complex64, complex128.
    name (str|None, optional): Name for the operation (optional, default is None).
        For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor: The number of elements for the input Tensor, whose shape is [].

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.full(shape=[4, 5, 7], fill_value=0, dtype='int32')
        >>> numel = paddle.numel(x)
        >>> print(numel.numpy())
        140


zx must be a Tensor in numelrT   sizeInputr6   )r8   r9   r:   )rX   )r
   r   rX   r=   r   	TypeErrorr   rD   rE   r   r>   r?   INT64rF   )r!   rH   rJ   r&   s       rL   rX   rX   `  s    0 ||A!X&&9::1177,,&&,, 8 
 	fgq\E3<P
rM   .modec                    g Nrw   r!   r#   rG   r~   rH   s        rL   	nanmedianr     s      rM   c                    g r   rw   r   s        rL   r   r          rM   c                V   [        U [        [        R                  R                  45      (       d  [        S5      e[        U[        [        45      (       a  [        U5      S:X  a  [        S5      eUS;  a  [        SU S35      eUSL=(       a    [        U[        [        45      (       + nUc  / nO9[        U[        5      (       a  [        U5      nO[        U[        5      (       a  U/n[        5       (       a!  [        R                  " XX#5      u  pgSUl        O[        U S	/ S
QS5        [!        S0 [#        5       D6nXUS.n	UR%                  U R&                  5      nUR%                  [        R(                  5      nUR+                  SS	U 0XgS.U	S9  SUl        US:X  a
  U(       a  Xg4$ U$ )a[
  
Compute the median along the specified axis, while ignoring NaNs.

If the valid count of elements is a even number,
the average value of both elements in the middle is calculated as the median.

Args:
    x (Tensor): The input Tensor, it's data type can be int32, int64, float16, bfloat16, float32, float64.
    axis (None|int|list|tuple, optional):
        The axis along which to perform median calculations ``axis`` should be int or list of int.
        ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
        If ``axis`` is less than 0, it works the same way as :math:`axis + D`.
        If ``axis`` is None, median is calculated over all elements of ``x``. Default is None.
    keepdim (bool, optional): Whether to reserve the reduced dimension(s)
        in the output Tensor. If ``keepdim`` is True, the dimensions of
        the output Tensor is the same as ``x`` except in the reduced
        dimensions(it is of size 1 in this case). Otherwise, the shape of
        the output Tensor is squeezed in ``axis`` . Default is False.
    mode (str, optional): Whether to use mean or min operation to calculate
        the nanmedian values when the input tensor has an even number of non-NaN elements
        along the dimension ``axis``. Support 'avg' and 'min'. Default is 'avg'.
    name (str|None, optional): Name for the operation (optional, default is None).
        For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor or tuple of Tensor. If ``mode`` == 'min' and ``axis`` is int, the result
    will be a tuple of two tensors (nanmedian value and nanmedian index). Otherwise,
    only nanmedian value will be returned.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> x = paddle.to_tensor([[float('nan'), 2. , 3. ], [0. , 1. , 2. ]])

        >>> y1 = x.nanmedian()
        >>> print(y1.numpy())
        2.0

        >>> y2 = x.nanmedian(0)
        >>> print(y2.numpy())
        [0.  1.5 2.5]

        >>> y3 = x.nanmedian(0, keepdim=True)
        >>> print(y3.numpy())
        [[0.  1.5 2.5]]

        >>> y4 = x.nanmedian((0, 1))
        >>> print(y4.numpy())
        2.0

        >>> y5 = x.nanmedian(mode='min')
        >>> print(y5.numpy())
        2.0

        >>> y6, y6_index = x.nanmedian(0, mode='min')
        >>> print(y6.numpy())
        [0. 1. 2.]
        >>> print(y6_index.numpy())
        [1 1 1]

        >>> y7, y7_index = x.nanmedian(1, mode='min')
        >>> print(y7.numpy())
        [2. 1.]
        >>> print(y7_index.numpy())
        [1 1]

        >>> y8 = x.nanmedian((0,1), mode='min')
        >>> print(y8.numpy())
        2.0
*In median, the input x should be a Tensor.r   Axis list should not be empty.avgminMode & is not supported. Must be avg or min.NTr5   )r.   r/   r+   r,   r-   r*   r   )r#   rG   r~   )r6   MedianIndexr7   r   )r   )r=   r   rV   pirValuer|   rB   rC   rl   rc   rA   r
   r   r   rb   r   r   rD   rE   r%   r/   rF   )
r!   r#   rG   r~   rH   
need_indexr&   r^   rJ   r;   s
             rL   r   r     s   \ a(FJJ$4$4566DEE$u&&3t9>9::>!5&LMNNd"MZtUm-L)LJ|	D%	 	 Dz	D#		v''? $ I		
 5FH54@77@;;FLLI88	 	 	
 !%u}|
rM   r(   c                   g r   rw   )r!   r#   rG   r~   rH   r&   s         rL   medianr     s      rM   c                    g r   rw   r   s        rL   r   r      r   rM   r   c                  [        U [        [        R                  R                  45      (       d  [        S5      e[        U[        [        45      (       a  [        U5      S:X  a  [        S5      e[        U R                  5      nUS:X  a  US;   d   S5       eO.Ub+  [        U[        5      (       a  X:  a  X* :  d  [        S5      eUS;  a  [        S	U S
35      eUSLnUc  SnUc  / nO[        U[        5      (       a  U/nUS:X  a=  U R                  [        R                  :X  d  U R                  [        R                  5      n [         R"                  " XX#US9u  pSU
l        US:X  a
  U(       a  X4$ U	$ )a  
Compute the median along the specified axis.

.. note::
    Alias Support: The parameter name ``input`` can be used as an alias for ``x``, and ``dim`` can be used as an alias for ``axis``.
    When an alias replacement occurs, the default parameter for mode setting is min instead of avg.
    For example, ``median(input=tensor_x, dim=1, ...)`` is equivalent to ``median(x=tensor_x, axis=1, ...)``.

Args:
    x (Tensor): The input Tensor, it's data type can be bfloat16, float16, float32, float64, int32, int64.
        alias: ``input``.
    axis (int|None, optional): The axis along which to perform median calculations ``axis`` should be int.
        alias: ``dim``.
        ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
        If ``axis`` is less than 0, it works the same way as :math:`axis + D`.
        If ``axis`` is None, median is calculated over all elements of ``x``. Default is None.
    keepdim (bool, optional): Whether to reserve the reduced dimension(s)
        in the output Tensor. If ``keepdim`` is True, the dimensions of
        the output Tensor is the same as ``x`` except in the reduced
        dimensions(it is of size 1 in this case). Otherwise, the shape of
        the output Tensor is squeezed in ``axis`` . Default is False.
    mode (str, optional): Whether to use mean or min operation to calculate
        the median values when the input tensor has an even number of elements
        in the dimension ``axis``. Support 'avg' and 'min'. Default is 'avg'.
        When an alias replacement occurs, the default parameter for mode setting is min instead of avg.
    name (str|None, optional): Name for the operation (optional, default is None).
        For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor or tuple of Tensor.
    If ``mode`` == 'avg', the result will be the tensor of median values;
    If ``mode`` == 'min' and ``axis`` is None, the result will be the tensor of median values;
    If ``mode`` == 'min' and ``axis`` is not None, the result will be a tuple of two tensors
    containing median values and their indices.

    When ``mode`` == 'avg', if data type of ``x`` is float64, data type of median values will be float64,
    otherwise data type of median values will be float32.
    When ``mode`` == 'min', the data type of median values will be the same as ``x``. The data type of
    indices will be int64.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import numpy as np

        >>> x = paddle.arange(12).reshape([3, 4])
        >>> print(x)
        Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[0 , 1 , 2 , 3 ],
         [4 , 5 , 6 , 7 ],
         [8 , 9 , 10, 11]])

        >>> y1 = paddle.median(x)
        >>> print(y1)
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        5.50000000)

        >>> y2 = paddle.median(x, axis=0)
        >>> print(y2)
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [4., 5., 6., 7.])

        >>> y3 = paddle.median(x, axis=1)
        >>> print(y3)
        Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
        [1.50000000, 5.50000000, 9.50000000])

        >>> y4 = paddle.median(x, axis=0, keepdim=True)
        >>> print(y4)
        Tensor(shape=[1, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[4., 5., 6., 7.]])

        >>> y5 = paddle.median(x, mode='min')
        >>> print(y5)
        Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
        5)

        >>> median_value, median_indices = paddle.median(x, axis=1, mode='min')
        >>> print(median_value)
        Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [1, 5, 9])
        >>> print(median_indices)
        Tensor(shape=[3], dtype=int64, place=Place(cpu), stop_gradient=True,
        [1, 1, 1])

        >>> # cases containing nan values
        >>> x = paddle.to_tensor(np.array([[1,float('nan'),3,float('nan')],[1,2,3,4],[float('nan'),1,2,3]]))

        >>> y6 = paddle.median(x, axis=-1, keepdim=True)
        >>> print(y6)
        Tensor(shape=[3, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[nan       ],
         [2.50000000],
         [nan       ]])

        >>> median_value, median_indices = paddle.median(x, axis=1, keepdim=True, mode='min')
        >>> print(median_value)
        Tensor(shape=[3, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
        [[nan],
         [2. ],
         [nan]])
        >>> print(median_indices)
        Tensor(shape=[3, 1], dtype=int64, place=Place(cpu), stop_gradient=True,
        [[1],
         [1],
         [0]])
r   r   r   )r   Nz8when input 0-D, axis can only be [-1, 0] or default NoneNzJIn median, axis should be none or an integer in range [-rank(x), rank(x)).r   r   r   Tr   r(   r   )r=   r   rV   r   r   r|   rB   rC   rl   rc   r]   rA   r%   r-   rf   r,   r   r   rb   )r!   r#   rG   r~   rH   r&   dimsneed_idx
is_flattenvaluesr^   s              rL   r   r   *  sf   l a(FJJ$4$4566DEE$u&&3t9>9::qww<Dqy 
 
 	F F		F 

 
	$$$T[TU]\  >!5&LMNN4H|
|	D#		vu}QWW6HHV^^$mmAWDOF Gu}rM   c                B	  ^ ^^^ [        T [        [        R                  R                  45      (       d  [        S5      e[        U[        [        45      (       a  U/nO[        U[        [        45      (       a  [        U5      S::  a  [        S5      eO{[        U[        [        R                  R                  45      (       aA  [        UR                  5      S:  a  [        S5      e[        UR                  5      S:X  a  U/nO[        S5      eU HZ  n[        5       (       d1  [        U[        [        R                  R                  45      (       a    OUS:  d  US:  d  MQ  [        S5      e   TS;  a  [        S	T 35      e[        T R                  5      n[        T R                  5      n	Tc   [        R                  " T 5      m SmS/U-  n	GO*[        T[        5      (       a  / / pT HM  n[        U[        5      (       a  X:  a  X* :  d  [        S
5      eUS:  a  X-   nU
R                  U5        SX'   MO     [        [!        [        T5      * S5      5      n[        R"                  " T X5      m [        U5      S:X  a  [        R                  " T 5      m SmOa[        R                  " T US   US   5      m US   mO=[        T[        5      (       a  TU:  a  TU* :  d  [        S
5      eTS:  a  TU-  mSU	T'   T R%                  5       nUR'                  5       R)                  TSS9n/ nU H  n[+        5       (       a  [        R,                  " UT R.                  S9nU(       a  UR                  X~S-
  -  5        MP  X~S-
  -  nT R                  T   S-
  n[        R0                  " UUS9n[        R2                  " UR5                  TSS9UU5      nUR                  U5        M     [        R6                  " T T5      mUUUU 4S jn/ nU HJ  nU" U5      nU(       d  [        R8                  " UTS9nOUR;                  U	5      nUR                  U5        ML     [        U5      S:  a  [        R<                  " US5      nOUS   nUb  [        R>                  " UU5        U$ U$ )a  
Compute the quantile of the input along the specified axis.

Args:
    x (Tensor): The input Tensor, it's data type can be float32, float64, int32, int64.
    q (int|float|list|Tensor): The q for calculate quantile, which should be in range [0, 1]. If q is a list or
        a 1-D Tensor, each element of q will be calculated and the first dimension of output is same to the number of ``q`` .
        If q is a 0-D Tensor, it will be treated as an integer or float.
    axis (int|list, optional): The axis along which to calculate quantile. ``axis`` should be int or list of int.
        ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
        If ``axis`` is less than 0, it works the same way as :math:`axis + D`.
        If ``axis`` is a list, quantile is calculated over all elements of given axes.
        If ``axis`` is None, quantile is calculated over all elements of ``x``. Default is None.
    keepdim (bool, optional): Whether to reserve the reduced dimension(s)
        in the output Tensor. If ``keepdim`` is True, the dimensions of
        the output Tensor is the same as ``x`` except in the reduced
        dimensions(it is of size 1 in this case). Otherwise, the shape of
        the output Tensor is squeezed in ``axis`` . Default is False.
    interpolation (str, optional): The interpolation method to use
        when the desired quantile falls between two data points. Must be one of linear, higher,
        lower, midpoint and nearest. Default is linear.
    ignore_nan: (bool, optional): Whether to ignore NaN of input Tensor.
        If ``ignore_nan`` is True, it will calculate nanquantile.
        Otherwise it will calculate quantile. Default is False.
    out (Tensor|None, optional): The output tensor. Default: None.

Returns:
    Tensor, results of quantile along ``axis`` of ``x``.
    In order to obtain higher precision, data type of results will be float64.
zinput x should be a Tensor.r   zq should not be emptyr   z(q should be a 0-D tensor or a 1-D tensorz8Type of q should be int, float, list or tuple, or tensorzq should be in range [0, 1])r   r   r   r   r   z[interpolation must be one of 'linear', 'lower', 'higher', 'nearest' or 'midpoint', but got zQAxis should be None, int, or a list, element should in range [-rank(x), rank(x)).r   T)r#   rG   rT   )
fill_valuec                r  > TS:X  aI  [         R                  " U 5      R                  [         R                  5      n[         R                  " T	UTS9$ [         R
                  " U 5      R                  [         R                  5      nTS:w  a  [         R                  " T	UTS9nTS:X  a  W$ [         R                  " U 5      R                  [         R                  5      n[         R                  " T	UTS9nTS:X  a  U$ TS:X  a:  UR                  T
R                  5      WR                  T
R                  5      -   S-  $ XR                  U R                  5      -
  R                  T
R                  5      n[         R                  " WR                  T
R                  5      UR                  T
R                  5      U5      $ )Nr   r#   r   r   r   r   )	rV   roundrf   r.   take_along_axisfloorceilr%   lerp)indexidxindices_belowtensor_belowindices_uppertensor_upperweightsr#   interpolationsorted_tensorr!   s          rL   _compute_index)_compute_quantile.<locals>._compute_indexP  sh   I%,,u%,,V\\:C))-4HHU+226<<@H$!11}4L G#E*11&,,?--=t
 H$J&##AGG,|/B/B177/KK  //<<DDQWWM{{((
 	
rM   r   ) r=   r   rV   r   r   r|   rA   r[   rB   rC   rl   rc   r]   r	   rZ   appendrangemoveaxisisnanlogical_notrd   r
   rm   r%   	full_likewhereri   sortsqueezer\   stackrn   )r!   qr#   rG   r   
ignore_nanr&   q_numr   	out_shapeaxis_srcaxis_dstaxis_singlemaskvalid_countsr^   r   
last_indexnumsr   r:   retr   s   ` ` `                 @rL   _compute_quantiler     s$   P a(FJJ$4$4566566 !c5\""C	Ae}	%	%q6Q;455 	A&**"2"23	4	4qww<!GHHqww<1AF
 	
   ZHfjj../&
 &
 19	:;;     ijwixy
 	
 qww<DQWWI|NN1C$J	dD!!!#Rh#!+s33&;%+?$k  ?"-"4K,)*	&  $ E3t9*a01H86A8}!NN1%NN1hqk8B<@{dC(($ g  axIdO779D##%))tT)BLG!##$$U!'':ENN51$456A-.E*J##EjADLLtT!BD%PENN5!  KK4(M 
  
D G U#..40C++i(Cs  7|a,,w*!*
gs#
NrM   c          
          [        U UUUUSUS9$ )a(  
Compute the quantile of the input along the specified axis.
If any values in a reduced row are NaN, then the quantiles for that reduction will be NaN.

Args:
    x (Tensor): The input Tensor, it's data type can be float32, float64, int32, int64.
    q (int|float|list|Tensor): The q for calculate quantile, which should be in range [0, 1]. If q is a list or
        a 1-D Tensor, each element of q will be calculated and the first dimension of output is same to the number of ``q`` .
        If q is a 0-D Tensor, it will be treated as an integer or float.
    axis (int|list, optional): The axis along which to calculate quantile. ``axis`` should be int or list of int.
        ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
        If ``axis`` is less than 0, it works the same way as :math:`axis + D`.
        If ``axis`` is a list, quantile is calculated over all elements of given axes.
        If ``axis`` is None, quantile is calculated over all elements of ``x``. Default is None.
    keepdim (bool, optional): Whether to reserve the reduced dimension(s)
        in the output Tensor. If ``keepdim`` is True, the dimensions of
        the output Tensor is the same as ``x`` except in the reduced
        dimensions(it is of size 1 in this case). Otherwise, the shape of
        the output Tensor is squeezed in ``axis`` . Default is False.
    interpolation (str, optional): The interpolation method to use
        when the desired quantile falls between two data points. Must be one of linear, higher,
        lower, midpoint and nearest. Default is linear.
    name (str, optional): Name for the operation (optional, default is None).
        For more information, please refer to :ref:`api_guide_Name`.
    out (Tensor|None, optional): The output tensor. Default: None.


Returns:
    Tensor, results of quantile along ``axis`` of ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> y = paddle.arange(0, 8 ,dtype="float32").reshape([4, 2])
        >>> print(y)
        Tensor(shape=[4, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[0., 1.],
         [2., 3.],
         [4., 5.],
         [6., 7.]])

        >>> y1 = paddle.quantile(y, q=0.5, axis=[0, 1])
        >>> print(y1)
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        3.50000000)

        >>> y2 = paddle.quantile(y, q=0.5, axis=1)
        >>> print(y2)
        Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
        [0.50000000, 2.50000000, 4.50000000, 6.50000000])

        >>> y3 = paddle.quantile(y, q=[0.3, 0.5], axis=0)
        >>> print(y3)
        Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[1.80000000, 2.80000000],
         [3.        , 4.        ]])

        >>> y[0,0] = float("nan")
        >>> y4 = paddle.quantile(y, q=0.8, axis=1, keepdim=True)
        >>> print(y4)
        Tensor(shape=[4, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[nan       ],
         [2.80000000],
         [4.80000000],
         [6.80000000]])

F)r#   rG   r   r   r&   r   )r!   r   r#   rG   r   rH   r&   s          rL   quantiler     s'    ` 		# rM   c           	         [        U UUUUSS9$ )a  
Compute the quantile of the input as if NaN values in input did not exist.
If all values in a reduced row are NaN, then the quantiles for that reduction will be NaN.

Args:
    x (Tensor): The input Tensor, it's data type can be float32, float64, int32, int64.
    q (int|float|list|Tensor): The q for calculate quantile, which should be in range [0, 1]. If q is a list or
        a 1-D Tensor, each element of q will be calculated and the first dimension of output is same to the number of ``q`` .
        If q is a 0-D Tensor, it will be treated as an integer or float.
    axis (int|list, optional): The axis along which to calculate quantile. ``axis`` should be int or list of int.
        ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
        If ``axis`` is less than 0, it works the same way as :math:`axis + D`.
        If ``axis`` is a list, quantile is calculated over all elements of given axes.
        If ``axis`` is None, quantile is calculated over all elements of ``x``. Default is None.
    keepdim (bool, optional): Whether to reserve the reduced dimension(s)
        in the output Tensor. If ``keepdim`` is True, the dimensions of
        the output Tensor is the same as ``x`` except in the reduced
        dimensions(it is of size 1 in this case). Otherwise, the shape of
        the output Tensor is squeezed in ``axis`` . Default is False.
    interpolation (str, optional): The interpolation method to use
        when the desired quantile falls between two data points. Must be one of linear, higher,
        lower, midpoint and nearest. Default is linear.
    name (str|None, optional): Name for the operation (optional, default is None).
        For more information, please refer to :ref:`api_guide_Name`.

Returns:
    Tensor, results of quantile along ``axis`` of ``x``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> x = paddle.to_tensor(
        ...     [[0, 1, 2, 3, 4],
        ...      [5, 6, 7, 8, 9]],
        ...     dtype="float32")
        >>> x[0,0] = float("nan")

        >>> y1 = paddle.nanquantile(x, q=0.5, axis=[0, 1])
        >>> print(y1)
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
        5.)

        >>> y2 = paddle.nanquantile(x, q=0.5, axis=1)
        >>> print(y2)
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
        [2.50000000, 7.        ])

        >>> y3 = paddle.nanquantile(x, q=[0.3, 0.5], axis=0)
        >>> print(y3)
        Tensor(shape=[2, 5], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[5.        , 2.50000000, 3.50000000, 4.50000000, 5.50000000],
         [5.        , 3.50000000, 4.50000000, 5.50000000, 6.50000000]])

        >>> y4 = paddle.nanquantile(x, q=0.8, axis=1, keepdim=True)
        >>> print(y4)
        Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[3.40000000],
         [8.20000000]])

        >>> nan = paddle.full(shape=[2, 3], fill_value=float("nan"))
        >>> y5 = paddle.nanquantile(nan, q=0.8, axis=1, keepdim=True)
        >>> print(y5)
        Tensor(shape=[2, 1], dtype=float32, place=Place(cpu), stop_gradient=True,
        [[nan],
         [nan]])

T)r#   rG   r   r   r   )r!   r   r#   rG   r   s        rL   nanquantiler     s$    X 		# rM   )NFN)r!   r   r#   int | Sequence[int] | NonerG   r)   rH   
str | Noner%   zDTypeLike | Noner&   Tensor | Nonereturnr   )NNFN)r!   r   r#   r   ro   zbool | NonerG   r)   rH   r   rN   r[   r&   r   r   r   )NTFN)r!   r   r#   r   ro   r)   rG   r)   rH   r   r   r   r   )r!   r   rH   r   r   r   )...)r!   r   r#   rA   rG   r)   r~   Literal['min']rH   r   r   tuple[Tensor, Tensor])....)r!   r   r#   r   rG   r)   r~   Literal['avg', 'min']rH   r   r   r   )NFr   N)r!   r   r#   rA   rG   r)   r~   r   rH   r   r&   ztuple[Tensor, Tensor] | Noner   r   )r!   r   r#   z
int | NonerG   r)   r~   r   rH   r   r   r   )NFr   FN)r!   r   r   z'float | Sequence[float] | Tensor | Noner#   int | list[int] | NonerG   r)   r   r    r   r)   r&   r   r   r   )NFr   N)r!   r   r    float | Sequence[float] | Tensorr#   r   rG   r)   r   r    rH   r   r&   r   r   r   )NFr   )r!   r   r   r   r#   zlist[int] | int | NonerG   r)   r   r    r   r   )0
__future__r   rj   typingr   r   typing_extensionsr   r   rV   r   paddle.frameworkr	   r
   paddle.utils.decorator_utilsr   r   r   base.data_feederr   r   common_ops_importr   	frameworkr   r   r   manipulationr   mathr   collections.abcr   r   paddle._typingr   r    __annotations____all__r<   rQ   rv   rX   r   r   r   r   r   rw   rM   rL   <module>r      s   #  ) 1    D ( E E  .((#6	   #w&%1 (,	y #yy
$y y 	y y 
y y 2yx G9ug67 (, k kk
$k k 	k
 k k 
k k 8k` (,==
$= = 	=
 = =@"J 
   
    	 
     
  
 (+"%
$   	
   
 
		xv 
   ),  
    	 
   
&    
  
 "%
   	
   
 c7^fe_vuoN 
		[ 	[ O[B $($,||.| !| 	|
 "| | 
| |~ #w&%1 $($,W WW'W !W 	W
 "W W 
W W 2Wz $($,SS'S !S 	S
 "S SrM   