ó
Ê½÷Xc           @` sì   d  Z  d d l m Z m Z m Z d Z d d g Z d d l Z d d l	 m
 Z
 d d	 l m Z d d
 l m Z m Z m Z m Z m Z d d l m Z m Z m Z m Z m Z m Z d d l m Z d e e f d „  ƒ  YZ d „  Z d S(   s#   Compressed Sparse Row matrix formati    (   t   divisiont   print_functiont   absolute_imports   restructuredtext ent
   csr_matrixt   isspmatrix_csrN(   t   xrangei   (   t   spmatrix(   t	   csr_tocsct	   csr_tobsrt   csr_count_blockst   get_csr_submatrixt   csr_sample_values(   t   upcastt	   isintliket
   IndexMixint
   issequencet   get_index_dtypet   ismatrix(   t
   _cs_matrixc           B` s×   e  Z d  Z d Z d e d „ Z e j j e _ e d „ Z e j j e _ e d „ Z	 e j	 j e	 _ e d „ Z
 e j
 j e
 _ d e d „ Z e j j e _ d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   sF  
    Compressed Sparse Row matrix

    This can be instantiated in several ways:
        csr_matrix(D)
            with a dense matrix or rank-2 ndarray D

        csr_matrix(S)
            with another sparse matrix S (equivalent to S.tocsr())

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

        csr_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
            where ``data``, ``row_ind`` and ``col_ind`` satisfy the
            relationship ``a[row_ind[k], col_ind[k]] = data[k]``.

        csr_matrix((data, indices, indptr), [shape=(M, N)])
            is the standard CSR representation where the column indices for
            row i are stored in ``indices[indptr[i]:indptr[i+1]]`` and their
            corresponding values are stored in ``data[indptr[i]:indptr[i+1]]``.
            If the shape parameter is not supplied, the matrix dimensions
            are 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
        CSR format data array of the matrix
    indices
        CSR format index array of the matrix
    indptr
        CSR format index pointer array of the matrix
    has_sorted_indices
        Whether indices are sorted

    Notes
    -----

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

    Advantages of the CSR format
      - efficient arithmetic operations CSR + CSR, CSR * CSR, etc.
      - efficient row slicing
      - fast matrix vector products

    Disadvantages of the CSR format
      - slow column slicing operations (consider CSC)
      - changes to the sparsity structure are expensive (consider LIL or DOK)

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import csr_matrix
    >>> csr_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, 0, 1, 2, 2, 2])
    >>> col = np.array([0, 2, 2, 0, 1, 2])
    >>> data = np.array([1, 2, 3, 4, 5, 6])
    >>> csr_matrix((data, (row, col)), shape=(3, 3)).toarray()
    array([[1, 0, 2],
           [0, 0, 3],
           [4, 5, 6]])

    >>> indptr = np.array([0, 2, 3, 6])
    >>> indices = np.array([0, 2, 2, 0, 1, 2])
    >>> data = np.array([1, 2, 3, 4, 5, 6])
    >>> csr_matrix((data, indices, indptr), shape=(3, 3)).toarray()
    array([[1, 0, 2],
           [0, 0, 3],
           [4, 5, 6]])

    As an example of how to construct a CSR matrix incrementally,
    the following snippet builds a term-document matrix from texts:

    >>> docs = [["hello", "world", "hello"], ["goodbye", "cruel", "world"]]
    >>> indptr = [0]
    >>> indices = []
    >>> data = []
    >>> vocabulary = {}
    >>> for d in docs:
    ...     for term in d:
    ...         index = vocabulary.setdefault(term, len(vocabulary))
    ...         indices.append(index)
    ...         data.append(1)
    ...     indptr.append(len(indices))
    ...
    >>> csr_matrix((data, indices, indptr), dtype=int).toarray()
    array([[2, 1, 0, 0],
           [0, 1, 1, 1]])

    t   csrc         C` sh   | d  k	 r t d ƒ ‚ n  |  j \ } } d d l m } | |  j |  j |  j f d | | f d | ƒS(   Nso   Sparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.i   (   t
   csc_matrixt   shapet   copy(   t   Nonet
   ValueErrorR   t   cscR   t   datat   indicest   indptr(   t   selft   axesR   t   Mt   NR   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt	   transposeƒ   s    c         C` sÊ   d d l  m } | |  j d |  j ƒ} |  j ƒ  |  j |  j |  j } } } | j | j } } xa t	 |  j d ƒ D]L }	 | |	 }
 | |	 d } | |
 | !j
 ƒ  | |	 <| |
 | !j
 ƒ  | |	 <qv W| S(   Ni   (   t
   lil_matrixt   dtypei    (   t   lilR"   R   R#   t   sum_duplicatesR   R   R   t   rowsR   t   tolist(   R   R   R"   R$   t   ptrt   indt   datR&   R   t   nt   startt   end(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   tolil‘   s    

c         C` s   | r |  j  ƒ  S|  Sd  S(   N(   R   (   R   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   tocsr£   s    
c      	   C` s  t  |  j |  j f d t |  j |  j d ƒ ƒ} t j |  j d d d | ƒ} t j |  j d | ƒ} t j |  j d t |  j	 ƒ ƒ} t
 |  j d |  j d |  j j | ƒ |  j j | ƒ |  j | | | ƒ d d l m } | | | | f d |  j ƒ} t | _ | S(   Nt   maxvali    i   R#   (   R   R   (   R   R   R   t   maxt   nnzR   t   npt   emptyR   R#   R   t   astypeR   R   R   t   Truet   has_sorted_indices(   R   R   t	   idx_dtypeR   R   R   R   t   A(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   tocsc«   s      !	c         C` sù  d d l  m } | d  k rB d d l m } |  j d | |  ƒ ƒ S| d k rŽ |  j j d d d ƒ |  j |  j	 f } | | d |  j
 d | ƒS| \ } } |  j
 \ } }	 | d k  sá | d k  sá | | d k sá |	 | d k rô t d	 | ƒ ‚ n  t | |	 | | |  j	 |  j ƒ }
 t |  j	 |  j f d
 t |	 | |
 ƒ ƒ} t j | | d d | ƒ} t j |
 d | ƒ} t j |
 | | f d |  j ƒ} t | |	 | | |  j	 j | ƒ |  j j | ƒ |  j | | | j ƒ  ƒ
 | | | | f d |  j
 ƒSd  S(   Ni   (   t
   bsr_matrix(   t   estimate_blocksizet	   blocksizeiÿÿÿÿR   R   i    s   invalid blocksize %sR0   R#   (   i   i   (   t   bsrR;   R   t   spfuncsR<   t   tobsrR   t   reshapeR   R   R   R   R	   R   R1   R3   R4   t   zerosR#   R   R5   t   ravel(   R   R=   R   R;   R<   t   arg1t   Rt   CR   R    t   blksR8   R   R   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyR@   Á   s.    '8!!c         C` s   | d | d f S(   sB   swap the members of x if this is a column-oriented matrix
        i    i   (    (   R   t   x(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   _swapç   s    c         ` s  d „  ‰  d „  ‰ ‡  ‡ ‡ f d †  } ˆ j  | ƒ \ } } t | ƒ rÃ t | ƒ rd ˆ j | | ƒ St | t ƒ rƒ ˆ j | | ƒ St | ƒ rª| | ˆ j d ƒ j } ˆ | d  d  … f | Snçt | t ƒ r‘t | ƒ rí | j	 d	 k st | t ƒ r*| j	 d
 k r*| j	 d k r*ˆ j | | ƒ St | ƒ rª| | ˆ j d ƒ j } ˆ } | t d  d  d  ƒ k r†| | d  d  … f } n  | | Snt | ƒ rt | ƒ s¸t | t ƒ rª| | ˆ j d ƒ } | ˆ } | t d  d  d  ƒ k rô| S| d  d  … | f Sqªnœ t | ƒ rªt | ƒ rªt | d ƒ d k rªt | d d ƒ rªˆ  | ƒ } | | d  d  … d f ˆ j d ƒ } | | ˆ j d ƒ j }	 | ˆ |	 Sn  t | ƒ o¿t | ƒ sÝˆ j | | ƒ \ } } n  ˆ  | ƒ } ˆ  | ƒ } | j | j k rt d ƒ ‚ n  | j d k s+t ‚ t j | ƒ }
 |
 d k ret t j | ƒ j d ˆ j ƒSˆ | ˆ j d ƒ ˆ | ˆ j d ƒ t j |
 d ˆ j ƒ} t ˆ j d ˆ j d ˆ j ˆ j ˆ j |
 | j ƒ  | j ƒ  | ƒ	 | j d k rt j | ƒ Sˆ j | j | j ƒ ƒ S(   Nc         S` sg   yI t  j |  ƒ }  t |  f d t ƒ} | |  j k rH |  j | ƒ }  n  Wn t d ƒ ‚ n X|  Sd  S(   Nt   check_contentss   invalid index(   R3   t   asarrayR   R6   R#   R5   t
   IndexError(   RH   R8   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt	   asindicesí   s    c         S` sx   |  j  d k r d S|  j ƒ  } | | k r> t d | ƒ ‚ n  |  j ƒ  } | | k  rn t d | | ƒ ‚ n  | | f S(   Ni    s   index (%d) out of range(   i    i    (   t   sizeR1   RL   t   min(   R   R    t   max_indxt   min_indx(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   check_boundsú   s    c         ` s½   ˆ  |  ƒ }  ˆ |  | ƒ \ } } | d k  rR |  j  ƒ  }  |  |  d k  c | 7<n  t j t |  ƒ d d |  j ƒ} t j t |  ƒ d ˆ j ƒ} t |  ƒ | f } t | |  | f d | ƒS(   so   Return a sparse matrix P so that P*self implements
            slicing of the form self[[1,2,3],:]
            i    i   R#   R   (   R   R3   t   aranget   lenR#   t   onesR   (   R   R    RQ   RP   R   R   R   (   RM   RR   R   (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt	   extractor  s    "i   i    s'   number of row and column indices differi   R#   (   i   N(   i   N(   i   N(    t   _unpack_indexR   t   _get_single_elementt
   isinstancet   slicet   _get_row_sliceR   R   t   Tt   stepR   t   _get_submatrixR   RT   t   _index_to_arraysRL   t   ndimt   AssertionErrorR3   RN   R   t
   atleast_2dR#   R4   R   R   R   R   RC   t   asmatrixt	   __class__RA   (   R   t   keyRV   t   rowt   colt   Pt   slicedt	   extractedt   P_rowt   P_colt   num_samplest   val(    (   RM   RR   R   s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   __getitem__ì   sp    		
*&c         C` s   |  j  | t d ƒ ƒ S(   s]   Returns a copy of row i of the matrix, as a (1 x n)
        CSR matrix (row vector).
        N(   R^   RZ   R   (   R   t   i(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   getrowd  s    c         C` s   |  j  t d ƒ | ƒ S(   sc   Returns a copy of column i of the matrix, as a (m x 1)
        CSR matrix (column vector).
        N(   R^   RZ   R   (   R   Rp   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   getcolj  s    c         C` s  | d k  r  | |  j  d 7} n  | d k  s? | |  j  d k rR t d | ƒ ‚ n  | j |  j  d ƒ \ } } } | d k r’ |  j | | ƒ } nq|  j |  j | |  j | d !} |  j |  j | |  j | d !} | d k rû | | k | | k  @}	 n% | d k  r | | k | | k @}	 n  t | ƒ d k rM|	 | | | d k @}	 n  | |	 | | } | |	 } t j d t	 | ƒ g ƒ }
 | d k  r¿| d d d … } t | d d d … ƒ } n  d t
 t j t | | ƒ | ƒ ƒ f } t | | |
 f d | ƒ} | S(   s.   Returns a copy of row self[i, cslice]
        i    s   index (%d) out of rangei   NiÿÿÿÿR   (   R   RL   R   R^   R   R   t   absR3   t   arrayRT   t   intt   ceilt   floatR   (   R   Rp   t   csliceR,   t   stopt   stridet	   row_slicet   row_indicest   row_dataR)   t
   row_indptrR   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyR[   p  s2    ""
)	c         C` sé   |  j  \ } } d „  } d „  } | | | ƒ \ } } | | | ƒ \ }	 }
 | | | | ƒ | |	 |
 | ƒ t | | |  j |  j |  j t | ƒ t | ƒ t |	 ƒ t |
 ƒ ƒ	 \ } } } | | |
 |	 f } |  j | | | f d | ƒS(   s:   Return a submatrix of this matrix (new matrix is created).c         S` sé   t  |  t ƒ r¦ |  j d k r- t d ƒ ‚ n  |  j |  j } } | d  k rU d } n | d k  rn | | } n  | d  k rƒ | } n | d k  rœ | | } n  | | f St |  ƒ rÙ |  d k  rË |  | 7}  n  |  |  d f St d ƒ ‚ d  S(   Ni   s$   slicing with step != 1 not supportedi    s   expected slice or scalar(   i   N(	   RY   RZ   R]   R   R   R,   Ry   R   t	   TypeError(   t   slt   numt   i0t   i1(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   process_slice   s$    		
c         S` sp   d |  k o | k n sG d | k o4 | k n sG |  | k rl t  d |  | | | |  | f ƒ ‚ n  d  S(   Ni    s;   index out of bounds: 0 <= %d <= %d, 0 <= %d <= %d, %d <= %d(   RL   (   R‚   Rƒ   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyRR   ·  s    GR   (   R   R
   R   R   R   Ru   Rd   (   R   R{   t	   col_sliceR   R    R„   RR   R‚   Rƒ   t   j0t   j1R   R   R   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyR^   ›  s    			3N(   t   __name__t
   __module__t   __doc__t   formatR   t   FalseR!   R   R.   R/   R:   R6   R@   RI   Ro   Rq   Rr   R[   R^   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyR      s$   i"		x			+c         C` s   t  |  t ƒ S(   N(   RY   R   (   RH   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyR   Ê  s    (   RŠ   t
   __future__R    R   R   t   __docformat__t   __all__t   numpyR3   t   scipy._lib.sixR   t   baseR   t   _sparsetoolsR   R   R	   R
   R   t   sputilsR   R   R   R   R   R   t
   compressedR   R   R   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/csr.pyt   <module>   s   (.ÿ ´