3

6^=                 @   s   d Z ddlmZmZmZ dZddgZddlZddlZ	ddl
mZmZmZmZm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 y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 matrix    )divisionprint_functionabsolute_importzrestructuredtext en
dok_matrixisspmatrix_dokN)zipxrange	iteritemsiterkeys
itervalues   )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   1/tmp/pip-build-vw4w4j08/scipy/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   s(  t j|  tj|  t|td| _t|trLt|rL|\}}t	||f| _
nt|rt|rj|rj|j }n|j }|d k	r|j|}t j| | t	|j| _
|j| _n~ytj|}W n tk
r   tdY nX t|jdkrtdddlm} |||dj }t j| | t	|j| _
|j| _d S )N)defaultzInvalid input format.   z(Expected rank <=2 dense array or matrix.r   )
coo_matrix)dtype)dict__init__r   r   floatr%   
isinstancetupler   r   _shaper   r   copytodokZastypeupdateshapenpZasarray	Exception	TypeErrorlencoor$   )	selfZarg1r/   r%   r,   MNr$   dr   r   r    r'   N   s4    




zdok_matrix.__init__c             C   s   t dd S )Nz9Direct modification to dok_matrix element is not allowed.)NotImplementedError)r5   valr   r   r    r.   q   s    zdok_matrix.updatec             C   s   t j| |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.   )r5   datar   r   r    _updatev   s    zdok_matrix._updatec             C   s8   | j |ddj| j}|j| _tj|  tj| | d S )NF)r,   )ZreshapeZasformatformat__dict__r&   clearr.   )r5   r/   Z
new_matrixr   r   r    	set_shape|   s    
zdok_matrix.set_shape)fgetfsetc             C   s   |d k	rt dtj| S )Nz6getnnz over an axis is not implemented for DOK format.)r9   r&   r   )r5   Zaxisr   r   r    getnnz   s    zdok_matrix.getnnzc             C   s   t dd t| 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>)sumr   )r5   r   r   r    count_nonzero   s    zdok_matrix.count_nonzeroc             C   s
   t j| S )N)r&   r   )r5   r   r   r    r      s    zdok_matrix.__len__        c             C   s   y |\}}t |rt |stW n" tttfk
rB   tdY nX |dk sp|| jd ksp|dk sp|| jd krxtdtj| ||S )ztThis overrides the dict.get method, providing type checking
        but otherwise equivalent functionality.
        z!Index must be a pair of integers.r   r   zIndex out of bounds.)r   AssertionErrorr2   
ValueError
IndexErrorr/   r&   get)r5   keyr"   ijr   r   r    rL      s    ,zdok_matrix.getc             C   s   t j| ||f| jjdS )Nr   )r&   rL   r%   type)r5   rowcolr   r   r    _get_intXint   s    zdok_matrix._get_intXintc             C   s   | j t||d |S )Nr   )_get_sliceXsliceslice)r5   rQ   rR   r   r   r    _get_intXslice   s    zdok_matrix._get_intXslicec             C   s   | j |t||d S )Nr   )rT   rU   )r5   rQ   rR   r   r   r    _get_sliceXint   s    zdok_matrix._get_sliceXintc             C   s0  |j | jd \}}}|j | jd \}}}t|||}	t|||}
t|	t|
f}t| d|d  |d  kr|| j|	|
S t|| jd}xt| 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
j| |}t
j|||f| qW |S )Nr   r   r#   )r%   )indicesr/   r   r3   _get_columnXarrayr   r%   r
   divmodintr&   __getitem____setitem__)r5   rQ   rR   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeZ	col_ranger/   newdokrM   rN   rirO   Zrjr   r   r   r    rT      s$    zdok_matrix._get_sliceXslicec             C   s   | j |g|S )N)rY   )r5   rQ   rR   r   r   r    _get_intXarray   s    zdok_matrix._get_intXarrayc             C   s   | j ||gS )N)rY   )r5   rQ   rR   r   r   r    _get_arrayXint   s    zdok_matrix._get_arrayXintc             C   s$   t t|j| jd  }| j||S )Nr   )listrangerX   r/   rY   )r5   rQ   rR   r   r   r    _get_sliceXarray   s    zdok_matrix._get_sliceXarrayc             C   s$   t t|j| jd  }| j||S )Nr   )rb   rc   rX   r/   rY   )r5   rQ   rR   r   r   r    _get_arrayXslice   s    zdok_matrix._get_arrayXslicec       	      C   sr   t t|t|f| jd}xRt|D ]F\}}x<t|D ]0\}}tj| ||fd}|r6tj|||f| q6W q$W |S )N)r%   r   )r   r3   r%   	enumerater&   rL   r]   )	r5   rQ   rR   r^   rN   rrO   cvr   r   r    rY      s    zdok_matrix._get_columnXarrayc             C   s   t tjtj||\}}t|j| jd}xTtjt	|jd t	|jd D ]0}t
j| || || fd}|rJt
j||| qJW |S )N)r%   r   r   )mapr0   Z
atleast_2dZbroadcast_arraysr   r/   r%   	itertoolsproductr   r&   rL   r]   )r5   rQ   rR   rN   rO   r^   rM   ri   r   r   r    _get_arrayXarray   s    &zdok_matrix._get_arrayXarrayc             C   s2   ||f}|rt j| || nt j| |r.| |= d S )N)r&   r]   __contains__)r5   rQ   rR   r   rM   r   r   r    _set_intXint   s
    zdok_matrix._set_intXintc             C   s   t tt|j }t tt|j }|j }tj| tt||| x@tj|dkd D ]*}|| || f}tj	| |dkrX| |= qXW d S )Nr   )
rb   rj   r[   Zravelr&   r.   izipr0   Znonzeror\   )r5   rQ   rR   r   rN   rM   r   r   r    _set_arrayXarray   s    zdok_matrix._set_arrayXarrayc                s$  t rjt| j}t| j|d | j\}}xtjt|t|D ]"}tj	| |d }|rB| |< qBW nt
rj| jkrtdt| jj}t| j|d tj |  tjdd& tj  fddtD  W d Q R X n8tr| j }|  ntr| j   ntS  S )N)r%   r   z Matrix dimensions are not equal.ignore)Zoverc             3   s"   | ]}| | |  fV  qd S )Nr   )rD   k)newotherr   r    rE     s    z%dok_matrix.__add__.<locals>.<genexpr>)r   r   r%   r   r/   rk   rl   r   r&   rL   r   rJ   r   r.   r0   Zerrstater
   r   tocscr   todenseNotImplemented)r5   ru   	res_dtyper6   r7   rM   aijcscr   )rt   ru   r    __add__   s0    
&


zdok_matrix.__add__c                s   t  r`tjjd}j\}}xtjt|t|D ]"}tj|d  }|r8|||< q8W nt	 r jjkr|t
dtjjd}tj| tj| fddt D  n4t rԈj }|  }nt r j  }ntS |S )N)r%   r   z Matrix dimensions are not equal.c             3   s"   | ]}||  |  fV  qd S )Nr   )rD   rs   )ru   r5   r   r    rE   %  s    z&dok_matrix.__radd__.<locals>.<genexpr>)r   r   r/   r%   rk   rl   r   r&   rL   r   rJ   r.   r
   r   rv   r   rw   rx   )r5   ru   rt   r6   r7   rM   rz   r{   r   )ru   r5   r    __radd__  s*    

zdok_matrix.__radd__c                sF    j jdkrtdt j j d}tj| fddt D  |S )Nbz2Negating a sparse boolean matrix is not supported.)r%   c             3   s   | ]}| |  fV  qd S )Nr   )rD   rs   )r5   r   r    rE   4  s    z%dok_matrix.__neg__.<locals>.<genexpr>)r%   kindr9   r   r/   r&   r.   r
   )r5   rt   r   )r5   r    __neg__/  s
    zdok_matrix.__neg__c                s<   t | j }t| j|d}tj| fddt| D  |S )N)r%   c             3   s   | ]\}}||  fV  qd S )Nr   )rD   rs   ri   )ru   r   r    rE   ;  s    z)dok_matrix._mul_scalar.<locals>.<genexpr>)r   r%   r   r/   r&   r.   r	   )r5   ru   ry   rt   r   )ru   r    _mul_scalar7  s    zdok_matrix._mul_scalarc             C   sT   t j| jd t| j|jd}x0t| D ]$\\}}}||  |||  7  < q(W |S )Nr   )r%   )r0   zerosr/   r   r%   r	   )r5   ru   resultrN   rO   ri   r   r   r    _mul_vector>  s    zdok_matrix._mul_vectorc          	   C   sv   | j d |j d f}t| j|j}tj||d}x@t| D ]4\\}}}||d d f  |||d d f  7  < q:W |S )Nr   r   )r%   )r/   r   r%   r0   r   r	   )r5   ru   Zresult_shapeZresult_dtyper   rN   rO   ri   r   r   r    _mul_multivectorE  s    ,zdok_matrix._mul_multivectorc                s.   t  r*tj|  fddt| D  | S tS )Nc             3   s   | ]\}}||  fV  qd S )Nr   )rD   rs   ri   )ru   r   r    rE   P  s    z&dok_matrix.__imul__.<locals>.<genexpr>)r   r&   r.   r	   rx   )r5   ru   r   )ru   r    __imul__N  s    zdok_matrix.__imul__c                sP   t  rDt| j }t| j|d}tj| fddt| D  |S | j   S )N)r%   c             3   s   | ]\}}||  fV  qd S )Nr   )rD   rs   ri   )ru   r   r    rE   X  s    z)dok_matrix.__truediv__.<locals>.<genexpr>)	r   r   r%   r   r/   r&   r.   r	   Ztocsr)r5   ru   ry   rt   r   )ru   r    __truediv__T  s    zdok_matrix.__truediv__c                s.   t  r*tj|  fddt| D  | S tS )Nc             3   s   | ]\}}||  fV  qd S )Nr   )rD   rs   ri   )ru   r   r    rE   ^  s    z*dok_matrix.__itruediv__.<locals>.<genexpr>)r   r&   r.   r	   rx   )r5   ru   r   )ru   r    __itruediv__\  s    zdok_matrix.__itruediv__c             C   s
   t j| S )N)r&   
__reduce__)r5   r   r   r    r   b  s    zdok_matrix.__reduce__c             C   sL   |d k	rt d| j\}}t||f| j|d}tj|dd t| 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 )Nr   )rD   leftrightr:   r   r   r    rE   t  s   z'dok_matrix.transpose.<locals>.<genexpr>)rJ   r/   r   r%   r&   r.   r	   )r5   Zaxesr,   r6   r7   rt   r   r   r    	transposel  s    
zdok_matrix.transposec             C   s:   | j \}}t||f| jd}tj|dd t| D  |S )zReturn the conjugate transpose.)r%   c             s   s(   | ] \\}}}||ft j|fV  qd S )N)r0   Zconj)rD   r   r   r:   r   r   r    rE   ~  s   z(dok_matrix.conjtransp.<locals>.<genexpr>)r/   r   r%   r&   r.   r	   )r5   r6   r7   rt   r   r   r    
conjtranspz  s
    
zdok_matrix.conjtranspc             C   s    t | j| jd}tj||  |S )N)r%   )r   r/   r%   r&   r.   )r5   rt   r   r   r    r,     s    zdok_matrix.copyc             C   s   ddl m} | jdkr&|| j| jdS tt| jd}tjt	| | j| jd}tjdd t
| D || jd}tjd	d t
| D || jd}||||ff| j| jd
}d|_|S )Nr   )r$   r   )r%   )maxval)r%   countc             s   s   | ]\}}|V  qd S )Nr   )rD   rN   _r   r   r    rE     s    z#dok_matrix.tocoo.<locals>.<genexpr>c             s   s   | ]\}}|V  qd S )Nr   )rD   r   rO   r   r   r    rE     s    )r/   r%   T)r4   r$   Znnzr/   r%   r   maxr0   Zfromiterr   r
   Zhas_canonical_format)r5   r,   r$   Z	idx_dtyper;   rQ   rR   Ar   r   r    tocoo  s    
  zdok_matrix.tocooc             C   s   |r| j  S | S )N)r,   )r5   r,   r   r   r    r-     s    zdok_matrix.todokc             C   s   | j ddj|dS )NF)r,   )r   rv   )r5   r,   r   r   r    rv     s    zdok_matrix.tocscc             G   sh   t |}|\}}| j\}}||k s*||k r^x2tt| D ]"\}}||ksP||kr8| ||f= q8W || _d S )N)r   r/   rb   r
   r+   )r5   r/   ZnewMZnewNr6   r7   rN   rO   r   r   r    resize  s    
zdok_matrix.resize)NNF)N)rH   )NF)F)F)F).__name__
__module____qualname____doc__r=   r'   r.   r<   r@   propertyr   Z	get_shaper/   rC   rG   r   rL   rS   rV   rW   rT   r`   ra   rd   re   rY   rm   ro   rq   r|   r}   r   r   r   r   r   r   r   r   r   r   r,   r   r-   rv   r   r   r   r   r    r      s`   0
#



	









c             C   s
   t | 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)   r   )r   r   r   r    r     s    )'r   
__future__r   r   r   Z__docformat____all__rk   Znumpyr0   Zscipy._lib.sixr   rp   r   r	   r
   r   baser   r   _indexr   Zsputilsr   r   r   r   r   r   r   r   r   operatorr   r!   ImportErrorr&   r   r   r   r   r   r    <module>   s$   ,   