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| _
nt|rt|rj|rj| }n| }|d k	r|j|dd}t | | t	|j| _
|j| _nzt|}W n, tk
r } ztd|W 5 d }~X Y nX t|jdkrt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 )zAn 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rt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rqtt	|d | |\}}|dks|dk s||d kr
qt
| |}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| |< qBnt
rj| jkrt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|||< q8nt	 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    ,   