o
    ְi7                     @   s   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dZ	dd	d
Z
dddZdddZdddZ				dddZdd Z				dddddZ				dddddZdd ZdS )    N)_core)_routines_statistics)_fusion_thread_local)contentFc                 C   8   t  r|r
tdt jtj| ||dS | j|||d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    r   J/home/ubuntu/.local/lib/python3.10/site-packages/cupy/_statistics/order.pyr   
      
r   c                 C   r   )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   r   r   r   r   /   r   r   c                 C   0   t j| |||d}t| rt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   resr   r   r   r   T      r   c                 C   r   )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!   r   r   r   r$   s   r#   r$   c                 C   s   | j |||d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   r   r   r   r%      s   r%   linearc                    s  |j dkr|d  }d}nd}|j dkrtd|j t|tr#|f}|rD|d u r/d j  }nt j}|D ]	}	d||	 j  < q6t|}|d u rV|rO  }
n 	 }
d}nHt fdd|D }t
t j t
| }t|}tt|D ]
\}} || qv|r  jd | d	 }
n  jd | d	  }
d
}|
j|d |
j| }||d  }|dv rtd| d|dkrt|tj}n3|dkrt|tj}n%|dkrdt|t|  }n|dkrtd|dkrntd||jtjkrt|
|}|j|d|d}n>|d u r*tj|
jd d
 |j tjd}nt|d|j }tdddd||
|
j dkrF|
jd
 nd|
j| t|d
}|rZ|d}|rl|jdkrgd	| }||}t !|S )Nr   TF   z5Expected q to have a dimension of 1.
Actual: {0} != 1)r'   c                 3   s    | ]}| j  V  qd S )N)ndim).0axr   r   r   	<genexpr>   s    z&_quantile_unchecked.<locals>.<genexpr>)r-   )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ceilr=   rollaxistakeemptyfloat64ElementwiseKernelsizesqueezer   _internal_ascontiguousarray)r   qr   r	   overwrite_inputmethodr   zerodkeepdimr*   apnkeepkeepisNxindicesretr   r+   r   _quantile_unchecked   s   






""


rk   c                 C   s$   t | dk st | dkrdS dS )Ng        r.   FT)rQ   count_nonzero)r^   r   r   r   _quantile_is_valid  s   rm   )interpolationc             	   C   s`   |dur
t ||d}t|tjstj|dd}t|d}t|s%tdt| ||||||d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dr<   d   z)Percentiles must be in the range [0, 100]r   r	   r_   r`   r   )	_check_interpolation_as_methodrA   rQ   ndarrayasarraytrue_dividerm   r?   rk   r   r^   r   r	   r_   r`   r   rn   r   r   r   ro   $  s    ro   c             	   C   sT   |dur
t ||d}t|tjstj|dd}t|stdt| ||||||d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quantilerp   r<   z%Quantiles must be in the range [0, 1]rr   )rs   rA   rQ   rt   ru   rm   r?   rk   rw   r   r   r   rx   S  s    rx   c                 C   s,   t jd| dtd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)r`   rn   fnamer   r   r   rs     s   
rs   )NNF)NNFr&   F)r   rQ   r   
cupy._corer   r   r   cupy._logicr   r   r   r   r$   r%   rk   rm   ro   rx   rs   r   r   r   r   <module>   s@    

%
%


m//