U
    Ë»|e>  ã                   @   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   úN/var/www/website-v5/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

    Ú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   ÚtodokÚastypeÚupdateÚshapeÚnpÚasarrayÚ	ExceptionÚ	TypeErrorÚlenÚ_coo_container)	ÚselfÚ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)r1   Ú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)   )r1   Údatar   r   r   Ú_updateq   s    zdok_matrix._updatec                 C   s8   | j |dd� | j¡}|j| _t | ¡ t | |¡ d S ©NFr   )ÚreshapeÚasformatÚformatÚ__dict__r!   Úclearr)   )r1   r*   Ú
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.)r7   r!   r   )r1   Ú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©r1   r   r   r   Úcount_nonzero…   s    zdok_matrix.count_nonzeroc                 C   s
   t  | ¡S ©N)r!   r   rK   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)r1   Úkeyr   ÚiÚjr5   r   r   r   rR   Ž   s    ,zdok_matrix.getc                 C   s   t  | ||f| j d¡¡S ©Nr   )r!   rR   r    Útype©r1   ÚrowÚcolr   r   r   Ú_get_intXint›   s    zdok_matrix._get_intXintc                 C   s   |   t||d ƒ|¡S ©Nr   ©Ú_get_sliceXsliceÚslicerX   r   r   r   Ú_get_intXslicež   s    zdok_matrix._get_intXslicec                 C   s   |   |t||d ƒ¡S r\   r]   rX   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__)r1   rY   rZ   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeÚ	col_ranger*   ÚnewdokrS   rT   ÚrirU   Zrjr   r   r   r   r^   ¤   s$    zdok_matrix._get_sliceXslicec                 C   s   |  ¡ }|  |g|¡S rM   ©Úsqueezerd   rX   r   r   r   Ú_get_intXarray¼   s    zdok_matrix._get_intXarrayc                 C   s   |  ¡ }|  ||g¡S rM   rn   rX   r   r   r   Ú_get_arrayXintÀ   s    zdok_matrix._get_arrayXintc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S rV   ©Úlistrc   rb   r*   rd   rX   r   r   r   Ú_get_sliceXarrayÄ   s    zdok_matrix._get_sliceXarrayc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S r\   rr   rX   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   )re   r/   r    Ú	enumerater!   rR   rj   )	r1   rY   rZ   rl   rT   ÚrrU   ÚcÚvr   r   r   rd   Ì   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+   Ú
atleast_2dÚbroadcast_arraysre   r*   r    Ú	itertoolsÚproductrc   r!   rR   rj   )r1   rY   rZ   rT   rU   rl   rS   ry   r   r   r   Ú_get_arrayXarray×   s    $zdok_matrix._get_arrayXarrayc                 C   s2   ||f}|rt  | ||¡ nt  | |¡r.| |= d S rM   )r!   rj   Ú__contains__)r1   rY   rZ   r   rS   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 rV   )
rs   rz   rh   Úravelr!   r)   Úzipr+   Únonzerori   )r1   rY   rZ   r   rT   rS   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)Úoverc                 3   s"   | ]}|ˆ | ˆ|  fV  qd S rM   r   ©rG   Úk©ÚnewÚotherr   r   rH   
  s     z%dok_matrix.__add__.<locals>.<genexpr>)r   r   r    re   r*   r}   r~   rc   r!   rR   r   rP   r   r)   r+   Úerrstaterf   r   Útocscr   ÚtodenseÚNotImplemented)r1   r�   Ú	res_dtyper3   r4   rS   Ú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   r†   c                 3   s"   | ]}|ˆ| ˆ |  fV  qd S rM   r   r‰   ©r�   r1   r   r   rH   "  s     z&dok_matrix.__radd__.<locals>.<genexpr>)r   re   r*   r    r}   r~   rc   r!   rR   r   rP   r)   rf   r   r�   r   r�   r‘   )r1   r�   rŒ   r3   r4   rS   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 rM   r   r‰   rK   r   r   rH   1  s     z%dok_matrix.__neg__.<locals>.<genexpr>)r    Úkindr7   re   r*   r!   r)   rf   ©r1   rŒ   r   rK   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 rM   r   ©rG   rŠ   ry   ©r�   r   r   rH   8  s     z)dok_matrix._mul_scalar.<locals>.<genexpr>)r   r    re   r*   r!   r)   Úitems©r1   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ž   )r1   r�   ÚresultrT   rU   ry   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�   Úresult_shapeÚresult_dtyper¢   rT   rU   ry   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 rM   r   rœ   r�   r   r   rH   M  s     z&dok_matrix.__imul__.<locals>.<genexpr>©r   r!   r)   rž   r‘   ©r1   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 rM   r   rœ   r�   r   r   rH   U  s     z)dok_matrix.__truediv__.<locals>.<genexpr>)	r   r   r    re   r*   r!   r)   rž   Ú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 rM   r   rœ   r�   r   r   rH   [  s     z*dok_matrix.__itruediv__.<locals>.<genexpr>r§   r¨   r   r�   r   Ú__itruediv__Y  s    zdok_matrix.__itruediv__c                 C   s
   t  | ¡S rM   )r!   Ú
__reduce__rK   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 rM   r   ©rG   ÚleftÚrightr8   r   r   r   rH   q  s   
ÿz'dok_matrix.transpose.<locals>.<genexpr>)rP   r*   re   r    r!   r)   rž   )r1   Úaxesr   r3   r4   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 rM   )r+   Úconjr®   r   r   r   rH   {  s   
ÿz(dok_matrix.conjtransp.<locals>.<genexpr>)r*   re   r    r!   r)   rž   )r1   r3   r4   rŒ   r   r   r   Ú
conjtranspw  s    
ÿzdok_matrix.conjtranspc                 C   s"   | j | j| jd�}t || ¡ |S )Nr   )re   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 rM   r   )rG   rT   Ú_r   r   r   rH   Œ  s     z#dok_matrix.tocoo.<locals>.<genexpr>c                 s   s   | ]\}}|V  qd S rM   r   )rG   r·   rU   r   r   r   rH   �  s     )r*   r    T)Únnzr0   r*   r    r   Úmaxr+   ÚfromiterrJ   rf   Úhas_canonical_format)r1   r   Ú	idx_dtyper9   rY   rZ   ÚAr   r   r   Útocoo†  s    
  
  ÿzdok_matrix.tocooc                 C   s   |r|   ¡ S | S rM   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 rM   )r   r*   rs   rf   r&   )r1   r*   ZnewMZnewNr3   r4   rT   rU   r   r   r   Úresize¢  s    
zdok_matrix.resize)NNF)N)rN   )NF)F)F)F).Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   r"   r)   r:   rB   Úpropertyr   Ú	get_shaper*   rF   rL   r   rR   r[   r`   ra   r^   rp   rq   rt   ru   rd   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      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)Ú_arraysrÇ   r$   r   )r   rÇ   r   r   r   r   °  s    )rÄ   Ú__docformat__Ú__all__r}   Únumpyr+   Ú_baser   r   Ú_indexr   Ú_sputilsr   r	   r
   r   r   r   r   r   r   Úoperatorr   r   ÚImportErrorr!   r   r   r   r   r   r   Ú<module>   s    ,   