
    xf                         d 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
mZmZ ddlmZ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 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'm(Z(  G d deee      Z)d Z*y)z>Base class for sparse matrix formats using compressed storage.    )warnN)_prune_array   )_spbaseissparseSparseEfficiencyWarning)_data_matrix_minmax_mixin)_sparsetools)get_csr_submatrixcsr_sample_offsetscsr_todensecsr_sample_valuescsr_row_indexcsr_row_slicecsr_column_index1csr_column_index2)
IndexMixin)upcastupcast_char	to_nativeisdenseisshapegetdtypeisscalarlike	isintlikedowncast_intp_indexget_sum_dtypecheck_shapeis_pydata_spmatrixc                   X   e Zd ZdZd=dZd>dZej
                  j                  e_        d?dZd@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dAdZej0                  j                  e_        d Zd Zej4                  j                  e_        d Zej6                  j                  e_        dBdZej8                  j                  e_        d>dZd Zd Zd Z d Z!d  Z"d?d!Z#d" Z$d?d#Z%d=d$Z&d% Z'd& Z(d' Z)d( Z*d) Z+d* Z,d+ Z-d, Z.d@d-Z/ej^                  j                  e/_        dCd.Z0ej`                  j                  e0_        d/ Z1d0 Z2d1 Z3 e4e2e32      Z5d3 Z6d4 Z7d5 Z8 e4e7e82      Z9d6 Z:d7 Z;d8 Z<d9 Z=ejz                  j                  e=_        d@d:Z>d; Z?d< Z@y)D
_cs_matrixzBbase matrix class for compressed row- and column-oriented matricesNc                    t        j                  |        t        |      rZ|j                  | j                  k(  r|r|j	                         }n|j                  | j                        }| j                  |       nt        |t              rt        |      rt        |      | _        | j                  \  }}| j                  t        ||            }t        j                   dt#        |t$                    | _        t        j                   d|      | _        t        j                   | j+                  ||f      d   dz   |      | _        n?t/        |      dk(  r5| j1                  | j3                  |||            }| j                  |       nt/        |      dk(  r|\  }	}
}d }|t        |      }| j                  |
|f|d	
      }t        j4                  |
||      | _        t        j4                  |||      | _        t        j4                  |	||      | _        nkt7        dj                  | j                              	 t        j8                  |      }| j                  | j1                  | j3                  ||                   |t        |      | _        nc| j                  W	 t/        | j,                        dz
  }| j(                  j                         dz   }t        | j+                  ||f            | _        |"| j&                  j=                  |d      | _        | j?                  d       y # t:        $ r*}t7        dj                  | j                              |d }~ww xY w# t:        $ r}t7        d      |d }~ww xY w)Nmaxvalr   )defaultr   dtype   shaper(      T)r%   check_contentscopyr(   z(unrecognized {}_matrix constructor usagez!unable to infer matrix dimensionsFr/   
full_check) r	   __init__r   formatr/   asformat	_set_self
isinstancetupler   r   _shaper+   _get_index_dtypemaxnpzerosr   floatdataindices_swapindptrlen	__class___coo_containerarray
ValueErrorasarray	Exceptionastypecheck_format)selfarg1r+   r(   r/   MN	idx_dtypeotherr?   r@   rB   r%   e	major_dim	minor_dims                   :/usr/lib/python3/dist-packages/scipy/sparse/_compressed.pyr3   z_cs_matrix.__init__   s"   d#D>{{dkk)dyy{}}T[[1NN4 e$t} *$/zz1 !11Q1C	HHQ(FG	!xx95 hhtzz1a&'9!'<q'@-68 t9> NN++DU+KE NN5)Y!^.2+T7F "F(!$U $ 5 5w6G7=?C !6 !EI $&88G$2;$=DL"$((6I"NDK "D FDI$ &99?9LN N
@zz$' NN4>>##D#6 
 %e,DKzz!G #DKK 01 4I $ 0 0 2Q 6I #.djj):C:E /F #GDK 		((U(;DIU+3  @  "$$*F4;;$79>?@@  ! Q$%HIqPQs0   L8 +5M. 8	M+%M&&M+.	N7NNc                 t   |t        | j                  d         S |dk  r|dz  }| j                  |d|z
  f      \  }}| j                  | j                        \  }}|dk(  r*t	        j
                  t        | j                        |      S |dk(  rt	        j                  | j                        S t        d      )Nr   r)   r   )	minlengthzaxis out of bounds)
intrB   rA   r+   r<   bincountr   r@   diffrG   )rL   axis_rO   s       rU   _getnnzz_cs_matrix._getnnzk   s    <t{{2''ax	jj$D!12GD!::djj)DAqqy{{#6t||#D-.0 0wwt{{++122    c                     |r|j                         }|j                  | _        |j                  | _        |j                  | _        t	        |j
                        | _        y)z:take the member variables of other and assign them to selfN)r/   r?   r@   rB   r   r+   r9   )rL   rQ   r/   s      rU   r6   z_cs_matrix._set_self|   sC     JJLEJJ	}}ll!%++.r_   c                    | j                  d      \  }}| j                  | j                        \  }}| j                  j                  j                  dk7  r:t        dj                  | j                  j                  j                        d       | j                  j                  j                  dk7  r:t        dj                  | j                  j                  j                        d       | j                  | j                  | j                  f      }t        j                  | j                  |      | _        t        j                  | j                  |      | _        t        | j                        | _        | j                  j                  | j                  j                  | j                  j                  fD ]  }|dk7  s	t        d	       t!        | j                        |dz   k7  r1t        d
j                  t!        | j                        |dz               | j                  d   dk7  rt        d      t!        | j                        t!        | j                        k7  rt        d      | j                  d   t!        | j                        kD  rt        d      | j#                          |r| j$                  dkD  r| j                  j'                         |k\  rt        dj                  ||            | j                  j)                         dk  rt        dj                  |            t        j*                  | j                        j)                         dk  rt        d      yyy)zcheck 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).
        )rowcolumniz'indptr array has non-integer dtype ({})r,   
stacklevelz(indices array has non-integer dtype ({})r'   r   z'data, indices, and indptr should be 1-Dz&index pointer size ({}) should be ({})r   z!index pointer should start with 0z*indices and data should have the same sizerW   zQLast value of index pointer should be less than the size of index and data arraysz{} index values must be < {}z{} index values must be >= 0z8index pointer values must form a non-decreasing sequenceN)rA   r+   rB   r(   kindr   r4   namer@   r:   r<   rH   r   r?   ndimrG   rC   prunennzr;   minr[   )rL   r2   
major_name
minor_namerS   rT   rP   xs           rU   rK   z_cs_matrix.check_format   s    "&,=!>
J#zz$**5	9 ;;!!S( F4;;,,112qB<<""c) F4<<--223C ))4;;*EF	jjI>zz$,,i@dii(	 ))..$,,"3"3T[[5E5EF 	LAAv !JKK	L
 	A-   &s4;;'7Q GI IKKNa@AA TYY/IJJKKOc$,,// A B B 	

xx!|<<##%2$ &((.z9(EG G<<##%)$ &((.z(:< <774;;'++-1$ &? @ @ 2  r_   c                     | j                          | j                   || j                  |      d      }|j                          |S )zScalar version of self._binopt, for cases in which no new nonzeros
        are added. Produces a new sparse array in canonical form.
        Tr0   )sum_duplicates
_with_datar?   eliminate_zeros)rL   rQ   opress       rU   _scalar_binoptz_cs_matrix._scalar_binopt   s>     	oobE2o>
r_   c                    t        |      rt        j                  |      r+| j                  | j                  t        j
                        S |dk(  rut        dt        d       | j                  t        j                  | j                  t        j
                              }| j                  |t        j                        }||z
  S | j                  |t        j                        S t        |      r| j                         |k(  S t        |      rt         S t#        |      rt        dt        d       | j                  |j                  k7  ry| j$                  |j$                  k7  r|j'                  | j$                        }| j)                  |d      }| j                  t        j                  | j                  t        j
                              }||z
  S y)	Nr'   r   zOComparing a sparse matrix with 0 using == is inefficient, try using != instead.r,   re   zHComparing sparse matrices using == is inefficient, try using != instead.F_ne_)r   r<   isnanrD   r+   bool_r   r   onesrv   operatorneeqr   todenser    NotImplementedr   r4   r5   _binopt)rL   rQ   all_trueinvru   s        rU   __eq__z_cs_matrix.__eq__   sY   xx~~djj~AAz /0G !#  >>"''$**BHH*MN))%=#~%**5(++>>U^<<>U**&!!e_  !8QH zzU[[(,t{{3,,uf-C~~bggdjj&IJHc>!r_   c                 R   t        |      rt        j                  |      rRt        dt        d       | j                  t        j                  | j                  t        j                              }|S |dk7  rut        dt        d       | j                  t        j                  | j                        t        j                        }| j                  |t        j                        }||z
  S | j                  |t        j                        S t        |      r| j                         |k7  S t        |      rt         S t#        |      r`| j                  |j                  k7  ry| j$                  |j$                  k7  r|j'                  | j$                        }| j)                  |d      S y)	Nz:Comparing a sparse matrix with nan using != is inefficientr,   re   r'   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Trx   )r   r<   ry   r   r   rD   r{   r+   rz   rv   r|   r~   r}   r   r   r    r   r   r4   r5   r   )rL   rQ   r   r   s       rU   __ne__z_cs_matrix.__ne__   s9   xx $%<L>>"''$**BHH*MN! >,<  >>"''$***=RXX>N))%=#~%**5(++>>U^<<>U**&!!e_zzU[[(,t{{3<<v..r_   c                    t        |      rd|k(  r|dv rt        d       |d|      rxt        |t               t	        j
                  | j                  t	        j                  |            }|j                  |       | j                  |      }| j                  ||      S | j                  ||      S t        |      r || j                         |      S t        |      r| j                  |j                  k7  rt        d      | j                   |j                   k7  r|j#                  | j                         }|dvr| j                  ||      S t        dt               | j                  t	        j$                  | j                  t        j&                              }| j                  ||dk(  rd	nd
      }||z
  S t        d      )Nr   )_le__ge_z >= and <= don't work with 0.r'   inconsistent shapes)r   r   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r   _gt__lt_zOperands could not be compared.)r   NotImplementedErrorr   r   r<   emptyr+   result_typefillrD   r   rv   r   r   r   rG   r4   r5   r{   rz   )rL   rQ   rt   op_namebad_scalar_msg	other_arrr   ru   s           rU   _inequalityz_cs_matrix._inequality  sn   Ezg)99)*IJJAu^%<=HHTZZr~~e7LM	u% NN95	||Iw77**5"55U^dllne,,e_zzU[[( !677,t{{3..||E733 /0GI~~bggdjj&IJH,,u60AfvNCc>!>??r_   c                 F    | j                  |t        j                  dd      S )Nr   zgComparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.)r   r|   ltrL   rQ   s     rU   __lt__z_cs_matrix.__lt__>  $    x{{F!89 	9r_   c                 F    | j                  |t        j                  dd      S )Nr   zdComparing a sparse matrix with a scalar less than zero using > is inefficient, try using <= instead.)r   r|   gtr   s     rU   __gt__z_cs_matrix.__gt__D  r   r_   c                 F    | j                  |t        j                  dd      S )Nr   zgComparing a sparse matrix with a scalar greater than zero using <= is inefficient, try using > instead.)r   r|   ler   s     rU   __le__z_cs_matrix.__le__J  $    x{{F!78 	8r_   c                 F    | j                  |t        j                  dd      S )Nr   zdComparing a sparse matrix with a scalar less than zero using >= is inefficient, try using < instead.)r   r|   ger   s     rU   __ge__z_cs_matrix.__ge__P  r   r_   c                 Z   |j                   | j                   k7  r/t        dj                  | j                   |j                               t        | j                  j
                  |j                  j
                        }| j                  d      d   }t        j                  |||d      }| j                  | j                         \  }}|j                  j                  r|n|j                  }t        ||| j                  | j                  | j                  |       | j!                  |d      S )NzIncompatible shapes ({} and {})CFr   T)r(   orderr/   Fr0   )r+   rG   r4   r   r(   charrA   r<   rF   flagsc_contiguousTr   rB   r@   r?   
_container)rL   rQ   r(   r   resultrN   rO   ys           rU   
_add_densez_cs_matrix._add_denseZ  s    ;;$**$>$fTZZ=? ?DJJOOU[[-=-=>

4 #%uEEzz$**%1ll//FVXXAq$++t||TYYBvE22r_   c                 &    | j                  |d      S )N_plus_r   r   s     rU   _add_sparsez_cs_matrix._add_sparsef  s    ||E8,,r_   c                 &    | j                  |d      S )N_minus_r   r   s     rU   _sub_sparsez_cs_matrix._sub_sparsei  s    ||E9--r_   c                 x   t        |      r| j                  |      S t        |      ro| j                  |j                  k(  r#| j	                  |      }| j                  |d      S |j                  dk(  r"| j                  |j                         d         S | j                  dk(  r"|j                  | j                         d         S | j                  d   dk(  r1|j                  d   dk(  r| j                  |j                               S | j                  d   dk(  r1|j                  d   dk(  r|j                  | j                               S |j                  d   dk(  r~| j                  d   |j                  d   k(  r_| j                  |j                         j                         dgf|j                  d   |j                  d   f      }| j                  |      S | j                  d   dk(  r~| j                  d   |j                  d   k(  r_| j                  | j                         j                         dgf| j                  d   | j                  d   f      }|j                  |      S |j                  d   dk(  r~| j                  d   |j                  d   k(  r_| j                  |j                         j                         dgf|j                  d   |j                  d   f      }|j                  |       S | j                  d   dk(  r~| j                  d   |j                  d   k(  r_| j                  | j                         j                         dgf| j                  d   | j                  d   f      }|j                  |      S t        d      t        j                  |      }|j                  dk7  r$t        j                  | j                         |      S |j                   dk(  r| j                  |j"                  d         S | j                  dk(  r't        j                  | j                         d   |      S | j%                         }| j                  |j                  k(  r;t        j                  |j&                  ||j(                  |j*                  f         }n| j                  d   dk(  rd|j                  d   dk(  r!t        j                  |j&                  |      }n\|j                  d   | j                  d   k(  r2t        j                  |j&                  |d	d	|j*                  f         }nt        d      t        j,                  t        j.                  |j                  d         t1        |j(                              }t        j2                  |j*                  |j                  d         }| j5                  |j7                  t        j8                        j                         ||ff|j                  d   | j                  d   fd
      S | j                  d   dk(  rn|j                  d   dk(  r(t        j                  |j&                  d	d	d	f   |      }n_|j                  d   | j                  d   k(  r5t        j                  |j&                  d	d	d	f   ||j(                           }nt        d      t        j,                  |j(                  |j                  d         }t        j2                  t        j.                  |j                  d         t1        |j*                              }| j5                  |j7                  t        j8                        j                         ||ff| j                  d   |j                  d   fd
      S |j                  d   dk(  r_| j                  d   |j                  d   k(  r@t        j                  |j&                  |d	d	|j*                  f   j                               }nx|j                  d   dk(  r[| j                  d   |j                  d   k(  r<t        j                  |j&                  ||j(                     j                               }nt        d      |j7                  t        j8                        j                         |_        |S )zPPoint-wise multiplication by another matrix, vector, or
        scalar.
        _elmul_)r   r   )r   r   r   r   r+   r   r)   NFr+   r/   )r   _mul_scalarr   r+   rD   r   toarray_mul_sparse_matrixtocsc_dia_containerravelrG   r<   
atleast_2dri   multiplysizeflattocoor?   rb   colrepeatarangerC   tilerE   viewndarray)rL   rQ   r/   retr?   rb   r   s          rU   r   z_cs_matrix.multiplyl  s   
 ##E**E?zzU[[(u-||E955&''(=>>v%(()=>>A!#A!(;..u{{}==A!#A!(;//

==Q1$A%++a.)H++]]_**,qc2 ;;q>5;;q>: ,  ..u55A!#

1Q(G**\\^))+aS1::a=$**Q-8 +  //55Q1$A%++a.)H++]]_**,qc2 ;;q>5;;q>: ,  //55A!#

1Q(G**\\^))+aS1::a=$**Q-8 +  ..u55 !677 e$::?;;t||~u55::?##EJJqM22ZZ6!;;t||~d3U;;jjl::$;;sxxsww/?)@ADZZ]a{{1~"{{388U3Q4::a=0{{388U1cgg:->? !677))BIIekk!n5s377|DC''#''5;;q>2C&&2::&,,.c
;{{1~tzz!}5 '   ZZ]a{{1~"{{388AtG#4e<Q4::a=0{{388AtG#4eCGGnE !677))CGGU[[^4C''"))EKKN3S\BC&&2::&,,.c
;zz!}ekk!n5 '   [[^q TZZ]ekk!n%D;;sxxq#''z):)@)@)BCD[[^q TZZ]ekk!n%D;;sxxsww)=)=)?@D23399RZZ(..0
r_   c           	      H   | j                   \  }}t        j                  |t        | j                  j
                  |j                  j
                              }t        t        | j                  dz         } |||| j                  | j                  | j                  ||       |S )Nr'   _matvec)r+   r<   r=   r   r(   r   getattrr   r4   rB   r@   r?   )rL   rQ   rN   rO   r   fns         rU   _mul_vectorz_cs_matrix._mul_vector  s}    zz1 !;tzz/4{{/?/?$A B \4;;#:;
1adllDIIufEr_   c                    | j                   \  }}|j                   d   }t        j                  ||ft        | j                  j
                  |j                  j
                              }t        t        | j                  dz         } ||||| j                  | j                  | j                  |j                         |j                                |S )Nr   r'   _matvecs)r+   r<   r=   r   r(   r   r   r   r4   rB   r@   r?   r   )rL   rQ   rN   rO   n_vecsr   r   s          rU   _mul_multivectorz_cs_matrix._mul_multivector  s    zz1Q1f+ +DJJOOU[[=M=M NP \4;;#;<
1adllDII;;=&,,.	* r_   c                 J   | j                   \  }}|j                   \  }}| j                  ||f      d   }| j                  |      }| j                  | j                  | j
                  |j                  |j
                  f      }t        t        | j                  dz         } |||t        j                  | j                  |      t        j                  | j
                  |      t        j                  |j                  |      t        j                  |j
                  |            }	| j                  | j                  | j
                  |j                  |j
                  f|	      }t        j                  |dz   |      }
t        j                  |	|      }t        j                  |	t        | j                  |j                              }t        t        | j                  dz         } |||t        j                  | j                  |      t        j                  | j
                  |      | j                  t        j                  |j                  |      t        j                  |j
                  |      |j                  |
||       | j                  |||
f||f      S )Nr   _matmat_maxnnzr'   r$   r   _matmatr   )r+   rA   rD   r:   rB   r@   r   r   r4   r<   rH   r   r   r(   r?   )rL   rQ   rN   K1K2rO   
major_axisrP   r   rk   rB   r@   r?   s                rU   r   z_cs_matrix._mul_sparse_matrix  s   

2AZZA'*
u%))4;;%*\\5==+B C	 \4;;1A#ABADKKy9DLL	:ELL	:EMM;	= ))4;;%*\\5==+B+. * 0	 *q.	:((3i0xx6$**ekk#BC\4;;#:;
1aDKKy9::dll)499::ell)4::emm95::7D	" ~~tWf5aV~DDr_   c           
         | j                   \  }}|| k  s||k\  r+t        j                  d| j                  j                        S t        t        | j                  dz         }t        j                  t        |t        |d      z   |t        |d      z
        t        | j                              } ||| j                   d   | j                   d   | j                  | j                  | j                  |       |S )Nr   r'   	_diagonalr   )r+   r<   r   r?   r(   r   r   r4   rl   r;   r   rB   r@   )rL   krowscolsr   r   s         rU   diagonalz_cs_matrix.diagonal  s    ZZ
d:d88ATYY__55\4;;#<=HHSAq	)4#a)+;<!$**-/
1djjmTZZ]DKK99a	r_   c                    t        |      r ||      rt        dt        d       t        j                  | j
                  t        j                  |      j                        }|j                  |       | j                  |      }| j                  ||      S | j                           || j                  t        j                  |            }| j                  || j                  | j                  f|j                  | j
                        }|S t        |      r || j!                         |      S t#        |      r| j                  ||      S t%        d      )NzITaking maximum (minimum) with > 0 (< 0) number results to a dense matrix.r,   re   r'   )r(   r+   zOperands not compatible.)r   r   r   r<   r   r+   rH   r(   r   rD   r   rq   r?   r@   rB   r   r   r   rG   )rL   rQ   npopr   dense_checkr   new_datamats           rU   _maximum_minimumz_cs_matrix._maximum_minimum/  s   5! +,C !# HHTZZrzz%7H7N7NO	u% NN95	||Iw77##%		2::e+<=nnhdkk%J+3>> % M
U^..e_<<w//788r_   c                 H    | j                  |t        j                  dd       S )N	_maximum_c                 2    t        j                  |       dkD  S Nr   r<   rH   ro   s    rU   <lambda>z$_cs_matrix.maximum.<locals>.<lambda>H      BJJqMA<M r_   )r   r<   maximumr   s     rU   r   z_cs_matrix.maximumF  %    $$UBJJ%02MO 	Or_   c                 H    | j                  |t        j                  dd       S )N	_minimum_c                 2    t        j                  |       dk  S r   r   r   s    rU   r   z$_cs_matrix.minimum.<locals>.<lambda>N  r   r_   )r   r<   minimumr   s     rU   r   z_cs_matrix.minimumL  r   r_   c                    t        | d      s|| j                  d      d   v rt        | j                        }t	        j
                  t        | j                        dz
  |      }| j                  t        j                        \  }}|||<   | j                  |      }|dz  dk(  r|j                  }|$|j                  |j                  k7  rt        d      |j                  d||	      S t        j                  | |||	      S )
z~Sum the matrix over the given axis.  If the axis is None, sum
        over both rows and columns, returning a scalar.
        	blocksize))r   rW   )r   r)   r   r   r'   r)   zdimensions do not match )r\   r(   out)hasattrrA   r   r(   r<   r=   rC   rB   _minor_reduceadd_ascontainerr   r+   rG   sumr   )rL   r\   r(   r   	res_dtyper   major_indexvalues           rU   r   z_cs_matrix.sumV  s     k*

#45a88%djj1I((3t{{+a/yAC!%!3!3BFF!;K$C##C(Cax1}ee399		#9 !:;;77%S799 ;;t$eEEr_   c                     || j                   }t        j                  t        j                  | j                              }|j                  |t        | j                  |               }||fS )a  Reduce nonzeros with a ufunc over the minor axis when non-empty

        Can be applied to a function of self.data by supplying data parameter.

        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?   r<   flatnonzeror[   rB   reduceatr   )rL   ufuncr?   r   r   s        rU   r   z_cs_matrix._minor_reduces  sZ     <99DnnRWWT[[%9:t24;;{3KLNE!!r_   c                    | j                  | j                        \  }}| j                  ||f      \  }}t        ||| j                  | j                  | j
                  ||dz   ||dz   	      \  }}}	|	j                  | j                        S )Nr   r'   )rA   r+   r   rB   r@   r?   r   r(   )
rL   rb   r   rN   rO   majorminorrB   r@   r?   s
             rU   _get_intXintz_cs_matrix._get_intXint  s~    zz$**%1zz3*-u 1q$++t||TYY519eUQY!0 xxdjjx))r_   c                     | j                  ||f      \  }}|j                  dv r"|j                  dv r| j                  ||d      S | j                  |      j	                  |      S )N)r   NTr0   )rA   step_get_submatrix_major_slice_minor_slicerL   rb   r   r  r  s        rU   _get_sliceXslicez_cs_matrix._get_sliceXslice  sc    zz3*-u::"uzzY'>&&ue$&??  '44U;;r_   c                    | j                   j                  }| j                  | j                        \  }}| j                  ||f      \  }}t	        j
                  ||      }t	        j
                  ||      }t	        j                  |j                  | j                        }t        ||| j                  | j                   | j                  |j                  |j                         |j                         |	       |j                  dk(  r| j                  |      S | j                  |j                  |j                              S )Nr'   r   )r@   r(   rA   r+   r<   rH   r   r   r   rB   r?   r   ri   r   rD   reshape)	rL   rb   r   rP   rN   rO   r  r  vals	            rU   _get_arrayXarrayz_cs_matrix._get_arrayXarray  s    LL&&	zz$**%1zz3*-u

5	2

5	2hhuzz4!QT\\499**ekkmU[[]C	I::?$$S))~~ckk%++677r_   c                 n    | j                  ||f      \  }}| j                  |      j                  |      S N)rA   _major_index_fancy_minor_index_fancyr  s        rU   _get_columnXarrayz_cs_matrix._get_columnXarray  s4    zz3*-u&&u-@@GGr_   c           	         | j                   j                  }t        j                  ||      j	                         }| j                  | j                        \  }}t        |      }| j                  ||f      }|dk(  r| j                  || j                        S | j                  |dz      | j                  |   z
  }| j                   j                  }t        j                  |dz   |      }	t        j                  ||	dd        |	d   }
t        j                  |
|      }t        j                  |
| j                        }t        ||| j                  | j                   | j                  ||       | j                  |||	f|d      S )	zBIndex along the major axis where idx is an array of ints.
        r'   r   r   Nr   rW   Fr   )r@   r(   r<   rH   r   rA   r+   rC   rD   rB   r=   cumsumr   r   r?   )rL   idxrP   r@   r]   rO   rN   	new_shaperow_nnz
res_indptrrk   res_indicesres_datas                rU   r  z_cs_matrix._major_index_fancy  sH    LL&&	**S	288:zz$**%1LJJ1v&	6>>)4::>>>++gk*T[[-AALL&&	XXac3

		'z!"~.nhhs)488Ctzz2a$++t||TYY!8	- ~~xjA$-E  ; 	;r_   c           
      D   |t        d      k(  r|r| j                         S | S | j                  | j                        \  }}|j	                  |      \  }}}t        t        |||            }| j                  ||f      }|dk(  r| j                  || j                        S ||}
}	|dk(  r|dk\  rd}
|dz   |dz   }}| j                  |||   | j                  |	|
|   z
  }| j                  j                  }t        j                  |dz   |      }t        j                  ||dd        |dk(  rot        | j                  |   | j                  |         }t        j                  | j                  |   |      }t        j                  | j                  |   |      }nm|d   }t        j                  ||      }t        j                  || j                        }t!        |||| j                  | j                  | j                  ||       | j                  |||f|d	      S )
z@Index along the major axis where idx is a slice object.
        Nr   r'   rW   r   r  r0   Fr   )slicer/   rA   r+   r@   rC   rangerD   r(   rB   r<   r=   r  rF   r?   r   r   )rL   r  r/   rN   rO   startstopr
  r  start0stop0start1stop1r  rP   r  all_idxr   r!  rk   s                       rU   r  z_cs_matrix._major_slice  s    %+"&499;0D0zz$**%1KKNtTeT4()JJ1v&	6>>)4::>>> t2:%1*E	4!8++fU4/0KKuT)*+LL&&	XXac3

		'z!"~.19DKK.D0ABG((4<<#8tDKxx		' 2>HR.C((3i8Kxx4::6H%tT[[$,,))[(< ~~xjA$-E  ; 	;r_   c           
      P   | j                   j                  }t        j                  ||      j	                         }| j                  | j                        \  }}t        |      }| j                  ||f      }|dk(  r| j                  || j                        S t        j                  ||      }t        j                  | j                        }t        ||||| j                  | j                   ||       t        j                  |      j                  |d      }	|d   }
t        j                  |
|      }t        j                  |
| j                        }t!        |	|t        | j                         | j                   | j"                  ||       | j                  |||f|d      S )zBIndex along the minor axis where idx is an array of ints.
        r'   r   Fr0   rW   r   )r@   r(   r<   rH   r   rA   r+   rC   rD   r=   
empty_likerB   r   argsortrJ   r   r   r?   )rL   r  rP   rN   rO   r   r  col_offsetsr  	col_orderrk   r   r!  s                rU   r  z_cs_matrix._minor_index_fancy  sY    LL&&	jjI.446zz$**%1HJJ1v&	6>>)4::>>> hhq	2]]4;;/
!S!QT\\%z	3 JJsO**95*A	nhhs)488Ctzz2)[#dll2C,,		;	J~~xjA$-E  ; 	;r_   c                    |t        d      k(  r|r| j                         S | S | j                  | j                        \  }}|j	                  |      \  }}}t        t        |||            }|dk(  r.| j                  | j                  ||f      | j                        S |dk(  r| j                  ||      S | j                  t        j                  |||            S )z@Index along the minor axis where idx is a slice object.
        Nr   r'   r   )r  r/   )r#  r/   rA   r+   r@   rC   r$  rD   r(   r  r  r<   r   )rL   r  r/   rN   rO   r%  r&  r
  s           rU   r  z_cs_matrix._minor_slice  s     %+"&499;0D0zz$**%1KKNtTeT4()6>>$**aV"4DJJ>GG19&&St&<<&&ryyd'CDDr_   c                    | j                  | j                        \  }}t        ||      \  }}t        ||      \  }}	|dk(  r#|dk(  r||k(  r|	|k(  r|r| j                         S | S t	        ||| j
                  | j                  | j                  ||||		      \  }
}}| j                  ||z
  |	|z
  f      }| j                  |||
f|| j                  d      S )zbReturn a submatrix of this matrix.

        major, minor: None, int, or slice with step 1
        r   F)r+   r(   r/   )
rA   r+   _process_slicer/   r   rB   r@   r?   rD   r(   )rL   r  r  r/   rN   rO   i0i1j0j1rB   r@   r?   r+   s                 rU   r  z_cs_matrix._get_submatrix  s    
 zz$**%1q)Bq)B7rQw27rQw"&499;0D0 1q$++t||TYYBB!H 

BGR"W-.~~tWf5U$(JJU  < 	<r_   c                 V    | j                  ||f      \  }}| j                  |||       y r  rA   	_set_manyrL   rb   r   ro   rd   js         rU   _set_intXintz_cs_matrix._set_intXint0  (    zz3*%1q!Qr_   c                 V    | j                  ||f      \  }}| j                  |||       y r  r9  r;  s         rU   _set_arrayXarrayz_cs_matrix._set_arrayXarray4  r>  r_   c                 R    | j                   | j                  ||f        |j                  \  }}|dk7  xr |j                  d   dk(  }|dk7  xr |j                  d   dk(  }|j                  |j                  }	}t        j                  |j                  | j                        }|j                  dk(  ry |r^t        j                  t        j                  |      t        |            }t        j                  |	|      }	t        j                  ||      }|r^t        j                  ||      }t        j                  t        j                  |      t        |	            }	t        j                  ||      }| j                  |||	f   |||	f   f      \  }
}| j                  |
||       y )Nr   r   r'   )
_zero_manyrA   r+   rb   r   r<   rH   r?   r(   r   r   r   rC   r   r:  )rL   rb   r   ro   rN   rO   broadcast_rowbroadcast_colrcrd   r<  s               rU   _set_arrayXarray_sparsez"_cs_matrix._set_arrayXarray_sparse8  sF   S#J/0yy1Q21771:?Q21771:?uuaee1JJqvvTZZ066Q;		"))A,A/A1A1A		!QA		!c!f-A		!QAzz3q!t9c!Q$i011q!Qr_   c                    d| j                   v ry | j                   \  }}|j                  dk(  }|dk  r|rt        ||z   |      }nt        ||z   |t        |            }t	        j
                  || j                  j                        }t	        j
                  || j                  j                        }||z  }n|rt        |||z
        }nt        |||z
  t        |            }t	        j
                  || j                  j                        }t	        j
                  || j                  j                        }||z  }|s|d t        |       }|| ||f<   y )Nr   r'   )r+   ri   rl   rC   r<   r   r@   r(   )	rL   valuesr   rN   rO   	broadcast	max_indexrd   r<  s	            rU   _setdiagz_cs_matrix._setdiagQ  s   

?zz1[[A%	q5AqM	Aq#f+6			)4<<+=+=>A		)4<<+=+=>AFA 1q5M	1q5#f+6			)4<<+=+=>A		)4<<+=+=>AFAGSV_FQT
r_   c                 `   | j                  | j                        \  }}d }t        j                  || j                  j
                  dd      j                         }t        j                  || j                  j
                  dd      j                         } |||        |||       ||||fS )Nc                     | j                         }||k\  rt        d||fz        | j                         }|| k  rt        d||fz        y )Nzindex (%d) out of range (>= %d)zindex (%d) out of range (< -%d))r;   
IndexErrorrl   )r@   boundr  s      rU   check_boundsz1_cs_matrix._prepare_indices.<locals>.check_boundsr  se    ++-Ce| !B"%u". / /++-CeV| !B"%u". / / r_   Fr   r(   r/   ndmin)rA   r+   r<   rF   r@   r(   r   )rL   rd   r<  rN   rO   rQ  s         rU   _prepare_indicesz_cs_matrix._prepare_indiceso  s    zz$**%1	/ HHQdll00uAFLLNHHQdll00uAFLLNQQ!Qzr_   c           
         | j                  ||      \  }}}}t        j                  || j                  dd      j	                         }|j
                  }t        j                  || j                  j                        }t        ||| j                  | j                  ||||      }|dk(  r6| j                          t        ||| j                  | j                  ||||       d|vr|| j                  |<   yt        dj                  | j                        t        d	       |dkD  }	||	   | j                  ||	   <   |	 }	||	   }||d
k  xx   |z  cc<   ||	   }||d
k  xx   |z  cc<   | j                  ||||	          y)zSets value at each (i, j) to x

        Here (i,j) index major and minor respectively, and must not contain
        duplicate entries.
        Fr   rR  r'   rW   NzZChanging the sparsity structure of a {}_matrix is expensive. lil_matrix is more efficient.r,   re   r   )rT  r<   rF   r(   r   r   r   r@   r   rB   rq   r?   r   r4   r   _insert_many)
rL   rd   r<  ro   rN   rO   	n_samplesoffsetsr   masks
             rU   r:  z_cs_matrix._set_many  sh    **1a0
1aHHQdjjuA>DDFFF	((9DLL,>,>? At{{DLL)!"Aw0!8!q!T[[$,,	 !W. W!"DIIg  228&2E(Q8 R<D'(wDIIgdm$5D$Aa!eHMH$Aa!eHMHaAdG,r_   c           
         | j                  ||      \  }}}}t        |      }t        j                  || j                  j
                        }t        ||| j                  | j                  ||||      }|dk(  r6| j                          t        ||| j                  | j                  ||||       d| j                  ||dkD     <   y)zSets value at each (i, j) to zero, preserving sparsity structure.

        Here (i,j) index major and minor respectively.
        r'   r   r   rW   N)
rT  rC   r<   r   r@   r(   r   rB   rq   r?   )rL   rd   r<  rN   rO   rW  rX  r   s           rU   rB  z_cs_matrix._zero_many  s    
 **1a0
1aF	((9DLL,>,>? At{{DLL)!"Aw0!8!q!T[[$,,	 !W. ,-		''B,'(r_   c           	         t        j                  |d      }|j                  |d      }|j                  |d      }|j                  |d      }| j                  }| j	                  | j
                  | j                  f| j                  d   |j                  z         }t        j                  | j                  |      | _        t        j                  | j
                  |      | _        t        j                  ||      }t        j                  ||      }g }g }t        j                  |d	      \  }	}
t        j                  |
t        |            }
t        j                  |
      }d
}t        t        |	|
|
dd             D ]  \  }\  }}}| j                  |   }| j                  |   }|j                  | j
                  ||        |j                  | j                  ||        t        j                  ||| ddd   d	      \  }}t        |      ||z
  k(  r)|j                  |||        |j                  |||        nH|j                  ||| ddd   |          |j                  ||| ddd   |          t        |      ||<   |} | j                     }|j                  | j
                  |d        |j                  | j                  |d        t        j                   |      | _        t        j                   |      | _        t        j"                  | j                  j$                  |      } |d
      |d
<   t        j                  | j                        }||	xx   |z  cc<   ||dd t        j&                  ||      | _        |rd| _        | j)                          | j+                  d       y)a:  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.
        	mergesort)rg   clip)moderW   r$   r'   T)return_indexr   r   Nr  Fr1   )r<   r.  takehas_sorted_indicesr:   r@   rB   r   rH   uniqueappendrC   r[   	enumeratezipr?   concatenater   r+   r  sort_indicesrK   )rL   rd   r<  ro   r   do_sortrP   indices_parts
data_partsui	ui_indptrnew_nnzsprevrF  iijsjer%  r&  uj	uj_indptrnnzsindptr_diffs                          rU   rV  z_cs_matrix._insert_many  s<    

1;/FF5vF&FF5vF&FF5vF&)) ))4<<*E,0KKOaff,D * G	jjI>zz$,,i@JJq	*JJq	* 
		!$7IIIiQ0	779%(RIabM)JK 	OA|BKK%E;;r?D  eD!9:diid34 IIa2httn4HMB	2w"r'!$$Qr"X.!!!Br(+$$Qr"Xdd^I%>?!!!Br(4R4.";<!"gD#	( BT\\%&12$))EF+, ~~m4NN:.	xx));A,Qggdkk*B8#QRii$/&+D#U+r_   c                    | j                  | j                        \  }}| j                  }t        j                  t        |      | j                  j                        }t        j                  || j                  |       | j                  ||f      \  }}| j                  | j                  ||ff| j                  || j                        S )Nr'   r.   )rA   r+   r@   r<   r   rC   r(   r   	expandptrrB   rE   r?   )rL   r/   rS   rT   minor_indicesmajor_indicesrb   r   s           rU   r   z_cs_matrix.tocoo	  s    #zz$**5	9]!34<<;M;MNy$++}E::}m<=S""YYc
#TZZd** # 
 	
r_   c                    ||| j                  d      d   }| j                  ||      }|j                  j                  s!|j                  j                  st        d      |j                  j                  r| j                         }|}n| j                         }|j                  }|j                  |j                        \  }}t        |||j                  |j                  |j                  |       |S )Ncfr   z&Output array must be C or F contiguous)rA   _process_toarray_argsr   r   f_contiguousrG   tocsrr   r   r+   r   rB   r@   r?   )rL   r   r   ro   r   rN   rO   s          rU   r   z_cs_matrix.toarray  s    ;5=JJt$Q'E((4		&&#))*@*@EFF99!!

AA

AAwwqww1Aq!((AIIqvvq9
r_   c                     | j                  | j                        \  }}t        j                  ||| j                  | j
                  | j                         | j                          y)zVRemove zero entries from the matrix

        This is an *in place* operation.
        N)rA   r+   r   csr_eliminate_zerosrB   r@   r?   rj   rL   rN   rO   s      rU   rs   z_cs_matrix.eliminate_zeros.  sH    
 zz$**%1((At{{DLL)-	4

r_   c                    t        | dd      sd| _        | j                  S t        | d      sOt        t	        j
                  t        | j                        dz
  | j                  | j                              | _	        | j                  S )a^  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.
        _has_sorted_indicesTF_has_canonical_formatr   )
r   r  r   boolr   csr_has_canonical_formatrC   rB   r@   has_canonical_formatrL   s    rU   __get_has_canonical_formatz%_cs_matrix.__get_has_canonical_format8  s|     t2D9).D&
 )))	 67(,55$q($++t||E)FD% )))r_   c                 8    t        |      | _        |rd| _        y y )NT)r  r  ra  rL   r  s     rU   __set_has_canonical_formatz%_cs_matrix.__set_has_canonical_formatN  s    %)#Y"&*D# r_   )fgetfsetc                    | j                   ry| j                          | j                  | j                        \  }}t	        j
                  ||| j                  | j                  | j                         | j                          d| _         y)zmEliminate duplicate matrix entries by adding them together

        This is an *in place* operation.
        NT)
r  rg  rA   r+   r   csr_sum_duplicatesrB   r@   r?   rj   r  s      rU   rq   z_cs_matrix.sum_duplicatesV  sj    
 $$zz$**%1''1dkk4<<(,			3 	

$(!r_   c                     t        | d      sOt        t        j                  t	        | j
                        dz
  | j
                  | j                              | _        | j                  S )zDetermine whether the matrix has sorted indices

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

        r  r   )r   r  r   csr_has_sorted_indicesrC   rB   r@   r  r  s    rU   __get_sortedz_cs_matrix.__get_sortedf  sW     t23'+33$q($++t||E(FD$ '''r_   c                 $    t        |      | _        y r  )r  r  r  s     rU   __set_sortedz_cs_matrix.__set_sortedv  s    #'9 r_   c                 F    | j                         }|j                          |S )z9Return a copy of this matrix with sorted indices
        )r/   rg  )rL   As     rU   sorted_indicesz_cs_matrix.sorted_indices{  s     IIK	r_   c                     | j                   sTt        j                  t        | j                        dz
  | j                  | j
                  | j                         d| _         yy)z3Sort the indices of this matrix *in place*
        r   TN)ra  r   csr_sort_indicesrC   rB   r@   r?   r  s    rU   rg  z_cs_matrix.sort_indices  sL     &&))#dkk*:Q*>*.,,		C&*D# 'r_   c                    | j                  | j                        d   }t        | j                        |dz   k7  rt	        d      t        | j
                        | j                  k  rt	        d      t        | j                        | j                  k  rt	        d      t        | j
                  d| j                         | _        t        | j                  d| j                         | _        y)z8Remove empty space after all non-zero elements.
        r   r   z index pointer has invalid lengthz)indices array has fewer than nnz elementsz&data array has fewer than nnz elementsN)	rA   r+   rC   rB   rG   r@   rk   r?   r   )rL   rS   s     rU   rj   z_cs_matrix.prune  s     JJtzz*1-	t{{y1},?@@t||txx'HIItyy>DHH$EFF#DLL$(($;< 9DHH!56	r_   c                    t        |      }t        | d      r| j                  \  }}t        |d   |      \  }}t        |d   |      \  }}|s|r%t	        dj                  | j                  |            | j                  d   |z  | j                  d   |z  }	}n2| j                  |      \  }}| j                  | j                        \  }}	||k  rZ| j                  d | j                  |    | _        | j                  d | j                  |    | _
        | j                  d |dz    | _	        n[||kD  rVt        j                  | j                  |dz         | _	        | j                  |dz   d  j                  | j                  |          ||	k  r| j                  |k  }
t        j                  |
      s| j                  |
   | _        | j                  |
   | _
        | j                  t        j                   |
      \  }}| j                  j                  d       || j                  dd  |<   t        j"                  | j                  | j                         || _        y )Nr   r   r   z.shape must be divisible into {} blocks. Got {}r  )r   r   r   divmodrG   r4   r+   rA   r@   rB   r?   r<   resizer   allr   r   r  r9   )rL   r+   bmbnnew_Mrmnew_NrnrN   rO   rY  r   r  s                rU   r  z_cs_matrix.resize  s   E"4%^^FBuQx,IE2uQx,IE2R  "**0&*GI I::a=B&

1(;qA::e,LE5::djj)DAq19<<(;U);<DL		"54;;u#56DI++juqy1DKQY))DKK;DKKKA$$T[[^419<<%'D66$<#||D1 IIdO	#'#5#5bffd#C S  #/2AB,		$++4;;7r_   c                 :   |r[| j                  || j                  j                         | j                  j                         f| j                  |j
                        S | j                  || j                  | j                  f| j                  |j
                        S )zReturns 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*   )rD   r@   r/   rB   r+   r(   )rL   r?   r/   s      rU   rr   z_cs_matrix._with_data  s    
 >>4):):)<#';;#3#3#5#7(,

(,

 " 4 4
 >>4t{{"C(,

$** " F Fr_   c                 N   | j                  |      }t        t        | j                  |z   | j                  z         }| j                  |j                  z   }| j                  | j                  | j                  |j                  |j                  f|      }t        j                  | j                  j                  |      }t        j                  ||      }g d}||v r&t        j                  |t        j                        }	n5t        j                  |t        | j                  |j                              }	 || j                  d   | j                  d   t        j                  | j                  |      t        j                  | j                  |      | j                  t        j                  |j                  |      t        j                  |j                  |      |j                  |||	       | j                  |	||f| j                        }
|
j!                          |
S )z5apply the binary operation fn to two sparse matrices.r$   r'   )rx   r   r   r   r   r   r   r   )rD   r   r   r4   rk   r:   rB   r@   r<   r   r+   rz   r   r(   rH   r?   rj   )rL   rQ   rt   r   maxnnzrP   rB   r@   bool_opsr?   r  s              rU   r   z_cs_matrix._binopt  s   u% \4;;#3dkk#ABEII%))4;;%*\\5==+B+1 * 3	 $++++9=((63;>88F"((3D88F&U[[*IJD
4::a=$**Q-::dkk3::dll)499::ell)4::emm95::7D	" NND'62$**NE		r_   c                 *   |j                   | j                   k7  rt        d      | j                  |d      }t        j                  |j
                  t        j                        rt        j                  | j                   | j
                        }|j                  t        j                         |j                         \  }}d|||f<   |j                         }|j                  ||j                  |j                  f<   | j                  |      }|S |}|S )z?
        Divide this matrix by a second sparse matrix.
        r   _eldiv_r'   r   )r+   rG   r   r<   
issubdtyper(   inexactr   r   nannonzeror   r?   rb   r   r   )rL   rQ   rE  r   rb   r   s         rU   _divide_sparsez_cs_matrix._divide_sparse  s     ;;$**$233LL	*=="**-
 ((4::TZZ8CHHRVV}}HCCSM	A !Cquu//#&C
 
 C
r_   )NNFr  )F)T)r   )NNN)NN)A__name__
__module____qualname____doc__r3   r^   r   r6   rK   rv   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r  r=  r@  rG  rL  rT  r:  rB  rV  r   r   rs   $_cs_matrix__get_has_canonical_format$_cs_matrix__set_has_canonical_formatpropertyr  rq   _cs_matrix__get_sorted_cs_matrix__set_sortedra  r  rg  rj   r  rr   r   r  r   r_   rU   r"   r"      s   LP,d3 oo--GO	/:@L"H D @D9988
3-.mf"EH	 ''//H9.O oo--GOO oo--GOF6 ++%%CK"4*<8H
;4&;P;8E <&   2<&%-N-(F,X

 MM))EM" oo--GO*,+
 $)C)CE) ( - "|,G+7 D ^^++FNF Dr_   r"   c                 ,   | d|}}||fS t        | t              r5| j                  |      \  }}}|dk7  rt        d      t	        ||      }||fS t        |       r0| dk  r| |z  } | | dz   }}|dk  s||kD  rt        d|||fz        ||fS t        d      )Nr   r   z$slicing with step != 1 not supportedz'index out of bounds: 0 <= %d < %d <= %dzexpected slice or scalar)r7   r#  r@   rG   rl   r   rO  	TypeError)slnumr4  r5  strides        rU   r3  r3    s    	zCB  r6M 
B	CBQ;CDDR[ r6M 
26#IBR!VB6R#XF "c]+ , ,
 r6M 233r_   )+r  __all__warningsr   r|   numpyr<   scipy._lib._utilr   _baser   r   r   _datar	   r
    r   r   r   r   r   r   r   r   r   _indexr   _sputilsr   r   r   r   r   r   r   r   r   r   r   r    r"   r3  r   r_   rU   <module>r     sh    D
    ) = = . A A A + + + +
z}j zz'r_   