L iRdZdZgdZddlmZddlZddlmZddl m Z m Z m Z dd l mZmZmZdd lmZmZmZmZmZmZdd lmZGd d e eZddZdZGddee ZGddeeZy)z"List of Lists sparse matrix class zrestructuredtext en) lil_array lil_matrixisspmatrix_lil) bisect_leftN)spmatrix)_spbasesparrayissparse) IndexMixin INT_TYPES_broadcast_arrays)getdtypeisshape isscalarlike upcast_scalar check_shapecheck_reshape_kwargs) _csparsetoolsceZdZdZd'dddZdZdZdZdZd(d Z e jje _ d(d Z e jje _ d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!d Z"e jDje"_ d!Z#e jFje#_ d"Z$e jHje$_ d)d#Z%e jJje%_ d*d$Z&e jLje&_ d+d%Z'e jNje'_ d+d&Z(e jPje(_ y), _lil_baselilNmaxprintctj|||t||t|_t |r|j dk(r|r|j}n|j}|t|}|j|d}t|j|_ |j|_|j|_ |j|_yt|t rt#|r| t%d|\}} t|| f|_ t'j(|ft*|_ t'j(|ft*|_t-|D] } g|j| <g|j| <"yt/d |j1|}t|t2r(|j4d k7rt%d |j4d |j7||j}t|j|_ t|j|_|j|_ |j|_y#t.$r} t/d | d} ~ wwxYw) Nr)defaultrFcopyzinvalid use of shape parameterdtypez(unrecognized lil_array constructor usagezunsupported matrix typezLIL arrays don't support zD input. Use 2D)r __init__rfloatr r formatrtolilastypershape_shaperowsdata isinstancetupler ValueErrornpemptyobjectrange TypeError _ascontainerr ndim_csr_container) selfarg1r'r rrAnewdtypeMNies W/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/sparse/_lil.pyr"z_lil_base.__init__sth7eT59  D>{{e#IIKJJL #E?HHXEH2%agg.DKDJDIDI U #t}$$%EFF1)1a&1 HHaT8 HHaT8 q&A#%DIIaL#%DIIaL& JKK B%%d+$(QVVq[ #__iadd__z_lil_base.__iadd__F5LQqS  c"||z |ddddf<|Sr@rArBs r>__isub__z_lil_base.__isub__JrErFcDt|r||z|ddddf<|StSr@rNotImplementedrBs r>__imul__z_lil_base.__imul__N&  u D1IK! !rFcDt|r||z |ddddf<|StSr@rJrBs r> __itruediv__z_lil_base.__itruediv__UrMrFc|,t|jDcgc] }t|c}S|dkr|dz }|dk(rRtj|j dtj }|jD]}||xxdz cc<|S|dk(rFtj|jDcgc] }t|c}tj Stdcc}wcc}w)Nrr!rraxis out of bounds) sumr*lenr.zerosr'intpr)arrayr-)r6axisrowvalsoutrows r>_getnnzz_lil_base._getnnz_s <DII>G >? ? !8 AID 19((4::a=8Cyy CA  J QY88CgS\C277S S12 2?Ds C&2C+cL|td|jDS|dkr|dz }|dk(rtj|jdtj }t |j|jD]6\}}t ||Dcgc] \}}|dk7s |}}}||xxdz cc<8|S|dk(rPtj|jDcgc]}tj|c}tj Stdcc}}wcc}w)Nc3FK|]}tj|ywr@)r. count_nonzero).0rXs r> z*_lil_base.count_nonzero..rsJWr''0Js!rr!rrrQ) rRr*r.rTr'rUzipr)rVr^r-) r6rWrYrZr*cdmaskrXs r>r^z_lil_base.count_nonzerops <J JJ J !8 AID 19((4::a=8C DII6  T&)#tn?daQ??D Q  J QY88:>))Dw!!'*DBGG 12 2@ Es D#DD!c|jd|jdf|j}|j||jd<|j||jd<|S)z;Returns a view of the 'i'th row (without copying). rrr)_lil_containerr'r r)r*)r6r<news r> getrowviewz_lil_base.getrowviewsY!!1djjm"4DJJ!Giil iil  rFc|j\}}|dkr||z }|dks||k\r td|jd|f|j}|j|dd|jd<|j |dd|j d<|S)z)Returns a copy of the 'i'th row. rzrow index out of boundsrrN)r' IndexErrorrfr r)r*)r6r<r:r;rgs r>getrowz_lil_base.getrowszz1 q5 FA q5AF67 7!!1a& !;iil1o iil1o  rFct|trCt|dk(r5t|dtr"t|dtr|j|St j ||S)Nr!rr)r+r,rSr _get_intXintr __getitem__)r6keys r>rnz_lil_base.__getitem__sX sE "s3x1}3q69-3q69-$4$$c* *%%dC00rFc tj|}|j dvr t d|S#tttf$r}t d|d}~wwxYw)Nz invalid index)rr!zIndex dimension must be <= 2)r.asarrayr-r2 MemoryErrorrjr4)r6idxr;xr=s r> _asindicesz_lil_base._asindicessY 5 3A 66 ;< < I{3 5_-1 4 5s2A AActj|jd|jd|j|j||}|j j |SNrr)rlil_get1r'r)r*r type)r6rZcolvs r>rmz_lil_base._get_intXintsJ  " "4::a=$**Q-#'99c3 8zzq!!rFct|j|jd}|j|t ||dzSrw)r1indicesr'_get_row_rangesslicer6rZrzs r>_get_sliceXintz_lil_base._get_sliceXints;S[[A/0##CsCE):;;rFc|j|jt||dz}|jdkDr|j |j S|S)Nr)r~ravelrr4reshaper')r6rZrzress r>_get_arrayXintz_lil_base._get_arrayXintsH""399;c3q50AB 88a<;;syy) ) rFc(|j|f|Sr@r~rs r>_get_intXslicez_lil_base._get_intXslices##SFC00rFcnt|j|jd}|j||SNr)r1r}r'r~rs r>_get_sliceXslicez_lil_base._get_sliceXslices0S[[A/0##C--rFc&|j||Sr@rrs r>_get_arrayXslicez_lil_base._get_arrayXslices##C--rFcjtj||jd}|j||S)Nr)r ndmin)r.rVr _get_columnXarrayrs r>_get_intXarrayz_lil_base._get_intXarrays+hhs#))15%%c3//rFctj|j|jd}|j ||Sr)r.aranger}r'rrs r>_get_sliceXarrayz_lil_base._get_sliceXarrays4iiTZZ]34%%c3//rFcRt|dddf|\}}|j||Sr@)r_get_arrayXarrayrs r>rz_lil_base._get_columnXarrays-$S4[#6S$$S#..rFc ^ttjt||\}}|j |j |j }tj|j d|j d|j|j|j|j|||SNrrr) mapr. atleast_2d_prepare_index_for_memoryviewrfr'r r lil_fancy_getr)r*)r6rZrzr<jrgs r>rz_lil_base._get_arrayXarrays2=="?S"IJ1!!!''!<##DJJqM4::a=$(IItyy$'HHchh$%q * rFc |j|jd\}}}t|||}t|}|j t||f|j }t j|jd|jd|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 rrr) r}r'r1rSrfr rlil_get_row_rangesr)r*) r6r) col_slicej_startj_stopj_stride col_rangenjrgs r>r~z_lil_base._get_row_rangess %.$5$5djjm$D!'684 ^!!3t9b/!D((A 1 )-DII),388)-)0&(B  H  rFc tj|jd|jd|j|j|||yrw)r lil_insertr'r)r*)r6rZrzrts r> _set_intXintz_lil_base._set_intXints7  A 1 tyy!%Ca 9rFc ttjt|||\}}}t j |j d|j d|j|j|||yrw) rr.rrr lil_fancy_setr'r)r*)r6rZrzrtr<rs r>_set_arrayXarrayz_lil_base._set_arrayXarraysXbmm%B3Q%OP1a##DJJqM4::a=$(IItyy$%q! -rFctj|j|j}t ||\}}|j |||y)Nr)r.rqtoarrayr rr)r6rZrzrt_s r>_set_arrayXarray_sparsez!_lil_base._set_arrayXarray_sparses> JJqyy{$** 5 C(1 c3*rFct|trt|dk(r |\}}t|trXt|trH|jj |}|j dkDr td|j|||St|trt|tr|tdk(rr|tdk(rdt|rY|j|jk(r@|j||j}|j|_ |j|_ytj |||y)Nr!rz&Trying to assign a sequence to an itemr)r+r,rSr r rysizer-rrr r'rfr)r*r __setitem__)r6rortrZrzs r>rz_lil_base.__setitem__ s c5 !c#h!mHC#y)ji.HJJOOA&66A:$%MNN((c1553&:c5+A5;&3%++=QKAGGtzz$9'''<FF FF tS!,rFcT|dk(r)|j|j|j}|St|j|}|j }|j |}t |jD]%\}}|Dcgc]}||z c}|j|<'|Scc}w)Nrr)rfr'r rrr& enumerater*)r6rCrg res_dtyperrXvals r> _mul_scalarz_lil_base._mul_scalars A:%%djj %CC &djj%8I))+C**Y'C'1 = 74; __truediv__z_lil_base.__truediv__-sz  ))+CtU3CI'1 = 74;rz_lil_base.copy8smzz1!!$**DJJ!?((Atyy$))),388U1X)*Aq! 5 rFct||j}t|\}}||jk(r|r|jS|S|j ||j }|dk(rl|jd}t |jD]C\}} t | D]0\} } tj||z| z|\} } ||| f|| | f<2E|S|dk(rm|jd}t |jD]D\}} t | D]1\} } tj|| |zz||\} } ||| f|| | f<3F|Std)NrCrFrz'order' must be 'C' or 'F') rr'rrrfr rr)r. unravel_indexr-)r6argskwargsr'orderrrgncolsr<rZrzrnew_rnew_cnrowss r>rz_lil_base.reshapeCsqD$**-*62 t DJJ yy{" !!%tzz!: C<JJqME#DII. 33'n3FC#%#3#3AIM5#ILE5(,QT Cu %3 3 c\JJqME#DII. 33'n3FC#%#3#3AE M5%#PLE5(,QT Cu %3 3 9: :rFc.t|}|\}}|j\}}||kr)|jd||_|jd||_n~||kDryt j |j||_t j |j||_t ||D] }g|j|<g|j|<"||kr>t|j|jD]\}}t||} || d=|| d=||_ yr@) rr'r)r*r.resizer1rarr() r6r'new_Mnew_Nr:r;r<rZr*truncs r>rz_lil_base.resizecsE" uzz1 19 &5)DI &5)DI QY $))U3DI $))U3DI1e_ "! ! ! !  " 19 DII6 ! T#C/KL !  rFc|j||}t|jD]/\}}t|D]\}}|j|||||f<1|Sr@)_process_toarray_argsrr)r*)r6rrYrcr<rZposrs r>rz_lil_base.toarray|sh  & &uc 2 * ,FAs#C. ,Q))A,s+!Q$ , ,rFch|j|j|djdS)NrF)axesr)r transposer%)r6rrs r>rz_lil_base.transposes2zztz$..Du.EKKQVKWWrFc*|r|jS|Sr@r)r6rs r>r%z_lil_base.tolils 99; KrFcd|j\}}|dk(s|dk(r|j||f|jS||ztjtj j krotj }tj|dz|}d|d<tj|j|ddtj|||d}n|j|}tj||}tj|j||jtj}|jt ||}tj|dz|}d|d<tj|||ddtj||}tj||j} tj|j|tj|j | |j| ||f|jS) Nrrr)rY)maxval)r rY)r')r'r5r r.iinfoint32maxr/rlil_get_lengthsr)cumsum_get_index_dtyperRint64lil_flatten_to_arrayr*) r6rr:r; idx_dtypeindptrnnzlengthsr}r*s r>rz_lil_base.tocsrszz1 6Q!V&&1vTZZ&@ @ Q3"((288$(( (IXXa!e95FF1I  ) )$))VABZ @ IIf& )*C--Q-7Ihhq 2G  ) )$))W =++BHH+-C--SC[-AIXXa!e95FF1I IIgYF12J ?((3i0xx4::.**499g>**499d;""D'6#:$**"MMrF)NNFr@)NN)NF)F))__name__ __module__ __qualname___formatr"rDrHrLrOr[r __doc__r^rhrkrnrurmrrrrrrrrrr~rrrrrrrrrrrr%rrArFr>rrsrG-T-^"" 3oo--GO3&$1199M 1" < 1..00/ :9- + -(  (<<''DL<oo--GO.^^++FNoo--GOX ))11I MM))EMN@MM))EMrFrc|j|jkDr|j|j}n4|j|jkr|j|j}|jjr,|jtj tj fvr|jtj}|jjr,|jtj tj fvr|jtj}|+|jjs|j}|||fS||fS)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.rrrUr)r<rrts r>rrs. ww HHQWW  177  HHQWW  77  "((/C C HHRWW  77  "((/C C HHRWW }ww  A!Qw!t rFc"t|tS)aIs `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)rts r>rrs. a $$rFceZdZdZy)ra 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``. NrrrrrArFr>rr=rFrceZdZdZy)ra 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``. NrrArFr>rr:rrFrr@)r __docformat____all__bisectrnumpyr._matrixr_baser r r _indexr r r_sputilsrrrrrrrrrrrrrArFr>rsq& 7--<<::^*^*B &R%6> 7>B>9>rF