U
    À»|e°Ç  ã                   @   sð   d Z g ZddlmZ ddlZddlZddl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(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é   )ÚspmatrixÚ
isspmatrixÚ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Úget_index_dtypeÚdowncast_intp_indexÚget_sum_dtypeÚcheck_shapeÚis_pydata_spmatrixc                   @   sZ  e Zd ZdZdydd„Zdzdd„Zejje_d{dd	„Z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.d/„ Zd0d1„ Zejje_d2d3„ Zejje_d~d4d5„Zejje_dd6d7„Zd8d9„ Zd:d;„ Zd<d=„ Z d>d?„ Z!d@dA„ Z"d€dBdC„Z#dDdE„ Z$d�dFdG„Z%d‚dHdI„Z&dJdK„ Z'dLdM„ Z(dNdO„ Z)dPdQ„ Z*dRdS„ Z+dTdU„ Z,dVdW„ Z-dXdY„ Z.dƒdZd[„Z/ej/je/_d„d\d]„Z0ej0je0_d^d_„ Z1d`da„ Z2dbdc„ Z3e4e2e3dd�Z5dedf„ Z6dgdh„ Z7didj„ Z8e4e7e8dd�Z9dkdl„ Z:dmdn„ Z;dodp„ Z<dqdr„ Z=ej=je=_d…dsdt„Z>dudv„ Z?dwdx„ Z@dS )†Ú
_cs_matrixzBbase matrix class for compressed row- and column-oriented matricesNFc              
   C   s�  t  | ¡ t|ƒrF|j| jkr,|r,| ¡ }n| | j¡}|  |¡ �n˜t|tƒ�r~t	|ƒrÆt
|ƒ| _| j\}}tt||ƒd�}t dt|td�¡| _t d|¡| _tj|  ||f¡d d |d�| _n¶t|ƒdkrô|  | j|||d�¡}|  |¡ nˆt|ƒdk�rl|\}	}
}d }|d k	�r"t|ƒ}t|
|f|d	d
�}tj|
||d�| _tj|||d�| _tj|	||d�| _ntd | j¡ƒ‚n`zt |¡}W n6 tk
�rÂ } ztd | j¡ƒ|‚W 5 d }~X Y nX |  |  | j||d�¡¡ |d k	�rôt
|ƒ| _np| jd k�rdz t| jƒd }| j ¡ d }W n. tk
�rN } ztdƒ|‚W 5 d }~X Y nX t
|  ||f¡ƒ| _|d k	�r€| jj|dd�| _| jdd� d S )N©Úmaxvalr   )Údefaultr   ©Údtypeé   ©Úshaper&   é   T)r#   Úcheck_contents©Úcopyr&   z(unrecognized {}_matrix constructor usagez!unable to infer matrix dimensionsF©r-   ©Ú
full_check) r   Ú__init__r   Úformatr-   ÚasformatÚ	_set_selfÚ
isinstanceÚtupler   r   Ú_shaper)   r   ÚmaxÚnpÚzerosr   ÚfloatÚdataÚindicesÚ_swapÚindptrÚlenÚ	__class__Ú_coo_containerÚarrayÚ
ValueErrorÚasarrayÚ	ExceptionÚastypeÚcheck_format)ÚselfÚarg1r)   r&   r-   ÚMÚNÚ	idx_dtypeÚotherr<   r=   r?   r#   ÚeÚ	major_dimÚ	minor_dim© rR   úU/var/www/website-v5/atlas_env/lib/python3.8/site-packages/scipy/sparse/_compressed.pyr1      s~    



ÿ
ÿ

þÿÿÿÿÿ
ÿ

z_cs_matrix.__init__c                 C   sˆ   |d k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 )Néÿÿÿÿr   r'   r   )Ú	minlengthzaxis out of bounds)
Úintr?   r>   r)   r9   Úbincountr   r=   ÚdiffrD   )rI   ÚaxisÚ_rL   rR   rR   rS   Úgetnnzl   s    ÿz_cs_matrix.getnnzc                 C   s4   |r|  ¡ }|j| _|j| _|j| _t|jƒ| _dS )z:take the member variables of other and assign them to selfN)r-   r<   r=   r?   r   r)   r7   )rI   rN   r-   rR   rR   rS   r4   }   s    z_cs_matrix._set_selfTc                 C   sÒ  |   d¡\}}|   | j¡\}}| jjjdkrDtd | jjj¡dd� | jjjdkrjtd | jjj¡dd� t	| j| jfƒ}t
j| j|d�| _t
j| j|d�| _t| jƒ| _| jj| jj| jjfD ]}|dkrÀtd	ƒ‚qÀt| jƒ|d k�rtd
 t| jƒ|d ¡ƒ‚| jd dk�rtdƒ‚t| jƒt| jƒk�r:tdƒ‚| jd t| jƒk�rXtdƒ‚|  ¡  |�rÎ| jdk�rÎ| j ¡ |k�r’td ||¡ƒ‚| j ¡ dk �r°td |¡ƒ‚t
 | j¡ ¡ dk �rÎtdƒ‚dS )z÷check whether the matrix format is valid

        Parameters
        ----------
        full_check : bool, optional
            If `True`, rigorous check, O(N) operations. Otherwise
            basic check, O(1) operations (default True).
        )ÚrowÚcolumnÚiz'indptr array has non-integer dtype ({})r*   ©Ú
stacklevelz(indices array has non-integer dtype ({})r%   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 sequenceN)r>   r)   r?   r&   Úkindr   r2   Únamer=   r   r9   rE   r   r<   ÚndimrD   r@   ÚpruneÚnnzr8   ÚminrX   )rI   r0   Z
major_nameZ
minor_namerP   rQ   rM   ÚxrR   rR   rS   rH   ˆ   s\    
ÿÿÿÿ
 ÿ ÿÿz_cs_matrix.check_formatc                 C   s*   |   ¡  | j|| j|ƒdd�}| ¡  |S )z‰Scalar version of self._binopt, for cases in which no new nonzeros
        are added. Produces a new spmatrix in canonical form.
        Tr.   )Úsum_duplicatesÚ
_with_datar<   Úeliminate_zeros)rI   rN   Ú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 n�t|ƒrŒ|  ¡ |kS t|ƒr˜tS t|ƒ�rtdtdd� | j|jkrÀdS | j|jkrØ| | j¡}|  |d¡}|  tj| jtjd�¡}|| S d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   r9   ÚisnanrA   r)   Úbool_r   r   Úonesrm   ÚoperatorÚneÚeqr   Útodenser    ÚNotImplementedr   r2   r3   Ú_binopt)rI   rN   Úall_trueÚinvrl   rR   rR   rS   Ú__eq__×   s:    
þ
 ÿz_cs_matrix.__eq__c                 C   sô   t |ƒr�t |¡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 n`t|ƒr¤|  ¡ |kS t|ƒr°tS t|ƒrì| j|jkrÈdS | j|jkrà| | j¡}|  |d¡S d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   r9   ro   r   r   rA   rq   r)   rp   rm   rr   rt   rs   r   ru   r    rv   r   r2   r3   rw   )rI   rN   rx   ry   rR   rR   rS   Ú__ne__û   s8    
 ÿ þz_cs_matrix.__ne__c                 C   s,  t |ƒrzd|kr"|dkr"tdƒ‚nV|d|ƒrlt|tƒ tj| jt |¡d�}| |¡ |  	|¡}|  
||¡S |  ||¡S n®t|ƒr�||  ¡ |ƒS t|ƒ�r | j|jkr°tdƒ‚n| j|jkrÈ| | j¡}|dkrÜ|  
||¡S tdtƒ |  	tj| jtjd�¡}|  
||dk�rd	nd
¡}|| S tdƒ‚d S )Nr   )Ú_le_Ú_ge_z >= and <= don't work with 0.r%   úinconsistent shapes)r}   r|   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r|   Ú_gt_Ú_lt_zOperands could not be compared.)r   ÚNotImplementedErrorr   r   r9   Úemptyr)   Úresult_typeÚfillrA   rw   rm   r   ru   r   rD   r2   r3   rq   rp   )rI   rN   rk   Úop_nameZbad_scalar_msgÚ	other_arrrx   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‡   rr   Últ©rI   rN   rR   rR   rS   Ú__lt__?  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‡   rr   Úgtr‰   rR   rR   rS   Ú__gt__E  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‡   rr   Úler‰   rR   rR   rS   Ú__le__K  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‡   rr   Úger‰   rR   rR   rS   Ú__ge__Q  s    ÿz_cs_matrix.__ge__c                 C   sš   |j | j kr td | j |j ¡ƒ‚t| jj|jjƒ}|  d¡d }tj|||dd�}|  | j ¡\}}|j	j
rn|n|j}t||| j| j| j|ƒ | j|dd�S )NzIncompatible shapes ({} and {})ÚCFr   T)r&   Úorderr-   Fr.   )r)   rD   r2   r   r&   Úcharr>   r9   rC   ÚflagsÚc_contiguousÚTr   r?   r=   r<   Ú
_container)rI   rN   r&   r’   ÚresultrK   rL   ÚyrR   rR   rS   Ú
_add_dense[  s     ÿz_cs_matrix._add_densec                 C   s   |   |d¡S )NZ_plus_©rw   r‰   rR   rR   rS   Ú_add_sparseg  s    z_cs_matrix._add_sparsec                 C   s   |   |d¡S )NZ_minus_r›   r‰   rR   rR   rS   Ú_sub_sparsej  s    z_cs_matrix._sub_sparsec                 C   s2  t |ƒr|  |¡S t|ƒ�rB| j|jkr>|  |¡}|  |d¡S |jdkrZ|  | ¡ d ¡S | jdkrv| |  ¡ 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�r:| jd |jd k�r:| j	|  ¡  
¡ dgf| jd | jd fd�}| |¡S tdƒ‚t |¡}|jdk�rht |  ¡ |¡S |jdk�r„|  |jd ¡S | jdk�r¤t |  ¡ d |¡S |  ¡ }| j|jk�rØt |j||j|jf ¡}�nD| jd dk�rª|jd dk�rt |j|¡}n<|jd | jd k�r<t |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�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�rÔ| jd |jd k�rÔt |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 )zPPoint-wise multiplication by another matrix, vector, or
        scalar.
        Z_elmul_)r   r   )r   r   r   r   ©r)   r~   r'   NF©r)   r-   )r   Ú_mul_scalarr   r)   rA   rw   ÚtoarrayÚ_mul_sparse_matrixÚtocscÚ_dia_containerÚravelrD   r9   Ú
atleast_2drc   ÚmultiplyÚsizeÚflatÚtocoor<   r\   ÚcolÚrepeatÚaranger@   ÚtilerB   ÚviewÚndarray)rI   rN   r-   Úretr<   r\   r«   rR   rR   rS   r§   m  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)   r9   r:   r   r&   r“   Úgetattrr
   r2   r?   r=   r<   )rI   rN   rK   rL   r˜   ÚfnrR   rR   rS   Ú_mul_vectorà  s    
ÿz_cs_matrix._mul_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)   r9   r:   r   r&   r“   r²   r
   r2   r?   r=   r<   r¥   )rI   rN   rK   rL   Zn_vecsr˜   r³   rR   rR   rS   Ú_mul_multivectorí  s    


ÿ ÿz_cs_matrix._mul_multivectorc                 C   sf  | j \}}|j \}}|  ||f¡d }|  |¡}t| 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�ƒ}	t| 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>   rA   r   r?   r=   r²   r
   r2   r9   rE   r‚   r   r&   r<   )rI   rN   rK   ZK1ZK2rL   Z
major_axisrM   r³   re   r?   r=   r<   rR   rR   rS   r¢   û  sH    



 ÿü
 ÿþ  úz_cs_matrix._mul_sparse_matrixr   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)   r9   r‚   r<   r&   r²   r
   r2   rf   r8   r   r?   r=   )rI   Ú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   r9   r‚   r)   rE   r&   r„   rA   rw   rh   r<   r=   r?   r   ru   r   rD   )rI   rN   Znpopr…   Zdense_checkr†   Znew_dataÚmatrR   rR   rS   Ú_maximum_minimum0  s,    þ

 ÿz_cs_matrix._maximum_minimumc                 C   s   |   |tjddd„ ¡S )NZ	_maximum_c                 S   s   t  | ¡dkS ©Nr   ©r9   rE   ©rg   rR   rR   rS   Ú<lambda>I  ó    z$_cs_matrix.maximum.<locals>.<lambda>)r»   r9   Úmaximumr‰   rR   rR   rS   rÁ   G  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¿   O  rÀ   z$_cs_matrix.minimum.<locals>.<lambda>)r»   r9   Úminimumr‰   rR   rR   rS   rÂ   M  s    
 ÿz_cs_matrix.minimumc                 C   s´   t | dƒsž||  d¡d kržt| jƒ}tjt| jƒd |d�}|  tj	¡\}}|||< |  
|¡}|d dkrr|j}|dk	rŽ|j|jkrŽtdƒ‚|jd	||d
�S tj| |||d
�S dS )z~Sum the 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   )rY   r&   Úout)Úhasattrr>   r   r&   r9   r:   r@   r?   Ú_minor_reduceÚaddÚ_ascontainerr–   r)   rD   Úsumr   )rI   rY   r&   rÄ   Ú	res_dtyper±   Úmajor_indexÚvaluerR   rR   rS   rÉ   W  s    
ÿ

z_cs_matrix.sumc                 C   s>   |dk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<   r9   ÚflatnonzerorX   r?   Úreduceatr   )rI   Úufuncr<   rË   rÌ   rR   rR   rS   rÆ   t  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   r?   r=   r<   rÉ   r&   )
rI   r\   r«   rK   rL   ÚmajorÚminorr?   r=   r<   rR   rR   rS   Ú_get_intXintŽ  s           þ
z_cs_matrix._get_intXintc                 C   sF   |   ||f¡\}}|jdkr6|jdkr6| j||dd�S |  |¡ |¡S )N)r   NTr.   )r>   ÚstepÚ_get_submatrixÚ_major_sliceÚ_minor_slice©rI   r\   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   )r=   r&   r>   r)   r9   rE   r‚   r¨   r   r?   r<   r¥   rc   rÈ   rA   Úreshape)	rI   r\   r«   rM   rK   rL   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}tj||d� ¡ }|  | j¡\}}t|ƒ}|  ||f¡}|dkrX| j|| jd�S | j	|d  | j	|  }| j j}tj
|d |d�}	tj||	dd… d� |	d }
tj|
|d�}tj|
| jd�}t||| j	| j | 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=   r&   r9   rE   r¥   r>   r)   r@   rA   r?   r:   Úcumsumr‚   r   r<   )rI   ÚidxrM   r=   rZ   rL   rK   Ú	new_shapeÚrow_nnzÚ
res_indptrre   Úres_indicesÚres_datarR   rR   rS   rÝ   °  s,     ÿ ÿ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r�d}
|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)   r=   r@   ÚrangerA   r&   r?   r9   r:   rá   rC   r<   r‚   r   )rI   râ   r-   rK   rL   ÚstartÚstoprÓ   rã   Zstart0Zstop0Zstart1Zstop1rä   rM   rå   Zall_idxræ   rç   re   rR   rR   rS   rÕ   Ê  sD    
ÿ
  ÿ ÿz_cs_matrix._major_slicec              	   C   sú   | j j}tj||d� ¡ }|  | j¡\}}t|ƒ}|  ||f¡}|dkrX| j|| jd�S tj	||d�}t 
| j¡}t||||| j| j ||ƒ t |¡j|dd�}	|d }
tj|
|d�}tj|
| jd�}t|	|t| j ƒ| j | j||ƒ | j|||f|dd�S )zBIndex along the minor axis where idx is an array of ints.
        r%   r   Fr.   rT   rŸ   )r=   r&   r9   rE   r¥   r>   r)   r@   rA   r:   Ú
empty_liker?   r   ÚargsortrG   r‚   r   r<   )rI   râ   rM   rK   rL   r¶   rã   Zcol_offsetsrå   Z	col_orderre   ræ   rç   rR   rR   rS   rÞ   ò  s6     ÿ   ÿ ÿ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)   r=   r@   ré   rA   r&   rÔ   rÞ   r9   r­   )rI   râ   r-   rK   rL   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   r?   r=   r<   rA   r&   )rI   rÐ   rÑ   r-   rK   rL   Úi0Úi1Zj0Új1r?   r=   r<   r)   rR   rR   rS   rÔ     s*             ÿ
 ÿz_cs_matrix._get_submatrixc                 C   s$   |   ||f¡\}}|  |||¡ d S rÜ   ©r>   Ú	_set_many©rI   r\   r«   rg   r^   ÚjrR   rR   rS   Ú_set_intXint1  s    z_cs_matrix._set_intXintc                 C   s$   |   ||f¡\}}|  |||¡ d S rÜ   rò   rô   rR   rR   rS   Ú_set_arrayXarray5  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 |rªt 
t |¡t|ƒ¡}t |	|¡}	t ||¡}|rÜt 
||¡}t t |¡t|	ƒ¡}	t 
||¡}|  |||	f |||	f f¡\}
}|  |
||¡ d S )Nr   r   r%   )Ú
_zero_manyr>   r)   r\   r«   r9   rE   r<   r&   r¨   r¬   r­   r@   r®   ró   )rI   r\   r«   rg   rK   rL   Zbroadcast_rowZbroadcast_colÚrÚcr^   rõ   rR   rR   rS   Ú_set_arrayXarray_sparse9  s$    

"z"_cs_matrix._set_arrayXarray_sparsec           	      C   sø   d| j k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�}||8 }nT|r”t||| ƒ}nt||| t|ƒƒ}tj|| jjd�}tj|| jjd�}||7 }|sè|d t|ƒ… }|| ||f< d S )Nr   r%   )r)   rc   rf   r@   r9   r­   r=   r&   )	rI   Úvaluesr¶   rK   rL   Ú	broadcastÚ	max_indexr^   rõ   rR   rR   rS   Ú_setdiagR  s(    



z_cs_matrix._setdiagc                 C   sl   |   | j¡\}}dd„ }tj|| jjddd� ¡ }tj|| jjdd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))r8   Ú
IndexErrorrf   )r=   Úboundrâ   rR   rR   rS   Úcheck_boundss  s    ÿ
ÿz1_cs_matrix._prepare_indices.<locals>.check_boundsFr   ©r&   r-   Úndmin)r>   r)   r9   rC   r=   r&   r¥   )rI   r^   rõ   rK   rL   r  rR   rR   rS   Ú_prepare_indicesp  s    


z_cs_matrix._prepare_indicesc           
   	   C   s"  |   ||¡\}}}}tj|| jddd� ¡ }|j}tj|| jjd�}t||| j	| j||||ƒ}|dkrˆ|  
¡  t||| j	| j||||ƒ d|krž|| j|< dS td | j¡tdd	� |dk}	||	 | j||	 < |	 }	||	 }||d
k   |7  < ||	 }||d
k   |7  < |  ||||	 ¡ dS )z�Sets value at each (i, j) to x

        Here (i,j) index major and minor respectively, and must not contain
        duplicate entries.
        Fr   r  r%   rT   NzZChanging the sparsity structure of a {}_matrix is expensive. lil_matrix is more efficient.r*   r_   r   )r  r9   rC   r&   r¥   r¨   r‚   r=   r   r?   rh   r<   r   r2   r   Ú_insert_many)
rI   r^   rõ   rg   rK   rL   Ú	n_samplesÚoffsetsr±   ÚmaskrR   rR   rS   ró   ƒ  sB      ÿ  ÿ
ÿ þ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 )z‚Sets 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@   r9   r‚   r=   r&   r   r?   rh   r<   )rI   r^   rõ   rK   rL   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}t| 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�| _|�ršd| _|  ¡  | 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)ra   Úclip)ÚmoderT   r"   r%   T)Úreturn_indexr   r   Nrà   Fr/   )r9   rí   ÚtakeÚhas_sorted_indicesr   r=   r?   r¨   rE   ÚuniqueÚappendr@   rX   Ú	enumerateÚzipr<   Úconcatenater‚   r)   rá   Úsort_indicesrH   )rI   r^   rõ   rg   r’   Zdo_sortrM   Zindices_partsZ
data_partsÚ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   sn   |   | j¡\}}| j}tjt|ƒ| jjd�}t || j	|¡ |   ||f¡\}}| j
| j||ff| j|| jd�S )Nr%   r,   )r>   r)   r=   r9   r‚   r@   r&   r
   Ú	expandptrr?   rB   r<   )rI   r-   rP   rQ   Zminor_indicesZmajor_indicesr\   r«   rR   rR   rS   rª   
  s      þz_cs_matrix.tocooc                 C   s’   |d kr|d k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>   Ú_process_toarray_argsr”   r•   Úf_contiguousrD   Útocsrr£   r–   r)   r   r?   r=   r<   )rI   r’   rÄ   rg   r™   rK   rL   rR   rR   rS   r¡     s    z_cs_matrix.toarrayc                 C   s4   |   | j¡\}}t ||| j| j| j¡ |  ¡  dS )zVRemove zero entries from the matrix

        This is an *in place* operation.
        N)r>   r)   r
   Úcsr_eliminate_zerosr?   r=   r<   rd   ©rI   rK   rL   rR   rR   rS   rj   /  s
    ÿz_cs_matrix.eliminate_zerosc                 C   sF   t | ddƒsd| _n,t| dƒs@tt t| jƒd | j| j¡ƒ| _	| jS )a^  Determine whether the 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_formatr@   r?   r=   Úhas_canonical_format©rI   rR   rR   rS   Z__get_has_canonical_format9  s    
  ÿÿz%_cs_matrix.__get_has_canonical_formatc                 C   s   t |ƒ| _|rd| _d S )NT)r#  r"  r  ©rI   rÚ   rR   rR   rS   Z__set_has_canonical_formatO  s    
z%_cs_matrix.__set_has_canonical_format)ÚfgetÚfsetc                 C   sL   | j r
dS |  ¡  |  | j¡\}}t ||| j| j| j¡ |  	¡  d| _ dS )zmEliminate duplicate matrix entries by adding them together

        This is an *in place* operation.
        NT)
r%  r  r>   r)   r
   Úcsr_sum_duplicatesr?   r=   r<   rd   r   rR   rR   rS   rh   W  s    ÿz_cs_matrix.sum_duplicatesc                 C   s2   t | dƒs,tt t| jƒd | j| j¡ƒ| _| jS )z®Determine whether the matrix has sorted indices

        Returns
            - True: if the indices of the matrix are in sorted order
            - False: otherwise

        r!  r   )rÅ   r#  r
   Úcsr_has_sorted_indicesr@   r?   r=   r!  r&  rR   rR   rS   Z__get_sortedg  s    

  ÿÿz_cs_matrix.__get_sortedc                 C   s   t |ƒ| _d S rÜ   )r#  r!  r'  rR   rR   rS   Z__set_sortedw  s    z_cs_matrix.__set_sortedc                 C   s   |   ¡ }| ¡  |S )z9Return a copy of this matrix with sorted indices
        )r-   r  )rI   Ú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 )z3Sort the indices of this matrix *in place*
        r   TN)r  r
   Úcsr_sort_indicesr@   r?   r=   r<   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@   r?   rD   r=   re   r<   r   )rI   rP   rR   rR   rS   rd   �  s    z_cs_matrix.prunec                 G   s¢  t |ƒ}t| dƒrz| j\}}t|d |ƒ\}}t|d |ƒ\}}|sH|rZtd| j|f ƒ‚| 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 %s blocks. Got %srà   )r   rÅ   rÃ   ÚdivmodrD   r)   r>   r=   r?   r<   r9   Úresizer„   ÚallrÆ   rÇ   rá   r7   )rI   r)   ÚbmÚbnZnew_MZrmZnew_NZrnrK   rL   r	  rË   rÚ   rR   rR   rS   r0  Ÿ  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 )zµReturns 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)rA   r=   r-   r?   r)   r&   )rI   r<   r-   rR   rR   rS   ri   È  s    ÿý ÿz_cs_matrix._with_datac                 C   s&  |   |¡}tt| j| | j ƒ}| j|j }t| j| j|j|jf|d�}tj	| jj
|d�}tj	||d�}dddddg}||kr�t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ž   )rA   r²   r
   r2   re   r   r?   r=   r9   r‚   r)   rp   r   r&   rE   r<   rd   )rI   rN   rk   r³   ZmaxnnzrM   r?   r=   Zbool_opsr<   r,  rR   rR   rS   rw   Ö  s:    

 ÿþ  ù	z_cs_matrix._binoptc                 C   s”   |j | j krtdƒ‚|  |d¡}t |jtj¡rŒt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)   rD   rw   r9   Ú
issubdtyper&   Úinexactr‚   r„   ÚnanÚnonzerorª   r<   r\   r«   r—   )rI   rN   rù   rÄ   r\   r«   rR   rR   rS   Ú_divide_sparseø  s    z_cs_matrix._divide_sparse)NNF)N)F)T)r   )NNN)N)F)F)NNF)T)NN)T)AÚ__name__Ú
__module__Ú__qualname__Ú__doc__r1   r[   r   r4   rH   rm   rz   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¡   rj   Z$_cs_matrix__get_has_canonical_formatZ$_cs_matrix__set_has_canonical_formatÚpropertyr%  rh   Z_cs_matrix__get_sortedZ_cs_matrix__set_sortedr  r-  r  rd   r0  ri   rw   r8  rR   rR   rR   rS   r!      sŒ   
R



F	$""
s$







(

'L




ÿ	"

"r!   c                 C   s¤   | d krd| }}nˆt | tƒrJ|  |¡\}}}|dkr>tdƒ‚t||ƒ}nRt| ƒr”| dk rb| |7 } | | d  }}|dk s€||krœt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)r5   rè   r=   rD   rf   r   r   Ú	TypeError)ÚslÚnumrï   rð   ÚstriderR   rR   rS   rî     s"    
ÿrî   ),r<  Ú__all__Úwarningsr   rr   Únumpyr9   Úscipy._lib._utilr   Ú_baser   r   r   Ú_datar   r	   Ú r
   r   r   r   r   r   r   r   r   Z_indexr   Ú_sputilsr   r   r   r   r   r   r   r   r   r   r   r   r    r!   rî   rR   rR   rR   rS   Ú<module>   s.   (<          