a
    aV                     @   s   d Z dZddgZddlmZ ddlZddlmZm	Z	m
Z
 dd	lmZmZmZ dd
lmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZ ddlZG dd deeZdd Z dS )z2 A sparse matrix in COOrdinate or 'triplet' formatzrestructuredtext en
coo_matrixisspmatrix_coo    )warnN   )	coo_tocsrcoo_todense
coo_matvec)
isspmatrixSparseEfficiencyWarningspmatrix)_data_matrix_minmax_mixin)upcastupcast_char	to_nativeisshapegetdtypegetdataget_index_dtypedowncast_intp_indexcheck_shapecheck_reshape_kwargsmatrixc                   @   s"  e Zd ZdZdZd1ddZdd Zejje_d2d	d
Zejje_dd Z	d3ddZ
ej
je
_dd Zejje_d4ddZd5ddZd6ddZd7ddZejje_d8ddZejje_d9ddZejje_d:ddZejje_d d! Zd;d#d$Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 ZdS )<r   a  
    A sparse matrix in COOrdinate format.

    Also known as the 'ijv' or 'triplet' format.

    This can be instantiated in several ways:
        coo_matrix(D)
            with a dense matrix D

        coo_matrix(S)
            with another sparse matrix S (equivalent to S.tocoo())

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

        coo_matrix((data, (i, j)), [shape=(M, N)])
            to construct from three arrays:
                1. data[:]   the entries of the matrix, in any order
                2. i[:]      the row indices of the matrix entries
                3. j[:]      the column indices of the matrix entries

            Where ``A[i[k], j[k]] = data[k]``.  When shape is not
            specified, it is 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
        COO format data array of the matrix
    row
        COO format row index array of the matrix
    col
        COO format column index array of the matrix

    Notes
    -----

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

    Advantages of the COO format
        - facilitates fast conversion among sparse formats
        - permits duplicate entries (see example)
        - very fast conversion to and from CSR/CSC formats

    Disadvantages of the COO format
        - does not directly support:
            + arithmetic operations
            + slicing

    Intended Usage
        - COO is a fast format for constructing sparse matrices
        - Once a matrix has been constructed, convert to CSR or
          CSC format for fast arithmetic and matrix vector operations
        - By default when converting to CSR or CSC format, duplicate (i,j)
          entries will be summed together.  This facilitates efficient
          construction of finite element matrices and the like. (see example)

    Examples
    --------

    >>> # Constructing an empty matrix
    >>> from scipy.sparse import coo_matrix
    >>> coo_matrix((3, 4), dtype=np.int8).toarray()
    array([[0, 0, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 0]], dtype=int8)

    >>> # Constructing a matrix using ijv format
    >>> row  = np.array([0, 3, 1, 0])
    >>> col  = np.array([0, 3, 1, 2])
    >>> data = np.array([4, 5, 7, 9])
    >>> coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
    array([[4, 0, 9, 0],
           [0, 7, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 5]])

    >>> # Constructing a matrix with duplicate indices
    >>> row  = np.array([0, 0, 1, 3, 1, 0, 0])
    >>> col  = np.array([0, 2, 1, 3, 1, 0, 0])
    >>> data = np.array([1, 1, 1, 1, 1, 1, 1])
    >>> coo = coo_matrix((data, (row, col)), shape=(4, 4))
    >>> # Duplicate indices are maintained until implicitly or explicitly summed
    >>> np.max(coo.data)
    1
    >>> coo.toarray()
    array([[3, 0, 1, 0],
           [0, 2, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 1]])

    cooNFc              
   C   s  t |  t|trt|r|\}}t||f| _tt||d}t	|t
d}tjg |d| _tjg |d| _tjg |d| _d| _qz|\}	\}
}W n2 ttfy } ztd|W Y d }~n
d }~0 0 |d u r0t|
dkst|dkrtdtt|
d }tt|d }t||f| _n|\}}t||f| _tt| jd}tj|
||d	| _tj|||d	| _t|	||d	| _d
| _nt|rt|r|r|j | _|j | _|j | _t|j| _n,| }|j| _|j| _|j| _t|j| _d
| _ntt|}|jdkr8tdt|j| _|d urpt|| jkrptd|| jf | \| _| _|| j| jf | _d| _|d ur| jj |d
d| _| !  d S )Nmaxval)defaultdtypeTzinvalid input formatr   z4cannot infer dimensions from zero sized index arraysr   )copyr   F   z'expected dimension <= 2 array or matrixzinconsistent shapes: %s != %sr   )"r   __init__
isinstancetupler   r   _shaper   maxr   floatnparrayrowcoldatahas_canonical_format	TypeError
ValueErrorlenoperatorindexshaper   r	   r   r   tocooZ
atleast_2dasarrayndimnonzeroastype_check)selfZarg1r3   r   r   MN	idx_dtypeZ
data_dtypeobjr*   r+   er    r@   `/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/scipy/sparse/coo.pyr"   ~   sn    

 




zcoo_matrix.__init__c                 O   s&  t || j}t|\}}|| jkr2|r.|  S | S | j\}}|dkrt|td|d  td|d  d}tj|| j|d| j	 }	t
|	|d \}
}nb|dkrt|td|d  td|d  d}tj|| j	|d| j }	t
|	|d \}}
ntd|r
| j }n| j}t||
|ff|dd	S )
NCr   r   r   r   Fz'order' must be 'C' or 'F'Fr3   r   )r   r3   r   r   r   r&   r(   multiplyr*   r+   divmodr/   r,   r   )r:   argskwargsr3   orderr   ZnrowsZncolsr   Zflat_indicesnew_rownew_colnew_datar@   r@   rA   reshape   s,    

&&zcoo_matrix.reshapec                 C   s   |d u rjt | j}|t | jks.|t | jkr6td| jjdksZ| jjdksZ| jjdkrbtdt|S |dk rz|d7 }|dkrtjt	| j| j
d dS |dkrtjt	| j| j
d dS tdd S )Nz7row, column, and data array must all be the same lengthr   z(row, column, and data arrays must be 1-Dr   r    )Z	minlengthzaxis out of bounds)r0   r,   r*   r+   r/   r6   intr(   Zbincountr   r3   )r:   Zaxisnnzr@   r@   rA   getnnz   s(    

zcoo_matrix.getnnzc                 C   s   | j jjdkr td| j jj  | jjjdkr@td| jjj  tt| jd}t	j
| j |d| _ t	j
| j|d| _t| j| _| jdkr| j  | jd krtd| j | jd krtd	| j  dk rtd
| j dk rtddS )z' Checks data structure for consistency iz,row index array has non-integer dtype (%s)  z+col index array has non-integer dtype (%s) r   r   r   z#row index exceeds matrix dimensionsr   z&column index exceeds matrix dimensionsznegative row index foundznegative column index foundN)r*   r   kindr   namer+   r   r&   r3   r(   r5   r   r,   rO   r/   min)r:   r=   r@   r@   rA   r9   	  s*    
zcoo_matrix._checkc                 C   s:   |d urt d| j\}}t| j| j| jff||f|dS )NzoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.rD   )r/   r3   r   r,   r+   r*   )r:   Zaxesr   r;   r<   r@   r@   rA   	transpose#  s    
zcoo_matrix.transposec                 G   sx   t |}|\}}| j\}}||k s*||k rnt| j|k | j|k }| sn| j| | _| j| | _| j| | _|| _d S N)	r   r3   r(   logical_andr*   r+   allr,   r%   )r:   r3   Znew_MZnew_Nr;   r<   maskr@   r@   rA   resize/  s    
zcoo_matrix.resizec              
   C   s^   |  ||}t|jj}|s,|jjs,td| j\}}t||| j| j	| j
| j|d| |S )z)See the docstring for `spmatrix.toarray`.z&Output array must be C or F contiguousA)Z_process_toarray_argsrN   flagsf_contiguousc_contiguousr/   r3   r   rO   r*   r+   r,   ravel)r:   rI   outBfortranr;   r<   r@   r@   rA   toarray?  s    

zcoo_matrix.toarrayc              
   C   s   ddl m} | jdkr&|| j| jdS | j\}}t| j| jft| j|d}| jj	|dd}| jj	|dd}t
j|d |d}t
j||d}	t
j| jt| jd}
t||| j||| j||	|
	 ||
|	|f| jd}| js|  |S d	S )
aM  Convert this matrix to Compressed Sparse Column format

        Duplicate entries will be summed together.

        Examples
        --------
        >>> from numpy import array
        >>> from scipy.sparse import coo_matrix
        >>> row  = array([0, 0, 1, 3, 1, 0, 0])
        >>> col  = array([0, 2, 1, 3, 1, 0, 0])
        >>> data = array([1, 1, 1, 1, 1, 1, 1])
        >>> A = coo_matrix((data, (row, col)), shape=(4, 4)).tocsc()
        >>> A.toarray()
        array([[3, 0, 1, 0],
               [0, 2, 0, 0],
               [0, 0, 0, 0],
               [0, 0, 0, 1]])

        r   )
csc_matrixr   r   r   Fr!   r3   N)Zcscrd   rO   r3   r   r   r+   r*   r&   r8   r(   empty
empty_liker,   r   r   r-   sum_duplicates)r:   r   rd   r;   r<   r=   r*   r+   indptrindicesr,   xr@   r@   rA   tocscJ  s&    


zcoo_matrix.tocscc              
   C   s   ddl m} | jdkr&|| j| jdS | j\}}t| j| jft| j|d}| jj	|dd}| jj	|dd}t
j|d |d}t
j||d}	t
j| jt| jd}
t||| j||| j||	|
	 ||
|	|f| jd}| js|  |S d	S )
aJ  Convert this matrix to Compressed Sparse Row format

        Duplicate entries will be summed together.

        Examples
        --------
        >>> from numpy import array
        >>> from scipy.sparse import coo_matrix
        >>> row  = array([0, 0, 1, 3, 1, 0, 0])
        >>> col  = array([0, 2, 1, 3, 1, 0, 0])
        >>> data = array([1, 1, 1, 1, 1, 1, 1])
        >>> A = coo_matrix((data, (row, col)), shape=(4, 4)).tocsr()
        >>> A.toarray()
        array([[3, 0, 1, 0],
               [0, 2, 0, 0],
               [0, 0, 0, 0],
               [0, 0, 0, 1]])

        r   )
csr_matrixr   r   r   Fr!   re   N)Zcsrrm   rO   r3   r   r   r*   r+   r&   r8   r(   rf   rg   r,   r   r   r-   rh   )r:   r   rm   r;   r<   r=   r*   r+   ri   rj   r,   rk   r@   r@   rA   tocsrt  s&    


zcoo_matrix.tocsrc                 C   s   |r|   S | S d S rV   r!   )r:   r   r@   r@   rA   r4     s    zcoo_matrix.tocooc                 C   s   ddl m} |   | j| j }tj|dd\}}t|dkrPtdt| t	 | j
jdkrntjd| jd	}n2tjt|| j d f| jd	}| j
||| jf< |||f| jd
S )Nr   )
dia_matrixT)Zreturn_inversed   z:Constructing a DIA matrix with %d diagonals is inefficientr   )r   r   r   re   )Zdiaro   rh   r+   r*   r(   uniquer0   r   r
   r,   sizezerosr   r&   r3   )r:   r   ro   ksZdiagsZdiag_idxr,   r@   r@   rA   todia  s    "zcoo_matrix.todiac                 C   sD   ddl m} |   || j| jd}|tt| j| j| j	 |S )Nr   )
dok_matrixr   )
dokrv   rh   r3   r   _updatezipr*   r+   r,   )r:   r   rv   rw   r@   r@   rA   todok  s
    zcoo_matrix.todokr   c           	      C   s   | j \}}|| ks||kr.tjd| jjdS tjt|t|d |t|d | jd}| j| | j	k}| j
r| j| }| j| }n&| | j| | j	| | j| \}}}|||t|d < |S Nr   r   )r3   r(   rf   r,   r   rs   rT   r&   r*   r+   r-   _sum_duplicates)	r:   krowscolsZdiagZ	diag_maskr*   r,   _r@   r@   rA   diagonal  s     
 

zcoo_matrix.diagonalc                 C   sl  | j \}}|jrt|sd S | jj}| j| j |k}|dk rt|| |}|jr^t|t|}t|| j|k}tj	| | | |d}	tj	||d}
nVt||| }|jrt|t|}t|| j|k}tj	||d}	tj	||| |d}
|jr|d | }ntj
|| jd}||d d < t| j| |	f| _t| j| |
f| _t| j| |f| _d| _d S )Nr   r   F)r3   r6   r0   r*   r   r+   rT   r(   
logical_orZarangerf   Zconcatenater,   r-   )r:   valuesr}   r;   r<   r=   Z	full_keepZ	max_indexZkeeprJ   rK   rL   r@   r@   rA   _setdiag  s4    
zcoo_matrix._setdiagTc                 C   sL   |r*t || j | j ff| j|jdS t || j| jff| j|jdS dS )zReturns a matrix with the same sparsity structure as self,
        but with different data.  By default the index arrays
        (i.e. .row and .col) are copied.
        )r3   r   N)r   r*   r   r+   r3   r   )r:   r,   r   r@   r@   rA   
_with_data  s    zcoo_matrix._with_datac                 C   s8   | j r
dS | | j| j| j}|\| _| _| _d| _ dS )zlEliminate duplicate matrix entries by adding them together

        This is an *in place* operation
        NT)r-   r|   r*   r+   r,   )r:   Zsummedr@   r@   rA   rh     s
    zcoo_matrix.sum_duplicatesc                 C   s   t |dkr|||fS t||f}|| }|| }|| }|dd  |d d k|dd  |d d kB }td|}|| }|| }t|\}tjj||| jd}|||fS )Nr   r   Tr   )r0   r(   Zlexsortappendr7   addZreduceatr   )r:   r*   r+   r,   rI   Zunique_maskZunique_indsr@   r@   rA   r|     s    
zcoo_matrix._sum_duplicatesc                 C   s2   | j dk}| j | | _ | j| | _| j| | _dS )zURemove zero entries from the matrix

        This is an *in place* operation
        r   N)r,   r*   r+   )r:   rY   r@   r@   rA   eliminate_zeros*  s    
zcoo_matrix.eliminate_zerosc              
   C   s   |j | j kr td| j |j t| jj|jj}tj||dd}t|j	j
}| j \}}t||| j| j| j| j|d| t|ddS )NzIncompatible shapes ({} and {})T)r   r   r[   Fr!   )r3   r/   formatr   r   charr(   r)   rN   r\   r]   r   rO   r*   r+   r,   r_   r   )r:   otherr   resultrb   r;   r<   r@   r@   rA   
_add_dense8  s    

zcoo_matrix._add_densec                 C   s@   t j| jd t| jj|jjd}t| j| j| j	| j
|| |S r{   )r(   rs   r3   r   r   r   r   rO   r*   r+   r,   )r:   r   r   r@   r@   rA   _mul_vectorD  s
    zcoo_matrix._mul_vectorc              	   C   sp   t j|jd | jd ft| jj|jjd}t|jD ]&\}}t| j	| j
| j| j|||  q6|jjt|dS )Nr   r   r   )type)r(   rs   r3   r   r   r   	enumerateTr   rO   r*   r+   r,   viewr   )r:   r   r   rQ   r+   r@   r@   rA   _mul_multivectorK  s     zcoo_matrix._mul_multivector)NNF)N)NF)NN)F)F)F)F)F)r   )T)__name__
__module____qualname____doc__r   r"   rM   r   rP   r9   rU   rZ   rc   rl   rn   r4   ru   rz   r   r   r   r   rh   r|   r   r   r   r   r@   r@   r@   rA   r      s>   e
H'








*
*




	


%
c                 C   s
   t | tS )a  Is x of coo_matrix type?

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

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

    Examples
    --------
    >>> from scipy.sparse import coo_matrix, isspmatrix_coo
    >>> isspmatrix_coo(coo_matrix([[5]]))
    True

    >>> from scipy.sparse import coo_matrix, csr_matrix, isspmatrix_coo
    >>> isspmatrix_coo(csr_matrix([[5]]))
    False
    )r#   r   )rk   r@   r@   rA   r   S  s    )!r   Z__docformat____all__warningsr   Znumpyr(   Z_sparsetoolsr   r   r   baser	   r
   r   r,   r   r   Zsputilsr   r   r   r   r   r   r   r   r   r   r   r1   r   r   r@   r@   r@   rA   <module>   s   4    A