ó
î&]\c           @` s  d  Z  d d l m Z m Z m Z d Z d d g Z d d l m Z d d l	 Z
 d d l m 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" d e e f d „  ƒ  YZ# d „  Z$ d S(   s2    A sparse matrix in COOrdinate or 'triplet' formati    (   t   divisiont   print_functiont   absolute_imports   restructuredtext ent
   coo_matrixt   isspmatrix_coo(   t   warnN(   t   zipi   (   t	   coo_tocsrt   coo_todenset
   coo_matvec(   t
   isspmatrixt   SparseEfficiencyWarningt   spmatrix(   t   _data_matrixt   _minmax_mixin(	   t   upcastt   upcast_chart	   to_nativet   isshapet   getdtypet   get_index_dtypet   downcast_intp_indext   check_shapet   check_reshape_kwargsc           B` sv  e  Z d  Z d Z d d e d „ Z d „  Z e j j e _ d d „ Z	 e j	 j e	 _ d „  Z
 d e d „ Z e j j e _ d „  Z e j j e _ d d d „ Z e d	 „ Z e d
 „ Z e d „ Z e j j e _ e d „ Z e j j e _ e d „ Z e j j e _ d d „ Z e j j e _ d „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sÛ  
    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 nonzero elements
    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]])

    t   cooc         C` s  t  j |  ƒ t | t ƒ rt | ƒ r¾ | \ } } t | | f ƒ |  _ t d t | | ƒ ƒ } t	 j
 g  d | ƒ|  _ t	 j
 g  d | ƒ|  _ t	 j
 g  t | d t ƒƒ |  _ t |  _ qIy | \ } \ }	 }
 Wn# t t f k
 rù t d ƒ ‚ n X| d  k rwt |	 ƒ d k s*t |
 ƒ d k r9t d ƒ ‚ n  t	 j |	 ƒ d } t	 j |
 ƒ d } t | | f ƒ |  _ n! | \ } } t | | f ƒ |  _ t d t |  j ƒ ƒ } t	 j
 |	 d | d | ƒ|  _ t	 j
 |
 d | d | ƒ|  _ t	 j
 | d | ƒ|  _ t |  _ n9t | ƒ rÇt | ƒ ry| ry| j j ƒ  |  _ | j j ƒ  |  _ | j j ƒ  |  _ t | j ƒ |  _ nB | j ƒ  } | j |  _ | j |  _ | j |  _ t | j ƒ |  _ t |  _ n‚ t	 j t	 j | ƒ ƒ } | j d	 k rýt d
 ƒ ‚ n t | j ƒ |  _ | j ƒ  \ |  _ |  _ | |  j |  j f |  _ t |  _ | d  k	 rs|  j j  | d t ƒ|  _ n  |  j! ƒ  d  S(   Nt   maxvalt   dtypet   defaults   invalid input formati    s4   cannot infer dimensions from zero sized index arraysi   t   copyi   s'   expected dimension <= 2 array or matrix("   R   t   __init__t
   isinstancet   tupleR   R   t   _shapeR   t   maxt   npt   arrayt   rowt   colR   t   floatt   datat   Truet   has_canonical_formatt	   TypeErrort
   ValueErrort   Nonet   lent   shapet   FalseR
   R   R   t   tocoot
   atleast_2dt   asarrayt   ndimt   nonzerot   astypet   _check(   t   selft   arg1R.   R   R   t   Mt   Nt	   idx_dtypet   objR$   R%   R   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyR   ~   sb    !$	c         O` sž  t  | |  j ƒ } t | ƒ \ } } | |  j k rJ | rC |  j ƒ  S|  Sn  |  j \ } } | d k rÔ t d | t d | d ƒ t d | d ƒ ƒ } t j | |  j d | ƒ|  j	 }	 t
 |	 | d ƒ \ }
 } n‡ | d k rOt d | t d | d ƒ t d | d ƒ ƒ } t j | |  j	 d | ƒ|  j }	 t
 |	 | d ƒ \ } }
 n t d ƒ ‚ | rs|  j j ƒ  } n	 |  j } t | |
 | f f d | d	 t ƒS(
   Nt   CR   i    i   R   t   Fs   'order' must be 'C' or 'F'R.   R   (   R   R.   R   R   R   R!   R"   t   multiplyR$   R%   t   divmodR+   R'   R   R/   (   R7   t   argst   kwargsR.   t   orderR   t   nrowst   ncolsR   t   flat_indicest   new_rowt   new_colt   new_data(    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   reshapeÂ   s*    
1"1"	c         C` s*  | d  k r£ t |  j ƒ } | t |  j ƒ k sE | t |  j ƒ k rT t d ƒ ‚ n  |  j j d k sŠ |  j j d k sŠ |  j j d k r™ t d ƒ ‚ n  t | ƒ S| d k  r¼ | d 7} n  | d k rë t j	 t
 |  j ƒ d |  j d ƒS| d k rt j	 t
 |  j ƒ d |  j d ƒSt d ƒ ‚ d  S(   Ns7   row, column, and data array must all be the same lengthi   s(   row, column, and data arrays must be 1-Di    i   t	   minlengths   axis out of bounds(   R,   R-   R'   R$   R%   R+   R3   t   intR"   t   bincountR   R.   (   R7   t   axist   nnz(    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   getnnzë   s"    *$
c         C` sr  |  j  j j d k r/ t d |  j  j j ƒ n  |  j j j d k r^ t d |  j j j ƒ n  t d t |  j ƒ ƒ } t	 j
 |  j  d | ƒ|  _  t	 j
 |  j d | ƒ|  _ t |  j ƒ |  _ |  j d k rn|  j  j ƒ  |  j d k rø t d ƒ ‚ n  |  j j ƒ  |  j d k r#t d	 ƒ ‚ n  |  j  j ƒ  d k  rGt d
 ƒ ‚ n  |  j j ƒ  d k  rnt d ƒ ‚ qnn  d S(   s'    Checks data structure for consistency t   is,   row index array has non-integer dtype (%s)  s+   col index array has non-integer dtype (%s) R   R   i    s#   row index exceeds matrix dimensionsi   s&   column index exceeds matrix dimensionss   negative row index founds   negative column index foundN(   R$   R   t   kindR   t   nameR%   R   R!   R.   R"   R2   R   R'   RO   R+   t   min(   R7   R;   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyR6     s&    c         C` s[   | d  k	 r t d ƒ ‚ n  |  j \ } } t |  j |  j |  j f f d | | f d | ƒS(   Nso   Sparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.R.   R   (   R,   R+   R.   R   R'   R%   R$   (   R7   t   axesR   R9   R:   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt	   transpose  s
    c         G` s²   t  | ƒ } | \ } } |  j \ } } | | k  s? | | k  r¥ t j |  j | k  |  j | k  ƒ } | j ƒ  s¥ |  j | |  _ |  j | |  _ |  j | |  _ q¥ n  | |  _ d  S(   N(	   R   R.   R"   t   logical_andR$   R%   t   allR'   R    (   R7   R.   t   new_Mt   new_NR9   R:   t   mask(    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   resize+  s    $c      	   C` sŽ   |  j  | | ƒ } t | j j ƒ } | rG | j j rG t d ƒ ‚ n  |  j \ } } t | | |  j |  j	 |  j
 |  j | j d ƒ | ƒ | S(   s)   See the docstring for `spmatrix.toarray`.s&   Output array must be C or F contiguoust   A(   t   _process_toarray_argsRL   t   flagst   f_contiguoust   c_contiguousR+   R.   R   RO   R$   R%   R'   t   ravel(   R7   RC   t   outt   Bt   fortranR9   R:   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   toarray;  s    !c      
   C` sQ  d d l  m } |  j d k r5 | |  j d |  j ƒS|  j \ } } t |  j |  j f d t |  j | ƒ ƒ} |  j j	 | d t
 ƒ} |  j j	 | d t
 ƒ} t j | d d | ƒ} t j | d | ƒ}	 t j |  j d t |  j ƒ ƒ}
 t | | |  j | | |  j | |	 |
 ƒ	 | |
 |	 | f d |  j ƒ} |  j sI| j ƒ  n  | Sd S(	   sM  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]])

        i   (   t
   csc_matrixi    R   R   R   R.   N(   t   cscRg   RO   R.   R   R   R%   R$   R!   R5   R/   R"   t   emptyt
   empty_likeR'   R   R   R)   t   sum_duplicates(   R7   R   Rg   R9   R:   R;   R$   R%   t   indptrt   indicesR'   t   x(    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   tocscF  s"    !	c      
   C` sQ  d d l  m } |  j d k r5 | |  j d |  j ƒS|  j \ } } t |  j |  j f d t |  j | ƒ ƒ} |  j j	 | d t
 ƒ} |  j j	 | d t
 ƒ} t j | d d | ƒ} t j | d | ƒ}	 t j |  j d t |  j ƒ ƒ}
 t | | |  j | | |  j | |	 |
 ƒ	 | |
 |	 | f d |  j ƒ} |  j sI| j ƒ  n  | Sd S(	   sJ  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]])

        i   (   t
   csr_matrixi    R   R   R   R.   N(   t   csrRp   RO   R.   R   R   R$   R%   R!   R5   R/   R"   Ri   Rj   R'   R   R   R)   Rk   (   R7   R   Rp   R9   R:   R;   R$   R%   Rl   Rm   R'   Rn   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   tocsrp  s"    !	c         C` s   | r |  j  ƒ  S|  Sd  S(   N(   R   (   R7   R   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyR0   š  s    
c         C` sþ   d d l  m } |  j ƒ  |  j |  j } t j | d t ƒ\ } } t | ƒ d k rq t	 d t | ƒ t
 ƒ n  |  j j d k rž t j d	 d |  j ƒ} nG t j t | ƒ |  j j ƒ  d f d |  j ƒ} |  j | | |  j f <| | | f d |  j ƒS(
   Ni   (   t
   dia_matrixt   return_inverseid   s:   Constructing a DIA matrix with %d diagonals is inefficienti    R   R.   (   i    i    (   t   diaRs   Rk   R%   R$   R"   t   uniqueR(   R-   R   R   R'   t   sizet   zerosR   R!   R.   (   R7   R   Rs   t   kst   diagst   diag_idxR'   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   todia¢  s    
1c         C` s^   d d l  m } |  j ƒ  | |  j d |  j ƒ} | j t t |  j |  j ƒ |  j	 ƒ ƒ | S(   Ni   (   t
   dok_matrixR   (
   t   dokR}   Rk   R.   R   t   _updatet   izipR$   R%   R'   (   R7   R   R}   R~   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   todok¹  s
    
(i    c   	      C` sü   |  j  \ } } | | k s( | | k r7 t d ƒ ‚ n  t j t | t | d ƒ | t | d ƒ ƒ d |  j ƒ} |  j | |  j k } |  j	 r® |  j | } |  j
 | } n3 |  j |  j | |  j | |  j
 | ƒ \ } } } | | | t | d ƒ <| S(   Ns   k exceeds matrix dimensionsi    R   (   R.   R+   R"   Rx   RT   R!   R   R$   R%   R)   R'   t   _sum_duplicates(	   R7   t   kt   rowst   colst   diagt	   diag_maskR$   R'   t   _(    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   diagonalÄ  s    /	
c         C` sû  |  j  \ } } | j r) t | ƒ r) d  S|  j j } |  j |  j | k } | d k  rÜ t | | | ƒ } | j r‹ t | t | ƒ ƒ } n  t j | |  j | k ƒ } t j	 | | | d | ƒ}	 t j	 | d | ƒ}
 n€ t | | | ƒ } | j rt | t | ƒ ƒ } n  t j | |  j | k ƒ } t j	 | d | ƒ}	 t j	 | | | d | ƒ}
 | j rr| |  } n t j
 | d |  j ƒ} | | (t j |  j | |	 f ƒ |  _ t j |  j | |
 f ƒ |  _ t j |  j | | f ƒ |  _ t |  _ d  S(   Ni    R   (   R.   R3   R-   R$   R   R%   RT   R"   t
   logical_ort   arangeRi   t   concatenateR'   R/   R)   (   R7   t   valuesRƒ   R9   R:   R;   t	   full_keept	   max_indext   keepRG   RH   RI   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   _setdiagÙ  s4    			c         C` sr   | r@ t  | |  j j ƒ  |  j j ƒ  f f d |  j d | j ƒSt  | |  j |  j f f d |  j d | j ƒSd S(   sª   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.
        R.   R   N(   R   R$   R   R%   R.   R   (   R7   R'   R   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt
   _with_dataþ  s
    'c         C` sP   |  j  r d S|  j |  j |  j |  j ƒ } | \ |  _ |  _ |  _ t |  _  d S(   sl   Eliminate duplicate matrix entries by adding them together

        This is an *in place* operation
        N(   R)   R‚   R$   R%   R'   R(   (   R7   t   summed(    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyRk   
  s
    	c         C` sÛ   t  | ƒ d k r | | | f St j | | f ƒ } | | } | | } | | } | d | d  k | d | d  k B} t j t | ƒ } | | } | | } t j | ƒ \ } t j j | | d |  j ƒ} | | | f S(   Ni    i   iÿÿÿÿR   (	   R-   R"   t   lexsortt   appendR(   R4   t   addt   reduceatR   (   R7   R$   R%   R'   RC   t   unique_maskt   unique_inds(    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyR‚     s    




c         C` sC   |  j  d k } |  j  | |  _  |  j | |  _ |  j | |  _ d S(   sU   Remove zero entries from the matrix

        This is an *in place* operation
        i    N(   R'   R$   R%   (   R7   R[   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   eliminate_zeros&  s    c      	   C` s¿   | j  |  j  k r! t d ƒ ‚ n  t |  j j | j j ƒ } t j | d | d t ƒ} t | j	 j
 ƒ } |  j  \ } } t | | |  j |  j |  j |  j | j d ƒ | ƒ t j | d t ƒS(   Ns   Incompatible shapes.R   R   R]   (   R.   R+   R   R   t   charR"   R#   R(   RL   R_   R`   R   RO   R$   R%   R'   Rb   t   matrixR/   (   R7   t   otherR   t   resultRe   R9   R:   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt
   _add_dense4  s    !c         C` sZ   t  j |  j d d t |  j j | j j ƒ ƒ} t |  j |  j |  j	 |  j
 | | ƒ | S(   Ni    R   (   R"   Rx   R.   R   R   R›   R	   RO   R$   R%   R'   (   R7   R   Rž   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   _mul_vector?  s    %c      	   C` s    t  j | j d |  j d f d t |  j j | j j ƒ ƒ} xF t | j ƒ D]5 \ } } t |  j	 |  j
 |  j |  j | | | ƒ qN W| j j d t | ƒ ƒ S(   Ni   i    R   t   type(   R"   Rx   R.   R   R   R›   t	   enumeratet   TR	   RO   R$   R%   R'   t   viewR¡   (   R7   R   Rž   RQ   R%   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   _mul_multivectorF  s
     -N(   t   __name__t
   __module__t   __doc__t   formatR,   R/   R   RJ   R   RP   R6   RV   R\   Rf   Ro   Rr   R0   R|   R   R‰   R   R‘   R(   R’   Rk   R‚   Rš   RŸ   R    R¥   (    (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyR      s>   eD	'	
	**		%					c         C` s   t  |  t ƒ S(   sÍ  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   (   Rn   (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyR   N  s    (%   R¨   t
   __future__R    R   R   t   __docformat__t   __all__t   warningsR   t   numpyR"   t   scipy._lib.sixR   R€   t   _sparsetoolsR   R   R	   t   baseR
   R   R   R'   R   R   t   sputilsR   R   R   R   R   R   R   R   R   R   R   (    (    (    s/   lib/python2.7/site-packages/scipy/sparse/coo.pyt   <module>   s   @ÿ ÿ :