U
    »mœd>  ã                   @   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k
r”   dd„ ZY nX 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/sam/Atlas/atlas_env/lib/python3.8/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   s2  t  | ¡ t | ¡ t|td�| _t|tƒrLt|ƒrL|\}}t	||fƒ| _
nât|ƒrªt|ƒrj|rj| ¡ }n| ¡ }|d k	rˆ|j|dd�}t  | |¡ t	|jƒ| _
|j| _n„zt |¡}W n, tk
rä } ztdƒ|‚W 5 d }~X Y nX 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 k	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>†   s     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   rC   r   r   r   r   ‹   s    zdok_matrix.__len__ç        c              
   C   s–   z |\}}t |ƒrt |ƒst‚W n2 tttfk
rR } ztdƒ|‚W 5 d}~X Y nX |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   rJ   Ž   s    ,zdok_matrix.getc                 C   s   t  | ||f| j d¡¡S ©Nr   )r    rJ   r   Útype©r.   ÚrowÚcolr   r   r   Ú_get_intXint›   s    zdok_matrix._get_intXintc                 C   s   |   t||d ƒ|¡S ©Nr   ©Ú_get_sliceXsliceÚslicerP   r   r   r   Ú_get_intXslicež   s    zdok_matrix._get_intXslicec                 C   s   |   |t||d ƒ¡S rT   rU   rP   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.   rQ   rR   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeZ	col_ranger(   ÚnewdokrK   rL   ÚrirM   Zrjr   r   r   r   rV   ¤   s$    zdok_matrix._get_sliceXslicec                 C   s   |  ¡ }|  |g|¡S rE   ©Zsqueezer\   rP   r   r   r   Ú_get_intXarray¼   s    zdok_matrix._get_intXarrayc                 C   s   |  ¡ }|  ||g¡S rE   re   rP   r   r   r   Ú_get_arrayXintÀ   s    zdok_matrix._get_arrayXintc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S rN   ©Úlistr[   rZ   r(   r\   rP   r   r   r   Ú_get_sliceXarrayÄ   s    zdok_matrix._get_sliceXarrayc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S rT   rh   rP   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    rJ   rb   )	r.   rQ   rR   rc   rL   ÚrrM   Ú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    rJ   rb   )r.   rQ   rR   rL   rM   rc   rK   ro   r   r   r   Ú_get_arrayXarray×   s    $zdok_matrix._get_arrayXarrayc                 C   s2   ||f}|rt  | ||¡ nt  | |¡r.| |= d S rE   )r    rb   Ú__contains__)r.   rQ   rR   r   rK   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 rN   )
ri   rp   r`   Zravelr    r'   Úzipr)   Znonzerora   )r.   rQ   rR   r   rL   rK   r   r   r   Ú_set_arrayXarrayé   s    zdok_matrix._set_arrayXarrayc              	      s$  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��& t ˆ ‡ ‡fdd„ˆ ¡ D ƒ¡ W 5 Q R X 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 rE   r   ©r?   Úk©ÚnewÚotherr   r   r@   
  s     z%dok_matrix.__add__.<locals>.<genexpr>)r   r   r   r]   r(   rq   rr   r[   r    rJ   r   rH   r   r'   r)   Zerrstater^   r   Útocscr   ÚtodenseÚNotImplemented)r.   r~   Ú	res_dtyper/   r0   rK   Ú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   rx   c                 3   s"   | ]}|ˆ| ˆ |  fV  qd S rE   r   rz   ©r~   r.   r   r   r@   "  s     z&dok_matrix.__radd__.<locals>.<genexpr>)r   r]   r(   r   rq   rr   r[   r    rJ   r   rH   r'   r^   r   r   r   r€   r�   )r.   r~   r}   r/   r0   rK   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 rE   r   rz   rC   r   r   r@   1  s     z%dok_matrix.__neg__.<locals>.<genexpr>)r   Úkindr3   r]   r(   r    r'   r^   ©r.   r}   r   rC   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 rE   r   ©r?   r{   ro   ©r~   r   r   r@   8  s     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~   ÚresultrL   rM   ro   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’   rL   rM   ro   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 rE   r   rŒ   r�   r   r   r@   M  s     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 rE   r   rŒ   r�   r   r   r@   U  s     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 rE   r   rŒ   r�   r   r   r@   [  s     z*dok_matrix.__itruediv__.<locals>.<genexpr>r•   r–   r   r�   r   Ú__itruediv__Y  s    zdok_matrix.__itruediv__c                 C   s
   t  | ¡S rE   )r    Ú
__reduce__rC   r   r   r   rš   _  s    zdok_matrix.__reduce__c                 C   sN   |d k	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 rE   r   ©r?   ÚleftÚrightr4   r   r   r   r@   q  s   
ÿz'dok_matrix.transpose.<locals>.<genexpr>)rH   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 rE   )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 rE   r   )r?   rL   Ú_r   r   r   r@   Œ  s     z#dok_matrix.tocoo.<locals>.<genexpr>c                 s   s   | ]\}}|V  qd S rE   r   )r?   r¢   rM   r   r   r   r@   �  s     )r(   r   T)Znnzr-   r(   r   r   Úmaxr)   ZfromiterrB   r^   Zhas_canonical_format)r.   r   Z	idx_dtyper5   rQ   rR   ÚAr   r   r   Útocoo†  s    
  
  ÿzdok_matrix.tocooc                 C   s   |r|   ¡ S | S rE   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 rE   )r   r(   ri   r^   r%   )r.   r(   ZnewMZnewNr/   r0   rL   rM   r   r   r   Úresize¢  s    
zdok_matrix.resize)NNF)N)rF   )NF)F)F)F).Ú__name__Ú
__module__Ú__qualname__Ú__doc__r8   r!   r'   r6   r;   Úpropertyr   Ú	get_shaper(   r>   rD   r   rJ   rS   rX   rY   rV   rf   rg   rj   rk   r\   rs   ru   rw   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«   Ú__docformat__Ú__all__rq   Ú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    ,   