L igRldZddlZddlmZmZmZddlZddlZddl m Z ddl m Z ddlmZmZgdZej$ej&ej(ej*ej,ej.ej0eeej2ej4ej6ej8ej:ej<ej>ej@gZ!iZ"dZ#dZ$d Z%d Z&d Z'd-d Z(d.d ejRfdZ*ejVdfdZ,d/dZ-dej\d ej\fdZ/d e0fdZ1d e0fdZ2d0ddd e0fdZ3d e0fdZ4d e0fdZ5d e0fdZ6ddd e7e8dfdzfdZ9d1ddd e7e8dffd Z:d!Z;d"Zd*Z?d1d+Z@d ejRfd,ZAy)2z, Utility functions for sparse matrix module N)AnyLiteralUnion)prod)np_longnp_ulong) upcastgetdtypegetdata isscalarlike isintlikeisshape issequenceisdenseismatrix get_sum_dtypebroadcast_shapesctjt|}||Stj|}t D].}tj ||s|tt|<|cStd|)aReturns the nearest supported sparse dtype for the combination of one or more types. upcast(t0, t1, ..., tn) -> T where T is a supported dtype Examples -------- >>> from scipy.sparse._sputils import upcast >>> upcast('int32') >>> upcast('bool') >>> upcast('int32','float32') >>> upcast('bool',complex,float) z#no supported conversion for types: ) _upcast_memogethashnp result_typesupported_dtypescan_cast TypeError)argstr s [/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/scipy/sparse/_sputils.pyr r su( d$A} ^^T "F  ;;vq !'(Ld $H 9$B CCctj|}||Stttj |}|t|<|S)z9Same as `upcast` but taking dtype.char as input (faster).)rrr maprdtype)rrs r upcast_charr$;s@A}BHHd#$AL Hr cLtjdg||zjS)z`Determine data type for binary operation between an array of type `dtype` and a scalar. rr#)rarrayr#)r#scalars r upcast_scalarr)Es" HHaS & / 6 66r c>|jjtjtjjkDr|jdk(r|j tjS|j }|j}|tjtjj kDs0|tjtjjkr td|j tjS|S)z Down-cast index array to np.intp dtype if it is of a larger dtype. Raise an error if the array contains a value that is too large for intp. rzzCannot deal with arrays with indices larger than the machine maximum address size (e.g. 64-bit indices on 32-bit machine).) r#itemsizerintpsizeastypemaxminiinfo ValueError)arrmaxvalminvals rdowncast_intp_indexr6Ls yyBHHRWW-666 88q=::bgg& & BHHRWW%)) )Vbhhrww6G6K6K-KHI Izz"''"" Jr c|j}|jr|Stj||j dS)a Ensure that the data type of the NumPy array `A` has native byte order. `A` must be a NumPy array. If the data type of `A` does not have native byte order, a copy of `A` with a native byte order is returned. Otherwise `A` is returned. nativer&)r#isnativerasarray newbyteorder)Adts r to_nativer>`s5 B {{ ::arx8 99r c0| |j}ntj|}|tvr.dj dtD}t d|d|d|S#t$r.}|tj|}n td|Yd}~jd}~wwxYw)aForm a supported numpy dtype based on input arguments. Returns a valid ``numpy.dtype`` from `dtype` if not None, or else ``a.dtype`` if possible, or else the given `default` if not None, or else raise a ``TypeError``. The resulting ``dtype`` must be in ``supported_dtypes``: bool_, int8, uint8, int16, uint16, int32, uint32, int64, uint64, longlong, ulonglong, float32, float64, longdouble, complex64, complex128, clongdouble Nzcould not interpret data type, c34K|]}|jywN)__name__).0rs r zgetdtype..s(N(Nsz$scipy.sparse does not support dtype z . The only supported types are: .)r#AttributeErrorrrrjoinr2)r#adefaultnewdtypeesupported_dtypes_fmts rr r ps } HwwH88E?''#yy(N=M(NN?zJ::N9OqRS S O H"88G, ?@aG Hs A B'$BBreturnc`tj|||}t|j|S)z This is a wrapper of `np.array(obj, dtype=dtype, copy=copy)` that will generate a warning if the result is an object array. )r#copy)rr'r r#)objr#rPdatas rr r s) 88Cu4 0D TZZ Kr c|sd}tjj|jdvr|jdkDrt d|t|j kDr+|jkDjrt d||jjd}|jjd}||fS|jdk(r_t|j kDr,tfd |jDrt d |tfd |jDS|jd k(r_t|j kDr+|jkDjrt d ||jjd}|S|jdk(r|j\}}|jd|zkDr t dt|j kDr.|j|zkDjrt d||jjd}|jjd}||fStd|jd)a6 Safely cast sparse array indices to `idx_dtype`. Check the shape of `A` to determine if it is safe to cast its index arrays to dtype `idx_dtype`. If any dimension in shape is larger than fits in the dtype, casting is unsafe so raise ``ValueError``. If safe, cast the index arrays to `idx_dtype` and return the result without changing the input `A`. The caller can assign results to `A` attributes if desired or use the recast index arrays directly. Unless downcasting is needed, the original index arrays are returned. You can test e.g. ``A.indptr is new_indptr`` to see if downcasting occurred. .. versionadded:: 1.15.0 Parameters ---------- A : sparse array or matrix The array for which index arrays should be downcast. idx_dtype : dtype Desired dtype. Should be an integer dtype (default: ``np.int32``). Most of scipy.sparse uses either int64 or int32. msg : string, optional A string to be added to the end of the ValueError message if the array shape is too big to fit in `idx_dtype`. The error message is ``f" values too large for {msg}"`` It should indicate why the downcasting is needed, e.g. "SuperLU", and defaults to f"dtype {idx_dtype}". Returns ------- idx_arrays : ndarray or tuple of ndarrays Based on ``A.format``, index arrays are returned after casting to `idx_dtype`. For CSC/CSR, returns ``(indices, indptr)``. For COO, returns ``coords``. For DIA, returns ``offsets``. For BSR, returns ``(indices, indptr)``. Raises ------ ValueError If the array has shape that would not fit in the new dtype, or if the sparse format does not use index arrays. Examples -------- >>> import numpy as np >>> from scipy import sparse >>> data = [3] >>> coords = (np.array([3]), np.array([1])) # Note: int64 arrays >>> A = sparse.coo_array((data, coords)) >>> A.coords[0].dtype dtype('int64') >>> # rescast after construction, raising exception if shape too big >>> coords = sparse.safely_cast_index_arrays(A, np.int32) >>> A.coords[0] is coords[0] # False if casting is needed False >>> A.coords = coords # set the index dtype of A >>> A.coords[0].dtype dtype('int32') zdtype csccsrzindptr values too large for zindices values too large for FrPcooc3DK|]}|kDjywrB)any)rDco max_values rrEz+safely_cast_index_arrays..s=bBN'')= zcoords values too large for c3DK|]}|jdyw)FrYN)r.)rDr] idx_dtypes rrEz+safely_cast_index_arrays..sI"RYYyuY5Ir_diazoffsets values too large for bsrz!indptr values too large for {msg}zFormat zP is not associated with index arrays. DOK and LIL have dict and list, not array.)rr1r/formatindptrr2shapeindicesr\r.coordstupleoffsets blocksizer) r<ramsgrgrerjRCr^s ` @rsafely_cast_index_arraysrosY| yk"#''Ixx>! 88B<) #;C5AB B =9 $ I%**, #@!FGG))""95"97 U  =9 $=AHH== #?u!EFFIIII U  =9 $ I%**, #@!FGG))""95"9 U {{1 88B>> import numpy as np >>> from scipy import sparse >>> # select index dtype based on shape >>> shape = (3, 3) >>> idx_dtype = sparse.get_index_dtype(maxval=max(shape)) >>> data = [1.1, 3.0, 1.5] >>> indices = np.array([0, 1, 0], dtype=idx_dtype) >>> indptr = np.array([0, 2, 3, 3], dtype=idx_dtype) >>> A = sparse.csr_array((data, indices, indptr), shape=shape) >>> A.indptr.dtype dtype('int32') >>> # select based on larger of existing arrays and shape >>> shape = (3, 3) >>> idx_dtype = sparse.get_index_dtype(A.indptr, maxval=max(shape)) >>> idx_dtype r)rintcr+int64int32r1r0r/ isinstancendarrayr:rr#r- issubdtypeinteger)arraysr4check_contentsint32minint32maxr3r5s rget_index_dtyper}s3R wwyQxxxx*../Hxx*../H &! H 88O&"**% jjo{{399bhh/88q=]]399bjj9 WWYF WWYF)f.@ 88O  88Or r#c|jdk(r4tj|tjrtjStj|tjrtjS|S)z Mimic numpy's casting for np.sumu)kindrruintint_r&s rrrPsH zzSR[[8ww {{5"''"ww Lr chtj|xst|xr|jdk(S)z8Is x either a scalar, an array scalar, or a 0-dim array?r)risscalarrndimxs rr r Ys& ;;q> 9gaj8QVVq[9r c tj|dk7ry tj|y#tt f$rC t t||k(}n#tt f$rYYywxYw|r d}t ||cYSwxYw)zsIs x appropriate as an index into a sparse matrix? Returns True if it can be cast safely to a machine int. rFz4Inexact indices into sparse matrices are not allowedT)rroperatorindexrr2boolint)r loose_intrls rr r ^s  wwqzQ q  z " SVq[)I:&   HCS/ !s31BABA,(B+A,,BB))allow_ndc`t|}||vry|D]}t|sy|s|dksyy)zIs x a valid tuple of dimensions? If nonneg, also checks that the dimensions are non-negative. Shapes of length in the tuple allow_nd are allowed. FrT)lenr )rnonnegrrds rrrtsB q6D 8 | a!e  r ct|ttzxr(t|dk(xst j |dxs+t|tj xr|jdk(S)Nr)rulistrirrrrvrrs rrrsW 4%< (/ Vq[ -BKK!- : 2:: & 8AFFaK;r ct|ttzxrt|dkDxrt |dxs+t|t j xr|jdk(S)Nrr)rurrirrrrvrrs rrrsV 4%< (- VaZ-&qt, 8 2:: & 6166Q;9r c6t|tjSrB)rurrvrs rrrs a $$r r)r.c>|y|dk(r tdt|tsZtjtj t |tjstdt ||f}g}|D]L}t|std|d|dkr||z }|dks||k\r td|j|Nt|}|tt|k7r td||kDr td ||k(ryt|S) NzWsparse does not accept 0D axis (). Either use toarray (for dense) or copy (for sparse).z+axis must be an integer/tuple of ints, not z axis must be an integer. (given )rzaxis out of range for ndimzduplicate value in axisz axis tuple has too many elements) r2rurirrwr#typerxrr appendrset)axisr canon_axisaxlen_axiss r validateaxisrs& | rz $  dE "}}RXXd4j12::>I$t*VW WwJ}>rd!DE E 6 $JB 6R4Z9: :":H3s:''233 D;<< T Z  r cpt|dk(r tdt|dk(r" t|d}td|D}ntd|D}|;t||vrt d|d|td |Drt d t|}t|Dcgc] \}}|dks |}}}|s!t|} | |k7rt d |d |t|dk(rf|d} t|d| || dzdz} t|| \} } | dk7r#td |D}t d |d ||d| | fz|| dzdz}n t dt||vrt d|d||S#t$rt j |df}YRwxYwcc}}w)a@Imitate numpy.matrix handling of shape arguments Parameters ---------- args : array_like Data structures providing information about the shape of the sparse array. current_shape : tuple, optional The current shape of the sparse array or matrix. If None (default), the current shape will be inferred from args. allow_nd : tuple of ints, optional default: (2,) If shape does not have a length in the tuple allow_nd an error is raised. Returns ------- new_shape: tuple The new shape after validation. rz8function missing 1 required positional argument: 'shape'rc3FK|]}tj|ywrBrrrDargs rrEzcheck_shape..sHchnnS1H!c3FK|]}tj|ywrBrrs rrEzcheck_shape..s>#(..->rNzshape must have length in z. Got new_shape=c3&K|] }|dk yw)rNr)rDrs rrEzcheck_shape..s(q1u(sz#'shape' elements cannot be negativezcannot reshape array of size z into shape c3.K|] }|dkrdn|yw)rnewshapeNr)rDrs rrEzcheck_shape..s!PA*1">>  y> )9(CT)VW W (i( (BC CM* +4I*>H$!Q!a%AHHIH<' #@#/ {"<== ! "a '#A&DYu- $q&'0BBCI%+L)%D "KA~!!Pi!PP  #@#/ {"<==!%4(K>9Id1fg QF Q&c"G w-C '> c#c(3 DAqAv!s1v+q6Q;$%TUUA   S7NRs#B c|jdd}|jdd}|r+tddj|j||fS)a?Unpack keyword arguments for reshape function. This is useful because keyword arguments after star arguments are not allowed in Python 2, but star keyword arguments are. This function unpacks 'order' and 'copy' from the star keyword arguments (with defaults) and throws an error for any remaining. orderrnrPFz-reshape() got unexpected keywords arguments: r@)poprrHkeys)kwargsrrPs rcheck_reshape_kwargsrs[ JJw $E ::fe $D G99V[[]3467 7 $;r czttjjddd}|duxr t ||S)zV Check whether object is pydata/sparse matrix, avoiding importing the module. sparse SparseArrayN)getattrsysmodulesrru)mbase_clss ris_pydata_spmatrixr.s6s{{x0-FH 4  ;Jq($;;r r target_formatrU accept_fvz sp.spmatrixct|rF |j|}||j|}|S|jdvr|j }|S#t$r|j}YPwxYw)z_ Convert a pydata/sparse array to scipy sparse matrix, pass through anything else. )rrU)rto_scipy_sparserasformatrdtocsc)rrrs rconvert_pydata_sparse_to_scipyr6s~# (%% %:C  $,,}-C JZZ~ -))+C J  (%%'C (sAA0/A0cftj|i|jtjSrB)rr'viewmatrix)rrs rrrSs% 88T $V $ ) )")) 44r ct|tjr||j|k(r|Stj||j tjS)Nr&)rurrr#r:r)rRr#s rasmatrixrWsD$ " u9L ::d% ( - -bii 88r c0t|tjjr|j St|tj r:t j|j|j|jSt|tjrWt j|j|j}tjj|j||S|j!j S)zAccess nonzero values, possibly after summing duplicates. Parameters ---------- s : sparse array Input sparse array. Returns ------- data: ndarray Nonzero values of the array, with shape (s.nnz,) )r#countr&)rusp_data _data_matrix _deduped_data dok_arrayrfromitervaluesr#nnz lil_arrayempty _csparsetoolslil_flatten_to_arrayrRtocoo)srRs r_todatar_s!RXX**+  !R\\"{{188:QWWAEEBB!R\\"xxQWW- --affd; 779 " " $$r )NN)NF)rNF)FrB)B__doc__rtypingrrrrnumpyrmathr scipy.sparserrscipy._lib._utilrr__all__bool_byteubyteshortushortrruintclonglong ulonglongfloat32float64 longdouble complex64 complex128 clongdoublerrr r$r)r6r>r rvr rtror}r#rrr r rrrrrirrrrrrrrrrrr rrs &&. HHbggrxx299bggHHgxbllJJ BMMLL"--A  DD 7( : < BJJ +-((kF\EPbhh:t: D,$$;T; 949 %$% !#!U38_t%;#!L=d=uS#X=@B"