+
    nDjO                        R t ^ RIt^ RIHtHt ^ RIt^ RIt^ RIH	t	 ^ RI
Ht . R.Ot]P                  ]P                  ]P                   ]P"                  ]P$                  ]P&                  ]P(                  ]P*                  ]P,                  ]P.                  ]P0                  ]P2                  ]P4                  ]P6                  ]P8                  ]P:                  ]P<                  .t/ t R t!R t"R t#R t$R t%R/R	 lt&R0R
 R llt']PP                  R3R lt)R1R lt*R R lt+R R lt,R R lt-R2RR3RR/R R lllt.R R lt/R R lt0R R lt1R ^/R! R" llt2R4RR3/R# R$ lllt3R% t4R& R' lt5R/R( R) llt6R* t7R4R+ lt8R, R- lt9R# )5z+Utility functions for sparse matrix module
NAnyLiteral)prodc                    \         P                  \        V 4      4      pVe   V# \        P                  ! V !  p\
         F5  p\        P                  ! W!4      '       g   K!  V\         \        V 4      &   Vu # 	  \        RV : 24      h)a  Returns 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')
<class 'numpy.int32'>
>>> upcast('bool')
<class 'numpy.bool'>
>>> upcast('int32','float32')
<class 'numpy.float64'>
>>> upcast('bool',complex,float)
<class 'numpy.complex128'>

z#no supported conversion for types: )_upcast_memogethashnpresult_typesupported_dtypescan_cast	TypeError)argstupcasts   *  O/data/cameron/venvs/s3viz/lib/python3.14/site-packages/scipy/sparse/_sputils.pyr   r      sq    ( 	d$A}^^T"F;;v!!'(Ld$H 
 9$B
CC    c                     \         P                  V 4      pVe   V# \        \        \        P
                  V 4      !  pV\         V &   V# )z9Same as `upcast` but taking dtype.char as input (faster).)r   r   r   mapr
   dtype)r   r   s   * r   upcast_charr   :   s@    A}BHHd#$ALHr   c                V    \         P                  ! ^ .V R7      V,          P                  # )zXDetermine data type for binary operation between an array of
type `dtype` and a scalar.
r   )r
   arrayr   )r   scalars   &&r   upcast_scalarr   D   s"     HHaS&/666r   c                T   V P                   P                  \        P                   ! \        P                  4      P                  8  d   V P                  ^ 8X  d    V P                  \        P                  4      # V P                  4       pV P                  4       pV\        P                  ! \        P                  4      P                  8  g4   V\        P                  ! \        P                  4      P                  8  d   \        R4      hV P                  \        P                  4      # V # )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.
zzCannot deal with arrays with indices larger than the machine maximum address size (e.g. 64-bit indices on 32-bit machine).)
r   itemsizer
   intpsizeastypemaxminiinfo
ValueError)arrmaxvalminvals   &  r   downcast_intp_indexr)   K   s     yyBHHRWW-66688q=::bgg&&BHHRWW%)))Vbhhrww6G6K6K-K H I I zz"''""Jr   c                    V P                   pVP                  '       d   V # \        P                  ! WP	                  R4      R7      # )z
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   isnativer
   asarraynewbyteorder)Adts   & r   	to_nativer1   _   s5     
B	{{{ ::ax899r   c                D   V f    VP                   pM\        P                   ! V 4      pV\        9  d/   RP                  R \         4       4      p\        RV RV R24      hV#   \         d1   pTe   \        P                   ! T4      p Rp?Le\        R4      ThRp?ii ; i)a  Form 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
Nzcould not interpret data typez, c              3   8   "   T F  qP                   x  K  	  R # 5iN)__name__).0r   s   & r   	<genexpr>getdtype.<locals>.<genexpr>   s     (N=M=Ms   z$scipy.sparse does not support dtype z . The only supported types are: .)r   AttributeErrorr
   r   r   joinr%   )r   adefaultnewdtypeesupported_dtypes_fmts   &&&   r   getdtyperA   o   s     }	HwwH 88E?''#yy(N=M(NN?z J::N9OqR S 	SO  	H"88G, ?@aG		Hs   A$ $B/BBBc                8    V ^8  d   QhR\         P                  /#    returnr
   ndarray)formats   "r   __annotate__rI      s     	 	BJJ 	r   c                `    \         P                  ! WVR7      p\        VP                  4       V# )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)r
   r   rA   r   )objr   rK   datas   &&& r   getdatarN      s'    
 88C40D TZZKr    c                L  aa V'       g   RS 2p\         P                  ! S4      P                  oV P                  R9   d   V P                  R,          S8  d   \        RV 24      h\        V P                  !  S8  d2   V P                  S8  P                  4       '       d   \        RV 24      hV P                  P                  SRR7      pV P                  P                  SRR7      pW43# V P                  R8X  d   \        V P                  !  S8  dc   \        ;QJ d)    V3R lV P                   4       F  '       g   K   RM	  RM! V3R lV P                   4       4      '       d   \        R	V 24      h\        ;QJ d!    . V3R
 lV P                   4       F  NK  	  5# ! V3R
 lV P                   4       4      # V P                  R8X  di   \        V P                  !  S8  d2   V P                  S8  P                  4       '       d   \        RV 24      hV P                  P                  SRR7      pV# V P                  R8X  d   V P                  w  rgV P                  R,          V,          S8  d   \        R4      h\        V P                  !  S8  d9   V P                  V,          S8  P                  4       '       d   \        RV 24      hV P                  P                  SRR7      pV P                  P                  SRR7      pW43# \        RV P                   R24      h)ac  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 : str, 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"<index> 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')
zdtype zindptr values too large for zindices values too large for FrK   cooc              3   H   <"   T F  qS8  P                  4       x  K  	  R # 5ir4   )any)r6   co	max_values   & r   r7   +safely_cast_index_arrays.<locals>.<genexpr>   s     =HbN''))H   "Tzcoords values too large for c              3   H   <"   T F  qP                  SR R7      x  K  	  R# 5i)FrQ   N)r!   )r6   rU   	idx_dtypes   & r   r7   rW      s     I"YYyuY55rX   diazoffsets values too large for bsrz!indptr values too large for {msg}zFormat zP is not associated with index arrays. DOK and LIL have dict and list, not array.csccsr)r
   r$   r"   rH   indptrr%   shapeindicesrT   r!   coordstupleoffsets	blocksizer   )	r/   rZ   msgrc   ra   rf   RCrV   s	   &f&     @r   safely_cast_index_arraysrk      s   | yk"#''Ixx>!88B<)#;C5ABB =9$		I%**,, #@!FGG))""95"97	
U	=9$s=AHH=sss=AHH=== #?u!EFFuIIuIuIIII	
U	=9$		I%**,, #@!FGG))""95"9	
U	{{88B<!i'@AA=9$		A	)..00 #@!FGG))""95"97 '!(( ,E E F 	Fr   c                    \         P                  ! 4       P                  ^8w  d   \         P                  # \         P                  ! \         P
                  ! \         P                  4      P                  4      p\         P                  ! \         P
                  ! \         P                  4      P                  4      pVe-   \         P                  ! V4      pW8  d   \         P                  # \        V \         P                  4      '       d   V 3p V  F  p\         P                  ! V4      p\         P                  ! VP                  \         P                  4      '       d   KP  V'       dw   VP                  ^ 8X  d   Kk  \         P                  ! VP                  \         P                  4      '       d/   VP                  4       pVP                  4       pWc8  d	   W8:  d   K  \         P                  u # 	  \         P                  # )a;  
Based on input (integer) arrays `a`, determine a suitable index data
type that can hold the data in the arrays.

Parameters
----------
arrays : tuple of array_like
    Input arrays whose types/contents to check
maxval : float, optional
    Maximum value needed
check_contents : bool, optional
    Whether to check the values in the arrays and not just their types.
    Default: False (check only the types)

Returns
-------
dtype : dtype
    Suitable index data type (int32 or int64)

Examples
--------
>>> 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
<class 'numpy.int32'>
)r
   intcr   int64int32r$   r#   r"   
isinstancerG   r-   r   r   r    
issubdtypeinteger)arraysr'   check_contentsint32minint32maxr&   r(   s   &&&    r   get_index_dtyperw     s1   R 
wwyQxxxx*../Hxx*../H&!88O&"**%%jjo{{399bhh//88q=]]399bjj99 WWYF WWYF)f.@ 88O  88Or   c                    V ^8  d   QhR\         P                  R\         P                  \        \         P                  ,          ,          /# )rD   r   rE   )r
   r   typegeneric)rH   s   "r   rI   rI   O  s0       bhhbjj1A&A r   c                   V P                   R8X  d<   \        P                  ! V \        P                  4      '       d   \        P                  # \        P                  ! V \        P                  4      '       d   \        P                  # V # )z Mimic numpy's casting for np.sumu)kindr
   r   uintint_r   s   &r   get_sum_dtyper   O  sN    zzSR[[88ww	{{5"''""wwLr   c                $    V ^8  d   QhR\         /# rC   bool)rH   s   "r   rI   rI   X  s     : :t :r   c                    \         P                  ! V 4      ;'       g#    \        V 4      ;'       d    V P                  ^ 8H  # )z8Is x either a scalar, an array scalar, or a 0-dim array?)r
   isscalarisdensendimxs   &r   isscalarliker   X  s,    ;;q>99gaj88QVVq[9r   c                $    V ^8  d   QhR\         /# rC   r   )rH   s   "r   rI   rI   ]  s      D r   c                .   \         P                  ! V 4      ^ 8w  d   R#  \        P                  ! V 4       R#   \        \
        3 dM     \        \        T 4      T 8H  4      pM  \        \
        3 d      R# i ; iT'       d   Rp\        T4      hTu # i ; i)zkIs x appropriate as an index into a sparse matrix? Returns True
if it can be cast safely to a machine int.
Fz4Inexact indices into sparse matrices are not allowedT)r
   r   operatorindexr   r%   r   int)r   	loose_intrh   s   &  r   	isintliker   ]  s     
wwqzQ
q  z" 	SVq[)I:& 		HCS/!s9   7 B	A! B!A72B6A77
BBBallow_ndcheck_ndTc                $    V ^8  d   QhR\         /# rC   r   )rH   s   "r   rI   rI   s  s       r   c                   \        V 4      pV'       d	   WB9  d   R# V  F+  p\        V4      '       g    R# V'       g   K!  V^ 8  g   K*   R# 	  R# )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.
FT)lenr   )r   nonnegr   r   r   ds   &&$$  r   isshaper   s  sA     q6DD(||6a!e	 
 r   c                $    V ^8  d   QhR\         /# rC   r   )rH   s   "r   rI   rI     s     ; ;T ;r   c                     \        V \        \        ,          4      ;'       d<    \        V 4      ^ 8H  ;'       gW    \        P
                  ! V ^ ,          4      ;'       g2    \        V \        P                  4      ;'       d    V P                  ^8H  #     )rp   listre   r   r
   r   rG   r   r   s   &r   
issequencer     si    4%<( / /Vq[:-BKK!-: :2::&88AFFaK;r   c                $    V ^8  d   QhR\         /# rC   r   )rH   s   "r   rI   rI     s     9 94 9r   c                 
   \        V \        \        ,          4      ;'       d1    \        V 4      ^ 8  ;'       d    \	        V ^ ,          4      ;'       g2    \        V \
        P                  4      ;'       d    V P                  ^8H  # r   )rp   r   re   r   r   r
   rG   r   r   s   &r   ismatrixr     sh    4%<( - -VaZ- -&qt,8 82::&66166Q;9r   c                $    V ^8  d   QhR\         /# rC   r   )rH   s   "r   rI   rI     s     % %$ %r   c                 6    \        V \        P                  4      # r4   )rp   r
   rG   r   s   &r   r   r     s    a$$r   r   c                L    V ^8  d   QhR\         \        R3,          R,          /# )rD   rE   .Nre   r   )rH   s   "r   rI   rI     s      #! #!U38_t%; #!r   c                   V f   R # V R8X  d   \        R4      h\        V \        4      '       gc   \        P                  ! \        P
                  ! \        V 4      4      \        P                  4      '       g   \        R\        V 4       24      hV 3p . pV  F[  p\        V4      '       g   \        RV R24      hV^ 8  d	   W1,          pV^ 8  g   W18  d   \        R4      hVP                  V4       K]  	  \        V4      pV\        \        V4      4      8w  d   \        R4      hWA8  d   \        R4      hWA8X  d   R # \        V4      # )	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 )zaxis out of range for ndimzduplicate value in axisz axis tuple has too many elements )r%   rp   re   r
   rq   r   ry   rr   r   r   appendr   set)axisr   
canon_axisaxlen_axiss   &$   r   validateaxisr     s!   |rz$
 	

 dE"" }}RXXd4j12::>>I$t*VWWwJ}}>rd!DEE6JB6RZ9::"  :H3s:''233	;<<		Z  r   c                >    V ^8  d   QhR\         \        R3,          /# )rD   rE   .r   )rH   s   "r   rI   rI     s     = =uS#X =r   c                  \        V 4      ^ 8X  d   \        R4      h\        V 4      ^8X  dA    \        V ^ ,          4      p\        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      pM,\        ;QJ d    . R V  4       F  NK  	  5M! R V  4       4      pVfj   \        V4      V9  d   \        RV RV: 24      h\        ;QJ d    R V 4       F  '       g   K   RM	  R	M! R V 4       4      '       d   \        R
4      hEM\        V4      p\        V4       UUu. uF  w  rgV^ 8  g   K  VNK  	  pppV'       g$   \        V4      p	W8w  d   \        RV RV 24      hM\        V4      ^8X  d   V^ ,          p
\        VRV
 WJ^,           R ,           4      p\        W[4      w  rV^ 8w  d>   \        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      p\        RV RV 24      hVRV
 V3,           WJ^,           R ,           pM\        R4      h\        V4      V9  d   \        RV RV: 24      hV#   \         d#    \        P
                  ! T ^ ,          4      3p ELi ; iu uppi )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.
z8function missing 1 required positional argument: 'shape'c              3   N   "   T F  p\         P                  ! V4      x  K  	  R # 5ir4   r   r   r6   args   & r   r7   check_shape.<locals>.<genexpr>  s     HZchnnS11Z   #%c              3   N   "   T F  p\         P                  ! V4      x  K  	  R # 5ir4   r   r   s   & r   r7   r     s     >#(..--r   Nzshape must have length in z. Got new_shape=c              3   *   "   T F	  q^ 8  x  K  	  R# 5i)r   Nr   )r6   r   s   & r   r7   r     s     (i1uis   TFz#'shape' elements cannot be negativezcannot reshape array of size z into shape c              3   6   "   T F  q^ 8  d   RMTx  K  	  R# 5i)r   newshapeNr   )r6   r   s   & r   r7   r     s     !PiA*1"<is   z&can only specify one unknown dimension)r   r   iterre   r   r   r%   rT   r   	enumeratedivmod)r   current_shaper   
shape_iter	new_shapecurrent_sizeir   negative_indexesnew_sizeskip	specifiedunspecified	remainder	err_shapes   &&$            r   check_shaper     sP   $ 4yA~RSS
4yA~	Id1gJ HZHHZHHIE>>EE>>>	y>)9(CT)VWW3(i(333(i(((BCC ) M* +4I*>H*>$!!a%AA*>HIH' #@#/	{"< = = ( !"a'#A&DYu-	q&'0BBCI%+L%D"KA~!E!Pi!PEE!Pi!PP	 #@#/	{"< = =!%4(K>9I1fg<NNIEFF
9~X%5hZ?Pi\RSSM  	4!Q03I	4" Is   H7 I'.I'7)I$#I$c                    V '       g   R# V  Uu. uF)  p\        V\        \        ,          4      '       d   TMV3NK+  	  p p\        V \        R7      p\        V4      pV  F^  pWJ d   K
  \        V\	        V4      ) R7       F9  w  rEV^8w  g   K  WSV,          8w  g   K  W4,          ^8w  d   \        R4      hWSV&   K;  	  K`  	  . VO5# u upi )a  Check if shapes can be broadcast and return resulting shape

This is similar to the NumPy ``broadcast_shapes`` function but
does not check memory consequences of the resulting dense matrix.

Parameters
----------
*shapes : tuple of shape tuples
    The tuple of shapes to be considered for broadcasting.
    Shapes should be tuples of non-negative integers.

Returns
-------
new_shape : tuple of integers
    The shape that results from broadcasting th input shapes.
)key)startz-shapes cannot be broadcast to a single shape.r   )rp   re   r   r"   r   r   r%   )shapesshpbig_shpoutr   r   s   *     r   broadcast_shapesr     s    " 	JPQ&3ZUT\22c>&FQ&c"G
w-C>c#c(3DAAv!1v+6Q;$%TUUA	 4  S7N Rs   /Cc                $    V ^8  d   QhR\         /# rC   r   )rH   s   "r   rI   rI     s     < <T <r   c                    \        \        P                  P                  R4      RR4      pVRJ;'       d    \	        W4      # )zN
Check whether object is pydata/sparse matrix, avoiding importing the module.
sparseSparseArrayN)getattrsysmodulesr   rp   )mbase_clss   & r   is_pydata_spmatrixr     s7     s{{x0-FH4;;Jq$;;r   c                \    V ^8  d   QhR\         RR\        R,          ,          R\         RR/# )rD   r   target_formatN	accept_fvrE   zsp.spmatrix | Anyr]   r   )rH   s   "r   rI   rI   $  s:      	',//  	r   c                    \        V 4      '       dL    V P                  VR7      p Ve   V P                  V4      p V # V P                  R9  d   V P                  4       p V #   \         d    T P                  4       p  LWi ; i)zS
Convert a pydata/sparse array to scipy sparse matrix,
pass through anything else.
)r   r]   )r   to_scipy_sparser   asformatrH   tocsc)r   r   r   s   &&&r   convert_pydata_sparse_to_scipyr   $  s     #	(%%	%:C $,,}-C J ZZ~-))+CJ  	(%%'C	(s   A A=<A=c                  h    \         P                  ! V / VB P                  \         P                  4      # r4   )r
   r   viewmatrix)r   kwargss   *,r   r   r   A  s%    88T$V$))"))44r   c                     \        V \        P                  4      '       d   Ve   V P                  V8X  d   V # \        P                  ! WR7      P                  \        P                  4      # )Nr   )rp   r
   r   r   r-   r   )rM   r   s   &&r   asmatrixr   E  sE    $		""u9L::d(--bii88r   c                8    V ^8  d   QhR\         P                  /# rC   rF   )rH   s   "r   rI   rI   M  s     % %"** %r   c                X   \        V \        P                  P                  4      '       d   V P	                  4       # \        V \        P
                  4      '       d<   \        P                  ! V P                  4       V P                  V P                  R7      # \        V \        P                  4      '       dY   \        P                  ! V P                  V P                  R7      p\        P                  P                  V P                  V4       V# V P!                  4       P	                  4       # )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   )rp   sp_data_data_matrix_deduped_data	dok_arrayr
   fromitervaluesr   nnz	lil_arrayempty_csparsetoolslil_flatten_to_arrayrM   tocoo)srM   s   & r   _todatar   M  s     !RXX**++  !R\\""{{188:QWWAEEBB!R\\""xxQWW-
--affd;779""$$r   )r   rA   rN   r   r   r   r   r   r   r   r   )NN)NF)r   NF)F)rD   r4   ):__doc__r   typingr   r   r   numpyr
   mathr   scipy.sparser   r   __all__bool_byteubyteshortushortrm   uintclongulonglonglong	ulonglongfloat32float64
longdouble	complex64
complex128clongdoubler   r   r   r   r   r)   r1   rA   rN   ro   rk   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r     sT         HHbggrxx299bggHHbggrxxbllJJ

BMMLL"--A 
 DD7(: <	 +-(( kF\EP:
,   $;9%#!q #!L=d = =@B<:59%r   