a
    a                     @   sl   d Z dZddgZddlZddlmZ ddlmZm	Z	 dd	l
mZmZ dd
lmZ G dd deZdd ZdS )z&Compressed Sparse Column matrix formatzrestructuredtext en
csc_matrixisspmatrix_csc    N   )spmatrix)	csc_tocsr	expandptr)upcastget_index_dtype)
_cs_matrixc                   @   s   e Zd ZdZdZd!ddZejje_dd Zd"d	d
Zejje_d#ddZ	ej	je	_dd Z
ej
je
_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d  ZdS )$r   a
  
    Compressed Sparse Column matrix

    This can be instantiated in several ways:

        csc_matrix(D)
            with a dense matrix or rank-2 ndarray D

        csc_matrix(S)
            with another sparse matrix S (equivalent to S.tocsc())

        csc_matrix((M, N), [dtype])
            to construct an empty matrix with shape (M, N)
            dtype is optional, defaulting to dtype='d'.

        csc_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
            where ``data``, ``row_ind`` and ``col_ind`` satisfy the
            relationship ``a[row_ind[k], col_ind[k]] = data[k]``.

        csc_matrix((data, indices, indptr), [shape=(M, N)])
            is the standard CSC representation where the row indices for
            column i are stored in ``indices[indptr[i]:indptr[i+1]]``
            and their corresponding values are stored in
            ``data[indptr[i]:indptr[i+1]]``.  If the shape parameter is
            not supplied, the matrix dimensions are inferred from
            the index arrays.

    Attributes
    ----------
    dtype : dtype
        Data type of the matrix
    shape : 2-tuple
        Shape of the matrix
    ndim : int
        Number of dimensions (this is always 2)
    nnz
        Number of stored values, including explicit zeros
    data
        Data array of the matrix
    indices
        CSC format index array
    indptr
        CSC format index pointer array
    has_sorted_indices
        Whether indices are sorted

    Notes
    -----

    Sparse matrices can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.

    Advantages of the CSC format
        - efficient arithmetic operations CSC + CSC, CSC * CSC, etc.
        - efficient column slicing
        - fast matrix vector products (CSR, BSR may be faster)

    Disadvantages of the CSC format
      - slow row slicing operations (consider CSR)
      - changes to the sparsity structure are expensive (consider LIL or DOK)


    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import csc_matrix
    >>> csc_matrix((3, 4), dtype=np.int8).toarray()
    array([[0, 0, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 0]], dtype=int8)

    >>> row = np.array([0, 2, 2, 0, 1, 2])
    >>> col = np.array([0, 0, 1, 2, 2, 2])
    >>> data = np.array([1, 2, 3, 4, 5, 6])
    >>> csc_matrix((data, (row, col)), shape=(3, 3)).toarray()
    array([[1, 0, 4],
           [0, 0, 5],
           [2, 3, 6]])

    >>> indptr = np.array([0, 2, 3, 6])
    >>> indices = np.array([0, 2, 2, 0, 1, 2])
    >>> data = np.array([1, 2, 3, 4, 5, 6])
    >>> csc_matrix((data, indices, indptr), shape=(3, 3)).toarray()
    array([[1, 0, 4],
           [0, 0, 5],
           [2, 3, 6]])

    ZcscNFc                 C   sD   |d urt d| j\}}ddlm} || j| j| jf||f|dS )NzoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.r   
csr_matrixcopy)
ValueErrorshapecsrr   dataindicesindptr)selfZaxesr   MNr    r   `/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/sparse/csc.py	transposel   s    

zcsc_matrix.transposec                 c   s   |   E d H  d S N)tocsr)r   r   r   r   __iter__z   s    zcsc_matrix.__iter__c                 C   s   |r|   S | S d S r   r   )r   r   r   r   r   tocsc}   s    zcsc_matrix.tocscc           
   	   C   s   | j \}}t| j| jft| j|d}tj|d |d}tj| j|d}tj| jt| j	d}t
||| j|| j|| j||| ddlm} ||||f| j dd}	d|	_|	S )N)maxvalr   dtyper   F)r   r   T)r   r	   r   r   maxZnnznpemptyr   r!   r   Zastyper   r   r   Zhas_sorted_indices)
r   r   r   r   Z	idx_dtyper   r   r   r   Ar   r   r   r      s&    



zcsc_matrix.tocsrc           	      C   s   |  | j\}}| j}tjt|| jjd}t|| j| |  ||f\}}| j	dk}|| }|| }tj
|dd}|| }|| }||fS )Nr    r   Z	mergesort)kind)_swapr   r   r#   r$   lenr!   r   r   r   Zargsort)	r   Z	major_dimZ	minor_dimZminor_indicesZmajor_indicesrowcolZnz_maskindr   r   r   nonzero   s    
zcsc_matrix.nonzeroc                 C   sN   | j \}}t|}|dk r"||7 }|dk s2||kr>td| | j|d S )z]Returns a copy of row i of the matrix, as a (1 x n)
        CSR matrix (row vector).
        r   index (%d) out of rangeminor)r   int
IndexError_get_submatrixr   r   ir   r   r   r   r   getrow   s    
zcsc_matrix.getrowc                 C   sL   | j \}}t|}|dk r"||7 }|dk s2||kr>td| | j|ddS )zcReturns a copy of column i of the matrix, as a (m x 1)
        CSC matrix (column vector).
        r   r-   T)majorr   )r   r0   r1   r2   r3   r   r   r   getcol   s    
zcsc_matrix.getcolc                 C   s   |  |j|dS )Nr.   )_major_index_fancyr2   r   r)   r*   r   r   r   _get_intXarray   s    zcsc_matrix._get_intXarrayc                 C   s,   |j dv r| j||ddS | |j|dS )Nr   NTr6   r/   r   r.   )stepr2   _major_slicer9   r   r   r   _get_intXslice   s    
zcsc_matrix._get_intXslicec                 C   s,   |j dv r| j||ddS | j|d|S )Nr;   Tr<   r6   )r=   r2   _minor_slicer9   r   r   r   _get_sliceXint   s    
zcsc_matrix._get_sliceXintc                 C   s   |  ||S r   )r8   rA   r9   r   r   r   _get_sliceXarray   s    zcsc_matrix._get_sliceXarrayc                 C   s   | j |d|S )Nr@   )r2   _minor_index_fancyr9   r   r   r   _get_arrayXint   s    zcsc_matrix._get_arrayXintc                 C   s   |  ||S r   )r>   rD   r9   r   r   r   _get_arrayXslice   s    zcsc_matrix._get_arrayXslicec                 C   s   |d |d fS )zBswap the members of x if this is a column-oriented matrix
        r   r   r   )r   xr   r   r   r'      s    zcsc_matrix._swap)NF)F)F)__name__
__module____qualname____doc__formatr   r   r   r   r   r,   r
   r5   r7   r:   r?   rB   rC   rE   rF   r'   r   r   r   r   r      s(   Y






c                 C   s
   t | tS )a  Is x of csc_matrix type?

    Parameters
    ----------
    x
        object to check for being a csc matrix

    Returns
    -------
    bool
        True if x is a csc matrix, False otherwise

    Examples
    --------
    >>> from scipy.sparse import csc_matrix, isspmatrix_csc
    >>> isspmatrix_csc(csc_matrix([[5]]))
    True

    >>> from scipy.sparse import csc_matrix, csr_matrix, isspmatrix_csc
    >>> isspmatrix_csc(csr_matrix([[5]]))
    False
    )
isinstancer   )rG   r   r   r   r      s    )rK   Z__docformat____all__Znumpyr#   baser   Z_sparsetoolsr   r   Zsputilsr   r	   
compressedr
   r   r   r   r   r   r   <module>   s    \