o
    װi5                     @   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   K/home/ubuntu/.local/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   	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rt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
rtjrt 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%   transb   csrmvispmv   csrmm2spmmzcould not interpret dimensions)!r   r   cupyisscalarsum_duplicates
_with_datar   r   r   isspmatrix_csrr   tocsrr#   r   is_hipTr$   r&   AssertionErrorisspmatrix_csc
isspmatrixr   isdensendimr   get_build_versionr)   r*   asfortranarrayr,   r-   
ValueErrorNotImplemented)r   otherr   abr)   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)r5   eliminate_zerosr   r   r   )r   tcompressr   r   r   rD      s   zcsc_matrix.eliminate_zerosc                 C   s*   ddl m} | js||  d| _dS dS )zSorts the indices of this matrix *in place*.

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

        r   r   TN)r   r   has_sorted_indices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   uppernnzr.   zerosr   rK   copyhas_canonical_formatr0   r   r   r4   rM   ascontiguousarrayr=   	csr2denser5   	csc2dense)r   rL   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   r0   tocscr5   r   rY   rZ   NotImplementedError)r   r?   alphabetar   rZ   r   r   r   _add_sparse   s   


zcsc_matrix._add_sparseFc                 C   s@   ddl m} |r| j }| j }n| j}| j}|| ||S )zConverts 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   rR   r   csc2coo)r   rR   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.

        rR   )r   rR   r   r   r   r[     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   rc   rd   r\   )r   rR   r   rc   r   r   r   r3   .  s   

zcsc_matrix.tocsrc                 C   s   |   S )zInverts the format.
        )r3   )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   rR   )
r=   r   r   r   r   
csr_matrixr   r   r   rS   )r   axesrR   r   transr   r   r   	transposeO  s   zcsc_matrix.transposec                 C   s   | j t||d dd S )zReturns 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(   Trb   )_minor_sliceslicer3   r   ir   r   r   getrowf  s   
zcsc_matrix.getrowc                 C   s   | j t||d ddS )zReturns 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(   Trb   )_major_slicerk   rl   r   r   r   getcolr  s   
zcsc_matrix.getcolc                 C   s   t ||d }| ||S Nr(   )rk   _major_index_fancyrj   r   rowcolr   r   r   _get_intXarray~     zcsc_matrix._get_intXarrayc                 C   s,   t ||d }|jdv }| |j||dS )Nr(   )r(   Nrb   )rk   stepro   rj   )r   rt   ru   rR   r   r   r   _get_intXslice  s   
zcsc_matrix._get_intXslicec                 C   s"   t ||d }| |j|ddS )Nr(   Trb   )rk   ro   rj   rs   r   r   r   _get_sliceXint  s   zcsc_matrix._get_sliceXintc                 C      |  ||S r    )rr   rj   rs   r   r   r   _get_sliceXarray     zcsc_matrix._get_sliceXarrayc                 C   s   t ||d }| ||S rq   )rk   ro   _minor_index_fancyrs   r   r   r   _get_arrayXint  rw   zcsc_matrix._get_arrayXintc                 C   r{   r    )ro   r~   rs   r   r   r   _get_arrayXslice  r}   zcsc_matrix._get_arrayXslicer    )NN)F)NF)__name__
__module____qualname____doc__r   r   r   r"   rC   rD   rI   rX   r_   ra   r[   r3   re   ri   rn   rp   rv   ry   rz   r|   r   r   r   r   r   r   r      s0    
	^
-



	r   c                 C   s
   t | tS )zChecks 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   r7     s   
r7   )scipy.sparser   r	   ImportErrorr.   cupy_backends.cuda.apir   r   cupyx.scipy.sparser   r   r   _compressed_sparse_matrixr   r7   r   r   r   r   <module>   s"       
