
    (phQW                     @   % S r SSKrSSKJr  SSKJrJrJr  SSKr	SSK
Jr  SSKJr  SSKJrJrJrJrJrJrJrJrJr  / SQr\R@                  RC                  SS	5      r"\#\$-  \%S'   \R@                  RC                  S
S5      r&\"\&S.r'\r(\\%S'   \(\RR                  -  r)\\%S'   S r*S\(S\SS4S jr+S\(S\4S jr,   SJSS	S	S.S\)S\S\S   S-  S\$S-  S\S-  S\$S\$S\(4S jjjr-SS.S\(S\S-  S\(4S  jjr.S!S!S!S!S".S# jr/S$ r0S!S!S!S!S%SS&.S' jr1SSS!S!S!S!S%SS(.S) jr2S!S!S!S!S%S!SS*.S+ jr3SKS, jr4SLS- jr5S.\S\#4S/ jr6S\(S\S\$4S0 jr7S\S\8\#   \8S   -  4S1 jr9S\S\S-  4S2 jr:SS.S\(S3\;S\S-  S\(4S4 jjr<SS.S5\(S6\(S\S-  S\(4S7 jjr=SS.S\(S\S-  S\(4S8 jjr>SS	S9SS:.S\(S;\;\?\;   -  S-  S<\$S=\;\@-  S\S-  S\(4S> jjrASS.S\(S\S-  S\(4S? jjrBSS.S\(S\S-  S\(4S@ jjrCSASSB.SC\(SD\(S;\;S\S-  S\(4
SE jjrDS	S	SSF.SG jrESMSC\(S\S-  S\(4SH jjrFSI rGg)Na6  Utility functions to use Python Array API compatible libraries.

For the context about the Array API see:
https://data-apis.org/array-api/latest/purpose_and_scope.html

The SciPy use case of the Array API is described on the following page:
https://data-apis.org/array-api/latest/use_cases.html#use-case-scipy
    N)
ModuleType)AnyLiteral	TypeAlias)array_api_compat)	is_array_api_objsizenumpydeviceis_numpy_namespaceis_cupy_namespaceis_torch_namespaceis_jax_namespaceis_array_api_strict_namespace)_asarrayarray_namespaceassert_almost_equalassert_array_almost_equalget_xp_devicesis_array_api_strict
is_complexis_cupyis_jaxis_numpyis_torchSCIPY_ARRAY_APISCIPY_DEVICEscipy_namespace_forxp_assert_closexp_assert_equalxp_assert_lessxp_copyxp_copysign	xp_devicexp_moveaxis_to_endxp_ravelxp_realxp_signxp_sizexp_take_along_axisxp_unsupported_param_msgxp_vector_normr   Fr   cpu)r   r   Array	ArrayLikec                    [        [        U 5      5       GH  nX   nSSKJn  U" U5      (       a  Sn[	        U5      e[        U[        R                  R                  5      (       a  [        S5      e[        U[        R                  5      (       a  [        S5      e[        U[        R                  [        R                  -  5      (       av  UR                  n[        R                  " U[        R                  5      (       d=  [        R                  " U[        R                   5      (       d  [        SU< S35      eGM)  GM,  [#        U5      (       a  GM?   [        R$                  " U5      nUR                  n[        R                  " U[        R                  5      (       d<  [        R                  " U[        R                   5      (       d  S	U< S3n[        U5      eX U'   GM     U $ ! [         a    [        S5      ef = f)
ad  Raise exceptions on known-bad subclasses.

The following subclasses are not supported and raise and error:
- `numpy.ma.MaskedArray`
- `numpy.matrix`
- NumPy arrays which do not have a boolean or numerical dtype
- Any array-like which is neither array API compatible nor coercible by NumPy
- Any array-like which is coerced by NumPy to an unsupported dtype
r   )issparsezSparse arrays/matrices are not supported by this function. Perhaps one of the `scipy.sparse.linalg` functions would work instead.z8Inputs of type `numpy.ma.MaskedArray` are not supported.z0Inputs of type `numpy.matrix` are not supported.zAn argument has dtype `z3`; only boolean and numerical dtypes are supported.zCAn argument is neither array API compatible nor coercible by NumPy.z1An argument was coerced to an unsupported dtype `)rangelenscipy.sparser1   
ValueError
isinstancenpmaMaskedArray	TypeErrormatrixndarraygenericdtype
issubdtypenumberbool_r   
asanyarray)arraysiarrayr1   msgr>   messages          H/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/_array_api.py_compliance_scipyrI   9   s    3v;	)E??)C S/!eRUU..//VWWryy))NOOeRZZ"**455KKEMM%33r}}UBHH7U7U"9% CS !T U U 8V3 "%((7e, KKEMM%33r}}UBHH7U7UGy QG H   ((1IC  D M  7 !6 7 77s   G((G>rE   xpreturnc                     Sn UR                  UR                  U 5      5      (       d  [        U5      eg! [         a    [        U5      ef = f)zCheck for NaNs or Infs.z#array must not contain infs or NaNsN)allisfiniter5   r:   )rE   rJ   rF   s      rH   _check_finiterO   h   sL    
/Cvvbkk%())S/! * os	   05 ArC   c                      [         S   (       d  [        $ U  Vs/ s H	  oc  M  UPM     nn[        U5      n[        R                  " U6 $ s  snf )a  Get the array API compatible namespace for the arrays xs.

Parameters
----------
*arrays : sequence of array_like
    Arrays used to infer the common namespace.

Returns
-------
namespace : module
    Common namespace.

Notes
-----
Thin wrapper around `array_api_compat.array_namespace`.

1. Check for the global switch: SCIPY_ARRAY_API. This can also be accessed
   dynamically through ``_GLOBAL_CONFIG['SCIPY_ARRAY_API']``.
2. `_compliance_scipy` raise exceptions on known-bad subclasses. See
   its definition for more details.

When the global switch is False, it defaults to the `numpy` namespace.
In that case, there is no compliance check. This is a convenience to
ease the adoption. Otherwise, arrays must comply with the new rules.
r   )_GLOBAL_CONFIG	np_compatrI   r   r   )rC   rE   _arrayss      rH   r   r   r   sG    4 +,"(>&u&G>(G++W55	 ?s
   A	A	)rJ   check_finitesubokr>   order)KACFcopyrT   rU   c                   Uc  [        U 5      n[        U5      (       aN  USL a  [        R                  " XXS9n ODU(       a  [        R                  " XUS9n O'[        R
                  " XUS9n O UR                  XUS9n U(       a  [        X5        U $ ! [         a-    [        UR                  S5      5      nUR                  XUS9n  NJf = f)a8  SciPy-specific replacement for `np.asarray` with `order`, `check_finite`, and
`subok`.

Memory layout parameter `order` is not exposed in the Array API standard.
`order` is only enforced if the input array implementation
is NumPy based, otherwise `order` is just silently ignored.

`check_finite` is also not a keyword in the array API standard; included
here for convenience rather than that having to be a separate function
call inside SciPy functions.

`subok` is included to allow this function to preserve the behaviour of
`np.asanyarray` for NumPy based inputs.
T)rV   r>   rU   )rV   r>   )r>   r[      )r   r   r7   rE   rB   asarrayr:   rO   )rE   r>   rV   r[   rJ   rT   rU   
coerced_xps           rH   r   r      s    0 
zU#||4<HHUuJEMM%EBEJJu?E	FJJuJ=E
 e L  	F(A7J&&u&EE	Fs   .B 4C	C	rJ   xc                4    Uc  [        U 5      n[        U SUS9$ )z
Copies an array.

Parameters
----------
x : array

xp : array_namespace

Returns
-------
copy : array
    Copied array

Notes
-----
This copy function does not offer all the semantics of `np.copy`, i.e. the
`subok` and `order` keywords are not used.
T)r[   rJ   )r   r   ra   rJ   s     rH   r"   r"      s"    , 
zQADR((    Tcheck_namespacecheck_dtypecheck_shapecheck_0dc                   SnU(       a  [        X5        [        U5      (       a  U(       ay  S[        U 5       S[        U5       3nUR                  U 5      (       a  UR                  U5      (       d3  UR                  U 5      (       d  UR                  U5      (       a   U5       eUR	                  U 5      n UR	                  U5      nU(       a=  SU R
                   SUR
                   3nU R
                  UR
                  :X  d   U5       eU(       a=  SU R                   SUR                   3nU R                  UR                  :X  d   U5       eUR                  XR                  5      nX4$ )NTz$Array-ness does not match:
 Actual: z
 Desired: zdtypes do not match.
Actual: 

Desired: zShapes do not match.
Actual: )_assert_matching_namespacer   typeisscalarr^   r>   shapebroadcast_to)	actualdesiredrJ   rf   rg   rh   ri   __tracebackhide___msgs	            rH   _strict_checkru      s6    "63 ||7<.T']O=V$$W)=)=F++BKK4H4H	QLP	Q J ZZFjj!G/~[X||w}},2d2,/~[X||w}},2d2,oog||4G?rd   c                     Sn[        U [        5      (       a  U OU 4n [        U5      nU  H6  n[        U5      nSUR                   SUR                   3nXS:X  a  M1   U5       e   g )NTz!Namespaces do not match.
Actual: rk   )r6   tupler   __name__)rq   rr   rs   desired_spacearr	arr_spacert   s          rH   rl   rl      sw    !&%00VviF#G,M#C(	$--. /)2235 )/4/) rd    )rf   rg   rh   ri   err_msgrJ   c          
      >   SnUc  [        U 5      n[        XXrX4US9u  p[        U5      (       a  UR                  R	                  XUS9$ [        U5      (       a(  US:X  a  S OUnUR                  R                  XSSSSUS9$ [        R                  R	                  XUS9$ )NTre   )r}   r|   r   Frtolatol	equal_nanrg   rF   )r   ru   r   testingassert_array_equalr   assert_closer7   )	rq   rr   rf   rg   rh   ri   r}   rJ   rs   s	            rH   r    r      s    	zV$#OF r{{zz,,Vg,NN	" "R-$Wzz&&vQQRV38g ' G 	G ::(('(JJrd   )r   r   rf   rg   rh   ri   r}   rJ   c          
         Sn
U	c  [        U 5      n	[        XU	XEXgS9u  pU	R                  U R                  S5      nUc3  U(       a,  U	R	                  U R                  5      R
                  S-  S-  nOUc  Sn[        U	5      (       a  U	R                  R                  XUX8S9$ [        U	5      (       a'  US:X  a  S OUnU	R                  R                  XX#SS	US
9$ [        R                  R                  XUX8S9$ )NTre   )real floatingcomplex floating      ?   gHz>)r   r   r}   r|   Fr   )r   ru   isdtyper>   finfoepsr   r   assert_allcloser   r   r7   )rq   rr   r   r   rf   rg   rh   ri   r}   rJ   rs   floatings               rH   r   r   $  s    	zV$#'OF zz&,,(MNH| xx%))3.2	r{{zz))&/3 * F 	F	"!R-$Wzz&&vT155g ' W 	W ::%%fD+/ & B Brd   )rf   rg   rh   ri   r}   verboserJ   c          
         Sn	Uc  [        U 5      n[        XXX4US9u  p[        U5      (       a  UR                  R	                  XXgS9$ [        U5      (       aT  U R                  R                  S:w  a  U R                  5       n UR                  R                  S:w  a  UR                  5       n[        R                  R	                  XXgS9$ )NTre   )r}   r   r-   )
r   ru   r   r   assert_array_lessr   r   rm   r-   r7   )
rq   rr   rf   rg   rh   ri   r}   r   rJ   rs   s
             rH   r!   r!   F  s    	zV$#OF r{{zz++F4; , N 	N	"==&ZZ\F>>%'kkmG::''07 ( J Jrd   c                 <    SSSU* -  -  pe[        X/UQ7XeSSS.UD6$ zPBackwards compatible replacement. In new code, use xp_assert_close instead.
    r   g      ?
   F)r   r   rg   rh   r   rq   rr   decimalargskwdsr   r   s          rH   r   r   _  B     CgX&$6 * * $UPU*$(* *rd   c                 <    SSSU* -  -  pe[        X/UQ7XeSSS.UD6$ r   r   r   s          rH   r   r   h  r   rd   paramc                     SU < S3$ )Nz
Providing z$ is only supported for numpy arrays. )r   s    rH   r+   r+   q  s    y DEErd   c                 :    UR                  U R                  S5      $ Nr   )r   r>   rc   s     rH   r   r   u  s    ::agg122rd   c                    / n[        U 5      (       aq  US/-  nSSKnUR                  R                  5       n[	        SU5       H  nUSU 3/-  nM     UR
                  R                  R                  5       (       a  US/-  nU$ [        U 5      (       aF  SSK	nUR                  R                  R                  5       n[	        SU5       H  nUSU 3/-  nM     U$ [        U 5      (       a  SSKnUR                  SS9n[	        SU5       H  nUSU 3/-  nM     UR                  SS9n[	        SU5       H  nUS	U 3/-  nM     UR                  S
S9n	[	        SU	5       H  nUSU 3/-  nM     U$ S/$ )z<Returns a list of available devices for the given namespace.r-   r   Nzcuda:mps)backendzcpu:gpuzgpu:tpuztpu:)r   torchcudadevice_countr2   backendsr   is_availabler   cupyruntimegetDeviceCountr   jax)
rJ   devicesr   num_cudarD   r   r   num_cpunum_gpunum_tpus
             rH   r   r   y  s   G||E7::**,q(#A%s}$G $>>**,,wG	99$$335q(#A%s}$G $	""5"1q'"A$qc
|#G #""5"1q'"A$qc
|#G #""5"1q'"A$qc
|#G # 6Mrd   c                     [        U 5      (       a  SSKnUR                  $ [        U 5      (       a  SSKnUR                  $ [        U 5      (       a  U $ g)a   Return the `scipy`-like namespace of a non-NumPy backend

That is, return the namespace corresponding with backend `xp` that contains
`scipy` sub-namespaces like `linalg` and `special`. If no such namespace
exists, return ``None``. Useful for dispatching.
r   N)r   cupyxscipyr   r   r   )rJ   r   r   s      rH   r   r     sA     r{{{{bzzyy||	rd   sourcec                   Uc  [        U5      OUn[        [        U R                  5      5      nUR	                  U5      nX4/-   nUR                  X5      $ N)r   listr2   ndimpoppermute_dims)ra   r   rJ   axestemps        rH   r%   r%     sL    
 !#
	BaffD88FD&=D??1##rd   x1x2c               p    Uc  [        X5      OUnUR                  U 5      nUR                  US:  X3* 5      $ )Nr   )r   abswhere)r   r   rJ   abs_x1s       rH   r#   r#     s6    $&J	 BBVVBZF88B!GVW--rd   c               t   Uc  [        U 5      OUn[        U5      (       a  UR                  U 5      $ UR                  U 5      nUR	                  SU R
                  S9nUR                  U S:  X25      nUR                  U S:  U* U5      nUR                  UR                  U 5      UR                  U-  U5      nU$ )N   )r>   r   )	r   r   sign
zeros_liker^   r>   r   isnannan)ra   rJ   r   ones       rH   r(   r(     s    !z	rB||wwqz==D
**Qagg*
&C88AE3%D88AEC4&D88BHHQKT2DKrd      )axiskeepdimsordrJ   r   r   r   c               6   Uc  [        U 5      OUn[        (       aa  [        US5      (       a  UR                  R	                  XX#S9$ US:w  a  [        S5      eUR                  UR                  U 5      U -  XS9S-  $ [        R                  R                  XXS9$ )Nlinalg)r   r   r   r   zonly the Euclidean norm (`ord=2`) is currently supported in `xp_vector_norm` for backends not implementing the `linalg` extension.)r   r   r   )r   r   r   )
r   r   hasattrr   vector_normr5   sumconjr7   norm)ra   r   r   r   rJ   s        rH   r,   r,     s    
  "z	rB2x  99(((RRax !  66"''!*q.t6GLL yy~~at~GGrd   c               F    Uc  [        U 5      OUnUR                  U S5      $ )N))r   reshaperc   s     rH   r&   r&     s%      "z	rB::ard   c                   Uc  [        U 5      OUnUR                  U R                  S5      (       a  UR                  U 5      $ U $ r   )r   r   r>   realrc   s     rH   r'   r'     s<      "z	rBAGG-?@@2771:GaGrd   r   )r   rJ   rz   indicesc                   Uc  [        U 5      OUn[        U5      (       a  UR                  XUS9$ [        U5      (       a  [	        S5      eUR                  XU5      $ )N)dimz2Array API standard does not define take_along_axis)r   r   take_along_dimr   NotImplementedErrortake_along_axis)rz   r   r   rJ   s       rH   r*   r*     s]     "$	B||  4 88	R	 	 !"VWW!!#55rd   )ensure_writeableforce_floatingrJ   c                    Uc  [        U6 OUnU Vs/ s H  oDb
  [        USS9OUPM     nnU Vs/ s H	  oDc  M  UPM     nnUR                  S5      R                  nU Vs/ s H  oDR                  PM     nn UR                  " U6 nU(       a(  UR                  US5      (       a  UR	                  X5      nU Vs1 s H  oDR                  iM     n
n [        U
5      S:w  a  [        R                  " U
6 OUS   R                  n/ nU H  nUc  UR                  U5        M  UR                  U:w  a)  [        U5      (       a  SS0O0 nUR                  " XK40 UD6nUR                  U:w  d  U (       a  UR                  XHSS	9nUR                  U5        M     U$ s  snf s  snf s  snf ! [         an    U Vs/ s H  nUR                  US5      (       a  M  UPM      Os  snf n	nU	(       a  UR                  " / U	QUP76 n GNIU(       a  Un GNUUR                  " U6 n GNgf = fs  snf ! [         a  nSn[        U5      UeS nAff = f)
NT)rU   g      ?integralr   r   z/Array shapes are incompatible for broadcasting.rU   )r[   )r   r   r^   r>   result_typer   r:   ro   r3   r7   broadcast_shapesr5   appendr   rp   astype)r   r   rJ   r   argargs_not_nonedefault_floatdtypesr>   float_dtypesshapesro   erG   outkwargss                   rH   xp_broadcast_promoter     s$   #%:$	2BOSTt/Xc&sBtDT$(<DSSDM< JJrN((M#01=Cii=F1,'bjj
;; NN58E $11=Cii=F1)14V1A$$f-#A&,, 	 C;JJsO
 99(0gt_"F//#77C II#3))CT)2C

3 " Jc U< 2  ,+1 >6%!zz%< 6 > >NN@L@-@E!ENNF+E, 2  )C!q()sc   FFF!F"9>F' ;H"1H' 'H5GG#H 	HHH'
I1H??Ic                    Uc  [        U 5      OUnU R                  nUR                  X!R                  5      (       a  UR	                  XR
                  5      n U $ UR                  US5      (       a  UR	                  XR                  5      n U $ )Nr   )r   r>   r   float32r   	complex64
complex128)rz   rJ   	arr_dtypes      rH   xp_float_to_complexr   H  ss    !#	B		I 
zz)ZZ((ii\\* J 
I	/	/ii]]+Jrd   c                 Z    [        U 5      (       a  U R                  5       $ U R                  $ )z@Query the namespace-dependent default floating-point dtype.
    )r   get_default_dtypefloat64r`   s    rH   xp_default_dtyper   V  s(     ||##%% zzrd   )NNN)   )   r   )H__doc__ostypesr   typingr   r   r   r
   r7   numpy.typingnpt
scipy._libr   scipy._lib.array_api_compatr   r	   r)   rR   r   r$   r   r   r   r   r   r   r   r   r   r   __all__environgetr   strbool__annotations__r   rQ   r.   r/   rI   rO   r   r   r"   ru   rl   r    r   r!   r   r   r+   r   r   r   r   intr%   r#   r(   rw   floatr,   r&   r'   r*   r   r   r   r   rd   rH   <module>r     s   
  * *   '
 
 
	 !jjnn->Ft Fzz~~ne4 '  y s}},	9 ,,^ J 4 "6U "6z "6N 48 	, !%",,, )*T1, Tk	, , , , 
,^ 26 )u )Z$. )% ): #'Dd<	0 9=$ $tRDK0 .24 $$4BD 8<#dBQUJ2**FC FC F3% 3Z 3D 3!z !d3i$t*&< !HJ :+< 8 !%		$	$	$ 		$ */		$ EI .E .u .
T0A .U . 59 	u 	
T 1 	U 	 48$)&'+/	He HuSz)D0H!H eH "D(	H 5:	H4 6:    d!2  e   59 Hu H
T 1 HU H $&/36E 6 %6 6 &,6 9>6  27uQU 4nU 
T(9 U rd   