a
    ˜¥»a;Ç  ã                   @   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 dd
lmZmZmZmZmZmZmZmZ ddlmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,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)Ú
dia_matrix)Ú_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ÚmatrixÚasmatrixÚ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þddlm} |  ||||d�¡}	|  |	¡ nˆt|ƒd	k�rv|\}
}}d }|d u�r,t|ƒ}t||f|d
d�}tj|||d�| _tj|||d�| _tj|
||d�| _ntd | j¡ƒ‚nlzt |¡}W n8 t�yÎ } ztd | j¡ƒ|‚W Y d }~n
d }~0 0 ddlm} |  |  |||d�¡¡ |d u�r
t
|ƒ| _nr| jd u �r|z t| jƒd }| j ¡ d }W n0 t�yf } 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é   ©Ú
coo_matrix©Úshaper)   é   T)r&   Zcheck_contents©Úcopyr)   z(unrecognized {}_matrix constructor usagez!unable to infer matrix dimensionsF©r1   ©Ú
full_check)!r   Ú__init__r   Úformatr1   ÚasformatÚ	_set_selfÚ
isinstanceÚtupler   r    Ú_shaper.   r   ÚmaxÚnpÚzerosr   ÚfloatÚdataÚindicesÚ_swapÚindptrÚlenÚcoor,   Ú	__class__ÚarrayÚ
ValueErrorÚasarrayÚ	ExceptionÚastypeÚcheck_format)ÚselfZarg1r.   r)   r1   ÚMÚNÚ	idx_dtyper,   Úotherr@   rA   rC   r&   ÚeÚ	major_dimÚ	minor_dim© rU   úQ/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/scipy/sparse/compressed.pyr5      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rhtjt| jƒ|d�S |dkr|t | j¡S t	dƒ‚d S )Néÿÿÿÿr   r*   r   )Z	minlengthzaxis out of bounds)
ÚintrC   rB   r.   r=   Zbincountr   rA   ÚdiffrH   )rM   ÚaxisÚ_rO   rU   rU   rV   Ú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)r1   r@   rA   rC   r    r.   r;   )rM   rQ   r1   rU   rU   rV   r8   }   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 sizerW   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)rB   r.   rC   r)   Úkindr   r6   ÚnamerA   r   r=   rI   r   r@   ÚndimrH   rD   ÚpruneÚnnzr<   ÚminrY   )rM   r4   Z
major_nameZ
minor_namerS   rT   rP   ÚxrU   rU   rV   rL   ˆ   sX    
ÿÿÿÿ
ÿÿÿ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.
        Tr2   )Úsum_duplicatesÚ
_with_datar@   Úeliminate_zeros)rM   rQ   ÚopÚresrU   rU   rV   Ú_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   r=   ÚisnanrF   r.   Úbool_r   r   Úonesrn   ÚoperatorÚneÚeqr   Útodenser#   ÚNotImplementedr   r6   r7   Ú_binopt)rM   rQ   Úall_trueÚinvrm   rU   rU   rV   Ú__eq__×   s8    
þ
ÿ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.Tro   )r   r=   rp   r   r   rF   rr   r.   rq   rn   rs   ru   rt   r   rv   r#   rw   r   r6   r7   rx   )rM   rQ   ry   rz   rU   rU   rV   Ú__ne__û   s4    
ÿþz_cs_matrix.__ne__c                 C   s,  t |ƒrzd|kr"|dv 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v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   r=   Úemptyr.   Zresult_typeÚfillrF   rx   rn   r   rv   r   rH   r6   r7   rr   rq   )rM   rQ   rl   Úop_nameZbad_scalar_msgÚ	other_arrry   rm   rU   rU   rV   Ú_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©rM   rQ   rU   rU   rV   Ú__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‡   rs   Úgtr‰   rU   rU   rV   Ú__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‡   rs   Úler‰   rU   rU   rV   Ú__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‡   rs   Úger‰   rU   rU   rV   Ú__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|ƒ t|dd�S )NzIncompatible shapes ({} and {})ZCFr   T)r)   Úorderr1   Fr2   )r.   rH   r6   r   r)   ÚcharrB   r=   rG   ÚflagsÚc_contiguousÚTr   rC   rA   r@   r!   )rM   rQ   r)   r‘   ÚresultrN   rO   ÚyrU   rU   rV   Ú
_add_dense[  s    ÿz_cs_matrix._add_densec                 C   s   |   |d¡S )NZ_plus_©rx   r‰   rU   rU   rV   Ú_add_sparseg  s    z_cs_matrix._add_sparsec                 C   s   |   |d¡S )NZ_minus_r™   r‰   rU   rU   rV   Ú_sub_sparsej  s    z_cs_matrix._sub_sparsec                 C   s2  t |ƒr|  |¡S t|ƒ�r:| 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$t	| ¡  
¡ dgf|jd |jd fd�}|  |¡S | jd dk�r~| jd |jd k�r~t	|  ¡  
¡ dgf| jd | jd fd�}| |¡S |jd dk�rØ| jd |jd k�rØt	| ¡  
¡ dgf|jd |jd fd�}| | ¡S | jd dk�r2| jd |jd k�r2t	|  ¡  
¡ dgf| jd | jd fd�}| |¡S tdƒ‚t |¡}|jdk�r`t |  ¡ |¡S |jdk�r||  |jd ¡S | jdk�rœt |  ¡ d |¡S dd	lm} |  ¡ }| j|jk�rÜt |j||j|jf ¡}�n@| 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 ¡}|| 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ƒ¡}|| 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*   r+   NF©r.   r1   )r   Z_mul_scalarr   r.   rF   rx   ÚtoarrayÚ_mul_sparse_matrixÚtocscr
   ÚravelrH   r=   Z
atleast_2drd   ÚmultiplyÚsizeZflatrE   r,   Útocoor@   r]   ÚcolÚrepeatÚarangerD   ÚtileÚviewZndarray)rM   rQ   r1   r,   Úretr@   r]   r¥   rU   rU   rV   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.   r=   r>   r   r)   r’   Úgetattrr   r6   rC   rA   r@   )rM   rQ   rN   rO   r–   ÚfnrU   rU   rV   Ú_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.   r=   r>   r   r)   r’   r«   r   r6   rC   rA   r@   r¡   )rM   rQ   rN   rO   Zn_vecsr–   r¬   rU   rU   rV   Ú_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.   rB   rF   r   rC   rA   r«   r   r6   r=   rI   rƒ   r   r)   r@   )rM   rQ   rN   ZK1ZK2rO   Z
major_axisrP   r¬   rf   rC   rA   r@   rU   rU   rV   rŸ   ð  s@    



ÿü
ÿþú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.   r=   rƒ   r@   r)   r«   r   r6   rg   r<   r   rC   rA   )rM   ÚkÚrowsÚcolsr¬   r—   rU   rU   rV   Ú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.   rI   r)   r„   rF   rx   ri   r@   rA   rC   r   rv   r   rH   )rM   rQ   Znpopr…   Zdense_checkr†   Znew_dataÚmatrU   rU   rV   Ú_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=   rI   ©rh   rU   rU   rV   Ú<lambda>>  ó    z$_cs_matrix.maximum.<locals>.<lambda>)r´   r=   Úmaximumr‰   rU   rU   rV   rº   <  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·   rU   rU   rV   r¸   D  r¹   z$_cs_matrix.minimum.<locals>.<lambda>)r´   r=   Úminimumr‰   rU   rU   rV   r»   B  s    
ÿz_cs_matrix.minimumc                 C   s²   t | dƒsœ||  d¡d v rœt| jƒ}tjt| jƒd |d�}|  tj	¡\}}|||< t
|ƒ}|d dkrp|j}|du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   rW   )r   r*   r   r   r(   r*   Nzdimensions do not matchrU   )rZ   r)   Úout)ÚhasattrrB   r   r)   r=   r>   rD   rC   Ú_minor_reduceÚaddr"   r•   r.   rH   Úsumr   )rM   rZ   r)   r½   Z	res_dtyperª   Úmajor_indexÚvaluerU   rU   rV   rÁ   L  s    
ÿ
z_cs_matrix.sumc                 C   s>   |du r| j }t t | j¡¡}| |t| j| ƒ¡}||fS )a›  Reduce nonzeros with a ufunc over the minor axis when non-empty

        Can be applied to a function of self.data by supplying data parameter.

        Warning: this does not call sum_duplicates()

        Returns
        -------
        major_index : array of ints
            Major indices where nonzero

        value : array of self.dtype
            Reduce result for nonzeros in each major_index
        N)r@   r=   ZflatnonzerorY   rC   Zreduceatr   )rM   Zufuncr@   rÂ   rÃ   rU   rU   rV   r¿   i  s    ÿz_cs_matrix._minor_reducec           
      C   s\   |   | j¡\}}|   ||f¡\}}t||| j| j| j||d ||d ƒ	\}}}	|	j| jd�S )Nr   r(   )rB   r.   r   rC   rA   r@   rÁ   r)   )
rM   r]   r¥   rN   rO   ÚmajorÚminorrC   rA   r@   rU   rU   rV   Ú_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   NTr2   )rB   ÚstepÚ_get_submatrixÚ_major_sliceÚ_minor_slice©rM   r]   r¥   rÄ   rÅ   rU   rU   rV   Ú_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’t|ƒS |  | |j¡¡S )Nr(   r   )rA   r)   rB   r.   r=   rI   rƒ   r£   r   rC   r@   r¡   rd   r"   rF   Zreshape)	rM   r]   r¥   rP   rN   rO   rÄ   rÅ   ÚvalrU   rU   rV   Ú_get_arrayXarray‘  s    ÿ
z_cs_matrix._get_arrayXarrayc                 C   s"   |   ||f¡\}}|  |¡ |¡S ©N)rB   Ú_major_index_fancyÚ_minor_index_fancyrË   rU   rU   rV   Ú_get_columnXarray   s    z_cs_matrix._get_columnXarrayc                 C   sð   | j j}tj||d� ¡ }|  | j¡\}}t|ƒ}|  ||f¡}|dkrR|  |¡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½   rW   Fr�   )rA   r)   r=   rI   r¡   rB   r.   rD   rF   rC   r>   Úcumsumrƒ   r   r@   )rM   ÚidxrP   rA   r[   rO   rN   Ú	new_shapeÚrow_nnzÚ
res_indptrrf   Úres_indicesÚres_datarU   rU   rV   rÐ   ¥  s(    
ÿÿz_cs_matrix._major_index_fancyc              	   C   sŽ  |t dƒkr|r|  ¡ S | S |  | j¡\}}| |¡\}}}tt|||ƒƒ}|  ||f¡}|dkrl|  |¡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�r6t | 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   rW   r   r(   rÓ   r2   Fr�   )Úslicer1   rB   r.   rA   rD   ÚrangerF   rC   r)   r=   r>   rÔ   rG   r@   rƒ   r   )rM   rÕ   r1   rN   rO   ÚstartÚstoprÇ   rÖ   Zstart0Zstop0Zstart1Zstop1r×   rP   rØ   Zall_idxrÙ   rÚ   rf   rU   rU   rV   rÉ   ¿  s>    

ÿ
ÿÿz_cs_matrix._major_slicec              	   C   sô   | j j}tj||d� ¡ }|  | j¡\}}t|ƒ}|  ||f¡}|dkrR|  |¡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   Fr2   rW   r�   )rA   r)   r=   rI   r¡   rB   r.   rD   rF   r>   Z
empty_likerC   r   ÚargsortrK   rƒ   r   r@   )rM   rÕ   rP   rN   rO   r¯   rÖ   Zcol_offsetsrØ   Z	col_orderrf   rÙ   rÚ   rU   rU   rV   rÑ   ç  s,    
ÿÿÿz_cs_matrix._minor_index_fancyc                 C   s’   |t dƒkr|r|  ¡ S | S |  | j¡\}}| |¡\}}}tt|||ƒƒ}|dkrh|  |  ||f¡¡S |dkr~| j||d�S |  	t
 |||¡¡S )z@Index along the minor axis where idx is a slice object.
        Nr   r   )rÅ   r1   )rÛ   r1   rB   r.   rA   rD   rÜ   rF   rÈ   rÑ   r=   r§   )rM   rÕ   r1   rN   rO   rÝ   rÞ   rÇ   rU   rU   rV   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)   r1   )
rB   r.   Ú_process_slicer1   r   rC   rA   r@   rF   r)   )rM   rÄ   rÅ   r1   rN   rO   Úi0Úi1Zj0Zj1rC   rA   r@   r.   rU   rU   rV   rÈ     s     ÿ
ÿz_cs_matrix._get_submatrixc                 C   s$   |   ||f¡\}}|  |||¡ d S rÏ   ©rB   Ú	_set_many©rM   r]   r¥   rh   r_   ÚjrU   rU   rV   Ú_set_intXint&  s    z_cs_matrix._set_intXintc                 C   s$   |   ||f¡\}}|  |||¡ d S rÏ   rã   rå   rU   rU   rV   Ú_set_arrayXarray*  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_manyrB   r.   r]   r¥   r=   rI   r@   r)   r£   r¦   r§   rD   r¨   rä   )rM   r]   r¥   rh   rN   rO   Zbroadcast_rowZbroadcast_colÚrÚcr_   ræ   rU   rU   rV   Ú_set_arrayXarray_sparse.  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�}||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.   rd   rg   rD   r=   r§   rA   r)   )	rM   Úvaluesr¯   rN   rO   Ú	broadcastZ	max_indexr_   ræ   rU   rU   rV   Ú_setdiagG  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))r<   Ú
IndexErrorrg   )rA   ÚboundrÕ   rU   rU   rV   Úcheck_boundsh  s    ÿ
ÿz1_cs_matrix._prepare_indices.<locals>.check_boundsFr   ©r)   r1   Zndmin)rB   r.   r=   rG   rA   r)   r¡   )rM   r_   ræ   rN   rO   rò   rU   rU   rV   Ú_prepare_indicese  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|v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(   rW   NzZChanging the sparsity structure of a {}_matrix is expensive. lil_matrix is more efficient.r/   r`   r   )rô   r=   rG   r)   r¡   r£   rƒ   rA   r   rC   ri   r@   r   r6   r   Ú_insert_many)
rM   r_   ræ   rh   rN   rO   Ú	n_samplesÚoffsetsrª   ÚmaskrU   rU   rV   rä   x  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 )z‚Sets value at each (i, j) to zero, preserving sparsity structure.

        Here (i,j) index major and minor respectively.
        r(   r   r   rW   N)
rô   rD   r=   rƒ   rA   r)   r   rC   ri   r@   )rM   r_   ræ   rN   rO   rö   r÷   rª   rU   rU   rV   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.
        Z	mergesort)rb   Zclip)ÚmoderW   r%   r(   T)Zreturn_indexr   r   NrÓ   Fr3   )r=   rß   ZtakeÚhas_sorted_indicesr   rA   rC   r£   rI   ÚuniqueÚappendrD   rY   Ú	enumerateÚzipr@   Zconcatenaterƒ   r.   rÔ   Úsort_indicesrL   )rM   r_   ræ   rh   r‘   Zdo_sortrP   Zindices_partsZ
data_partsZuiZ	ui_indptrZnew_nnzsÚprevrë   ÚiiZjsZjerÝ   rÞ   ZujZ	uj_indptrZnnzsZindptr_diffrU   rU   rV   rõ   ³  s\    	ÿ
&

$  
z_cs_matrix._insert_manyc           	      C   sx   |   | j¡\}}| j}tjt|ƒ| jjd�}t || j	|¡ |   ||f¡\}}ddl
m} || j||ff| j|| jd�S )Nr(   r   r+   r0   )rB   r.   rA   r=   rƒ   rD   r)   r   Z	expandptrrC   rE   r,   r@   )	rM   r1   rS   rT   Zminor_indicesZmajor_indicesr]   r¥   r,   rU   rU   rV   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)rB   Z_process_toarray_argsr“   r”   Úf_contiguousrH   Ztocsrr    r•   r.   r   rC   rA   r@   )rM   r‘   r½   rh   r—   rN   rO   rU   rU   rV   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)rB   r.   r   Zcsr_eliminate_zerosrC   rA   r@   re   ©rM   rN   rO   rU   rU   rV   rk   #  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   Zcsr_has_canonical_formatrD   rC   rA   Úhas_canonical_format©rM   rU   rU   rV   Z__get_has_canonical_format-  s    
ÿÿz%_cs_matrix.__get_has_canonical_formatc                 C   s   t |ƒ| _|rd| _d S )NT)r  r  rú   ©rM   rÍ   rU   rU   rV   Z__set_has_canonical_formatC  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ÿ   rB   r.   r   Zcsr_sum_duplicatesrC   rA   r@   re   r  rU   rU   rV   ri   K  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   Zcsr_has_sorted_indicesrD   rC   rA   r  r	  rU   rU   rV   Z__get_sorted[  s    

ÿÿz_cs_matrix.__get_sortedc                 C   s   t |ƒ| _d S rÏ   )r  r  r
  rU   rU   rV   Z__set_sortedk  s    z_cs_matrix.__set_sortedc                 C   s   |   ¡ }| ¡  |S )z9Return a copy of this matrix with sorted indices
        )r1   rÿ   )rM   ÚArU   rU   rV   Úsorted_indicesp  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   Zcsr_sort_indicesrD   rC   rA   r@   r	  rU   rU   rV   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)	rB   r.   rD   rC   rH   rA   rf   r@   r   )rM   rS   rU   rU   rV   re   „  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¼   ÚdivmodrH   r.   rB   rA   rC   r@   r=   Úresizer„   Úallr¿   rÀ   rÔ   r;   )rM   r.   ZbmZbnZnew_MZrmZnew_NZrnrN   rO   rø   rÂ   rÍ   rU   rU   rV   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 )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)rF   rA   r1   rC   r.   r)   )rM   r@   r1   rU   rU   rV   rj   ¼  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�}g d¢}||v 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(   )ro   r�   r€   r}   r~   r   r   rœ   )rF   r«   r   r6   rf   r   rC   rA   r=   rƒ   r.   rq   r   r)   rI   r@   re   )rM   rQ   rl   r¬   ZmaxnnzrP   rC   rA   Zbool_opsr@   r  rU   rU   rV   rx   Ê  s4    

ÿþù	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< t|ƒ}n|}|S )z?
        Divide this matrix by a second sparse matrix.
        r   Z_eldiv_r(   r   )r.   rH   rx   r=   Z
issubdtyper)   Zinexactrƒ   r„   ÚnanZnonzeror¤   r@   r]   r¥   r!   )rM   rQ   rê   r½   r]   r¥   rU   rU   rV   Ú_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__r5   r\   r   r8   rL   rn   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ž   rk   Z$_cs_matrix__get_has_canonical_formatZ$_cs_matrix__set_has_canonical_formatÚpropertyr  ri   Z_cs_matrix__get_sortedZ_cs_matrix__set_sortedrú   r  rÿ   re   r  rj   rx   r  rU   rU   rU   rV   r$      sŒ   
Q



F	$""
h$







(

'L




ÿ	"

"r$   c                 C   s¤   | d u 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)r9   rÛ   rA   rH   rg   r   rð   Ú	TypeError)ÚslÚnumrá   râ   ZstriderU   rU   rV   rà     s"    
ÿrà   )0r  Ú__all__Úwarningsr   rs   Únumpyr=   Zscipy._lib._utilr   Úbaser   r   r   r@   r   r	   Zdiar
   Ú r   r   r   r   r   r   r   r   r   Ú_indexr   Zsputilsr   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   rà   rU   rU   rU   rV   Ú<module>   s.   (D         y