o
    i                     @   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%   Zallow_ndmaxvalr   )defaultr   dtype   shaper,      T)r)   Zcheck_contentscopyr,   zunrecognized z constructor input: Z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r2   
full_check)'r
   __init__r   formatr2   asformatindptrindicesdata_shape
isinstancetupler   	_allow_ndr    _swap_shape_as_2d_get_index_dtypemaxr/   npzerosr   floatlen_coo_container_coo_to_compressedsum_duplicatesr   array
ValueError	__class____name__asarray	Exceptionr   ndimastypecheck_format)selfZarg1r/   r,   r2   r&   MN	idx_dtypecooarraysr;   r:   r9   r)   eZnewdtype r[   R/home/kim/smarthome/.venv/lib/python3.10/site-packages/scipy/sparse/_compressed.pyr6      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   r]   axis out of boundsr   r-   Z	minlength)intr9   rQ   rL   r@   r/   rD   bincountr   r:   diff)rT   axis_rV   r[   r[   r\   _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   r^   r_   r   r-   r`   c                    s$   g | ]\}}t  j|| qS r[   )rD   count_nonzeror;   ).0ijrT   r[   r\   
<listcomp>   s   $ z,_cs_matrix.count_nonzero.<locals>.<listcomp>)rJ   rD   rg   r;   rQ   rL   r@   r/   allr:   rb   r   rc   r9   	itertoolspairwiserK   )rT   rd   re   rV   maskidxpairsr[   rk   r\   rg      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`.
        ri   z$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 sizer]   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)r9   r,   kindr   namer:   r;   rQ   rL   r@   rA   rG   prunennzrC   minrD   rc   rB   rO   r   )rT   r5   xrU   rV   rW   r[   r[   r\   rS      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.
        Tr3   )rJ   
_with_datar;   eliminate_zeros)rT   otheropresr[   r[   r\   _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.r0   rt   zHComparing sparse matrices using == is inefficient, try using != instead.F_ne_)r   rD   isnanrM   r/   bool_r   r	   onesr   operatorneeqr   todenser#   NotImplementedr   r7   r8   _binopt)rT   r~   all_trueinvr   r[   r[   r\   __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 inefficientr0   rt   r+   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Tr   )r   rD   r   r   r	   rM   r   r/   r   r   r   r   r   r   r   r#   r   r   r7   r8   r   )rT   r~   r   r   r[   r[   r\   __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.r0   rt   r+   inconsistent shapes)r   r   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r   _gt__lt_)r   NotImplementedErrorr   r	   rD   emptyr/   Zresult_typefillrM   r   r   r   r   r   rL   r7   r8   r   r   r   )rT   r~   r   op_nameZbad_scalar_msg	other_arrr   r   r[   r[   r\   _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rT   r~   r[   r[   r\   __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[   r\   __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[   r\   __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[   r\   __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 rs   ZCFr   T)r,   orderr2   Fr3   )r/   rL   r   r,   charr@   rD   rK   flagsc_contiguousTrA   r   r9   r:   r;   
_container)rT   r~   r,   r   resultyrU   rV   r[   r[   r\   
_add_densev  s   z_cs_matrix._add_densec                 C      |  |dS )NZ_plus_r   r   r[   r[   r\   _add_sparse     z_cs_matrix._add_sparsec                 C   r   )NZ_minus_r   r   r[   r[   r\   _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.Z_elmul_r   r   )r   r   r   r   r   )r   r   r-   r   r]   Nr(   r+   Fr3   r/   r2   )*r   Z_mul_scalarr   r/   rM   r   toarrayrQ   reshaper;   sumrA   _matmul_sparsetocscr=   r   _make_diagonal_csrravelrL   rD   Z
asanyarraymultiplysizer,   Zobject_r   r"   ZflattocoocoordsviewZndarrayZ
atleast_2dcolrB   ry   repeatarangetilerR   rH   row)rT   r~   r   ZsMZsNoMoNis_arrayZ	new_otherr2   Znew_selfZbshaperetr;   Zother2drW   r   r   r[   r[   r\   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+   Z_matvecr   r   )rA   rD   rE   r   r,   r   getattrr   r7   r9   r:   r;   rQ   )rT   r~   rU   rV   r   fnr[   r[   r\   _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 )Nr]   r+   Z_matvecsr   )rA   r/   rD   rE   r   r,   r   r   r   r7   r9   r:   r;   r   rQ   r   )rT   r~   rU   rV   Zn_vecsr   r   r[   r[   r\   _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[   Z_matmat_maxnnzr+   r(   Z_matmatr/   csr)rA   rQ   r   r/   r   r<   r@   rM   rB   r9   r:   r   r   r7   rD   rO   rK   r   r,   r   r;   tocsr)rT   r~   rU   ZK1Zo_ndimZK2rV   	new_shapeZ
faux_shape	major_dimrW   r   ry   r9   r:   r;   r   r[   r[   r\   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+   Z	_diagonalr   )r/   rD   r   r;   r,   r   r   r7   rz   rC   r   r9   r:   )rT   krowscolsr   r   r[   r[   r\   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.r0   rt   r+   )r,   r/   zOperands not compatible.)r   r   r	   rD   r   r/   rO   r,   r   rM   r   rJ   r;   r:   r9   r   r   r   rL   )rT   r~   Znpopr   Zdense_checkr   Znew_dataZmatr[   r[   r\   _maximum_minimumo  s*   

z_cs_matrix._maximum_minimumc                 C      |  |tjddd S )NZ	_maximum_c                 S   s   t | dkS Nr   rD   rO   r{   r[   r[   r\   <lambda>      z$_cs_matrix.maximum.<locals>.<lambda>)r   rD   maximumr   r[   r[   r\   r        
z_cs_matrix.maximumc                 C   r   )NZ	_minimum_c                 S   s   t | dk S r   r   r   r[   r[   r\   r     r   z$_cs_matrix.minimum.<locals>.<lambda>)r   rD   minimumr   r[   r[   r\   r     r   z_cs_matrix.minimumc                 C   s   | j dkrFt| dsF|| dd v rFt| j}tjt| jd |d}| 	tj
\}}|||< | |}|d dkr>|j}|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   r]   )r   r   r   r+   r[   )rd   r,   out)rQ   hasattrr@   r   r,   rD   rE   rG   r9   _minor_reduceadd_ascontainerr   r   r   )rT   rd   r,   r   Z	res_dtyper   major_indexvaluer[   r[   r\   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;   rD   Zflatnonzerorc   r9   Zreduceatr   )rT   Zufuncr;   r   r   r[   r[   r\   r     s   z_cs_matrix._minor_reducec           
      C   s\   |  | j\}}|  ||f\}}t||| j| j| j||d ||d 	\}}}	|	j| jdS )Nr   r+   )r@   r/   r   r9   r:   r;   r   r,   )
rT   r   r   rU   rV   majorminorr9   r:   r;   r[   r[   r\   _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   NTr3   )r@   step_get_submatrix_major_slice_minor_slicerT   r   r   r   r   r[   r[   r\   _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,   r@   r/   rD   rO   r   r   r   r9   r;   r   rQ   r   rM   r   )	rT   r   r   rW   rU   rV   r   r   valr[   r[   r\   _get_arrayXarray  s   

z_cs_matrix._get_arrayXarrayc                 C   s"   |  ||f\}}| ||S N)r@   _major_index_fancy_minor_index_fancyr   r[   r[   r\   _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   r]   Fr3   r   )rB   r9   r:   rD   rO   r   r@   rA   rG   rQ   rM   r,   rR   rE   cumsumr   r   r;   )rT   rq   rW   r:   rV   rU   r   row_nnz
res_indptrry   res_indicesres_datar[   r[   r\   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+   r]   r   r   r3   Fr   )slicer2   r@   rA   r:   rG   rangerQ   rM   r,   r9   rD   rE   r   rK   r;   r   r   )rT   rq   r2   rU   rV   startstopr   r   Zstart0Zstop0Zstart1Zstop1r   rW   r   Zall_idxr   r  ry   r[   r[   r\   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.
        Fr3   r+   r-   r   r]   r   )rB   r:   r9   rR   rD   rO   r   r@   rA   rG   rQ   rM   r,   rE   Z
empty_liker   argsortr   r   r;   )rT   rq   rW   r:   r9   rU   rV   r   r   Zcol_offsetsr   Z	col_orderry   r   r  r[   r[   r\   r   3  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   r2   )r  r2   r@   rA   r:   rG   r  rM   r,   r   r   rD   r   )rT   rq   r2   rU   rV   r  r  r   r[   r[   r\   r   Z  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)r/   r,   r2   )r@   rA   _process_slicer2   r   r9   r:   r;   rQ   rM   r,   )rT   r   r   r2   rU   rV   i0i1Zj0Zj1r9   r:   r;   r/   r[   r[   r\   r   j  s    


z_cs_matrix._get_submatrixc                 C   $   |  ||f\}}| ||| d S r   r@   	_set_manyrT   r   r   r{   ri   rj   r[   r[   r\   _set_intXint     z_cs_matrix._set_intXintc                 C   r
  r   r  r  r[   r[   r\   _set_arrayXarray  r  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_manyr@   r/   r   r   rD   rO   r;   r,   r   r   r   rG   r   r  )rT   r   r   r{   rU   rV   Zbroadcast_rowZbroadcast_colrcri   rj   r[   r[   r\   _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+   r]   gMbP?)r/   rQ   r   rz   rG   rD   r   r:   r,   
atleast_1drO   r   ZsqueezeZbroadcast_tor   r@   r   r   r9   rJ   r;   ry   r   _insert_manyr   _setdiagrI   )rT   valuesr   rU   rV   	broadcastZ	max_indexri   rj   r{   	n_samplesoffsetsr   rp   rX   rY   re   r[   r[   r\   r    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 (>= rs   z) out of range (< -)rC   
IndexErrorrz   )r:   boundrq   r[   r[   r\   check_bounds  s   
z1_cs_matrix._prepare_indices.<locals>.check_boundsr+   )r@   rA   rD   r  rO   r:   r,   r   )rT   ri   rj   rU   rV   r  r[   r[   r\   _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   r]   Nz%Changing the sparsity structure of a z. is expensive. lil and dok are more efficient.r0   rt   r   )r  rD   r  rO   r,   r   r   r   r:   r   r9   rJ   r;   r   rM   rN   r	   r  )
rT   ri   rj   r{   rU   rV   r  r  r   rp   r[   r[   r\   r    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   r]   N)
r  rG   rD   r   r:   r,   r   r9   rJ   r;   )rT   ri   rj   rU   rV   r  r  r   r[   r[   r\   r    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.
        Z	mergesort)rv   Zclip)moder]   r(   r+   T)Zreturn_indexr   r   Nr   Fr4   )rD   r  Ztakehas_sorted_indicesrB   r:   r9   r   rO   uniqueappendrG   rc   	enumeratezipr;   Zconcatenater   r/   r   sort_indicesrS   )rT   ri   rj   r{   r   Zdo_sortrW   Zindices_partsZ
data_partsZuiZ	ui_indptrZnew_nnzsprevr  iijsZjer  r  ZujZ	uj_indptrZnnzsZindptr_diffr[   r[   r\   r  0  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   r3   r+   r1   )rQ   r   rH   r;   r:   r/   r@   rD   r   rG   r,   r   Z	expandptrr9   )rT   r2   r   r   Z	minor_dimZminor_indicesZmajor_indicesr   r[   r[   r\   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)r@   Z_process_toarray_argsr   r   f_contiguousrL   r   r   r   rA   r   r9   r:   r;   )rT   r   r   r{   r   rU   rV   r[   r[   r\   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)r@   rA   r   Zcsr_eliminate_zerosr9   r:   r;   rx   rT   rU   rV   r[   r[   r\   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   Zcsr_has_canonical_formatrG   r9   r:   has_canonical_formatrk   r[   r[   r\   r1    s   
z_cs_matrix.has_canonical_formatr   c                 C   s   t || _|rd| _d S d S )NT)r0  r/  r!  rT   r   r[   r[   r\   r1    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)
r1  r&  r@   rA   r   Zcsr_sum_duplicatesr9   r:   r;   rx   r,  r[   r[   r\   rJ     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   r0  r   Zcsr_has_sorted_indicesrG   r9   r:   r.  rk   r[   r[   r\   r!    s   
	z_cs_matrix.has_sorted_indicesc                 C   s   t || _d S r   )r0  r.  r2  r[   r[   r\   r!    s   c                 C   s   |   }|  |S )z?Return a copy of this array/matrix with sorted indices
        )r2   r&  )rT   Ar[   r[   r\   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   Zcsr_sort_indicesrG   r9   r:   r;   rk   r[   r[   r\   r&    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)	r@   rA   rG   r9   rL   r:   ry   r;   r   )rT   r   r[   r[   r\   rx     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    r?   r   r   divmodrL   r/   r@   rG   rA   r:   r9   r;   rD   resizer   rm   r   r   r   r<   )rT   r/   ZbmZbnZnew_MZrmZnew_NZrnrU   rV   rp   r   r   r[   r[   r\   r6    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.   )rM   r:   r2   r9   r/   r,   )rT   r;   r2   r[   r[   r\   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   )rM   r   r   r7   ry   rB   r9   r:   rD   r   r/   r   r   r,   rA   rO   r;   rx   )rT   r~   r   r   ZmaxnnzrW   r9   r:   Zbool_opsr;   rU   rV   r3  r[   r[   r\   r   H  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   Z_eldiv_r+   r   r]   r   )r/   rL   r   rD   Z
issubdtyper,   Zinexactr   r   nanZnonzerorQ   r   r;   r   r   )rT   r~   r  r   r   r[   r[   r\   _divide_sparsek  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,   r2   r   r-   z0CSR/CSC broadcast_to cannot have shape >2D. Got Dc                 s   s    | ]}|d kV  qdS r   r[   )rh   sr[   r[   r\   	<genexpr>  s    z+_cs_matrix._broadcast_to.<locals>.<genexpr>)r/   r2   r    r?   r"   rL   rG   rJ   ry   rM   r,   rD   fullr;   r   rK   Z_csr_containerrA   r@   rm   r   r:   r   r9   )rT   r/   r2   rV   r;   r:   r9   Z	old_shaperQ   rU   r   r   r[   r[   r\   _broadcast_to  sH   



z_cs_matrix._broadcast_to)NNFr   )T)r   )NNNF)NN)@rN   
__module____qualname____doc__r6   rf   r   rg   rS   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  r   r   r}   propertyr0  r1  setterrJ   r!  r4  r&  rx   r6  r|   r   r8  r=  r[   r[   r[   r\   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+   Nr]   r   )Z_csrrD  rE  rG   r!   rD   r   )r;   r   rD  rE  rV   rW   r9   r:   r[   r[   r\   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:   rL   rz   r   r  	TypeError)slnumr  r	  Zstrider[   r[   r\   r    s"   



r  r>  )1rA  __all__warningsr   rn   r   numpyrD   Zscipy._lib._utilr   r   Z_baser   r   r   r	   _datar
   r    r   r   r   r   r   r   r   r   r   _indexr   Z_sputilsr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r   r  r[   r[   r[   r\   <module>   s6    (@           
-