
    `i7                         d dl Z d dlZd dlmZ d dlmZ d dlmZ d dlmZ ddZ	ddZ
dd	Zdd
ZddZ	 	 	 	 ddZd Z	 	 	 	 ddddZ	 	 	 	 ddddZd ZdS )    N)_core)_routines_statistics)_fusion_thread_local)contentFc                     t          j                    r3|rt          d          t          j        t          j        | ||          S |                     |||          S )a  Returns the minimum of an array or the minimum along an axis.

    .. note::

       When at least one element is NaN, the corresponding min value will be
       NaN.

    Args:
        a (cupy.ndarray): Array to take the minimum.
        axis (int): Along which axis to take the minimum. The flattened array
            is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.

    Returns:
        cupy.ndarray: The minimum of ``a``, along the axis if specified.

    .. note::
       When cuTENSOR accelerator is used, the output value might be collapsed
       for reduction axes that have one or more NaN elements.

    .. seealso:: :func:`numpy.amin`

    z4cupy.amin does not support `keepdims` in fusion yet.axisoutr	   r
   keepdims)r   	is_fusingNotImplementedErrorcall_reduction_statisticsaminminar	   r
   r   s       j/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/cupy/_statistics/order.pyr   r   
   v    4 %'' 5 	H%FH H H#2ad5 5 5 	5 55dh5777    c                     t          j                    r3|rt          d          t          j        t          j        | ||          S |                     |||          S )a  Returns the maximum of an array or the maximum along an axis.

    .. note::

       When at least one element is NaN, the corresponding min value will be
       NaN.

    Args:
        a (cupy.ndarray): Array to take the maximum.
        axis (int): Along which axis to take the maximum. The flattened array
            is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.

    Returns:
        cupy.ndarray: The maximum of ``a``, along the axis if specified.

    .. note::
       When cuTENSOR accelerator is used, the output value might be collapsed
       for reduction axes that have one or more NaN elements.

    .. seealso:: :func:`numpy.amax`

    z4cupy.amax does not support `keepdims` in fusion yet.r   r   )r   r   r   r   r   amaxmaxr   s       r   r   r   /   r   r   c                     t          j        | |||          }t          j        |                                          rt          j        dt                     |S )a  Returns the minimum of an array along an axis ignoring NaN.

    When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`
    is raised and NaN is returned.

    Args:
        a (cupy.ndarray): Array to take the minimum.
        axis (int): Along which axis to take the minimum. The flattened array
            is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.

    Returns:
        cupy.ndarray: The minimum of ``a``, along the axis if specified.

    .. warning::

        This function may synchronize the device.

    .. seealso:: :func:`numpy.nanmin`

    r   All-NaN slice encountered)r   nanminr   isnananywarningswarnRuntimeWarningr   r	   r
   r   ress        r   r   r   T   S    2 ,qtx
@
@
@C}S C1>BBBJr   c                     t          j        | |||          }t          j        |                                          rt          j        dt                     |S )a  Returns the maximum of an array along an axis ignoring NaN.

    When there is a slice whose elements are all NaN, a :class:`RuntimeWarning`
    is raised and NaN is returned.

    Args:
        a (cupy.ndarray): Array to take the maximum.
        axis (int): Along which axis to take the maximum. The flattened array
            is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.

    Returns:
        cupy.ndarray: The maximum of ``a``, along the axis if specified.

    .. warning::

        This function may synchronize the device.

    .. seealso:: :func:`numpy.nanmax`

    r   r   )r   nanmaxr   r   r   r    r!   r"   r#   s        r   r'   r'   s   r%   r   c                 2    |                      |||          S )aP  Returns the range of values (maximum - minimum) along an axis.

    .. note::

       The name of the function comes from the acronym for 'peak to peak'.

       When at least one element is NaN, the corresponding ptp value will be
       NaN.

    Args:
        a (cupy.ndarray): Array over which to take the range.
        axis (int): Axis along which to take the minimum. The flattened
            array is used by default.
        out (cupy.ndarray): Output array.
        keepdims (bool): If ``True``, the axis is retained as an axis of
            size one.

    Returns:
        cupy.ndarray: The minimum of ``a``, along the axis if specified.

    .. note::
       When cuTENSOR accelerator is used, the output value might be collapsed
       for reduction axes that have one or more NaN elements.

    .. seealso:: :func:`numpy.amin`

    r   )ptpr   s       r   r)   r)      s    8 55dh5777r   linearc                 Z    |j         dk    r|d          }d}nd}|j         dk    r't          d                    |j                             t          |t                    r|f}|rB|d j         z  }n5t           j                  }|D ]}	d||	 j         z  <   t          |          }|.|r                                 }
n 	                                }
d}nt           fd|D                       }t          t           j                             t          |          z
  }t          |          }t          t          |                    D ]\  }}                     ||           |r&                      j        d |         dz             }
n7                      j        d |         dz                                             }
d	}|
                    |
           |
j        |         }||dz
  z  }|dv rt          d| d          |dk    r2t'          j        |                              t&          j                  }n|dk    r2t'          j        |                              t&          j                  }nq|dk    r-dt'          j        |          t'          j        |          z   z  }n>|dk    rt          d          |dk    rn"t          d                    |                    |j        t&          j        k    r.t'          j        |
|          }|                    |d|          }n|6t'          j        |
j        d d	         |j        z   t&          j                  }nt'          j        |d|j                   } t'          j        dddd          ||
|
j         dk    r|
j        d	         nd|
j        |           t'          j        |d	          }|r|                    d          }|r%|j        dk    rd|z   }|                    |          }tA          j!        |          S )Nr   TF   z5Expected q to have a dimension of 1.
Actual: {0} != 1)r,   c              3   ,   K   | ]}|j         z  V  d S )N)ndim).0axr   s     r   	<genexpr>z&_quantile_unchecked.<locals>.<genexpr>   s)      00RR!&[000000r   )r2   )r	         ?)inverted_cdfaveraged_inverted_cdfclosest_observationinterpolated_inverted_cdfhazenweibullmedian_unbiasednormal_unbiased'z;' method is not yet supported. Please use any other method.lowerhighermidpointg      ?nearestzC'nearest' method is not yet supported. Please use any other method.r*   z_Unexpected interpolation method.
Actual: '{0}' not in ('linear', 'lower', 'higher', 'midpoint')r   dtypez0S idx, raw T a, raw int32 offset, raw int32 sizezU reta#  
            ptrdiff_t idx_below = floor(idx);
            U weight_above = idx - idx_below;

            ptrdiff_t max_idx = size - 1;
            ptrdiff_t offset_bottom = _ind.get()[0] * offset + idx_below;
            ptrdiff_t offset_top = min(offset_bottom + 1, max_idx);

            U diff = a[offset_top] - a[offset_bottom];

            if (weight_above < 0.5) {
                ret = a[offset_bottom] + diff * weight_above;
            } else {
                ret = a[offset_top] - diff * (1 - weight_above);
            }
            cupy_percentile_weightnening)"r.   
ValueErrorformat
isinstanceintlistshapetupleravelflattensetrangelen	enumeratesortedswapaxesreshapecopysortcupyfloorastypeint32ceilrB   rollaxistakeemptyfloat64ElementwiseKernelsizesqueezer   _internal_ascontiguousarray)r   qr	   r
   overwrite_inputmethodr   zerodkeepdimr0   apnkeepkeepisNxindicesrets   `                 r   _quantile_uncheckedrp      sW    	v{{dGvzz ,,2F16NN< < 	<$ u %<QVmGG17mmG ) )'(QV$$GnnG| 	BBB 00004000005==!!CII-D		fTll++ 	! 	!DAq

1a  AA 	;176E6?U233BB176E6?U23388::BDGGG	$B27mG L L L  8f 8 8 8 9 9 	9	7		*W%%,,TZ88	8		)G$$++DJ77	:		G,,ty/A/AAB	9		 8 9 9 	9	8		 55;VF^^E E 	E }
""mB%%hhwQCh00;*RXcrc]QW4DLIIICC-Q11C	
>  +%	
 	
& 2rw{{rx||27C'	I 	I 	I( mC$$ kk!nn #6A::goGkk'"",S111r   c                 j    t          j        | dk               st          j        | dk              rdS dS )Ng        r3   FT)rV   count_nonzero)rc   s    r   _quantile_is_validrs     s9    !c'"" d&8S&A&A u4r   )interpolationc          	         |t          ||d          }t          |t          j                  st          j        |d          }t          j        |d          }t          |          st          d          t          | ||||||          S )a  Computes the q-th percentile of the data along the specified axis.

    Args:
        a (cupy.ndarray): Array for which to compute percentiles.
        q (float, tuple of floats or cupy.ndarray): Percentiles to compute
            in the range between 0 and 100 inclusive.
        axis (int or tuple of ints): Along which axis or axes to compute the
            percentiles. The flattened array is used by default.
        out (cupy.ndarray): Output array.
        overwrite_input (bool): If True, then allow the input array `a`
            to be modified by the intermediate calculations, to save
            memory. In this case, the contents of the input `a` after this
            function completes is undefined.
        method (str): Interpolation method when a quantile lies between
            two data points. ``linear`` interpolation is used by default.
            Supported interpolations are``lower``, ``higher``, ``midpoint``,
            ``nearest`` and ``linear``.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.
        interpolation (str): Deprecated name for the method keyword argument.

    Returns:
        cupy.ndarray: The percentiles of ``a``, along the axis if specified.

    .. seealso:: :func:`numpy.percentile`
    N
percentiledrA   d   z)Percentiles must be in the range [0, 100]r	   r
   rd   re   r   )	_check_interpolation_as_methodrF   rV   ndarrayasarraytrue_dividers   rD   rp   r   rc   r	   r
   rd   re   r   rt   s           r   rv   rv   $  s    @  /M<1 1a&& 'L#&&&C  Aa   FDEEE	14S'	   r   c          	          |t          ||d          }t          |t          j                  st          j        |d          }t          |          st          d          t          | ||||||          S )a  Computes the q-th quantile of the data along the specified axis.

    Args:
        a (cupy.ndarray): Array for which to compute quantiles.
        q (float, tuple of floats or cupy.ndarray): Quantiles to compute
            in the range between 0 and 1 inclusive.
        axis (int or tuple of ints): Along which axis or axes to compute the
            quantiles. The flattened array is used by default.
        out (cupy.ndarray): Output array.
        overwrite_input (bool): If True, then allow the input array `a`
            to be modified by the intermediate calculations, to save
            memory. In this case, the contents of the input `a` after this
            function completes is undefined.
        method (str): Interpolation method when a quantile lies between
            two data points. ``linear`` interpolation is used by default.
            Supported interpolations are``lower``, ``higher``, ``midpoint``,
            ``nearest`` and ``linear``.
        keepdims (bool): If ``True``, the axis is remained as an axis of
            size one.
        interpolation (str): Deprecated name for the method keyword argument.

    Returns:
        cupy.ndarray: The quantiles of ``a``, along the axis if specified.

    .. seealso:: :func:`numpy.quantile`
    Nquantilerw   rA   z%Quantiles must be in the range [0, 1]ry   )rz   rF   rV   r{   r|   rs   rD   rp   r~   s           r   r   r   S  s    @  /M:/ /a&& 'L#&&&a   B@AAA	14S'	   r   c                 p    t          j        d| dt          d           | dk    rt          d          |S )Nz!the `interpolation=` argument to z was renamed to `method=`, which has additional options.
Users of the modes 'nearest', 'lower', 'higher', or 'midpoint' are encouraged to review the method they. (Deprecated NumPy 1.22)   )
stacklevelr*   zjYou shall not pass both `method` and `interpolation`!
(`interpolation` is Deprecated in favor of `method`))r    r!   DeprecationWarning	TypeError)re   rt   fnames      r   rz   rz     sh    M	"E 	" 	" 	"
 	q* * * * CD D 	D r   )NNF)NNFr*   F)r    rV   r   
cupy._corer   r   r   cupy._logicr   r   r   r   r'   r)   rp   rs   rv   r   rz    r   r   <module>r      sx           : : : : : : + + + + + +      "8 "8 "8 "8J"8 "8 "8 "8J   >   >8 8 8 8> .2(-'!&j2 j2 j2 j2Z   %)$,
 ", , , , ,^ #'"+
  + + + + +^    r   