a
    Re?                     @   s   d Z dZg dZddlZddlZddlmZ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 zdd
lmZ W n ey   dd ZY n0 G dd de
eeZdd ZG dd deeZej e_ G dd deeZ eej e _ dS )zDictionary Of Keys based matrixzrestructuredtext en)	dok_array
dok_matrixisspmatrix_dok    N   )spmatrix_array_doc_to_matrix)_spbasesparrayissparse)
IndexMixin)isdensegetdtypeisshape	isintlikeisscalarlikeupcastupcast_scalarcheck_shape)isSequenceTypec                 C   s   t | dpt | dpt | dS )N__len____next__next)hasattrx r   E/var/www/sunrise/env/lib/python3.9/site-packages/scipy/sparse/_dok.py_is_sequence   s    r   c                   @   s  e Zd ZdZdZdNddZdd Zd	d
 ZdOddZdd Z	e
jje_e
j	je	_dd ZdPd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,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"dQd@dAZ#e
j#je#_dBdC Z$dDdE Z%e
j%je%_dRdFdGZ&e
j&je&_dSdHdIZ'e
j'je'_dTdJdKZ(e
j(je(_dLdM Z)e
j)je)_dS )U	_dok_basea  
    Dictionary Of Keys based sparse matrix.

    This is an efficient structure for constructing sparse
    matrices incrementally.

    This can be instantiated in several ways:
        dok_array(D)
            with a dense matrix, D

        dok_array(S)
            with a sparse matrix, S

        dok_array((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_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

    dokNFc           	   
   C   s:  t |  t|  t|td| _t|trLt|rL|\}}t	||f| _
nt|r|j| jkrn|rn| }n| }|d ur|j|dd}t | | t	|j| _
|j| _nzt|}W n. ty } ztd|W Y d }~n
d }~0 0 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
   formatr"   todokZastypeupdateshapenpZasarray	Exception	TypeErrorlen_coo_container)	selfZarg1r/   r%   r"   MNedr   r   r   r'   K   s2    



 z_dok_base.__init__c                 C   s   t dd S )Nz8Direct modification to dok_array element is not allowed.)NotImplementedError)r5   valr   r   r   r.   m   s    z_dok_base.updatec                 C   s   t | |S )zAn update method for dict data defined for direct access to
        `dok_array` data. Main purpose is to be used for effcient conversion
        from other _spbase classes. Has no checking if `data` is valid.)r&   r.   )r5   datar   r   r   _updater   s    z_dok_base._updatec                 C   s   |d urt dt| S )Nz7_getnnz over an axis is not implemented for DOK format.)r:   r&   r   )r5   Zaxisr   r   r   _getnnzx   s    z_dok_base._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_base.count_nonzero.<locals>.<genexpr>)sumvaluesr5   r   r   r   count_nonzero~   s    z_dok_base.count_nonzeroc                 C   s
   t | S N)r&   r   rD   r   r   r   r      s    z_dok_base.__len__        c              
   C   s   z |\}}t |rt |sJ W n4 tttfyT } ztd|W Y d}~n
d}~0 0 |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   AssertionErrorr2   
ValueError
IndexErrorr/   r&   get)r5   keyr    ijr8   r   r   r   rK      s     ,z_dok_base.getc                 C   s   t | ||f| jdS Nr   )r&   rK   r%   typer5   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rqtt	|d | |\}}|dks|dk s||d kr
qt
| |}t
|||f| q|S )Nr   r   r#   r$   )indicesr/   ranger3   _get_columnXarray_dok_containerr%   keysdivmodintr&   __getitem____setitem__)r5   rR   rS   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeZ	col_ranger/   newdokrL   rM   rirN   Zrjr   r   r   r   rW      s$    z_dok_base._get_sliceXslicec                 C   s   |  }| |g|S rF   Zsqueezer]   rQ   r   r   r   _get_intXarray   s    z_dok_base._get_intXarrayc                 C   s   |  }| ||gS rF   rf   rQ   r   r   r   _get_arrayXint   s    z_dok_base._get_arrayXintc                 C   s$   t t|| jd  }| ||S rO   listr\   r[   r/   r]   rQ   r   r   r   _get_sliceXarray   s    z_dok_base._get_sliceXarrayc                 C   s$   t t|| jd  }| ||S rU   ri   rQ   r   r   r   _get_arrayXslice   s    z_dok_base._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   )r^   r3   r%   	enumerater&   rK   rc   )	r5   rR   rS   rd   rM   rrN   cvr   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 ]0}t
| || || fd}|rJt
||| qJ|S )Nr$   r   r   )mapr0   Z
atleast_2dZbroadcast_arraysr^   r/   r%   	itertoolsproductr\   r&   rK   rc   )r5   rR   rS   rM   rN   rd   rL   rp   r   r   r   _get_arrayXarray   s    $z_dok_base._get_arrayXarrayc                 C   s2   ||f}|rt | || nt | |r.| |= d S rF   )r&   rc   __contains__)r5   rR   rS   r   rL   r   r   r   _set_intXint   s
    z_dok_base._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 rO   )
rj   rq   ra   Zravelr&   r.   zipr0   Znonzerorb   )r5   rR   rS   r   rM   rL   r   r   r   _set_arrayXarray   s    z_dok_base._set_arrayXarrayc                    s<  t rht| j}| j| j|d | j\}}tt|t|D ]"}t	| |d }|rB| |< qBnt
rjdkr
j| jkrtdt| jj}| j| j|d t |  tjdd0 t  fdd D  W d    n1 s0    Y  n|  }|  ntr4|    ntS  S )	Nr$   r   r    Matrix dimensions are not equal.ignore)Zoverc                 3   s"   | ]}| | |  fV  qd S rF   r   r?   knewotherr   r   r@     rA   z$_dok_base.__add__.<locals>.<genexpr>)r   r   r%   r^   r/   rr   rs   r\   r&   rK   r
   r,   rI   r   r.   r0   Zerrstater_   tocscr   todenseNotImplemented)r5   r   	res_dtyper6   r7   rL   aijcscr   r}   r   __add__   s2    

$

z_dok_base.__add__c                    s   t  r^jjjd}j\}}tt|t|D ]"}t|d  }|r8|||< q8nt	 rֈ j
dkrĈ jjkrtdjjjd}t| t| fdd  D  q }|  }nt r   }ntS |S )Nr$   r   r   ry   c                 3   s"   | ]}||  |  fV  qd S rF   r   r{   r   r5   r   r   r@     rA   z%_dok_base.__radd__.<locals>.<genexpr>)r   r^   r/   r%   rr   rs   r\   r&   rK   r
   r,   rI   r.   r_   r   r   r   r   )r5   r   r~   r6   r7   rL   r   r   r   r   r   __radd__  s,    


z_dok_base.__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 rF   r   r{   rD   r   r   r@   ,  rA   z$_dok_base.__neg__.<locals>.<genexpr>)r%   kindr:   r^   r/   r&   r.   r_   r5   r~   r   rD   r   __neg__'  s
    z_dok_base.__neg__c                    s>   t | j }| j| j|d}t| fdd|  D  |S )Nr$   c                 3   s   | ]\}}||  fV  qd S rF   r   r?   r|   rp   r   r   r   r@   3  rA   z(_dok_base._mul_scalar.<locals>.<genexpr>)r   r%   r^   r/   r&   r.   itemsr5   r   r   r~   r   r   r   _mul_scalar/  s    z_dok_base._mul_scalarc                 C   sP   t j| jd t| j|jd}|  D ]$\\}}}||  |||  7  < q&|S )Nr   r$   )r0   zerosr/   r   r%   r   )r5   r   resultrM   rN   rp   r   r   r   _mul_vector6  s    z_dok_base._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%   r0   r   r   )r5   r   Zresult_shapeZresult_dtyper   rM   rN   rp   r   r   r   _mul_multivector=  s    *z_dok_base._mul_multivectorc                    s.   t  r*t|  fdd|  D  | S tS )Nc                 3   s   | ]\}}||  fV  qd S rF   r   r   r   r   r   r@   H  rA   z%_dok_base.__imul__.<locals>.<genexpr>r   r&   r.   r   r   r5   r   r   r   r   __imul__F  s    z_dok_base.__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 rF   r   r   r   r   r   r@   P  rA   z(_dok_base.__truediv__.<locals>.<genexpr>)	r   r   r%   r^   r/   r&   r.   r   Ztocsrr   r   r   r   __truediv__L  s    z_dok_base.__truediv__c                    s.   t  r*t|  fdd|  D  | S tS )Nc                 3   s   | ]\}}||  fV  qd S rF   r   r   r   r   r   r@   V  rA   z)_dok_base.__itruediv__.<locals>.<genexpr>r   r   r   r   r   __itruediv__T  s    z_dok_base.__itruediv__c                 C   s
   t | S rF   )r&   
__reduce__rD   r   r   r   r   Z  s    z_dok_base.__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 rF   r   r?   leftrightr;   r   r   r   r@   l  s   
z&_dok_base.transpose.<locals>.<genexpr>)rI   r/   r^   r%   r&   r.   r   )r5   Zaxesr"   r6   r7   r~   r   r   r   	transposed  s    
z_dok_base.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 rF   )r0   Zconjr   r   r   r   r@   v  s   
z'_dok_base.conjtransp.<locals>.<genexpr>)r/   r^   r%   r&   r.   r   )r5   r6   r7   r~   r   r   r   
conjtranspr  s    
z_dok_base.conjtranspc                 C   s"   | j | j| jd}t||  |S )Nr$   )r^   r/   r%   r&   r.   r   r   r   r   r"   z  s    z_dok_base.copyc                 C   s   | j dkr| j| j| jdS | j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 rF   r   )r?   rM   _r   r   r   r@     rA   z"_dok_base.tocoo.<locals>.<genexpr>c                 s   s   | ]\}}|V  qd S rF   r   )r?   r   rN   r   r   r   r@     rA   )r/   r%   T)Znnzr4   r/   r%   Z_get_index_dtypemaxr0   ZfromiterrC   r_   Zhas_canonical_format)r5   r"   Z	idx_dtyper<   rR   rS   Ar   r   r   tocoo  s    
  z_dok_base.tocooc                 C   s   |r|   S | S rF   r!   r5   r"   r   r   r   r-     s    z_dok_base.todokc                 C   s   | j ddj|dS NFr!   )r   r   r   r   r   r   r     s    z_dok_base.tocscc                 G   sd   t |}|\}}| j\}}||k s*||k rZt|  D ]"\}}||ksN||kr6| ||f= q6|| _d S rF   )r   r/   rj   r_   r+   )r5   r/   ZnewMZnewNr6   r7   rM   rN   r   r   r   resize  s    
z_dok_base.resize)NNF)N)rG   )NF)F)F)F)*__name__
__module____qualname____doc___formatr'   r.   r=   r>   rE   r   r   rK   rT   rY   rZ   rW   rg   rh   rk   rl   r]   rt   rv   rx   r   r   r   r   r   r   r   r   r   r   r   r   r"   r   r-   r   r   r   r   r   r   r      s\   0
"



 	









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S )r   N)r   r   r   r   r   r   r   r     s   r   c                   @   s(   e Zd Zdd Zdd ZeeedZdS )r   c                 C   s8   | j |dd| j}|j| _t|  t| | d S r   )ZreshapeZasformatr,   __dict__r&   clearr.   )r5   r/   Z
new_matrixr   r   r   	set_shape  s    
zdok_matrix.set_shapec                 C   s   | j S )zGet shape of a sparse array.)r+   rD   r   r   r   	get_shape  s    zdok_matrix.get_shape)fgetfsetN)r   r   r   r   r   propertyr/   r   r   r   r   r     s   r   )!r   Z__docformat____all__rr   numpyr0   Z_matrixr   r   _baser   r	   r
   _indexr   Z_sputilsr   r   r   r   r   r   r   r   operatorr   r   ImportErrorr&   r   r   r   r   r   r   r   r   <module>   s*   (   