o
    װi3                     @   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lm	Z	 g dZ
G dd	 d	ejZd
d Zdd ZG dd deZdd Zdd Ze  dS )    N)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   L/home/ubuntu/.local/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.   2   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   r/   A   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   meanP   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   r8   r    r   r   r   r   powerl   s
   
z_data_matrix.power)T)NNNr   )__name__
__module____qualname__r   propertyr    r$   r&   r'   r(   r.   r   spmatrix__doc__r#   r/   r9   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)indr8   	positionsdiffr   r   r   _find_missing_index|   s   rK   c                 C   sT   t | j}|| jkr|S | j| jd  | j }t||}t||kt	|||S )N   )
npprodr2   nnzrowcolrK   r+   rD   minimum)matamzeromrC   rH   zero_indr   r   r   _non_zero_cmp   s   


rX   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 operationrL   )r    r2   )r2   
ValueErrortocsctocsrsum_duplicates_minor_reducer+   rB   compressr   
coo_matrixzeroslenr    )
r   r4   
min_or_maxexplicitNMrS   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   rZ   r   )r    F   )r[   r   r1   r2   r+   rb   r    rO   r^   r   r   rN   minrR   maxmaximumrk   )r   r4   r5   rd   re   rU   rV   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.)r2   r[   r\   r]   r^   _arg_minor_reduce)r   r4   oprS   rh   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 )Nrl   r   z-Can't apply the operation to an empty matrix.rL   rm   )r[   r   r1   r2   rO   r+   rG   r    typetocoor^   r   rD   rP   rQ   rX   rt   )	r   r4   r5   rs   comparerU   rS   rT   rV   r   r   r   _arg_min_or_max   s(   




$z_minmax_mixin._arg_min_or_maxNF)re   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   experimentalrq   r+   ro   r   r4   r5   re   api_namer   r   r   ro        
z_minmax_mixin.maxc                C   ry   )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)rz   r{   r;   r   r|   rq   r+   rn   r}   r   r   r   rn   &  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.

        )rx   r+   rF   greaterr   r4   r5   r   r   r   rF   J  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.

        )rx   r+   argminlessr   r   r   r   r   b  s   z_minmax_mixin.argmin)NN)r;   r<   r=   r@   rk   rq   rt   rx   ro   rn   rF   r   r   r   r   r   rY      s      $
$rY   c                    s,    fdd}d  |_  |_tt | d S )Nc                    s   t t }|| j}| |S r   )getattrr+   r   r$   )r   ufuncresult	func_namer   r   f~  s   


z_install_ufunc.<locals>.fzElementwise %s.)r@   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   )r+   numpyrM   
cupy._corer   r   cupyx.scipy.sparser   r   r   r   r?   r   rK   rX   objectrY   r   r   r   r   r   r   <module>   s     k	 k
