U
    À»|e£-  ã                   @   st   d Z dZddgZddlZddlmZ ddlmZm	Z	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 Row matrix formatzrestructuredtext enÚ
csr_matrixÚisspmatrix_csré    Né   )Úspmatrix)Ú	csr_tocscÚ	csr_tobsrÚcsr_count_blocksÚget_csr_submatrix)Ú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d„Zejje_d&d	d
„Zejje_d'dd„Z	ej	je	_d(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"d#„ ZdS ))r   a|  
    Compressed Sparse Row matrix

    This can be instantiated in several ways:
        csr_matrix(D)
            with a dense matrix or rank-2 ndarray D

        csr_matrix(S)
            with another sparse matrix S (equivalent to S.tocsr())

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

        csr_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]``.

        csr_matrix((data, indices, indptr), [shape=(M, N)])
            is the standard CSR representation where the column indices for
            row 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
        CSR format data array of the matrix
    indices
        CSR format index array of the matrix
    indptr
        CSR format index pointer array of the matrix
    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 CSR format
      - efficient arithmetic operations CSR + CSR, CSR * CSR, etc.
      - efficient row slicing
      - fast matrix vector products

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

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import csr_matrix
    >>> csr_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, 0, 1, 2, 2, 2])
    >>> col = np.array([0, 2, 2, 0, 1, 2])
    >>> data = np.array([1, 2, 3, 4, 5, 6])
    >>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
    array([[1, 0, 2],
           [0, 0, 3],
           [4, 5, 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])
    >>> csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
    array([[1, 0, 2],
           [0, 0, 3],
           [4, 5, 6]])

    Duplicate entries are summed together:

    >>> row = np.array([0, 1, 2, 0])
    >>> col = np.array([0, 1, 1, 0])
    >>> data = np.array([1, 2, 4, 8])
    >>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
    array([[9, 0, 0],
           [0, 2, 0],
           [0, 4, 0]])

    As an example of how to construct a CSR matrix incrementally,
    the following snippet builds a term-document matrix from texts:

    >>> docs = [["hello", "world", "hello"], ["goodbye", "cruel", "world"]]
    >>> indptr = [0]
    >>> indices = []
    >>> data = []
    >>> vocabulary = {}
    >>> for d in docs:
    ...     for term in d:
    ...         index = vocabulary.setdefault(term, len(vocabulary))
    ...         indices.append(index)
    ...         data.append(1)
    ...     indptr.append(len(indices))
    ...
    >>> csr_matrix((data, indices, indptr), dtype=int).toarray()
    array([[2, 1, 0, 0],
           [0, 1, 1, 1]])

    ÚcsrNFc                 C   s:   |d k	rt dƒ‚| j\}}| j| j| j| jf||f|d�S )NzoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.©ÚshapeÚcopy)Ú
ValueErrorr   Ú_csc_containerÚdataÚindicesÚindptr)ÚselfÚaxesr   ÚMÚN© r   úN/var/www/website-v5/atlas_env/lib/python3.8/site-packages/scipy/sparse/_csr.pyÚ	transpose‡   s    
ÿ ÿzcsr_matrix.transposec                 C   s’   | j | j| jd�}|  ¡  | j| j| j  }}}|j|j }}t| jd ƒD ]@}|| }	||d  }
||	|
…  	¡ ||< ||	|
…  	¡ ||< qL|S )N©Údtyper   r   )
Ú_lil_containerr   r   Úsum_duplicatesr   r   r   ÚrowsÚrangeÚtolist)r   r   ÚlilÚptrÚindZdatr!   r   ÚnÚstartÚendr   r   r   Útolil“   s    zcsr_matrix.tolilc                 C   s   |r|   ¡ S | S d S ©N©r   )r   r   r   r   r   Útocsr¤   s    zcsr_matrix.tocsrc              	   C   s´   t | j| jft| j| jd ƒd�}tj| jd d |d�}tj| j|d�}tj| jt| j	ƒd�}t
| jd | jd | j |¡| j |¡| j|||ƒ | j|||f| jd�}d|_|S )Nr   ©Úmaxvalr   r   ©r   T)r   r   r   ÚmaxÚnnzr   ÚnpÚemptyr
   r   r   Úastyper   r   Úhas_sorted_indices)r   r   Ú	idx_dtyper   r   r   ÚAr   r   r   Útocsc¬   s"    ÿ

úzcsr_matrix.tocscTc                 C   sV  |d kr$ddl m} | j|| ƒd�S |dkrX| j ddd¡| j| jf}| j|| j|d�S |\}}| j\}}|dk s’|dk s’|| dks’|| dkržt	d| ƒ‚t
||||| j| jƒ}	t| j| jft|| |	ƒd	�}
tj|| d |
d
�}tj|	|
d
�}tj|	||f| jd
�}t||||| j |
¡| j |
¡| j||| ¡ ƒ
 | j|||f| jd�S d S )Nr   )Úestimate_blocksize)Ú	blocksize)r   r   éÿÿÿÿr   r   zinvalid blocksize %sr.   r   r0   )Z_spfuncsr:   Útobsrr   Úreshaper   r   Ú_bsr_containerr   r   r   r   r1   r3   r4   Úzerosr   r   r5   Úravel)r   r;   r   r:   Úarg1ÚRÚCr   r   Zblksr7   r   r   r   r   r   r   r=   Á   s:    
(ÿ


  ü ÿzcsr_matrix.tobsrc                 C   s   |S )zBswap the members of x if this is a column-oriented matrix
        r   )r   Úxr   r   r   Ú_swapç   s    zcsr_matrix._swapc                 c   s€   t jd| jjd�}d| jd f}d}| jdd … D ]H}|| |d< | j||… }| j||… }| j|||f|dd�V  |}q2d S )Né   r   r   r   Tr   )r3   r@   r   r   r   r   r   Ú	__class__)r   r   r   Úi0Úi1r   r   r   r   r   Ú__iter__ì   s      ÿzcsr_matrix.__iter__c              
   C   s„   | j \}}t|ƒ}|dk r"||7 }|dk s2||kr>td| ƒ‚t||| j| j| j||d d|ƒ	\}}}| j|||fd|f| jd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 ranger   F©r   r   r   ©	r   ÚintÚ
IndexErrorr	   r   r   r   rH   r   ©r   Úir   r   r   r   r   r   r   r   Úgetrowù   s*    
        ÿ
 ÿzcsr_matrix.getrowc                 C   s„   | j \}}t|ƒ}|dk r"||7 }|dk s2||kr>td| ƒ‚t||| j| j| jd|||d ƒ	\}}}| j|||f|df| jdd�S )zcReturns a copy of column i of the matrix, as a (m x 1)
        CSR matrix (column vector).
        r   rL   r   FrM   rN   rQ   r   r   r   Úgetcol  s*    
        ÿ
 ÿzcsr_matrix.getcolc                 C   s   |   |¡ |¡S r+   )rS   Ú_minor_index_fancy©r   ÚrowÚcolr   r   r   Ú_get_intXarray  s    zcsr_matrix._get_intXarrayc              	   C   sB  |j dkr| j||dd�S | j\}}| |¡\}}}| j||d … \}}	| j||	… }
| j||	… }|dkr€|
|k|
|k @ }n|
|k|
|k@ }t|ƒdkr°||
| | dkM }|
| | | }
|| }t dt	|
ƒg¡}|dk �r|d d d… }t|
d d d… ƒ}
dt
dtt t|| ƒ| ¡ƒƒf}| j||
|f|| jdd	�S )
N©r   NTr,   rG   r   r   r<   FrM   )ÚstepÚ_get_submatrixr   r   r   r   Úabsr3   ÚarrayÚlenr1   rO   ÚceilÚfloatrH   r   )r   rW   rX   r   r   r(   ÚstopÚstrideÚiiZjjÚrow_indicesÚrow_datar&   Z
row_indptrr   r   r   r   Ú_get_intXslice  s.    


$ ÿzcsr_matrix._get_intXslicec                 C   s,   |j dkr| j||dd�S |  |¡j|d�S )NrZ   Tr,   ©Úminor)r[   r\   Ú_major_slicerV   r   r   r   Ú_get_sliceXint;  s    
zcsr_matrix._get_sliceXintc                 C   s   |   |¡ |¡S r+   )rj   rU   rV   r   r   r   Ú_get_sliceXarray@  s    zcsr_matrix._get_sliceXarrayc                 C   s   |   |¡j|d�S )Nrh   )Ú_major_index_fancyr\   rV   r   r   r   Ú_get_arrayXintC  s    zcsr_matrix._get_arrayXintc                 C   s>   |j dkr,tj| | jd ¡Ž }|  ||¡S |  |¡j|d�S )NrZ   r   rh   )r[   r3   Úaranger   r   Z_get_arrayXarrayrm   r\   rV   r   r   r   Ú_get_arrayXsliceF  s    
zcsr_matrix._get_arrayXslice)NF)F)F)F)NT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úformatr   r   r*   r-   r9   r=   rF   rK   rS   rT   rY   rg   rk   rl   rn   rp   r   r   r   r   r      s,   s









"
!c                 C   s    ddl m} t| tƒpt| |ƒS )aÍ  Is x of csr_matrix type?

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

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

    Examples
    --------
    >>> from scipy.sparse import csr_matrix, isspmatrix_csr
    >>> isspmatrix_csr(csr_matrix([[5]]))
    True

    >>> from scipy.sparse import csc_matrix, csr_matrix, isspmatrix_csc
    >>> isspmatrix_csr(csc_matrix([[5]]))
    False
    r   )Ú	csr_array)Ú_arraysrv   Ú
isinstancer   )rE   rv   r   r   r   r   M  s    )rt   Ú__docformat__Ú__all__Únumpyr3   Ú_baser   Z_sparsetoolsr   r   r   r	   Ú_sputilsr
   r   Z_compressedr   r   r   r   r   r   r   Ú<module>   s     >