
    Ug-                        d Z g ZddlmZ ddlZddlZddlmZm	Z	 ddl
mZ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dZ,d Z-dS )z>Base class for sparse matrix formats using compressed storage.    )warnN)_prune_arraycopy_if_needed   )_spbaseissparsesparray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dCdZdDdZej        j        e_        dEd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dFdZej        j        e_        d Zd Zej        j        e_        d Zej        j        e_        dGdZej        j        e_        dDdZd Zd Zd  Zd! Z d" Z!d# Z"dHd$Z#d% Z$dHd&Z%dCd'Z&d( Z'd) Z(d* Z)d+ Z*d, Z+d- Z,d. Z-d/ Z.d0 Z/d1 Z0dEd2Z1ej1        j        e1_        dId3Z2ej2        j        e2_        d4 Z3e4d5e5fd6            Z6e6j7        d7e5fd8            Z6d9 Z8e4d5e5fd:            Z9e9j7        d7e5fd;            Z9d< Z:d= Z;d> Z<d? Z=ej=        j        e=_        dEd@Z>dA Z?dB Z@dS )J
_cs_matrixzY
    base array/matrix class for compressed row- and column-oriented arrays/matrices
    NFc                 	   t          j        | |           t          | t                    }t	          |          rv|j        | j        k    r|r|                                }n|                    | j                  }|j        |j	        |j
        |j        f\  | _        | _	        | _
        | _        nt          |t                    rt          ||          rt          ||          | _        |                     | j                  \  }}|                     t%          | j                            }t)          j        dt-          |t.                              | _
        t)          j        d|          | _	        t)          j        |dz   |          | _        nt1          |          dk    rc|                     |||          }	|	                    | j                  }
|
\  | _        | _	        | _
        | _        |                                  nt1          |          d	k    r|\  }}}d }|d|vrt%          |          }|                     ||f|d
          }|st8          }t)          j        |||          | _	        t)          j        |||          | _        t)          j        |||          | _
        nt=          d| j        j          d|           	 t)          j!        |          }n2# tD          $ r%}t=          d| j        j          d|           |d }~ww xY wt          | t                    r.|j#        dk     r#| j        dk    rt=          d|j#         d          |                     ||          }	|	                    | j                  }
|
\  | _        | _	        | _
        | _        |t          ||          | _        n| j        	 t1          | j                  dz
  }| j	                                        dz   }n"# tD          $ r}t=          d          |d }~ww xY wt          |                     ||f          |          | _        |!| j
        $                    |d          | _
        | %                    d           d S )Nallow_1dmaxvalr   )defaultr   dtype   shaper,      T)r)   check_contentscopyr,   zunrecognized z constructor input: csczCSC arrays don't support zD input. Use 2Dz!unable to infer matrix dimensionsFr3   
full_check)&r   __init__
isinstancer	   r   formatr3   asformatindptrindicesdata_shapetupler   r!   _swap_shape_as_2d_get_index_dtypemaxr/   npzerosr   floatlen_coo_container_coo_to_compressedsum_duplicatesr   array
ValueError	__class____name__asarray	Exceptionndimastypecheck_format)selfarg1r/   r,   r3   is_arrayMN	idx_dtypecooarraysr>   r=   r<   r)   emajor_dminor_ds                     W/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/scipy/sparse/_compressed.pyr8   z_cs_matrix.__init__   s   dD)))dG,,D>> ?	G{dk))d)yy{{}}T[11T\49dkA=DKty$++ e$$ 6	Gth/// &C *$BBBzz$"3441 !11TZ1II	HQ(F(F(FGG	!x955 hq1uI>>>t99>>--d%u-MMC 33DJ??FHNEDKty$+''))))YY!^^.2+T7F "F(Qe^^!$U $ 5 5w6G7=?C !6 !E !EI   .-#%8G$i#P#P#PDL"$(6I"N"N"NDK "D F F FDII$ &BT^5L &B &B;?&B &B C C C
Fz$'' F F F  ">1H "> ">7;"> "> ? ?DEFF $(( TY]]t{e?S?S J	JJJ   %%d%%88C++DJ77F@F=DKty$+ %eh???DKKZMdk**Q.,**,,q0 M M M !DEE1LM &djj'71C&D&DxXXXDK	((U(;;DIU+++++s0   K- -
L7 LL3P 
P"PP"c                    |t          | j        d                   S | j        dk    r-|dv rt          | j        d                   S t          d          |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   rb   zaxis out of boundsr   r-   )	minlength)intr<   rR   rM   rA   r/   rE   bincountr   r=   diff)rU   axis_rY   s       r`   _getnnzz_cs_matrix._getnnzr   s    <t{2'''Y!^^w4;r?+++1222axx	jj$D!122GD!::dj))DAqqyy{#6t|#D#D-.0 0 0 0wt{+++1222    Tc                    | j         j        j        dk    r$t          d| j         j        j         dd           | j        j        j        dk    r$t          d| j        j        j         dd           | j        j        | j        j        | j         j        fD ]}|dk    rt          d          | 	                    | j
                  \  }}t          | j                   |dz   k    r*t          d	t          | j                    d
|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          |                     | j         | j        f          }t!          j        | j         |          | _         t!          j        | j        |          | _        t)          | j                  | _        dS dS )a  Check whether the array/matrix respects the CSR or CSC format.

        Parameters
        ----------
        full_check : bool, optional
            If `True`, run rigorous check, scanning arrays for valid values.
            Note that activating those check might copy arrays for casting,
            modifying indices and index pointers' inplace.
            If `False`, run basic checks on attributes. O(1) operations.
            Default is `True`.
        iz$indptr array has non-integer dtype ()r0   
stacklevelz%indices array has non-integer dtype (r   z'data, indices, and indptr should be 1-Dzindex pointer size z should be r   z!index pointer should start with 0z*indices and data should have the same sizerb   zQLast value of index pointer should be less than the size of index and data arrayszindices must be < zindices must be >= 0z(indptr must be a non-decreasing sequencer+   N)r<   r,   kindr   namer=   r>   rR   rM   rA   rB   rH   prunennzrD   minrE   rf   rC   rP   r   )rU   r7   xrX   rY   rZ   s         r`   rT   z_cs_matrix.check_format   s    ;!S((Q8I8NQQQ   <"c))S9K9PSSS    ).$,"3T[5EF 	L 	LAAvv !JKKK  zz$+,,1A%%W3t{3C3CWWPQTUPUWWXXXKNa@AAA TY//IJJJKOc$,//// A B B B 	

 	-x!||<##%%**$%=!%=%=>>><##%%))$%;<<<74;''++--11$%OPPP--t{DL.IJJI*T[	BBBDK:dl)DDDDL!$),,DIII	- 	-rj   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.
        Tr5   )rK   
_with_datar>   eliminate_zeros)rU   otheropress       r`   _scalar_binoptz_cs_matrix._scalar_binopt   sP     	oobbE22o>>
rj   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 t           S )	Nr+   r   zOComparing a sparse matrix with 0 using == is inefficient, try using != instead.r0   rn   zHComparing sparse matrices using == is inefficient, try using != instead.F_ne_)r   rE   isnanrN   r/   bool_r   r
   onesr|   operatorneeqr   todenser"   NotImplementedr   r:   r;   _binopt)rU   ry   all_trueinvr{   s        r`   __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>!!!rj   c                 h   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 t           S )	Nz:Comparing a sparse matrix with nan using != is inefficientr0   rn   r+   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Tr~   )r   rE   r   r   r
   rN   r   r/   r   r|   r   r   r   r   r   r"   r   r   r:   r;   r   )rU   ry   r   r   s       r`   __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...!!rj   c                    t          |          rd|k    r|dv rt          d           |d|          rt          |t          d           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          d           | 	                    t	          j        | j        t          j                            }| 
                    ||d
k    rdnd          }||z
  S t(          S )Nr   )_le__ge_z >= and <= don't work with 0.r0   rn   r+   inconsistent shapes)r   r   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r   _gt__lt_)r   NotImplementedErrorr   r
   rE   emptyr/   result_typefillrN   r   r|   r   r   r   rM   r:   r;   r   r   r   )rU   ry   rz   op_namebad_scalar_msg	other_arrr   r{   s           r`   _inequalityz_cs_matrix._inequality  s    	"Ezzg)999)*IJJJAu 6^%<KKKK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 /(Q8 8 8 8 ~~bgdj&I&I&IJJH,,u60A0AffvNNCc>!!!rj   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rU   ry   s     r`   __lt__z_cs_matrix.__lt__<  '    x{F!89 9 	9rj   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     r`   __gt__z_cs_matrix.__gt__B  r   rj   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     r`   __le__z_cs_matrix.__le__H  '    x{F!78 8 	8rj   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     r`   __ge__z_cs_matrix.__ge__N  r   rj   c                    |j         | j         k    r t          d| j          d|j          d          t          | j        j        |j        j                  }|                     d          d         }t          j        |||d          }|j        j	        r|n|j
        }|                     | j                  \  }}t          ||| j        | j        | j        |           |                     |d	          S )
NzIncompatible shapes (z and rm   CFr   T)r,   orderr3   Fr5   )r/   rM   r   r,   charrA   rE   rL   flagsc_contiguousTrB   r   r<   r=   r>   
_container)rU   ry   r,   r   resultyrX   rY   s           r`   
_add_densez_cs_matrix._add_denseX  s    ;$*$$TTZTTekTTTUUUDJOU[-=>>

4  #%uEEEEl/=FFVXzz$+,,1Aq$+t|TYBBBvE222rj   c                 .    |                      |d          S )N_plus_r   r   s     r`   _add_sparsez_cs_matrix._add_sparsec  s    ||E8,,,rj   c                 .    |                      |d          S )N_minus_r   r   s     r`   _sub_sparsez_cs_matrix._sub_sparsef  s    ||E9---rj   c                 <   t          |          r|                     |          S t          |          rM| j        |j        k    r+|                     |          }|                     |d          S |j        dk    r\|                     |                                d                   }| j        dk    r"|                    d| j        d         f          S |S |j        dk    r-|                     |                                d                   S | j        dv r,|                    | j	        
                                          S | j        \  }}|j        \  }}|dk    rA|dk    r;|                    |                     ||                                                    S |dk    rA|dk    r;|                     |                    ||                                                    S t          | t                    }|dk    rs||k    rmt!          |                                                                |          }|                     |          }| j        dk    r|n|                    d|f          S |dk    rO||k    rIt!          |                                                                 |          }	|                    |	          S |dk    rO||k    rIt!          |                                                                |          }|                    |           S |dk    rO||k    rIt!          |                                                                 |          }
|
                    |          S t%          d	          t'          j        |          }|j        dk    r't'          j        |                                 |          S |j        dk    rn|j        t&          j        k    rt2          S t'          j        | j        |j                  }|                     |j        d                                       |          S | j        dv r^t'          j        | j        |j                  }t'          j        | j	        
                                |                              |          S |                                 }| j        |j        k    r]t'          j        |j	        ||j                           }|                    t&          j                                                  |_	        |S t'          j         |          }| j        d         dk    s| j        dk    r@|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                   |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                   tQ          |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 )z=Point-wise multiplication by array/matrix, vector, or scalar._elmul_r   r   )r   r   r   r   r   )r   r   r-   r   rb   NFr/   r3   ))r   _mul_scalarr   r/   rN   r   toarrayrR   reshaper>   sumrB   _matmul_sparsetocscr9   r	   _make_diagonal_csrravelrM   rE   
asanyarraymultiplysizer,   object_r   broadcast_shapesflattocoocoordsviewndarray
atleast_2dcolrepeatarangers   tilerI   rowrH   )rU   ry   r   sMsNoMoNrW   	new_otherr3   new_selfbshaperetr>   other2dr   r   s                    r`   r   z_cs_matrix.multiplyi  s     	+##E***E?? +	4zU[((u--||E9555{f$$))%--//$*?@@9>>!>>1djm*<==={d""''(:;;;z^++((999 &FB'FBQww277++DLLR,@,@,F,F,H,HIIIQww277**5==R+@+@+F+F+H+HIII!$00HQww288.u}}/D/D/F/FQQ	,,Y77!%avvV^^QG5L5LLQww288)$,,..*>*>*@*@(KK++D111 Qww288.u}}/D/D/F/FQQ	 //555Qww288-dllnn.B.B.D.DhOO..u5552333 e$$:>>;t||~~u555:??{bj((%%(U[AAF##EJqM22::6BBB:''(U[AAF;ty}}66>>vFFFjjll:$$;sxsz):;;Dyy,,2244CHJ -&&:a=Aa}Q1$${38W55q!TZ^33{38WQQQZ-@AA !6777)BIgmA&677AAC'#'7=#344C&&2:&&,,..c
;}Q'B8 '    :a=A}Q1$${38AAAtG#4g>>q!TZ]22{38AAAtG#4gcg6FGG !6777)CGW]1%566C'")GM!$455s37||DDC&&2:&&,,..c
;z!}gmA&67 '    =q  TZ]gmA6F%F%F;sxCG)<)B)B)D)DEEDD]1""tz!}a8H'H'H;sx)9)?)?)A)ABBDD233399RZ((..00
rj   c           	      *   | j         \  }}t          j        |t          | j        j        |j        j                            }t          t          | j        dz             } |||| j	        | j
        | j        ||           | j        dk    r|d         n|S )Nr+   _matvecr   r   )rB   rE   rF   r   r,   r   getattrr   r:   r<   r=   r>   rR   )rU   ry   rX   rY   r   fns         r`   _matmul_vectorz_cs_matrix._matmul_vector  s     1 !;tz@P#Q#QRRR \4;#:;;
1adlDIufEEE INNvayy6rj   c                    | j         \  }}|j        d         }t          j        ||ft	          | j        j        |j        j                            }t          t          | j	        dz             } ||||| j
        | j        | j        |                                |                                           | j        dk    r|                    |f          S |S )Nrb   r+   _matvecsr   )rB   r/   rE   rF   r   r,   r   r   r   r:   r<   r=   r>   r   rR   r   )rU   ry   rX   rY   n_vecsr   r   s          r`   _matmul_multivectorz_cs_matrix._matmul_multivector  s     1R1f+ +DJOU[=M N NP P P \4;#;<<
1adlDI;;==&,,..	* 	* 	* 9>>>>6),,,rj   c                 N   | j         \  }}|j        }|dk    r'|                    d|j        d         f          j        }|j        \  }}d}| j        dk    r||fz  }|dk    r||fz  }|                     ||f          d         }|                     |          }|                     | j	        | j
        |j	        |j
        f          }	t          t          | j        dz             }
 |
||t          j        | j	        |	          t          j        | j
        |	          t          j        |j	        |	          t          j        |j
        |	                    }|dk    rc|dk    r.t          j        dt#          | j        |j                            S |                     |t#          | j        |j                            S |                     | 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        |||           |dk    rt          j        |d                   S |                     |||f|	          S )
Nr   r    r-   _matmat_maxnnzr+   r(   _matmatr/   )rB   rR   r   r/   r   r?   rA   rN   rC   r<   r=   r   r   r:   rE   rP   rL   r   r,   r   r>   )rU   ry   rX   K1o_ndimK2rY   	new_shape	major_dimrZ   r   rs   r<   r=   r>   s                  r`   r   z_cs_matrix._matmul_sparse   s   !2Q;;MM1ek!n"5668EA 	9>>!IQ;;!IJJ1v&&q)	u%%))4;%*\5=+B C C	 \4;1A#ABBbADKy999DL	:::EL	:::EM;;;	= =
 !88Bx
EK)H)HIIII>>)6$*ek3R3R>SSS))4;%*\5=+B+. * 0 0	 )a-y999(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	" 	" 	" ??8DG$$$~~tWf5Y~GGGrj   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/   rE   r   r>   r,   r   r   r:   rt   rD   r   r<   r=   )rU   krowscolsr   r   s         r`   diagonalz_cs_matrix.diagonal6  s    Z
d::d8ATY_5555\4;#<==HSAq		)4#a))+;<<!$*--/ / /
1djmTZ]DK9a	 	 	rj   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.r0   rn   r+   )r,   r/   zOperands not compatible.)r   r   r
   rE   r   r/   rP   r,   r   rN   r   rK   r>   r=   r<   r   r   r   rM   )rU   ry   npopr   dense_checkr   new_datamats           r`   _maximum_minimumz_cs_matrix._maximum_minimumG  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8rj   c                 H    |                      |t          j        dd           S )N	_maximum_c                 2    t          j        |           dk    S Nr   rE   rP   ru   s    r`   <lambda>z$_cs_matrix.maximum.<locals>.<lambda>`      BJqMMA<M rj   )r   rE   maximumr   s     r`   r	  z_cs_matrix.maximum^  ,    $$UBJ%02M2MO O 	Orj   c                 H    |                      |t          j        dd           S )N	_minimum_c                 2    t          j        |           dk     S r  r  r  s    r`   r  z$_cs_matrix.minimum.<locals>.<lambda>f  r  rj   )r   rE   minimumr   s     r`   r  z_cs_matrix.minimumd  r
  rj   c                 *   | j         dk    rt          | 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 )zSum the array/matrix over the given axis.  If the axis is None, sum
        over both rows and columns, returning a scalar.
        r-   	blocksize))r   rb   )r   r   r   r+   Nzdimensions do not matchr   )rg   r,   out)rR   hasattrrA   r    r,   rE   rF   rH   r<   _minor_reduceadd_ascontainerr   r/   rM   r   r   )rU   rg   r,   r  	res_dtyper   major_indexvalues           r`   r   z_cs_matrix.sumn  s    INN74#=#=N

#566q999%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Erj   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>   rE   flatnonzerorf   r<   reduceatr   )rU   ufuncr>   r  r  s        r`   r  z_cs_matrix._minor_reduce  s_     <9DnRWT[%9%9::t24;{3KLLN NE!!rj   c                 
   d|cxk    r| j         d         k    rYn nVt          j        | j        |k              }|j        r| j        |d                  S | j        j                            d          S t          d| d          )Nr   zindex (z) out of range)	r/   rE   r  r=   r   r>   r,   type
IndexError)rU   idxspots      r`   _get_intz_cs_matrix._get_int  s    $$$$tz!}$$$$$>$,#"566Dy *ya))9?''***63666777rj   c                    |                      | j                  \  }}|                      ||f          \  }}t          ||| j        | j        | j        ||dz   ||dz   	  	        \  }}}	|	                    | j                  S )Nr   r+   )rA   r/   r   r<   r=   r>   r   r,   )
rU   r   r   rX   rY   majorminorr<   r=   r>   s
             r`   _get_intXintz_cs_matrix._get_intXint  s    zz$*%%1zz3*--u 1q$+t|TY519eUQY!0 !0 xxdjx)))rj   c                     |                      ||f          \  }}|j        dv r!|j        dv r|                     ||d          S |                     |                              |          S )N)r   NTr5   )rA   step_get_submatrix_major_slice_minor_slicerU   r   r   r%  r&  s        r`   _get_sliceXslicez_cs_matrix._get_sliceXslice  sq    zz3*--u:""uzY'>'>&&ue$&???  ''44U;;;rj   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/   rE   rP   r   r   r   r<   r>   r   rR   r  rN   r   )	rU   r   r   rZ   rX   rY   r%  r&  vals	            r`   _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7rj   c                     |                      ||f          \  }}|                     |                              |          S N)rA   _major_index_fancy_minor_index_fancyr-  s        r`   _get_columnXarrayz_cs_matrix._get_columnXarray  s=    zz3*--u&&u--@@GGGrj   c           	         |                      | j        | j        f          }t          j        ||                                          }|                     | j                  d         }t          |          }| j	        dk    r|                     ||f          n|f}|dk    r| 
                    || j                  S | j        |dz            | j        |         z
                      |          }t          j        |dz   |          }t          j        ||dd                    |d         }	t          j        |	|          }
t          j        |	| j                  }t!          ||| j                            |d	          | j                            |d	          | j        |
|           | 
                    ||
|f|d
          S )zBIndex along the major axis where idx is an array of ints.
        r+   r   r-   r   Nr  rb   Fr5   r   )rC   r<   r=   rE   rP   r   rA   rB   rH   rR   rN   r,   rS   rF   cumsumr   r   r>   )rU   r!  rZ   r=   rY   rX   r   row_nnz
res_indptrrs   res_indicesres_datas               r`   r4  z_cs_matrix._major_index_fancy  s    ))4;*EFF	*S	22288::JJt())!,LL*.)q..DJJ1v&&&qd	66>>)4:>>>>;w{+dk'.BBJJ9UUXac333

	'z!""~....nhs)4448Ctz222Kyu55L	66I	
 	
 	
 ~~xjA$-E  ; ; 	;rj   c           
      ^   |t          d          k    r|r|                                 n| S |                     | j                  \  }}|                    |          \  }}}t          t          |||                    }| j        dk    r|                     ||f          n|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   r+   rb   r   r8  r5   Fr   )slicer3   rA   rB   r=   rH   rangerR   rN   r,   r<   rE   rF   r9  rL   r>   r   r   )rU   r!  r3   rX   rY   startstopr)  r   start0stop0start1stop1r:  rZ   r;  all_idxr<  r=  rs   s                       r`   r+  z_cs_matrix._major_slice  s9    %++"&0499;;;D0zz$+,,1KKNNtTeT4(())*.)q..DJJ1v&&&qd	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  ; ; 	;rj   c           
         |                      | j        | j        f          }| j                            |d          }| j                            |d          }t	          j        ||                                          }|                     | j                  \  }}t          |          }| j
        dk    r|                     ||f          n|f}|dk    r|                     || j                  S t	          j        ||          }	t	          j        | j        |          }
t          |||||||	|
           t	          j        |                              |d          }|
d         }t	          j        ||          }t	          j        || j                  }t%          ||	t          | j                  || j        ||           |                     |||
f|d          S )zBIndex along the minor axis where idx is an array of ints.
        Fr5   r+   r-   r   rb   r   )rC   r=   r<   rS   rE   rP   r   rA   rB   rH   rR   rN   r,   rF   
empty_liker   argsortr   r   r>   )rU   r!  rZ   r=   r<   rX   rY   r   r   col_offsetsr;  	col_orderrs   r<  r=  s                  r`   r5  z_cs_matrix._minor_index_fancy4  s    ))4<*EFF	,%%ie%<<##IE#::jI...4466zz$+,,1HH*.)q..DJJ1v&&&qd	66>>)4:>>>> hq	222]4;i@@@
		
 		
 		
 JsOO**95*AA	nhs)4448Ctz222)[#dl2C2C!49k8	E 	E 	E~~xjA$-E  ; ; 	;rj   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&  r3   )r?  r3   rA   rB   r=   rH   r@  rN   r,   r*  r5  rE   r   )rU   r!  r3   rX   rY   rA  rB  r)  s           r`   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Drj   c                    |                      | j                  \  }}t          ||          \  }}t          ||          \  }}	|dk    r*|dk    r$||k    r|	|k    r|r|                                 n| S t	          ||| j        | j        | j        ||||		  	        \  }
}}|                      ||z
  |	|z
  f          }| j        dk    r	|d         f}| 	                    |||
f|| j
        d          S )zbReturn a submatrix of this matrix.

        major, minor: None, int, or slice with step 1
        r   r   F)r/   r,   r3   )rA   rB   _process_slicer3   r   r<   r=   r>   rR   rN   r,   )rU   r%  r&  r3   rX   rY   i0i1j0j1r<   r=   r>   r/   s                 r`   r*  z_cs_matrix._get_submatrixk  s   
 zz$+,,1q))Bq))B77rQww277rQww"&0499;;;D0 1q$+t|TYBB!H !H 

BGR"W-..9>>1XKE~~tWf5U$(JU  < < 	<rj   c                 h    |                      d|f          \  }}|                     |||           d S r  rA   	_set_many)rU   r!  ru   r%  r&  s        r`   _set_intz_cs_matrix._set_int  s7    zz1c(++uueQ'''''rj   c                    |                      t          j        |          |f          \  }}|j        d         dk    o|j        d         dk    }|r%t          j        |j        |j        d                   }|                     |||           d S )Nrb   r   )rA   rE   
zeros_liker/   r   r>   rV  )rU   r!  ru   r%  r&  	broadcasts         r`   
_set_arrayz_cs_matrix._set_array  s    zz2=#5#5s";<<uGBK1$=RA)=	 	1	!&#)B-00AueQ'''''rj   c                 h    |                      ||f          \  }}|                     |||           d S r3  rU  rU   r   r   ru   rl   js         r`   _set_intXintz_cs_matrix._set_intXint  7    zz3*%%1q!Qrj   c                 h    |                      ||f          \  }}|                     |||           d S r3  rU  r]  s         r`   _set_arrayXarrayz_cs_matrix._set_arrayXarray  r`  rj   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/   r   r   rE   rP   r>   r,   r   r   r   rH   r   rV  )rU   r   r   ru   rX   rY   broadcast_rowbroadcast_colrcrl   r^  s               r`   _set_arrayXarray_sparsez"_cs_matrix._set_arrayXarray_sparse  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rj   c           
         d| j         v rd S | j        dk    rt          d          | j         \  }}|j        dk    }|dk     r}|rt          ||z   |          }n!t          ||z   |t	          |                    }t          j        | ||z
  | j        j                  }t          j        || j        j                  }n{|rt          |||z
            }n!t          |||z
  t	          |                    }t          j        || j        j                  }t          j        |||z   | j        j                  }|s|d t	          |                   }t          j	        t          j
        || j                                                            }	|	                                j         |                                j         k    rt          j        |	|j                   }	|	j        dk    rd S |                     ||f          \  }}|                     ||f          \  }}|	j        }
t          j        |
| j        j                  }t#          ||| j        | j        |
|||          }|dk    r4|                                  t#          ||| j        | j        |
|||           d|vr|	| j        |<   d S |dk    }|                                | j        dz  k     rH||         }||         }|                     |||	|                    | }|	|         | j        ||         <   d S |                                 }|                    ||           |                    | j                  }|\  | _        | _        | _        }d S )Nr   r   z"diagonals cant be set in 1d arraysr+   rb   gMbP?)r/   rR   r   rt   rH   rE   r   r=   r,   
atleast_1drP   r   squeezebroadcast_tor   rA   r   r   r<   rK   r>   r   rs   _insert_manyr   _setdiagrJ   )rU   valuesr   rX   rY   rZ  	max_indexrl   r^  ru   	n_samplesoffsetsr   maskr[   r\   rh   s                    r`   ro  z_cs_matrix._setdiag  sQ   
??F9>>%&JKKKz1[A%	q55 7AqMM		Aq#f++66		1"i!m4<3EFFFA	)4<+=>>>AA  71q5MM		1q5#f++66		)4<+=>>>A	!Q]$,2DEEEA 	%GSVVG_FM"*V4:>>>??EEGG99;;		 11117++A6Q;;Fzz1a&!!1zz1a&!!1F	(9DL,>??? At{DL)!"Aw0 0!88!!!q!T[$,	 !W. . .W!"DIgF2 88::5((($A$AaAdG,,,5D'(wDIgdm$$$ **,,CLL###++DJ77F6<3DKty!!!rj   c                    |                      | j                  \  }}d }t          j        t          j        || j        j                                                            }t          j        t          j        || j        j                                                            } |||            |||           ||||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))rD   r   rt   )r=   boundr!  s      r`   check_boundsz1_cs_matrix._prepare_indices.<locals>.check_bounds  sy    ++--Ce|| !B"%u". / / /++--CeV|| !B"%u". / / / |rj   r+   )rA   rB   rE   rk  rP   r=   r,   r   )rU   rl   r^  rX   rY   rx  s         r`   _prepare_indicesz_cs_matrix._prepare_indices  s    zz$+,,1	/ 	/ 	/ M"*Qdl.@AAABBHHJJM"*Qdl.@AAABBHHJJQQ!Qzrj   c           
         |                      ||          \  }}}}t          j        t          j        || j                                                            }|j        }t          j        || j        j                  }t          ||| j
        | j        ||||          }|dk    r4|                                  t          ||| j
        | j        ||||           d|vr|| j        |<   dS t          d| j        j         d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.
        r+   r   rb   Nz%Changing the sparsity structure of a z. is expensive. lil and dok are more efficient.r0   rn   r   )ry  rE   rk  rP   r,   r   r   r   r=   r   r<   rK   r>   r   rN   rO   r
   rn  )
rU   rl   r^  ru   rX   rY   rr  rs  r   rt  s
             r`   rV  z_cs_matrix._set_many  s    **1a00
1aM"*Qdj999::@@BBF	(9DL,>??? At{DL)!"Aw0 0!88!!!q!T[$,	 !W. . . W!"DIgF  ?9P ? ? ?(Q8 8 8 8 R<D'(wDIgdm$5D$Aa!eHHHMHHH$Aa!eHHHMHHHaAdG,,,,,rj   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   rb   N)
ry  rH   rE   r   r=   r,   r   r<   rK   r>   )rU   rl   r^  rX   rY   rr  rs  r   s           r`   rd  z_cs_matrix._zero_many*  s    
 **1a00
1aFF	(9DL,>??? At{DL)!"Aw0 0!88!!!q!T[$,	 !W. . . ,-	''B,'(((rj   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)rp   clip)moderb   r(   r+   T)return_indexr   r   Nr8  Fr6   )rE   rJ  takehas_sorted_indicesrC   r=   r<   r   rP   uniqueappendrH   rf   	enumeratezipr>   concatenater   r/   r9  sort_indicesrT   )rU   rl   r^  ru   r   do_sortrZ   indices_parts
data_partsui	ui_indptrnew_nnzsprevrh  iijsjerA  rB  uj	uj_indptrnnzsindptr_diffs                          r`   rn  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+++++rj   c                    | j         dk    r>|                                 }|                     |j        |j        ff|j        |          S |                     | j                  \  }}| j        }t          j        t          |          | j        j
                  }t          j        || j        |           |                     ||f          }|                     | j        |f| j        || j
                  S )Nr   r5   r+   r2   )rR   tocsrrI   r>   r=   r/   rA   rE   r   rH   r,   r   	expandptrr<   )rU   r3   csrr   	minor_dimminor_indicesmajor_indicesr   s           r`   r   z_cs_matrix.tocoo  s    9>>**,,C&&3;.'A39SW&XXX#zz$*55	9]!3!34<;MNNNy$+}EEE]M:;;""Y$dj # 
 
 	
rj   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_contiguousrM   r  r   r   rB   r   r<   r=   r>   )rU   r   r  ru   r   rX   rY   s          r`   r   z_cs_matrix.toarray  s    ;5=JJt$$Q'E((44	& 	G#)*@ 	GEFFF9! 	

AAA

AAwwq~&&1Aq!(AIqvq999
rj   c                     |                      | j                  \  }}t          j        ||| j        | j        | j                   |                                  dS )z\Remove zero entries from the array/matrix

        This is an *in place* operation.
        N)rA   rB   r   csr_eliminate_zerosr<   r=   r>   rr   rU   rX   rY   s      r`   rx   z_cs_matrix.eliminate_zeros  sL    
 zz$+,,1(At{DL$)TTT

rj   returnc                     t          | dd          sd| _        nWt          | d          sGt          t	          j        t          | j                  dz
  | j        | j                            | _	        | j        S )aZ  Whether the array/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_formatrH   r<   r=   has_canonical_formatrU   s    r`   r  z_cs_matrix.has_canonical_format  s     t2D99 	).D&&677 	(,5$$q($+t|E E) )D% ))rj   r0  c                 D    t          |          | _        |r	d| _        d S d S )NT)r  r  r  rU   r0  s     r`   r  z_cs_matrix.has_canonical_format  s0    %)#YY" 	+&*D###	+ 	+rj   c                     | j         rdS |                                  |                     | j                  \  }}t	          j        ||| j        | j        | j                   | 	                                 d| _         dS )zfEliminate duplicate entries by adding them together

        This is an *in place* operation.
        NT)
r  r  rA   rB   r   csr_sum_duplicatesr<   r=   r>   rr   r  s      r`   rK   z_cs_matrix.sum_duplicates  sx    
 $ 	Fzz$+,,1'1dk4<SSS

$(!!!rj   c                     t          | d          sGt          t          j        t	          | j                  dz
  | j        | j                            | _        | j        S )zWhether the indices are sorted

        Returns
            - True: if the indices of the array/matrix are in sorted order
            - False: otherwise
        r  r   )r  r  r   csr_has_sorted_indicesrH   r<   r=   r  r  s    r`   r  z_cs_matrix.has_sorted_indices  s`     t233 	'+3$$q($+t|E E( (D$ ''rj   c                 .    t          |          | _        d S r3  )r  r  r  s     r`   r  z_cs_matrix.has_sorted_indices  s    #'99   rj   c                 V    |                                  }|                                 |S )z?Return a copy of this array/matrix with sorted indices
        )r3   r  )rU   As     r`   sorted_indicesz_cs_matrix.sorted_indices  s&     IIKK	rj   c                     | j         sDt          j        t          | j                  dz
  | j        | j        | j                   d| _         dS dS )z9Sort the indices of this array/matrix *in place*
        r   TN)r  r   csr_sort_indicesrH   r<   r=   r>   r  s    r`   r  z_cs_matrix.sort_indices  s]     & 	+)#dk*:*:Q*>*.,	C C C&*D###	+ 	+rj   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   rB   rH   r<   rM   r=   rs   r>   r   )rU   r   s     r`   rr   z_cs_matrix.prune  s     JJt011!4	t{y1},,?@@@t|tx''HIIIty>>DH$$EFFF#DL$($;<< 9DH9!566			rj   c                    t          |t          | t                              }t          | d          r{| j        \  }}t          |d         |          \  }}t          |d         |          \  }}|s|rt          d| j         d|           | j        d         |z  | j        d         |z  }	}nR|                     t          |          dk    r|n	d|d         f          \  }}|                     | 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   r   zshape must be divisible into z blocks. Got r8  )r!   r9   r	   r  r  divmodrM   r/   rA   rH   rB   r=   r<   r>   rE   resizer   allr  r  r9  r?   )rU   r/   bmbnnew_Mrmnew_NrnrX   rY   rt  r  r0  s                r`   r  z_cs_matrix.resize  s^   EJtW,E,EFFF4%% 
	1^FBuQx,,IE2uQx,,IE2 :R :  "9 "9 "916"9 "9 : : ::a=B&
1(;qAA::s5zz!||ee!U1XOOLE5::d/00DAq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rj   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.   )rN   r=   r3   r<   r/   r,   )rU   r>   r3   s      r`   rw   z_cs_matrix._with_dataH  s    
  	F>>4):):)<)<#';#3#3#5#5#7(,
(,
 " 4 4 4
 >>4t{"C(,
$* " F F Frj   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        \  }
} ||
|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+   )r~   r   r   r   r   r   )rN   r   r   r:   rs   rC   r<   r=   rE   r   r/   r   r   r,   rB   rP   r>   rr   )rU   ry   rz   r   maxnnzrZ   r<   r=   bool_opsr>   rX   rY   r  s                r`   r   z_cs_matrix._binoptV  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 1
1a:dk333:dl)4449:el)444:em9555:7D	" 	" 	" NND'62$*NEE				rj   c                    |j         | j         k    rt          d          |                     |d          }t          j        |j        t          j                  rt          j        | j         | j                  }|                    t          j	                   |
                                }| j        dk    r	|d         f}d||<   |                                }|j        ||j        <   |                     |          S |}|S )z?
        Divide this matrix by a second sparse matrix.
        r   _eldiv_r+   r   rb   r   )r/   rM   r   rE   
issubdtyper,   inexactr   r   nannonzerorR   r   r>   r   r   )rU   ry   rg  r  r   s        r`   _divide_sparsez_cs_matrix._divide_sparsey  s     ;$*$$2333LL	**="*-- 	
 (4:TZ888CHHRV]]__FyA~~ *CK		AFCM??3''' CJrj   )NNFr3  )T)r   )NNNF)NN)ArO   
__module____qualname____doc__r8   ri   r   rT   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  r   r  r#  r'  r.  r1  r6  r4  r+  r5  r,  r*  rW  r[  r_  rb  ri  ro  ry  rV  rd  rn  r   r   rx   propertyr  r  setterrK   r  r  r  rr   r  rw   r   r  r   rj   r`   r$   r$      s        U, U, U, U,n3 3 3 3& o-GO7- 7- 7- 7-F  "" "" ""H "  "  "D!" !" !"F9 9 99 9 98 8 88 8 8	3 	3 	3- - -. . .u u uv
7 
7 
7   4H 4H 4Hl	 	 	 	 '/H9 9 9.O O O o-GOO O O o-GOF F F F4 +%CK" " " "48 8 8J* * *< < <8 8 8H H H
; ; ;B&; &; &; &;P%; %; %;NE E E E < < < <*( ( (( ( (               2B= B= B=H  &%- %- %-N- - -(F, F, F,X
 
 
 
 M)EM   " o-GO   *d * * * X*,  + + + + ! +
) ) ) (D ( ( ( X( -d - - - -  + + +7 7 7! ! !F ^+FNF F F F! ! !F    rj   r$   Fc                     ddl m}m} |r|n|}t          |           }t	          j        |dz             }|dd         } || ||f||f          S )zbuild diagonal csc_array/csr_array => self._csr_container

    Parameter `data` should be a raveled numpy array holding the
    values on the diagonal of the resulting sparse matrix. 
    r   )	csr_array
csr_matrixNrb   r   )_csrr  r  rH   rE   r   )r>   rW   r  r  rY   r<   r=   s          r`   r   r     su     ,+++++++%5		:ID		AYq1uFSbSkG9dGV,QF;;;;rj   c                 j   | d|}}nt          | t                    r?|                     |          \  }}}|dk    rt          d          t	          ||          }nUt          |           r7| dk     r| |z  } | | dz   }}|dk     s||k    rt          d| d| d|           nt          d          ||fS )Nr   r   z$slicing with step != 1 not supportedzindex out of bounds: 0 <= z < z <= zexpected slice or scalar)r9   r?  r=   rM   rt   r   r   	TypeError)slnumrP  rQ  strides        r`   rO  rO    s    	zCB	B		 4CBQ;;CDDDR[[	2 466#IBR!VB66R#XXN"NNNNNNOOO  2333r6Mrj   r  ).r  __all__warningsr   r   numpyrE   scipy._lib._utilr   r   _baser   r   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$   r   rO  r   rj   r`   <module>r     s5   D D
            9 9 9 9 9 9 9 9 F F F F F F F F F F F F . . . . . . . .      A A A A A A A A A A A A A A A A A A A A      G G G G G G G G G G G G G G G G G G G G G G G G G G G G
} } } } }}j } } }@,< < < <     rj   