a
    CCCfa                     @   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 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)_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_d{d4d5Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d|d@dAZ"dBdC Z#d}dDdEZ$d~dFdGZ%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-ddXdYZ.ej.je._ddZd[Z/ej/je/_d\d] Z0e1e2d^d_d`Z3e3j4e2dadbd`Z3dcdd Z5e1e2d^dedfZ6e6j4e2dadgdfZ6dhdi Z7djdk Z8dldm Z9dndo Z:ej:je:_ddpdq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|rt|| _	| 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rPt|}| j||f|d	d
}|snt}tj|||d| _tj|||d| _tj|
||d| _ntd| j dn|zt|}W n> ty } z$d| j d}t||W Y d }~n
d }~0 0 | j||d}|| j}	|	\| _| _| _| _	|d urLt|| _	nr| jd u rz t| jd }| j d }W n0 ty } ztd|W Y d }~nd }~0 0 t| ||f| _	|d ur| jj|dd| _| jdd d S )Nmaxvalr   )defaultr   dtype   shaper&      T)r#   Zcheck_contentscopyr&   zunrecognized z_matrix constructor usagez!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_compressedr   array
ValueErrorasarray	Exceptionastypecheck_format)selfZarg1r)   r&   r,   MN	idx_dtypecooarraysr5   r4   r3   r#   emsg	major_dim	minor_dim rR   T/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/sparse/_compressed.pyr0      sp    







 
 
z_cs_matrix.__init__c                 C   s   |d u rt | jd S |dk r&|d7 }| |d| f\}}| | j\}}|dkrhtjt| j|dS |dkr|t| jS t	dd S )Nr   r'   r   )Z	minlengthzaxis out of bounds)
intr3   r>   r)   r;   Zbincountr   r4   diffrC   )rH   axis_rJ   rR   rR   rS   _getnnzn   s    z_cs_matrix._getnnzTc                 C   s  |  d\}}|  | j\}}| jjjdkrFtd| jjj ddd | jjjdkrntd| jjj ddd | jj	| jj	| jj	fD ]}|dkrt
d	qt| j|d krt
d
t| j|d | jd dkrt
dt| jt| jkrt
d| jd t| jkrt
d|   |r| jdkr| j |krRt
| d| | j dk rpt
| dt| j dk rt
d| | j| jf}tj| j|d| _tj| j|d| _t| j| _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 sizerT   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)r>   r)   r3   r&   kindr   namer4   r5   ndimrC   r?   r1   prunennzr:   minr;   rV   r9   rD   r   )rH   r/   Z
major_nameZ
minor_namerP   rQ   xrK   rR   rR   rS   rG      sH    
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.
        Tr-   )sum_duplicates
_with_datar5   eliminate_zeros)rH   otheropresrR   rR   rS   _scalar_binopt   s    z_cs_matrix._scalar_binoptc                 C   s  t |rxt|r$| j| jtjdS |dkrhtdtdd | tj| jtjd}| 	|t
j}|| S | 	|t
jS nt|r|  |kS t|rtS t|rtdtdd | j|jkrdS | j|jkr|| j}| |d}| tj| jtjd}|| S tS d S )	Nr%   r   zOComparing a sparse matrix with 0 using == is inefficient, try using != instead.r*   r^   zHComparing sparse matrices using == is inefficient, try using != instead.F_ne_)r   r;   isnan	__class__r)   bool_r   r   onesrm   operatorneeqr   todenser    NotImplementedr   r1   r2   _binopt)rH   rj   all_trueinvrl   rR   rR   rS   __eq__   s8    

z_cs_matrix.__eq__c                 C   s   t |rt|r<tdtdd | tj| jtjd}|S |dkrtdtdd | jt| jtjd}| 	|t
j}|| S | 	|t
jS n`t|r|  |kS t|rtS t|r| j|jkrdS | j|jkr|| j}| |dS tS d S )	Nz:Comparing a sparse matrix with nan using != is inefficientr*   r^   r%   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Trn   )r   r;   ro   r   r   rp   rr   r)   rq   rm   rs   ru   rt   r   rv   r    rw   r   r1   r2   rx   )rH   rj   ry   rz   rR   rR   rS   __ne__   s4    
z_cs_matrix.__ne__c                 C   s0  t |r~d|kr"|dv r"tdnZ|d|rpt|tdd tj| jt|d}|| | 	|}| 
||S | ||S nt|r||  |S t|r(| j|jkrtdn| j|jkr|| j}|dvr| 
||S td	tdd | 	tj| jtjd}| 
||d
krdnd}|| S tS d S )Nr   )_le__ge_z >= and <= don't work with 0.r*   r^   r%   inconsistent shapes)r~   r}   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r}   _gt__lt_)r   NotImplementedErrorr   r   r;   emptyr)   Zresult_typefillrp   rx   rm   r   rv   r   rC   r1   r2   rr   rq   rw   )rH   rj   rk   op_nameZbad_scalar_msg	other_arrry   rl   rR   rR   rS   _inequality  s4    





z_cs_matrix._inequalityc                 C   s   |  |tjddS )Nr   zgComparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.)r   rs   ltrH   rj   rR   rR   rS   __lt__8  s    z_cs_matrix.__lt__c                 C   s   |  |tjddS )Nr   zdComparing a sparse matrix with a scalar less than zero using > is inefficient, try using <= instead.)r   rs   gtr   rR   rR   rS   __gt__>  s    z_cs_matrix.__gt__c                 C   s   |  |tjddS )Nr}   zgComparing a sparse matrix with a scalar greater than zero using <= is inefficient, try using > instead.)r   rs   ler   rR   rR   rS   __le__D  s    z_cs_matrix.__le__c                 C   s   |  |tjddS )Nr~   zdComparing a sparse matrix with a scalar less than zero using >= is inefficient, try using < instead.)r   rs   ger   rR   rR   rS   __ge__J  s    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	rt|n|j
}t||| j| j| j| | j|dd	S )
NzIncompatible shapes (z and r]   ZCFr   T)r&   orderr,   Fr-   )r)   rC   r   r&   charr>   r;   rB   flagsc_contiguousTr   r3   r4   r5   
_container)rH   rj   r&   r   resultrI   rJ   yrR   rR   rS   
_add_denseT  s    z_cs_matrix._add_densec                 C   s   |  |dS )NZ_plus_rx   r   rR   rR   rS   _add_sparse_  s    z_cs_matrix._add_sparsec                 C   s   |  |dS )NZ_minus_r   r   rR   rR   rS   _sub_sparseb  s    z_cs_matrix._sub_sparsec                 C   sZ  t |r| |S t|rX| j|jkr>| |}| |dS |jdkrTtdn|jdkrp| | d S | jdkr||  d S | jd dkr|jd dkr| 	|
 S | jd dkr|jd dkr|	| 
 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rP| jd |jd krP| j|   dgf| jd | jd fd}|	|S tdt|}|jd	kr~t|  |S |jdkr|jtjkrtS | |jd S | jdkrt|  d |S |  }| j|jkr t|j||j|jf }nD| jd dkr|jd dkr0t|j|}n<|jd | jd krdt|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r|jd dkrt|jd
d
d
f |}n@|jd | jd krFt|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r| jd |jd krt|j|d
d
|jf  }nH|jd dkr<| jd |jd kr<t|j||j  }ntd|tj  |_|S )zVPoint-wise multiplication by another array/matrix, vector, or
        scalar.
        Z_elmul_r   z+broadcast from a 1d array not yet supported)r   r   )r   r   r   r)   r   r'   NFr)   r,   )!r   Z_mul_scalarr   r)   rp   rx   rb   	TypeErrortoarray_matmul_sparsetocscZ_dia_containerravelrC   r;   Z
atleast_2dmultiplysizer&   Zobject_rw   Zflattocoor5   rZ   colrepeataranger?   tiler@   viewZndarray)rH   rj   r,   retr5   rZ   r   rR   rR   rS   r   e  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%   Z_matvec)r)   r;   r<   r   r&   r   getattrr   r1   r3   r4   r5   )rH   rj   rI   rJ   r   fnrR   rR   rS   _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%   Z_matvecs)r)   r;   r<   r   r&   r   r   r   r1   r3   r4   r5   r   )rH   rj   rI   rJ   Zn_vecsr   r   rR   rR   rS   _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   Z_matmat_maxnnzr%   r"   r   Z_matmatr   )r)   r>   rp   r9   r3   r4   r   r   r1   r;   rD   r   r   r&   r5   )rH   rj   rI   ZK1ZK2rJ   Z
major_axisrK   r   rd   r3   r4   r5   rR   rR   rS   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%   Z	_diagonalr   )r)   r;   r   r5   r&   r   r   r1   re   r:   r   r3   r4   )rH   krowscolsr   r   rR   rR   rS   diagonal  s    
 z_cs_matrix.diagonalc                 C   s   t |r||rVtdtdd tj| jt|jd}|| | 	|}| 
||S |   || jt|}| j	|| j| jf|j| jd}|S n2t|r||  |S t|r| 
||S tdd S )NzITaking maximum (minimum) with > 0 (< 0) number results to a dense matrix.r*   r^   r%   )r&   r)   zOperands not compatible.)r   r   r   r;   r   r)   rD   r&   r   rp   rx   rg   r5   r4   r3   r   rv   r   rC   )rH   rj   Znpopr   Zdense_checkr   Znew_datamatrR   rR   rS   _maximum_minimum-  s*    

z_cs_matrix._maximum_minimumc                 C   s   |  |tjddd S )NZ	_maximum_c                 S   s   t | dkS Nr   r;   rD   rf   rR   rR   rS   <lambda>F      z$_cs_matrix.maximum.<locals>.<lambda>)r   r;   maximumr   rR   rR   rS   r   D  s    
z_cs_matrix.maximumc                 C   s   |  |tjddd S )NZ	_minimum_c                 S   s   t | dk S r   r   r   rR   rR   rS   r   L  r   z$_cs_matrix.minimum.<locals>.<lambda>)r   r;   minimumr   rR   rR   rS   r   J  s    
z_cs_matrix.minimumc                 C   s   t | ds|| dd v rt| j}tjt| jd |d}| tj	\}}|||< | 
|}|d dkrr|j}|dur|j|jkrtd|jd	||d
S tj| |||d
S 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   rT   )r   r'   r   r   r%   r'   Nzdimensions do not matchrR   )rW   r&   out)hasattrr>   r   r&   r;   r<   r?   r3   _minor_reduceadd_ascontainerr   r)   rC   sumr   )rH   rW   r&   r   Z	res_dtyper   major_indexvaluerR   rR   rS   r   T  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)r5   r;   ZflatnonzerorV   r3   Zreduceatr   )rH   Zufuncr5   r   r   rR   rR   rS   r   q  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   r3   r4   r5   r   r&   )
rH   rZ   r   rI   rJ   majorminorr3   r4   r5   rR   rR   rS   _get_intXint  s    
z_cs_matrix._get_intXintc                 C   sF   |  ||f\}}|jdv r6|jdv r6| j||ddS | ||S )N)r   NTr-   )r>   step_get_submatrix_major_slice_minor_slicerH   rZ   r   r   r   rR   rR   rS   _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r| |S | ||jS )Nr%   r   )r4   r&   r>   r)   r;   rD   r   r   r   r3   r5   r   rb   r   rp   Zreshape)	rH   rZ   r   rK   rI   rJ   r   r   valrR   rR   rS   _get_arrayXarray  s    

z_cs_matrix._get_arrayXarrayc                 C   s"   |  ||f\}}| ||S N)r>   _major_index_fancy_minor_index_fancyr   rR   rR   rS   _get_columnXarray  s    z_cs_matrix._get_columnXarrayc                 C   s  |  | j| jf}tj||d }| | j\}}t|}| ||f}|dkrb| 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   rT   Fr-   r   )r9   r3   r4   r;   rD   r   r>   r)   r?   rp   r&   rF   r<   cumsumr   r   r5   )rH   idxrK   r4   rX   rJ   rI   	new_shaperow_nnz
res_indptrrd   res_indicesres_datarR   rR   rS   r     s2    
z_cs_matrix._major_index_fancyc              	   C   s  |t dkr|r|  S | S | | j\}}||\}}}tt|||}| ||f}|dkrr| j|| jdS || }	}
|dkr|dkrd}
|d |d  }}| j	||| | j	|	|
|  }| jj}t
j|d |d}t
j||dd d |dkr<t | j	| | j	| }t
j| j| |d}t
j| j| |d}nB|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%   rT   r   r   r-   Fr   )slicer,   r>   r)   r4   r?   rangerp   r&   r3   r;   r<   r   rB   r5   r   r   )rH   r   r,   rI   rJ   startstopr   r   Zstart0Zstop0Zstart1Zstop1r   rK   r   Zall_idxr   r   rd   rR   rR   rS   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r| 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.
        Fr-   r%   r   rT   r   )r9   r4   r3   rF   r;   rD   r   r>   r)   r?   rp   r&   r<   Z
empty_liker   argsortr   r   r5   )rH   r   rK   r4   r3   rI   rJ   r   r   Zcol_offsetsr   Z	col_orderrd   r   r   rR   rR   rS   r     s>    
z_cs_matrix._minor_index_fancyc                 C   s   |t dkr|r|  S | S | | j\}}||\}}}tt|||}|dkrn| j| ||f| jdS |dkr| j	||dS | 
t|||S )z@Index along the minor axis where idx is a slice object.
        Nr   r%   r   )r   r,   )r   r,   r>   r)   r4   r?   r   rp   r&   r   r   r;   r   )rH   r   r,   rI   rJ   r   r   r   rR   rR   rS   r     s    z_cs_matrix._minor_slicec              
   C   s   |  | j\}}t||\}}t||\}}	|dkr\|dkr\||kr\|	|kr\|rX|  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,   )
r>   r)   _process_slicer,   r   r3   r4   r5   rp   r&   )rH   r   r   r,   rI   rJ   i0i1Zj0Zj1r3   r4   r5   r)   rR   rR   rS   r   -  s     
z_cs_matrix._get_submatrixc                 C   s$   |  ||f\}}| ||| d S r   r>   	_set_manyrH   rZ   r   rf   r\   jrR   rR   rS   _set_intXint@  s    z_cs_matrix._set_intXintc                 C   s$   |  ||f\}}| ||| d S r   r   r   rR   rR   rS   _set_arrayXarrayD  s    z_cs_matrix._set_arrayXarrayc                 C   s  | j | ||f  |j\}}|dko2|jd dk}|dkoH|jd dk}|j|j }}	tj|j| jd}|j	dkrxd S |rt
t|t|}t|	|}	t||}|rt
||}tt|t|	}	t
||}| |||	f |||	f f\}
}| |
|| d S )Nr   r   r%   )
_zero_manyr>   r)   rZ   r   r;   rD   r5   r&   r   r   r   r?   r   r   )rH   rZ   r   rf   rI   rJ   Zbroadcast_rowZbroadcast_colrcr\   r   rR   rR   rS   _set_arrayXarray_sparseH  s$    

"z"_cs_matrix._set_arrayXarray_sparsec              	   C   s\  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}nR|rt||| }nt||| t|}tj|| jjd}tj||| | jjd}|s|d t| }ttj	|| jd
 }	|	 j | j kr$t|	|j }	|	jdkr4d 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|| < n2|  }||| || j}|\| _| _| _}d S )Nr   r%   r   rT   gMbP?)r)   rb   re   r?   r;   r   r4   r&   
atleast_1drD   r   ZsqueezeZbroadcast_tor   r>   r   r   r3   rg   r5   r   rd   _insert_manyr   _setdiagrA   )rH   valuesr   rI   rJ   	broadcastZ	max_indexr\   r   rf   	n_samplesoffsetsr   maskrL   rM   rX   rR   rR   rS   r   a  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 rBtd||f d S )Nzindex (%d) out of range (>= %d)zindex (%d) out of range (< -%d))r:   
IndexErrorre   )r4   boundr   rR   rR   rS   check_bounds  s    
z1_cs_matrix._prepare_indices.<locals>.check_boundsr%   )r>   r)   r;   r   rD   r4   r&   r   )rH   r\   r   rI   rJ   r   rR   rR   rS   _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r|   t	||| j
| j|||| d|vr|| 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   rT   NzZChanging the sparsity structure of a {}_matrix is expensive. lil_matrix is more efficient.r*   r^   r   )r   r;   r   rD   r&   r   r   r   r4   r   r3   rg   r5   r   r1   r   r   )
rH   r\   r   rf   rI   rJ   r   r   r   r   rR   rR   rS   r     s8    
z_cs_matrix._set_manyc              	   C   s   |  ||\}}}}t|}tj|| jjd}t||| j| j||||}|dkrr|   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   rT   N)
r   r?   r;   r   r4   r&   r   r3   rg   r5   )rH   r\   r   rI   rJ   r   r   r   rR   rR   rS   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 ]\}\}}}| j| }| j| }|
| j||  |
| j||  t j	||| ddd dd	\}}t||| kr|
|||  |
|||  nL|
||| ddd |  |
||| ddd |  t|||< |}q| j| }|
| j|d  |
| j|d  t || _t || _t j| jj|d}|d
|d
< t | j}||	  |7  < ||dd< t j||d| _|rd| _|   | 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)r`   Zclip)moderT   r"   r%   T)Zreturn_indexr   r   Nr   Fr.   )r;   r   Ztakehas_sorted_indicesr9   r4   r3   r   rD   uniqueappendr?   rV   	enumeratezipr5   Zconcatenater   r)   r   sort_indicesrG   )rH   r\   r   rf   r   Zdo_sortrK   Zindices_partsZ
data_partsZuiZ	ui_indptrZnew_nnzsprevr   iijsZjer   r   ZujZ	uj_indptrZnnzsZindptr_diffrR   rR   rS   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+   )r>   r)   r4   r;   r   r?   r&   r   Z	expandptrr3   r@   r5   )rH   r,   rP   rQ   Zminor_indicesZmajor_indicesZcoordsrR   rR   rS   r   =  s    z_cs_matrix.tocooc                 C   s   |d u r|d u r|  dd }| ||}|jjsB|jjsBtd|jjrX|  }|}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_contiguousrC   Ztocsrr   r   r)   r   r3   r4   r5   )rH   r   r   rf   r   rI   rJ   rR   rR   rS   r   J  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>   r)   r   Zcsr_eliminate_zerosr3   r4   r5   rc   rH   rI   rJ   rR   rR   rS   ri   a  s
    z_cs_matrix.eliminate_zeros)returnc                 C   sF   t | ddsd| _n,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_formatr?   r3   r4   has_canonical_formatrH   rR   rR   rS   r  k  s    
z_cs_matrix.has_canonical_format)r   c                 C   s   t || _|rd| _d S )NT)r  r  r   rH   r   rR   rR   rS   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  r  r>   r)   r   Zcsr_sum_duplicatesr3   r4   r5   rc   r  rR   rR   rS   rg     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   Zcsr_has_sorted_indicesr?   r3   r4   r
  r  rR   rR   rS   r     s    	
z_cs_matrix.has_sorted_indicesc                 C   s   t || _d S r   )r  r
  r  rR   rR   rS   r     s    c                 C   s   |   }|  |S )z?Return a copy of this array/matrix with sorted indices
        )r,   r  )rH   ArR   rR   rS   sorted_indices  s    z_cs_matrix.sorted_indicesc                 C   s0   | j s,tt| jd | j| j| j d| _ dS )z9Sort the indices of this array/matrix *in place*
        r   TN)r   r   Zcsr_sort_indicesr?   r3   r4   r5   r  rR   rR   rS   r    s
    z_cs_matrix.sort_indicesc                 C   s   |  | jd }t| j|d kr*tdt| j| jk rBtdt| j| jk rZt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>   r)   r?   r3   rC   r4   rd   r5   r   )rH   rP   rR   rR   rS   rc     s    z_cs_matrix.prunec                 G   s  t |}t| drz| j\}}t|d |\}}t|d |\}}|sH|rZtd| j|| jd | | jd |  }}	n| |\}}| | j\}}	||k r| jd | j	|  | _| j
d | j	|  | _
| j	d |d  | _	n<||krt| 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   divmodrC   r1   r)   r>   r4   r3   r5   r;   resizer   allr   r   r   r6   )rH   r)   ZbmZbnZnew_MZrmZnew_NZrnrI   rJ   r   r   r   rR   rR   rS   r    s:    

 


z_cs_matrix.resizec                 C   sL   |r*| j || j | j f| j|jdS | j || j| jf| j|jdS 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(   N)rp   r4   r,   r3   r)   r&   )rH   r5   r,   rR   rR   rS   rh     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 rt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%   )rn   r   r   r}   r~   r   r   r   )rp   r   r   r1   rd   r9   r3   r4   r;   r   r)   rq   r   r&   rD   r5   rc   )rH   rj   rk   r   ZmaxnnzrK   r3   r4   Zbool_opsr5   r  rR   rR   rS   rx     s4    
	z_cs_matrix._binoptc                 C   s   |j | j krtd| |d}t|jtjrtj| j | jd}|tj	 |
 \}}d|||f< | }|j||j|jf< | |}n|}|S )z?
        Divide this matrix by a second sparse matrix.
        r   Z_eldiv_r%   r   )r)   rC   rx   r;   Z
issubdtyper&   Zinexactr   r   nanZnonzeror   r5   rZ   r   r   )rH   rj   r   r   rZ   r   rR   rR   rS   _divide_sparse)  s    z_cs_matrix._divide_sparse)NNF)N)T)r   )NNN)N)F)F)NNF)T)NN)T)>__name__
__module____qualname____doc__r0   rY   r   rG   rm   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   ri   propertyr  r  setterrg   r   r  r  rc   r  rh   rx   r  rR   rR   rR   rS   r!      s   
S


G	$"#
x$






!
('

B'L




	"

"r!   c                 C   s   | d u rd| }}nt | trJ| |\}}}|dkr>tdt||}nRt| r| dk rb| |7 } | | d  }}|dk s||krtd|||f ntd||fS )Nr   r   z$slicing with step != 1 not supportedz'index out of bounds: 0 <= %d < %d <= %dzexpected slice or scalar)r7   r   r4   rC   re   r   r   r   )slnumr   r   ZstriderR   rR   rS   r   E  s"    
r   ),r  __all__warningsr   rs   numpyr;   Zscipy._lib._utilr   r   _baser   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   rR   rR   rR   rS   <module>   s.   (8          9