o
    Eb>                     @   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yI   dd ZY nw 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   3/usr/lib/python3/dist-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*_dUdKdLZ+e
j+je+_dUdMdNZ,e
j,je,_dOdP Z-e
j-je-_dS )Vr   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r't|r'|\}}t	||f| _
d S t|rWt|r6|r6| }n| }|d urE|j|dd}t | | t	|j| _
|j| _d S zt|}W n tyo } ztd|d }~ww 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   s6   



zdok_matrix.__init__c                 C   s   t d)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 )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'   )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      zdok_matrix.count_nonzeroc                 C   
   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 n tttfy$ } ztd|d}~ww |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   rM      s   
,zdok_matrix.getc                 C   s   t | ||f| jdS Nr   )r    rM   r   typer.   rowcolr   r   r   _get_intXint   s   zdok_matrix._get_intXintc                 C   s   |  t||d |S Nr   _get_sliceXsliceslicerS   r   r   r   _get_intXslice   rF   zdok_matrix._get_intXslicec                 C   s   |  |t||d S rW   rX   rS   r   r   r   _get_sliceXint   rF   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 ]I}tt	|d | |\}}|dksg|dk sg||d krhqJtt	|d | |\}}|dks|dk s||d krqJt
| |}t
|||f| qJ|S )Nr   r   r   r   )indicesr(   ranger,   _get_columnXarray_dok_containerr   keysdivmodintr    __getitem____setitem__)r.   rT   rU   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeZ	col_ranger(   newdokrN   rO   ZrirP   Zrjr   r   r   r   rY      s$   zdok_matrix._get_sliceXslicec                 C   s   |  }| |g|S rH   Zsqueezer_   rS   r   r   r   _get_intXarray      zdok_matrix._get_intXarrayc                 C   s   |  }| ||gS rH   rg   rS   r   r   r   _get_arrayXint   ri   zdok_matrix._get_arrayXintc                 C   s$   t t|| jd  }| ||S rQ   listr^   r]   r(   r_   rS   r   r   r   _get_sliceXarray      zdok_matrix._get_sliceXarrayc                 C   s$   t t|| jd  }| ||S rW   rk   rS   r   r   r   _get_arrayXslice   rn   zdok_matrix._get_arrayXslicec           	      C   sl   | j t|t|f| jd}t|D ]!\}}t|D ]\}}t| ||fd}|r2t|||f| qq|S )Nr   r   )r`   r,   r   	enumerater    rM   re   )	r.   rT   rU   rf   rO   rrP   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 ]}t
| || || fd}|r=t
||| q%|S )Nr   r   r   )mapr)   Z
atleast_2dZbroadcast_arraysr`   r(   r   	itertoolsproductr^   r    rM   re   )r.   rT   rU   rO   rP   rf   rN   rs   r   r   r   _get_arrayXarray   s   $zdok_matrix._get_arrayXarrayc                 C   s8   ||f}|rt | || d S t | |r| |= d S d S rH   )r    re   __contains__)r.   rT   rU   r   rN   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r@| |= q+d S rQ   )
rl   rt   rc   Zravelr    r'   zipr)   Znonzerord   )r.   rT   rU   r   rO   rN   r   r   r   _set_arrayXarray   s   zdok_matrix._set_arrayXarrayc                    s:  t r5t| j}| j| j|d | j\}}tt|t|D ]}t	| |d }|r2| |< q! S t
rj| jkrCtdt| jj}| j| j|d t |  tjdd t  fdd D  W d     S 1 szw   Y   S tr|  }|   S tr|     S tS )Nr   r    Matrix dimensions are not equal.ignore)Zoverc                 3   s$    | ]}| | |  fV  qd S rH   r   r?   knewotherr   r   r@   
     " z%dok_matrix.__add__.<locals>.<genexpr>)r   r   r   r`   r(   ru   rv   r^   r    rM   r   rK   r   r'   r)   Zerrstatera   r   tocscr   todenseNotImplemented)r.   r   	res_dtyper/   r0   rN   aijcscr   r   r   __add__   s@   



zdok_matrix.__add__c                    s   t  r0jjjd}j\}}tt|t|D ]}t|d  }|r-|||< q|S t	 r_ jjkr>t
djjjd}t| t| fdd  D  |S t rm }|  }|S t ry   }|S tS )Nr   r   r|   c                 3   s$    | ]}||  |  fV  qd S rH   r   r~   r   r.   r   r   r@   "  r   z&dok_matrix.__radd__.<locals>.<genexpr>)r   r`   r(   r   ru   rv   r^   r    rM   r   rK   r'   ra   r   r   r   r   r   )r.   r   r   r/   r0   rN   r   r   r   r   r   __radd__  s4   
	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 rH   r   r~   rD   r   r   r@   1  s    z%dok_matrix.__neg__.<locals>.<genexpr>)r   kindr3   r`   r(   r    r'   ra   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        | ]\}}||  fV  qd S rH   r   r?   r   rs   r   r   r   r@   8      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   resultrO   rP   rs   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 ]\\}}}||d d f  |||d d f  7  < q|S )Nr   r   r   )r(   r   r   r)   r   r   )r.   r   Zresult_shapeZresult_dtyper   rO   rP   rs   r   r   r   _mul_multivectorB  s   *zdok_matrix._mul_multivectorc                    .   t  rt|  fdd|  D  | S tS )Nc                 3   r   rH   r   r   r   r   r   r@   M  r   z&dok_matrix.__imul__.<locals>.<genexpr>r   r    r'   r   r   r.   r   r   r   r   __imul__K     zdok_matrix.__imul__c                    sR   t  r#t| j }| j| j|d}t| fdd|  D  |S |    S )Nr   c                 3        | ]\}}||  fV  qd S rH   r   r   r   r   r   r@   U  r   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                    r   )Nc                 3   r   rH   r   r   r   r   r   r@   [  r   z*dok_matrix.__itruediv__.<locals>.<genexpr>r   r   r   r   r   __itruediv__Y  r   zdok_matrix.__itruediv__c                 C   rG   rH   )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 rH   r   r?   leftrightr4   r   r   r   r@   q  s    
z'dok_matrix.transpose.<locals>.<genexpr>)rK   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 rH   )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   )Zmaxval)r   countc                 s   s    | ]\}}|V  qd S rH   r   )r?   rO   _r   r   r   r@     rA   z#dok_matrix.tocoo.<locals>.<genexpr>c                 s   s    | ]\}}|V  qd S rH   r   )r?   r   rP   r   r   r   r@     rA   )r(   r   T)Znnzr-   r(   r   r   maxr)   ZfromiterrC   ra   Zhas_canonical_format)r.   r   Z	idx_dtyper5   rT   rU   Ar   r   r   tocoo  s   
  zdok_matrix.tocooc                 C   s   |r|   S | S rH   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 r-t|  D ]\}}||ks'||kr,| ||f= q|| _d S rH   )r   r(   rl   ra   r%   )r.   r(   ZnewMZnewNr/   r0   rO   rP   r   r   r   resize  s   


zdok_matrix.resize)NNFrH   )rI   )NF)F).__name__
__module____qualname____doc__r8   r!   r'   r6   r;   propertyr   Z	get_shaper(   r>   rE   r   rM   rV   r[   r\   rY   rh   rj   rm   ro   r_   rw   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r&   r   r   r   r   r   r   r      sb    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__ru   Znumpyr)   Z_baser   r   Z_indexr   Z_sputilsr   r	   r
   r   r   r   r   r   r   operatorr   r   ImportErrorr    r   r   r   r   r   r   <module>   s$    ,   