a
    CCCf|                     @   s   d Z dZg dZddlZddlmZ ddlZddlm	Z	 dd	l
mZ dd
lmZmZ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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dZ$dd Z%G dd de#eZ&G dd dee#Z'dS )z2 A sparse matrix in COOrdinate or 'triplet' formatzrestructuredtext en)	coo_array
coo_matrixisspmatrix_coo    N)warn   )copy_if_needed   )spmatrix)	coo_tocsrcoo_todense
coo_matvec)issparseSparseEfficiencyWarning_spbasesparray)_data_matrix_minmax_mixin)	upcast_char	to_nativeisshapegetdtypegetdatadowncast_intp_indexget_index_dtypecheck_shapecheck_reshape_kwargsc                   @   sp  e Zd ZdZd9ddZedd Zejdd Zed	d
 Zejdd
 Zdd Z	e
j	je	_d:ddZe
jje_dd Zd;ddZe
jje_ddddZe
jje_d<ddZe
jje_d=ddZd>ddZdd Zd?dd Ze
jje_d@d!d"Ze
jje_dAd#d$Ze
jje_dBd&d'Zejje_d(d) ZdCd+d,Zddd-d.Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Z dS )D	_coo_basecooNFc              
      s  t |  t| t} st t|trht||drt||d| _| j	t
| jdt|td}tfddtt| jD | _tjg |d| _d| _nz|\}}W n2 ttfy }	 ztd|	W Y d }	~	n
d }	~	0 0 |d u rtd	d |D rtd
tdd |D }t||d| _| j	|t
| jddt fdd|D | _t| |d| _d| _nFt|r|j| jkr rtdd |jD | _|j | _t|j|d| _|j| _n2| }
t|
j| _|
j| _t|
j|d| _d| _nt|}|s$t|}|jdkr$tdt|j|d| _|d urlt||d| jkrld| d| j }t|| j	t
| jd|  }tfdd|D | _|| | _d| _|d ur| jj!|dd| _| "  d S )NZallow_1dmaxval)defaultc                 3   s   | ]}t jg  d V  qdS dtypeNnparray).0_	idx_dtype M/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/sparse/_coo.py	<genexpr>&   s   z%_coo_base.__init__.<locals>.<genexpr>r#   Tzinvalid input formatc                 s   s   | ]}t |d kV  qdS )r   Nlenr(   idxr,   r,   r-   r.   1       z4cannot infer dimensions from zero sized index arraysc                 s   s"   | ]}t t|d  V  qdS r   N)operatorindexr&   maxr1   r,   r,   r-   r.   4   s   )r    Zcheck_contentsc                 3   s   | ]}t j| d V  qdS )copyr$   Nr%   r1   )r9   r+   r,   r-   r.   ;   s   r8   Fc                 s   s   | ]}|  V  qd S Nr9   r1   r,   r,   r-   r.   B   r3   r   z'expected dimension <= 2 array or matrixzinconsistent shapes: z != c                 3   s   | ]}|j  d dV  qdS )Fr;   N)astyper1   )index_dtyper,   r-   r.   [   s   r;   )#r   __init__
isinstancer   r   tupler   r   _shape_get_index_dtyper7   r   floatranger0   coordsr&   r'   datahas_canonical_format	TypeError
ValueErroranyshaper   r   formatr9   tocooasarrayZ
atleast_2dndimnonzeror<   _check)selfZarg1rK   r$   r9   is_arrayZ
data_dtypeobjrE   er   Mmessager,   )r9   r+   r=   r-   r>      s    


 










z_coo_base.__init__c                 C   s0   | j dkr| jd S t| j}|jdd |S )Nr   F)write)rO   rE   r&   
zeros_likecolZsetflags)rR   resultr,   r,   r-   rowe   s
    

z_coo_base.rowc                 C   sP   | j dk rtdtj|| jd jd}| jd d |f | jdd   | _d S )Nr   z8cannot set row attribute of a 1-dimensional sparse arrayrX   r#   )rO   rI   r&   rN   rE   r$   )rR   new_rowr,   r,   r-   r]   n   s    
c                 C   s
   | j d S )Nr^   rE   rR   r,   r,   r-   r[   u   s    z_coo_base.colc                 C   s0   t j|| jd jd}| jd d |f | _d S )Nr^   r#   )r&   rN   rE   r$   )rR   new_colr,   r,   r-   r[   y   s    c           
      O   s   t | t}t|| j|d}t|\}}|| jkr@|r<|  S | S t| j| j|d}t|dkr|dkrvt	||d }qt	||d d d d }nt
j|||d}|r| j }	n| j}	| j|	|f|dd	S )
Nr   orderr   Cr   r   r^   FrK   r9   )r?   r   r   rK   r   r9   _ravel_coordsrE   r0   divmodr&   unravel_indexrF   	__class__)
rR   argskwargsrS   rK   rd   r9   flat_coordsZ
new_coordsnew_datar,   r,   r-   reshape~   s"    

z_coo_base.reshapec                    s   |d u s|dkrt| j dkrtt| j t fdd| jD rDtd| jj dksdtdd | jD rltdt S |dk r|| j 7 }|| j krtd| j d	krtd
tj	t
| jd|  | jd|  dS )Nr   r   c                 3   s   | ]}t | kV  qd S r:   r/   r1   nnzr,   r-   r.      r3   z$_coo_base._getnnz.<locals>.<genexpr>z3all index and data arrays must have the same lengthc                 s   s   | ]}|j d kV  qdS r4   )rO   r1   r,   r,   r-   r.      r3   z(row, column, and data arrays must be 1-Dzaxis out of boundsr   z?per-axis nnz for COO arrays with >2 dimensions is not supported)Z	minlength)rO   r0   rF   rJ   rE   rI   intNotImplementedErrorr&   Zbincountr   rK   )rR   Zaxisr,   rp   r-   _getnnz   s     
 


z_coo_base._getnnzc              	      s$  | j t| jkr,tdt| j d| j  t| jD ]2\}}|jjdkr6td| d|jj ddd q6| j	| jt
| jd	 t fd
d| jD | _t| j| _| jdkr t| jD ]b\}}|
 | j| krtd| d|
  d| j|  | dk rtd| d|  qdS )z' Checks data structure for consistency z2mismatching number of index arrays for shape; got z, expected izindex array z has non-integer dtype ()   
stacklevelr   c                 3   s   | ]}t j| d V  qdS r"   )r&   rN   r1   r*   r,   r-   r.      s   z#_coo_base._check.<locals>.<genexpr>r   zaxis z index z exceeds matrix dimension znegative axis z index: N)rO   r0   rE   rI   	enumerater$   kindr   namerB   r7   rK   r@   r   rF   rq   min)rR   ru   r2   r,   r*   r-   rQ      s0    
z_coo_base._checkc                    s   |d u rt  jd d d }nLt trZt| jkr>tdtt| jkrjtdn|dkrjtdt fdd|D }t fdd|D } j j	|f||d	S )
Nr^   z"axes don't match matrix dimensionszrepeated axis in transpose)r   r   zoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.c                 3   s   | ]} j | V  qd S r:   )rA   r(   ru   ra   r,   r-   r.      r3   z&_coo_base.transpose.<locals>.<genexpr>c                 3   s   | ]} j | V  qd S r:   r`   r~   ra   r,   r-   r.      r3   rf   )
rD   rO   r?   r   r0   rI   setr@   rj   rF   )rR   Zaxesr9   Zpermuted_shapeZpermuted_coordsr,   ra   r-   	transpose   s    

z_coo_base.transpose)returnc                    sP  t | t}t||d}t|| jkrlt| j| j}t	|}t
|d | || _| jd | | _|| _d S t|| jk r| jd t|d  d d| jt|   }| |}|jd t| | _|jd t| | _tdd t| j|D }|rFt
jdd t| j|D    sFt fd	d| jD | _| j  | _|| _d S )
Nr   r   )r^   )r   c                 s   s   | ]\}}||kV  qd S r:   r,   )r(   oldnewr,   r,   r-   r.      r3   z#_coo_base.resize.<locals>.<genexpr>c                 S   s   g | ]\}}||k qS r,   r,   )r(   r2   sizer,   r,   r-   
<listcomp>  s   z$_coo_base.resize.<locals>.<listcomp>c                 3   s   | ]}|  V  qd S r:   r,   r1   maskr,   r-   r.     r3   )r?   r   r   r0   rO   rg   rE   rK   mathprodr&   ri   rF   rA   ro   rJ   ziplogical_andreduceallr@   )rR   rK   rS   rm   max_sizeZ	tmp_shapetmpZis_truncatingr,   r   r-   resize   s8    




z_coo_base.resizec              
   C   sx   |  ||}t|jj}|s,|jjs,td| jdkr>td| j\}}t||| j	| j
| j| j|d| || jS )Nz&Output array must be C or F contiguousr   z'Cannot densify higher-rank sparse arrayA)Z_process_toarray_argsrr   flagsf_contiguousc_contiguousrI   rO   _shape_as_2dr   rq   r]   r[   rF   ravelro   rK   )rR   rd   outBfortranrV   Nr,   r,   r-   toarray  s    


z_coo_base.toarrayc                 C   sx   | j dkrtd| jdkr.| j| j| jdS ddlm} | |j	\}}}}| j|||f|d}| j
sp|  |S dS )	aQ  Convert this array/matrix to Compressed Sparse Column format

        Duplicate entries will be summed together.

        Examples
        --------
        >>> from numpy import array
        >>> from scipy.sparse import coo_array
        >>> 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_array((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   z.Cannot convert a 1d sparse array to csc formatr   r#   r   )	csc_arrayrK   N)rO   rI   rq   Z_csc_containerrK   r$   Z_cscr   _coo_to_compressed_swaprG   sum_duplicates)rR   r9   r   indptrindicesrF   rK   xr,   r,   r-   tocsc  s    

z_coo_base.tocscc                 C   sz   | j dkrtd| jdkr.| j| j| jdS ddlm} | |j	\}}}}| j|||f| jd}| j
sr|  |S dS )	aN  Convert this array/matrix to Compressed Sparse Row format

        Duplicate entries will be summed together.

        Examples
        --------
        >>> from numpy import array
        >>> from scipy.sparse import coo_array
        >>> 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_array((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   z.Cannot convert a 1d sparse array to csr formatr   r#   r   )	csr_arrayr   N)rO   rI   rq   Z_csr_containerrK   r$   Z_csrr   r   r   rG   r   )rR   r9   r   r   r   rF   rK   r   r,   r,   r-   tocsr>  s    

z_coo_base.tocsrc              
   C   s   || j \}}|| j\}}t|}| j| jt| j|d}|j|dd}|j|dd}tj|d |d}tj	||d}	tj	| j
| jd}
t|||||| j
||	|
	 ||	|
| j fS )z?convert (shape, coords, data) to (indptr, indices, data, shape)r   Fr;   r   r#   )rK   rE   r0   rB   r7   rq   r<   r&   emptyZ
empty_likerF   r$   r
   )rR   ZswaprV   r   majorminorrq   r+   r   r   rF   r,   r,   r-   r   _  s    z_coo_base._coo_to_compressedc                 C   s   |r|   S | S d S r:   r;   )rR   r9   r,   r,   r-   rM   q  s    z_coo_base.tocooc                 C   s   | j dkrtd|   | j| j }tj|dd\}}t|dkrZtdt| t	dd | j
jdkrxtjd	| jd
}n2tjt|| j d f| jd
}| j
||| jf< | j||f| jdS )Nr   z.Cannot convert a 1d sparse array to dia formatT)Zreturn_inversed   z:Constructing a DIA matrix with %d diagonals is inefficientrx   r   )r   r   r#   r   r   )rO   rI   r   r[   r]   r&   uniquer0   r   r   rF   r   zerosr$   r7   Z_dia_containerrK   )rR   r9   ksZdiagsZdiag_idxrF   r,   r,   r-   todiay  s     
"z_coo_base.todiac                 C   sP   |    | j| j| jd}| jdkr0| jd }n
t| j }tt|| j|_	|S )Nr#   r   r   )
r   Z_dok_containerrK   r$   rO   rE   r   dictrF   _dict)rR   r9   ZdokrE   r,   r,   r-   todok  s    

z_coo_base.todokr   c           	         s   | j dkrtd| 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  }n2t fdd| jD }| || j  \\}}}|||t|d < |S )Nr   z diagonal requires two dimensionsr   r#   c                 3   s   | ]}|  V  qd S r:   r,   r1   Z	diag_maskr,   r-   r.     r3   z%_coo_base.diagonal.<locals>.<genexpr>)rO   rI   rK   r&   r   rF   r$   r   r}   r7   r]   r[   rG   r@   rE   _sum_duplicates)	rR   krowscolsZdiagr]   rF   Zindsr)   r,   r   r-   diagonal  s     

 
z_coo_base.diagonalc                 C   s|  | j dkrtd| j\}}|j r.t|s.d S | jj}| j| j |k}|dk rt|| |}|j rpt|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f| _t| j| |f| _d| _d S )Nr   z*setting a diagonal requires two dimensionsr   r#   F)rO   rI   rK   r0   r]   r$   r[   r}   r&   
logical_orZaranger   ZconcatenaterE   rF   rG   )rR   valuesr   rV   r   r+   Z	full_keepZ	max_indexZkeepr_   rb   rn   r,   r,   r-   _setdiag  s:    

z_coo_base._setdiagTc                 C   s8   |rt dd | jD }n| j}| j||f| j|jdS )zReturns a matrix with the same sparsity structure as self,
        but with different data. By default the index arrays are copied.
        c                 s   s   | ]}|  V  qd S r:   r;   r1   r,   r,   r-   r.     r3   z'_coo_base._with_data.<locals>.<genexpr>)rK   r$   )r@   rE   rj   rK   r$   )rR   rF   r9   rE   r,   r,   r-   
_with_data  s    z_coo_base._with_datac                 C   s0   | j r
dS | | j| j}|\| _| _d| _ dS )zeEliminate duplicate entries by adding them together

        This is an *in place* operation
        NT)rG   r   rE   rF   )rR   Zsummedr,   r,   r-   r     s
    z_coo_base.sum_duplicatesc                    s   t |dkr||fS t|d d d  t fdd|D }|  }tjdd |D tdtfdd|D }t\}tjj	||| j
d	}||fS )
Nr   r^   c                 3   s   | ]}|  V  qd S r:   r,   r1   rc   r,   r-   r.     r3   z,_coo_base._sum_duplicates.<locals>.<genexpr>c                 S   s$   g | ]}|d d |dd kqS )r   Nr^   r,   r1   r,   r,   r-   r     s   z-_coo_base._sum_duplicates.<locals>.<listcomp>Tc                 3   s   | ]}|  V  qd S r:   r,   r1   )unique_maskr,   r-   r.     r3   r#   )r0   r&   Zlexsortr@   r   r   appendrP   addZreduceatr$   )rR   rE   rF   Zunique_indsr,   )rd   r   r-   r     s    z_coo_base._sum_duplicatesc                    s4   | j dk | j   | _ t fdd| jD | _dS )z[Remove zero entries from the array/matrix

        This is an *in place* operation
        r   c                 3   s   | ]}|  V  qd S r:   r,   r1   r   r,   r-   r.     r3   z,_coo_base.eliminate_zeros.<locals>.<genexpr>N)rF   r@   rE   ra   r,   r   r-   eliminate_zeros  s    
z_coo_base.eliminate_zerosc              
   C   s   |j | j kr&td| j  d|j  d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 (z and rv   T)r$   r9   r   Fr;   )rK   rI   r   r$   charr&   r'   rr   r   r   r   r   rq   r]   r[   rF   r   
_container)rR   otherr$   r\   r   rV   r   r,   r,   r-   
_add_dense  s    

z_coo_base._add_densec                 C   s   | j dkr| jd nd}tj|t| jj|jjd}| j dkrL| j}| j}n0| j dkrl| j	d }t
|}ntd| j  t| j||| j|| t| tr|dkr|d S |S )Nr   r   r#   r   $coo_matvec not implemented for ndim=)rO   rK   r&   r   r   r$   r   r[   r]   rE   rZ   rs   r   rq   rF   r?   r   )rR   r   result_shaper\   r[   r]   r,   r,   r-   _matmul_vector  s"    



z_coo_base._matmul_vectorc           	      C   s   t | jj|jj}| jdkr>|jd | jd f}| j}| j}n<| jdkrj|jd f}| jd }t	|}nt
d| j tj||d}t|jD ]*\}}t| j||| j||||d   q|jjt|dS )Nr   r   r   r   r#   )type)r   r$   r   rO   rK   r[   r]   rE   r&   rZ   rs   r   rz   Tr   rq   rF   viewr   )	rR   r   Zresult_dtyper   r[   r]   r\   ru   Z	other_colr,   r,   r-   _matmul_multivector1  s     



$z_coo_base._matmul_multivector)NNF)N)NF)NN)F)F)F)F)F)r   )T)!__name__
__module____qualname___formatr>   propertyr]   setterr[   ro   r   __doc__rt   rQ   r   r   r   r   r   r   rM   r   r   r   r   r   r   r   r   r   r   r   r   r,   r,   r,   r-   r      sP   
J



!




$



!
!







'

r   re   c           	      C   s   t | dkr| d S t | dkr|\}}| \}}|dkrv|td|d  td|d  }t|d}tj|||d| S |dkr|td|d  td|d  }t|d}tj|||d| S tdtj| ||d	S )
z;Like np.ravel_multi_index, but avoids some overflow issues.r   r   r   re   r   r#   Fz'order' must be 'C' or 'F'rc   )r0   r7   r   r&   multiplyrI   Zravel_multi_index)	rE   rK   rd   ZnrowsZncolsr]   r[   r    r+   r,   r,   r-   rg   E  s     
 
rg   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_array, coo_matrix, csr_matrix, isspmatrix_coo
    >>> isspmatrix_coo(coo_matrix([[5]]))
    True
    >>> isspmatrix_coo(coo_array([[5]]))
    False
    >>> isspmatrix_coo(csr_matrix([[5]]))
    False
    )r?   r   )r   r,   r,   r-   r   Z  s    r   c                   @   s   e Zd ZdZdS )r   a  
    A sparse array in COOrdinate format.

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

    This can be instantiated in several ways:
        coo_array(D)
            where D is an ndarray

        coo_array(S)
            with another sparse array or matrix S (equivalent to S.tocoo())

        coo_array(shape, [dtype])
            to construct an empty sparse array with shape `shape`
            dtype is optional, defaulting to dtype='d'.

        coo_array((data, coords), [shape])
            to construct from existing data and index arrays:
                1. data[:]       the entries of the sparse array, in any order
                2. coords[i][:]  the axis-i coordinates of the data entries

            Where ``A[coords] = data``, and coords is a tuple of index arrays.
            When shape is not specified, it is inferred from the index arrays.

    Attributes
    ----------
    dtype : dtype
        Data type of the sparse array
    shape : tuple of integers
        Shape of the sparse array
    ndim : int
        Number of dimensions of the sparse array
    nnz
    size
    data
        COO format data array of the sparse array
    coords
        COO format tuple of index arrays
    has_canonical_format : bool
        Whether the matrix has sorted coordinates and no duplicates
    format
    T

    Notes
    -----

    Sparse arrays 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 arrays
        - Once a COO array 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)

    Canonical format
        - Entries and coordinates sorted by row, then column.
        - There are no duplicate entries (i.e. duplicate (i,j) locations)
        - Data arrays MAY have explicit zeros.

    Examples
    --------

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

    >>> # Constructing a sparse array 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_array((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 sparse array with duplicate coordinates
    >>> 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_array((data, (row, col)), shape=(4, 4))
    >>> # Duplicate coordinates 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]])

    N)r   r   r   r   r,   r,   r,   r-   r   u  s   r   c                   @   s   e Zd ZdZdd 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)
            where D is a 2-D ndarray

        coo_matrix(S)
            with another sparse array or 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
    size
    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
    has_canonical_format : bool
        Whether the matrix has sorted indices and no duplicates
    format
    T

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

    Canonical format
        - Entries and coordinates sorted by row, then column.
        - There are no duplicate entries (i.e. duplicate (i,j) locations)
        - Data arrays MAY have explicit zeros.

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

    c                 C   s0   d|vr | d| df|d< | j| d S )NrE   r]   r[   )pop__dict__update)rR   stater,   r,   r-   __setstate__U  s    zcoo_matrix.__setstate__N)r   r   r   r   r   r,   r,   r,   r-   r     s   pr   )re   )(r   __docformat____all__r   warningsr   numpyr&   Z
_lib._utilr   Z_matrixr	   Z_sparsetoolsr
   r   r   _baser   r   r   r   _datar   r   Z_sputilsr   r   r   r   r   r   r   r   r   r5   r   rg   r   r   r   r,   r,   r,   r-   <module>   s*   ,    1
o