a
    ˆù»jYY  ã                   @   s¸   d Z dZg d¢ZddlZddlZddlmZ ddlm	Z	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 G d
d„ de	eeƒZdd„ ZG dd„ dee
ƒZG dd„ deeƒZdS )zDictionary Of Keys based matrixzrestructuredtext en)Ú	dok_arrayÚ
dok_matrixÚisspmatrix_doké    Né   )Úspmatrix)Ú_spbaseÚsparrayÚissparse)Ú
IndexMixin)ÚisdenseÚgetdtypeÚisshapeÚ	isintlikeÚisscalarlikeÚupcastÚupcast_scalarÚcheck_shapec                       sB  e Zd ZdZdwdd„Zdd„ Zdxdd	„Zd
d„ Zejj	e_	ejj	e_	dd„ Z
dd„ Zdyd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dzd)d*„Z‡ f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$‡ fdCdD„Z%dEdF„ Z&dGdH„ Z'dIdJ„ Z(dKdL„ Z)dMdN„ Z*dOdP„ Z+dQdR„ Z,dSdT„ Z-dUdV„ Z.dWdX„ Z/dYdZ„ Z0d[d\„ Z1d]d^„ Z2d{‡ fd`da„	Z3d|dbdc„Z4ej4j	e4_	ddde„ Z5dfdg„ Z6ej6j	e6_	e7d}didj„ƒZ8d~dkdl„Z9ej9j	e9_	ddmdn„Z:ej:j	e:_	d€dodp„Z;ej;j	e;_	dqdr„ Z<ej<j	e<_	d�dudv„Z=‡  Z>S )‚Ú	_dok_baseÚdokNFc              
   C   s|  t  | ¡ t| tƒ}t|tƒrPt||d�rPt||d�| _i | _t	|t
d�| _�n(t|ƒr¶|j| jkrv|rp| ¡ n|}n| ¡ }|d ur”|j|dd�}|j| _t|j|d�| _|j| _nÂzt |¡}W n. tyò } ztdƒ|‚W Y d }~n
d }~0 0 |jdk�rtdƒ‚|jdk�rF|d u�r(| |¡}d	d
„ t|ƒD ƒ| _|j| _n"| j||d� ¡ }|j| _|j| _t|j|d�| _d S )N©Zallow_1d)ÚdefaultF©ÚcopyzInvalid input format.é   z(Expected rank <=2 dense array or matrix.r   c                 S   s   i | ]\}}|d kr||“qS )r   © )Ú.0ÚiÚvr   r   ú7E:\soccer\ml_env\Lib\site-packages\scipy/sparse/_dok.pyÚ
<dictcomp>4   ó    z&_dok_base.__init__.<locals>.<dictcomp>©Údtype)r   Ú__init__Ú
isinstancer   Útupler   r   Ú_shapeÚ_dictr   Úfloatr"   r	   Úformatr   ÚtodokÚastypeÚshapeÚnpZasarrayÚ	ExceptionÚ	TypeErrorÚndimÚ	enumerateÚ_coo_container)ÚselfZarg1r,   r"   r   Úis_arrayÚeÚdr   r   r   r#      s<    


 


z_dok_base.__init__c                 C   s   t dƒ‚d S )Nz2Direct update to DOK sparse format is not allowed.)ÚNotImplementedError)r3   Úvalr   r   r   Úupdate<   s    z_dok_base.updatec                 C   s   |d urt dƒ‚t| jƒS )Nz7_getnnz over an axis is not implemented for DOK format.)r7   Úlenr'   )r3   Zaxisr   r   r   Ú_getnnz@   s
    ÿz_dok_base._getnnzc                 C   s   t dd„ |  ¡ D ƒƒS )Nc                 s   s   | ]}|d kV  qdS ©r   Nr   )r   Úxr   r   r   Ú	<genexpr>H   r    z*_dok_base.count_nonzero.<locals>.<genexpr>)ÚsumÚvalues©r3   r   r   r   Úcount_nonzeroG   s    z_dok_base.count_nonzeroc                 C   s
   t | jƒS ©N)r:   r'   rA   r   r   r   Ú__len__M   s    z_dok_base.__len__c                 C   s
   || j v S rC   ©r'   ©r3   Úkeyr   r   r   Ú__contains__P   s    z_dok_base.__contains__c                C   s   | j  ||¡S rC   )r'   Ú
setdefault)r3   rG   r   r   r   r   rI   S   s    z_dok_base.setdefaultc                C   s   | j |= d S rC   rE   rF   r   r   r   Ú__delitem__V   s    z_dok_base.__delitem__c                 C   s
   | j  ¡ S rC   )r'   ÚclearrA   r   r   r   rK   Y   s    z_dok_base.clearc                G   s   | j j|Ž S rC   )r'   Úpop)r3   Úargsr   r   r   rL   \   s    z_dok_base.popc                 C   s   t dƒ‚d S )Nz*reversed is not defined for dok_array type)r/   rA   r   r   r   Ú__reversed___   s    z_dok_base.__reversed__c                 C   s,   t | ƒj› dt |ƒj› �}td|› �ƒ‚d S ©Nz and z unsupported operand type for |: ©ÚtypeÚ__name__r/   ©r3   ÚotherZ
type_namesr   r   r   Ú__or__b   s    z_dok_base.__or__c                 C   s,   t | ƒj› dt |ƒj› �}td|› �ƒ‚d S rO   rP   rS   r   r   r   Ú__ror__f   s    z_dok_base.__ror__c                 C   s,   t | ƒj› dt |ƒj› �}td|› �ƒ‚d S rO   rP   rS   r   r   r   Ú__ior__j   s    z_dok_base.__ior__c                 C   s
   | j  ¡ S rC   )r'   ÚpopitemrA   r   r   r   rX   n   s    z_dok_base.popitemc                 C   s
   | j  ¡ S rC   )r'   ÚitemsrA   r   r   r   rY   q   s    z_dok_base.itemsc                 C   s
   | j  ¡ S rC   )r'   ÚkeysrA   r   r   r   rZ   t   s    z_dok_base.keysc                 C   s
   | j  ¡ S rC   )r'   r@   rA   r   r   r   r@   w   s    z_dok_base.valuesç        c              
   C   sö   || j v r| j | S t|ƒr,| jdkr,|f}| jt|ƒkrJtd|› d�ƒ‚z|D ]}t|ƒsPJ ‚qPW n4 tttfy˜ } ztdƒ|‚W Y d}~n
d}~0 0 tdd„ t	|| j
ƒD ƒƒ}tdd„ t	|| j
ƒD ƒƒrÖtd	ƒ‚| jdkrè|d
 }| j  ||¡S )z>This provides dict.get method functionality with type checkingr   zIndex z! length needs to match self.shapez%Index must be or consist of integers.Nc                 s   s&   | ]\}}|d k r|| n|V  qdS r<   r   ©r   r   ÚMr   r   r   r>   ‡   r    z _dok_base.get.<locals>.<genexpr>c                 s   s"   | ]\}}|d k p||kV  qdS r<   r   r\   r   r   r   r>   ˆ   r    zIndex out of bounds.r   )r'   r   r0   r:   Ú
IndexErrorÚAssertionErrorr/   Ú
ValueErrorr%   Úzipr,   ÚanyÚget)r3   rG   r   r   r5   r   r   r   rc   z   s"    

 
z_dok_base.getc                    s’   | j dkrtƒ  |¡S t|tƒr4t|ƒdkr4|d }ttjf}t||ƒr†|dk r^|| j	d 7 }|dk st|| j	d kr|t
dƒ‚|  |¡S t
dƒ‚d S )Nr   r   r   éÿÿÿÿúindex value out of boundsz4array/slice index for 1d dok_array not yet supported)r0   ÚsuperÚ__getitem__r$   r%   r:   Úintr-   Úintegerr,   r^   Ú_get_int)r3   rG   Ú	INT_TYPES©Ú	__class__r   r   rg   �   s    



z_dok_base.__getitem__c                 C   s   | j  || j d¡¡S ©Nr   ©r'   rc   r"   rQ   )r3   Úidxr   r   r   rj       s    z_dok_base._get_intc                 C   s   | j  ||f| j d¡¡S rn   ro   ©r3   ÚrowÚcolr   r   r   Ú_get_intXint¤   s    z_dok_base._get_intXintc                 C   s   |   t||d ƒ|¡S ©Nr   ©Ú_get_sliceXsliceÚslicerq   r   r   r   Ú_get_intXslice§   s    z_dok_base._get_intXslicec                 C   s   |   |t||d ƒ¡S ru   rv   rq   r   r   r   Ú_get_sliceXintª   s    z_dok_base._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”| j
| |j
||f< q”|S )Nr   r   r   r!   )Úindicesr,   Úranger:   Ú_get_columnXarrayÚ_dok_containerr"   rZ   Údivmodrh   r'   )r3   rr   rs   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeZ	col_ranger,   ÚnewdokrG   r   ÚriÚjZrjr   r   r   rw   ­   s"    z_dok_base._get_sliceXslicec                 C   s   |  ¡ }|  |g|¡S rC   ©Zsqueezer}   rq   r   r   r   Ú_get_intXarrayÄ   s    z_dok_base._get_intXarrayc                 C   s   |  ¡ }|  ||g¡S rC   rƒ   rq   r   r   r   Ú_get_arrayXintÈ   s    z_dok_base._get_arrayXintc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S rn   ©Úlistr|   r{   r,   r}   rq   r   r   r   Ú_get_sliceXarrayÌ   s    z_dok_base._get_sliceXarrayc                 C   s$   t t| | jd ¡Ž ƒ}|  ||¡S ru   r†   rq   r   r   r   Ú_get_arrayXsliceÐ   s    z_dok_base._get_arrayXslicec           	      C   sh   | j t|ƒt|ƒf| jd�}t|ƒD ]>\}}t|ƒD ],\}}| j ||fd¡}|r4||j||f< q4q$|S )Nr!   r   )r~   r:   r"   r1   r'   rc   )	r3   rr   rs   r€   r   Úrr‚   Úcr   r   r   r   r}   Ô   s    z_dok_base._get_columnXarrayc                 C   s|   t tjt ||¡ƒ\}}| j|j| jd�}t t	|jd ƒt	|jd ƒ¡D ],}| j
 || || fd¡}|rJ||j
|< qJ|S )Nr!   r   r   )Úmapr-   Z
atleast_2dZbroadcast_arraysr~   r,   r"   Ú	itertoolsÚproductr|   r'   rc   )r3   rr   rs   r   r‚   r€   rG   r   r   r   r   Ú_get_arrayXarrayß   s    $z_dok_base._get_arrayXarrayc                    s–   | j dkrtƒ  ||¡S t|tƒr6t|ƒdkr6|d }ttjf}t||ƒrŠ|dk r`|| j	d 7 }|dk sv|| j	d kr~t
dƒ‚|  ||¡S t
dƒ‚d S )Nr   r   r   rd   re   z-array index for 1d dok_array not yet provided)r0   rf   Ú__setitem__r$   r%   r:   rh   r-   ri   r,   r^   Ú_set_int)r3   rG   Úvaluerk   rl   r   r   r�   ë   s    


z_dok_base.__setitem__c                 C   s&   |r|| j |< n|| j v r"| j |= d S rC   rE   )r3   rp   r=   r   r   r   r‘   ü   s    
z_dok_base._set_intc                 C   s.   ||f}|r|| j |< n|| j v r*| j |= d S rC   rE   )r3   rr   rs   r=   rG   r   r   r   Ú_set_intXint  s
    
z_dok_base._set_intXintc                 C   s†   t tt| ¡ ƒƒ}t tt| ¡ ƒƒ}| ¡ }| j tt||ƒ|ƒ¡ t |dk¡d D ]*}|| || f}| j| dkrV| j|= qVd S rn   )	r‡   rŒ   rh   Zravelr'   r9   ra   r-   Znonzero)r3   rr   rs   r=   r   rG   r   r   r   Ú_set_arrayXarray
  s    z_dok_base._set_arrayXarrayc                    s^  t |ƒr`t| j|ƒ}| j| j|d�‰ tjdd„ | jD ƒŽ D ]"}| j |d¡| }|r:|ˆ |< q:nút	|ƒ�r>|j| jkr~t
dƒ‚t| j|jƒ}| j| j|d�‰ | j ¡ ˆ _|jdkr¼| ¡ }n8| ¡ }| jdkrât|jd |jƒ}ntt|jŽ |jƒ}tjdd	��* ˆ j ‡ fd
d„|D ƒ¡ W d   ƒ n1 �s20    Y  nt|ƒ�rV|  ¡ | ‰ ntS ˆ S )Nr!   c                 S   s   g | ]}t |ƒ‘qS r   )r|   )r   r6   r   r   r   Ú
<listcomp>  r    z%_dok_base.__add__.<locals>.<listcomp>r   z Matrix dimensions are not equal.r   r   Úignore)Zoverc                 3   s"   | ]\}}|ˆ | | fV  qd S rC   r   ©r   Úkr   ©Únewr   r   r>   .  r    z$_dok_base.__add__.<locals>.<genexpr>)r   r   r"   r~   r,   r�   rŽ   r'   rc   r	   r`   r   r   r)   rY   Útocoor0   ra   ÚcoordsÚdatar-   Zerrstater9   r   ZtodenseÚNotImplemented)r3   rT   Ú	res_dtyperG   ZaijZo_itemsr   r™   r   Ú__add__  s2    



<
z_dok_base.__add__c                 C   s   | | S rC   r   ©r3   rT   r   r   r   Ú__radd__5  s    z_dok_base.__radd__c                 C   sD   | j jdkrtdƒ‚| j| j| j d�}|j dd„ |  ¡ D ƒ¡ |S )NÚbz2Negating a sparse boolean matrix is not supported.r!   c                 s   s   | ]\}}|| fV  qd S rC   r   r—   r   r   r   r>   >  r    z$_dok_base.__neg__.<locals>.<genexpr>)r"   Úkindr7   r~   r,   r'   r9   rY   ©r3   rš   r   r   r   Ú__neg__8  s    ÿz_dok_base.__neg__c                    s>   t | jˆ ƒ}| j| j|d�}|j ‡ fdd„|  ¡ D ƒ¡ |S )Nr!   c                 3   s   | ]\}}||ˆ  fV  qd S rC   r   r—   ©rT   r   r   r>   E  r    z(_dok_base._mul_scalar.<locals>.<genexpr>)r   r"   r~   r,   r'   r9   rY   ©r3   rT   rŸ   rš   r   r§   r   Ú_mul_scalarA  s    z_dok_base._mul_scalarc                    sÞ   t ˆjˆ jƒ}ˆjdkr˜tˆ ƒrnˆ jdkr<ˆ ¡ ˆ  ¡ @ }nˆ ¡ ˆ  ¡ jd @ }|t‡ ‡fdd„|D ƒƒƒS t	ˆ ƒr”|t‡ fdd„ˆ 
¡ D ƒƒƒS tS tjˆjd |d�}ˆ 
¡ D ]$\\}}}||  |ˆ |  7  < q´|S )Nr   r   r   c                 3   s"   | ]}ˆj | ˆ j |  V  qd S rC   rE   )r   r˜   ©rT   r3   r   r   r>   R  r    z+_dok_base._matmul_vector.<locals>.<genexpr>c                 3   s   | ]\}}ˆ | | V  qd S rC   r   r—   r§   r   r   r>   T  r    r!   )r   r"   r0   r	   r)   rZ   r›   rœ   r?   r   rY   rž   r-   Úzerosr,   )r3   rT   rŸ   rZ   Úresultr   r‚   r   r   rª   r   Ú_matmul_vectorH  s    

z_dok_base._matmul_vectorc           	         sœ   t | jˆ jƒ}| jdkr4t‡ fdd„| j ¡ D ƒƒS | jd }ˆ jdkrN|fn|ˆ jd f}tj||d�}|  ¡ D ]$\\}}}||  |ˆ |  7  < qr|S )Nr   c                 3   s   | ]\}}|ˆ |  V  qd S rC   r   )r   r‚   r   r§   r   r   r>   c  r    z0_dok_base._matmul_multivector.<locals>.<genexpr>r   r!   )	r   r"   r0   r?   r'   rY   r,   r-   r«   )	r3   rT   Zresult_dtyper]   Z	new_shaper¬   r   r‚   r   r   r§   r   Ú_matmul_multivector^  s    

z_dok_base._matmul_multivectorc                    s.   t ˆ ƒr*| j ‡ fdd„|  ¡ D ƒ¡ | S tS )Nc                 3   s   | ]\}}||ˆ  fV  qd S rC   r   r—   r§   r   r   r>   o  r    z%_dok_base.__imul__.<locals>.<genexpr>©r   r'   r9   rY   rž   r¡   r   r§   r   Ú__imul__m  s    z_dok_base.__imul__c                    sR   t ˆ ƒrFt| jˆ ƒ}| j| j|d�}|j ‡ fdd„|  ¡ D ƒ¡ |S |  ¡ ˆ  S )Nr!   c                 3   s   | ]\}}||ˆ  fV  qd S rC   r   r—   r§   r   r   r>   w  r    z(_dok_base.__truediv__.<locals>.<genexpr>)	r   r   r"   r~   r,   r'   r9   rY   Ztocsrr¨   r   r§   r   Ú__truediv__s  s    z_dok_base.__truediv__c                    s.   t ˆ ƒr*| j ‡ fdd„|  ¡ D ƒ¡ | S tS )Nc                 3   s   | ]\}}||ˆ  fV  qd S rC   r   r—   r§   r   r   r>   }  r    z)_dok_base.__itruediv__.<locals>.<genexpr>r¯   r¡   r   r§   r   Ú__itruediv__{  s    z_dok_base.__itruediv__c                 C   s
   t  | ¡S rC   )ÚdictÚ
__reduce__rA   r   r   r   r´   �  s    z_dok_base.__reduce__r   c                    s"   | j dkrtƒ  |¡S tdƒ‚d S )Nr   z diagonal requires two dimensions)r0   rf   Údiagonalr`   )r3   r˜   rl   r   r   rµ   ‡  s    
z_dok_base.diagonalc                 C   sh   | j dkr|  ¡ S |d ur*|dkr*tdƒ‚| j\}}| j||f| j|d�}|j dd„ |  ¡ D ƒ¡ |S )Nr   )r   r   zvSparse arrays/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 rC   r   ©r   ÚleftÚrightr8   r   r   r   r>   ™  r    z&_dok_base.transpose.<locals>.<genexpr>)	r0   r   r`   r,   r~   r"   r'   r9   rY   )r3   Zaxesr   r]   ÚNrš   r   r   r   Ú	transposeŒ  s    
ÿ
z_dok_base.transposec                 C   sX   | j dkr"|  ¡ }|j ¡ |_|S | j\}}| j||f| jd�}dd„ |  ¡ D ƒ|_|S )zReturn the conjugate transpose.r   r!   c                 S   s$   i | ]\\}}}||ft  |¡“qS r   )r-   Zconjr¶   r   r   r   r   ¦  r    z(_dok_base.conjtransp.<locals>.<dictcomp>)	r0   r›   r�   Ú	conjugater,   r~   r"   rY   r'   )r3   rš   r]   r¹   r   r   r   Ú
conjtranspž  s    

z_dok_base.conjtranspc                 C   s$   | j | j| jd�}|j | j¡ |S ©Nr!   )r~   r,   r"   r'   r9   r¥   r   r   r   r   ©  s    z_dok_base.copyr   c                C   s^   t  ||¡}ttt|ƒƒtƒr6tdd„ t|Ž D ƒƒ}nt|ƒd f}| |t|ƒd�}||_	|S )Nc                 s   s   | ]}t |ƒd  V  qdS )r   N)Úmax)r   rp   r   r   r   r>   ´  r    z%_dok_base.fromkeys.<locals>.<genexpr>r   r!   )
r³   Úfromkeysr$   ÚnextÚiterr%   ra   r¾   rQ   r'   )ÚclsÚiterabler’   Útmpr,   r¬   r   r   r   r¿   °  s    z_dok_base.fromkeysc                    s¢   | j ‰ˆdkr | j| j| jd�S | jt| jƒd�‰ tj|  ¡ | jˆd�}| j	dkr^t
|  ¡ Ž n|  ¡ f}t‡ ‡fdd„|D ƒƒ}| j||f| j| jd�}d	|_|S )
Nr   r!   )Úmaxval©r"   Úcountr   c                 3   s   | ]}t j|ˆ ˆd �V  qdS )rÆ   N)r-   Úfromiter)r   Zix©Z	idx_dtypeÚnnzr   r   r>   Ä  r    z"_dok_base.tocoo.<locals>.<genexpr>)r,   r"   T)rÊ   r2   r,   r"   Z_get_index_dtyper¾   r-   rÈ   r@   r0   ra   rZ   r%   Zhas_canonical_format)r3   r   r�   Zindsrœ   ÚAr   rÉ   r   r›   »  s     z_dok_base.tocooc                 C   s   |r|   ¡ S | S rC   r   ©r3   r   r   r   r   r*   Ë  s    z_dok_base.todokc                 C   s&   | j dkrtdƒ‚| jdd�j|d�S )Nr   z%tocsr() not valid for 1d sparse arrayFr   )r0   r7   r›   ÚtocscrÌ   r   r   r   rÍ   Ò  s    
z_dok_base.tocscc           	      G   sÆ   t | tƒ}t||d�}t|ƒt| jƒkr,t‚| jdkrh|d }t| jƒD ]}||krH| j|= qH|| _	d S |\}}| j\}}||k sŠ||k r¼t|  
¡ ƒD ]$\}}||ks®||kr–| j||f= q–|| _	d S )Nr   r   rd   )r$   r   r   r:   r,   r7   r0   r‡   r'   r&   rZ   )	r3   r,   r4   ZnewNr   ZnewMr]   r¹   r‚   r   r   r   ÚresizeÙ  s$    



z_dok_base.resizeÚunsafeTc                 C   sb   t  |¡}| j|krR| j| j|d�}t jt| j ¡ ƒ|d�}tt	| j|ƒƒ|_|S |r^|  
¡ S | S r½   )r-   r"   r~   r,   Úarrayr‡   r'   r@   r³   ra   r   )r3   r"   Zcastingr   r¬   r�   r   r   r   r+   ô  s    

z_dok_base.astype)NNF)N)N)r[   )r   )NF)r   )F)F)F)rÏ   T)?rR   Ú
__module__Ú__qualname__Ú_formatr#   r9   r;   rB   r   Ú__doc__rD   rH   rI   rJ   rK   rL   rN   rU   rV   rW   rX   rY   rZ   r@   rc   rg   rj   rt   ry   rz   rw   r„   r…   rˆ   r‰   r}   r�   r�   r‘   r“   r”   r    r¢   r¦   r©   r­   r®   r°   r±   r²   r´   rµ   rº   r¼   r   Úclassmethodr¿   r›   r*   rÍ   rÎ   r+   Ú__classcell__r   r   rl   r   r      s‚   
(




	










r   c                 C   s
   t | tƒS )aÑ  Is `x` of dok_array 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_array, dok_matrix, coo_matrix, isspmatrix_dok
    >>> isspmatrix_dok(dok_matrix([[5]]))
    True
    >>> isspmatrix_dok(dok_array([[5]]))
    False
    >>> isspmatrix_dok(coo_matrix([[5]]))
    False
    )r$   r   )r=   r   r   r   r      s    r   c                   @   s   e Zd ZdZdS )r   a!  
    Dictionary Of Keys based sparse array.

    This is an efficient structure for constructing sparse
    arrays incrementally.

    This can be instantiated in several ways:
        dok_array(D)
            where D is a 2-D ndarray

        dok_array(S)
            with another sparse array or matrix S (equivalent to S.todok())

        dok_array((M,N), [dtype])
            create the array with initial shape (M,N)
            dtype is optional, defaulting to dtype='d'

    Attributes
    ----------
    dtype : dtype
        Data type of the array
    shape : 2-tuple
        Shape of the array
    ndim : int
        Number of dimensions (this is always 2)
    nnz
        Number of nonzero elements
    size
    T

    Notes
    -----

    Sparse arrays 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_array once constructed.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import dok_array
    >>> S = dok_array((5, 5), dtype=np.float32)
    >>> for i in range(5):
    ...     for j in range(5):
    ...         S[i, j] = i + j    # Update element

    N)rR   rÑ   rÒ   rÔ   r   r   r   r   r     s   r   c                   @   sL   e Zd ZdZdd„ Zdd„ Zeeed�Zdd„ Zd	d
„ Z	dd„ Z
dd„ ZdS )r   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)
            where D is a 2-D ndarray

        dok_matrix(S)
            with another sparse array or matrix S (equivalent to S.todok())

        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
    size
    T

    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

    c                 C   s"   | j |dd� | j¡}|j| _d S )NFr   )ZreshapeZasformatr)   Ú__dict__)r3   r,   Z
new_matrixr   r   r   Ú	set_shape„  s    zdok_matrix.set_shapec                 C   s   | j S )zGet shape of a sparse matrix.)r&   rA   r   r   r   Ú	get_shapeˆ  s    zdok_matrix.get_shape)ÚfgetÚfsetc                 C   s
   | j  ¡ S rC   )r'   rN   rA   r   r   r   rN   Ž  s    zdok_matrix.__reversed__c                 C   s    t |tƒr| j|jB S | j|B S rC   ©r$   r   r'   r¡   r   r   r   rU   ‘  s    
zdok_matrix.__or__c                 C   s    t |tƒr| j|jB S | j|B S rC   rÜ   r¡   r   r   r   rV   –  s    
zdok_matrix.__ror__c                 C   s.   t |tƒr|  j|jO  _n|  j|O  _| S rC   rÜ   r¡   r   r   r   rW   ›  s    
zdok_matrix.__ior__N)rR   rÑ   rÒ   rÔ   rØ   rÙ   Úpropertyr,   rN   rU   rV   rW   r   r   r   r   r   P  s   3r   )rÔ   Ú__docformat__Ú__all__r�   Únumpyr-   Z_matrixr   Z_baser   r   r	   Ú_indexr
   Z_sputilsr   r   r   r   r   r   r   r   r³   r   r   r   r   r   r   r   r   Ú<module>   s   (   r5