o
    Õ0 i”5  ã                   @   s†   zd dl ZdZW n ey   dZY nw d dlZd dlmZ d dlmZ d dlZ	d dlm
Z
 d dlmZ G dd	„ d	ejƒZd
d„ ZdS )é    NTF)Údriver)Úruntime)Ú_base)Ú_compressedc                   @   sÈ   e Zd ZdZdZd/dd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
d0dd„Zdd„ Zd1dd„Zd/dd„Zd1dd„Zdd„ Zd2dd„Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ ZdS )3Ú
csc_matrixaâ  Compressed Sparse Column matrix.

    This can be instantiated in several ways.

    ``csc_matrix(D)``
        ``D`` is a rank-2 :class:`cupy.ndarray`.
    ``csc_matrix(S)``
        ``S`` is another sparse matrix. It is equivalent to ``S.tocsc()``.
    ``csc_matrix((M, N), [dtype])``
        It constructs an empty matrix whose shape is ``(M, N)``. Default dtype
        is float64.
    ``csc_matrix((data, (row, col)))``
        All ``data``, ``row`` and ``col`` are one-dimenaional
        :class:`cupy.ndarray`.
    ``csc_matrix((data, indices, indptr))``
        All ``data``, ``indices`` and ``indptr`` are one-dimenaional
        :class:`cupy.ndarray`.

    Args:
        arg1: Arguments for the initializer.
        shape (tuple): Shape of a matrix. Its length must be two.
        dtype: Data type. It must be an argument of :class:`numpy.dtype`.
        copy (bool): If ``True``, copies of given arrays are always used.

    .. seealso::
        :class:`scipy.sparse.csc_matrix`

    ÚcscNc                 C   sH   t stdƒ‚| j |¡}| j |¡}| j |¡}tjj|||f| j	dS )a‰  Returns a copy of the array on host memory.

        .. warning::
           You need to install SciPy to use this method.

        Args:
            stream (cupy.cuda.Stream): CUDA stream object. If it is given, the
                copy runs asynchronously. Otherwise, the copy is synchronous.

        Returns:
            scipy.sparse.csc_matrix: Copy of the array on host memory.

        zscipy is not available)Úshape)
Ú_scipy_availableÚRuntimeErrorÚdataÚgetÚindicesÚindptrÚscipyÚsparser   Ú_shape)ÚselfÚstreamr   r   r   © r   úc/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/cupyx/scipy/sparse/_csc.pyr   0   s   ÿzcsc_matrix.getc                 C   s@   ddl m} | d¡r|j|dd}n| |¡}|j|j|jfS )Nr   ©ÚcusparseÚdenseToSparser   )Úformat)Úcupyxr   Úcheck_availabilityr   Z	dense2cscr   r   r   )r   Úxr   Úmr   r   r   Ú_convert_denseF   s
   

zcsc_matrix._convert_densec                 C   s   ||fS ©Nr   )r   r   Úyr   r   r   Ú_swapO   s   zcsc_matrix._swapc                 C   s¦  ddl m} t |¡r|  ¡  |  | j| ¡S t jj 	|¡ra|  ¡  | ¡  | 
d¡r9|  ¡ }| ¡  | ||¡S | 
d¡rLtjsL| j}|j||ddS | 
d¡r_|  ¡ }| ¡  | ||¡S t‚t|ƒr¼|  ¡  | ¡  | 
d¡r„tjs„| j}|j}|j||dddS | 
d¡rŸ|  ¡ }| ¡ }| ¡  | ¡  | ||¡S | 
d¡rº|  ¡ }| ¡ }| ¡  | ¡  | ||¡S t‚t jj |¡rÉ| | ¡  S t |¡rQ|jdkrà|  ¡  |  | j| ¡S |jd	kr|  ¡  | 
d
¡rütjrøt ¡ dkrü|j}n| 
d¡r
tjs
|j}nt‚|| jt |¡ddS |jdkrM|  ¡  | 
d¡r6tjr2t ¡ dkr6|j}n| 
d¡r@|j}nt‚|| jt |¡ddS tdƒ‚t S )Nr   r   ÚspgemmÚcsrgemmT)ÚtransaÚcsrgemm2)r$   Ztransbé   Úcsrmvi€ðúÚspmvé   Úcsrmm2Úspmmzcould not interpret dimensions)!r   r   ÚcupyZisscalarÚsum_duplicatesZ
_with_datar   r   r   Zisspmatrix_csrr   Útocsrr"   r   Úis_hipÚTr#   r%   ÚAssertionErrorÚisspmatrix_cscZ
isspmatrixr   ZisdenseÚndimr   Zget_build_versionr'   r(   Zasfortranarrayr*   r+   Ú
ValueErrorÚNotImplemented)r   Úotherr   ÚaÚbr'   Zcsrmmr   r   r   Ú__mul__R   s’   





ÿýÿÿýzcsc_matrix.__mul__c                 C   s0   | j }| ¡  |j }|j| _|j| _|j| _dS )zRemoves zero entories in place.N)r0   Úeliminate_zerosr   r   r   )r   ÚtÚcompressr   r   r   r:   °   s   zcsc_matrix.eliminate_zerosc                 C   s*   ddl m} | js| | ¡ d| _dS dS )zŒSorts the indices of this matrix *in place*.

        .. warning::
            Calling this function might synchronize the device.

        r   r   TN)r   r   Zhas_sorted_indicesZcscsort)r   r   r   r   r   Úsort_indices¾   s
   

þzcsc_matrix.sort_indicesc                 C   sÎ   ddl m} |du rd}| ¡ }| jdkrtj| j| j|dS |  ¡ }d|_	| 
¡  | d¡rOtjr7|jdkrO| |¡}|dkrB|S |dkrKt |¡S td	ƒ‚|dkrZ| |j¡jS |dkrc| |¡S td	ƒ‚)
a’  Returns a dense matrix representing the same value.

        Args:
            order ({'C', 'F', None}): Whether to store data in C (row-major)
                order or F (column-major) order. Default is C-order.
            out: Not supported.

        Returns:
            cupy.ndarray: Dense array representing the same matrix.

        .. seealso:: :meth:`scipy.sparse.csc_matrix.toarray`

        r   r   NÚC)r   ÚdtypeÚorderFÚsparseToDenseÚFzorder not understood)r   r   ÚupperZnnzr,   Zzerosr   r?   ÚcopyÚhas_canonical_formatr-   r   r   r/   rA   Zascontiguousarrayr4   Z	csr2denser0   Z	csc2dense)r   r@   Úoutr   r   r    r   r   r   ÚtoarrayË   s0   

ÿ



zcsc_matrix.toarrayc                 C   s`   ddl m} |  ¡  | ¡ j}| ¡  | d¡r|j}n| d¡r%|j}nt‚|| j|||ƒjS )Nr   r   Úcsrgeam2Úcsrgeam)	r   r   r-   Útocscr0   r   rH   rI   ÚNotImplementedError)r   r6   ÚalphaÚbetar   rI   r   r   r   Ú_add_sparseø   s   


zcsc_matrix._add_sparseFc                 C   s@   ddl m} |r| j ¡ }| j ¡ }n| j}| j}| | ||¡S )zóConverts the matrix to COOrdinate format.

        Args:
            copy (bool): If ``False``, it shares data arrays as much as
                possible.

        Returns:
            cupyx.scipy.sparse.coo_matrix: Converted matrix.

        r   r   )r   r   r   rD   r   Zcsc2coo)r   rD   r   r   r   r   r   r   Útocoo  s   
zcsc_matrix.tocooc                 C   s   |r|   ¡ S | S )a  Converts the matrix to Compressed Sparse Column format.

        Args:
            copy (bool): If ``False``, the method returns itself.
                Otherwise it makes a copy of the matrix.

        Returns:
            cupyx.scipy.sparse.csc_matrix: Converted matrix.

        ©rD   )r   rD   r   r   r   rJ     s   zcsc_matrix.tocscc                 C   s@   ddl m} | d¡r|j}|| ƒS | d¡r|j}|| ƒS t‚)ax  Converts the matrix to Compressed Sparse Row format.

        Args:
            copy (bool): If ``False``, it shares data arrays as much as
                possible. Actually this option is ignored because all
                arrays in a matrix cannot be shared in csr to csc conversion.

        Returns:
            cupyx.scipy.sparse.csr_matrix: Converted matrix.

        r   r   Úcsc2csrÚ
csc2csrEx2)r   r   r   rQ   rR   rK   )r   rD   r   rQ   r   r   r   r.   .  s   

ûþzcsc_matrix.tocsrc                 C   s   |   ¡ S )zInverts the format.
        )r.   )r   r   r   r   Ú_tocsxF  s   zcsc_matrix._tocsxc                 C   sP   |durt dƒ‚| jd | jd f}tjjj| j| j| jf||d}| j	|_	|S )aT  Returns a transpose matrix.

        Args:
            axes: This option is not supported.
            copy (bool): If ``True``, a returned matrix shares no data.
                Otherwise, it shared data arrays as much as possible.

        Returns:
            cupyx.scipy.sparse.csr_matrix: `self` with the dimensions reversed.

        NzoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.r&   r   )r   rD   )
r4   r   r   r   r   Z
csr_matrixr   r   r   rE   )r   ZaxesrD   r   Ztransr   r   r   Ú	transposeO  s   ÿÿzcsc_matrix.transposec                 C   s   | j t||d ƒdd ¡ S )zäReturns a copy of row i of the matrix, as a (1 x n)
        CSR matrix (row vector).

        Args:
            i (integer): Row

        Returns:
            cupyx.scipy.sparse.csc_matrix: Sparse matrix with single row
        r&   TrP   )Ú_minor_sliceÚslicer.   ©r   Úir   r   r   Úgetrowf  s   
zcsc_matrix.getrowc                 C   s   | j t||d ƒddS )zðReturns a copy of column i of the matrix, as a (m x 1)
        CSC matrix (column vector).

        Args:
            i (integer): Column

        Returns:
            cupyx.scipy.sparse.csc_matrix: Sparse matrix with single column
        r&   TrP   )Ú_major_slicerV   rW   r   r   r   Úgetcolr  s   
zcsc_matrix.getcolc                 C   s   t ||d ƒ}|  |¡ |¡S ©Nr&   )rV   Ú_major_index_fancyrU   ©r   ÚrowÚcolr   r   r   Ú_get_intXarray~  ó   zcsc_matrix._get_intXarrayc                 C   s,   t ||d ƒ}|jdv }|  |¡j||dS )Nr&   )r&   NrP   )rV   ÚsteprZ   rU   )r   r_   r`   rD   r   r   r   Ú_get_intXslice‚  s   
zcsc_matrix._get_intXslicec                 C   s"   t ||d ƒ}|  |¡j|ddS )Nr&   TrP   )rV   rZ   rU   r^   r   r   r   Ú_get_sliceXint‡  s   zcsc_matrix._get_sliceXintc                 C   ó   |   |¡ |¡S r   )r]   rU   r^   r   r   r   Ú_get_sliceXarray‹  ó   zcsc_matrix._get_sliceXarrayc                 C   s   t ||d ƒ}|  |¡ |¡S r\   )rV   rZ   Ú_minor_index_fancyr^   r   r   r   Ú_get_arrayXintŽ  rb   zcsc_matrix._get_arrayXintc                 C   rf   r   )rZ   ri   r^   r   r   r   Ú_get_arrayXslice’  rh   zcsc_matrix._get_arrayXslicer   )NN)F)NF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r9   r:   r=   rG   rN   rO   rJ   r.   rS   rT   rY   r[   ra   rd   re   rg   rj   rk   r   r   r   r   r      s0    
	^
-



	r   c                 C   s
   t | tƒS )zˆChecks if a given matrix is of CSC format.

    Returns:
        bool: Returns if ``x`` is :class:`cupyx.scipy.sparse.csc_matrix`.

    )Ú
isinstancer   )r   r   r   r   r2   –  s   
r2   )Zscipy.sparser   r	   ÚImportErrorr,   Zcupy_backends.cuda.apir   r   Zcupyx.scipy.sparser   r   r   Z_compressed_sparse_matrixr   r2   r   r   r   r   Ú<module>   s"    ÿ   
