ó
Ê½÷Xc           @` 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  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_indexc           B` s:  e  Z d  Z d Z d d e d „ Z d d „ Z e j j e _ d „  Z	 d 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 „  Z e j j e _ d „  Z e 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
    --------
    >>> 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)

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

    >>> # example with duplicates
    >>> 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_matrix((data, (row, col)), shape=(4, 4)).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¸ | \ } } | | f |  _ t d t | | ƒ ƒ } t j	 g  d | ƒ|  _
 t j	 g  d | ƒ|  _ t j	 g  t | d t ƒƒ |  _ t |  _ q%y | \ } \ }	 }
 Wn# t t f k
 ró t d ƒ ‚ n X| d  k rkt |	 ƒ d k s$t |
 ƒ d k r3t d ƒ ‚ n  t j |	 ƒ d } t j |
 ƒ d } | | f |  _ n | \ } } | | f |  _ t d t |  j ƒ ƒ } t j	 |	 d | d | ƒ|  _
 t j	 |
 d | d | ƒ|  _ t j	 | d | ƒ|  _ t |  _ n't | ƒ r©t | ƒ ra| ra| j
 j ƒ  |  _
 | j j ƒ  |  _ | j j ƒ  |  _ | j |  _ n< | j ƒ  } | j
 |  _
 | j |  _ | j |  _ | j |  _ t |  _ n| t j t j | ƒ ƒ } | j d	 k rßt d
 ƒ ‚ n | j |  _ | j ƒ  \ |  _
 |  _ | |  j
 |  j f |  _ t |  _ | d  k	 rO|  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   t   shapeR   t   maxt   npt   arrayt   rowt   colR   t   floatt   datat   Truet   has_canonical_formatt	   TypeErrort
   ValueErrort   Nonet   lent   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/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyR   v   sb    !$	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)   R0   t   intR    t   bincountR   R   (   R4   t   axist   nnz(    (    s/   /tmp/pip-build-7oUkmx/scipy/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    R/   R   R%   R>   R)   t   min(   R4   R8   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyR3   Ô   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"   (   R4   t   axesR   R6   R7   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyt	   transposeî   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_argsR;   t   flagst   f_contiguoust   c_contiguousR)   R   R   R>   R"   R#   R%   t   ravel(   R4   t   ordert   outt   Bt   fortranR6   R7   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyt   toarrayú   s    !c      
   C` s?  d d l  m } |  j d k r5 | |  j d |  j ƒS|  j \ } } |  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 ƒ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   cscRQ   R>   R   R   t   sum_duplicatesR   R#   R"   R   R2   R,   R    t   emptyt
   empty_likeR%   R   R   (   R4   R   RQ   R6   R7   R8   R"   R#   t   indptrt   indicesR%   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyt   tocsc  s    
!c      
   C` s?  d d l  m } |  j d k r5 | |  j d |  j ƒS|  j \ } } |  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 ƒ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   csrRY   R>   R   R   RS   R   R"   R#   R   R2   R,   R    RT   RU   R%   R   R   (   R4   R   RY   R6   R7   R8   R"   R#   RV   RW   R%   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyt   tocsr-  s    
!c         C` s   | r |  j  ƒ  S|  Sd  S(   N(   R   (   R4   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyR-   U  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   diaR\   RS   R#   R"   R    t   uniqueR&   R+   R   R   R%   t   sizet   zerosR   R   R   (   R4   R   R\   t   kst   diagst   diag_idxR%   (    (    s/   /tmp/pip-build-7oUkmx/scipy/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   dokRf   RS   R   R   t   updatet   izipR"   R#   R%   (   R4   R   Rf   Rg   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyt   todokt  s
    
(c         C` sš   t  j t |  j ƒ d |  j ƒ} |  j |  j k } |  j rY |  j | } |  j | } n3 |  j	 |  j | |  j | |  j | ƒ \ } } } | | | <| S(   NR   (
   R    Ra   RC   R   R   R"   R#   R'   R%   t   _sum_duplicates(   R4   t   diagt	   diag_maskR"   R%   t   _(    (    s/   /tmp/pip-build-7oUkmx/scipy/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   R0   R+   R"   R   R#   RC   R    t
   logical_ort   arangeRT   t   concatenateR%   R,   R'   (   R4   t   valuest   kR6   R7   R8   t	   full_keept	   max_indext   keept   new_rowt   new_colt   new_data(    (    s/   /tmp/pip-build-7oUkmx/scipy/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   (   R4   R%   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/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'   Rk   R"   R#   R%   R&   (   R4   t   summed(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyRS   Á  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&   R1   t   addt   reduceatR   (   R4   R"   R#   R%   RL   t   unique_maskt   unique_inds(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyRk   Ì  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#   (   R4   t   mask(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyt   eliminate_zerosÝ  s    c         C` sZ   t  j |  j d d t |  j j | j j ƒ ƒ} t |  j |  j |  j	 |  j
 | | ƒ | S(   Ni    R   (   R    Ra   R   R   R   t   charR	   R>   R"   R#   R%   (   R4   t   othert   result(    (    s/   /tmp/pip-build-7oUkmx/scipy/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    Ra   R   R   R   R†   t	   enumeratet   TR	   R>   R"   R#   R%   t   viewRŠ   (   R4   R‡   Rˆ   R@   R#   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyt   _mul_multivectorò  s
     -N(   t   __name__t
   __module__t   __doc__t   formatR*   R,   R   R?   R   R3   RE   RP   RX   R[   R-   Re   Rj   Ro   R   R{   R&   R|   RS   Rk   R…   R‰   RŽ   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyR      s4   ^D	
((			%				c         C` s   t  |  t ƒ S(   N(   R   R   (   t   x(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyR   ú  s    (#   R‘   t
   __future__R    R   R   t   __docformat__t   __all__t   warningsR   t   numpyR    t   scipy._lib.sixR   Ri   t   _sparsetoolsR   R   R	   t   baseR
   R   R   R%   R   R   t   sputilsR   R   R   R   R   R   R   R   R   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/coo.pyt   <module>   s   4ÿ æ