a
    ™¥»aK=  ã                   @   s´   d Z dZddgZddlZddl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mZ zdd
lmZ W n ey’   dd„ ZY n0 G dd„ dee
eƒZdd„ ZdS )zDictionary Of Keys based matrixzrestructuredtext enÚ
dok_matrixÚisspmatrix_doké    Né   )ÚspmatrixÚ
isspmatrix)Ú
IndexMixin)	ÚisdenseÚgetdtypeÚisshapeÚ	isintlikeÚisscalarlikeÚupcastÚupcast_scalarÚget_index_dtypeÚcheck_shape)ÚisSequenceTypec                 C   s   t | dƒpt | dƒpt | dƒS )NÚ__len__Ú__next__Únext)Úhasattr©Úx© r   úJ/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/scipy/sparse/dok.pyÚ_is_sequence   s    ÿr   c                   @   s¨  e Zd ZdZdZdQdd„Zdd„ Zd	d
„ Zdd„ Ze	e
jed�ZdRdd„Zdd„ Ze
jje_e
jje_dd„ ZdSd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/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Z d7d8„ Z!d9d:„ Z"d;d<„ Z#d=d>„ Z$d?d@„ Z%dAdB„ Z&dTdCdD„Z'e
j'je'_dEdF„ Z(dGdH„ Z)e
j)je)_dUdIdJ„Z*e
j*je*_dVdKdL„Z+e
j+je+_dWdMdN„Z,e
j,je,_dOdP„ Z-e
j-je-_dS )Xr   að  
    Dictionary Of Keys based sparse matrix.

    This is an efficient structure for constructing sparse
    matrices incrementally.

    This can be instantiated in several ways:
        dok_matrix(D)
            with a dense matrix, D

        dok_matrix(S)
            with a sparse matrix, S

        dok_matrix((M,N), [dtype])
            create the matrix with initial shape (M,N)
            dtype is optional, defaulting to dtype='d'

    Attributes
    ----------
    dtype : dtype
        Data type of the matrix
    shape : 2-tuple
        Shape of the matrix
    ndim : int
        Number of dimensions (this is always 2)
    nnz
        Number of nonzero elements

    Notes
    -----

    Sparse matrices can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.

    Allows for efficient O(1) access of individual elements.
    Duplicates are not allowed.
    Can be efficiently converted to a coo_matrix once constructed.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import dok_matrix
    >>> S = dok_matrix((5, 5), dtype=np.float32)
    >>> for i in range(5):
    ...     for j in range(5):
    ...         S[i, j] = i + j    # Update element

    ZdokNFc           
   
   C   s>  t  | ¡ t | ¡ t|td�| _t|tƒrLt|ƒrL|\}}t	||fƒ| _
nît|ƒrªt|ƒrj|rj| ¡ }n| ¡ }|d urˆ|j|dd�}t  | |¡ t	|jƒ| _
|j| _n�zt |¡}W n. tyæ } ztdƒ|‚W Y d }~n
d }~0 0 t|jƒdkrþtdƒ‚ddlm} |||d	� ¡ }	t  | |	¡ t	|jƒ| _
|	j| _d S )
N)ÚdefaultF©ÚcopyzInvalid input format.é   z(Expected rank <=2 dense array or matrix.r   ©Ú
coo_matrix©Údtype)ÚdictÚ__init__r   r	   Úfloatr"   Ú
isinstanceÚtupler
   r   Ú_shaper   r   r   ÚtodokZastypeÚupdateÚshapeÚnpZasarrayÚ	ExceptionÚ	TypeErrorÚlenÚcoor    )
ÚselfZarg1r+   r"   r   ÚMÚNÚer    Údr   r   r   r$   J   s4    



 zdok_matrix.__init__c                 C   s   t dƒ‚d S )Nz9Direct modification to dok_matrix element is not allowed.)ÚNotImplementedError)r1   Úvalr   r   r   r*   m   s    zdok_matrix.updatec                 C   s   t  | |¡S )zÒAn update method for dict data defined for direct access to
        `dok_matrix` data. Main purpose is to be used for effcient conversion
        from other spmatrix classes. Has no checking if `data` is valid.)r#   r*   )r1   Údatar   r   r   Ú_updater   s    zdok_matrix._updatec                 C   s8   | j |dd� | j¡}|j| _t | ¡ t | |¡ d S ©NFr   )ZreshapeZasformatÚformatÚ__dict__r#   Úclearr*   )r1   r+   Z
new_matrixr   r   r   Ú	set_shapex   s    
zdok_matrix.set_shape)ÚfgetÚfsetc                 C   s   |d urt dƒ‚t | ¡S )Nz6getnnz over an axis is not implemented for DOK format.)r6   r#   r   )r1   Zaxisr   r   r   Úgetnnz€   s    zdok_matrix.getnnzc                 C   s   t dd„ |  ¡ D ƒƒS )Nc                 s   s   | ]}|d kV  qdS )r   Nr   )Ú.0r   r   r   r   Ú	<genexpr>‡   ó    z+dok_matrix.count_nonzero.<locals>.<genexpr>)ÚsumÚvalues©r1   r   r   r   Úcount_nonzero†   s    zdok_matrix.count_nonzeroc                 C   s
   t  | ¡S ©N)r#   r   rG   r   r   r   r   Œ   s    zdok_matrix.__len__ç        c              
   C   s˜   z |\}}t |ƒrt |ƒsJ ‚W n4 tttfyT } ztdƒ|‚W Y d}~n
d}~0 0 |dk s‚|| jd ks‚|dk s‚|| jd krŠtdƒ‚t | ||¡S )ztThis overrides the dict.get method, providing type checking
        but otherwise equivalent functionality.
        z!Index must be a pair of integers.Nr   r   zIndex out of bounds.)r   ÚAssertionErrorr.   Ú
ValueErrorÚ
IndexErrorr+   r#   Úget)r1   Úkeyr   ÚiÚjr4   r   r   r   rN   �   s     ,zdok_matrix.getc                 C   s   t  | ||f| j d¡¡S ©Nr   )r#   rN   r"   Útype©r1   ÚrowÚcolr   r   r   Ú_get_intXintœ   s    zdok_matrix._get_intXintc                 C   s   |   t||d ƒ|¡S ©Nr   ©Ú_get_sliceXsliceÚslicerT   r   r   r   Ú_get_intXsliceŸ   s    zdok_matrix._get_intXslicec                 C   s   |   |t||d ƒ¡S rX   rY   rT   r   r   r   Ú_get_sliceXint¢   s    zdok_matrix._get_sliceXintc                 C   s,  |  | jd ¡\}}}|  | jd ¡\}}}t|||ƒ}	t|||ƒ}
t|	ƒt|
ƒf}t| ƒd|d  |d  kr||  |	|
¡S t|| jd�}|  ¡ D ]”}tt	|d ƒ| |ƒ\}}|dks’|dk s’||d krÎq’tt	|d ƒ| |ƒ\}}|dks’|dk s’||d k�rq’t
 | |¡}t
 |||f|¡ q’|S )Nr   r   r   r!   )Úindicesr+   Úranger/   Ú_get_columnXarrayr   r"   ÚkeysÚdivmodÚintr#   Ú__getitem__Ú__setitem__)r1   rU   rV   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeZ	col_ranger+   ÚnewdokrO   rP   ÚrirQ   Zrjr   r   r   r   rZ   ¥   s$    zdok_matrix._get_sliceXslicec                 C   s   |   |g|¡S rI   ©r`   rT   r   r   r   Ú_get_intXarray½   s    zdok_matrix._get_intXarrayc                 C   s   |   ||g¡S rI   rh   rT   r   r   r   Ú_get_arrayXintÀ   s    zdok_matrix._get_arrayXintc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S rR   ©Úlistr_   r^   r+   r`   rT   r   r   r   Ú_get_sliceXarrayÃ   s    zdok_matrix._get_sliceXarrayc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S rX   rk   rT   r   r   r   Ú_get_arrayXsliceÇ   s    zdok_matrix._get_arrayXslicec           	      C   sj   t t|ƒt|ƒf| jd�}t|ƒD ]B\}}t|ƒD ]0\}}t | ||fd¡}|r2t |||f|¡ q2q"|S )Nr!   r   )r   r/   r"   Ú	enumerater#   rN   re   )	r1   rU   rV   rf   rP   ÚrrQ   ÚcÚvr   r   r   r`   Ë   s    zdok_matrix._get_columnXarrayc                 C   s~   t tjt ||¡ƒ\}}t|j| jd�}t t	|jd ƒt	|jd ƒ¡D ]0}t
 | || || fd¡}|rHt
 |||¡ qH|S )Nr!   r   r   )Úmapr,   Z
atleast_2dZbroadcast_arraysr   r+   r"   Ú	itertoolsÚproductr_   r#   rN   re   )r1   rU   rV   rP   rQ   rf   rO   rr   r   r   r   Ú_get_arrayXarrayÖ   s    $zdok_matrix._get_arrayXarrayc                 C   s2   ||f}|rt  | ||¡ nt  | |¡r.| |= d S rI   )r#   re   Ú__contains__)r1   rU   rV   r   rO   r   r   r   Ú_set_intXintá   s
    zdok_matrix._set_intXintc                 C   s†   t tt| ¡ ƒƒ}t tt| ¡ ƒƒ}| ¡ }t | tt||ƒ|ƒ¡ t |dk¡d D ]*}|| || f}t 	| |¡dkrV| |= qVd S rR   )
rl   rs   rc   Zravelr#   r*   Úzipr,   Znonzerord   )r1   rU   rV   r   rP   rO   r   r   r   Ú_set_arrayXarrayè   s    zdok_matrix._set_arrayXarrayc                    s4  t ˆƒrft| jˆƒ}t| j|d�‰ | j\}}t t|ƒt|ƒ¡D ]"}t 	| |d¡ˆ }|r@|ˆ |< q@nÊt
ˆƒrøˆj| jkr‚tdƒ‚t| jˆjƒ}t| j|d�‰ t ˆ | ¡ tjdd��0 t ˆ ‡ ‡fdd„ˆ ¡ D ƒ¡ W d   ƒ n1 sì0    Y  n8tˆƒ�r|  ¡ }|ˆ ‰ ntˆƒ�r,|  ¡ ˆ ‰ ntS ˆ S )Nr!   r   ú Matrix dimensions are not equal.Úignore)Zoverc                 3   s"   | ]}|ˆ | ˆ|  fV  qd S rI   r   ©rB   Úk©ÚnewÚotherr   r   rC   	  rD   z%dok_matrix.__add__.<locals>.<genexpr>)r   r   r"   r   r+   rt   ru   r_   r#   rN   r   rL   r   r*   r,   Zerrstatera   r   Útocscr   ÚtodenseÚNotImplemented)r1   r�   Ú	res_dtyper2   r3   rO   ÚaijÚcscr   r   r   Ú__add__ô   s2    
ÿ$


zdok_matrix.__add__c                    sî   t ˆ ƒr\tˆjˆjd�}ˆj\}}t t|ƒt|ƒ¡D ]"}t ˆ|d¡ˆ  }|r6|||< q6nŽt	ˆ ƒr¶ˆ jˆjkrxt
dƒ‚tˆjˆjd�}t |ˆ¡ t |‡ ‡fdd„ˆ  ¡ D ƒ¡ n4tˆ ƒrÐˆ ¡ }|ˆ  }ntˆ ƒræˆ ˆ ¡  }ntS |S )Nr!   r   r{   c                 3   s"   | ]}|ˆ| ˆ |  fV  qd S rI   r   r}   ©r�   r1   r   r   rC   !  rD   z&dok_matrix.__radd__.<locals>.<genexpr>)r   r   r+   r"   rt   ru   r_   r#   rN   r   rL   r*   ra   r   r‚   r   rƒ   r„   )r1   r�   r€   r2   r3   rO   r†   r‡   r   r‰   r   Ú__radd__  s,    
ÿ
zdok_matrix.__radd__c                    sF   ˆ j jdkrtdƒ‚tˆ jˆ j d�}t |‡ fdd„ˆ  ¡ D ƒ¡ |S )NÚbz2Negating a sparse boolean matrix is not supported.r!   c                 3   s   | ]}|ˆ |  fV  qd S rI   r   r}   rG   r   r   rC   0  rD   z%dok_matrix.__neg__.<locals>.<genexpr>)r"   Úkindr6   r   r+   r#   r*   ra   ©r1   r€   r   rG   r   Ú__neg__+  s
    zdok_matrix.__neg__c                    s<   t | jˆ ƒ}t| j|d�}t |‡ fdd„|  ¡ D ƒ¡ |S )Nr!   c                 3   s   | ]\}}||ˆ  fV  qd S rI   r   ©rB   r~   rr   ©r�   r   r   rC   7  rD   z)dok_matrix._mul_scalar.<locals>.<genexpr>)r   r"   r   r+   r#   r*   Úitems©r1   r�   r…   r€   r   r�   r   Ú_mul_scalar3  s    zdok_matrix._mul_scalarc                 C   sP   t j| jd t| j|jƒd�}|  ¡ D ]$\\}}}||  |||  7  < q&|S )Nr   r!   )r,   Úzerosr+   r   r"   r‘   )r1   r�   ÚresultrP   rQ   rr   r   r   r   Ú_mul_vector:  s    zdok_matrix._mul_vectorc              	   C   sr   | j d |j d f}t| j|jƒ}tj||d�}|  ¡ D ]4\\}}}||d d …f  |||d d …f  7  < q8|S )Nr   r   r!   )r+   r   r"   r,   r”   r‘   )r1   r�   Zresult_shapeZresult_dtyper•   rP   rQ   rr   r   r   r   Ú_mul_multivectorA  s    *zdok_matrix._mul_multivectorc                    s.   t ˆ ƒr*t | ‡ fdd„|  ¡ D ƒ¡ | S tS )Nc                 3   s   | ]\}}||ˆ  fV  qd S rI   r   r�   r�   r   r   rC   L  rD   z&dok_matrix.__imul__.<locals>.<genexpr>©r   r#   r*   r‘   r„   ©r1   r�   r   r�   r   Ú__imul__J  s    zdok_matrix.__imul__c                    sP   t ˆ ƒrDt| jˆ ƒ}t| j|d�}t |‡ fdd„|  ¡ D ƒ¡ |S |  ¡ ˆ  S )Nr!   c                 3   s   | ]\}}||ˆ  fV  qd S rI   r   r�   r�   r   r   rC   T  rD   z)dok_matrix.__truediv__.<locals>.<genexpr>)	r   r   r"   r   r+   r#   r*   r‘   Ztocsrr’   r   r�   r   Ú__truediv__P  s    zdok_matrix.__truediv__c                    s.   t ˆ ƒr*t | ‡ fdd„|  ¡ D ƒ¡ | S tS )Nc                 3   s   | ]\}}||ˆ  fV  qd S rI   r   r�   r�   r   r   rC   Z  rD   z*dok_matrix.__itruediv__.<locals>.<genexpr>r˜   r™   r   r�   r   Ú__itruediv__X  s    zdok_matrix.__itruediv__c                 C   s
   t  | ¡S rI   )r#   Ú
__reduce__rG   r   r   r   r�   ^  s    zdok_matrix.__reduce__c                 C   sL   |d urt dƒ‚| j\}}t||f| j|d�}t |dd„ |  ¡ D ƒ¡ |S )NzoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.)r"   r   c                 s   s"   | ]\\}}}||f|fV  qd S rI   r   ©rB   ÚleftÚrightr7   r   r   r   rC   p  s   
ÿz'dok_matrix.transpose.<locals>.<genexpr>)rL   r+   r   r"   r#   r*   r‘   )r1   Zaxesr   r2   r3   r€   r   r   r   Ú	transposeh  s    
ÿzdok_matrix.transposec                 C   s:   | j \}}t||f| jd�}t |dd„ |  ¡ D ƒ¡ |S )zReturn the conjugate transpose.r!   c                 s   s(   | ] \\}}}||ft  |¡fV  qd S rI   )r,   Zconjrž   r   r   r   rC   z  s   
ÿz(dok_matrix.conjtransp.<locals>.<genexpr>)r+   r   r"   r#   r*   r‘   )r1   r2   r3   r€   r   r   r   Ú
conjtranspv  s    
ÿzdok_matrix.conjtranspc                 C   s    t | j| jd�}t || ¡ |S )Nr!   )r   r+   r"   r#   r*   r�   r   r   r   r   ~  s    zdok_matrix.copyc                 C   s²   ddl m} | jdkr&|| j| jd�S tt| jƒd�}tj|  	¡ | j| jd�}tjdd„ |  
¡ D ƒ|| jd�}tjd	d„ |  
¡ D ƒ|| jd�}||||ff| j| jd
�}d|_|S )Nr   r   r   r!   )Úmaxval)r"   Úcountc                 s   s   | ]\}}|V  qd S rI   r   )rB   rP   Ú_r   r   r   rC   Œ  rD   z#dok_matrix.tocoo.<locals>.<genexpr>c                 s   s   | ]\}}|V  qd S rI   r   )rB   r¥   rQ   r   r   r   rC   �  rD   )r+   r"   T)r0   r    Znnzr+   r"   r   Úmaxr,   ZfromiterrF   ra   Zhas_canonical_format)r1   r   r    Z	idx_dtyper8   rU   rV   ÚAr   r   r   Útocoo…  s    
  zdok_matrix.tocooc                 C   s   |r|   ¡ S | S rI   r   ©r1   r   r   r   r   r)   ”  s    zdok_matrix.todokc                 C   s   | j dd�j|d�S r:   )r¨   r‚   r©   r   r   r   r‚   ›  s    zdok_matrix.tocscc                 G   sd   t |ƒ}|\}}| j\}}||k s*||k rZt|  ¡ ƒD ]"\}}||ksN||kr6| ||f= q6|| _d S rI   )r   r+   rl   ra   r(   )r1   r+   ZnewMZnewNr2   r3   rP   rQ   r   r   r   Úresize   s    
zdok_matrix.resize)NNF)N)rJ   )NF)F)F)F).Ú__name__Ú
__module__Ú__qualname__Ú__doc__r;   r$   r*   r9   r>   Úpropertyr   Z	get_shaper+   rA   rH   r   rN   rW   r\   r]   rZ   ri   rj   rm   rn   r`   rv   rx   rz   rˆ   rŠ   rŽ   r“   r–   r—   rš   r›   rœ   r�   r¡   r¢   r   r¨   r)   r‚   rª   r   r   r   r   r      s`   0
#



	









c                 C   s
   t | tƒS )aÍ  Is x of dok_matrix type?

    Parameters
    ----------
    x
        object to check for being a dok matrix

    Returns
    -------
    bool
        True if x is a dok matrix, False otherwise

    Examples
    --------
    >>> from scipy.sparse import dok_matrix, isspmatrix_dok
    >>> isspmatrix_dok(dok_matrix([[5]]))
    True

    >>> from scipy.sparse import dok_matrix, csr_matrix, isspmatrix_dok
    >>> isspmatrix_dok(csr_matrix([[5]]))
    False
    )r&   r   r   r   r   r   r   ®  s    )r®   Z__docformat__Ú__all__rt   Únumpyr,   Úbaser   r   Ú_indexr   Zsputilsr   r	   r
   r   r   r   r   r   r   Úoperatorr   r   ÚImportErrorr#   r   r   r   r   r   r   Ú<module>   s    ,   