
    9=e                         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*dS )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                      e Zd ZdZd@dZdAdZej        j        e_        dBdZdCd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dDdZej        j        e_        d Zd Zej        j        e_        d Zej        j        e_        dEdZej        j        e_        dAdZd Zd  Zd! Z d" Z!d# Z"dBd$Z#d% Z$dBd&Z%d@d'Z&d( Z'd) Z(d* Z)d+ Z*d, Z+d- Z,d. Z-d/ Z.dCd0Z/ej/        j        e/_        dFd1Z0ej0        j        e0_        d2 Z1d3 Z2d4 Z3 e4e2e35          Z5d6 Z6d7 Z7d8 Z8 e4e7e85          Z9d9 Z:d: Z;d; Z<d< Z=ej=        j        e=_        dCd=Z>d> Z?d? Z@dS )G
_cs_matrixzBbase matrix class for compressed row- and column-oriented matricesNFc                    t          j        |            t          |          rX|j        | j        k    r|r|                                }n|                    | j                  }|                     |           nt          |t                    rt          |          rt          |          | _        | j        \  }}|                     t          ||                    }t          j        dt#          |t$                              | _        t          j        d|          | _        t          j        |                     ||f          d         dz   |          | _        nt/          |          dk    rB|                     |                     |||                    }|                     |           nOt/          |          dk    r|\  }	}
}d }|t          |          }|                     |
|f|d	
          }t          j        |
||          | _        t          j        |||          | _        t          j        |	||          | _        nt7          d                    | j                            	 t          j        |          }n:# t:          $ r-}t7          d                    | j                            |d }~ww xY w|                     |                     |                     ||                               |t          |          | _        n| j        	 t/          | j                  dz
  }| j                                        dz   }t          |                     ||f                    | _        n"# t:          $ r}t7          d          |d }~ww xY w|!| j                            |d          | _        |                     d           d S )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                   8lib/python3.11/site-packages/scipy/sparse/_compressed.pyr3   z_cs_matrix.__init__   s    d###D>> 9	{dk))d)yy{{}}T[11NN4    e$$ 2	t}} &N *$//z1 !11Q1CC	HQ(F(F(FGG	!x955 htzz1a&'9'9!'<q'@-68 8 8 t99>> NN++DU+KK E NN5))))YY!^^.2+T7F "F(!$U $ 5 5w6G7=?C !6 !E !EI $&8G$2;$= $= $=DL"$(6I"N"N"NDK "D F F FDII$ &99?9L9LN N N
@z$'' @ @ @  "$$*F4;$7$79 9>?@@ NN4>>##D#66    
 %e,,DKKz!G #DK 0 01 4I $ 0 0 2 2Q 6I #.djj):C:E /F /F #G #GDKK ! Q Q Q$%HIIqPQ 	((U(;;DIU+++++s0   >J 
K
(KK
)3N 
N%N  N%c                 |   |t          | j        d                   S |dk     r|dz  }|                     |d|z
  f          \  }}|                     | 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'''axx	jj$D!122GD!::dj))DAqqyy{#6t|#D#D-.0 0 0 0wt{+++1222    c                     |r|                                 }|j        | _        |j        | _        |j        | _        t	          |j                  | _        dS )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|   sI      	!JJLLEJ	}l!%+..r_   Tc                 z   |                      d          \  }}|                      | j                  \  }}| j        j        j        dk    r3t          d                    | j        j        j                  d           | j        j        j        dk    r3t          d                    | j        j        j                  d           | 	                    | j        | j        f          }t          j        | j        |          | _        t          j        | j        |          | _        t          | j                  | _        | j        j        | j        j        | j        j        fD ]}|dk    rt          d	          t!          | j                  |dz   k    r8t          d
                    t!          | j                  |dz                       | j        d         dk    rt          d          t!          | j                  t!          | j                  k    rt          d          | j        d         t!          | j                  k    rt          d          |                                  |r| j        dk    r| j                                        |k    r#t          d                    ||                    | j                                        dk     r"t          d                    |                    t          j        | j                                                  dk     rt          d          dS dS dS )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5	9 ;!S(( F4;,122qB B B B<"c)) F4<-233C C C C ))4;*EFF	jI>>>z$,i@@@di((	 ).$,"3T[5EF 	L 	LAAvv !JKKK  	A--   &s4;'7'7Q G GI I IKNa@AAA TY//IJJJKOc$,//// A B B B 	

 	@x!||<##%%22$ &((.z9(E(EG G G<##%%))$ &((.z(:(:< < <74;''++--11$ &? @ @ @	@ 	@| 21r_   c                     |                                   |                      || j        |          d          }|                                 |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   sP     	oobbE22o>>
r_   c                    t          |          rt          j        |          r&|                     | j        t          j                  S |dk    rtt          dt          d           |                     t          j        | j        t          j                            }| 	                    |t          j                  }||z
  S | 	                    |t          j                  S t          |          r|                                 |k    S t          |          rt           S t#          |          rt          dt          d           | j        |j        k    rdS | j        |j        k    r|                    | j                  }|                     |d          }|                     t          j        | j        t          j                            }||z
  S dS )	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__   s     	x B~~dj~AAAzz /0G !# # # #  >>"'$*BH*M*M*MNN))%==#~%**5(+>>>U^^ 	<<>>U**&& 	!!e__ 	  !8QH H H H zU[((u,,t{33,,uf--C~~bgdj&I&I&IJJHc>!5r_   c                 ^   t          |          rt          j        |          rQt          dt          d           |                     t          j        | j        t          j                            }|S |dk    rtt          dt          d           |                     t          j        | j                  t          j                  }| 	                    |t          j                  }||z
  S | 	                    |t          j                  S t          |          r|                                 |k    S t          |          rt           S t#          |          rR| j        |j        k    rdS | j        |j        k    r|                    | j                  }|                     |d          S dS )	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__   s    	x ? $%<L L L L>>"'$*BH*M*M*MNN! >,< < < <  >>"'$**=*=RX>NN))%==#~%**5(+>>>U^^ 	<<>>U**&& 	!!e__ 	zU[((t,,t{33<<v...4r_   c                    t          |          rd|k    r|dv rt          d           |d|          rt          |t                     t	          j        | j        t	          j        |                    }|                    |           | 	                    |          }| 
                    ||          S |                     ||          S t          |          r ||                                 |          S t          |          r| j        |j        k    rt          d          | j        |j        k    r|                    | j                  }|dvr| 
                    ||          S t          dt                     | 	                    t	          j        | j        t          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  s    	@Ezzg)999)*IJJJAu 6^%<===HTZr~e7L7LMMM	u%%% NN955	||Iw777**5"555U^^ 	@2dllnne,,,e__ 	@zU[(( !6777,,t{33...||E7333 /0GI I I~~bgdj&I&I&IJJH,,u60A0AffvNNCc>!>???r_   c                 F    |                      |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 9 	9r_   c                 F    |                      |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    |                      |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 8 	8r_   c                 F    |                      |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                    |j         | j         k    r-t          d                    | j         |j                             t          | j        j        |j        j                  }|                     d          d         }t          j        |||d          }|                     | j                   \  }}|j	        j
        r|n|j        }t          ||| 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    ;$*$$>$fTZ==? ? ?DJOU[-=>>

4  #%uEEEEzz$*%%1l/=FFVXAq$+t|TYBBBvE222r_   c                 .    |                      |d          S )N_plus_r   r   s     rU   _add_sparsez_cs_matrix._add_sparsef  s    ||E8,,,r_   c                 .    |                      |d          S )N_minus_r   r   s     rU   _sub_sparsez_cs_matrix._sub_sparsei  s    ||E9---r_   c                 ~   t          |          r|                     |          S t          |          r| j        |j        k    r+|                     |          }|                     |d          S |j        dk    r-|                     |                                d                   S | j        dk    r-|                    |                                 d                   S | j        d         dk    r8|j        d         dk    r'|                     |                                          S | j        d         dk    r8|j        d         dk    r'|                    |                                           S |j        d         dk    r| j        d         |j        d         k    rk| 	                    |                                
                                dgf|j        d         |j        d         f          }|                     |          S | j        d         dk    r| j        d         |j        d         k    rk| 	                    |                                 
                                dgf| j        d         | j        d         f          }|                    |          S |j        d         dk    r| j        d         |j        d         k    rk| 	                    |                                
                                dgf|j        d         |j        d         f          }|                    |           S | j        d         dk    r| j        d         |j        d         k    rk| 	                    |                                 
                                dgf| j        d         | j        d         f          }|                    |          S t          d          t          j        |          }|j        dk    r't          j        |                                 |          S |j        dk    r |                     |j        d                   S | j        dk    r-t          j        |                                 d         |          S |                                 }| j        |j        k    r.t          j        |j        ||j        |j        f                   }n| j        d         dk    rM|j        d         dk    rt          j        |j        |          }nU|j        d         | j        d         k    r*t          j        |j        |d	d	|j        f                   }nt          d          t          j        t          j        |j        d                   t1          |j                            }t          j        |j        |j        d                   }|                     |                    t          j                  
                                ||ff|j        d         | j        d         fd
          S | j        d         dk    r]|j        d         dk    r%t          j        |j        d	d	d	f         |          }n[|j        d         | j        d         k    r0t          j        |j        d	d	d	f         ||j                           }nt          d          t          j        |j        |j        d                   }t          j        t          j        |j        d                   t1          |j                            }|                     |                    t          j                  
                                ||ff| j        d         |j        d         fd
          S |j        d         dk    rX| j        d         |j        d         k    r<t          j        |j        |d	d	|j        f         
                                          }nt|j        d         dk    rT| j        d         |j        d         k    r8t          j        |j        ||j                 
                                          }nt          d          |                    t          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  sb   
  	+##E***E?? +	8zU[((u--||E9555&&''(=>>>v%%(()=>>>A!##A!(;(;..u{{}}===A!##A!(;(;//

===Q1$$A%+a.)H)H++]]__**,,qc2 ;q>5;q>: ,   ..u555A!##
1Q(G(G**\\^^))++aS1:a=$*Q-8 +   //555Q1$$A%+a.)H)H++]]__**,,qc2 ;q>5;q>: ,   //555A!##
1Q(G(G**\\^^))++aS1:a=$*Q-8 +   ..u555 !6777 e$$:??;t||~~u555:??##EJqM222Z6!!;t||~~d3U;;;jjll:$$;sxsw/?)@AADDZ]a{1~""{38U33Q4:a=00{38U111cg:->?? !6777)BIek!n55s37||DDC'#'5;q>22C&&2:&&,,..c
;{1~tz!}5 '    Z]a{1~""{38AAAtG#4e<<Q4:a=00{38AAAtG#4eCGnEE !6777)CGU[^44C'")EKN33S\\BBC&&2:&&,,..c
;z!}ek!n5 '    [^q  TZ]ek!n%D%D;sxqqq#'z):)@)@)B)BCCDD[^q  TZ]ek!n%D%D;sxsw)=)=)?)?@@DD233399RZ((..00
r_   c           	         | 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    z1 !;tz/4{/?$A $A B B B \4;#:;;
1adlDIufEEEr_   c                 l   | j         \  }}|j         d         }t          j        ||ft          | j        j        |j        j                            }t          t          | j        dz             } ||||| 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    z1Q1f+ +DJOU[=M N NP P P \4;#;<<
1adlDI;;==&,,..	* 	* 	* r_   c                    | j         \  }}|j         \  }}|                     ||f          d         }|                     |          }|                     | 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        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        |
||           |                     |||
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 C	 \4;1A#ABBbADKy999DL	:::EL	:::EM;;;	= = ))4;%*\5=+B+. * 0 0	 *q.	:::(3i000x6$*ek#B#BCCC\4;#:;;
1aDKy999:dl)4449:el)444:em9555:7D	" 	" 	" ~~tWf5aV~DDDr_   r   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    Z
d::d8ATY_5555\4;#<==HSAq		)4#a))+;<<!$*--/ / /
1djmTZ]DK9a	 	 	r_   c                    t          |          r ||          rt          dt          d           t          j        | j        t          j        |          j                  }|                    |           | 	                    |          }| 
                    ||          S |                                   || j        t          j        |                    }| 	                    || j        | j        f|j        | j                  }|S t          |          r ||                                 |          S t#          |          r| 
                    ||          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_    	9{5!!  +,C !# # # # HTZrz%7H7H7NOOO	u%%% NN955	||Iw777##%%%4	2:e+<+<==nnhdk%J+3> % M M
U^^ 	94...e__ 	9<<w///7888r_   c                 H    |                      |t          j        dd           S )N	_maximum_c                 2    t          j        |           dk    S Nr   r<   rH   ro   s    rU   <lambda>z$_cs_matrix.maximum.<locals>.<lambda>H      BJqMMA<M r_   )r   r<   maximumr   s     rU   r   z_cs_matrix.maximumF  ,    $$UBJ%02M2MO O 	Or_   c                 H    |                      |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||                     d          d         v rt          | j                  }t	          j        t          | j                  dz
  |          }|                     t          j	                  \  }}|||<   | 
                    |          }|dz  dk    r|j        }||j        |j        k    rt          d          |                    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)   Nz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** 	F

#455a888%dj11I(3t{++a/yAAAC!%!3!3BF!;!;K$C##C((Cax1}}e39	#9#9 !:;;;77%S7999 ;t$eEEEEr_   c                     || j         }t          j        t          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  s_     <9DnRWT[%9%9::t24;{3KLLN NE!!r_   c                    |                      | j                  \  }}|                      ||f          \  }}t          ||| j        | j        | j        ||dz   ||dz   	  	        \  }}}	|	                    | 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|TY519eUQY!0 !0 xxdjx)))r_   c                     |                      ||f          \  }}|j        dv r!|j        dv r|                     ||d          S |                     |                              |          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  sq    zz3*--u:""uzY'>'>&&ue$&???  ''44U;;;r_   c                 X   | j         j        }|                     | j                  \  }}|                     ||f          \  }}t	          j        ||          }t	          j        ||          }t	          j        |j        | j                  }t          ||| j	        | j         | j
        |j        |                                |                                |	  	         |j        dk    r|                     |          S |                     |                    |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    L&	zz$*%%1zz3*--u
5	222
5	222huz444!QT\49*ekkmmU[[]]C	I 	I 	I:??$$S)))~~ckk%+66777r_   c                     |                      ||f          \  }}|                     |                              |          S N)rA   _major_index_fancy_minor_index_fancyr  s        rU   _get_columnXarrayz_cs_matrix._get_columnXarray  s=    zz3*--u&&u--@@GGGr_   c           	         | j         j        }t          j        ||                                          }|                     | j                  \  }}t          |          }|                     ||f          }|dk    r|                     || 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        ||           |                     |||	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  sf    L&	*S	22288::zz$*%%1LLJJ1v&&	66>>)4:>>>>+gk*T[-AAL&	Xac333

	'z!""~....nhs)4448Ctz222a$+t|TY!8	- 	- 	- ~~xjA$-E  ; ; 	;r_   c           
      B   |t          d          k    r|r|                                 n| S |                     | j                  \  }}|                    |          \  }}}t          t          |||                    }|                     ||f          }|dk    r|                     || 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    rit          | j	        |         | j	        |                   }t          j        | j        |         |          }t          j        | j        |         |          }n^|d         }t          j        ||          }t          j        || j                  }t!          |||| 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'    %++"&0499;;;D0zz$*%%1KKNNtTeT4(())JJ1v&&	66>>)4:>>>> t2::%1**E	4!8+fU4/0KuT)*+L&	Xac333

	'z!""~....199DK.D0ABBG(4<#8tDDDKx	' 2>>>HHR.C(3i888Kx4:666H%tT[$,)[(< < < ~~xjA$-E  ; ; 	;r_   c           
      .   | j         j        }t          j        ||                                          }|                     | j                  \  }}t          |          }|                     ||f          }|dk    r|                     || j                  S t          j	        ||          }t          j
        | j                  }t          ||||| j        | j         ||           t          j        |                              |d          }	|d         }
t          j        |
|          }t          j        |
| j                  }t!          |	|t          | 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  s    L&	jI...4466zz$*%%1HHJJ1v&&	66>>)4:>>>> hq	222]4;//
!S!QT\%z	3 	3 	3 JsOO**95*AA	nhs)4448Ctz222)[#dl2C2C,	;	J 	J 	J~~xjA$-E  ; ; 	;r_   c                    |t          d          k    r|r|                                 n| S |                     | j                  \  }}|                    |          \  }}}t          t          |||                    }|dk    r1|                     |                     ||f          | j                  S |dk    r| 	                    ||          S | 
                    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     %++"&0499;;;D0zz$*%%1KKNNtTeT4(())66>>$**aV"4"4DJ>GGG199&&St&<<<&&ryd'C'CDDDr_   c                    |                      | j                  \  }}t          ||          \  }}t          ||          \  }}	|dk    r*|dk    r$||k    r|	|k    r|r|                                 n| S t	          ||| j        | j        | j        ||||		  	        \  }
}}|                      ||z
  |	|z
  f          }|                     |||
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77rQww277rQww"&0499;;;D0 1q$+t|TYBB!H !H 

BGR"W-..~~tWf5U$(JU  < < 	<r_   c                 h    |                      ||f          \  }}|                     |||           d S r  rA   	_set_manyrL   rb   r   ro   rd   js         rU   _set_intXintz_cs_matrix._set_intXint0  7    zz3*%%1q!Qr_   c                 h    |                      ||f          \  }}|                     |||           d S r  r9  r;  s         rU   _set_arrayXarrayz_cs_matrix._set_arrayXarray4  r>  r_   c                 ,    | j         |                     ||f            |j        \  }}|dk    o|j        d         dk    }|dk    o|j        d         dk    }|j        |j        }	}t          j        |j        | j                  }|j	        dk    rd S |r^t          j
        t          j        |          t          |                    }t          j        |	|          }	t          j        ||          }|r^t          j
        ||          }t          j        t          j        |          t          |	                    }	t          j
        ||          }|                     |||	f         |||	f         f          \  }
}|                     |
||           d S )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  sa   S#J//00y1Q2171:?Q2171:?uae1JqvTZ0006Q;;F 		")A,,A//A1A1A 	 	!QA	!c!ff--A	!QAzz3q!t9c!Q$i0111q!Qr_   c                    d| j         v rd S | 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<   d S )Nr   r'   )r+   ri   rl   rC   r<   r   r@   r(   )	rL   valuesr   rN   rO   	broadcast	max_indexrd   r<  s	            rU   _setdiagz_cs_matrix._setdiagQ  sD   
??Fz1[A%	q55 7AqMM		Aq#f++66		)4<+=>>>A	)4<+=>>>AFAA  71q5MM		1q5#f++66		)4<+=>>>A	)4<+=>>>AFA 	%GSVVG_FQT


r_   c                 N   |                      | j                  \  }}d }t          j        || j        j        dd                                          }t          j        || j        j        dd                                          } |||            |||           ||||fS )Nc                     |                                  }||k    rt          d||fz            |                                 }|| k     rt          d||fz            d S )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  sy    ++--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	/ 	/ 	/ HQdl0uAFFFLLNNHQdl0uAFFFLLNNQQ!Qzr_   c           
         |                      ||          \  }}}}t          j        || j        dd                                          }|j        }t          j        || j        j                  }t          ||| j	        | j        ||||          }|dk    r4| 
                                 t          ||| j	        | j        ||||           d|vr|| j        |<   dS t          d                    | j                  t          d	           |dk    }	||	         | j        ||	         <   |	 }	||	         }||d
k     xx         |z  cc<   ||	         }||d
k     xx         |z  cc<   |                     ||||	                    dS )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  s    **1a00
1aHQdjuA>>>DDFFF	(9DL,>??? At{DL)!"Aw0 0!88!!!q!T[$,	 !W. . . W!"DIgF  228&2E2E(Q8 8 8 8 R<D'(wDIgdm$5D$Aa!eHHHMHHH$Aa!eHHHMHHHaAdG,,,,,r_   c           
      v   |                      ||          \  }}}}t          |          }t          j        || j        j                  }t          ||| j        | j        ||||          }|dk    r4|                                  t          ||| j        | j        ||||           d| j	        ||dk             <   dS )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0
1aFF	(9DL,>??? At{DL)!"Aw0 0!88!!!q!T[$,	 !W. . . ,-	''B,'(((r_   c           	         t          j        |d          }|                    |d          }|                    |d          }|                    |d          }| 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 ]O\  }\  }}}| j        |         }| j        |         }|
                    | j        ||                    |
                    | j        ||                    t          j	        |||         ddd         d	          \  }}t          |          ||z
  k    r;|
                    |||                    |
                    |||                    nj|
                    |||         ddd         |                    |
                    |||         ddd         |                    t          |          ||<   |}Q| j        |         }|
                    | j        |d                    |
                    | 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| _        |                                  |                     d           dS )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,0KOaf,D * G G	jI>>>z$,i@@@Jq	***Jq	*** 
	!$777IIiQ00	79%%(RIabbM)J)JKK 	 	OA|BK%E;r?D  eDj!9:::did
3444 Ia2htttn4HHHMB	2ww"r'!!$$Qr"uX...!!!BrE(++++$$Qr"uXddd^I%>???!!!BrE(44R4.";<<<!"ggDD BT\%&&1222$)EFF+,,, ~m44N:..	x);;;)A,,Qgdk**B8#QRRi$/// 	 &+D#U+++++r_   c                 f   |                      | j                  \  }}| j        }t          j        t          |          | j        j                  }t          j        || j	        |           |                      ||f          \  }}| 
                    | 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5	9]!3!34<;MNNNy$+}EEE::}m<==S""Yc
#TZd* # 
 
 	
r_   c                    |||                      d          d         }|                     ||          }|j        j        s|j        j        st          d          |j        j        r|                                 }|}n|                                 }|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4	& 	G#)*@ 	GEFFF9! 	

AAA

AAwwqw1Aq!(AIqvq999
r_   c                     |                      | j                  \  }}t          j        ||| j        | j        | j                   |                                  dS )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.  sR    
 zz$*%%1(At{DL)-	4 	4 	4

r_   c                     t          | dd          sd| _        nWt          | d          sGt          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9 	F).D&&677 	F(,5$$q($+t|E E)F )FD% ))r_   c                 D    t          |          | _        |r	d| _        d S d S )NT)r  r  ra  rL   r  s     rU   __set_has_canonical_formatz%_cs_matrix.__set_has_canonical_formatN  s0    %)#YY" 	+&*D###	+ 	+r_   )fgetfsetc                     | j         rdS |                                  |                     | j                  \  }}t	          j        ||| j        | j        | j                   | 	                                 d| _         dS )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  s    
 $ 	Fzz$*%%1'1dk4<(,		3 	3 	3 	

$(!!!r_   c                     t          | d          sGt          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  sc     t233 	F'+3$$q($+t|E E(F (FD$ ''r_   c                 .    t          |          | _        d S r  )r  r  r  s     rU   __set_sortedz_cs_matrix.__set_sortedv  s    #'99   r_   c                 V    |                                  }|                                 |S )z9Return a copy of this matrix with sorted indices
        )r/   rg  )rL   As     rU   sorted_indicesz_cs_matrix.sorted_indices{  s&     IIKK	r_   c                     | j         sDt          j        t          | j                  dz
  | j        | j        | j                   d| _         dS dS )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  s]     & 	+)#dk*:*:Q*>*.,	C C C&*D###	+ 	+r_   c                    |                      | j                  d         }t          | j                  |dz   k    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                           | _        dS )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     JJtz**1-	t{y1},,?@@@t|tx''HIIIty>>DH$$EFFF#DL$($;<< 9DH9!566			r_   c                    t          |          }t          | d          r| j        \  }}t          |d         |          \  }}t          |d         |          \  }}|s|r(t	          d                    | j        |                    | j        d         |z  | j        d         |z  }	}n5|                     |          \  }}|                     | j                  \  }}	||k     rV| j        d | j	        |                  | _        | j
        d | j	        |                  | _
        | j	        d |dz            | _	        nX||k    rRt          j        | j	        |dz             | _	        | j	        |dz   d                              | j	        |                    ||	k     r| j        |k     }
t          j        |
          s| j        |
         | _        | j
        |
         | _
        |                     t          j        |
          \  }}| j	                            d           || j	        dd          |<   t          j        | j	        | j	                   || _        d S )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 IR I  "**0&*G*GI I I:a=B&
1(;qAA::e,,LE5::dj))DAq199<(;U);(;<DL	"54;u#5"56DI+juqyj1DKKQYY)DK;;DKKA$$T[^444199<%'D6$<< 8#|D1 IdO	#'#5#5bfd#C#C S  ###/2ABB,	$+4;7777r_   c                 
   |rS|                      || j                                        | j                                        f| j        |j                  S |                      || 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    
  	F>>4):):)<)<#';#3#3#5#5#7(,
(,
 " 4 4 4
 >>4t{"C(,
$* " F F Fr_   c                    |                      |          }t          t          | j        |z   | j        z             }| j        |j        z   }|                     | j        | j        |j        |j        f|          }t          j	        | j        j
        |          }t          j	        ||          }g d}||v r!t          j	        |t          j                  }	n.t          j	        |t          | j        |j                            }	 || j
        d         | j
        d         t          j        | j        |          t          j        | j        |          | j        t          j        |j        |          t          j        |j        |          |j        |||	           |                      |	||f| 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;#3dk#ABBEI%))4;%*\5=+B+1 * 3 3	 $++9===(6333;;;>>8F"(333DD8F&U[*I*IJJJD
4:a=$*Q-:dk333:dl)4449:el)444:em9555:7D	" 	" 	" NND'62$*NEE				r_   c                    |j         | j         k    rt          d          |                     |d          }t          j        |j        t          j                  rt          j        | j         | j                  }|                    t          j	                   |
                                \  }}d|||f<   |                                }|j        ||j        |j        f<   |                     |          }n|}|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3LL	**="*-- 	
 (4:TZ888CHHRV}}HCCSM		A !Cqu//#&&CC 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LP, P, P, P,d3 3 3 3 o-GO	/ 	/ 	/ 	/:@ :@ :@ :@L  " " "H     D @  @  @D9 9 99 9 98 8 88 8 8
3 
3 
3- - -. . .m m mf    "E "E "EH	 	 	 	 '/H9 9 9.O O O o-GOO O O o-GOF F F F6 +%CK" " " "4* * *< < <8 8 8H H H
; ; ;4&; &; &; &;P; ; ;8E E E E < < < <&               2  <  &%- %- %-N- - -(F, F, F,X

 

 

 

 M)EM   " o-GO  * * *,+ + +
 $8)C)CE E E) ) ) ( ( ( - - - "|,GGG  + + +7 7 7     D ^+FNF F F F     D    r_   r"   c                 d   | d|}}nt          | t                    r?|                     |          \  }}}|dk    rt          d          t	          ||          }nRt          |           r4| dk     r| |z  } | | dz   }}|dk     s||k    rt          d|||fz            nt          d          ||fS )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	B		 4CBQ;;CDDDR[[	2 466#IBR!VB66R#XXF "c]+ , , ,  2333r6Mr_   )+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     s   D D
            ) ) ) ) ) ) = = = = = = = = = = . . . . . . . .      A A A A A A A A A A A A A A A A A A A A      + + + + + + + + + + + + + + + + + + + + + + + + + + + +
z z z z z}j z z zz'    r_   