o
    `^hg                     @   s  d Z g ZddlmZ ddl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+m,Z,m-Z- G dd deeeZ.dddZ/dd Z0d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get_index_dtypebroadcast_shapesis_pydata_spmatrixc                   @   s  e Zd ZdZd{ddddZd|ddZejje_d|d	d
Zejje_d}ddZdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd~d-d.Zejje_d/d0 Zd1d2 Zejje_d3d4 Zejje_dd5d6Zejje_d|d7d8Zd9d: Zd;d< Zd=d> Z d?d@ Z!dAdB Z"ddCdDZ#dEdF Z$ddGdHZ%d{dIdJZ&dKdL Z'dMdN Z(dOdP Z)dQdR Z*dSdT Z+dUdV Z,dWdX Z-dYdZ Z.d}d[d\Z/ej/je/_dd]d^Z0ej0je0_d_d` Z1e2dae3fdbdcZ4e4j5dde3fdedcZ4dfdg Z6e2dae3fdhdiZ7e7j5dde3fdjdiZ7dkdl Z8dmdn Z9dodp Z:dqdr Z;ej;je;_d}dsdtZ<dudv Z=dwdx Z>ddydzZ?dS )
_cs_matrixzY
    base array/matrix class for compressed row- and column-oriented arrays/matrices
    NFmaxprintc             
   C   sP  t j| ||d t|r3|j| jkr|r| }n|| j}|j|j|j|j	f\| _| _| _| _	nt
|trt|| jdrut|| jd| _	| | j\}}| jt| jd}tdt|td| _td|| _tj|d |d| _nt|dkr| j|||d	}	|	| j}
|
\| _| _| _| _	|   nt|d
kr|\}}}d }|d urd|vrt|}| j||f|dd}|st}tj|||d| _tj|||d| _tj|||d| _notd| jj  d| zt!|}W n t"y } ztd| jj  d| |d }~ww t
| t#r$|j$dkr$| jdkr$td|j$ d|j$dkr3td|j$ d| j||d}	|	| j}
|
\| _| _| _| _	|d urXt|| jd| _	n6| jd u rzt| jd }| j d }W n t"y } ztd|d }~ww t| ||f| jd| _	|d urt|}| jj%|dd| _| j&dd d S )Nr%   allow_nd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CSR arrays don't yet support zD.z!unable to infer matrix dimensionsFr4   
full_check)'r
   __init__r   formatr4   asformatindptrindicesdata_shape
isinstancetupler   	_allow_ndr    _swap_shape_as_2d_get_index_dtypemaxr0   npzerosr   floatlen_coo_container_coo_to_compressedsum_duplicatesr   array
ValueError	__class____name__asarray	Exceptionr   ndimastypecheck_format)selfarg1r0   r-   r4   r&   MN	idx_dtypecooarraysr>   r=   r<   r*   enewdtype r`   V/home/air/shanriGPT/back/venv/lib/python3.10/site-packages/scipy/sparse/_compressed.pyr9      s   




$


z_cs_matrix.__init__c                 C   s   |d u rt | jd S | jdkr|dv rt | jd S td|dk r'|d7 }| |d| f\}}| | j\}}|dkrHtjt| j	|dS |dkrRt
| jS td)Nr   r   rb   axis out of boundsr   r.   	minlength)intr<   rT   rO   rC   r0   rG   bincountr   r=   diff)rW   axis_rZ   r`   r`   ra   _getnnzu   s   
z_cs_matrix._getnnzc                    s       |d u rt jS  jdkr!|dvrtdt jS |dk r)|d7 } |d| f\}}|dkrZ  j\}} jdk}| rL j	n j	| }tj
t||dS |dkr{ j rit jS t j}t fdd|D S td)	Nr   rc   rd   r   r.   re   c                    s$   g | ]\}}t  j|| qS r`   )rG   count_nonzeror>   ).0ijrW   r`   ra   
<listcomp>   s   $ z,_cs_matrix.count_nonzero.<locals>.<listcomp>)rM   rG   rm   r>   rT   rO   rC   r0   allr=   rh   r   ri   r<   	itertoolspairwiserN   )rW   rj   rk   rZ   maskidxpairsr`   rq   ra   rm      s*   


z_cs_matrix.count_nonzeroTc                 C   s  | j jjdkrtd| j jj ddd | jjjdkr(td| jjj ddd | jj| jj| j jfD ]
}|dkr=tdq3| 	| j
\}}t| j |d kr^td	t| j  d
|d  | j d dkritdt| jt| jkrwtd| j d t| jkrtd|   |r| jdkr| j |krtd| | j dk rtdt| j  dk rtd| | j | jf}tj| j |d| _ tj| j|d| _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`.
        ro   z$indptr array has non-integer dtype ()r1   
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>   rT   rO   rC   rD   rJ   prunennzrF   minrG   ri   rE   rR   r   )rW   r8   xrY   rZ   r[   r`   r`   ra   rV      sF   
z_cs_matrix.check_formatc                 C   s*   |    | j|| j|dd}|  |S )zScalar version of self._binopt, for cases in which no new nonzeros
        are added. Produces a new sparse array in canonical form.
        Tr6   )rM   
_with_datar>   eliminate_zeros)rW   otheropresr`   r`   ra   _scalar_binopt   s   z_cs_matrix._scalar_binoptc                 C   s  t |r;t|r| j| jtjdS |dkr4tdtdd | tj| jtjd}| 	|t
j}|| S | 	|t
jS t|rE|  |kS t|rKtS t|rtdtdd | j|jkr^dS | j|jkrj|| j}| |d}| tj| jtjd}|| S tS )	Nr,   r   zOComparing a sparse matrix with 0 using == is inefficient, try using != instead.r1   rz   zHComparing sparse matrices using == is inefficient, try using != instead.F_ne_)r   rG   isnanrP   r0   bool_r   r	   onesr   operatorneeqr   todenser#   NotImplementedr   r:   r;   _binopt)rW   r   all_trueinvr   r`   r`   ra   __eq__   s8   
z_cs_matrix.__eq__c                 C   s   t |rGt|rtdtdd | tj| jtjd}|S |dkr@tdtdd | jt| jtjd}| 	|t
j}|| S | 	|t
jS t|rQ|  |kS t|rWtS t|ru| j|jkrcdS | j|jkro|| j}| |dS tS )	Nz:Comparing a sparse matrix with nan using != is inefficientr1   rz   r,   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Tr   )r   rG   r   r   r	   rP   r   r0   r   r   r   r   r   r   r   r#   r   r   r:   r;   r   )rW   r   r   r   r`   r`   ra   __ne__  s4   
z_cs_matrix.__ne__c                 C   s"  t |r=d|kr|dv rtd|d|r7t|tdd tj| jt|d}|| | 	|}| 
||S | ||S t|rH||  |S t|r| j|jkrVtd| j|jkrb|| j}|dvrl| 
||S td	tdd | 	tj| jtjd}| 
||d
krdnd}|| S tS )Nr   )_le__ge_z >= and <= don't work with 0.r1   rz   r,   inconsistent shapes)r   r   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r   _gt__lt_)r   NotImplementedErrorr   r	   rG   emptyr0   result_typefillrP   r   r   r   r   r   rO   r:   r;   r   r   r   )rW   r   r   op_namebad_scalar_msg	other_arrr   r   r`   r`   ra   _inequality7  s4   


z_cs_matrix._inequalityc                 C      |  |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rW   r   r`   r`   ra   __lt__Z     z_cs_matrix.__lt__c                 C   r   )Nr   zdComparing a sparse matrix with a scalar less than zero using > is inefficient, try using <= instead.)r   r   gtr   r`   r`   ra   __gt__`  r   z_cs_matrix.__gt__c                 C   r   )Nr   zgComparing a sparse matrix with a scalar greater than zero using <= is inefficient, try using > instead.)r   r   ler   r`   r`   ra   __le__f  r   z_cs_matrix.__le__c                 C   r   )Nr   zdComparing a sparse matrix with a scalar less than zero using >= is inefficient, try using < instead.)r   r   ger   r`   r`   ra   __ge__l  r   z_cs_matrix.__ge__c                 C   s   |j | j krtd| j  d|j  dt| jj|jj}| dd }tj|||dd}|jj	r2|n|j
}| | j\}}t||| j| j| j| | j|dd	S )
NzIncompatible shapes (z and ry   CFr   T)r-   orderr4   Fr6   )r0   rO   r   r-   charrC   rG   rN   flagsc_contiguousTrD   r   r<   r=   r>   
_container)rW   r   r-   r   resultyrY   rZ   r`   r`   ra   
_add_densev  s   z_cs_matrix._add_densec                 C      |  |dS )N_plus_r   r   r`   r`   ra   _add_sparse     z_cs_matrix._add_sparsec                 C   r   )N_minus_r   r   r`   r`   ra   _sub_sparse  r   z_cs_matrix._sub_sparsec                 C   s  t |r	| |S t|r| j|jkr| |}| |dS |jdkr=| | d }| jdkr;|d| jd fS |S |jdkrK| | d S | jdv rX|| j	
 S | j\}}|j\}}|dkru|dkru|| || S |dkr|dkr| ||| S t| t}|dkr||krt|  |}| |}| jdkr|S |d|fS |dkr||krt|   |}	||	S |dkr||krt|  |}|| S |dkr||krt|   |}
|
|S td	t|}|jdkr
t|  |S |jdkr+|jtjkrtS t| j|j}| |jd |S | jdv rDt| j|j}t| j	
 ||S |  }| j|jkrdt|j	||j }|tj |_	|S t |}| jd dksw| jdkr|jd dkrt|j	|}n|jd | jd
 krt|j	|dd|j!f }ntd	| j"|j!|j#|jd  d}t$tj%|jd |d|j#}t&|j!j'|dd|jd }| j(|tj ||ff|jd | jd
 fddS | jd dkro|jd dkr	t|j	dddf |}n |jd | jd kr%t|j	dddf ||j) }ntd	| j"|j)|j#|jd  d}t$|j)j'|dd|jd }t&tj%|jd |d|j#}| j(|tj ||ff| jd |jd fddS |jd dkr| jd |jd krt|j	|dd|j!f  }n$|jd dkr| jd |jd krt|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   Nr)   r,   Fr6   r0   r4   )*r   _mul_scalarr   r0   rP   r   toarrayrT   reshaper>   sumrD   _matmul_sparsetocscr@   r   _make_diagonal_csrravelrO   rG   
asanyarraymultiplysizer-   object_r   r"   flattocoocoordsviewndarray
atleast_2dcolrE   r   repeatarangetilerU   rK   row)rW   r   r   sMsNoMoNis_array	new_otherr4   new_selfbshaperetr>   other2dr[   r   r   r`   r`   ra   r     s   














"&"&z_cs_matrix.multiplyc                 C   sf   | j \}}tj|t| jj|jjd}tt| jd }|||| j	| j
| j|| | jdkr1|d S |S )Nr,   _matvecr   r   )rD   rG   rH   r   r-   r   getattrr   r:   r<   r=   r>   rT   )rW   r   rY   rZ   r   fnr`   r`   ra   _matmul_vector  s
   
z_cs_matrix._matmul_vectorc              
   C   s   | j \}}|jd }tj||ft| jj|jjd}tt| j	d }||||| j
| j| j| |  | jdkr?||fS |S )Nrb   r,   _matvecsr   )rD   r0   rG   rH   r   r-   r   r   r   r:   r<   r=   r>   r   rT   r   )rW   r   rY   rZ   n_vecsr   r   r`   r`   ra   _matmul_multivector  s   



z_cs_matrix._matmul_multivectorc                 C   sr  | j \}}|j}|dkr|d|jd fj}|jdkr|jn|jd df\}}d}| jdkr4||f7 }|dkr=||f7 }| jdkrD|nd|dkrK|ndf}| ||fd }	| |}| | j	| j
|j	|j
f}
tt| jd }|||tj| j	|
dtj| j
|
dtj|j	|
dtj|j
|
d}|dkr|dkrtjdt| j|jdS | j|t| j|jdS | j| j	| j
|j	|j
f|d}
tj|	d |
d}tj||
d}tj|t| j|jd}tt| jd }|||tj| j	|
dtj| j
|
d| jtj|j	|
dtj|j
|
d|j||| |dkrt|d S | j|||f|d	}||kr7|jd
kr2| }||}|S )Nr   r   r.   r`   _matmat_maxnnzr,   r)   _matmatr0   csr)rD   rT   r   r0   r   r?   rC   rP   rE   r<   r=   r   r   r:   rG   rR   rN   r   r-   r   r>   tocsr)rW   r   rY   K1o_ndimK2rZ   	new_shape
faux_shape	major_dimr[   r   r   r<   r=   r>   r   r`   r`   ra   r   "  sh   
"


"



z_cs_matrix._matmul_sparser   c                 C   s   | j \}}|| ks||krtjd| jjdS tt| jd }tjt|t|d |t	|d t
| jd}||| j d | j d | j| j| j| |S )Nr   r,   	_diagonalr   )r0   rG   r   r>   r-   r   r   r:   r   rF   r   r<   r=   )rW   krowscolsr   r   r`   r`   ra   diagonal^  s   
 z_cs_matrix.diagonalc                 C   s   t |rI||r+tdtdd tj| jt|jd}|| | 	|}| 
||S |   || jt|}| j	|| j| jf|j| jd}|S t|rT||  |S t|r^| 
||S td)NzITaking maximum (minimum) with > 0 (< 0) number results to a dense matrix.r1   rz   r,   )r-   r0   zOperands not compatible.)r   r   r	   rG   r   r0   rR   r-   r   rP   r   rM   r>   r=   r<   r   r   r   rO   )rW   r   npopr   dense_checkr   new_datamatr`   r`   ra   _maximum_minimumo  s*   

z_cs_matrix._maximum_minimumc                 C      |  |tjddd S )N	_maximum_c                 S   s   t | dkS Nr   rG   rR   r   r`   r`   ra   <lambda>      z$_cs_matrix.maximum.<locals>.<lambda>)r   rG   maximumr   r`   r`   ra   r       
z_cs_matrix.maximumc                 C   r   )N	_minimum_c                 S   s   t | dk S r  r  r  r`   r`   ra   r    r  z$_cs_matrix.minimum.<locals>.<lambda>)r   rG   minimumr   r`   r`   ra   r
    r  z_cs_matrix.minimumc                 C   s   | j dkrTt| dsT|| dd v rTt| j}tjt| jd |d}| 	tj
\}}|||< | |}|d dkr>|j}|durL|j|jkrLtd|jd	||d
S tj| |||d
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`   )rj   r-   out)rT   hasattrrC   r   r-   rG   rH   rJ   r<   _minor_reduceadd_ascontainerr   r0   rO   r   r   )rW   rj   r-   r  	res_dtyper   major_indexvaluer`   r`   ra   r     s   

z_cs_matrix.sumc                 C   s>   |du r| j }tt| 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
        N)r>   rG   flatnonzerori   r<   reduceatr   )rW   ufuncr>   r  r  r`   r`   ra   r    s   z_cs_matrix._minor_reducec           
      C   s\   |  | j\}}|  ||f\}}t||| j| j| j||d ||d 	\}}}	|	j| jdS )Nr   r,   )rC   r0   r   r<   r=   r>   r   r-   )
rW   r   r   rY   rZ   majorminorr<   r=   r>   r`   r`   ra   _get_intXint  s   
z_cs_matrix._get_intXintc                 C   sF   |  ||f\}}|jdv r|jdv r| j||ddS | ||S )Nr   NTr6   )rC   step_get_submatrix_major_slice_minor_slicerW   r   r   r  r  r`   r`   ra   _get_sliceXslice  s   z_cs_matrix._get_sliceXslicec           	   
   C   s   | j j}| | j\}}| ||f\}}tj||d}tj||d}tj|j| jd}t||| j	| j | j
|j| | |	 |jdkrJ| |S | ||jS )Nr,   r   )r=   r-   rC   r0   rG   rR   r   r   r   r<   r>   r   rT   r  rP   r   )	rW   r   r   r[   rY   rZ   r  r  valr`   r`   ra   _get_arrayXarray  s   

z_cs_matrix._get_arrayXarrayc                 C   s"   |  ||f\}}| ||S N)rC   _major_index_fancy_minor_index_fancyr   r`   r`   ra   _get_columnXarray  s   z_cs_matrix._get_columnXarrayc                 C   s"  |  | j| jf}tj||d }| | jd }t|}| j	dkr*| ||fn|f}|dkr9| j
|| jdS | j|d  | j|  |}tj|d |d}tj||dd d |d }	tj|	|d}
tj|	| jd}t||| jj|dd	| jj|dd	| j|
| | j
||
|f|dd
S )zBIndex along the major axis where idx is an array of ints.
        r,   r   r.   r   Nr  rb   Fr6   r   )rE   r<   r=   rG   rR   r   rC   rD   rJ   rT   rP   r-   rU   rH   cumsumr   r   r>   )rW   rw   r[   r=   rZ   rY   r   row_nnz
res_indptrr   res_indicesres_datar`   r`   ra   r%    s2   
z_cs_matrix._major_index_fancyc              	   C   s  |t dkr|r|  S | S | | j\}}||\}}}tt|||}| jdkr2| ||fn|f}|dkrA| j|| j	dS ||}	}
|dkrP|dkrPd}
|d |d }}| j
||| | j
|	|
|  }| jj	}tj|d |d}tj||dd d |dkrt | j
| | j
| }tj| j| |d}tj| j| |d}n!|d }tj||d}tj|| j	d}t|||| j
| j| j|| | j|||f|d	d
S )z@Index along the major axis where idx is a slice object.
        Nr.   r   r,   rb   r   r(  r6   Fr   )slicer4   rC   rD   r=   rJ   rangerT   rP   r-   r<   rG   rH   r)  rN   r>   r   r   )rW   rw   r4   rY   rZ   startstopr  r   start0stop0start1stop1r*  r[   r+  all_idxr,  r-  r   r`   r`   ra   r    s>   
z_cs_matrix._major_slicec              	   C   s2  |  | j| jf}| jj|dd}| jj|dd}tj||d }| | j\}}t	|}| j
dkr:| ||fn|f}|dkrI| j|| jdS tj||d}	tj| j|d}
t|||||||	|
 t|j|dd}|
d }tj||d}tj|| jd}t||	t	| j|| j|| | j|||
f|ddS )zBIndex along the minor axis where idx is an array of ints.
        Fr6   r,   r.   r   rb   r   )rE   r=   r<   rU   rG   rR   r   rC   rD   rJ   rT   rP   r-   rH   
empty_liker   argsortr   r   r>   )rW   rw   r[   r=   r<   rY   rZ   r   r   col_offsetsr+  	col_orderr   r,  r-  r`   r`   ra   r&  6  s>   
z_cs_matrix._minor_index_fancyc                 C   s   |t dkr|r|  S | S | | j\}}||\}}}tt|||}|dkr7| j| ||f| jdS |dkrB| j	||dS | 
t|||S )z@Index along the minor axis where idx is a slice object.
        Nr   r,   r   )r  r4   )r.  r4   rC   rD   r=   rJ   r/  rP   r-   r  r&  rG   r   )rW   rw   r4   rY   rZ   r0  r1  r  r`   r`   ra   r  ]  s   z_cs_matrix._minor_slicec              
   C   s   |  | j\}}t||\}}t||\}}	|dkr.|dkr.||kr.|	|kr.|r,|  S | S t||| j| j| j||||		\}
}}|  || |	| f}| jdkrU|d f}| j	|||
f|| j
ddS )zbReturn a submatrix of this matrix.

        major, minor: None, int, or slice with step 1
        r   r   F)r0   r-   r4   )rC   rD   _process_slicer4   r   r<   r=   r>   rT   rP   r-   )rW   r  r  r4   rY   rZ   i0i1j0j1r<   r=   r>   r0   r`   r`   ra   r  m  s    


z_cs_matrix._get_submatrixc                 C   $   |  ||f\}}| ||| d S r$  rC   	_set_manyrW   r   r   r   ro   rp   r`   r`   ra   _set_intXint     z_cs_matrix._set_intXintc                 C   r@  r$  rA  rC  r`   r`   ra   _set_arrayXarray  rE  z_cs_matrix._set_arrayXarrayc                 C   s  | j | ||f  |j\}}|dko|jd dk}|dko$|jd dk}|j|j}}	tj|j| jd}|j	dkr<d S |rUt
t|t|}t|	|}	t||}|rnt
||}tt|t|	}	t
||}| |||	f |||	f f\}
}| |
|| d S )Nr   r   r,   )
_zero_manyrC   r0   r   r   rG   rR   r>   r-   r   r   r   rJ   r   rB  )rW   r   r   r   rY   rZ   broadcast_rowbroadcast_colrcro   rp   r`   r`   ra   _set_arrayXarray_sparse  s$   

"z"_cs_matrix._set_arrayXarray_sparsec              	   C   sn  d| j v rd S | jdkrtd| j \}}|jdk}|dk rI|r(t|| |}n
t|| |t|}tj| || | jjd}tj|| jjd}n)|rSt||| }n
t||| t|}tj|| jjd}tj||| | jjd}|s||d t| }t	tj
|| jd }	|	 j | j krt|	|j }	|	jdkrd S | ||f\}}| ||f\}}|	j}
tj|
| jjd}t||| j| j|
|||}|dkr|   t||| j| j|
||| d|vr|	| j|< d S |dk}| j|  | jd k r|	| | j|| < | }|| }|| }| |||	|  d S |  }||| || j}|\| _| _| _}d S )Nr   r   z"diagonals cant be set in 1d arraysr,   rb   gMbP?)r0   rT   r   r   rJ   rG   r   r=   r-   
atleast_1drR   r   squeezebroadcast_tor   rC   r   r   r<   rM   r>   r   r   _insert_manyr   _setdiagrL   )rW   valuesr   rY   rZ   	broadcast	max_indexro   rp   r   	n_samplesoffsetsr   rv   r\   r]   rk   r`   r`   ra   rQ    sd   





z_cs_matrix._setdiagc                 C   sp   |  | j\}}dd }ttj|| jjd }ttj|| jjd }||| ||| ||||fS )Nc                 S   sR   |   }||krtd| d| d|  }|| k r'td| d| dd S )Nzindex (z) out of range (>= ry   z) out of range (< -)rF   
IndexErrorr   )r=   boundrw   r`   r`   ra   check_bounds  s   
z1_cs_matrix._prepare_indices.<locals>.check_boundsr,   )rC   rD   rG   rM  rR   r=   r-   r   )rW   ro   rp   rY   rZ   rY  r`   r`   ra   _prepare_indices  s   

z_cs_matrix._prepare_indicesc           
   	   C   s(  |  ||\}}}}ttj|| jd }|j}tj|| jjd}t	||| j
| j||||}|dkrE|   t	||| j
| j|||| d|vrP|| j|< dS td| jj dtdd |dk}	||	 | j||	 < |	 }	||	 }||d	k   |7  < ||	 }||d	k   |7  < | ||||	  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.r1   rz   r   )rZ  rG   rM  rR   r-   r   r   r   r=   r   r<   rM   r>   r   rP   rQ   r	   rP  )
rW   ro   rp   r   rY   rZ   rU  rV  r   rv   r`   r`   ra   rB    s4   
z_cs_matrix._set_manyc              	   C   s   |  ||\}}}}t|}tj|| jjd}t||| j| j||||}|dkr9|   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)
rZ  rJ   rG   r   r=   r-   r   r<   rM   r>   )rW   ro   rp   rY   rZ   rU  rV  r   r`   r`   ra   rG    s   z_cs_matrix._zero_manyc                 C   s  t j|dd}|j|dd}|j|dd}|j|dd}| j}| j| j| jf| jd |j d}t j| j|d| _t j| j|d| _t j||d}t j||d}g }g }t j	|dd	\}	}
t 
|
t|}
t |
}d
}tt|	|
|
dd D ]z\}\}}}| j| }| j| }|
| j||  |
| j||  t j	||| ddd dd	\}}t||| kr|
|||  |
|||  n&|
||| ddd |  |
||| ddd |  t|||< |}qx| j| }|
| j|d  |
| j|d  t || _t || _t j| jj|d}|d
|d
< t | j}||	  |7  < ||dd< t j||d| _|rMd| _|   | j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)r|   clip)moderb   r)   r,   T)return_indexr   r   Nr(  Fr7   )rG   r8  takehas_sorted_indicesrE   r=   r<   r   rR   uniqueappendrJ   ri   	enumeratezipr>   concatenater   r0   r)  sort_indicesrV   )rW   ro   rp   r   r   do_sortr[   indices_parts
data_partsui	ui_indptrnew_nnzsprevrK  iijsjer0  r1  uj	uj_indptrnnzsindptr_diffr`   r`   ra   rP  3  s\   	
&

$  
z_cs_matrix._insert_manyc                 C   s   | j dkr|  }| j|j|jff|j|dS | | j\}}| j}tjt	|| jj
d}t|| j| | ||f}| j| j|f| j|| j
dS )Nr   r6   r,   r3   )rT   r   rK   r>   r=   r0   rC   rG   r   rJ   r-   r   	expandptrr<   )rW   r4   r   r   	minor_dimminor_indicesmajor_indicesr   r`   r`   ra   r     s   
z_cs_matrix.tocooc                 C   s   |d u r|d u r|  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)rC   _process_toarray_argsr   r   f_contiguousrO   r   r   r   rD   r   r<   r=   r>   )rW   r   r  r   r   rY   rZ   r`   r`   ra   r     s   z_cs_matrix.toarrayc                 C   s4   |  | j\}}t||| j| j| j |   dS )z\Remove zero entries from the array/matrix

        This is an *in place* operation.
        N)rC   rD   r   csr_eliminate_zerosr<   r=   r>   r~   rW   rY   rZ   r`   r`   ra   r     s   z_cs_matrix.eliminate_zerosreturnc                 C   sJ   t | ddsd| _| jS t| ds"ttt| jd | 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_formatrJ   r<   r=   has_canonical_formatrq   r`   r`   ra   r    s   
z_cs_matrix.has_canonical_formatr"  c                 C   s   t || _|rd| _d S d S )NT)r  r  r`  rW   r"  r`   r`   ra   r    s   

c                 C   sL   | j rdS |   | | j\}}t||| j| j| j | 	  d| _ dS )zfEliminate duplicate entries by adding them together

        This is an *in place* operation.
        NT)
r  rf  rC   rD   r   csr_sum_duplicatesr<   r=   r>   r~   r}  r`   r`   ra   rM     s   
z_cs_matrix.sum_duplicatesc                 C   s2   t | dsttt| jd | 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_indicesrJ   r<   r=   r  rq   r`   r`   ra   r`    s   
	z_cs_matrix.has_sorted_indicesc                 C   s   t || _d S r$  )r  r  r  r`   r`   ra   r`    s   c                 C   s   |   }|  |S )z?Return a copy of this array/matrix with sorted indices
        )r4   rf  )rW   Ar`   r`   ra   sorted_indices  s   z_cs_matrix.sorted_indicesc                 C   s4   | j stt| jd | j| j| j d| _ dS dS )z9Sort the indices of this array/matrix *in place*
        r   TN)r`  r   csr_sort_indicesrJ   r<   r=   r>   rq   r`   r`   ra   rf    s   
z_cs_matrix.sort_indicesc                 C   s   |  | jd }t| j|d 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)	rC   rD   rJ   r<   rO   r=   r   r>   r   )rW   r   r`   r`   ra   r~     s   z_cs_matrix.prunec                 G   s  t || jd}t| drB| j\}}t|d |\}}t|d |\}}|s'|r2td| j d| | jd | | jd | }}	n| t|dkrL|nd|d f\}}| | j	\}}	||k r| j
d | j|  | _
| jd | j|  | _| jd |d  | _n||krt| j|d | _| j|d d  | j|  ||	k r| j
|k }
t|
s| j
|
 | _
| j|
 | _| tj|
\}}| jd || jdd  |< tj| j| jd || _d S )Nr'   r  r   r   zshape must be divisible into z blocks. Got r(  )r    rB   r  r  divmodrO   r0   rC   rJ   rD   r=   r<   r>   rG   resizer   rs   r  r  r)  r?   )rW   r0   bmbnnew_Mrmnew_NrnrY   rZ   rv   r  r"  r`   r`   ra   r    s:   

 &


z_cs_matrix.resizec                 C   sH   |r| j || j | j f| j|jdS | j || j| jf| j|jd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/   )rP   r=   r4   r<   r0   r-   )rW   r>   r4   r`   r`   ra   r   =  s   z_cs_matrix._with_datac                 C   s   |  |}tt| j| | j }| j|j }| j| j| j|j|jf|d}tj	| jj
|d}tj	||d}g d}||v rFtj	|tjd}	ntj	|t| j|jd}	| j\}
}||
|tj| j|dtj| j|d| jtj|j|dtj|j|d|j|||	 | j |	||f| j
d}|  |S )z5apply the binary operation fn to two sparse matrices.r)   r,   )r   r   r   r   r   r   )rP   r   r   r:   r   rE   r<   r=   rG   r   r0   r   r   r-   rD   rR   r>   r~   )rW   r   r   r   maxnnzr[   r<   r=   bool_opsr>   rY   rZ   r  r`   r`   ra   r   K  s6   

	z_cs_matrix._binoptc                 C   s   |j | j kr
td| |d}t|jtjrHtj| j | jd}|tj	 |
 }| jdkr5|d f}d||< | }|j||j< | |S |}|S )z?
        Divide this matrix by a second sparse matrix.
        r   _eldiv_r,   r   rb   r   )r0   rO   r   rG   
issubdtyper-   inexactr   r   nannonzerorT   r   r>   r   r   )rW   r   rJ  r  r   r`   r`   ra   _divide_sparsen  s   


z_cs_matrix._divide_sparsec                 C   s(  | j |kr|r|  S | S t|| jd}t| j ||kr tdt| j dkrdt|dkrd|   | jdkr?| j	|| j
ddS |d }t|| jd }td|}td|g}| j|||f|ddS | j}t|dkryt|}td	| d
| jdkr| j	|| j
ddS |   | |\}	}| |\}
}tdd |D rt|	| | jd }tt||	}tdt|d |}nG|
dkr||krt| j|	}t| j|	}tdt|d t| j}n"|dkr	|
|	kr	t| j|}tt|t| j}| j| }| j	|||f|ddS )Nr'   zcannot be broadcastr   r   F)r-   r4   r   r.   z0CSR/CSC broadcast_to cannot have shape >2D. Got Dc                 s   s    | ]}|d kV  qdS r  r`   )rn   sr`   r`   ra   	<genexpr>  s    z+_cs_matrix._broadcast_to.<locals>.<genexpr>)r0   r4   r    rB   r"   rO   rJ   rM   r   rP   r-   rG   fullr>   r   rN   _csr_containerrD   rC   rs   r   r=   r   r<   )rW   r0   r4   rZ   r>   r=   r<   	old_shaperT   rY   r   r   r`   r`   ra   _broadcast_to  sH   



z_cs_matrix._broadcast_to)NNFr$  )T)r   )NNNF)NN)@rQ   
__module____qualname____doc__r9   rl   r   rm   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  rD  rF  rL  rQ  rZ  rB  rG  rP  r   r   r   propertyr  r  setterrM   r`  r  rf  r~   r  r   r   r  r  r`   r`   r`   ra   r$      s    
X



C	$"#

<






 (
'
D'
L


		
#
#r$   Fc                 C   sb   ddl m}m} |r|n|}t| }t|d}tj|d |d}|dd }|| ||f||fd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_matrixr)   r,   Nrb   r   )_csrr  r  rJ   r!   rG   r   )r>   r   r  r  rZ   r[   r<   r=   r`   r`   ra   r     s   
r   c                 C   s   | d u rd|}}||fS t | tr+| |\}}}|dkr"tdt||}||fS t| rW| dk r7| |7 } | | d }}|dk sF||krStd| d| d| ||fS td)Nr   r   z$slicing with step != 1 not supportedzindex out of bounds: 0 <= z < z <= zexpected slice or scalar)r@   r.  r=   rO   r   r   rW  	TypeError)slnumr<  r=  strider`   r`   ra   r;    s"   



r;  r  )1r  __all__warningsr   rt   r   numpyrG   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#   r$   r   r;  r`   r`   r`   ra   <module>   s6    (@           
0