o
    i?                     @   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yQ   dd ZY nw 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   `/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/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&_dRdHdIZ'e
j'je'_dRdJdKZ(e
j(je(_dLdM Z)e
j)je)_dS )S	_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   s0  t |  t|  t|td| _t|tr't|r'|\}}t	||f| _
d S t|rY|j| jkr8|r8| }n| }|d urG|j|dd}t | | t	|j| _
|j| _d S zt|}W n tyq } 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
   formatr"   todokZastypeupdateshapenpZasarray	Exception	TypeErrorlen_coo_container)	selfZarg1r/   r%   r"   MNedr   r   r   r'   K   s6   



z_dok_base.__init__c                 C   s   t d)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~      z_dok_base.count_nonzeroc                 C   
   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 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   AssertionErrorr2   
ValueError
IndexErrorr/   r&   get)r5   keyr    ijr8   r   r   r   rM      s   
,z_dok_base.getc                 C   s   t | ||f| jdS Nr   )r&   rM   r%   typer5   rowcolr   r   r   _get_intXint   s   z_dok_base._get_intXintc                 C   s   |  t||d |S Nr   _get_sliceXsliceslicerS   r   r   r   _get_intXslice   rF   z_dok_base._get_intXslicec                 C   s   |  |t||d S rW   rX   rS   r   r   r   _get_sliceXint   rF   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 ]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/   ranger3   _get_columnXarray_dok_containerr%   keysdivmodintr&   __getitem____setitem__)r5   rT   rU   Z	row_startZrow_stopZrow_stepZ	col_startZcol_stopZcol_stepZ	row_rangeZ	col_ranger/   newdokrN   rO   rirP   Zrjr   r   r   r   rY      s$   z_dok_base._get_sliceXslicec                 C   s   |  }| |g|S rH   Zsqueezer_   rS   r   r   r   _get_intXarray      z_dok_base._get_intXarrayc                 C   s   |  }| ||gS rH   rh   rS   r   r   r   _get_arrayXint   rj   z_dok_base._get_arrayXintc                 C   s$   t t|| jd  }| ||S rQ   listr^   r]   r/   r_   rS   r   r   r   _get_sliceXarray      z_dok_base._get_sliceXarrayc                 C   s$   t t|| jd  }| ||S rW   rl   rS   r   r   r   _get_arrayXslice   ro   z_dok_base._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`   r3   r%   	enumerater&   rM   re   )	r5   rT   rU   rf   rO   rrP   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 ]}t
| || || fd}|r=t
||| q%|S )Nr$   r   r   )mapr0   Z
atleast_2dZbroadcast_arraysr`   r/   r%   	itertoolsproductr^   r&   rM   re   )r5   rT   rU   rO   rP   rf   rN   rt   r   r   r   _get_arrayXarray   s   $z_dok_base._get_arrayXarrayc                 C   s8   ||f}|rt | || d S t | |r| |= d S d S rH   )r&   re   __contains__)r5   rT   rU   r   rN   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r@| |= q+d S rQ   )
rm   ru   rc   Zravelr&   r.   zipr0   Znonzerord   )r5   rT   rU   r   rO   rN   r   r   r   _set_arrayXarray   s   z_dok_base._set_arrayXarrayc                    s<  t r5t| j}| j| j|d | j\}}tt|t|D ]}t	| |d }|r2| |< q! S t
rjdkrj| jkrHtdt| jj}| j| j|d t |  tjdd t  fdd D  W d     S 1 sw   Y   S |  }|   S tr|     S tS )	Nr$   r   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_base.__add__.<locals>.<genexpr>)r   r   r%   r`   r/   rv   rw   r^   r&   rM   r
   r,   rK   r   r.   r0   Zerrstatera   tocscr   todenseNotImplemented)r5   r   	res_dtyper6   r7   rN   aijcscr   r   r   __add__   s@   




z_dok_base.__add__c                    s   t  r0jjjd}j\}}tt|t|D ]}t|d  }|r-|||< q|S t	 rn j
dkrd jjkrCtdjjjd}t| t| fdd  D  |S  }|  }|S t rz   }|S tS )Nr$   r   r   r}   c                 3   s$    | ]}||  |  fV  qd S rH   r   r   r   r5   r   r   r@     r   z%_dok_base.__radd__.<locals>.<genexpr>)r   r`   r/   r%   rv   rw   r^   r&   rM   r
   r,   rK   r.   ra   r   r   r   r   )r5   r   r   r6   r7   rN   r   r   r   r   r   __radd__  s4   

	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 rH   r   r   rD   r   r   r@   ,  s    z$_dok_base.__neg__.<locals>.<genexpr>)r%   kindr:   r`   r/   r&   r.   ra   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        | ]\}}||  fV  qd S rH   r   r?   r   rt   r   r   r   r@   3      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   resultrO   rP   rt   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 ]\\}}}||d d f  |||d d f  7  < q|S )Nr   r   r$   )r/   r   r%   r0   r   r   )r5   r   Zresult_shapeZresult_dtyper   rO   rP   rt   r   r   r   _mul_multivector=  s   *z_dok_base._mul_multivectorc                    .   t  rt|  fdd|  D  | S tS )Nc                 3   r   rH   r   r   r   r   r   r@   H  r   z%_dok_base.__imul__.<locals>.<genexpr>r   r&   r.   r   r   r5   r   r   r   r   __imul__F     z_dok_base.__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@   P  r   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                    r   )Nc                 3   r   rH   r   r   r   r   r   r@   V  r   z)_dok_base.__itruediv__.<locals>.<genexpr>r   r   r   r   r   __itruediv__T  r   z_dok_base.__itruediv__c                 C   rG   rH   )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 rH   r   r?   leftrightr;   r   r   r   r@   l  s    
z&_dok_base.transpose.<locals>.<genexpr>)rK   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 rH   )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 rH   r   )r?   rO   _r   r   r   r@     rA   z"_dok_base.tocoo.<locals>.<genexpr>c                 s   s    | ]\}}|V  qd S rH   r   )r?   r   rP   r   r   r   r@     rA   )r/   r%   T)Znnzr4   r/   r%   Z_get_index_dtypemaxr0   ZfromiterrC   ra   Zhas_canonical_format)r5   r"   Z	idx_dtyper<   rT   rU   Ar   r   r   tocoo  s   
  z_dok_base.tocooc                 C   s   |r|   S | S rH   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 r-t|  D ]\}}||ks'||kr,| ||f= q|| _d S rH   )r   r/   rm   ra   r+   )r5   r/   ZnewMZnewNr6   r7   rO   rP   r   r   r   resize  s   


z_dok_base.resize)NNFrH   )rI   )NF)F)*__name__
__module____qualname____doc___formatr'   r.   r=   r>   rE   r   r   rM   rV   r[   r\   rY   ri   rk   rn   rp   r_   rx   rz   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
"



 	









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   __docformat____all__rv   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.    (   