U
    É»|ežV  ã                   @   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 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_kwargsc                   @   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
    >>> import numpy as np
    >>> 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ƒ�r’t|ƒrŠ|\}}t||fƒ| _tt||ƒd�}t	|t
d�}tjg |d�| _tjg |d�| _tjg |d�| _d| _�q–z|\}	\}
}W n0 ttfk
rÊ } ztdƒ|‚W 5 d }~X Y nX |d k�r.t|
ƒdksît|ƒdkröt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
| _n„t t |¡¡}|jdk�r6tdƒ‚t|jƒ| _|d k	�rnt|ƒ| jk�rntd|| jf ƒ‚| ¡ \| _| _|| j| jf | _d| _|d k	�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   ÚtocooÚ
atleast_2dÚasarrayÚndimÚnonzeroÚastypeÚ_check)ÚselfÚarg1r2   r   r   ÚMÚNÚ	idx_dtypeZ
data_dtypeÚobjr)   r*   Úer   © rA   úN/var/www/website-v5/atlas_env/lib/python3.8/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r–t|td|d ƒ td|d ƒ d�}tj|| j|d�| j	 }	t
|	|d ƒ\}
}nb|dkrðt|td|d ƒ td|d ƒ d�}tj|| j	|d�| j }	t
|	|d ƒ\}}
ntdƒ‚|�r
| j ¡ }n| j}| j||
|ff|dd	�S )
NÚCr   r   r   r   ÚFz'order' must be 'C' or 'F'F©r2   r   )r   r2   r   r   r   r%   r'   Úmultiplyr)   r*   Údivmodr.   r+   Ú	__class__)r:   ÚargsÚkwargsr2   Úorderr   ÚnrowsÚncolsr   Zflat_indicesÚnew_rowÚnew_colÚnew_datarA   rA   rB   ÚreshapeÇ   s.    

&& ÿzcoo_matrix.reshapec                 C   sÊ   |d k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rœtjt	| jƒ| j
d d�S |dkr¾t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   )Ú	minlengthzaxis out of bounds)r/   r+   r)   r*   r.   r6   Úintr'   Úbincountr   r2   )r:   ÚaxisÚnnzrA   rA   rB   Ú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r¦tdƒ‚| j ¡ | jd krÂtd	ƒ‚| j  ¡ dk rØtd
ƒ‚| j ¡ dk rît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%   r2   r'   r5   r   r+   rV   r.   Úmin)r:   r>   rA   rA   rB   r9   
  s*    ÿÿ
zcoo_matrix._checkc                 C   s<   |d k	rt dƒ‚| j\}}| j| j| j| jff||f|d�S )NzoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.rE   )r.   r2   rH   r+   r*   r)   )r:   Úaxesr   r<   r=   rA   rA   rB   Ú	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   r2   r'   Úlogical_andr)   r*   Úallr+   r$   )r:   r2   Únew_MÚnew_Nr<   r=   ÚmaskrA   rA   rB   Úresize0  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)Ú_process_toarray_argsrS   ÚflagsÚf_contiguousÚc_contiguousr.   r2   r   rV   r)   r*   r+   Úravel)r:   rK   ÚoutÚBÚfortranr<   r=   rA   rA   rB   Útoarray@  s    
 ÿzcoo_matrix.toarrayc              
   C   sà   | j dkr| j| 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|||	ƒ	 | 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   r   r   Fr    r   ©r2   N)rV   Ú_csc_containerr2   r   r   r*   r)   r%   r8   r'   ÚemptyÚ
empty_liker+   r   r   r,   Úsum_duplicates©r:   r   r<   r=   r>   r)   r*   ÚindptrÚindicesr+   ÚxrA   rA   rB   ÚtocscK  s(    


ÿ  ÿzcoo_matrix.tocscc              
   C   sà   | j dkr| j| 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|||	ƒ	 | 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   r   r   Fr    r   ro   N)rV   Ú_csr_containerr2   r   r   r)   r*   r%   r8   r'   rq   rr   r+   r   r   r,   rs   rt   rA   rA   rB   Útocsrt  s(    


ÿ  ÿzcoo_matrix.tocsrc                 C   s   |r|   ¡ S | S d S r^   r    )r:   r   rA   rA   rB   r3   �  s    zcoo_matrix.tocooc                 C   s¨   |   ¡  | j| j }tj|dd�\}}t|ƒdkrDtdt|ƒ tƒ | jj	dkrbtj
d| jd�}n2tj
t|ƒ| j ¡ d f| jd�}| j||| jf< | j||f| jd	�S )
NT)Úreturn_inverseéd   z:Constructing a DIA matrix with %d diagonals is inefficientr   )r   r   r   r   ro   )rs   r*   r)   r'   Úuniquer/   r   r
   r+   ÚsizeÚzerosr   r%   Ú_dia_containerr2   )r:   r   ÚksZdiagsZdiag_idxr+   rA   rA   rB   Útodia¥  s    ÿÿ"zcoo_matrix.todiac                 C   s:   |   ¡  | j| j| jd�}| tt| j| jƒ| jƒ¡ |S )Nr   )	rs   Ú_dok_containerr2   r   Ú_updateÚzipr)   r*   r+   )r:   r   ÚdokrA   rA   rB   Ú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   )r2   r'   rq   r+   r   r   r[   r%   r)   r*   r,   Ú_sum_duplicates)	r:   ÚkÚrowsÚcolsÚdiagZ	diag_maskr)   r+   Ú_rA   rA   rB   ÚdiagonalÃ  s     
 ÿ
þ
zcoo_matrix.diagonalc                 C   sl  | j \}}|jrt|ƒsd S | jj}| j| j |k}|dk r˜t|| |ƒ}|jr^t|t|ƒƒ}t || j|k¡}tj	| | | |d�}	tj	||d�}
nVt||| ƒ}|jrºt|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)r2   r6   r/   r)   r   r*   r[   r'   Ú
logical_orÚarangerq   Úconcatenater+   r,   )r:   ÚvaluesrŠ   r<   r=   r>   Z	full_keepÚ	max_indexÚkeeprN   rO   rP   rA   rA   rB   Ú_setdiagØ  s4    
zcoo_matrix._setdiagTc                 C   sP   |r,| j || j ¡ | j ¡ ff| j|jd�S | j || j| jff| j|jd�S dS )zªReturns 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.
        )r2   r   N)rH   r)   r   r*   r2   r   )r:   r+   r   rA   rA   rB   Ú
_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summedrA   rA   rB   rs   	  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   )r/   r'   ÚlexsortÚappendr7   ÚaddÚreduceatr   )r:   r)   r*   r+   rK   Zunique_maskZunique_indsrA   rA   rB   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:   rc   rA   rA   rB   Ú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¡|ƒ | j|dd�S )NzIncompatible shapes ({} and {})T)r   r   re   Fr    )r2   r.   Úformatr   r   Úcharr'   r(   rS   rg   rh   r   rV   r)   r*   r+   rj   Ú
_container)r:   Úotherr   Úresultrm   r<   r=   rA   rA   rB   Ú
_add_dense3  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'   r   r2   r   r   rŸ   r   rV   r)   r*   r+   )r:   r¡   r¢   rA   rA   rB   Ú_mul_vector?  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'   r   r2   r   r   rŸ   Ú	enumerateÚTr   rV   r)   r*   r+   Úviewr¥   )r:   r¡   r¢   rX   r*   rA   rA   rB   Ú_mul_multivectorF  s    ÿ zcoo_matrix._mul_multivector)NNF)N)NF)NN)F)F)F)F)F)r   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rž   r!   rQ   r   rW   r9   r]   rd   rn   rx   rz   r3   r‚   r‡   r�   r   r–   r—   rs   r‰   r�   r£   r¤   r©   rA   rA   rA   rB   r      s>   f
H'








)
)







%
c                 C   s    ddl m} t| tƒp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   )Ú	coo_array)Ú_arraysr®   r"   r   )rw   r®   rA   rA   rB   r   N  s    ) r­   Ú__docformat__Ú__all__Úwarningsr   Únumpyr'   Ú_sparsetoolsr   r   r   Ú_baser	   r
   r   Ú_datar   r   Ú_sputilsr   r   r   r   r   r   r   r   r   r   r0   r   r   rA   rA   rA   rB   Ú<module>   s   0    <