o
    5Bih                     @   s   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 deeeZ+dd Z,dS )z>Base class for sparse matrix formats using compressed storage.    )warnN)_prune_arraycopy_if_needed   )_spbaseissparseSparseEfficiencyWarningsparray)_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                   @   sd  e Zd ZdZdvddZdwddZejje_dxd	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dyd*d+Zejje_d,d- Zd.d/ Zejje_d0d1 Zejje_dzd2d3Zejje_dwd4d5Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d{d@dAZ"dBdC Z#d{dDdEZ$dvdFdGZ%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dxdXdYZ.ej.je._d|dZd[Z/ej/je/_d\d] Z0e1d^e2fd_d`Z3e3j4dae2fdbd`Z3dcdd Z5e1d^e2fdedfZ6e6j4dae2fdgdfZ6dhdi Z7djdk Z8dldm Z9dndo Z:ej:je:_dxdpdqZ;drds Z<dtdu Z=dS )}
_cs_matrixzY
    base array/matrix class for compressed row- and column-oriented arrays/matrices
    NFc              
   C   s   t |  t|r/|j| jkr|r| }n|| j}|j|j|j|j	f\| _| _| _| _	nt
|trt|rot|| _	| j\}}| jt||d}tdt|td| _td|| _tj| ||fd d |d| _nt|dkr| j|||d}|| j}	|	\| _| _| _| _	|   nt|dkr|\}
}}d }|d urt|}| j||f|d	d
}|st}tj|||d| _tj|||d| _tj|
||d| _n[td| j dzt|}W n ty } zd| j d}t||d }~ww t
| tr|j dk r| jdkrtd|j  d| j||d}|| j}	|	\| _| _| _| _	|d ur7t|| _	n3| jd u rjzt| jd }| j d }W n ty_ } ztd|d }~ww t| ||f| _	|d urx| jj!|dd| _| j"dd d S )Nmaxvalr   )defaultr   dtype   shaper'      T)r$   check_contentscopyr'   zunrecognized z_matrix constructor usagecsczCSC arrays don't support zD input. Use 2Dz!unable to infer matrix dimensionsFr.   
full_check)#r
   __init__r   formatr.   asformatindptrindicesdata_shape
isinstancetupler   r    r*   _get_index_dtypemaxnpzerosr   float_swaplen_coo_container_coo_to_compressedsum_duplicatesr   array
ValueErrorasarray	Exceptionr	   ndimastypecheck_format)selfarg1r*   r'   r.   MN	idx_dtypecooarraysr8   r7   r6   r$   emsg	major_dim	minor_dim rX   X/var/www/html/Trade-python/venv/lib/python3.10/site-packages/scipy/sparse/_compressed.pyr3      s   









$


z_cs_matrix.__init__c                 C   s   |d u rt | jd S |dk r|d7 }| |d| f\}}| | j\}}|dkr4tjt| j|dS |dkr>t| jS t	d)Nr   r(   r   )	minlengthzaxis out of bounds)
intr6   rA   r*   r>   bincountr   r7   diffrG   )rM   axis_rP   rX   rX   rY   _getnnzs   s   z_cs_matrix._getnnzTc                 C   s  |  d\}}|  | j\}}| jjjdkr#td| jjj ddd | jjjdkr7td| jjj ddd | jj	| jj	| jj	fD ]
}|dkrLt
d	qBt| j|d krct
d
t| j|d | jd dkrnt
dt| jt| jkr|t
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`.
        )rowcolumniz$indptr array has non-integer dtype ()r+   
stacklevelz%indices array has non-integer dtype (r   z'data, indices, and indptr should be 1-Dz&index pointer size ({}) should be ({})r   z!index pointer should start with 0z*indices and data should have the same sizerZ   zQLast value of index pointer should be less than the size of index and data arraysz index values must be < z index values must be >= 0z8index pointer values must form a non-decreasing sequencer&   N)rA   r*   r6   r'   kindr   namer7   r8   rJ   rG   rB   r4   prunennzr=   minr>   r^   r<   rH   r   )rM   r2   
major_name
minor_namerV   rW   xrQ   rX   rX   rY   rL      sL   
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.
        Tr0   )rE   
_with_datar8   eliminate_zeros)rM   otheropresrX   rX   rY   _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.r+   rf   zHComparing sparse matrices using == is inefficient, try using != instead.F_ne_)r   r>   isnan	__class__r*   bool_r   r   onesru   operatorneeqr   todenser!   NotImplementedr   r4   r5   _binopt)rM   rr   all_trueinvrt   rX   rX   rY   __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 inefficientr+   rf   r&   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Trv   )r   r>   rw   r   r   rx   rz   r*   ry   ru   r{   r}   r|   r   r~   r!   r   r   r4   r5   r   )rM   rr   r   r   rX   rX   rY   __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.r+   rf   r&   inconsistent shapes)r   r   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r   _gt__lt_)r   NotImplementedErrorr   r   r>   emptyr*   result_typefillrx   r   ru   r   r~   r   rG   r4   r5   rz   ry   r   )rM   rr   rs   op_namebad_scalar_msg	other_arrr   rt   rX   rX   rY   _inequality  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rM   rr   rX   rX   rY   __lt__=     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   rX   rX   rY   __gt__C  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   rX   rX   rY   __le__I  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   rX   rX   rY   __ge__O  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j	r:|n|j
}t||| j| j| j| | j|dd	S )
NzIncompatible shapes (z and re   CFr   T)r'   orderr.   Fr0   )r*   rG   r   r'   charrA   r>   rF   flagsc_contiguousTr   r6   r7   r8   
_container)rM   rr   r'   r   resultrO   rP   yrX   rX   rY   
_add_denseY  s   z_cs_matrix._add_densec                 C      |  |dS )N_plus_r   r   rX   rX   rY   _add_sparsed     z_cs_matrix._add_sparsec                 C   r   )N_minus_r   r   rX   rX   rY   _sub_sparseg  r   z_cs_matrix._sub_sparsec                 C   sJ  t |r	| |S t|r$| j|jkr| |}| |dS |jdkr(td|jdkr6| | d S | jdkrD||  d S | jd dkrY|jd dkrY| 	|
 S | jd dkrn|jd dkrn|	| 
 S |jd dkr| jd |jd kr| j|  dgf|jd |jd fd}| 	|S | jd dkr| jd |jd kr| j|   dgf| jd | jd fd}|	|S |jd dkr| jd |jd kr| j|  dgf|jd |jd fd}|	| S | jd dkr | jd |jd kr | j|   dgf| jd | jd fd}|	|S tdt|}|jd	kr7t|  |S |jdkrN|jtjkrFtS | |jd S | jdkr^t|  d |S |  }| j|jkrxt|j||j|jf }n"| jd dkr|jd dkrt|j|}n|jd | jd krt|j|d
d
|jf }ntdtt|jd t|j}t|j|jd }| j|tj  ||ff|jd | jd fddS | jd dkrR|jd dkrt|jd
d
d
f |}n |jd | jd krt|jd
d
d
f ||j }ntdt|j|jd }tt|jd t|j}| j|tj  ||ff| jd |jd fddS |jd dkrv| jd |jd krvt|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 )zVPoint-wise multiplication by another array/matrix, vector, or
        scalar.
        _elmul_r   z+broadcast from a 1d array not yet supported)r   r   )r   r   r   r*   r   r(   NFr*   r.   )!r   _mul_scalarr   r*   rx   r   rJ   	TypeErrortoarray_matmul_sparsetocsc_dia_containerravelrG   r>   
atleast_2dmultiplysizer'   object_r   flattocoor8   rb   colrepeatarangerB   tilerC   viewndarray)rM   rr   r.   retr8   rb   r   rX   rX   rY   r   j  s   





"
"
"
&

"&"&z_cs_matrix.multiplyc                 C   sT   | j \}}tj|t| jj|jjd}tt| jd }|||| j	| j
| j|| |S )Nr&   _matvec)r*   r>   r?   r   r'   r   getattrr   r4   r6   r7   r8   )rM   rr   rO   rP   r   fnrX   rX   rY   _matmul_vector  s   
z_cs_matrix._matmul_vectorc              
   C   sl   | j \}}|j d }tj||ft| jj|jjd}tt| jd }||||| j	| j
| j| |  |S )Nr   r&   _matvecs)r*   r>   r?   r   r'   r   r   r   r4   r6   r7   r8   r   )rM   rr   rO   rP   n_vecsr   r   rX   rX   rY   _matmul_multivector  s   


z_cs_matrix._matmul_multivectorc                 C   sj  | j \}}|j \}}| ||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}	| 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|
|| | j|||
f||fdS )Nr   _matmat_maxnnzr&   r#   r   _matmatr   )r*   rA   rx   r<   r6   r7   r   r   r4   r>   rH   r   r   r'   r8   )rM   rr   rO   K1K2rP   
major_axisrQ   r   rk   r6   r7   r8   rX   rX   rY   r     s@   


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   )r*   r>   r   r8   r'   r   r   r4   rl   r=   r   r6   r7   )rM   krowscolsr   r   rX   rX   rY   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.r+   rf   r&   )r'   r*   zOperands not compatible.)r   r   r   r>   r   r*   rH   r'   r   rx   r   rE   r8   r7   r6   r   r~   r   rG   )rM   rr   npopr   dense_checkr   new_datamatrX   rX   rY   _maximum_minimum2  s*   

z_cs_matrix._maximum_minimumc                 C      |  |tjddd S )N	_maximum_c                 S   s   t | dkS Nr   r>   rH   ro   rX   rX   rY   <lambda>K      z$_cs_matrix.maximum.<locals>.<lambda>)r   r>   maximumr   rX   rX   rY   r   I     
z_cs_matrix.maximumc                 C   r   )N	_minimum_c                 S   s   t | dk S r   r   r   rX   rX   rY   r   Q  r   z$_cs_matrix.minimum.<locals>.<lambda>)r   r>   minimumr   rX   rX   rY   r   O  r   z_cs_matrix.minimumc                 C   s   t | dsO|| dd v rOt| j}tjt| jd |d}| tj	\}}|||< | 
|}|d dkr9|j}|durG|j|jkrGt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.
        	blocksize))r   rZ   )r   r(   r   r   r&   r(   Nzdimensions do not matchrX   )r_   r'   out)hasattrrA   r   r'   r>   r?   rB   r6   _minor_reduceadd_ascontainerr   r*   rG   sumr   )rM   r_   r'   r   	res_dtyper   major_indexvaluerX   rX   rY   r   Y  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)r8   r>   flatnonzeror^   r6   reduceatr   )rM   ufuncr8   r   r   rX   rX   rY   r   v  s   z_cs_matrix._minor_reducec           
      C   s\   |  | j\}}|  ||f\}}t||| j| j| j||d ||d 	\}}}	|	j| jdS )Nr   r&   )rA   r*   r   r6   r7   r8   r   r'   )
rM   rb   r   rO   rP   majorminorr6   r7   r8   rX   rX   rY   _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   NTr0   )rA   step_get_submatrix_major_slice_minor_slicerM   rb   r   r   r   rX   rX   rY   _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   )r7   r'   rA   r*   r>   rH   r   r   r   r6   r8   r   rJ   r   rx   reshape)	rM   rb   r   rQ   rO   rP   r   r   valrX   rX   rY   _get_arrayXarray  s   

z_cs_matrix._get_arrayXarrayc                 C   s"   |  ||f\}}| ||S N)rA   _major_index_fancy_minor_index_fancyr   rX   rX   rY   _get_columnXarray  s   z_cs_matrix._get_columnXarrayc                 C   s  |  | j| jf}tj||d }| | j\}}t|}| ||f}|dkr1| 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   Nr   rZ   Fr0   r   )r<   r6   r7   r>   rH   r   rA   r*   rB   rx   r'   rK   r?   cumsumr   r   r8   )rM   idxrQ   r7   r`   rP   rO   	new_shaperow_nnz
res_indptrrk   res_indicesres_datarX   rX   rY   r     s2   
z_cs_matrix._major_index_fancyc              	   C   s  |t dkr|r|  S | S | | j\}}||\}}}tt|||}| ||f}|dkr9| j|| jdS ||}	}
|dkrH|dkrHd}
|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&   rZ   r   r  r0   Fr   )slicer.   rA   r*   r7   rB   rangerx   r'   r6   r>   r?   r  rF   r8   r   r   )rM   r  r.   rO   rP   startstopr   r  start0stop0start1stop1r  rQ   r  all_idxr	  r
  rk   rX   rX   rY   r     s>   
z_cs_matrix._major_slicec              	   C   s"  |  | j| jf}| jj|dd}| jj|dd}tj||d }| | j\}}t	|}| ||f}|dkrA| 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.
        Fr0   r&   r   rZ   r   )r<   r7   r6   rK   r>   rH   r   rA   r*   rB   rx   r'   r?   
empty_liker   argsortr   r   r8   )rM   r  rQ   r7   r6   rO   rP   r   r  col_offsetsr  	col_orderrk   r	  r
  rX   rX   rY   r    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   r.   )r  r.   rA   r*   r7   rB   r  rx   r'   r   r  r>   r   )rM   r  r.   rO   rP   r  r  r   rX   rX   rY   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|||
f|| j	ddS )zbReturn a submatrix of this matrix.

        major, minor: None, int, or slice with step 1
        r   F)r*   r'   r.   )
rA   r*   _process_slicer.   r   r6   r7   r8   rx   r'   )rM   r   r   r.   rO   rP   i0i1j0j1r6   r7   r8   r*   rX   rX   rY   r   2  s    
z_cs_matrix._get_submatrixc                 C   $   |  ||f\}}| ||| d S r   rA   	_set_manyrM   rb   r   ro   rd   jrX   rX   rY   _set_intXintE     z_cs_matrix._set_intXintc                 C   r  r   r  r   rX   rX   rY   _set_arrayXarrayI  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_manyrA   r*   rb   r   r>   rH   r8   r'   r   r   r   rB   r   r  )rM   rb   r   ro   rO   rP   broadcast_rowbroadcast_colrcrd   r!  rX   rX   rY   _set_arrayXarray_sparseM  s$   

"z"_cs_matrix._set_arrayXarray_sparsec              	   C   sV  d| j v rd S | j \}}|jdk}|dk r@|rt|| |}n
t|| |t|}tj| || | jjd}tj|| jjd}n)|rJt||| }n
t||| t|}tj|| jjd}tj||| | jjd}|ss|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d k r|| }|| }| |||	|  | }|	| | j|| < d S |  }||| || j}|\| _| _| _}d S )Nr   r&   r   rZ   gMbP?)r*   rJ   rl   rB   r>   r   r7   r'   
atleast_1drH   r   squeezebroadcast_tor   rA   r   r   r6   rE   r8   r   rk   _insert_manyr   _setdiagrD   )rM   valuesr   rO   rP   	broadcast	max_indexrd   r!  ro   	n_samplesoffsetsr   maskrR   rS   r`   rX   rX   rY   r/  f  s`   




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   sF   |   }||krtd||f |  }|| k r!td||f d S )Nzindex (%d) out of range (>= %d)zindex (%d) out of range (< -%d))r=   
IndexErrorrl   )r7   boundr  rX   rX   rY   check_bounds  s   
z1_cs_matrix._prepare_indices.<locals>.check_boundsr&   )rA   r*   r>   r+  rH   r7   r'   r   )rM   rd   r!  rO   rP   r8  rX   rX   rY   _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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   rZ   NzZChanging the sparsity structure of a {}_matrix is expensive. lil_matrix is more efficient.r+   rf   r   )r9  r>   r+  rH   r'   r   r   r   r7   r   r6   rE   r8   r   r4   r   r.  )
rM   rd   r!  ro   rO   rP   r3  r4  r   r5  rX   rX   rY   r    s6   
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   rZ   N)
r9  rB   r>   r   r7   r'   r   r6   rE   r8   )rM   rd   r!  rO   rP   r3  r4  r   rX   rX   rY   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.
        	mergesort)rh   clip)moderZ   r#   r&   T)return_indexr   r   Nr  Fr1   )r>   r  takehas_sorted_indicesr<   r7   r6   r   rH   uniqueappendrB   r^   	enumeratezipr8   concatenater   r*   r  sort_indicesrL   )rM   rd   r!  ro   r   do_sortrQ   indices_parts
data_partsui	ui_indptrnew_nnzsprevr)  iijsjer  r  uj	uj_indptrnnzsindptr_diffrX   rX   rY   r.    s\   	
&

$  
z_cs_matrix._insert_manyc                 C   sf   |  | j\}}| j}tjt|| jjd}t|| j	| |  ||f}| j
| j|f| j|| jdS )Nr&   r-   )rA   r*   r7   r>   r   rB   r'   r   	expandptrr6   rC   r8   )rM   r.   rV   rW   minor_indicesmajor_indicescoordsrX   rX   rY   r   B  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)rA   _process_toarray_argsr   r   f_contiguousrG   tocsrr   r   r*   r   r6   r7   r8   )rM   r   r   ro   r   rO   rP   rX   rX   rY   r   O  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)rA   r*   r   csr_eliminate_zerosr6   r7   r8   rj   rM   rO   rP   rX   rX   rY   rq   f  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_formatrB   r6   r7   has_canonical_formatrM   rX   rX   rY   rc  p  s   
z_cs_matrix.has_canonical_formatr   c                 C   s   t || _|rd| _d S d S )NT)ra  r`  r?  rM   r   rX   rX   rY   rc    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)
rc  rE  rA   r*   r   csr_sum_duplicatesr6   r7   r8   rj   r]  rX   rX   rY   rE     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   ra  r   csr_has_sorted_indicesrB   r6   r7   r_  rd  rX   rX   rY   r?    s   
	z_cs_matrix.has_sorted_indicesc                 C   s   t || _d S r   )ra  r_  re  rX   rX   rY   r?    s   c                 C   s   |   }|  |S )z?Return a copy of this array/matrix with sorted indices
        )r.   rE  )rM   ArX   rX   rY   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_indicesrB   r6   r7   r8   rd  rX   rX   rY   rE    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)	rA   r*   rB   r6   rG   r7   rk   r8   r   )rM   rV   rX   rX   rY   rj     s   z_cs_matrix.prunec                 G   s  t |}t| dr=| j\}}t|d |\}}t|d |\}}|s$|r-td| j|| jd | | jd | }}	n| |\}}| | j\}}	||k rq| 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   z.shape must be divisible into {} blocks. Got {}r  )r    r   r   divmodrG   r4   r*   rA   r7   r6   r8   r>   resizer   allr   r   r  r9   )rM   r*   bmbnnew_Mrmnew_NrnrO   rP   r5  r   r   rX   rX   rY   rl    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)   )rx   r7   r.   r6   r*   r'   )rM   r8   r.   rX   rX   rY   rp     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
d | j
d 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&   )rv   r   r   r   r   r   r   r   )rx   r   r   r4   rk   r<   r6   r7   r>   r   r*   ry   r   r'   rH   r8   rj   )rM   rr   rs   r   maxnnzrQ   r6   r7   bool_opsr8   rh  rX   rX   rY   r     s4   
	z_cs_matrix._binoptc                 C   s   |j | j kr
td| |d}t|jtjrGtj| j | jd}|tj	 |
 \}}d|||f< | }|j||j|jf< | |}|S |}|S )z?
        Divide this matrix by a second sparse matrix.
        r   _eldiv_r&   r   )r*   rG   r   r>   
issubdtyper'   inexactr   r   nannonzeror   r8   rb   r   r   )rM   rr   r(  r   rb   r   rX   rX   rY   _divide_sparse.  s   
z_cs_matrix._divide_sparse)NNFr   )T)r   )NNN)F)NN)>__name__
__module____qualname____doc__r3   ra   r   rL   ru   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/  r9  r  r%  r.  r   r   rq   propertyra  rc  setterrE   r?  ri  rE  rj   rl  rp   r   r{  rX   rX   rX   rY   r"      s    

X

G	$"#
x
$






!(
'
B'
L



	
"
"r"   c                 C   s   | d u rd|}}||fS t | tr+| |\}}}|dkr"tdt||}||fS t| rS| dk r7| |7 } | | d }}|dk sF||krOtd|||f ||fS td)Nr   r   z$slicing with step != 1 not supportedz'index out of bounds: 0 <= %d < %d <= %dzexpected slice or scalar)r:   r  r7   rG   rl   r   r6  r   )slnumr  r  striderX   rX   rY   r  J  s&   


r  )-r  __all__warningsr   r{   numpyr>   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  rX   rX   rX   rY   <module>   s0    (8          >