ó
Ê½÷Xc           @` s.  d  Z  d d l m Z m Z m Z g  Z d d l m Z d d l 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 d d
 l m Z d d l m Z d d l m Z m Z m Z m Z m  Z  m! Z! m" Z" m# Z# m$ Z$ m% Z% m& Z& d e e e# f d „  ƒ  YZ' d S(   s>   Base class for sparse matrix formats using compressed storage.i    (   t   divisiont   print_functiont   absolute_import(   t   warnN(   t   zip(   t   _prune_arrayi   (   t   spmatrixt
   isspmatrixt   SparseEfficiencyWarning(   t   _data_matrixt   _minmax_mixin(   t
   dia_matrix(   t   _sparsetools(   t   upcastt   upcast_chart	   to_nativet   isdenset   isshapet   getdtypet   isscalarliket
   IndexMixint   get_index_dtypet   downcast_intp_indext   get_sum_dtypet
   _cs_matrixc           B` sZ  e  Z d  Z d3 d3 e d „ Z d3 d „ Z e j j e _ e d „ Z e	 d „ Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z 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 „  Z e j j e _ d3 d3 d3 d „ Z e j j e _ d „  Z  d „  Z! d „  Z" d „  Z# d „  Z$ d „  Z% d  „  Z& d! „  Z' d" „  Z( e	 d# „ Z) e j) j e) _ d3 d3 d$ „ Z* d% „  Z+ d& „  Z, d' „  Z- e. d( e, d) e- ƒ Z/ d* „  Z0 d+ „  Z1 d, „  Z2 e. d( e1 d) e2 ƒ Z3 d- „  Z4 d. „  Z5 d/ „  Z6 e	 d0 „ Z7 d1 „  Z8 d2 „  Z9 RS(4   sA   base matrix class for compressed row and column oriented matricesc         C` sW  t  j |  ƒ t | ƒ rb | j |  j k r@ | r@ | j ƒ  } n | j |  j ƒ } |  j | ƒ n5t | t ƒ r5t	 | ƒ r| |  _
 |  j
 \ } } t d t | | ƒ ƒ } t j d t | d t ƒƒ |  _ t j d | ƒ |  _ t j |  j | | f ƒ d d d | ƒ|  _ q—t | ƒ d k rbd d l m } |  j | | d | ƒƒ }	 |  j |	 ƒ q—t | ƒ d	 k r| \ }
 } } d  } | d  k	 r¤t | ƒ } n  t | | f d | d
 t ƒ} t j | d | d | ƒ|  _ t j | d | d | ƒ|  _ t j |
 d | d | ƒ|  _ q—t d |  j ƒ ‚ nb y t j | ƒ } Wn t d |  j ƒ ‚ n Xd d l m } |  j |  j | | d | ƒƒ ƒ | d  k	 r¯| |  _
 nj |  j
 d  k ry* t |  j ƒ d } |  j j ƒ  d } Wn t d ƒ ‚ qX|  j | | f ƒ |  _
 n  | d  k	 rCt j |  j d | ƒ|  _ n  |  j d t ƒ d  S(   Nt   maxvali    t   defaulti   t   dtypei   (   t
   coo_matrixt   shapei   t   check_contentst   copys(   unrecognized %s_matrix constructor usages!   unable to infer matrix dimensionst
   full_check(    R	   t   __init__R   t   formatR   t   asformatt	   _set_selft
   isinstancet   tupleR   R   R   t   maxt   npt   zerosR   t   floatt   datat   indicest   _swapt   indptrt   lent   cooR   t	   __class__t   Nonet   Truet   arrayt
   ValueErrort   asarrayt   check_formatt   False(   t   selft   arg1R   R   R   t   Mt   Nt	   idx_dtypeR   t   otherR+   R,   R.   R   t	   major_dimt	   minor_dim(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR!      s`    	!2!"c         C` sÁ   | d  k r t |  j d ƒ S| d k  r6 | d 7} n  |  j | d | f ƒ \ } } |  j |  j ƒ \ } } | d k r• t j t |  j ƒ d | ƒS| d k r± t j	 |  j ƒ St
 d ƒ ‚ d  S(   Niÿÿÿÿi    i   i   t	   minlengths   axis out of bounds(   R2   t   intR.   R-   R   R(   t   bincountR   R,   t   diffR5   (   R9   t   axist   _R<   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   getnnzd   s    c         C` sI   | r | j  ƒ  } n  | j |  _ | j |  _ | j |  _ | j |  _ d S(   s:   take the member variables of other and assign them to selfN(   R   R+   R,   R.   R   (   R9   R>   R   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR$   u   s    c         C` s•  |  j  d ƒ \ } } |  j  |  j ƒ \ } } |  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 |  j |  j f ƒ } t	 j
 |  j d | ƒ|  _ t	 j
 |  j d | ƒ|  _ t |  j ƒ |  _ |  j j d k s!|  j j d k s!|  j j d k r0t d ƒ ‚ n  t |  j ƒ | d k rot d	 t |  j ƒ | d f ƒ ‚ n  |  j d
 d
 k r‘t d ƒ ‚ n  t |  j ƒ t |  j ƒ k r¾t d ƒ ‚ n  |  j d t |  j ƒ k rét d ƒ ‚ n  |  j ƒ  | r‘|  j d
 k r‘|  j j ƒ  | k r6t d | | f ƒ ‚ n  |  j j ƒ  d
 k  r^t d | ƒ ‚ n  t	 j |  j ƒ j ƒ  d
 k  rŽt d ƒ ‚ qŽq‘n  d S(   s÷   check whether the matrix format is valid

        Parameters
        ----------
        full_check : bool, optional
            If `True`, rigorous check, O(N) operations. Otherwise
            basic check, O(1) operations (default True).
        t   rowt   columnt   is'   indptr array has non-integer dtype (%s)s(   indices array has non-integer dtype (%s)R   i   s'   data, indices, and indptr should be 1-Ds&   index pointer size (%d) should be (%d)i    s!   index pointer should start with 0s*   indices and data should have the same sizeiÿÿÿÿsQ   Last value of index pointer should be less than the size of index and data arrayss   %s index values must be < %ds   %s index values must be >= 0s8   index pointer values must form a non-decreasing sequenceN(   s   rows   column(   R-   R   R.   R   t   kindR   t   nameR,   R   R(   R6   R   R+   t   ndimR5   R/   t   prunet   nnzR'   t   minRD   (   R9   R    t
   major_namet
   minor_nameR?   R@   R=   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR7   €   sD    
6 
c         C` s9   |  j  ƒ  |  j | |  j | ƒ d t ƒ} | j ƒ  | S(   s‰   Scalar version of self._binopt, for cases in which no new nonzeros
        are added. Produces a new spmatrix in canonical form.
        R   (   t   sum_duplicatest
   _with_dataR+   R3   t   eliminate_zeros(   R9   R>   t   opt   res(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _scalar_binoptÅ   s    
!
c         C` s\  t  | ƒ r¤ t j | ƒ r4 |  j |  j d t j ƒS| d k rŽ t d t ƒ |  j t j |  j d t j ƒƒ } |  j	 | t
 j ƒ } | | S|  j	 | t
 j ƒ Sn´ t | ƒ rÀ |  j ƒ  | k St | ƒ rTt d t ƒ |  j | j k rï t S|  j | j k r| j |  j ƒ } n  |  j | d ƒ } |  j t j |  j d t j ƒƒ } | | St Sd  S(   NR   i    sO   Comparing a sparse matrix with 0 using == is inefficient, try using != instead.sH   Comparing sparse matrices using == is inefficient, try using != instead.t   _ne_(   R   R(   t   isnanR1   R   t   bool_R   R   t   onesRX   t   operatort   net   eqR   t   todenseR   R8   R"   R#   t   _binopt(   R9   R>   t   all_truet   invRW   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __eq__Î   s.    $$c         C` s=  t  | ƒ rÀ t j | ƒ rP t d t ƒ |  j t j |  j d t j ƒƒ } | S| d k rª t d t ƒ |  j t j |  j ƒ d t j ƒ} |  j	 | t
 j ƒ } | | S|  j	 | t
 j ƒ Sny t | ƒ rÜ |  j ƒ  | k St | ƒ r5|  j | j k rþ t S|  j | j k r%| j |  j ƒ } n  |  j | d ƒ St Sd  S(   Ns:   Comparing a sparse matrix with nan using != is inefficientR   i    s^   Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.RY   (   R   R(   RZ   R   R   R1   R\   R   R[   RX   R]   R_   R^   R   R`   R   R3   R"   R#   Ra   (   R9   R>   Rb   Rc   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __ne__î   s,    $$c         C` s¢  t  | ƒ r¯ d | k r3 | d k r3 t d ƒ ‚ qž| d | ƒ rœ t | t ƒ t j |  j d t j | ƒ ƒ} | j | ƒ |  j	 | ƒ } |  j
 | | ƒ S|  j | | ƒ Snï t | ƒ rÎ | |  j ƒ  | ƒ St | ƒ r’|  j | j k rû t d ƒ ‚ n' |  j | j k r"| j |  j ƒ } n  | d k r>|  j
 | | ƒ St d t ƒ |  j	 t j |  j ƒ ƒ } |  j
 | | d k rd n d	 ƒ } | | St d
 ƒ ‚ d  S(   Ni    t   _le_t   _ge_s    >= and <= don't work with 0.R   s   inconsistent shapessU   Comparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.t   _gt_t   _lt_s   Operands could not be compared.(   Rf   Rg   (   Rg   Rf   (   R   t   NotImplementedErrorR   R   R(   t   emptyR   t   result_typet   fillR1   Ra   RX   R   R`   R   R5   R"   R#   R\   (   R9   R>   RV   t   op_namet   bad_scalar_msgt	   other_arrRb   RW   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _inequality  s2    !$c         C` s   |  j  | t j d d ƒ S(   NRi   sg   Comparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.(   Rq   R]   t   lt(   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __lt__.  s    c         C` s   |  j  | t j d d ƒ S(   NRh   sd   Comparing a sparse matrix with a scalar less than zero using > is inefficient, try using <= instead.(   Rq   R]   t   gt(   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __gt__4  s    c         C` s   |  j  | t j d d ƒ S(   NRf   sg   Comparing a sparse matrix with a scalar greater than zero using <= is inefficient, try using > instead.(   Rq   R]   t   le(   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __le__:  s    c         C` s   |  j  | t j d d ƒ S(   NRg   sd   Comparing a sparse matrix with a scalar less than zero using >= is inefficient, try using < instead.(   Rq   R]   t   ge(   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __ge__@  s    c         C` s   t  | ƒ r1 | d k r" |  j ƒ  St d ƒ ‚ n[ t | ƒ rn | j |  j k r^ t d ƒ ‚ n  |  j | d ƒ St | ƒ rˆ |  j ƒ  | St	 Sd  S(   Ni    s;   adding a nonzero scalar to a sparse matrix is not supporteds   inconsistent shapest   _plus_(
   R   R   Rj   R   R   R5   Ra   R   R`   t   NotImplemented(   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __add__J  s    
c         C` s   |  j  | ƒ S(   N(   R|   (   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __radd__]  s    c         C` s   t  | ƒ r1 | d k r" |  j ƒ  St d ƒ ‚ n[ t | ƒ rn | j |  j k r^ t d ƒ ‚ n  |  j | d ƒ St | ƒ rˆ |  j ƒ  | St	 Sd  S(   Ni    s;   adding a nonzero scalar to a sparse matrix is not supporteds   inconsistent shapest   _minus_(
   R   R   Rj   R   R   R5   Ra   R   R`   R{   (   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __sub__`  s    
c         C` sT   t  | ƒ r2 | d k r# |  j ƒ  St d ƒ ‚ n t | ƒ rL | |  j ƒ  St Sd  S(   Ni    s;   adding a nonzero scalar to a sparse matrix is not supported(   R   R   Rj   R   R`   R{   (   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __rsub__s  s    c         C` sø  t  | ƒ r |  j | ƒ St | ƒ r÷|  j | j k rV |  j | ƒ } |  j | d ƒ S| j d
 k r| |  j | j ƒ  d ƒ S|  j d k r¢ | j |  j ƒ  d ƒ S|  j d d k rÛ | j d d k rÛ |  j | j ƒ  ƒ S|  j d d k r| j d d k r| j |  j ƒ  ƒ S| j d d k r‰|  j d | j d k r‰t	 | j ƒ  j
 ƒ  d g f d | j d | j d f ƒ} |  j | ƒ S|  j d d k rþ|  j d | j d k rþt	 |  j ƒ  j
 ƒ  d g f d |  j d |  j d f ƒ} | j | ƒ S| j d d k rs|  j d | j d k rst	 | j ƒ  j
 ƒ  d g f d | j d | j d f ƒ} | j |  ƒ S|  j d d k rè|  j d | j d k rèt	 |  j ƒ  j
 ƒ  d g f d |  j d |  j d f ƒ} | j | ƒ St d ƒ ‚ n  t j | ƒ } | j d k r+t j |  j ƒ  | ƒ S| j d k rN|  j | j d ƒ S|  j d k rwt j |  j ƒ  d | ƒ Sd d l m } |  j ƒ  } |  j | j k rÍt j | j | | j | j f ƒ } n|  j d d k rë| j d d k rt j | j | ƒ } nQ | j d |  j d k rPt j | j | d d … | j f ƒ } n t d ƒ ‚ t j t j | j d ƒ t | j ƒ ƒ } t j | j | j d ƒ } | | j t j ƒ j
 ƒ  | | f f d | j d |  j d f d	 t ƒS|  j d d k r| j d d k r9t j | j d d … d f | ƒ } nU | j d |  j d k r‚t j | j d d … d f | | j ƒ } n t d ƒ ‚ t j | j | j d ƒ } t j t j | j d ƒ t | j ƒ ƒ } | | j t j ƒ j
 ƒ  | | f f d |  j d | j d f d	 t ƒS| j d d k r{|  j d | j d k r{t j | j | d d … | j f j
 ƒ  ƒ } n^ | j d d k rÍ|  j d | j d k rÍt j | j | | j j
 ƒ  ƒ } n t d ƒ ‚ | j t j ƒ j
 ƒ  | _ | S(   sP   Point-wise multiplication by another matrix, vector, or
        scalar.
        t   _elmul_i   i    R   s   inconsistent shapesi   (   R   NR   (   i   i   (   i    i    (   i   i   (   i    i    (   i   i   (   i    i    (    R   t   _mul_scalarR   R   R1   Ra   t   toarrayt   _mul_sparse_matrixt   tocscR   t   ravelR5   R(   t
   atleast_2dRM   t   multiplyt   sizet   flatR0   R   t   tocooR+   RH   t   colt   repeatt   arangeR/   t   tilet   viewt   ndarrayR8   R2   (   R9   R>   R   R   t   retR+   RH   RŒ   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRˆ     sŽ    &&----(++'(/+'-1-%c         C` sx   |  j  \ } } t j | d t |  j j | j j ƒ ƒ} t t |  j d ƒ } | | | |  j	 |  j
 |  j | | ƒ | S(   NR   t   _matvec(   R   R(   R)   R   R   t   chart   getattrR   R"   R.   R,   R+   (   R9   R>   R;   R<   t   resultt   fn(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _mul_vectoré  s    %c      	   C` sš   |  j  \ } } | j  d } t j | | f d t |  j j | j j ƒ ƒ} t t |  j d ƒ } | | | | |  j	 |  j
 |  j | j ƒ  | j ƒ  ƒ | S(   Ni   R   t   _matvecs(   R   R(   R)   R   R   R”   R•   R   R"   R.   R,   R+   R†   (   R9   R>   R;   R<   t   n_vecsR–   R—   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _mul_multivectorö  s    4c         C` s7  |  j  \ } } | j  \ } } |  j | | f ƒ d } |  j | ƒ } t |  j |  j | j | j f d | | ƒ} t j | d d | ƒ} t t	 |  j
 d ƒ }	 |	 | | t j |  j d | ƒt j |  j d | ƒt j | j d | ƒt j | j d | ƒ| ƒ | d }
 t |  j |  j | j | j f d |
 ƒ} t j | d | ƒ} t j |
 d | ƒ} t j |
 d t |  j | j ƒ ƒ} t t	 |  j
 d ƒ }	 |	 | | t j |  j d | ƒt j |  j d | ƒ|  j t j | j d | ƒt j | j d | ƒ| j | | | ƒ |  j | | | f d | | f ƒS(	   Ni    R   i   R   t   _matmat_pass1iÿÿÿÿt   _matmat_pass2R   (   R   R-   R1   R   R.   R,   R(   Rk   R•   R   R"   R6   R   R   R+   (   R9   R>   R;   t   K1t   K2R<   t
   major_axisR=   R.   R—   RO   R,   R+   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR„     s>    	
	$c         C` sq   t  t |  j d ƒ } t j t |  j ƒ d t |  j ƒ ƒ} | |  j d |  j d |  j	 |  j
 |  j | ƒ | S(   s0   Returns the main diagonal of the matrix
        t	   _diagonalR   i    i   (   R•   R   R"   R(   Rk   RP   R   R   R   R.   R,   R+   (   R9   R—   t   y(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   diagonal*  s    '0c         C` s  t  | ƒ rÑ | | ƒ ru t d t ƒ t j |  j d t j | ƒ j ƒ} | j | ƒ |  j	 | ƒ } |  j
 | | ƒ S|  j ƒ  | |  j t j | ƒ ƒ } |  j	 | |  j |  j f d | j d |  j ƒ} | SnG t | ƒ rð | |  j ƒ  | ƒ St | ƒ r|  j
 | | ƒ St d ƒ ‚ d  S(   NsI   Taking maximum (minimum) with > 0 (< 0) number results to a dense matrix.R   R   s   Operands not compatible.(   R   R   R   R(   Rk   R   R6   R   Rm   R1   Ra   RS   R+   R,   R.   R   R`   R   R5   (   R9   R>   t   npopRn   t   dense_checkRp   t   new_datat   mat(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _maximum_minimum7  s$    $
c         C` s   |  j  | t j d d „  ƒ S(   Nt	   _maximum_c         S` s   t  j |  ƒ d k S(   Ni    (   R(   R6   (   t   x(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   <lambda>O  s    (   R¨   R(   t   maximum(   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR¬   N  s    c         C` s   |  j  | t j d d „  ƒ S(   Nt	   _minimum_c         S` s   t  j |  ƒ d k  S(   Ni    (   R(   R6   (   Rª   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR«   T  s    (   R¨   R(   t   minimum(   R9   R>   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR®   S  s    c      	   C` s  t  |  d ƒ rö | |  j d d f ƒ d k rö t |  j ƒ } t j t |  j ƒ d d | ƒ} |  j t j	 ƒ \ } } | | | <t j
 | ƒ } | d d k r­ | j } n  | d
 k	 rÚ | j | j k rÚ t d ƒ ‚ n  | j d d d | d	 | ƒ St j |  d | d | d	 | ƒSd
 S(   s~   Sum the matrix over the given axis.  If the axis is None, sum
        over both rows and columns, returning a scalar.
        t	   blocksizei   iÿÿÿÿi    i   R   s   dimensions do not matchRE   t   outN(   i   iÿÿÿÿ(   i    i   (    (   t   hasattrR-   R   R   R(   R)   R/   R.   t   _minor_reducet   addt   asmatrixt   TR2   R   R5   t   sumR   (   R9   RE   R   R°   t	   res_dtypeR’   t   major_indext   value(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR¶   \  s    "
c         C` sG   t  j t  j |  j ƒ ƒ } | j |  j t |  j | ƒ ƒ } | | f S(   sK  Reduce nonzeros with a ufunc over the minor axis when non-empty

        Warning: this does not call sum_duplicates()

        Returns
        -------
        major_index : array of ints
            Major indices where nonzero

        value : array of self.dtype
            Reduce result for nonzeros in each major_index
        (   R(   t   flatnonzeroRD   R.   t   reduceatR+   R   (   R9   t   ufuncR¸   R¹   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR²   y  s    c      	   C` sÜ  |  j  | ƒ \ } } |  j | | ƒ \ } } t | ƒ r.| j d d k o\ | j d d k } | j d d k o‚ | j d d k } | s¥ | j d | j d k oÂ | pÂ | j d | j d k sÔ t d ƒ ‚ n  |  j | j ƒ  | j ƒ  f ƒ \ } } |  j | | ƒ | j ƒ  } | j	 | j
 }	 }
 t j | j d |  j ƒ} | r¨t j t j | j d ƒ t |	 ƒ ƒ }	 t j |
 | j d ƒ }
 t j | | j d ƒ } n  | rt j |	 | j d ƒ }	 t j t j | j d ƒ t |
 ƒ ƒ }
 t j | | j d ƒ } n  | |	 |
 f } | |	 |
 f } nQ t j | d |  j ƒ} t j | | ƒ \ } } | j | j k rt d ƒ ‚ n  t j | ƒ d k r˜d  S|  j | j ƒ  | j ƒ  f ƒ \ } } |  j | | | j ƒ  ƒ d  S(   Ni    i   s   shape mismatch in assignmentR   (   t   _unpack_indext   _index_to_arraysR   R   R5   R-   R†   t
   _zero_manyR‹   RH   RŒ   R(   R6   R+   R   R   RŽ   R/   R   t   broadcast_arraysR‰   t	   _set_many(   R9   t   indexRª   RJ   t   jt   broadcast_rowt   broadcast_colt   cit   cjt   rt   cRF   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __setitem__  s>    &&  '(('c   	      C` s]  d |  j  k r d  S|  j  \ } } | j d k } | d k  r¸ | rY t | | | ƒ } n t | | | t | ƒ ƒ } t j | d |  j j ƒ} t j | d |  j j ƒ} | | 8} nx | rÔ t | | | ƒ } n t | | | t | ƒ ƒ } t j | d |  j j ƒ} t j | d |  j j ƒ} | | 7} | sI| t | ƒ  } n  | |  | | f <d  S(   Ni    R   (   R   RM   RP   R/   R(   RŽ   R,   R   (	   R9   t   valuest   kR;   R<   t	   broadcastt	   max_indexRJ   RÃ   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _setdiagº  s(    
c         C` s   |  j  |  j ƒ \ } } d „  } | | | ƒ | | | ƒ t j | d |  j j ƒ} t j | d |  j j ƒ} | | | | f S(   Nc         S` sg   |  j  ƒ  } | | k r1 t d | | f ƒ ‚ n  |  j ƒ  } | | k  rc t d | | f ƒ ‚ n  d  S(   Ns   index (%d) out of range (>= %d)s   index (%d) out of range (< -%d)(   R'   t
   IndexErrorRP   (   R,   t   boundt   idx(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   check_boundsÛ  s    R   (   R-   R   R(   R6   R,   R   (   R9   RJ   RÃ   R;   R<   RÓ   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _prepare_indicesØ  s    	
c   
   	   C` sd  |  j  | | ƒ \ } } } } t | ƒ } t j | d |  j j ƒ} t j | | |  j |  j | | | | ƒ } | d k r° |  j	 ƒ  t j | | |  j |  j | | | | ƒ n  d | k rÍ | |  j
 | <d St d |  j t ƒ | d k }	 | |	 |  j
 | |	 <|	 }	 | |	 } | | d k  c | 7<| |	 } | | d k  c | 7<|  j | | | |	 ƒ d S(   s_   Sets value at each (i, j) to x

        Here (i,j) index major and minor respectively.
        R   i   iÿÿÿÿNsZ   Changing the sparsity structure of a %s_matrix is expensive. lil_matrix is more efficient.i    (   RÔ   R/   R(   Rk   R,   R   R   t   csr_sample_offsetsR.   RS   R+   R   R"   R   t   _insert_many(
   R9   RJ   RÃ   Rª   R;   R<   t	   n_samplest   offsetsR’   t   mask(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRÁ   ì  s0    



c      	   C` sË   |  j  | | ƒ \ } } } } t | ƒ } t j | d |  j j ƒ} t j | | |  j |  j | | | | ƒ } | d k r° |  j	 ƒ  t j | | |  j |  j | | | | ƒ n  d |  j
 | | d k <d S(   s‚   Sets value at each (i, j) to zero, preserving sparsity structure.

        Here (i,j) index major and minor respectively.
        R   i   i    iÿÿÿÿN(   RÔ   R/   R(   Rk   R,   R   R   RÕ   R.   RS   R+   (   R9   RJ   RÃ   R;   R<   R×   RØ   R’   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR¿     s    

c         C` sg  t  j | d d ƒ} | j | d d ƒ} | j | d d ƒ} | j | d d ƒ} |  j } t |  j |  j f d |  j d | j ƒ} t  j |  j d | ƒ|  _ t  j |  j d | ƒ|  _ t  j | d | ƒ} t  j | d | ƒ} g  } g  } t  j	 | d t
 ƒ\ }	 }
 t  j |
 t | ƒ ƒ }
 t  j |
 ƒ } d	 } xKt t |	 |
 |
 d
 ƒ ƒ D]-\ } \ } } } |  j | } |  j | } | j |  j | | !ƒ | j |  j | | !ƒ t  j	 | | | !d d d … d t
 ƒ\ } } t | ƒ | | k r'| j | | | !ƒ | j | | | !ƒ nZ | j | | | !d d d … | ƒ | j | | | !d d d … | ƒ t | ƒ | | <| } qZW|  j | } | j |  j | ƒ | j |  j | ƒ t  j | ƒ |  _ t  j | ƒ |  _ t  j t  j |  j d d	 ƒd | ƒ} | d
 |	 c | 7<t  j | d | ƒ|  _ | rSt |  _ |  j ƒ  n  |  j d t ƒ d S(   s:  Inserts new nonzero at each (i, j) with value x

        Here (i,j) index major and minor respectively.
        i, j and x must be non-empty, 1d arrays.
        Inserts each major group (e.g. all entries per row) at a time.
        Maintains has_sorted_indices property.
        Modifies i, j, x in place.
        RK   t	   mergesortt   modet   clipR   iÿÿÿÿR   t   return_indexi    i   Nt   to_beginR°   R    (   R(   t   argsortt   taket   has_sorted_indicesR   R,   R.   R‰   R6   t   uniqueR3   t   appendR/   RD   t	   enumeratet   izipR+   t   concatenatet   ediff1dt   cumsumR8   t   sort_indicesR7   (   R9   RJ   RÃ   Rª   t   ordert   do_sortR=   t   indices_partst
   data_partst   uit	   ui_indptrt   new_nnzst   prevRÉ   t   iit   jst   jet   startt   stopt   ujt	   uj_indptrt   nnzs(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRÖ   '  sT    		2/%%
'	c   	      C` sü   |  j  \ } } | d k  r( | | 7} n  | d k  rA | | 7} n  d | k oX | k  n s{ d | k ou | k  n rŠ t d ƒ ‚ n  |  j | | f ƒ \ } } |  j | } |  j | d } t j | |  j | | !k |  j | | !ƒ j d |  j	 ƒ S(   Ni    s   index out of boundsi   R   (
   R   RÐ   R-   R.   R(   t   compressR,   R+   R¶   R   (	   R9   RH   RŒ   R;   R<   R¸   t   minor_indexRõ   t   end(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _get_single_elementl  s    :c      
   C` s  |  j  | | f ƒ \ } } |  j  |  j ƒ \ } } d „  } d „  } | | | ƒ \ } } | | | ƒ \ }	 }
 | | | | ƒ | |	 |
 | ƒ t j | | |  j |  j |  j | | |	 |
 ƒ	 } | d | d | d } } } |  j  | | |
 |	 f ƒ } |  j | | | f d | ƒS(   s:   Return a submatrix of this matrix (new matrix is created).c         S` sÔ   t  |  t ƒ rˆ |  j |  j } } | d  k r7 d } n | d k  rP | | } n  | d  k re | } n | d k  r~ | | } n  | | f St j |  ƒ r¾ |  d k  r° |  | 7}  n  |  |  d f S|  d |  d f Sd  S(   Ni    i   (   R%   t   sliceRõ   Rö   R2   R(   t   isscalar(   t   slt   numt   i0t   i1(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.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(   RÐ   (   R  R  R  (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt
   _in_bounds  s    Gi   i   i    R   (   R-   R   R   t   get_csr_submatrixR.   R,   R+   R1   (   R9   t   slice0t   slice1t   shape0t   shape1R  R  R  R  t   j0t   j1t   auxR+   R,   R.   R   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _get_submatrix  s    		 c   	      C` s±   |  j  |  j ƒ \ } } |  j } t j t | ƒ d |  j j ƒ} t j | |  j	 | ƒ |  j  | | f ƒ \ } } d d l
 m } | |  j | | f f |  j d | d |  j ƒS(   NR   i   (   R   R   (   R-   R   R,   R(   Rk   R/   R   R   t	   expandptrR.   R0   R   R+   (	   R9   R   R?   R@   t   minor_indicest   major_indicesRH   RŒ   R   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR‹   µ  s    	!$c         C` s"   |  j  d t ƒ j d | d | ƒ S(   s)   See the docstring for `spmatrix.toarray`.R   Rê   R°   (   R‹   R8   Rƒ   (   R9   Rê   R°   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRƒ   Â  s    c         C` sH   |  j  |  j ƒ \ } } t j | | |  j |  j |  j ƒ |  j ƒ  d S(   sU   Remove zero entries from the matrix

        This is an *in place* operation
        N(   R-   R   R   t   csr_eliminate_zerosR.   R,   R+   RN   (   R9   R;   R<   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRU   Ê  s    
c         C` sb   t  |  d t ƒ s t |  _ n= t |  d ƒ s[ t j t |  j ƒ d |  j |  j	 ƒ |  _
 n  |  j S(   s^  Determine whether the matrix has sorted indices and no duplicates

        Returns
            - True: if the above applies
            - False: otherwise

        has_canonical_format implies has_sorted_indices, so if the latter flag
        is False, so will the former be; if the former is found True, the
        latter flag is also set.
        t   _has_sorted_indicest   _has_canonical_formati   (   R•   R3   R8   R  R±   R   t   csr_has_canonical_formatR/   R.   R,   t   has_canonical_format(   R9   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __get_has_canonical_formatÔ  s    (c         C` s%   t  | ƒ |  _ | r! t |  _ n  d  S(   N(   t   boolR  R3   Rá   (   R9   t   val(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __set_has_canonical_formaté  s    t   fgett   fsetc         C` sh   |  j  r d S|  j ƒ  |  j |  j ƒ \ } } t j | | |  j |  j |  j ƒ |  j	 ƒ  t
 |  _  d S(   sk   Eliminate duplicate matrix entries by adding them together

        The is an *in place* operation
        N(   R  Ré   R-   R   R   t   csr_sum_duplicatesR.   R,   R+   RN   R3   (   R9   R;   R<   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRS   ñ  s    	


c         C` sD   t  |  d ƒ s= t j t |  j ƒ d |  j |  j ƒ |  _ n  |  j S(   s®   Determine whether the matrix has sorted indices

        Returns
            - True: if the indices of the matrix are in sorted order
            - False: otherwise

        R  i   (   R±   R   t   csr_has_sorted_indicesR/   R.   R,   R  (   R9   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __get_sorted  s    
(c         C` s   t  | ƒ |  _ d  S(   N(   R  R  (   R9   R  (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   __set_sorted  s    c         C` s   |  j  ƒ  } | j ƒ  | S(   s9   Return a copy of this matrix with sorted indices
        (   R   Ré   (   R9   t   A(    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   sorted_indices  s    
c         C` sE   |  j  sA t j t |  j ƒ d |  j |  j |  j ƒ t |  _  n  d S(   s3   Sort the indices of this matrix *in place*
        i   N(   Rá   R   t   csr_sort_indicesR/   R.   R,   R+   R3   (   R9   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRé      s    	c         C` sÂ   |  j  |  j ƒ d } t |  j ƒ | d k r> t d ƒ ‚ n  t |  j ƒ |  j k  re t d ƒ ‚ n  t |  j ƒ |  j k  rŒ t d ƒ ‚ n  t |  j |  j  ƒ |  _ t |  j |  j  ƒ |  _ d S(   s8   Remove empty space after all non-zero elements.
        i    i   s    index pointer has invalid lengths)   indices array has fewer than nnz elementss&   data array has fewer than nnz elementsN(	   R-   R   R/   R.   R5   R,   RO   R+   R   (   R9   R?   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRN   )  s    c         C` sr   | r@ |  j  | |  j j ƒ  |  j j ƒ  f d |  j d | j ƒS|  j  | |  j |  j 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 structure arrays
        (i.e. .indptr and .indices) are copied.
        R   R   N(   R1   R,   R   R.   R   R   (   R9   R+   R   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRT   =  s
    'c         C` s©  |  j  | ƒ } t t |  j | |  j ƒ } |  j | j } t |  j |  j | j | j f d | ƒ} t j	 |  j j
 d | ƒ} t j	 | d | ƒ} d d d d d g } | | k rÒ t j	 | d t j ƒ}	 n$ t j	 | d t |  j | j ƒ ƒ}	 | |  j
 d |  j
 d	 t j |  j d | ƒt j |  j d | ƒ|  j t j | j d | ƒt j | j d | ƒ| j | | |	 ƒ |  j  |	 | | f d
 |  j
 ƒ}
 |
 j ƒ  |
 S(   s5   apply the binary operation fn to two sparse matrices.R   R   RY   Ri   Rh   Rf   Rg   i    i   R   (   R1   R•   R   R"   RO   R   R.   R,   R(   Rk   R   R[   R   R   R6   R+   RN   (   R9   R>   RV   R—   t   maxnnzR=   R.   R,   t   bool_opsR+   R!  (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyRa   I  s.    	$!
c         C` sÙ   | j  |  j  k r! t d ƒ ‚ n  |  j | d ƒ } t j | j t j ƒ rÏ t j |  j  d |  j ƒ} | j t j	 ƒ | j
 ƒ  \ } } d | | | f <| j ƒ  } | j | | j | j f <t j | ƒ } n | } | S(   s?   
        Divide this matrix by a second sparse matrix.
        s   inconsistent shapest   _eldiv_R   i    (   R   R5   Ra   R(   t
   issubdtypeR   t   inexactRk   Rm   t   nant   nonzeroR‹   R+   RH   RŒ   t   matrix(   R9   R>   RÈ   R°   RH   RŒ   (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   _divide_sparsek  s    N(:   t   __name__t
   __module__t   __doc__R2   R8   R!   RG   R   R$   R3   R7   RX   Rd   Re   Rq   Rs   Ru   Rw   Ry   R|   R}   R   R€   Rˆ   R˜   R›   R„   R£   R¨   R¬   R®   R¶   R²   RÊ   RÏ   RÔ   RÁ   R¿   RÖ   Rý   R  R‹   Rƒ   RU   t$   _cs_matrix__get_has_canonical_formatt$   _cs_matrix__set_has_canonical_formatt   propertyR  RS   t   _cs_matrix__get_sortedt   _cs_matrix__set_sortedRá   R"  Ré   RN   RT   Ra   R,  (    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyR      sr   KE			 		"				
					h			'						+			&		E		6	
											"((   R/  t
   __future__R    R   R   t   __all__t   warningsR   R]   t   numpyR(   t   scipy._lib.sixR   Rå   t   scipy._lib._utilR   t   baseR   R   R   R+   R	   R
   t   diaR   t    R   t   sputilsR   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s6   /tmp/pip-build-7oUkmx/scipy/scipy/sparse/compressed.pyt   <module>   s   L