o
    X۷ir4                     @  s   d dl mZ 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lmZ g dZG d	d
 d
e	jZdd Zdd ZG dd dZdd Zdd Ze  dS )    )annotationsN)internal)_util)_base)_coo)_sputils)arcsinarcsinharctanarctanhceildeg2radexpm1floorlog1prad2degrintsignsinsinhsqrttantanhtruncc                   @  s   e Zd Zdd Zedd ZdddZdd	 Zd
d Zdd Z	dddZ
ejj
je
_dd Zejjje_dd ZdddZdddZdS )_data_matrixc                 C  s
   || _ d S N)data)selfr    r   N/home/ubuntu/vllm_env/lib/python3.10/site-packages/cupyx/scipy/sparse/_data.py__init__   s   
z_data_matrix.__init__c                 C  s   | j jS )zData type of the matrix.)r   dtyper   r   r   r   r!      s   z_data_matrix.dtypeTc                 C  s   t r   )NotImplementedError)r   r   copyr   r   r   
_with_data   s   z_data_matrix._with_datac                 C  s   |  t| jS )zElementwise absolute.)r%   absr   r"   r   r   r   __abs__    s   z_data_matrix.__abs__c                 C  s   |  | j S )zElementwise negative.)r%   r   r"   r   r   r   __neg__$   s   z_data_matrix.__neg__c                 C  s   |  | j|S )zCasts the array to given data type.

        Args:
            dtype: Type specifier.

        Returns:
            A copy of the array with a given type.

        )r%   r   astype)r   tr   r   r   r)   (   s   
z_data_matrix.astypec                 C  s4   t | jt jr| j| j |dS |r|  S | S )Nr$   )cupy
issubdtyper!   complexfloatingr%   r   conjr$   )r   r$   r   r   r   r/   4   s
   z_data_matrix.conjc                 C  s   | j | j ddS )NTr+   )r%   r   r$   r"   r   r   r   r$   >   s   z_data_matrix.copyc                 C  s   t | jS )aW  Returns number of non-zero entries.

        .. note::
           This method counts the actual number of non-zero entories, which
           does not include explicit zero entries.
           Instead ``nnz`` returns the number of entries including explicit
           zeros.

        Returns:
            Number of non-zero entries.

        )r,   count_nonzeror   r"   r   r   r   r0   C   s   z_data_matrix.count_nonzeroNc                 C  sZ   t | | j\}}| j }|du r|| }n	|dv r|}n|}| || |||S )a  Compute the arithmetic mean along the specified axis.

        Args:
            axis (int or ``None``): Axis along which the sum is computed.
                If it is ``None``, it computes the average of all the elements.
                Select from ``{None, 0, 1, -2, -1}``.

        Returns:
            cupy.ndarray: Summed array.

        .. seealso::
           :meth:`scipy.sparse.spmatrix.mean`

        N)r   )r   validateaxisshaper   r$   r%   sum)r   axisr!   outnRownColr   nr   r   r   meanR   s   



z_data_matrix.meanc                 C  s6   |du r
| j  }n| j j|dd}||C }| |S )zpElementwise power function.

        Args:
            n: Exponent.
            dtype: Type specifier.

        NTr+   )r   r$   r)   r%   )r   r9   r!   r   r   r   r   powern   s
   
z_data_matrix.power)T)NNNr   )__name__
__module____qualname__r    propertyr!   r%   r'   r(   r)   r/   r   spmatrix__doc__r$   r0   r:   r;   r   r   r   r   r      s    



r   c                 C  sD   t | j}| |k}t | | t | j|k r| jS dS )N)r,   arangesizewhereanyargmaxasarray)indr9   	positionsdiffr   r   r   _find_missing_index~   s   rL   c                 C  sT   t | j}|| jkr|S | j| jd  | j }t||}t||kt	|||S )N   )
npprodr3   nnzrowcolrL   r,   rE   minimum)matamzeromrD   rI   zero_indr   r   r   _non_zero_cmp   s   


rY   c                   @  sd   e Zd ZdZdd Zdd Zdd Zdd	 ZdddddZdddddZ	dddZ
dddZd
S )_minmax_mixinzlMixin for min and max methods.
    These are not implemented for dia_matrix, hence the separate class.

    c           
      C  s   | j | }|dkrtd| j d|  }|dkr|  n|  }|  ||||}t|}|dk}	t|	|}t|	|}|dkrXt	j
|tt||ff| jd|fdS t	j
||tt|ff| j|dfdS )Nr   &zero-size array to reduction operationrM   )r!   r3   )r3   
ValueErrortocsctocsrsum_duplicates_minor_reducer,   rC   compressr   
coo_matrixzeroslenr!   )
r   r5   
min_or_maxexplicitNMrT   valuemajor_indexmaskr   r   r   _min_or_max_axis   s(   



z_minmax_mixin._min_or_max_axisc                 C  s   |d urt dt| |d u r]d| jv rt dtjd| jd}| jdkr)|S |   || j	}|r6|S | jt
| jkr[|tju rLt||}|S |tju rYt||}|S J |S |dk re|d7 }| |||S )N2Sparse matrices do not support an 'out' parameter.r   r[   r   )r!   F   )r\   r   r2   r3   r,   rc   r!   rP   r_   r   r   rO   minrS   maxmaximumrl   )r   r5   r6   re   rf   rV   rW   r   r   r   _min_or_max   s2   





z_minmax_mixin._min_or_maxc                 C  sj   | j | dkrtd|dkr|  n|  }|  |||}|dkr-|d d d f S |d d d f S )Nr   z7Can't apply the operation along a zero-sized dimension.)r3   r\   r]   r^   r_   _arg_minor_reduce)r   r5   oprT   ri   r   r   r   _arg_min_or_max_axis   s   z"_minmax_mixin._arg_min_or_max_axisc           	   	   C  s   |d urt dt| |d u rXd| jv rt d| jdkr!dS t| jd}| 	 }|
  ||j}|j| }t||||j| |jd  |j|  t||||S |dk r`|d7 }| ||S )Nrm   r   z-Can't apply the operation to an empty matrix.rM   rn   )r\   r   r2   r3   rP   r,   rH   r!   typetocoor_   r   rE   rQ   rR   rY   ru   )	r   r5   r6   rt   comparerV   rT   rU   rW   r   r   r   _arg_min_or_max   s(   




$z_minmax_mixin._arg_min_or_maxNF)rf   c                C  .   |rd | jj}t| | ||tj|S )a  Returns the maximum of the matrix or maximum along an axis.

        Args:
            axis (int): {-2, -1, 0, 1, ``None``} (optional)
                Axis along which the sum is computed. The default is to
                compute the maximum over all the matrix elements, returning
                a scalar (i.e. ``axis`` = ``None``).
            out (None): (optional)
                This argument is in the signature *solely* for NumPy
                compatibility reasons. Do not pass in anything except
                for the default value, as this argument is not used.
            explicit (bool): Return the maximum value explicitly specified and
                ignore all implicit zero entries. If the dimension has no
                explicit values, a zero is then returned to indicate that it is
                the only implicit value. This parameter is experimental and may
                change in the future.

        Returns:
            (cupy.ndarray or float): Maximum of ``a``. If ``axis`` is
            ``None``, the result is a scalar value. If ``axis`` is given,
            the result is an array of dimension ``a.ndim - 1``. This
            differs from numpy for computational efficiency.

        .. seealso:: min : The minimum value of a sparse matrix along a given
          axis.
        .. seealso:: numpy.matrix.max : NumPy's implementation of ``max`` for
          matrices

        z%explicit of cupyx.scipy.sparse.{}.max)format	__class__r<   r   experimentalrr   r,   rp   r   r5   r6   rf   api_namer   r   r   rp        
z_minmax_mixin.maxc                C  rz   )a  Returns the minimum of the matrix or maximum along an axis.

        Args:
            axis (int): {-2, -1, 0, 1, ``None``} (optional)
                Axis along which the sum is computed. The default is to
                compute the minimum over all the matrix elements, returning
                a scalar (i.e. ``axis`` = ``None``).
            out (None): (optional)
                This argument is in the signature *solely* for NumPy
                compatibility reasons. Do not pass in anything except for
                the default value, as this argument is not used.
            explicit (bool): Return the minimum value explicitly specified and
                ignore all implicit zero entries. If the dimension has no
                explicit values, a zero is then returned to indicate that it is
                the only implicit value. This parameter is experimental and may
                change in the future.

        Returns:
            (cupy.ndarray or float): Minimum of ``a``. If ``axis`` is
            None, the result is a scalar value. If ``axis`` is given, the
            result is an array of dimension ``a.ndim - 1``. This differs
            from numpy for computational efficiency.

        .. seealso:: max : The maximum value of a sparse matrix along a given
          axis.
        .. seealso:: numpy.matrix.min : NumPy's implementation of 'min' for
          matrices

        z%explicit of cupyx.scipy.sparse.{}.min)r{   r|   r<   r   r}   rr   r,   ro   r~   r   r   r   ro   (  r   z_minmax_mixin.minc                 C     |  ||tjtjS )a  Returns indices of maximum elements along an axis.

        Implicit zero elements are taken into account. If there are several
        maximum values, the index of the first occurrence is returned. If
        ``NaN`` values occur in the matrix, the output defaults to a zero entry
        for the row/column in which the NaN occurs.

        Args:
            axis (int): {-2, -1, 0, 1, ``None``} (optional)
                Axis along which the argmax is computed. If ``None`` (default),
                index of the maximum element in the flatten data is returned.
            out (None): (optional)
                This argument is in the signature *solely* for NumPy
                compatibility reasons. Do not pass in anything except for
                the default value, as this argument is not used.

        Returns:
            (cupy.narray or int): Indices of maximum elements. If array,
            its size along ``axis`` is 1.

        )ry   r,   rG   greaterr   r5   r6   r   r   r   rG   L  s   z_minmax_mixin.argmaxc                 C  r   )a  
        Returns indices of minimum elements along an axis.

        Implicit zero elements are taken into account. If there are several
        minimum values, the index of the first occurrence is returned. If
        ``NaN`` values occur in the matrix, the output defaults to a zero entry
        for the row/column in which the NaN occurs.

        Args:
            axis (int): {-2, -1, 0, 1, ``None``} (optional)
                Axis along which the argmin is computed. If ``None`` (default),
                index of the minimum element in the flatten data is returned.
            out (None): (optional)
                This argument is in the signature *solely* for NumPy
                compatibility reasons. Do not pass in anything except for
                the default value, as this argument is not used.

        Returns:
            (cupy.narray or int): Indices of minimum elements. If matrix,
            its size along ``axis`` is 1.

        )ry   r,   argminlessr   r   r   r   r   d  s   z_minmax_mixin.argmin)NN)r<   r=   r>   rA   rl   rr   ru   ry   rp   ro   rG   r   r   r   r   r   rZ      s      $
$rZ   c                   s,    fdd}d  |_  |_tt | d S )Nc                   s2    dkr
t jjj}ntt  }|| j}| |S )Nr   )r,   _mathmisc_legacy_signgetattrr   r%   )r   ufuncresult	func_namer   r   f  s
   


z_install_ufunc.<locals>.fzElementwise %s.)rA   r<   setattrr   )r   r   r   r   r   _install_ufunc~  s   
r   c                  C  s   t D ]} t|  qd S r   )_ufuncsr   r   r   r   r   _install_ufuncs  s   
r   )
__future__r   r,   numpyrN   
cupy._corer   r   cupyx.scipy.sparser   r   r   r   r@   r   rL   rY   rZ   r   r   r   r   r   r   <module>   s"    k	 k
