
    ^jX                         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 ddlmZ  G d de
e      ZddZd Z G d dee      Z G d dee      Zy)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)_csparsetoolsc                      e Zd ZdZd)dddZd Zd Zd Zd	 Zd*d
Z	e
j                  j                  e	_        d*dZe
j                  j                  e_        d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!e
jB                  j                  e!_        d!dd"d#Z"e
jD                  j                  e"_        d$ Z#e
jF                  j                  e#_        d+d%Z$e
jH                  j                  e$_        d,d&Z%e
jJ                  j                  e%_        d-d'Z&e
jL                  j                  e&_        d-d(Z'e
jN                  j                  e'_        y).	_lil_baselilNFmaxprintc                   t        j                  | ||       t        ||t              | _        t        |      r|j                  dk(  r|r|j                         }n|j                         }|t        |      }|j                  |d      }t        |j                        | _        |j                  | _        |j                  | _        |j                  | _        y t        |t               rt#        |      r|t%        d      |\  }}	t        ||	f      | _        t'        j(                  |ft*              | _        t'        j(                  |ft*              | _        t-        |      D ]   }
g | j                  |
<   g | j                  |
<   " y t/        d      	 | j1                  |      }t        | t2              r(|j4                  d
k7  rt%        d|j4                   d      | j7                  ||      j                         }t        |j                        | _        t        |j                        | _        |j                  | _        |j                  | _        y # t.        $ r}t/        d	      |d }~ww xY w)Nr   )defaultr   Fcopyzinvalid use of shape parameterdtypez(unrecognized lil_array constructor usagezunsupported matrix type   zLIL arrays don't support zD input. Use 2D)r	   __init__r   floatr   r   formatr   tolilastyper   shape_shaperowsdata
isinstancetupler   
ValueErrornpemptyobjectrange	TypeError_ascontainerr
   ndim_csr_container)selfarg1r&   r   r   r   AnewdtypeMNies               J/opt/ringagent/.cad-venv/lib/python3.12/site-packages/scipy/sparse/_lil.pyr!   z_lil_base.__init__   s   th7eT59
 D>{{e#IIKJJL #E?HHXEH2%agg.DKDJDIDIU#t}$$%EFF1)1a&1HHaT8	HHaT8	q &A#%DIIaL#%DIIaL&   JKKB%%d+ $(QVVq[ #<QVVHO!TUU##AU#399;A%agg.DK!!''*DJDIDI  B 9:ABs   I 	I2!I--I2c                 "    | |z   | d d d d f<   | S N r5   others     r=   __iadd__z_lil_base.__iadd__F       5LQqS	    c                 "    | |z
  | d d d d f<   | S r?   r@   rA   s     r=   __isub__z_lil_base.__isub__J   rD   rE   c                 D    t        |      r| |z  | d d d d f<   | S t        S r?   r   NotImplementedrA   s     r=   __imul__z_lil_base.__imul__N   &    uD1IK!!rE   c                 D    t        |      r| |z  | d d d d f<   | S t        S r?   rI   rA   s     r=   __itruediv__z_lil_base.__itruediv__U   rL   rE   c                    |,t        | j                  D cg c]  }t        |       c}      S |dk  r|dz  }|dk(  rRt        j                  | j
                  d   t        j                        }| j                  D ]  }||xx   dz  cc<    |S |dk(  rFt        j                  | j                  D cg c]  }t        |       c}t        j                        S t        d      c c}w c c}w )Nr   r    r   r   axis out of bounds)
sumr)   lenr-   zerosr&   intpr(   arrayr,   )r5   axisrowvalsoutrows        r=   _getnnzz_lil_base._getnnz_   s    <DII>G>??!8AID19((4::a=8Cyy CAJQY88CgS\C277SS122 ? Ds   C&2C+c                 L   |t        d | j                  D              S |dk  r|dz  }|dk(  rt        j                  | j                  d   t        j
                        }t        | j                  | j                        D ]6  \  }}t        ||      D cg c]  \  }}|dk7  s| }}}||xx   dz  cc<   8 |S |dk(  rPt        j                  | j                  D cg c]  }t        j                  |       c}t        j
                        S t        d      c c}}w c c}w )Nc              3   F   K   | ]  }t        j                  |        y wr?   )r-   count_nonzero).0rW   s     r=   	<genexpr>z*_lil_base.count_nonzero.<locals>.<genexpr>r   s     JWr''0Js   !r   r    r   r   rP   )rQ   r)   r-   rS   r&   rT   zipr(   rU   r]   r,   )	r5   rV   rX   rY   r)   cdmaskrW   s	            r=   r]   z_lil_base.count_nonzerop   s    <J		JJJ!8AID19((4::a=8C DII6 	T&)#tn?daQ??D	Q	 JQY88:>))Dw!!'*DBGG  122 @
 Es   D#DD!c                     | j                  d| j                  d   f| j                        }| j                  |   |j                  d<   | j                  |   |j                  d<   |S )zReturns a view of the 'i'th row (without copying).

        Parameters
        ----------
        i : int
            Row to return view of.

        Returns
        -------
        lil_array or lil_matrix
            A view of the 'i'th row.
        r   r   r   )_lil_containerr&   r   r(   r)   )r5   r;   news      r=   
getrowviewz_lil_base.getrowview   sY     !!1djjm"4DJJ!Giiliil
rE   c                    | j                   \  }}|dk  r||z  }|dk  s||k\  rt        d      | j                  d|f| j                        }| j                  |   dd |j                  d<   | j
                  |   dd |j
                  d<   |S )zReturns a copy of the 'i'th row.

        Parameters
        ----------
        i : int
            Row to return a copy of.

        Returns
        -------
        lil_array or lil_matrix
            A copy of the 'i'th row.
        r   zrow index out of boundsr   r   N)r&   
IndexErrorre   r   r(   r)   )r5   r;   r9   r:   rf   s        r=   getrowz_lil_base.getrow   s     zz1q5FAq5AF677!!1a&

!;iil1oiil1o
rE   c                     t        |t              rCt        |      dk(  r5t        |d   t              r"t        |d   t              r | j                  | S t        j                  | |      S )Nr    r   r   )r*   r+   rR   r   _get_intXintr   __getitem__)r5   keys     r=   rm   z_lil_base.__getitem__   sX    sE"s3x1}3q69-3q69-$4$$c**%%dC00rE   c                     t        j                  | j                  d   | j                  d   | j                  | j                  ||      }| j
                  j                  |      S Nr   r   )r   lil_get1r&   r(   r)   r   type)r5   rY   colvs       r=   rl   z_lil_base._get_intXint   sJ    ""4::a=$**Q-#'99c38zzq!!rE   c                     t        |j                  | j                  d          }| j                  |t	        ||dz               S rp   )r0   indicesr&   _get_row_rangesslicer5   rY   rs   s      r=   _get_sliceXintz_lil_base._get_sliceXint   s;    S[[A/0##CsCE):;;rE   c                     | j                  |j                         t        ||dz               }|j                  dkD  r|j	                  |j
                        S |S )Nr   )rw   ravelrx   r3   reshaper&   )r5   rY   rs   ress       r=   _get_arrayXintz_lil_base._get_arrayXint   sH    ""399;c3q50AB88a<;;syy))
rE   c                 (    | j                  |f|      S r?   rw   ry   s      r=   _get_intXslicez_lil_base._get_intXslice   s    ##SFC00rE   c                 n    t        |j                  | j                  d          }| j                  ||      S Nr   )r0   rv   r&   rw   ry   s      r=   _get_sliceXslicez_lil_base._get_sliceXslice   s0    S[[A/0##C--rE   c                 &    | j                  ||      S r?   r   ry   s      r=   _get_arrayXslicez_lil_base._get_arrayXslice   s    ##C--rE   c                 j    t        j                  ||j                  d      }| j                  ||      S )Nr   )r   ndmin)r-   rU   r   _get_columnXarrayry   s      r=   _get_intXarrayz_lil_base._get_intXarray   s+    hhs#))15%%c3//rE   c                     t        j                  |j                  | j                  d          }| j	                  ||      S r   )r-   arangerv   r&   r   ry   s      r=   _get_sliceXarrayz_lil_base._get_sliceXarray   s4    iiTZZ]34%%c3//rE   c                 R    t        |d d d f   |      \  }}| j                  ||      S r?   )r   _get_arrayXarrayry   s      r=   r   z_lil_base._get_columnXarray   s-    $S4[#6S$$S#..rE   c           
      ^   t        t        j                  t        ||            \  }}| j	                  |j
                  | j                        }t        j                  | j
                  d   | j
                  d   | j                  | j                  |j                  |j                  ||       |S Nr   r   r   )mapr-   
atleast_2d_prepare_index_for_memoryviewre   r&   r   r   lil_fancy_getr(   r)   )r5   rY   rs   r;   jrf   s         r=   r   z_lil_base._get_arrayXarray   s    2=="?S"IJ1!!!''!<##DJJqM4::a=$(IItyy$'HHchh$%q	* 
rE   c                    |j                  | j                  d         \  }}}t        |||      }t        |      }| j	                  t        |      |f| j
                        }t        j                  | 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   )
rv   r&   r0   rR   re   r   r   lil_get_row_rangesr(   r)   )	r5   r(   	col_slicej_startj_stopj_stride	col_rangenjrf   s	            r=   rw   z_lil_base._get_row_ranges   s      %.$5$5djjm$D!'684	^!!3t9b/!D((A

1)-DII),388)-)0&(B		H 
rE   c           	          t        j                  | j                  d   | j                  d   | j                  | j                  |||       y rp   )r   
lil_insertr&   r(   r)   )r5   rY   rs   xs       r=   _set_intXintz_lil_base._set_intXint  s7      A

1tyy!%Ca	9rE   c           	          t        t        j                  t        |||            \  }}}t	        j
                  | j                  d   | j                  d   | j                  | j                  |||       y rp   )	r   r-   r   r   r   lil_fancy_setr&   r(   r)   )r5   rY   rs   r   r;   r   s         r=   _set_arrayXarrayz_lil_base._set_arrayXarray	  sX    bmm%B3Q%OP1a##DJJqM4::a=$(IItyy$%q!	-rE   c                     t        j                  |j                         | j                        }t	        ||      \  }}| j                  |||       y )Nr   )r-   asarraytoarrayr   r   r   )r5   rY   rs   r   _s        r=   _set_arrayXarray_sparsez!_lil_base._set_arrayXarray_sparse  s>    JJqyy{$**5 C(1c3*rE   c                    t        |t              rat        |      dk(  rR|\  }}t        |t              rt        |t              rt	        |      r|j                         }t        |t        j                        r|j                         }| j                  j                  |      }|j                  dkD  rt        d      | j                  |||      S t        |t              rt        |t              r|t        d       k(  rr|t        d       k(  rdt	        |      rY|j                  | j                  k(  r@| j!                  || j                        }|j"                  | _        |j$                  | _        y t'        j(                  | ||       y )Nr    r   z&Trying to assign a sequence to an itemr   )r*   r+   rR   r   r   r   r-   ndarrayitemr   rr   sizer,   r   rx   r&   re   r(   r)   r   __setitem__)r5   rn   r   rY   rs   s        r=   r   z_lil_base.__setitem__  s   c5!c#h!mHC#y)ji.HA;		Aa,AJJOOA&66A:$%MNN((c1553&:c5+A5;&3%++=QKAGGtzz$9'''<FF	FF	tS!,rE   c                 4   |dk(  r)| j                  | j                  | j                        }|S t        | j                  |      }| j	                  |      }t        |j                        D ]%  \  }}|D cg c]  }||z  	 c}|j                  |<   ' |S c c}w )Nr   r   )re   r&   r   r   r%   	enumerater)   )r5   rB   rf   	res_dtyper   rW   vals          r=   _mul_scalarz_lil_base._mul_scalar-  s    A:%%djj

%CC 
 &djj%8I++i(C'1 =
74;<Ss5y<=
 =s   5Bc                    t        |      rj| j                         }t        j                  | |      |_        t        |j                        D ]%  \  }}|D cg c]  }||z  	 c}|j                  |<   ' |S | j                         |z  S c c}w r?   )r   r   r-   result_typer   r   r)   tocsr)r5   rB   rf   r   rW   r   s         r=   __truediv__z_lil_base.__truediv__:  sz    ))+CtU3CI'1 =
74;<Ss5y<=J::<%'' =s   B	c                    | j                   \  }}| j                  | j                   | j                        }t        j                  ||| j
                  | j                  |j
                  |j                  t        |      d|d|       |S r   )r&   re   r   r   r   r(   r)   r0   )r5   r9   r:   rf   s       r=   r   z_lil_base.copyE  sm    zz1!!$**DJJ!?((Atyy$))),388U1X)*Aq!	5 
rE   C)orderr   c                   t        || j                        }|| j                  k(  r|r| j                         S | S | j                  || j                        }|dk(  rl| j                  d   }t        | j                        D ]C  \  }}t        |      D ]0  \  }}	t        j                  ||z  |	z   |      \  }
}| ||	f   ||
|f<   2 E |S |dk(  rm| j                  d   }t        | j                        D ]D  \  }}t        |      D ]1  \  }}	t        j                  ||	|z  z   ||      \  }
}| ||	f   ||
|f<   3 F |S t        d      )Nr   r   r   Fr   z'order' must be 'C' or 'F')
r   r&   r   re   r   r   r(   r-   unravel_indexr,   )r5   r   r   r&   rf   ncolsr;   rY   rs   r   new_rnew_cnrowss                r=   r}   z_lil_base.reshapeP  sc   E4::. DJJyy{"!!%tzz!:C<JJqME#DII. 33'n 3FC#%#3#3AIM5#ILE5(,QT
Cu%33 
 c\JJqME#DII. 33'n 3FC#%#3#3AE	M5%#PLE5(,QT
Cu%33 
 9::rE   c                 .   t        |      }|\  }}| j                  \  }}||k  r)| j                  d | | _        | j                  d | | _        n~||kD  ryt	        j
                  | j                  |      | _        t	        j
                  | j                  |      | _        t        ||      D ]   }g | j                  |<   g | j                  |<   " ||k  r>t        | j                  | j                        D ]  \  }}t        ||      }	||	d = ||	d =  || _	        y r?   )
r   r&   r(   r)   r-   resizer0   r`   r   r'   )
r5   r&   new_Mnew_Nr9   r:   r;   rY   r)   truncs
             r=   r   z_lil_base.resizeo  s   E"uzz119		&5)DI		&5)DIQY		$))U3DI		$))U3DI1e_ "!		!!		!" 19 DII6 !	T#C/KL!
 rE   c                     | j                  ||      }t        | j                        D ]/  \  }}t        |      D ]  \  }}| j                  |   |   |||f<    1 |S r?   )_process_toarray_argsr   r(   r)   )r5   r   rX   rb   r;   rY   posr   s           r=   r   z_lil_base.toarray  sh    &&uc2		* 	,FAs#C. ,Q))A,s+!Q$,	, rE   c                 h    | j                  |      j                  |d      j                  d      S )Nr   F)axesr   )r   	transposer$   )r5   r   r   s      r=   r   z_lil_base.transpose  s2    zztz$..Du.EKKQVKWWrE   c                 *    |r| j                         S | S r?   r   )r5   r   s     r=   r$   z_lil_base.tolil  s    99;KrE   c                 d   | j                   \  }}|dk(  s|dk(  r| j                  ||f| j                        S ||z  t        j                  t        j
                        j                  k  rot        j
                  }t        j                  |dz   |      }d|d<   t        j                  | j                  |dd         t        j                  ||       |d   }n| j                  |      }t        j                  ||      }t        j                  | j                  |       |j                  t        j                        }| j                  t        ||            }t        j                  |dz   |      }d|d<   t        j                  |||dd         t        j                  ||      }t        j                  || j                        }	t        j                  | j                  |       t        j                  | j                   |	       | j                  |	||f| j                         S )	Nr   r   r   )rX   )maxval)r   rX   )r&   )r&   r4   r   r-   iinfoint32maxr.   r   lil_get_lengthsr(   cumsum_get_index_dtyperQ   int64lil_flatten_to_arrayr)   )
r5   r   r9   r:   	idx_dtypeindptrnnzlengthsrv   r)   s
             r=   r   z_lil_base.tocsr  s   zz16Q!V&&1vTZZ&@@ Q3"((288$(((IXXa!e95FF1I))$))VABZ@IIf&)*C--Q-7Ihhq	2G))$))W=++BHH+-C--SC[-AIXXa!e95FF1IIIgYF12J?((3i0xx4::.**499g>**499d; ""D'6#:$**"MMrE   )NNFr?   )NN)NF)F)(__name__
__module____qualname___formatr!   rC   rG   rK   rN   rZ   r	   __doc__r]   rg   rj   rm   rl   rz   r   r   r   r   r   r   r   r   rw   r   r   r   r   r   r   r   r}   r   r   r   r$   r   r@   rE   r=   r   r      sr   G-T -^""3 oo--GO3& $1199M$.1"
<1..00/
:9-+-0	( <<''DL$'e : oo--GO. ^^++FN oo--GOX  ))11I MM))EMN@ MM))EMrE   r   c                    | j                   |j                   kD  r|j                  | j                         }n4| j                   |j                   k  r| j                  |j                         } | j                  j                  r,| j                   t        j
                  t        j                  fvr| j                  t        j                        } |j                  j                  r,|j                   t        j
                  t        j                  fvr|j                  t        j                        }|+|j                  j                  s|j                         }| ||fS | |f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)

    )	r   r%   flags	writeabler-   r   r   rT   r   )r;   r   r   s      r=   r   r     s    . 	wwHHQWW	
177	HHQWW77"((/C CHHRWW77"((/C CHHRWW}ww  A!Qw!trE   c                 "    t        | t              S )a  Is `x` of lil_matrix type?

    .. warning::

       SciPy sparse is shifting from a sparse matrix interface to a sparse
       array interface. In the next few releases we expect to deprecate the
       sparse matrix interface. For documentation of the matrix
       interface, see the :ref:`spmatrix interface docs <spmatrix_api>`.
       For guidance on converting existing code to sparse arrays, see
       :ref:`Migration from spmatrix to sparray <migration_to_sparray>`.

    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   )r   s    r=   r   r     s    @ a$$rE   c                       e Zd ZdZy)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
    ----------
    data : ndarray
        LIL format data array of the array
    rows : ndarray
        LIL format row index array of the array
    dtype : dtype
        Data type of the array
    shape : 2-tuple
        Shape of the array
    ndim : int
        Number of dimensions (this is always 2)
    format : str
        Three letter code for the format of the array storage, e.g. 'lil'
    nnz : int
        Number of values stored in the array
    size : int
        Number of values stored in the array
    T : lil_array
        The transpose of the array
    mT : lil_array
        The matrix transpose of the array

    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   r@   rE   r=   r   r     s    DrE   r   c                       e Zd ZdZy)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.

    .. warning::

       SciPy sparse is shifting from a sparse matrix interface to a sparse
       array interface. In the next few releases we expect to deprecate the
       sparse matrix interface. For documentation of the matrix
       interface, see the :ref:`spmatrix interface docs <spmatrix_api>`.
       For guidance on converting existing code to sparse arrays, see
       :ref:`Migration from spmatrix to sparray <migration_to_sparray>`.

    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
    ----------
    data : ndarray
        LIL format data array of the matrix
    rows : ndarray
        LIL format row index array of the matrix
    dtype : dtype
        Data type of the matrix
    shape : 2-tuple
        Shape of the matrix
    ndim : int
        Number of dimensions (this is always 2)
    format : str
        Three letter code for the format of the matrix storage, e.g. 'lil'
    nnz : int
        Number of values stored in the matrix
    size : int
        Number of values stored in the matrix
    T : lil_matrix
        The transpose of the matrix
    mT : lil_array
        The transpose of the matrix

    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   r@   rE   r=   r   r   V  s    MrE   r   r?   )r   __docformat____all__bisectr   numpyr-   _matrixr   _baser	   r
   r   _indexr   r   r   _sputilsr   r   r   r   r    r   r   r   r   r   r   r@   rE   r=   <module>r      sv    &
7    - - < <$ $ j* j*Z&R %HE	7 EPN9 NrE   