o
    0 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)ZarcsinZarcsinhZarctanZarctanhceilZdeg2radexpm1floorlog1pZrad2degZ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   d/home/app/PaddleOCR-VL-test/.venv_paddleocr/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   )cupyZ
issubdtyper   Z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   outZnRowZ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   r-   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&   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)indr-   Z	positionsdiffr   r   r   _find_missing_index|   s   r?   c                 C   sT   t | j}|| jkr|S | j| jd  | j }t||}t||kt	|||S )N   )
npprodr)   nnzrowcolr?   r$   r9   minimum)matamzeromr8   r=   Zzero_indr   r   r   _non_zero_cmp   s   


rK   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 operationr@   )r   r)   )r)   
ValueErrortocsctocsrsum_duplicatesZ_minor_reducer$   r7   compressr   Z
coo_matrixzeroslenr   )
r   r+   
min_or_maxexplicitNMrG   valueZ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   rM   r   )r   F   )rN   r   r(   r)   r$   rS   r   rC   rQ   r   r   rB   minrF   maxmaximumr[   )r   r+   r,   rU   rV   rI   rJ   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.)r)   rN   rO   rP   rQ   Z_arg_minor_reduce)r   r+   oprG   rY   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 )Nr\   r   z-Can't apply the operation to an empty matrix.r@   r]   )rN   r   r(   r)   rC   r$   r<   r   typeZtocoorQ   r   r9   rD   rE   rK   rc   )	r   r+   r,   rb   comparerI   rG   rH   rJ   r   r   r   _arg_min_or_max   s(   




$z_minmax_mixin._arg_min_or_maxNF)rV   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__r0   r   experimentalra   r$   r_   r   r+   r,   rV   Zapi_namer   r   r   r_        
z_minmax_mixin.maxc                C   rg   )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)rh   ri   r0   r   rj   ra   r$   r^   rk   r   r   r   r^   &  rl   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.

        )rf   r$   r;   Zgreaterr   r+   r,   r   r   r   r;   J  s   z_minmax_mixin.argmaxc                 C   rm   )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.

        )rf   r$   argminlessrn   r   r   r   ro   b  s   z_minmax_mixin.argmin)NN)r0   r1   r2   r5   r[   ra   rc   rf   r_   r^   r;   ro   r   r   r   r   rL      s      $
$rL   c                    s,    fdd}d  |_  |_tt | d S )Nc                    s   t t }|| j}| |S r   )getattrr$   r   r   )r   Zufuncresult	func_namer   r   f~  s   


z_install_ufunc.<locals>.fzElementwise %s.)r5   r0   setattrr   )rt   ru   r   rs   r   _install_ufunc|  s   
rw   c                  C   s   t D ]} t|  qd S r   )_ufuncsrw   rs   r   r   r   _install_ufuncs  s   
ry   )r$   numpyrA   Z
cupy._corer   r   Zcupyx.scipy.sparser   r   r   rx   r4   r   r?   rK   objectrL   rw   ry   r   r   r   r   <module>   s     k	 k
