o
    Ö­jïå  ã                   @   s  d Z g ZddlmZ ddl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+m,Z,m-Z- G dd„ deeeƒZ.ddd„Z/dd„ Z0dS )z>Base class for sparse matrix formats using compressed storage.é    )ÚwarnN)Ú_prune_arrayÚcopy_if_neededé   )Ú_spbaseÚissparseÚsparrayÚSparseEfficiencyWarning)Ú_data_matrixÚ_minmax_mixin)Ú_sparsetools)Úget_csr_submatrixÚcsr_sample_offsetsÚcsr_todenseÚcsr_sample_valuesÚcsr_row_indexÚcsr_row_sliceÚcsr_column_index1Úcsr_column_index2)Ú
IndexMixin)ÚupcastÚupcast_charÚ	to_nativeÚisdenseÚisshapeÚgetdtypeÚisscalarlikeÚ	isintlikeÚdowncast_intp_indexÚget_sum_dtypeÚcheck_shapeÚget_index_dtypeÚbroadcast_shapesÚis_pydata_spmatrixc                   @   sˆ  e Zd ZdZd{ddœdd„Zd|dd„Zejje_d|d	d
„Zejje_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/d0„ Zd1d2„ Zejje_d3d4„ Zejje_dd5d6„Zejje_d|d7d8„Zd9d:„ Zd;d<„ Zd=d>„ Z d?d@„ Z!dAdB„ Z"d€dCdD„Z#dEdF„ Z$d€dGdH„Z%d{dIdJ„Z&dKdL„ Z'dMdN„ Z(dOdP„ Z)dQdR„ Z*dSdT„ Z+dUdV„ Z,dWdX„ Z-dYdZ„ Z.d}d[d\„Z/ej/je/_d�d]d^„Z0ej0je0_d_d`„ Z1e2dae3fdbdc„ƒZ4e4j5dde3fdedc„ƒZ4dfdg„ Z6e2dae3fdhdi„ƒZ7e7j5dde3fdjdi„ƒZ7dkdl„ Z8dmdn„ Z9dodp„ Z:dqdr„ Z;ej;je;_d}dsdt„Z<dudv„ Z=dwdx„ Z>d€dydz„Z?dS )‚Ú
_cs_matrixzY
    base array/matrix class for compressed row- and column-oriented arrays/matrices
    NF©Úmaxprintc             
   C   sP  t j| ||d� t|ƒr3|j| jkr|r| ¡ }n| | j¡}|j|j|j|j	f\| _| _| _| _	�nt
|tƒrçt|| jd�rut|| jd�| _	|  | j¡\}}| jt| jƒd�}t dt|td�¡| _t d|¡| _tj|d |d�| _nÕt|ƒdkr˜| j|||d	�}	|	 | j¡}
|
\| _| _| _| _	|  ¡  n²t|ƒd
krÛ|\}}}d }|d ur±d|vr±t|ƒ}| j||f|dd�}|s¿t}tj|||d�| _tj|||d�| _tj|||d�| _notd| jj › d|› �ƒ‚zt !|¡}W n t"�y } ztd| jj › d|› �ƒ|‚d }~ww t
| t#ƒ�r$|j$dk�r$| jdk�r$td|j$› d�ƒ‚|j$dk�r3td|j$› d�ƒ‚| j||d�}	|	 | j¡}
|
\| _| _| _| _	|d u�rXt|| jd�| _	n6| jd u �rŽzt| jƒd }| j ¡ d }W n t"�y€ } ztdƒ|‚d }~ww t|  ||f¡| jd�| _	|d u�r t|ƒ}| jj%|dd�| _| j&dd� d S )Nr%   ©Úallow_nd©Úmaxvalr   )Údefaultr   ©Údtypeé   ©Úshaper-   é   T)r*   Úcheck_contents©Úcopyr-   zunrecognized z constructor input: ÚcsczCSC arrays don't support zD input. Use 2DzCSR arrays don't yet support zD.z!unable to infer matrix dimensionsF©r4   ©Ú
full_check)'r
   Ú__init__r   Úformatr4   ÚasformatÚindptrÚindicesÚdataÚ_shapeÚ
isinstanceÚtupler   Ú	_allow_ndr    Ú_swapÚ_shape_as_2dÚ_get_index_dtypeÚmaxr0   ÚnpÚzerosr   ÚfloatÚlenÚ_coo_containerÚ_coo_to_compressedÚsum_duplicatesr   ÚarrayÚ
ValueErrorÚ	__class__Ú__name__ÚasarrayÚ	Exceptionr   ÚndimÚastypeÚcheck_format)ÚselfÚarg1r0   r-   r4   r&   ÚMÚNÚ	idx_dtypeÚcooÚarraysr>   r=   r<   r*   ÚeÚnewdtype© r`   úU/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/sparse/_compressed.pyr9      sŠ   
ÿ



þÿÿÿ€ÿ$

€ÿ
z_cs_matrix.__init__c                 C   s¬   |d u rt | jd ƒS | jdkr|dv rt | jd ƒS tdƒ‚|dk r'|d7 }|  |d| f¡\}}|  | j¡\}}|dkrHtjt| j	ƒ|d�S |dkrRt 
| j¡S tdƒ‚)Néÿÿÿÿr   ©r   rb   úaxis out of boundsr   r.   ©Ú	minlength)Úintr<   rT   rO   rC   r0   rG   Úbincountr   r=   Údiff)rW   ÚaxisÚ_rZ   r`   r`   ra   Ú_getnnzu   s   
z_cs_matrix._getnnzc                    sþ   ˆ   ¡  |d u rt ˆ j¡S ˆ jdkr!|dvrtdƒ‚t ˆ j¡S |dk r)|d7 }ˆ  |d| f¡\}}|dkrZˆ  ˆ j¡\}}ˆ jdk}| ¡ rLˆ j	nˆ j	| }tj
t|ƒ|d�S |dkr{ˆ j ¡ rit ˆ j¡S t ˆ j¡}t ‡ fdd„|D ƒ¡S tdƒ‚)	Nr   rc   rd   r   r.   re   c                    s$   g | ]\}}t  ˆ j||… ¡‘qS r`   )rG   Úcount_nonzeror>   )Ú.0ÚiÚj©rW   r`   ra   Ú
<listcomp>Ÿ   s   $ z,_cs_matrix.count_nonzero.<locals>.<listcomp>)rM   rG   rm   r>   rT   rO   rC   r0   Úallr=   rh   r   ri   r<   Ú	itertoolsÚpairwiserN   )rW   rj   rk   rZ   ÚmaskÚidxÚpairsr`   rq   ra   rm   ‰   s*   


z_cs_matrix.count_nonzeroTc                 C   s¸  | j jjdkrtd| j jj› d�dd� | jjjdkr(td| jjj› d�dd� | jj| jj| j jfD ]
}|dkr=tdƒ‚q3|  	| j
¡\}}t| j ƒ|d kr^td	t| j ƒ› d
|d › �ƒ‚| j d dkritdƒ‚t| jƒt| jƒkrwtdƒ‚| j d t| jƒkr…tdƒ‚|  ¡  |rÚ| jdkr·| j ¡ |kržtd|› �ƒ‚| j ¡ dk r©tdƒ‚t | j ¡ ¡ dk r·tdƒ‚|  | j | jf¡}tj| j |d�| _ tj| j|d�| _t| jƒ| _dS dS )aÎ  Check whether the array/matrix respects the CSR or CSC format.

        Parameters
        ----------
        full_check : bool, optional
            If `True`, run rigorous check, scanning arrays for valid values.
            Note that activating those check might copy arrays for casting,
            modifying indices and index pointers' inplace.
            If `False`, run basic checks on attributes. O(1) operations.
            Default is `True`.
        ro   z$indptr array has non-integer dtype (ú)r1   ©Ú
stacklevelz%indices array has non-integer dtype (r   z'data, indices, and indptr should be 1-Dzindex pointer size z should be r   z!index pointer should start with 0z*indices and data should have the same sizerb   zQLast value of index pointer should be less than the size of index and data arrayszindices must be < zindices must be >= 0z(indptr must be a non-decreasing sequencer,   N)r<   r-   Úkindr   Únamer=   r>   rT   rO   rC   rD   rJ   ÚpruneÚnnzrF   ÚminrG   ri   rE   rR   r   )rW   r8   ÚxrY   rZ   r[   r`   r`   ra   rV   ¥   sF   ÿÿÿ
ó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.
        Tr6   )rM   Ú
_with_datar>   Úeliminate_zeros)rW   ÚotherÚopÚresr`   r`   ra   Ú_scalar_binoptè   s   z_cs_matrix._scalar_binoptc                 C   s  t |ƒr;t |¡r| j| jtjd�S |dkr4tdtdd� |  tj| jtjd�¡}|  	|t
j¡}|| S |  	|t
j¡S t|ƒrE|  ¡ |kS t|ƒrKtS t|ƒr€tdtdd� | j|jkr^dS | j|jkrj| | j¡}|  |d¡}|  tj| jtjd�¡}|| S tS )	Nr,   r   zOComparing a sparse matrix with 0 using == is inefficient, try using != instead.r1   rz   zHComparing sparse matrices using == is inefficient, try using != instead.FÚ_ne_)r   rG   ÚisnanrP   r0   Úbool_r   r	   Úonesr‡   ÚoperatorÚneÚeqr   Útodenser#   ÚNotImplementedr   r:   r;   Ú_binopt)rW   r„   Úall_trueÚinvr†   r`   r`   ra   Ú__eq__ñ   s8   
þÿz_cs_matrix.__eq__c                 C   sî   t |ƒrGt |¡rtdtdd� |  tj| jtjd�¡}|S |dkr@tdtdd� | jt | j¡tjd�}|  	|t
j¡}|| S |  	|t
j¡S t|ƒrQ|  ¡ |kS t|ƒrWtS t|ƒru| j|jkrcdS | j|jkro| | j¡}|  |d¡S tS )	Nz:Comparing a sparse matrix with nan using != is inefficientr1   rz   r,   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Trˆ   )r   rG   r‰   r   r	   rP   r‹   r0   rŠ   r‡   rŒ   rŽ   r�   r   r�   r#   r�   r   r:   r;   r‘   )rW   r„   r’   r“   r`   r`   ra   Ú__ne__  s4   
ÿþz_cs_matrix.__ne__c                 C   s"  t |ƒr=d|kr|dv rtdƒ‚|d|ƒr7t|tdd� tj| jt |¡d�}| |¡ |  	|¡}|  
||¡S |  ||¡S t|ƒrH||  ¡ |ƒS t|ƒr�| j|jkrVtdƒ‚| j|jkrb| | j¡}|dvrl|  
||¡S td	tdd� |  	tj| jtjd�¡}|  
||d
krˆdnd¡}|| S tS )Nr   )Ú_le_Ú_ge_z >= and <= don't work with 0.r1   rz   r,   úinconsistent shapes)r—   r–   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r–   Ú_gt_Ú_lt_)r   ÚNotImplementedErrorr   r	   rG   Úemptyr0   Úresult_typeÚfillrP   r‘   r‡   r   r�   r   rO   r:   r;   r‹   rŠ   r�   )rW   r„   r…   Úop_nameÚbad_scalar_msgÚ	other_arrr’   r†   r`   r`   ra   Ú_inequality7  s4   


þz_cs_matrix._inequalityc                 C   ó   |   |tjdd¡S )Nrš   zgComparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.)r¢   rŒ   Últ©rW   r„   r`   r`   ra   Ú__lt__Z  ó   ÿz_cs_matrix.__lt__c                 C   r£   )Nr™   zdComparing a sparse matrix with a scalar less than zero using > is inefficient, try using <= instead.)r¢   rŒ   Úgtr¥   r`   r`   ra   Ú__gt__`  r§   z_cs_matrix.__gt__c                 C   r£   )Nr–   zgComparing a sparse matrix with a scalar greater than zero using <= is inefficient, try using > instead.)r¢   rŒ   Úler¥   r`   r`   ra   Ú__le__f  r§   z_cs_matrix.__le__c                 C   r£   )Nr—   zdComparing a sparse matrix with a scalar less than zero using >= is inefficient, try using < instead.)r¢   rŒ   Úger¥   r`   r`   ra   Ú__ge__l  r§   z_cs_matrix.__ge__c                 C   s    |j | j krtd| j › d|j › d�ƒ‚t| jj|jjƒ}|  d¡d }tj|||dd�}|jj	r2|n|j
}|  | j¡\}}t||| j| j| j|ƒ | j|dd	�S )
NzIncompatible shapes (z and ry   ÚCFr   T)r-   Úorderr4   Fr6   )r0   rO   r   r-   ÚcharrC   rG   rN   ÚflagsÚc_contiguousÚTrD   r   r<   r=   r>   Ú
_container)rW   r„   r-   r¯   ÚresultÚyrY   rZ   r`   r`   ra   Ú
_add_densev  s   z_cs_matrix._add_densec                 C   ó   |   |d¡S )NÚ_plus_©r‘   r¥   r`   r`   ra   Ú_add_sparse�  ó   z_cs_matrix._add_sparsec                 C   r¸   )NÚ_minus_rº   r¥   r`   r`   ra   Ú_sub_sparse„  r¼   z_cs_matrix._sub_sparsec                 C   s„  t |ƒr	|  |¡S t|ƒr÷| j|jkr|  |¡}|  |d¡S |jdkr=|  | ¡ d ¡}| jdkr;| d| jd f¡S |S |jdkrK|  | ¡ d ¡S | jdv rX| | j	 
¡ ¡S | j\}}|j\}}|dkru|dkru| |  ||¡ ¡ ¡S |dkrˆ|dkrˆ|  | ||¡ ¡ ¡S t| tƒ}|dkr±||kr±t| ¡  ¡ |ƒ}|  |¡}| jdkrª|S | d|f¡S |dkrÇ||krÇt|  ¡  ¡ |ƒ}	| |	¡S |dkrÝ||krÝt| ¡  ¡ |ƒ}| | ¡S |dkró||krót|  ¡  ¡ |ƒ}
|
 |¡S td	ƒ‚t |¡}|jdk�r
t |  ¡ |¡S |jdk�r+|jtjk�rtS t| j|jƒ}|  |jd ¡ |¡S | jdv �rDt| j|jƒ}t | j	 
¡ |¡ |¡S |  ¡ }| j|jk�rdt |j	||j ¡}| tj¡ ¡ |_	|S t  |¡}| jd dk�sw| j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	ƒ‚| j"|j!|j#|jd  d�}t $tj%|jd |d�|j#¡}t &|j!j'|dd�|jd ¡}| j(| tj¡ ¡ ||ff|jd | jd
 fdd�S | jd dk�ro|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	ƒ‚| j"|j)|j#|jd  d�}t $|j)j'|dd�|jd ¡}t &tj%|jd |d�|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  ¡ ¡}n$|jd dk�r³| jd |jd k�r³t |j	||j)  ¡ ¡}ntd	ƒ‚| tj¡ ¡ |_	|S )z=Point-wise multiplication by array/matrix, vector, or scalar.Ú_elmul_©r   r   )r   r   r   r   ©r   )rÁ   rÀ   r.   r˜   rb   Nr)   r,   Fr6   ©r0   r4   )*r   Ú_mul_scalarr   r0   rP   r‘   ÚtoarrayrT   Úreshaper>   ÚsumrD   Ú_matmul_sparseÚtocscr@   r   Ú_make_diagonal_csrÚravelrO   rG   Ú
asanyarrayÚmultiplyÚsizer-   Úobject_r�   r"   ÚflatÚtocooÚcoordsÚviewÚndarrayÚ
atleast_2dÚcolrE   r   ÚrepeatÚarangeÚtilerU   rK   Úrow)rW   r„   rµ   ÚsMÚsNÚoMÚoNÚis_arrayÚ	new_otherr4   Únew_selfÚbshapeÚretr>   Úother2dr[   rÙ   rÕ   r`   r`   ra   rÌ   ‡  s¸   














ÿý"ÿý&"&z_cs_matrix.multiplyc                 C   sf   | j \}}tj|t| jj|jjƒd�}tt| jd ƒ}|||| j	| j
| j||ƒ | jdkr1|d S |S )Nr,   Ú_matvecr   r   )rD   rG   rH   r   r-   r°   Úgetattrr   r:   r<   r=   r>   rT   )rW   r„   rY   rZ   rµ   Úfnr`   r`   ra   Ú_matmul_vector  s
   
z_cs_matrix._matmul_vectorc              
   C   s‚   | j \}}|jd }tj||ft| jj|jjƒd�}tt| j	d ƒ}||||| j
| j| j| ¡ | ¡ ƒ | jdkr?| |f¡S |S )Nrb   r,   Ú_matvecsr   )rD   r0   rG   rH   r   r-   r°   rå   r   r:   r<   r=   r>   rÊ   rT   rÅ   )rW   r„   rY   rZ   Ún_vecsrµ   ræ   r`   r`   ra   Ú_matmul_multivector  s   


ÿÿ
z_cs_matrix._matmul_multivectorc                 C   sr  | j \}}|j}|dkr| d|jd f¡j}|jdkr|jn|jd df\}}d}| jdkr4||f7 }|dkr=||f7 }| jdkrD|nd|dkrK|ndf}|  ||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�ƒ}|dkr²|dkr¦tjdt| j|jƒd�S | j|t| j|jƒd�S | 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|||ƒ |dk�rt |d ¡S | j|||f|d	�}||k�r7|jd
k�r2| ¡ }| |¡}|S )Nr   r   r.   r`   Ú_matmat_maxnnzr,   r)   Ú_matmat©r0   Úcsr)rD   rT   rÅ   r0   r³   r?   rC   rP   rE   r<   r=   rå   r   r:   rG   rR   rN   r   r-   rœ   r>   Útocsr)rW   r„   rY   ÚK1Úo_ndimÚK2rZ   Ú	new_shapeÚ
faux_shapeÚ	major_dimr[   ræ   r   r<   r=   r>   r†   r`   r`   ra   rÇ   "  sh   
"


"
ÿüÿþú


z_cs_matrix._matmul_sparser   c                 C   s–   | j \}}|| ks||krtjd| jjd�S tt| jd ƒ}tjt|t|dƒ |t	|dƒ ƒt
| jƒd�}||| j d | j d | j| j| j|ƒ |S )Nr   r,   Ú	_diagonalr   )r0   rG   rœ   r>   r-   rå   r   r:   r€   rF   r   r<   r=   )rW   ÚkÚrowsÚcolsræ   r¶   r`   r`   ra   Údiagonal^  s   
 ÿÿz_cs_matrix.diagonalc                 C   sÄ   t |ƒrI||ƒr+tdtdd� tj| jt |¡jd�}| |¡ |  	|¡}|  
||¡S |  ¡  || jt |¡ƒ}| j	|| j| jf|j| jd�}|S t|ƒrT||  ¡ |ƒS t|ƒr^|  
||¡S tdƒ‚)NzITaking maximum (minimum) with > 0 (< 0) number results to a dense matrix.r1   rz   r,   )r-   r0   zOperands not compatible.)r   r   r	   rG   rœ   r0   rR   r-   rž   rP   r‘   rM   r>   r=   r<   r   r�   r   rO   )rW   r„   ÚnpoprŸ   Údense_checkr¡   Únew_dataÚmatr`   r`   ra   Ú_maximum_minimumo  s*   þ

ÿz_cs_matrix._maximum_minimumc                 C   ó   |   |tjddd„ ¡S )NÚ	_maximum_c                 S   s   t  | ¡dkS ©Nr   ©rG   rR   ©r�   r`   r`   ra   Ú<lambda>ˆ  ó    z$_cs_matrix.maximum.<locals>.<lambda>)rÿ   rG   Úmaximumr¥   r`   r`   ra   r  †  ó   
ÿz_cs_matrix.maximumc                 C   r   )NÚ	_minimum_c                 S   s   t  | ¡dk S r  r  r  r`   r`   ra   r  Ž  r  z$_cs_matrix.minimum.<locals>.<lambda>)rÿ   rG   Úminimumr¥   r`   r`   ra   r
  Œ  r  z_cs_matrix.minimumc                 C   sž   | j dkrFt| dƒsF||  d¡d v rFt| jƒ}tjt| jƒd |d�}|  	tj
¡\}}|||< |  |¡}|d dkr>|j}|jd||d�S tj| |||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.
        r.   Ú	blocksize))r   rb   )r   éþÿÿÿr   r   r,   r`   )rj   r-   Úout)rT   ÚhasattrrC   r   r-   rG   rH   rJ   r<   Ú_minor_reduceÚaddÚ_ascontainerr³   rÆ   r   )rW   rj   r-   r  Ú	res_dtyperâ   Úmajor_indexÚvaluer`   r`   ra   rÆ   –  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>   rG   Úflatnonzerori   r<   Úreduceatr   )rW   Úufuncr>   r  r  r`   r`   ra   r  ¯  s   ÿz_cs_matrix._minor_reducec           
      C   s\   |   | j¡\}}|   ||f¡\}}t||| j| j| j||d ||d ƒ	\}}}	|	j| jd�S )Nr   r,   )rC   r0   r   r<   r=   r>   rÆ   r-   )
rW   rÙ   rÕ   rY   rZ   ÚmajorÚminorr<   r=   r>   r`   r`   ra   Ú_get_intXintÉ  s   
þz_cs_matrix._get_intXintc                 C   sF   |   ||f¡\}}|jdv r|jdv r| j||dd�S |  |¡ |¡S )N©r   NTr6   )rC   ÚstepÚ_get_submatrixÚ_major_sliceÚ_minor_slice©rW   rÙ   rÕ   r  r  r`   r`   ra   Ú_get_sliceXsliceÑ  s   z_cs_matrix._get_sliceXslicec           	   
   C   s¦   | j j}|  | j¡\}}|  ||f¡\}}tj||d�}tj||d�}tj|j| jd�}t||| j	| j | j
|j| ¡ | ¡ |ƒ	 |jdkrJ|  |¡S |  | |j¡¡S )Nr,   r   )r=   r-   rC   r0   rG   rR   rœ   rÍ   r   r<   r>   rÊ   rT   r  rP   rÅ   )	rW   rÙ   rÕ   r[   rY   rZ   r  r  Úvalr`   r`   ra   Ú_get_arrayXarray×  s   ÿ

z_cs_matrix._get_arrayXarrayc                 C   s"   |   ||f¡\}}|  |¡ |¡S ©N)rC   Ú_major_index_fancyÚ_minor_index_fancyr   r`   r`   ra   Ú_get_columnXarrayæ  s   z_cs_matrix._get_columnXarrayc                 C   s"  |   | j| jf¡}tj||d� ¡ }|  | j¡d }t|ƒ}| j	dkr*|  ||f¡n|f}|dkr9| 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.   r   N©r  rb   Fr6   rÂ   )rE   r<   r=   rG   rR   rÊ   rC   rD   rJ   rT   rP   r-   rU   rH   Úcumsumrœ   r   r>   )rW   rw   r[   r=   rZ   rY   ró   Úrow_nnzÚ
res_indptrr   Úres_indicesÚres_datar`   r`   ra   r%  ë  s2   ù
ÿz_cs_matrix._major_index_fancyc              	   C   s¢  |t dƒkr|r|  ¡ S | S |  | j¡\}}| |¡\}}}tt|||ƒƒ}| jdkr2|  ||f¡n|f}|dkrA| j|| j	d�S ||}	}
|dkrP|dkrPd}
|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�}n!|d }tj||d�}tj|| j	d�}t|||| j
| j| j||ƒ | j|||f|d	d
�S )z@Index along the major axis where idx is a slice object.
        Nr.   r   r,   rb   r   r(  r6   FrÂ   )Úslicer4   rC   rD   r=   rJ   ÚrangerT   rP   r-   r<   rG   rH   r)  rN   r>   rœ   r   )rW   rw   r4   rY   rZ   ÚstartÚstopr  ró   Ústart0Ústop0Ústart1Ústop1r*  r[   r+  Úall_idxr,  r-  r   r`   r`   ra   r    s>   
ÿÿÿz_cs_matrix._major_slicec              	   C   s2  |   | j| jf¡}| jj|dd�}| jj|dd�}tj||d� ¡ }|  | j¡\}}t	|ƒ}| j
dkr:|  ||f¡n|f}|dkrI| 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.
        Fr6   r,   r.   r   rb   rÂ   )rE   r=   r<   rU   rG   rR   rÊ   rC   rD   rJ   rT   rP   r-   rH   Ú
empty_liker   Úargsortrœ   r   r>   )rW   rw   r[   r=   r<   rY   rZ   r÷   ró   Úcol_offsetsr+  Ú	col_orderr   r,  r-  r`   r`   ra   r&  3  s>   ø
ÿÿz_cs_matrix._minor_index_fancyc                 C   s˜   |t dƒkr|r|  ¡ S | S |  | j¡\}}| |¡\}}}tt|||ƒƒ}|dkr7| j|  ||f¡| jd�S |dkrB| j	||d�S |  
t |||¡¡S )z@Index along the minor axis where idx is a slice object.
        Nr   r,   r   )r  r4   )r.  r4   rC   rD   r=   rJ   r/  rP   r-   r  r&  rG   r×   )rW   rw   r4   rY   rZ   r0  r1  r  r`   r`   ra   r  Z  s   z_cs_matrix._minor_slicec              
   C   sÄ   |   | j¡\}}t||ƒ\}}t||ƒ\}}	|dkr.|dkr.||kr.|	|kr.|r,|  ¡ S | S t||| j| j| j||||	ƒ	\}
}}|   || |	| f¡}| jdkrU|d f}| j	|||
f|| j
dd�S )zbReturn a submatrix of this matrix.

        major, minor: None, int, or slice with step 1
        r   r   F)r0   r-   r4   )rC   rD   Ú_process_slicer4   r   r<   r=   r>   rT   rP   r-   )rW   r  r  r4   rY   rZ   Úi0Úi1Új0Új1r<   r=   r>   r0   r`   r`   ra   r  j  s    
ÿ

ÿz_cs_matrix._get_submatrixc                 C   ó$   |   ||f¡\}}|  |||¡ d S r$  ©rC   Ú	_set_many©rW   rÙ   rÕ   r�   ro   rp   r`   r`   ra   Ú_set_intXint  ó   z_cs_matrix._set_intXintc                 C   r@  r$  rA  rC  r`   r`   ra   Ú_set_arrayXarrayƒ  rE  z_cs_matrix._set_arrayXarrayc                 C   s  | j |  ||f¡Ž  |j\}}|dko|jd dk}|dko$|jd dk}|j|j}}	tj|j| jd�}|j	dkr<d S |rUt 
t |¡t|ƒ¡}t |	|¡}	t ||¡}|rnt 
||¡}t t |¡t|	ƒ¡}	t 
||¡}|  |||	f |||	f f¡\}
}|  |
||¡ d S )Nr   r   r,   )Ú
_zero_manyrC   r0   rÙ   rÕ   rG   rR   r>   r-   rÍ   rÖ   r×   rJ   rØ   rB  )rW   rÙ   rÕ   r�   rY   rZ   Úbroadcast_rowÚbroadcast_colÚrÚcro   rp   r`   r`   ra   Ú_set_arrayXarray_sparse‡  s$   

"z"_cs_matrix._set_arrayXarray_sparsec              	   C   sn  d| j v rd S | jdkrtdƒ‚| j \}}|jdk}|dk rI|r(t|| |ƒ}n
t|| |t|ƒƒ}tj| || | jjd�}tj|| jjd�}n)|rSt||| ƒ}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r¡d 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| ¡  | jd k �r|	| | j|| < | }|| }|| }|  |||	| ¡ d S |  ¡ }| ||¡ | | j¡}|\| _| _| _}d S )Nr   r   z"diagonals cant be set in 1d arraysr,   rb   gü©ñÒMbP?)r0   rT   r›   r€   rJ   rG   r×   r=   r-   Ú
atleast_1drR   rÊ   ÚsqueezeÚbroadcast_torÍ   rC   rœ   r   r<   rM   r>   r   rÆ   Ú_insert_manyrÐ   Ú_setdiagrL   )rW   Úvaluesr÷   rY   rZ   Ú	broadcastÚ	max_indexro   rp   r�   Ú	n_samplesÚoffsetsrâ   rv   r\   r]   rk   r`   r`   ra   rQ     sd   




ÿÿ
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   sR   |   ¡ }||krtd|› d|› d�ƒ‚|  ¡ }|| k r'td|› d|› d�ƒ‚d S )Nzindex (z) out of range (>= ry   z) out of range (< -)rF   Ú
IndexErrorr€   )r=   Úboundrw   r`   r`   ra   Úcheck_boundsç  s   
ÿz1_cs_matrix._prepare_indices.<locals>.check_boundsr,   )rC   rD   rG   rM  rR   r=   r-   rÊ   )rW   ro   rp   rY   rZ   rY  r`   r`   ra   Ú_prepare_indicesä  s   

z_cs_matrix._prepare_indicesc           
   	   C   s(  |   ||¡\}}}}t tj|| jd�¡ ¡ }|j}tj|| jjd�}t	||| j
| j||||ƒ}|dkrE|  ¡  t	||| j
| j||||ƒ d|vrP|| j|< dS td| jj› d�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   rb   Nz%Changing the sparsity structure of a z. is expensive. lil and dok are more efficient.r1   rz   r   )rZ  rG   rM  rR   r-   rÊ   rÍ   rœ   r=   r   r<   rM   r>   r   rP   rQ   r	   rP  )
rW   ro   rp   r�   rY   rZ   rU  rV  râ   rv   r`   r`   ra   rB  õ  s4   ÿÿ
þz_cs_matrix._set_manyc              	   C   sˆ   |   ||¡\}}}}t|ƒ}tj|| jjd�}t||| j| j||||ƒ}|dkr9|  ¡  t||| j| j||||ƒ d| j	||dk < dS )z‚Sets value at each (i, j) to zero, preserving sparsity structure.

        Here (i,j) index major and minor respectively.
        r,   r   r   rb   N)
rZ  rJ   rG   rœ   r=   r-   r   r<   rM   r>   )rW   ro   rp   rY   rZ   rU  rV  râ   r`   r`   ra   rG    s   ÿÿz_cs_matrix._zero_manyc                 C   sª  t j|dd�}|j|dd�}|j|dd�}|j|dd�}| j}| j| j| jf| jd |j d�}t j| j|d�| _t j| j|d�| _t j||d�}t j||d�}g }g }t j	|dd	�\}	}
t  
|
t|ƒ¡}
t  |
¡}d
}tt|	|
|
dd… ƒƒD ]z\}\}}}| j| }| j| }| 
| j||… ¡ | 
| j||… ¡ t j	|||… ddd… dd	�\}}t|ƒ|| krÊ| 
|||… ¡ | 
|||… ¡ n&| 
|||… ddd… | ¡ | 
|||… ddd… | ¡ t|ƒ||< |}qx| j| }| 
| j|d… ¡ | 
| j|d… ¡ t  |¡| _t  |¡| _t j| jj|d�}|d
ƒ|d
< t  | j¡}||	  |7  < ||dd…< t j||d�| _|�rMd| _|  ¡  | jdd� dS )a:  Inserts new nonzero at each (i, j) with value x

        Here (i,j) index major and minor respectively.
        i, j and x must be non-empty, 1d arrays.
        Inserts each major group (e.g. all entries per row) at a time.
        Maintains has_sorted_indices property.
        Modifies i, j, x in place.
        Ú	mergesort)r|   Úclip)Úmoderb   r)   r,   T)Úreturn_indexr   r   Nr(  Fr7   )rG   r8  ÚtakeÚhas_sorted_indicesrE   r=   r<   rÍ   rR   ÚuniqueÚappendrJ   ri   Ú	enumerateÚzipr>   Úconcatenaterœ   r0   r)  Úsort_indicesrV   )rW   ro   rp   r�   r¯   Údo_sortr[   Úindices_partsÚ
data_partsÚuiÚ	ui_indptrÚnew_nnzsÚprevrK  ÚiiÚjsÚjer0  r1  ÚujÚ	uj_indptrÚnnzsÚindptr_diffr`   r`   ra   rP  0  s\   	ÿ
&

$  
z_cs_matrix._insert_manyc                 C   s”   | j dkr|  ¡ }| j|j|jff|j|d�S |  | j¡\}}| j}tjt	|ƒ| jj
d�}t || j|¡ |  ||f¡}| j| j|f| j|| j
d�S )Nr   r6   r,   r3   )rT   rï   rK   r>   r=   r0   rC   rG   rœ   rJ   r-   r   Ú	expandptrr<   )rW   r4   rî   rõ   Ú	minor_dimÚminor_indicesÚmajor_indicesrÑ   r`   r`   ra   rÐ   |  s   
ÿz_cs_matrix.tocooc                 C   s’   |d u r|d u r|   d¡d }|  ||¡}|jjs!|jjs!tdƒ‚|jjr,|  ¡ }|}n|  ¡ }|j}|  |j	¡\}}t
|||j|j|j|ƒ |S )NÚcfr   z&Output array must be C or F contiguous)rC   Ú_process_toarray_argsr±   r²   Úf_contiguousrO   rï   rÈ   r³   rD   r   r<   r=   r>   )rW   r¯   r  r�   r¶   rY   rZ   r`   r`   ra   rÄ   Œ  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)rC   rD   r   Úcsr_eliminate_zerosr<   r=   r>   r~   ©rW   rY   rZ   r`   r`   ra   rƒ   £  s   z_cs_matrix.eliminate_zerosÚreturnc                 C   sJ   t | ddƒsd| _| jS t| dƒs"tt t| jƒd | j| j¡ƒ| _	| jS )aZ  Whether the array/matrix has sorted indices and no duplicates

        Returns
            - True: if the above applies
            - False: otherwise

        has_canonical_format implies has_sorted_indices, so if the latter flag
        is False, so will the former be; if the former is found True, the
        latter flag is also set.
        Ú_has_sorted_indicesTFÚ_has_canonical_formatr   )
rå   r€  r  Úboolr   Úcsr_has_canonical_formatrJ   r<   r=   Úhas_canonical_formatrq   r`   r`   ra   rƒ  ¬  s   
ûÿÿz_cs_matrix.has_canonical_formatr"  c                 C   s   t |ƒ| _|rd| _d S d S )NT)r�  r€  r`  ©rW   r"  r`   r`   ra   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ƒ  rf  rC   rD   r   Úcsr_sum_duplicatesr<   r=   r>   r~   r}  r`   r`   ra   rM   É  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   Úcsr_has_sorted_indicesrJ   r<   r=   r  rq   r`   r`   ra   r`  Ø  s   
	ÿÿz_cs_matrix.has_sorted_indicesc                 C   s   t |ƒ| _d S r$  )r�  r  r„  r`   r`   ra   r`  è  s   c                 C   s   |   ¡ }| ¡  |S )z?Return a copy of this array/matrix with sorted indices
        )r4   rf  )rW   ÚAr`   r`   ra   Úsorted_indicesí  s   z_cs_matrix.sorted_indicesc                 C   s4   | j st t| jƒd | j| j| j¡ d| _ dS dS )z9Sort the indices of this array/matrix *in place*
        r   TN)r`  r   Úcsr_sort_indicesrJ   r<   r=   r>   rq   r`   r`   ra   rf  ø  s   ÿ
ýz_cs_matrix.sort_indicesc                 C   sŠ   |   | j¡d }t| jƒ|d krtdƒ‚t| jƒ| jk r!tdƒ‚t| jƒ| jk r-tdƒ‚t| jd| j… ƒ| _t| jd| j… ƒ| _dS )z8Remove empty space after all non-zero elements.
        r   r   z index pointer has invalid lengthz)indices array has fewer than nnz elementsz&data array has fewer than nnz elementsN)	rC   rD   rJ   r<   rO   r=   r   r>   r   )rW   rõ   r`   r`   ra   r~     s   z_cs_matrix.prunec                 G   s¾  t || jd�}t| dƒrB| j\}}t|d |ƒ\}}t|d |ƒ\}}|s'|r2td| j› d|› �ƒ‚| jd | | jd | }}	n|  t|ƒdkrL|nd|d f¡\}}|  | 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   r   zshape must be divisible into z blocks. Got r(  )r    rB   r  r  ÚdivmodrO   r0   rC   rJ   rD   r=   r<   r>   rG   Úresizerž   rs   r  r  r)  r?   )rW   r0   ÚbmÚbnÚnew_MÚrmÚnew_NÚrnrY   rZ   rv   r  r"  r`   r`   ra   r‹    s:   

ÿ &


z_cs_matrix.resizec                 C   sH   |r| j || j ¡ | j ¡ f| j|jd�S | j || j| jf| j|j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/   )rP   r=   r4   r<   r0   r-   )rW   r>   r4   r`   r`   ra   r‚   :  s   ÿýÿz_cs_matrix._with_datac                 C   s   |   |¡}tt| j| | j ƒ}| j|j }| j| j| j|j|jf|d�}tj	| jj
|d�}tj	||d�}g d¢}||v rFtj	|tjd�}	ntj	|t| j|jƒd�}	| j\}
}||
|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,   )rˆ   rš   r™   r–   r—   rí   )rP   rå   r   r:   r   rE   r<   r=   rG   rœ   r0   rŠ   r   r-   rD   rR   r>   r~   )rW   r„   r…   ræ   Úmaxnnzr[   r<   r=   Úbool_opsr>   rY   rZ   r‡  r`   r`   ra   r‘   H  s6   
ÿþ
ù	z_cs_matrix._binoptc                 C   s˜   |j | j kr
tdƒ‚|  |d¡}t |jtj¡rHtj| j | jd�}| tj	¡ | 
¡ }| jdkr5|d f}d||< | ¡ }|j||j< |  |¡S |}|S )z?
        Divide this matrix by a second sparse matrix.
        r˜   Ú_eldiv_r,   r   rb   r   )r0   rO   r‘   rG   Ú
issubdtyper-   Úinexactrœ   rž   ÚnanÚnonzerorT   rÐ   r>   rÑ   r´   )rW   r„   rJ  r  rÑ   r`   r`   ra   Ú_divide_sparsek  s   


z_cs_matrix._divide_sparsec                 C   s(  | j |kr|r|  ¡ S | S t|| jd�}t| j |ƒ|kr tdƒ‚t| j ƒdkrdt|ƒdkrd|  ¡  | jdkr?| j	|| j
dd�S |d }t || jd ¡}t d|¡}t d|g¡}| j|||f|dd�S | j}t|ƒdkryt|ƒ}td	|› d
�ƒ‚| jdkr‡| j	|| j
dd�S |  ¡  |  |¡\}	}|  |¡\}
}tdd„ |D ƒƒrÂt |	| | jd ¡}t t |¡|	¡}t dt|ƒd |¡}nG|
dkrç||krçt | j|	¡}t | j|	¡}t dt|ƒd t| jƒ¡}n"|dk�r	|
|	k�r	t | j|¡}t t |¡t| jƒ¡}| j| }| j	|||f|dd�S )Nr'   zcannot be broadcastr   r   F)r-   r4   rÂ   r.   z0CSR/CSC broadcast_to cannot have shape >2D. Got ÚDc                 s   s   � | ]}|d kV  qdS r  r`   )rn   Úsr`   r`   ra   Ú	<genexpr>¨  s   € z+_cs_matrix._broadcast_to.<locals>.<genexpr>)r0   r4   r    rB   r"   rO   rJ   rM   r   rP   r-   rG   Úfullr>   r×   rN   Ú_csr_containerrD   rC   rs   rØ   r=   rÖ   r<   )rW   r0   r4   rZ   r>   r=   r<   Ú	old_shaperT   rY   rÜ   rÝ   r`   r`   ra   Ú_broadcast_to‡  sH   



z_cs_matrix._broadcast_to)NNFr$  )T)r   )NNN©F)NN)@rQ   Ú
__module__Ú__qualname__Ú__doc__r9   rl   r   rm   rV   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  rD  rF  rL  rQ  rZ  rB  rG  rP  rÐ   rÄ   rƒ   Úpropertyr�  rƒ  ÚsetterrM   r`  rˆ  rf  r~   r‹  r‚   r‘   r™  r   r`   r`   r`   ra   r$      s’    
X



C	$"#

<






 (
'
D'
L


		
#
#r$   Fc                 C   sb   ddl m}m} |r|n|}t| ƒ}t|d�}tj|d |d�}|dd… }|| ||f||fd�S )z»build diagonal csc_array/csr_array => self._csr_container

    Parameter `data` should be a raveled numpy array holding the
    values on the diagonal of the resulting sparse matrix.
    r   )Ú	csr_arrayÚ
csr_matrixr)   r,   Nrb   rí   )Ú_csrr§  r¨  rJ   r!   rG   r×   )r>   rÞ   r§  r¨  rZ   r[   r<   r=   r`   r`   ra   rÉ   º  s   
rÉ   c                 C   s¶   | d u rd|}}||fS t | tƒr+|  |¡\}}}|dkr"tdƒ‚t||ƒ}||fS t| ƒrW| dk r7| |7 } | | d }}|dk sF||krStd|› d|› d|› �ƒ‚||fS tdƒ‚)Nr   r   z$slicing with step != 1 not supportedzindex out of bounds: 0 <= z < z <= zexpected slice or scalar)r@   r.  r=   rO   r€   r   rW  Ú	TypeError)ÚslÚnumr<  r=  Ústrider`   r`   ra   r;  Ë  s"   

ò

÷þr;  r¡  )1r¤  Ú__all__Úwarningsr   rt   rŒ   ÚnumpyrG   Úscipy._lib._utilr   r   Ú_baser   r   r   r	   Ú_datar
   r   Ú r   r   r   r   r   r   r   r   r   Ú_indexr   Ú_sputilsr   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   rÉ   r;  r`   r`   r`   ra   Ú<module>   s6    (@           
-