o
    iRhY                     @   s   d Z dZg dZddlZddlm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)warn   )spmatrix)_spbasesparrayissparse)
IndexMixin)isdensegetdtypeisshape	isintlikeisscalarlikeupcastupcast_scalarcheck_shapec                       sN  e Zd ZdZdZd{ddddZdd	 Zd|d
dZd|ddZe	jj
e_
e	jj
e_
dd Zdd Zd|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+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$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_d` Z3dadb Z4d~ fddde	Z5ddfdgZ6e	j6j
e6_
dhdi Z7djdk Z8e	j8j
e8_
e9ddmdnZ:ddodpZ;e	j;j
e;_
ddqdrZ<e	j<j
e<_
ddsdtZ=e	j=j
e=_
dudv Z>e	j>j
e>_
ddydzZ?  Z@S )	_dok_basedok)r      NFmaxprintc             
   C   s  t j| ||d t|tr(t|| jdr(t|| jd| _i | _t	|t
d| _d S t|r_|j| jkr;|r8| n|}n| }|d urJ|j|dd}|j| _t|j| jd| _t	|j| _d S zt|}W n tyw } ztd|d }~ww |jdkrtd|j d	|jd
kr|d ur||}dd t|D | _t	|j| _n| j|||d }|j| _t	|j| _t|j| jd| _d S )Nr   allow_nd)defaultFcopyzInvalid input format.r   zDOK arrays don't yet support zD input.r   c                 S   s   i | ]\}}|d kr||qS r    ).0ivr   r   S/home/air/sanwanet/backup_V2/venv/lib/python3.10/site-packages/scipy/sparse/_dok.py
<dictcomp>5   s    z&_dok_base.__init__.<locals>.<dictcomp>shapedtype)r   __init__
isinstancetupler   	_allow_ndr   _shape_dictr   floatr'   r
   formatr   todokastyper&   npasarray	Exception	TypeErrorndim
ValueError	enumerate_coo_container)selfarg1r&   r'   r   r   edr   r   r#   r(      s>   



z_dok_base.__init__c                 C      t d)Nz2Direct update to DOK sparse format is not allowed.)NotImplementedError)r:   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.)r?   lenr-   r:   axisr   r   r#   _getnnzA   s
   
z_dok_base._getnnzc                 C   s&   |d urt dtdd |  D S )Nz=count_nonzero over an axis is not implemented for DOK format.c                 s   s    | ]}|d kV  qdS r   Nr   )r    xr   r   r#   	<genexpr>M   s    z*_dok_base.count_nonzero.<locals>.<genexpr>)r?   sumvaluesrC   r   r   r#   count_nonzeroH   s
   z_dok_base.count_nonzeroc                 C   s
   t | jS N)rB   r-   r:   r   r   r#   __len__R      
z_dok_base.__len__c                 C   s
   || j v S rL   r-   r:   keyr   r   r#   __contains__U   rO   z_dok_base.__contains__c                C   s   | j ||S rL   )r-   
setdefault)r:   rR   r   r   r   r#   rT   X   s   z_dok_base.setdefaultc                C   s   | j |= d S rL   rP   rQ   r   r   r#   __delitem__[      z_dok_base.__delitem__c                 C   
   | j  S rL   )r-   clearrM   r   r   r#   rX   ^   rO   z_dok_base.clearc                G   s   | j j| S rL   )r-   pop)r:   argsr   r   r#   rY   a   rV   z_dok_base.popc                 C   r>   )Nz*reversed is not defined for dok_array type)r5   rM   r   r   r#   __reversed__d      z_dok_base.__reversed__c                 C   (   t | j dt |j }td| Nz and z unsupported operand type for |: type__name__r5   r:   other
type_namesr   r   r#   __or__g      z_dok_base.__or__c                 C   r]   r^   r_   rb   r   r   r#   __ror__k   rf   z_dok_base.__ror__c                 C   r]   r^   r_   rb   r   r   r#   __ior__o   rf   z_dok_base.__ior__c                 C   rW   rL   )r-   popitemrM   r   r   r#   ri   s   rO   z_dok_base.popitemc                 C   rW   rL   )r-   itemsrM   r   r   r#   rj   v   rO   z_dok_base.itemsc                 C   rW   rL   )r-   keysrM   r   r   r#   rk   y   rO   z_dok_base.keysc                 C   rW   rL   )r-   rJ   rM   r   r   r#   rJ   |   rO   z_dok_base.values        c              
   C   s   || j v r
| j | S t|r| jdkr|f}| jt|kr%td| dz|D ]}t|s0J q(W n tttfyF } ztd|d}~ww tdd t	|| j
D }tdd t	|| j
D retd	| jdkrn|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 rF   r   r    r!   Mr   r   r#   rH      s   & z _dok_base.get.<locals>.<genexpr>c                 s   s$    | ]\}}|d k p||kV  qdS rF   r   rm   r   r   r#   rH         " zIndex out of bounds.r   )r-   r   r6   rB   
IndexErrorAssertionErrorr5   r7   r*   zipr&   anyget)r:   rR   r   r!   r<   r   r   r#   rt      s(   



z_dok_base.getc                 C   s   | j || jdS Nr   r-   rt   r'   r`   )r:   idxr   r   r#   _get_int      z_dok_base._get_intc                 C   s"   t || jd  }| t|S ru   )rangeindicesr&   
_get_arraylist)r:   rw   i_ranger   r   r#   
_get_slice   s   z_dok_base._get_slicec                    s   t |}|jdkr jt| jd}t j| jdS  j	|j
 jd} fdd| D }|r{t|j
dkrNt|D ]\}}|rK||j|< q@|S t t t||j
}t|dkrd|d nt| }t||ddD ]\}}|rz||j|< qo|S )	Nr   )styper'   c                    s   g | ]	} j |d qS r   )r-   rt   )r    r!   rM   r   r#   
<listcomp>   s    z(_dok_base._get_array.<locals>.<listcomp>r   T)strict)r2   r3   r6   r-   rt   intr'   r`   array_dok_containerr&   ravelrB   r8   unravel_indexarangerr   )r:   rw   r@   new_dokdok_valsr!   r"   new_idxr   rM   r#   r|      s(   



z_dok_base._get_arrayc                 C   s   | j ||f| jdS ru   rv   r:   rowcolr   r   r#   _get_intXint   s   z_dok_base._get_intXintc                 C   s   |  t||d |S Nr   _get_sliceXsliceslicer   r   r   r#   _get_intXslice   ry   z_dok_base._get_intXslicec                 C   s   |  |t||d S r   r   r   r   r   r#   _get_sliceXint   ry   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 ]D}tt	|d | |\}}|dksg|dk sg||d krhqJtt	|d | |\}}|dks|dk s||d krqJ| j
| |j
||f< qJ|S )Nr   r   r   r   )r{   r&   rz   rB   _get_columnXarrayr   r'   rk   divmodr   r-   )r:   r   r   	row_startrow_stoprow_step	col_startcol_stopcol_step	row_range	col_ranger&   newdokrR   r!   rijrjr   r   r#   r      s"   z_dok_base._get_sliceXslicec                 C   s   |  |g| S rL   )r   r   r   r   r   r#   _get_intXarray   s   z_dok_base._get_intXarrayc                 C   s,   |  | |g}|jdkr||jS |S r   )r   r   r6   reshaper&   )r:   r   r   resr   r   r#   _get_arrayXint   s   
z_dok_base._get_arrayXintc                 C   s$   t t|| jd  }| ||S ru   r}   rz   r{   r&   r   r   r   r   r#   _get_sliceXarray      z_dok_base._get_sliceXarrayc                 C   s$   t t|| jd  }| ||S r   r   r   r   r   r#   _get_arrayXslice   r   z_dok_base._get_arrayXslicec           	      C   sh   | j t|t|f| jd}t|D ]\}}t|D ]\}}| j||fd}|r0||j||f< qq|S )Nr   r   )r   rB   r'   r8   r-   rt   )	r:   r   r   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}|r;||j
|< q%|S )Nr   r   r   )mapr2   
atleast_2dbroadcast_arraysr   r&   r'   	itertoolsproductrz   r-   rt   )r:   r   r   r!   r   r   rR   r"   r   r   r#   _get_arrayXarray   s   $
z_dok_base._get_arrayXarrayc                 C   s,   |r	|| j |< d S || j v r| j |= d S d S rL   rP   )r:   rw   rG   r   r   r#   _set_int   s
   
z_dok_base._set_intc                 C   s   |  }|  }t|t|kr(t|dkr$tjt||d | jd}ntdt||D ]\}}|r9|| j|< q-|| jv rB| j|= q-d S )Nr   r   r   z*Need len(index)==len(data) or len(data)==1)r   rB   r2   fullr'   r7   rr   r-   )r:   rw   rG   idx_setx_setr!   r"   r   r   r#   
_set_array   s   
z_dok_base._set_arrayc                 C   s4   ||f}|r|| j |< d S || j v r| j |= d S d S rL   rP   )r:   r   r   rG   rR   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r@| j|= q+d S ru   )	r}   r   r   r   r-   rA   rr   r2   nonzero)r:   r   r   rG   r!   rR   r   r   r#   _set_arrayXarray  s   z_dok_base._set_arrayXarrayc                    s\  t |r1t| j|}| j| j|d tjdd | jD  D ]}| j|d| }|r.| |< q S t	|r|j| jkr?t
dt| j|j}| j| j|d | j  _|jdkr^| }n| }| jdkrqt|jd |j}n	tt|j |j}tjdd	  j fd
d|D  W d     S 1 sw   Y   S t|r|  |   S tS )Nr   c                 S   s   g | ]}t |qS r   )rz   )r    r=   r   r   r#   r   %  s    z%_dok_base.__add__.<locals>.<listcomp>r   z Matrix dimensions are not equal.r   r   ignore)overc                 3   s$    | ]\}}| | | fV  qd S rL   r   r    kr"   newr   r#   rH   8  ro   z$_dok_base.__add__.<locals>.<genexpr>)r   r   r'   r   r&   r   r   r-   rt   r
   r7   r   r   r/   rj   tocoor6   rr   coordsdatar2   errstaterA   r   todenseNotImplemented)r:   rc   	res_dtyperR   aijo_itemsr   r   r#   __add__   s>   



z_dok_base.__add__c                 C   s   | | S rL   r   r:   rc   r   r   r#   __radd__?  r\   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 rL   r   r   r   r   r#   rH   H  s    z$_dok_base.__neg__.<locals>.<genexpr>)r'   kindr?   r   r&   r-   rA   rj   r:   r   r   r   r#   __neg__B  s   z_dok_base.__neg__c                    s>   t | j }| j| j|d}|j fdd|  D  |S )Nr   c                 3        | ]\}}||  fV  qd S rL   r   r   rc   r   r#   rH   O      z(_dok_base._mul_scalar.<locals>.<genexpr>)r   r'   r   r&   r-   rA   rj   r:   rc   r   r   r   r   r#   _mul_scalarK  s   z_dok_base._mul_scalarc                    s   t j j}jdkrLt r7 jdkr   @ }n   jd @ }|t fdd|D S t	 rJ|t fdd
 D S tS tjjd |d}
 D ]\\}}}||  | |  7  < qZ|S )Nr   r   r   c                 3   s$    | ]}j |  j |  V  qd S rL   rP   )r    r   rc   r:   r   r#   rH   \  ro   z+_dok_base._matmul_vector.<locals>.<genexpr>c                 3   s     | ]\}} | | V  qd S rL   r   r   r   r   r#   rH   ^  r   r   )r   r'   r6   r
   r/   rk   r   r   rI   r   rj   r   r2   zerosr&   )r:   rc   r   rk   resultr!   r   r"   r   r   r#   _matmul_vectorR  s   

z_dok_base._matmul_vectorc           	         s   t | j j}| jdkrt fdd| j D S | jd } jdkr'|fn| jd f}tj||d}|  D ]\\}}}||  | |  7  < q9|S )Nr   c                 3   s     | ]\}}| |  V  qd S rL   r   )r    r   r"   r   r   r#   rH   m  r   z0_dok_base._matmul_multivector.<locals>.<genexpr>r   r   )	r   r'   r6   rI   r-   rj   r&   r2   r   )	r:   rc   result_dtypern   	new_shaper   r!   r   r"   r   r   r#   _matmul_multivectorh  s   

z_dok_base._matmul_multivectorc                    .   t  r| j fdd|  D  | S tS )Nc                 3   r   rL   r   r   r   r   r#   rH   y  r   z%_dok_base.__imul__.<locals>.<genexpr>r   r-   rA   rj   r   r   r   r   r#   __imul__w     z_dok_base.__imul__c                    sR   t  r#t| j }| j| j|d}|j fdd|  D  |S |    S )Nr   c                 3        | ]\}}||  fV  qd S rL   r   r   r   r   r#   rH     r   z(_dok_base.__truediv__.<locals>.<genexpr>)	r   r   r'   r   r&   r-   rA   rj   tocsrr   r   r   r#   __truediv__}  s   z_dok_base.__truediv__c                    r   )Nc                 3   r   rL   r   r   r   r   r#   rH     r   z)_dok_base.__itruediv__.<locals>.<genexpr>r   r   r   r   r#   __itruediv__  r   z_dok_base.__itruediv__c                 C   s
   t | S rL   )dict
__reduce__rM   r   r   r#   r     s   
z_dok_base.__reduce__r   c                    s   | j dkrt |S td)Nr   z diagonal requires two dimensions)r6   superdiagonalr7   )r:   r   	__class__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 rL   r   r    leftrightr@   r   r   r#   rH     ro   z&_dok_base.transpose.<locals>.<genexpr>)	r6   r   r7   r&   r   r'   r-   rA   rj   )r:   axesr   rn   Nr   r   r   r#   	transpose  s   

z_dok_base.transposec                 C   sj   d}t |tdd | jdkr|  }|j |_|S | j\}}| j||f| jd}dd | 	 D |_
|S )zDEPRECATED: Return the conjugate transpose.

        .. deprecated:: 1.14.0

            `conjtransp` is deprecated and will be removed in v1.16.0.
            Use ``.T.conj()`` instead.
        zS`conjtransp` is deprecated and will be removed in v1.16.0. Use `.T.conj()` instead.r   )
stacklevelr   r   c                 S   s$   i | ]\\}}}||ft |qS r   )r2   conjr   r   r   r#   r$     s   $ z(_dok_base.conjtransp.<locals>.<dictcomp>)r   DeprecationWarningr6   r   r   	conjugater&   r   r'   rj   r-   )r:   msgr   rn   r   r   r   r#   
conjtransp  s   

z_dok_base.conjtranspc                 C   s$   | j | j| jd}|j| j |S Nr   )r   r&   r'   r-   rA   r   r   r   r#   r     s   z_dok_base.copyr   c                C   s^   t ||}ttt|trtdd t| D }nt|d f}| |t|d}||_	|S )Nc                 s   s    | ]	}t |d  V  qdS )r   N)max)r    rw   r   r   r#   rH     s    z%_dok_base.fromkeys.<locals>.<genexpr>r   r   )
r   fromkeysr)   nextiterr*   rr   r   r`   r-   )clsiterablevalue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)r2   fromiter)r    ix	idx_dtypennzr   r#   rH     r   z"_dok_base.tocoo.<locals>.<genexpr>r%   T)r	  r9   r&   r'   _get_index_dtyper   r2   r  rJ   r6   rr   rk   r*   has_canonical_format)r:   r   r   indsr   Ar   r  r#   r     s    z_dok_base.tocooc                 C   s   |r|   S | S rL   r   r:   r   r   r   r#   r0     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   )r6   r?   r   tocscr  r   r   r#   r    s   
z_dok_base.tocscc                 G   s   t || jd}t|t| jkrt| jdkr0|d }t| jD ]
}||kr*| j|= q || _d S |\}}| j\}}||k sA||k rZt| 	 D ]\}}||ksS||krY| j||f= qG|| _d S )Nr   r   )
r   r+   rB   r&   r?   r6   r}   r-   r,   rk   )r:   r&   newNr!   newMrn   r   r   r   r   r#   resize  s&   


z_dok_base.resizeunsafeTc                 C   sb   t |}| j|kr)| j| j|d}t jt| j |d}tt	| j||_|S |r/| 
 S | S r   )r2   r'   r   r&   r   r}   r-   rJ   r   rr   r   )r:   r'   castingr   r   r   r   r   r#   r1     s   

z_dok_base.astype)NNFrL   )rl   r   )NF)r   )F)r  T)Ara   
__module____qualname___formatr+   r(   rA   rE   rK   r   __doc__rN   rS   rT   rU   rX   rY   r[   re   rg   rh   ri   rj   rk   rJ   rt   rx   r   r|   r   r   r   r   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   r0   r  r  r1   __classcell__r   r   r   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   )rG   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)ra   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   )r   asformatr/   __dict__)r:   r&   
new_matrixr   r   r#   	set_shape  s   zdok_matrix.set_shapec                 C   s   | j S )zGet shape of a sparse matrix.)r,   rM   r   r   r#   	get_shape  s   zdok_matrix.get_shape)fgetfsetc                 C   rW   rL   )r-   r[   rM   r   r   r#   r[     rO   zdok_matrix.__reversed__c                 C       t |tr| j|jB S | j|B S rL   r)   r   r-   r   r   r   r#   re        

zdok_matrix.__or__c                 C   r#  rL   r$  r   r   r   r#   rg     r%  zdok_matrix.__ror__c                 C   s0   t |tr|  j|jO  _| S |  j|O  _| S rL   r$  r   r   r   r#   rh     s
   
zdok_matrix.__ior__N)ra   r  r  r  r  r   propertyr&   r[   re   rg   rh   r   r   r   r#   r   d  s    3r   )r  __docformat____all__r   warningsr   numpyr2   _matrixr   _baser   r	   r
   _indexr   _sputilsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#   <module>   s$    (    5