a
    ¬ˆc>  ã                   @   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   úT/home/yuhw/wc/wire-cell-python/wcpy/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   s4  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ƒ‚| j||d� ¡ }t  | |¡ t	|jƒ| _
|j| _d S )N)ÚdefaultF©ÚcopyzInvalid input format.é   z(Expected rank <=2 dense array or matrix.©Údtype)ÚdictÚ__init__r   r	   Úfloatr   Ú
isinstanceÚtupler
   r   Ú_shaper   r   r   ÚtodokZastypeÚupdateÚshapeÚnpZasarrayÚ	ExceptionÚ	TypeErrorÚlenÚ_coo_container)	ÚselfZarg1r(   r   r   ÚMÚNÚeÚdr   r   r   r!   J   s2    



 zdok_matrix.__init__c                 C   s   t dƒ‚d S )Nz9Direct modification to dok_matrix element is not allowed.)ÚNotImplementedError)r.   Úvalr   r   r   r'   l   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'   )r.   Údatar   r   r   Ú_updateq   s    zdok_matrix._updatec                 C   s8   | j |dd� | j¡}|j| _t | ¡ t | |¡ d S ©NFr   )ZreshapeZasformatÚformatÚ__dict__r    Úclearr'   )r.   r(   Z
new_matrixr   r   r   Ú	set_shapew   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.)r3   r    r   )r.   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©r.   r   r   r   Úcount_nonzero…   s    zdok_matrix.count_nonzeroc                 C   s
   t  | ¡S ©N)r    r   rD   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)r.   Úkeyr   ÚiÚjr1   r   r   r   rK   Ž   s     ,zdok_matrix.getc                 C   s   t  | ||f| j d¡¡S ©Nr   )r    rK   r   Útype©r.   ÚrowÚcolr   r   r   Ú_get_intXint›   s    zdok_matrix._get_intXintc                 C   s   |   t||d ƒ|¡S ©Nr   ©Ú_get_sliceXsliceÚslicerQ   r   r   r   Ú_get_intXslicež   s    zdok_matrix._get_intXslicec                 C   s   |   |t||d ƒ¡S rU   rV   rQ   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 | j|| 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_columnXarrayÚ_dok_containerr   ÚkeysÚdivmodÚintr    Ú__getitem__Ú__setitem__)r.   rR   rS   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeZ	col_ranger(   ÚnewdokrL   rM   ÚrirN   Zrjr   r   r   r   rW   ¤   s$    zdok_matrix._get_sliceXslicec                 C   s   |  ¡ }|  |g|¡S rF   ©Zsqueezer]   rQ   r   r   r   Ú_get_intXarray¼   s    zdok_matrix._get_intXarrayc                 C   s   |  ¡ }|  ||g¡S rF   rf   rQ   r   r   r   Ú_get_arrayXintÀ   s    zdok_matrix._get_arrayXintc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S rO   ©Úlistr\   r[   r(   r]   rQ   r   r   r   Ú_get_sliceXarrayÄ   s    zdok_matrix._get_sliceXarrayc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S rU   ri   rQ   r   r   r   Ú_get_arrayXsliceÈ   s    zdok_matrix._get_arrayXslicec           	      C   sl   | j t|ƒt|ƒf| jd�}t|ƒD ]B\}}t|ƒD ]0\}}t | ||fd¡}|r4t |||f|¡ q4q$|S )Nr   r   )r^   r,   r   Ú	enumerater    rK   rc   )	r.   rR   rS   rd   rM   ÚrrN   ÚcÚvr   r   r   r]   Ì   s    zdok_matrix._get_columnXarrayc                 C   s€   t tjt ||¡ƒ\}}| j|j| jd�}t t	|jd ƒt	|jd ƒ¡D ]0}t
 | || || fd¡}|rJt
 |||¡ qJ|S )Nr   r   r   )Úmapr)   Z
atleast_2dZbroadcast_arraysr^   r(   r   Ú	itertoolsÚproductr\   r    rK   rc   )r.   rR   rS   rM   rN   rd   rL   rp   r   r   r   Ú_get_arrayXarray×   s    $zdok_matrix._get_arrayXarrayc                 C   s2   ||f}|rt  | ||¡ nt  | |¡r.| |= d S rF   )r    rc   Ú__contains__)r.   rR   rS   r   rL   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 rO   )
rj   rq   ra   Zravelr    r'   Úzipr)   Znonzerorb   )r.   rR   rS   r   rM   rL   r   r   r   Ú_set_arrayXarrayé   s    zdok_matrix._set_arrayXarrayc                    s8  t ˆƒrht| jˆƒ}| j| j|d�‰ | j\}}t t|ƒt|ƒ¡D ]"}t 	| |d¡ˆ }|rB|ˆ |< qBnÌt
ˆƒrüˆj| jkr„tdƒ‚t| jˆjƒ}| j| j|d�‰ t ˆ | ¡ tjdd��0 t ˆ ‡ ‡fdd„ˆ ¡ D ƒ¡ W d   ƒ n1 sð0    Y  n8tˆƒ�r|  ¡ }|ˆ ‰ ntˆƒ�r0|  ¡ ˆ ‰ ntS ˆ S )Nr   r   ú Matrix dimensions are not equal.Úignore)Zoverc                 3   s"   | ]}|ˆ | ˆ|  fV  qd S rF   r   ©r?   Úk©ÚnewÚotherr   r   r@   
  rA   z%dok_matrix.__add__.<locals>.<genexpr>)r   r   r   r^   r(   rr   rs   r\   r    rK   r   rI   r   r'   r)   Zerrstater_   r   Útocscr   ÚtodenseÚNotImplemented)r.   r   Ú	res_dtyper/   r0   rL   ÚaijÚcscr   r}   r   Ú__add__õ   s2    
ÿ$


zdok_matrix.__add__c                    sò   t ˆ ƒr^ˆjˆjˆjd�}ˆj\}}t t|ƒt|ƒ¡D ]"}t ˆ|d¡ˆ  }|r8|||< q8n�t	ˆ ƒrºˆ jˆjkrzt
dƒ‚ˆjˆjˆjd�}t |ˆ¡ t |‡ ‡fdd„ˆ  ¡ D ƒ¡ n4tˆ ƒrÔˆ ¡ }|ˆ  }ntˆ ƒrêˆ ˆ ¡  }ntS |S )Nr   r   ry   c                 3   s"   | ]}|ˆ| ˆ |  fV  qd S rF   r   r{   ©r   r.   r   r   r@   "  rA   z&dok_matrix.__radd__.<locals>.<genexpr>)r   r^   r(   r   rr   rs   r\   r    rK   r   rI   r'   r_   r   r€   r   r�   r‚   )r.   r   r~   r/   r0   rL   r„   r…   r   r‡   r   Ú__radd__  s,    
ÿ
zdok_matrix.__radd__c                    sH   ˆ j jdkrtdƒ‚ˆ jˆ 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 rF   r   r{   rD   r   r   r@   1  rA   z%dok_matrix.__neg__.<locals>.<genexpr>)r   Úkindr3   r^   r(   r    r'   r_   ©r.   r~   r   rD   r   Ú__neg__,  s
    zdok_matrix.__neg__c                    s>   t | jˆ ƒ}| j| j|d�}t |‡ fdd„|  ¡ D ƒ¡ |S )Nr   c                 3   s   | ]\}}||ˆ  fV  qd S rF   r   ©r?   r|   rp   ©r   r   r   r@   8  rA   z)dok_matrix._mul_scalar.<locals>.<genexpr>)r   r   r^   r(   r    r'   Úitems©r.   r   rƒ   r~   r   rŽ   r   Ú_mul_scalar4  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�   )r.   r   ÚresultrM   rN   rp   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�   )r.   r   Zresult_shapeZresult_dtyper“   rM   rN   rp   r   r   r   Ú_mul_multivectorB  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 rF   r   r�   rŽ   r   r   r@   M  rA   z&dok_matrix.__imul__.<locals>.<genexpr>©r   r    r'   r�   r‚   ©r.   r   r   rŽ   r   Ú__imul__K  s    zdok_matrix.__imul__c                    sR   t ˆ ƒrFt| jˆ ƒ}| j| j|d�}t |‡ fdd„|  ¡ D ƒ¡ |S |  ¡ ˆ  S )Nr   c                 3   s   | ]\}}||ˆ  fV  qd S rF   r   r�   rŽ   r   r   r@   U  rA   z)dok_matrix.__truediv__.<locals>.<genexpr>)	r   r   r   r^   r(   r    r'   r�   Ztocsrr�   r   rŽ   r   Ú__truediv__Q  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 rF   r   r�   rŽ   r   r   r@   [  rA   z*dok_matrix.__itruediv__.<locals>.<genexpr>r–   r—   r   rŽ   r   Ú__itruediv__Y  s    zdok_matrix.__itruediv__c                 C   s
   t  | ¡S rF   )r    Ú
__reduce__rD   r   r   r   r›   _  s    zdok_matrix.__reduce__c                 C   sN   |d urt dƒ‚| j\}}| j||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 rF   r   ©r?   ÚleftÚrightr4   r   r   r   r@   q  s   
ÿz'dok_matrix.transpose.<locals>.<genexpr>)rI   r(   r^   r   r    r'   r�   )r.   Zaxesr   r/   r0   r~   r   r   r   Ú	transposei  s    
ÿzdok_matrix.transposec                 C   s<   | j \}}| j||f| jd�}t |dd„ |  ¡ D ƒ¡ |S )zReturn the conjugate transpose.r   c                 s   s(   | ] \\}}}||ft  |¡fV  qd S rF   )r)   Zconjrœ   r   r   r   r@   {  s   
ÿz(dok_matrix.conjtransp.<locals>.<genexpr>)r(   r^   r   r    r'   r�   )r.   r/   r0   r~   r   r   r   Ú
conjtranspw  s    
ÿzdok_matrix.conjtranspc                 C   s"   | j | j| jd�}t || ¡ |S )Nr   )r^   r(   r   r    r'   r‹   r   r   r   r     s    zdok_matrix.copyc                 C   sª   | j dkr| j| 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�}| j|||ff| j| jd�}d	|_
|S )
Nr   r   )Úmaxval)r   Úcountc                 s   s   | ]\}}|V  qd S rF   r   )r?   rM   Ú_r   r   r   r@   Œ  rA   z#dok_matrix.tocoo.<locals>.<genexpr>c                 s   s   | ]\}}|V  qd S rF   r   )r?   r£   rN   r   r   r   r@   �  rA   )r(   r   T)Znnzr-   r(   r   r   Úmaxr)   ZfromiterrC   r_   Zhas_canonical_format)r.   r   Z	idx_dtyper5   rR   rS   ÚAr   r   r   Útocoo†  s    
  ÿzdok_matrix.tocooc                 C   s   |r|   ¡ S | S rF   r   ©r.   r   r   r   r   r&   –  s    zdok_matrix.todokc                 C   s   | j dd�j|d�S r7   )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 rF   )r   r(   rj   r_   r%   )r.   r(   ZnewMZnewNr/   r0   rM   rN   r   r   r   Úresize¢  s    
zdok_matrix.resize)NNF)N)rG   )NF)F)F)F).Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   r!   r'   r6   r;   Úpropertyr   Z	get_shaper(   r>   rE   r   rK   rT   rY   rZ   rW   rg   rh   rk   rl   r]   rt   rv   rx   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    ddl m} t| tƒp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   )Ú	dok_array)Z_arraysr®   r#   r   )r   r®   r   r   r   r   °  s    )r¬   Z__docformat__Ú__all__rr   Ú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    ,   