a
    ˆù»jÄÕ  ã                   @   sô   d Z g ZddlmZ ddlZddlZddlmZm	Z	 ddl
mZmZmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZmZmZmZmZ dd
lmZ ddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* G dd„ deeeƒZ+dd„ Z,dS )z>Base class for sparse matrix formats using compressed storage.é    )ÚwarnN)Ú_prune_arrayÚcopy_if_neededé   )Ú_spbaseÚissparseÚSparseEfficiencyWarningÚsparray)Ú_data_matrixÚ_minmax_mixin)Ú_sparsetools)Úget_csr_submatrixÚcsr_sample_offsetsÚcsr_todenseÚcsr_sample_valuesÚcsr_row_indexÚcsr_row_sliceÚcsr_column_index1Úcsr_column_index2)Ú
IndexMixin)ÚupcastÚupcast_charÚ	to_nativeÚisdenseÚisshapeÚgetdtypeÚisscalarlikeÚ	isintlikeÚdowncast_intp_indexÚget_sum_dtypeÚcheck_shapeÚis_pydata_spmatrixc                   @   sd  e Zd ZdZdvdd„Zdwdd„Zejje_dxd	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd'd(„ Zdyd*d+„Zejje_d,d-„ Zd.d/„ Zejje_d0d1„ Zejje_dzd2d3„Zejje_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._d€dZd[„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:_d�dpdq„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\| _| _| _| _	�nt
|tƒ�rÂt|ƒrât|ƒ| _	| j\}}| jt||ƒd�}t dt|td�¡| _t d|¡| _tj|  ||f¡d d |d�| _nÞt|ƒdk�r*| j|||d�}| | j¡}	|	\| _| _| _| _	|  ¡  n–t|ƒdk�r®|\}
}}d }|d u�rXt|ƒ}| j||f|d	d
�}|�svt}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 t
| tƒ�rF|j dk �rF| jdk�rFtd|j › d�ƒ‚| j||d�}| | j¡}	|	\| _| _| _| _	|d u�rŠt|ƒ| _	nr| jd u �rüz 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csczCSC arrays don't support zD input. Use 2Dz!unable to infer matrix dimensionsF©r-   ©Ú
full_check)#r
   Ú__init__r   Úformatr-   ÚasformatÚindptrÚindicesÚdataÚ_shapeÚ
isinstanceÚtupler   r    r*   Ú_get_index_dtypeÚmaxÚnpÚzerosr   ÚfloatÚ_swapÚlenÚ_coo_containerÚ_coo_to_compressedÚsum_duplicatesr   ÚarrayÚ
ValueErrorÚasarrayÚ	Exceptionr	   ÚndimÚastypeÚcheck_format)ÚselfZarg1r*   r'   r-   ÚMÚNÚ	idx_dtypeÚcooÚarraysr6   r5   r4   r$   ÚeÚmsgÚ	major_dimÚ	minor_dim© rU   ú>E:\soccer\ml_env\Lib\site-packages\scipy/sparse/_compressed.pyr1      sz    

ÿ

ÿ




þ $ÿ
 
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)
Úintr4   r?   r*   r<   Zbincountr   r5   ÚdiffrE   )rK   ÚaxisÚ_rM   rU   rU   rV   Ú_getnnzs   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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�rt
dƒ‚|  ¡  |�rÐ| jdk�rŽ| j ¡ |k�rRt
|› d|› �ƒ‚| j ¡ dk �rpt
|› d�ƒ‚t | j¡ ¡ dk �rŽt
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 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 sequencer&   N)r?   r*   r4   r'   Úkindr   Únamer5   r6   rH   rE   r@   r2   ÚpruneÚnnzr;   Úminr<   rY   r:   rF   r   )rK   r0   Z
major_nameZ
minor_namerS   rT   ÚxrN   rU   rU   rV   rJ   „   sH    ÿÿ
ÿ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 sparse array in canonical form.
        Tr.   )rC   Ú
_with_datar6   Úeliminate_zeros)rK   ÚotherÚ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 tS d S )	Nr&   r   zOComparing a sparse matrix with 0 using == is inefficient, try using != instead.r+   ra   zHComparing sparse matrices using == is inefficient, try using != instead.FÚ_ne_)r   r<   ÚisnanÚ	__class__r*   Úbool_r   r   Úonesrn   ÚoperatorÚneÚeqr   Útodenser!   ÚNotImplementedr   r2   r3   Ú_binopt)rK   rk   Ú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 tS d S )	Nz:Comparing a sparse matrix with nan using != is inefficientr+   ra   r&   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Tro   )r   r<   rp   r   r   rq   rs   r*   rr   rn   rt   rv   ru   r   rw   r!   rx   r   r2   r3   ry   )rK   rk   rz   r{   rU   rU   rV   Ú__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 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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+   ra   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Úfillrq   ry   rn   r   rw   r   rE   r2   r3   rs   rr   rx   )rK   rk   rl   Úop_nameZbad_scalar_msgÚ	other_arrrz   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ˆ   rt   Últ©rK   rk   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ˆ   rt   ÚgtrŠ   rU   rU   rV   Ú__gt__C  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ˆ   rt   ÚlerŠ   rU   rU   rV   Ú__le__I  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ˆ   rt   ÚgerŠ   rU   rU   rV   Ú__ge__O  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*   rE   r   r'   Úcharr?   r<   rD   ÚflagsÚc_contiguousÚTr   r4   r5   r6   Ú
_container)rK   rk   r'   r’   ÚresultrL   rM   ÚyrU   rU   rV   Ú
_add_denseY  s    z_cs_matrix._add_densec                 C   s   |   |d¡S )NZ_plus_©ry   rŠ   rU   rU   rV   Ú_add_sparsed  s    z_cs_matrix._add_sparsec                 C   s   |   |d¡S )NZ_minus_r›   rŠ   rU   rU   rV   Ú_sub_sparseg  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�rœtS |  |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�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�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 )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*   rq   ry   rH   Ú	TypeErrorÚtoarrayÚ_matmul_sparseÚtocscZ_dia_containerÚravelrE   r<   Z
atleast_2dÚmultiplyÚsizer'   Zobject_rx   ZflatÚtocoor6   r]   ÚcolÚrepeatÚaranger@   ÚtilerA   ÚviewZndarray)rK   rk   r-   Úretr6   r]   r¨   rU   rU   rV   r¥   j  s¬    





&þ
&þ
&þ
&þ

ý"ý&"&z_cs_matrix.multiplyc                 C   sT   | j \}}tj|t| jj|jjƒd�}tt| jd ƒ}|||| j	| j
| j||ƒ |S )Nr&   Z_matvec)r*   r<   r=   r   r'   r“   Úgetattrr   r2   r4   r5   r6   )rK   rk   rL   rM   r˜   ÚfnrU   rU   rV   Ú_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   r2   r4   r5   r6   r¤   )rK   rk   rL   rM   Zn_vecsr˜   r¯   rU   rU   rV   Ú_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?   rq   r:   r4   r5   r®   r   r2   r<   rF   r„   r   r'   r6   )rK   rk   rL   ZK1ZK2rM   Z
major_axisrN   r¯   rf   r4   r5   r6   rU   rU   rV   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„   r6   r'   r®   r   r2   rg   r;   r   r4   r5   )rK   Ú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+   ra   r&   )r'   r*   zOperands not compatible.)r   r   r   r<   r„   r*   rF   r'   r…   rq   ry   rC   r6   r5   r4   r   rw   r   rE   )rK   rk   Znpopr†   Zdense_checkr‡   Znew_dataZmatrU   rU   rV   Ú_maximum_minimum2  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<   rF   ©rh   rU   rU   rV   Ú<lambda>K  ó    z$_cs_matrix.maximum.<locals>.<lambda>)r¶   r<   ÚmaximumrŠ   rU   rU   rV   r¼   I  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º   Q  r»   z$_cs_matrix.minimum.<locals>.<lambda>)r¶   r<   ÚminimumrŠ   rU   rU   rV   r½   O  s    
ÿz_cs_matrix.minimumc                 C   s´   t | dƒsž||  d¡d v ržt| jƒ}tjt| jƒd |d�}|  tj	¡\}}|||< |  
|¡}|d dkrr|j}|durŽ|j|jkrŽtdƒ‚|jd	||d
�S tj| |||d
�S dS )z„Sum the array/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)Úhasattrr?   r   r'   r<   r=   r@   r4   Ú_minor_reduceÚaddÚ_ascontainerr–   r*   rE   Úsumr   )rK   rZ   r'   r¿   Z	res_dtyper­   Úmajor_indexÚvaluerU   rU   rV   rÄ   Y  s    
ÿ

z_cs_matrix.sumc                 C   s>   |du r| j }t t | j¡¡}| |t| j| ƒ¡}||fS )a›  Reduce nonzeros with a ufunc over the minor axis when non-empty

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

        Warning: this does not call sum_duplicates()

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

        value : array of self.dtype
            Reduce result for nonzeros in each major_index
        N)r6   r<   ZflatnonzerorY   r4   Zreduceatr   )rK   Zufuncr6   rÅ   rÆ   rU   rU   rV   rÁ   v  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   r4   r5   r6   rÄ   r'   )
rK   r]   r¨   rL   rM   ÚmajorÚminorr4   r5   r6   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   NTr.   )r?   ÚstepÚ_get_submatrixÚ_major_sliceÚ_minor_slice©rK   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”|  |¡S |  | |j¡¡S )Nr&   r   )r5   r'   r?   r*   r<   rF   r„   r¦   r   r4   r6   r¤   rH   rÃ   rq   Zreshape)	rK   r]   r¨   rN   rL   rM   rÇ   rÈ   ÚvalrU   rU   rV   Ú_get_arrayXarrayž  s    ÿ

z_cs_matrix._get_arrayXarrayc                 C   s"   |   ||f¡\}}|  |¡ |¡S ©N)r?   Ú_major_index_fancyÚ_minor_index_fancyrÎ   rU   rU   rV   Ú_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¿   rW   Fr.   rŸ   )r:   r4   r5   r<   rF   r¤   r?   r*   r@   rq   r'   rI   r=   Úcumsumr„   r   r6   )rK   ÚidxrN   r5   r[   rM   rL   Ú	new_shapeÚrow_nnzÚ
res_indptrrf   Úres_indicesÚres_datarU   rU   rV   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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&   rW   r   rÖ   r.   FrŸ   )Úslicer-   r?   r*   r5   r@   Úrangerq   r'   r4   r<   r=   r×   rD   r6   r„   r   )rK   rØ   r-   rL   rM   ÚstartÚstoprÊ   rÙ   Zstart0Zstop0Zstart1Zstop1rÚ   rN   rÛ   Zall_idxrÜ   rÝ   rf   rU   rU   rV   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   rW   rŸ   )r:   r5   r4   rI   r<   rF   r¤   r?   r*   r@   rq   r'   r=   Z
empty_liker   Úargsortr„   r   r6   )rK   rØ   rN   r5   r4   rL   rM   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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*   r5   r@   rß   rq   r'   rË   rÔ   r<   rª   )rK   rØ   r-   rL   rM   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'   r-   )
r?   r*   Ú_process_slicer-   r   r4   r5   r6   rq   r'   )rK   rÇ   rÈ   r-   rL   rM   Úi0Úi1Zj0Zj1r4   r5   r6   r*   rU   rU   rV   rË   2  s     ÿ
ÿz_cs_matrix._get_submatrixc                 C   s$   |   ||f¡\}}|  |||¡ d S rÒ   ©r?   Ú	_set_many©rK   r]   r¨   rh   r_   ÚjrU   rU   rV   Ú_set_intXintE  s    z_cs_matrix._set_intXintc                 C   s$   |   ||f¡\}}|  |||¡ d S rÒ   ræ   rè   rU   rU   rV   Ú_set_arrayXarrayI  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¨   r<   rF   r6   r'   r¦   r©   rª   r@   r«   rç   )rK   r]   r¨   rh   rL   rM   Zbroadcast_rowZbroadcast_colÚrÚcr_   ré   rU   rU   rV   Ú_set_arrayXarray_sparseM  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|r”t||| ƒ}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   rW   gü©ñÒMbP?)r*   rH   rg   r@   r<   rª   r5   r'   Ú
atleast_1drF   r¤   ZsqueezeZbroadcast_tor¦   r?   r„   r   r4   rC   r6   rÄ   rf   Ú_insert_manyr§   Ú_setdiagrB   )rK   Úvaluesr²   rL   rM   Ú	broadcastZ	max_indexr_   ré   rh   Ú	n_samplesÚoffsetsr­   ÚmaskrO   rP   r[   rU   rU   rV   rò   f  s`    


ÿ
ÿ

z_cs_matrix._setdiagc                 C   sp   |   | j¡\}}dd„ }t tj|| jjd�¡ ¡ }t tj|| jjd�¡ ¡ }|||ƒ |||ƒ ||||fS )Nc                 S   sF   |   ¡ }||kr td||f ƒ‚|  ¡ }|| k rBtd||f ƒ‚d S )Nzindex (%d) out of range (>= %d)zindex (%d) out of range (< -%d))r;   Ú
IndexErrorrg   )r5   ÚboundrØ   rU   rU   rV   Úcheck_bounds«  s    ÿ
ÿz1_cs_matrix._prepare_indices.<locals>.check_boundsr&   )r?   r*   r<   rð   rF   r5   r'   r¤   )rK   r_   ré   rL   rM   rú   rU   rU   rV   Ú_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 )	z�Sets value at each (i, j) to x

        Here (i,j) index major and minor respectively, and must not contain
        duplicate entries.
        r&   r   rW   NzZChanging the sparsity structure of a {}_matrix is expensive. lil_matrix is more efficient.r+   ra   r   )rû   r<   rð   rF   r'   r¤   r¦   r„   r5   r   r4   rC   r6   r   r2   r   rñ   )
rK   r_   ré   rh   rL   rM   rõ   rö   r­   r÷   rU   rU   rV   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 )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û   r@   r<   r„   r5   r'   r   r4   rC   r6   )rK   r_   ré   rL   rM   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}| 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�| _|�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)rc   Zclip)ÚmoderW   r#   r&   T)Zreturn_indexr   r   NrÖ   Fr/   )r<   râ   ZtakeÚhas_sorted_indicesr:   r5   r4   r¦   rF   ÚuniqueÚappendr@   rY   Ú	enumerateÚzipr6   Zconcatenater„   r*   r×   Úsort_indicesrJ   )rK   r_   ré   rh   r’   Zdo_sortrN   Zindices_partsZ
data_partsZuiZ	ui_indptrZnew_nnzsÚprevrî   ÚiiÚjsZjerà   rá   ZujZ	uj_indptrZnnzsZindptr_diffrU   rU   rV   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*   r5   r<   r„   r@   r'   r   Z	expandptrr4   rA   r6   )rK   r-   rS   rT   Zminor_indicesZmajor_indicesÚcoordsrU   rU   rV   r§   B  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_contiguousrE   Ztocsrr£   r–   r*   r   r4   r5   r6   )rK   r’   r¿   rh   r™   rL   rM   rU   rU   rV   r¡   O  s    z_cs_matrix.toarrayc                 C   s4   |   | j¡\}}t ||| j| j| j¡ |  ¡  dS )z\Remove zero entries from the array/matrix

        This is an *in place* operation.
        N)r?   r*   r   Zcsr_eliminate_zerosr4   r5   r6   re   ©rK   rL   rM   rU   rU   rV   rj   f  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@   r4   r5   Úhas_canonical_format©rK   rU   rU   rV   r  p  s    
ÿÿz_cs_matrix.has_canonical_format)rÐ   c                 C   s   t |ƒ| _|rd| _d S )NT)r  r  rý   ©rK   rÐ   rU   rU   rV   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_duplicatesr4   r5   r6   re   r	  rU   rU   rV   rC   �  s    ÿz_cs_matrix.sum_duplicatesc                 C   s2   t | dƒs,tt t| jƒd | j| j¡ƒ| _| jS )z¢Whether 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@   r4   r5   r  r  rU   rU   rV   rý   �  s    	
ÿÿz_cs_matrix.has_sorted_indicesc                 C   s   t |ƒ| _d S rÒ   )r  r  r  rU   rU   rV   rý   ­  s    c                 C   s   |   ¡ }| ¡  |S )z?Return a copy of this array/matrix with sorted indices
        )r-   r  )rK   ÚArU   rU   rV   Ú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@   r4   r5   r6   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)	r?   r*   r@   r4   rE   r5   rf   r6   r   )rK   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|¡ƒ‚| 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¾   ÚdivmodrE   r2   r*   r?   r5   r4   r6   r<   Úresizer…   ÚallrÁ   rÂ   r×   r7   )rK   r*   ZbmZbnZnew_MZrmZnew_NZrnrL   rM   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)rq   r5   r-   r4   r*   r'   )rK   r6   r-   rU   rU   rV   ri   þ  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 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ž   )rq   r®   r   r2   rf   r:   r4   r5   r<   r„   r*   rr   r   r'   rF   r6   re   )rK   rk   rl   r¯   ZmaxnnzrN   r4   r5   Zbool_opsr6   r  rU   rU   rV   ry     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< |  |¡}n|}|S )z?
        Divide this matrix by a second sparse matrix.
        r€   Z_eldiv_r&   r   )r*   rE   ry   r<   Z
issubdtyper'   Zinexactr„   r…   ÚnanZnonzeror§   r6   r]   r¨   r—   )rK   rk   rí   r¿   r]   r¨   rU   rU   rV   Ú_divide_sparse.  s    z_cs_matrix._divide_sparse)NNF)N)T)r   )NNN)N)F)F)NNF)T)NN)T)>Ú__name__Ú
__module__Ú__qualname__Ú__doc__r1   r\   r   rJ   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¡   rj   Úpropertyr  r  ÚsetterrC   rý   r  r  re   r  ri   ry   r  rU   rU   rU   rV   r"      sŠ   
X


G	$"#
x$






!
('

B'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)r8   rÞ   r5   rE   rg   r   rø   r    )ÚslÚnumrä   rå   ZstriderU   rU   rV   rã   J  s"    
ÿrã   )-r  Ú__all__Úwarningsr   rt   Únumpyr<   Zscipy._lib._utilr   r   Z_baser   r   r   r	   Ú_datar
   r   Ú r   r   r   r   r   r   r   r   r   Ú_indexr   Z_sputilsr   r   r   r   r   r   r   r   r   r   r    r!   r"   rã   rU   rU   rU   rV   Ú<module>   s.   (8          >