a
    CCCf)P                     @   s   d Z dZg d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mZmZ dd
lmZmZmZmZmZmZ ddlmZ G dd de
eZdddZdd ZG dd deeZG dd deeZdS )z"List of Lists sparse matrix class
zrestructuredtext en)	lil_array
lil_matrixisspmatrix_lil    )bisect_leftN   )spmatrix)_spbasesparrayissparse)
IndexMixin	INT_TYPES_broadcast_arrays)getdtypeisshapeisscalarlikeupcast_scalarcheck_shapecheck_reshape_kwargs)_csparsetoolsc                   @   s  e Zd ZdZdLddZdd Zdd	 Zd
d Zdd ZdMddZ	dd Z
ej	je	_ej
je
_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d.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Z d:d; Z!d<d= Z"d>d? Z#ej#je#_d@dA Z$ej$je$_dBdC Z%ej%je%_dNdDdEZ&ej&je&_dOdFdGZ'ej'je'_dPdHdIZ(ej(je(_dQdJdKZ)ej)je)_dS )R	_lil_baselilNFc           
   
   C   s  t |  t||td| _t|r|jdkr:|r:| }n| }|d urX|j	|dd}t
|j| _|j| _|j| _|j| _nt|trt|r|d urtd|\}}t
||f| _tj|ftd| _tj|ftd| _t|D ]}g | j|< g | j|< qntdnvz| |}W n0 tyL }	 ztd|	W Y d }	~	n@d }	~	0 0 | j||d }t
|j| _|j| _|j| _|j| _d S )	N)defaultr   Fcopyzinvalid use of shape parameterdtypez(unrecognized lil_array constructor usagezunsupported matrix type)r   __init__r   floatr   r
   formatr   tolilastyper   shape_shaperowsdata
isinstancetupler   
ValueErrornpemptyobjectrange	TypeErrorZ_ascontainer_csr_container)
selfZarg1r!   r   r   AMNie r4   M/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/sparse/_lil.pyr      sB    




 z_lil_base.__init__c                 C   s   | | | d d d d f< | S Nr4   r.   otherr4   r4   r5   __iadd__D   s    z_lil_base.__iadd__c                 C   s   | | | d d d d f< | S r6   r4   r7   r4   r4   r5   __isub__H   s    z_lil_base.__isub__c                 C   s,   t |r$| | | d d d d f< | S tS d S r6   r   NotImplementedr7   r4   r4   r5   __imul__L   s    z_lil_base.__imul__c                 C   s,   t |r$| | | d d d d f< | S tS d S r6   r;   r7   r4   r4   r5   __itruediv__S   s    z_lil_base.__itruediv__c                 C   s   |d u rt dd | jD S |dk r,|d7 }|dkrjtj| jd tjd}| jD ]}||  d7  < qP|S |dkrtjdd | jD tjdS tdd S )	Nc                 S   s   g | ]}t |qS r4   len.0rowvalsr4   r4   r5   
<listcomp>_       z%_lil_base._getnnz.<locals>.<listcomp>r      r   r   c                 S   s   g | ]}t |qS r4   r?   rA   r4   r4   r5   rD   h   rE   zaxis out of bounds)	sumr$   r(   Zzerosr!   intpr#   arrayr'   )r.   Zaxisoutrowr4   r4   r5   _getnnz]   s    
z_lil_base._getnnzc                 C   s   t dd | jD S )Nc                 s   s   | ]}t |V  qd S r6   )r(   count_nonzerorA   r4   r4   r5   	<genexpr>m   rE   z*_lil_base.count_nonzero.<locals>.<genexpr>)rG   r$   )r.   r4   r4   r5   rM   l   s    z_lil_base.count_nonzeroc              	   C   sb   d}t | jD ]F\}}t |D ]4\}}|dt||f dt| j| |  d7 }qq|d d S )N z  	
)	enumerater#   strr$   )r.   valr2   rK   posjr4   r4   r5   __str__r   s
    0z_lil_base.__str__c                 C   s>   | j d| jd f| jd}| j| |jd< | j| |jd< |S )z;Returns a view of the 'i'th row (without copying).
        r   r   r   )_lil_containerr!   r   r#   r$   )r.   r2   newr4   r4   r5   
getrowviewy   s    z_lil_base.getrowviewc                 C   sz   | j \}}|dk r||7 }|dk s*||kr2td| jd|f| jd}| j| dd |jd< | j| dd |jd< |S )z)Returns a copy of the 'i'th row.
        r   zrow index out of boundsr   r   N)r!   
IndexErrorrY   r   r#   r$   )r.   r2   r0   r1   rZ   r4   r4   r5   getrow   s    
z_lil_base.getrowc                 C   sH   t |tr<t|dkr<t |d tr<t |d tr<| j| S t| |S )NrF   r   r   )r%   r&   r@   r   _get_intXintr   __getitem__)r.   keyr4   r4   r5   r_      s    
z_lil_base.__getitem__c              
   C   sZ   zt |}W n4 tttfyB } ztd|W Y d }~n
d }~0 0 |jdvrVtd|S )Nzinvalid index)r   rF   zIndex dimension must be <= 2)r(   asarrayr'   r,   MemoryErrorr\   ndim)r.   idxr1   xr3   r4   r4   r5   
_asindices   s     
z_lil_base._asindicesc                 C   s0   t | jd | jd | j| j||}| j|S Nr   r   )r   Zlil_get1r!   r#   r$   r   type)r.   rK   colvr4   r4   r5   r^      s    z_lil_base._get_intXintc                 C   s*   t || jd  }| |t||d S rg   )r+   indicesr!   _get_row_rangesslicer.   rK   ri   r4   r4   r5   _get_sliceXint   s    z_lil_base._get_sliceXintc                 C   s   |  }| |t||d S )Nr   )Zsqueezerl   rm   rn   r4   r4   r5   _get_arrayXint   s    z_lil_base._get_arrayXintc                 C   s   |  |f|S r6   rl   rn   r4   r4   r5   _get_intXslice   s    z_lil_base._get_intXslicec                 C   s    t || jd  }| ||S Nr   )r+   rk   r!   rl   rn   r4   r4   r5   _get_sliceXslice   s    z_lil_base._get_sliceXslicec                 C   s   |  ||S r6   rq   rn   r4   r4   r5   _get_arrayXslice   s    z_lil_base._get_arrayXslicec                 C   s   t j||jdd}| ||S )Nr   )r   Zndmin)r(   rI   r   _get_columnXarrayrn   r4   r4   r5   _get_intXarray   s    z_lil_base._get_intXarrayc                 C   s"   t j|| jd  }| ||S rs   )r(   Zarangerk   r!   rv   rn   r4   r4   r5   _get_sliceXarray   s    z_lil_base._get_sliceXarrayc                 C   s&   t |d d d f |\}}| ||S r6   )r   _get_arrayXarrayrn   r4   r4   r5   rv      s    z_lil_base._get_columnXarrayc              
   C   sX   t tjt||\}}| j|j| jd}t| jd | jd | j	| j
|j	|j
|| |S Nr   r   r   )mapr(   
atleast_2d_prepare_index_for_memoryviewrY   r!   r   r   Zlil_fancy_getr#   r$   )r.   rK   ri   r2   rW   rZ   r4   r4   r5   ry      s    z_lil_base._get_arrayXarrayc           	      C   sx   | | jd \}}}t|||}t|}| jt||f| jd}t| jd | jd | j| j	|j|j	||||| |S )a  
        Fast path for indexing in the case where column index is slice.

        This gains performance improvement over brute force by more
        efficient skipping of zeros, by accessing the elements
        column-wise in order.

        Parameters
        ----------
        rows : sequence or range
            Rows indexed. If range, must be within valid bounds.
        col_slice : slice
            Columns indexed

        r   r   r   )
rk   r!   r+   r@   rY   r   r   lil_get_row_rangesr#   r$   )	r.   r#   Z	col_sliceZj_startZj_stopZj_strideZ	col_rangeZnjrZ   r4   r4   r5   rl      s    z_lil_base._get_row_rangesc              	   C   s*   t | jd | jd | j| j||| d S rg   )r   Z
lil_insertr!   r#   r$   )r.   rK   ri   re   r4   r4   r5   _set_intXint   s    
z_lil_base._set_intXintc              	   C   sD   t tjt|||\}}}t| jd | jd | j| j||| d S rg   )	r{   r(   r|   r}   r   Zlil_fancy_setr!   r#   r$   )r.   rK   ri   re   r2   rW   r4   r4   r5   _set_arrayXarray   s
    z_lil_base._set_arrayXarrayc                 C   s4   t j| | jd}t||\}}| ||| d S )Nr   )r(   ra   toarrayr   r   r   )r.   rK   ri   re   _r4   r4   r5   _set_arrayXarray_sparse   s    z!_lil_base._set_arrayXarray_sparsec                 C   s   t |trt|dkr|\}}t |tr^t |tr^| j|}|jdkrPtd| |||S t |t	rt |t	r|t	d kr|t	d krt
|r|j| jkr| j|| jd}|j| _|j| _d S t| || d S )NrF   r   z&Trying to assign a sequence to an itemr   )r%   r&   r@   r   r   rh   sizer'   r   rm   r
   r!   rY   r#   r$   r   __setitem__)r.   r`   re   rK   ri   r4   r4   r5   r      s*    



z_lil_base.__setitem__c                    sj    dkr| j | j| jd}nJt| j }|  }||}t|jD ] \}} fdd|D |j|< qD|S )Nr   r   c                    s   g | ]}|  qS r4   r4   rB   rU   r8   r4   r5   rD     rE   z)_lil_base._mul_scalar.<locals>.<listcomp>)rY   r!   r   r   r   r    rS   r$   )r.   r8   rZ   Z	res_dtyperW   rC   r4   r   r5   _mul_scalar  s    
z_lil_base._mul_scalarc                    s^   t  rN|  }t|  |_t|jD ] \}} fdd|D |j|< q(|S |    S d S )Nc                    s   g | ]}|  qS r4   r4   r   r   r4   r5   rD   %  rE   z)_lil_base.__truediv__.<locals>.<listcomp>)r   r   r(   Zresult_typer   rS   r$   tocsr)r.   r8   rZ   rW   rC   r4   r   r5   __truediv__  s    z_lil_base.__truediv__c                 C   sJ   | j \}}| j| j | jd}t||| j| j|j|jt|d|d| |S rz   )r!   rY   r   r   r~   r#   r$   r+   )r.   r0   r1   rZ   r4   r4   r5   r   *  s    
z_lil_base.copyc                 O   s  t || j}t|\}}|| jkr2|r.|  S | S | j|| jd}|dkr| jd }t| jD ]F\}}	t|	D ]4\}
}t	|| | |\}}| ||f |||f< qnq^nr|dkr| jd }t| jD ]H\}}	t|	D ]6\}
}t	|||  ||\}}| ||f |||f< qqnt
d|S )Nr   Cr   Fr   z'order' must be 'C' or 'F')r   r!   r   r   rY   r   rS   r#   r(   Zunravel_indexr'   )r.   argskwargsr!   orderr   rZ   Zncolsr2   rK   ri   rW   Znew_rZnew_cZnrowsr4   r4   r5   reshape5  s*    



z_lil_base.reshapec           
      G   s   t |}|\}}| j\}}||k rD| jd | | _| jd | | _nL||krt| j|| _t| j|| _t||D ]}g | j|< g | j|< qv||k rt| j| jD ]&\}}t||}	||	d = ||	d = q|| _	d S r6   )
r   r!   r#   r$   r(   resizer+   zipr   r"   )
r.   r!   Znew_MZnew_Nr0   r1   r2   rK   r$   truncr4   r4   r5   r   U  s$    



z_lil_base.resizec                 C   sL   |  ||}t| jD ]0\}}t|D ]\}}| j| | |||f< q&q|S r6   )Z_process_toarray_argsrS   r#   r$   )r.   r   rJ   dr2   rK   rV   rW   r4   r4   r5   r   n  s
    z_lil_base.toarrayc                 C   s   | j |dj|ddjddS )Nr   F)axesr   )r   	transposer   )r.   r   r   r4   r4   r5   r   w  s    z_lil_base.transposec                 C   s   |r|   S | S d S r6   r   )r.   r   r4   r4   r5   r   |  s    z_lil_base.tolilc           
      C   s\  | j \}}|dks|dkr.| j||f| jdS || ttjjkrtj}tj|d |d}d|d< t	| j
|dd   tj||d |d }nz| j|d}tj||d}t	| j
| |jtjd}| jt||d}tj|d |d}d|d< tj|||dd  d tj||d}tj|| jd}	t| j
| t| j|	 | j|	||f| j dS )	Nr   r   r   )rJ   rR   )maxval)r   rJ   )r!   )r!   r-   r   r(   Ziinfoint32maxr)   r   Zlil_get_lengthsr#   ZcumsumZ_get_index_dtyperG   int64Zlil_flatten_to_arrayr$   )
r.   r   r0   r1   Z	idx_dtypeZindptrZnnzlengthsrk   r$   r4   r4   r5   r     s.    

z_lil_base.tocsr)NNF)N)NN)NF)F)F)*__name__
__module____qualname___formatr   r9   r:   r=   r>   rL   rM   r   __doc__rX   r[   r]   r_   rf   r^   ro   rp   rr   rt   ru   rw   rx   rv   ry   rl   r   r   r   r   r   r   r   r   r   r   r   r   r   r4   r4   r4   r5   r      s\   
-






	









 r   c                 C   s   | j |j kr|| j }n| j |j k r2| |j } | jjrL| j tjtjfvrX| tj} |jjrr|j tjtjfvr~|tj}|dur|jjs| }| ||fS | |fS dS )a  
    Convert index and data arrays to form suitable for passing to the
    Cython fancy getset routines.

    The conversions are necessary since to (i) ensure the integer
    index arrays are in one of the accepted types, and (ii) to ensure
    the arrays are writable so that Cython memoryview support doesn't
    choke on them.

    Parameters
    ----------
    i, j
        Index arrays
    x : optional
        Data arrays

    Returns
    -------
    i, j, x
        Re-formatted arrays (x is omitted, if input was None)

    N)	r   r    flagsZ	writeabler(   r   r   rH   r   )r2   rW   re   r4   r4   r5   r}     s    
r}   c                 C   s
   t | tS )a  Is `x` of lil_matrix type?

    Parameters
    ----------
    x
        object to check for being a lil matrix

    Returns
    -------
    bool
        True if `x` is a lil matrix, False otherwise

    Examples
    --------
    >>> from scipy.sparse import lil_array, lil_matrix, coo_matrix, isspmatrix_lil
    >>> isspmatrix_lil(lil_matrix([[5]]))
    True
    >>> isspmatrix_lil(lil_array([[5]]))
    False
    >>> isspmatrix_lil(coo_matrix([[5]]))
    False
    )r%   r   )re   r4   r4   r5   r     s    r   c                   @   s   e Zd ZdZdS )r   a  
    Row-based LIst of Lists sparse array.

    This is a structure for constructing sparse arrays incrementally.
    Note that inserting a single item can take linear time in the worst case;
    to construct the array efficiently, make sure the items are pre-sorted by
    index, per row.

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

        lil_array(S)
            with another sparse array or matrix S (equivalent to S.tolil())

        lil_array((M, N), [dtype])
            to construct an empty array with 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
    size
    data
        LIL format data array of the array
    rows
        LIL format row index array of the array
    T

    Notes
    -----
    Sparse arrays can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.

    Advantages of the LIL format
        - supports flexible slicing
        - changes to the array sparsity structure are efficient

    Disadvantages of the LIL format
        - arithmetic operations LIL + LIL are slow (consider CSR or CSC)
        - slow column slicing (consider CSC)
        - slow matrix vector products (consider CSR or CSC)

    Intended Usage
        - LIL is a convenient format for constructing sparse arrays
        - once an array has been constructed, convert to CSR or
          CSC format for fast arithmetic and matrix vector operations
        - consider using the COO format when constructing large arrays

    Data Structure
        - An array (``self.rows``) of rows, each of which is a sorted
          list of column indices of non-zero elements.
        - The corresponding nonzero values are stored in similar
          fashion in ``self.data``.

    Nr   r   r   r   r4   r4   r4   r5   r     s   r   c                   @   s   e Zd ZdZdS )r   a  
    Row-based LIst of Lists sparse matrix.

    This is a structure for constructing sparse matrices incrementally.
    Note that inserting a single item can take linear time in the worst case;
    to construct the matrix efficiently, make sure the items are pre-sorted by
    index, per row.

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

        lil_matrix(S)
            with another sparse array or matrix S (equivalent to S.tolil())

        lil_matrix((M, N), [dtype])
            to construct an empty matrix with 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
    size
    data
        LIL format data array of the matrix
    rows
        LIL format row index array of the matrix
    T

    Notes
    -----
    Sparse matrices can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.

    Advantages of the LIL format
        - supports flexible slicing
        - changes to the matrix sparsity structure are efficient

    Disadvantages of the LIL format
        - arithmetic operations LIL + LIL are slow (consider CSR or CSC)
        - slow column slicing (consider CSC)
        - slow matrix vector products (consider CSR or CSC)

    Intended Usage
        - LIL is a convenient format for constructing sparse matrices
        - once a matrix has been constructed, convert to CSR or
          CSC format for fast arithmetic and matrix vector operations
        - consider using the COO format when constructing large matrices

    Data Structure
        - An array (``self.rows``) of rows, each of which is a sorted
          list of column indices of non-zero elements.
        - The corresponding nonzero values are stored in similar
          fashion in ``self.data``.

    Nr   r4   r4   r4   r5   r   ,  s   r   )N)r   __docformat____all__bisectr   numpyr(   Z_matrixr   _baser   r	   r
   _indexr   r   r   Z_sputilsr   r   r   r   r   r   rO   r   r   r}   r   r   r   r4   r4   r4   r5   <module>   s"       
)A