a
    CCCf;=                     @   s   d Z dZg dZddlZddlmZ ddlmZm	Z	 ddl
mZmZmZmZ dd	lmZ dd
lmZ G dd deZdd ZG dd dee	ZG dd deeZdS )z#Compressed Sparse Row matrix formatzrestructuredtext en)	csr_array
csr_matrixisspmatrix_csr    N   )spmatrix)_spbasesparray)	csr_tocsc	csr_tobsrcsr_count_blocksget_csr_submatrix)upcast)
_cs_matrixc                   @   s   e 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
_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 )(	_csr_baseZcsrNFc                 C   sB   |d ur|dkrt d| j\}}| j| j| j| jf||f|dS )N)r   r   zvSparse arrays/matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.shapecopy)
ValueErrorr   _csc_containerdataindicesindptr)selfZaxesr   MN r   M/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/sparse/_csr.py	transpose   s    
z_csr_base.transposec                 C   s   | j | j| jd}|   | j| j| j  }}}|j|j }}t| jd D ]@}|| }	||d  }
||	|
 	 ||< ||	|
 	 ||< qL|S )Ndtyper   r   )
Z_lil_containerr   r   Zsum_duplicatesr   r   r   rowsrangetolist)r   r   ZlilZptrindZdatr    r   nstartendr   r   r   tolil!   s    z_csr_base.tolilc                 C   s   |r|   S | S d S Nr   )r   r   r   r   r   tocsr2   s    z_csr_base.tocsrc              	   C   s   | j | 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)_get_index_dtyper   r   maxZnnzr   npemptyr   r   r	   astyper   r   Zhas_sorted_indices)r   r   	idx_dtyper   r   r   Ar   r   r   tocsc:   s"    

z_csr_base.tocscTc                 C   sX  |d u 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rt	d| t
||||| j| j}	| j| 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   r-   )Z_spfuncsr6   tobsrr   Zreshaper   r   Z_bsr_containerr   r   r   r.   r/   r0   r1   zerosr   r
   r2   Zravel)r   r7   r   r6   Zarg1RCr   r   Zblksr3   r   r   r   r   r   r   r9   O   s4    
(



z_csr_base.tobsrc                 C   s   | S )zBswap the members of x if this is a column-oriented matrix
        r   xr   r   r   _swapu   s    z_csr_base._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   )r0   r:   r   r   r   r   r   	__class__)r   r   r   Zi0i1r   r   r   r   r   __iter__{   s    z_csr_base.__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   rA   r   r   ir   r   r   r   r   r   r   r   _getrow   s    

z_csr_base._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   rD   r   FrE   rF   rI   r   r   r   _getcol   s    

z_csr_base._getcolc                 C   s   |  ||S r(   )rK   _minor_index_fancyr   rowcolr   r   r   _get_intXarray   s    z_csr_base._get_intXarrayc              	   C   sB  |j dv 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)   r@   r   r   r8   FrE   )step_get_submatrixr   r   r   r   absr0   arraylenr/   rG   ceilfloatrA   r   )r   rO   rP   r   r   r%   stopZstrideiiZjjZrow_indicesZrow_datar#   Z
row_indptrr   r   r   r   _get_intXslice   s,    


$z_csr_base._get_intXslicec                 C   s,   |j dv r| j||ddS | |j|dS )NrR   Tr)   minor)rS   rT   _major_slicerN   r   r   r   _get_sliceXint   s    
z_csr_base._get_sliceXintc                 C   s   |  ||S r(   )r_   rM   rN   r   r   r   _get_sliceXarray   s    z_csr_base._get_sliceXarrayc                 C   s   |  |j|dS )Nr]   )_major_index_fancyrT   rN   r   r   r   _get_arrayXint   s    z_csr_base._get_arrayXintc                 C   s>   |j dvr,tj|| jd  }| ||S | |j|dS )NrR   r   r]   )rS   r0   Zaranger   r   Z_get_arrayXarrayrb   rT   rN   r   r   r   _get_arrayXslice   s    
z_csr_base._get_arrayXslice)NF)F)F)F)NT)__name__
__module____qualname___formatr   r   __doc__r'   r*   r5   r9   staticmethodr?   rC   rK   rL   rQ   r\   r`   ra   rc   rd   r   r   r   r   r      s,   









"

!r   c                 C   s
   t | 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_array, csr_matrix, coo_matrix, isspmatrix_csr
    >>> isspmatrix_csr(csr_matrix([[5]]))
    True
    >>> isspmatrix_csr(csr_array([[5]]))
    False
    >>> isspmatrix_csr(coo_matrix([[5]]))
    False
    )
isinstancer   r=   r   r   r   r      s    r   c                   @   s   e Zd ZdZdS )r   a  
    Compressed Sparse Row array.

    This can be instantiated in several ways:
        csr_array(D)
            where D is a 2-D ndarray

        csr_array(S)
            with another sparse array or matrix S (equivalent to S.tocsr())

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

        csr_array((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_array((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 array dimensions
            are inferred from the index arrays.

    Attributes
    ----------
    dtype : dtype
        Data type of the array
    shape : 2-tuple
        Shape of the array
    ndim : int
        Number of dimensions (this is always 2)
    nnz
    size
    data
        CSR format data array of the array
    indices
        CSR format index array of the array
    indptr
        CSR format index pointer array of the array
    has_sorted_indices
    has_canonical_format
    T

    Notes
    -----

    Sparse arrays 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)

    Canonical Format
        - Within each row, indices are sorted by column.
        - There are no duplicate entries.

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import csr_array
    >>> csr_array((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_array((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_array((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_array((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 array incrementally,
    the following snippet builds a term-document array 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_array((data, indices, indptr), dtype=int).toarray()
    array([[2, 1, 0, 0],
           [0, 1, 1, 1]])

    Nre   rf   rg   ri   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZdS )r   a  
    Compressed Sparse Row matrix.

    This can be instantiated in several ways:
        csr_matrix(D)
            where D is a 2-D ndarray

        csr_matrix(S)
            with another sparse array or 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
    size
    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
    has_canonical_format
    T

    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)

    Canonical Format
        - Within each row, indices are sorted by column.
        - There are no duplicate entries.

    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]])

    Nrl   r   r   r   r   r   r  s   r   )ri   __docformat____all__numpyr0   Z_matrixr   _baser   r   Z_sparsetoolsr	   r
   r   r   Z_sputilsr   Z_compressedr   r   r   r   r   r   r   r   r   <module>   s    K{