ó
Ê½÷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 m Z d d
 l m Z m Z m Z m Z m Z m Z m Z d d l m Z d e e f d „  ƒ  YZ e d „ Z d „  Z d S(   s    LInked List sparse matrix class
i    (   t   divisiont   print_functiont   absolute_imports   restructuredtext ent
   lil_matrixt   isspmatrix_lilN(   t   xrangei   (   t   spmatrixt
   isspmatrix(   t   getdtypet   isshapet   isscalarliket
   IndexMixint   upcast_scalart   get_index_dtypet	   isintlike(   t   _csparsetoolsc           B` s  e  Z d  Z d Z d d e d „ Z d „  Z e j j e _ e	 d e j
 d e ƒ Z d „  Z d „  Z d „  Z d	 „  Z d d
 „ Z d „  Z e j j e _ e j j e _ d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z e j j e _ d d „ Z e j j e _ d d d „ Z d e d „ Z e j j e _ e d „ Z  e j  j e  _ e d „ Z! e j! j e! _ RS(   s  Row-based linked list sparse matrix

    This is a structure for constructing sparse matrices incrementally.
    Note that inserting a single item can take linear time in the worst case;
    to construct a matrix efficiently, make sure the items are pre-sorted by
    index, per row.

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

        lil_matrix(S)
            with another sparse matrix S (equivalent to S.tolil())

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

    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
        LIL format data array of the matrix
    rows
        LIL format row 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 LIL format
        - supports flexible slicing
        - changes to the matrix sparsity structure are efficient

    Disadvantages of the LIL format
        - arithmetic operations LIL + LIL are slow (consider CSR or CSC)
        - slow column slicing (consider CSC)
        - slow matrix vector products (consider CSR or CSC)

    Intended Usage
        - LIL is a convenient format for constructing sparse matrices
        - once a matrix has been constructed, convert to CSR or
          CSC format for fast arithmetic and matrix vector operations
        - consider using the COO format when constructing large matrices

    Data Structure
        - An array (``self.rows``) of rows, each of which is a sorted
          list of column indices of non-zero elements.
        - The corresponding nonzero values are stored in similar
          fashion in ``self.data``.


    t   lilc   
      C` s  t  j |  ƒ t | | d t ƒ|  _ t | ƒ r¯ t | ƒ rR | rR | j ƒ  } n | j ƒ  } | d  k	 r| | j
 | ƒ } n  | j |  _ | j |  _ | j |  _ | j |  _ nUt | t ƒ ryt | ƒ rj| d  k	 rå t d ƒ ‚ n  | \ } } | | f |  _ t j | f d t ƒ|  _ t j | f d t ƒ|  _ x= t | ƒ D]  } g  |  j | <g  |  j | <qCWqt d ƒ ‚ n‹ y t j | ƒ } Wn t k
 r«t d ƒ ‚ nY Xd d l m }	 |	 | d | ƒj ƒ  } | j |  _ | j |  _ | j |  _ | j |  _ d  S(   Nt   defaults   invalid use of shape parametert   dtypes)   unrecognized lil_matrix constructor usages   unsupported matrix typei   (   t
   csr_matrix(   R   t   __init__R   t   floatR   R   R   t   copyt   tolilt   Nonet   astypet   shapet   rowst   datat
   isinstancet   tupleR	   t
   ValueErrort   npt   emptyt   objectt   ranget	   TypeErrort   asmatrixt   csrR   (
   t   selft   arg1R   R   R   t   At   Mt   Nt   iR   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR   T   sD    c         C` s  t  | ƒ } t | ƒ d k r- t d ƒ ‚ n  y$ t | d ƒ t | d ƒ f } Wn t d ƒ ‚ n X| d d k o„ | d d k s– t d ƒ ‚ n  |  j | k rô |  j d  k	 rô y |  j | ƒ }  Wqô t k
 rð t d |  j	 j
 ƒ ‚ qô Xn  | |  _ d  S(   Ni   s1   Only two-dimensional sparse arrays are supported.i    i   s   invalid shapes!   Reshaping not implemented for %s.(   R   t   lenR   t   intR$   t   _shapeR   t   reshapet   NotImplementedErrort	   __class__t   __name__(   R'   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt	   set_shape‚   s     $ t   fgett   fsetc         C` s$   |  | |  d  d  … d  d  … f <|  S(   N(    (   R'   t   other(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   __iadd__œ   s     c         C` s$   |  | |  d  d  … d  d  … f <|  S(   N(    (   R'   R7   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   __isub__    s     c         C` s8   t  | ƒ r0 |  | |  d  d  … d  d  … f <|  St Sd  S(   N(   R
   t   NotImplemented(   R'   R7   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   __imul__¤   s     c         C` s8   t  | ƒ r0 |  | |  d  d  … d  d  … f <|  St Sd  S(   N(   R
   R:   (   R'   R7   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   __itruediv__«   s     c         C` sì   | d  k r2 t g  |  j D] } t | ƒ ^ q ƒ S| d k  rK | d 7} n  | d k rž t j |  j d d t j ƒ} x! |  j D] } | | c d 7<q€ W| S| d k rÜ t j	 g  |  j D] } t | ƒ ^ qº d t j ƒSt
 d ƒ ‚ d  S(   Ni    i   i   R   s   axis out of bounds(   R   t   sumR   R-   R    t   zerosR   t   intpR   t   arrayR   (   R'   t   axist   rowvalst   outt   row(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   getnnzµ   s    &2c         C` s   t  d „  |  j Dƒ ƒ S(   Nc         s` s   |  ] } t  j | ƒ Vq d  S(   N(   R    t   count_nonzero(   t   .0RB   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pys	   <genexpr>Å   s    (   R=   R   (   R'   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyRF   Ä   s    c         C` s|   d } xk t  |  j ƒ D]Z \ } } xK t  | ƒ D]= \ } } | d t | | f ƒ t |  j | | ƒ f 7} q/ Wq W| d  S(   Nt    s     %s	%s
iÿÿÿÿ(   t	   enumerateR   t   strR   (   R'   t   valR,   RD   t   post   j(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   __str__Ê   s
    9c         C` sN   t  d |  j d f d |  j ƒ} |  j | | j d <|  j | | j d <| S(   s;   Returns a view of the 'i'th row (without copying).
        i   R   i    (   R   R   R   R   R   (   R'   R,   t   new(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt
   getrowviewÑ   s    "c         C` s_   |  j  | ƒ } t d |  j d f d |  j ƒ} |  j | | j d <|  j | | j d <| S(   s)   Returns a copy of the 'i'th row.
        i   R   i    (   t   _check_row_boundsR   R   R   R   R   (   R'   R,   RO   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   getrowÙ   s
    "c         C` sR   | d k  r  | |  j  d 7} n  | d k  s? | |  j  d k rN t d ƒ ‚ n  | S(   Ni    s   row index out of bounds(   R   t
   IndexError(   R'   R,   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyRQ   â   s
    c         C` sR   | d k  r  | |  j  d 7} n  | d k  s? | |  j  d k rN t d ƒ ‚ n  | S(   Ni    i   s   column index out of bounds(   R   RS   (   R'   RM   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   _check_col_boundsé   s
    c   	      C` sÇ  t  | t ƒ r´ t | ƒ d k r´ | \ } } t  | t ƒ sN t  | t j ƒ r´ t  | t ƒ so t  | t j ƒ r´ t j |  j d |  j d |  j	 |  j
 | | ƒ } |  j j | ƒ Sn  |  j | ƒ \ } } t | ƒ } t | ƒ } | r/| r/t j |  j d |  j d |  j	 |  j
 | | ƒ } |  j j | ƒ S| sDt  | t ƒ r| ro|  j | ƒ } t | | d ƒ } n  | r |  j | ƒ } t | | d ƒ } d } nL t  | t ƒ rÔt | j |  j d ƒ Œ  } d } n t j | ƒ } | j } | d k s
t | ƒ d k r|  j | | ƒ Sn  |  j | | ƒ \ } } | j d k rZt | j d |  j ƒSt | j d |  j ƒ} t | | ƒ \ } } t j |  j d |  j d |  j	 |  j
 | j	 | j
 | | ƒ | S(   s®   Return the element(s) index=(i, j), where j may be a slice.
        This always returns a copy for consistency, since slices into
        Python lists return copies.
        i   i    i   R   N(   R   R   R-   R.   R    t   integerR   t   lil_get1R   R   R   R   t   typet   _unpack_indexR   t   sliceRT   RQ   R   R   t   indicest
   atleast_1dt   _get_row_rangest   _index_to_arrayst   sizeR   t   _prepare_index_for_memoryviewt   lil_fancy_get(	   R'   t   indexR,   RM   t   vt	   i_intliket	   j_intliket   i_shapeRO   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   __getitem__ð   sR    !!!			
c   	      C` s§   | j  |  j d ƒ \ } } } t | | | ƒ } t | ƒ } t t | ƒ | f d |  j ƒ} t j |  j d |  j d |  j |  j	 | j | j	 | | | | | ƒ | S(   sµ  
        Fast path for indexing in the case where column index is slice.

        This gains performance improvement over brute force by more
        efficient skipping of zeros, by accessing the elements
        column-wise in order.

        Parameters
        ----------
        rows : sequence or xrange
            Rows indexed. If xrange, must be within valid bounds.
        col_slice : slice
            Columns indexed

        i   R   i    (
   RZ   R   R   R-   R   R   R   t   lil_get_row_rangesR   R   (	   R'   R   t	   col_slicet   j_startt   j_stopt   j_stridet	   col_ranget   njRO   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR\   0  s    !c         C` sU  t  | t ƒ rÙ t | ƒ d k rÙ | \ } } t  | t ƒ sN t  | t j ƒ rÙ t  | t ƒ so t  | t j ƒ rÙ |  j j | ƒ } | j d k rŸ t	 d ƒ ‚ n  t
 j |  j d |  j d |  j |  j | | | ƒ d  Sn  |  j | ƒ \ } } t | ƒ rt  | t ƒ r| t d  ƒ k rt  | t ƒ r| t d  ƒ k r| j |  j k rt | d |  j ƒ} | j |  _ | j |  _ d  S|  j | | ƒ \ } } t | ƒ r²| j ƒ  } n  t j | d |  j ƒ} t j | | ƒ \ } } | j | j k rt	 d ƒ ‚ n  t | | | ƒ \ } } } t
 j |  j d |  j d |  j |  j | | | ƒ d  S(   Ni   i   s&   Trying to assign a sequence to an itemi    R   s   shape mismatch in assignment(   R   R   R-   R.   R    RU   R   RW   R^   R   R   t
   lil_insertR   R   R   RX   R   RY   R   R   R]   t   toarrayt   asarrayt   broadcast_arraysR_   t   lil_fancy_set(   R'   Ra   t   xR,   RM   t   _(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   __setitem__M  s:    !!!-!c         C` sœ   | d k r' t  |  j d |  j ƒ} nq t |  j | ƒ } |  j ƒ  } | j | ƒ } xA t | j ƒ D]0 \ } } g  | D] } | | ^ qw | j | <qd W| S(   Ni    R   (   R   R   R   R   R   R   RI   R   (   R'   R7   RO   t	   res_dtypeRM   RB   RK   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   _mul_scalar}  s    (c         C` sr   t  | ƒ r` |  j ƒ  } xA t | j ƒ D]0 \ } } g  | D] } | | ^ q; | j | <q( W| S|  j ƒ  | Sd  S(   N(   R
   R   RI   R   t   tocsr(   R'   R7   RO   RM   RB   RK   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   __truediv__‹  s    (c         C` sP   d d l  m } t |  j d |  j ƒ} | |  j ƒ | _ | |  j ƒ | _ | S(   Ni    (   t   deepcopyR   (   R   Rz   R   R   R   R   R   (   R'   Rz   RO   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR   •  s
    t   Cc         C` sK  t  | ƒ t k s | d k r- t d ƒ ‚ n  t  | ƒ t k rN t d ƒ ‚ n  t | ƒ d k ro t d ƒ ‚ n  t | d |  j ƒ} |  j d } | j d | j d |  j d |  j d k rÐ t d	 ƒ ‚ n  xt t	 |  j
 ƒ D]c \ } } xT t	 | ƒ D]F \ } } t j | | | | ƒ \ }	 }
 |  | | f | |	 |
 f <qù Wqà W| S(
   NR{   s4   Sparse matrices do not support an 'order' parameter.s%   a tuple must be passed in for 'shape'i   s.   a length-2 tuple must be passed in for 'shape'R   i   i    s^   the product of the dimensions for the new sparse matrix must equal that of the original matrix(   RW   RJ   R   R   R$   R-   R   R   R   RI   R   R    t   unravel_index(   R'   R   t   orderRO   t   j_maxR,   RD   t   colRM   t   new_rt   new_c(    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR0   ž  s    0 "c         C` sn   |  j  | | ƒ } xU t |  j ƒ D]D \ } } x5 t | ƒ D]' \ } } |  j | | | | | f <q; Wq" W| S(   s)   See the docstring for `spmatrix.toarray`.(   t   _process_toarray_argsRI   R   R   (   R'   R}   RC   t   dR,   RD   RL   RM   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyRo   º  s
    #c         C` s"   |  j  ƒ  j d | d | ƒ j ƒ  S(   Nt   axesR   (   Rx   t	   transposeR   (   R'   R„   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR…   Â  s    c         C` s   | r |  j  ƒ  S|  Sd  S(   N(   R   (   R'   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR   Ç  s    
c   	      C` s9  g  |  j  D] } t | ƒ ^ q
 } t d t |  j d t | ƒ ƒ ƒ } t j | d | ƒ} t j t j	 d g d | ƒt j
 | d | ƒf ƒ } g  } x |  j  D] } | j | ƒ q¢ Wt j | d | ƒ} g  } x |  j D] } | j | ƒ qÞ Wt j | d |  j ƒ} d d l m } | | | | f d |  j ƒS(   Nt   maxvali   R   i    (   R   R   (   R   R-   R   t   maxR   R=   R    Rp   t   concatenateR@   t   cumsumt   extendR   R   R&   R   (	   R'   R   Rs   t   lstt	   idx_dtypet   indptrRZ   R   R   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyRx   Ï  s    "%N("   R3   t
   __module__t   __doc__t   formatR   t   FalseR   R4   R   t   propertyt	   get_shapeR   R8   R9   R;   R<   RE   RF   RN   RP   RR   RQ   RT   Rf   R\   Ru   Rw   Ry   R   R0   Ro   R…   R   Rx   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR      sF   >.					
								@		0		
	c         C` s
  |  j  | j  k r' | j |  j  ƒ } n' |  j  | j  k  rN |  j | j  ƒ }  n  |  j j sv |  j  t j t j f k r‹ |  j t j ƒ }  n  | j j s³ | j  t j t j f k rÈ | j t j ƒ } n  | d k	 rü | j j sï | j	 ƒ  } n  |  | | f S|  | f Sd S(   s  
    Convert index and data arrays to form suitable for passing to the
    Cython fancy getset routines.

    The conversions are necessary since to (i) ensure the integer
    index arrays are in one of the accepted types, and (ii) to ensure
    the arrays are writable so that Cython memoryview support doesn't
    choke on them.

    Parameters
    ----------
    i, j
        Index arrays
    x : optional
        Data arrays

    Returns
    -------
    i, j, x
        Re-formatted arrays (x is omitted, if input was None)

    N(
   R   R   t   flagst	   writeableR    t   int32t   int64R?   R   R   (   R,   RM   Rs   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR_   æ  s    ((c         C` s   t  |  t ƒ S(   N(   R   R   (   Rs   (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyR     s    (   R   t
   __future__R    R   R   t   __docformat__t   __all__t   numpyR    t   scipy._lib.sixR   t   baseR   R   t   sputilsR   R	   R
   R   R   R   R   RH   R   R   R   R_   R   (    (    (    s/   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/lil.pyt   <module>   s   4ÿ Ô)