
    e!hQW                        U d Z ddlZddlmZ ddlmZmZmZ ddlZ	ddl
mZ ddlmZ ddlmZmZmZmZmZmZmZmZmZ g dZej@                  jC                  dd	      Z"e#e$z  e%d<   ej@                  jC                  d
d      Z&e"e&dZ'eZ(ee%d<   e(ejR                  z  Z)ee%d<   d Z*de(deddfdZ+de(defdZ,	 	 	 dJdd	d	dde)deded   dz  de$dz  dedz  de$de$de(fdZ-ddde(dedz  de(fd Z.d!d!d!d!d"d#Z/d$ Z0d!d!d!d!d%dd&d'Z1ddd!d!d!d!d%dd(d)Z2d!d!d!d!d%d!dd*d+Z3dKd,Z4dLd-Z5d.ede#fd/Z6de(dede$fd0Z7dede8e#   e8d   z  fd1Z9dededz  fd2Z:ddde(d3e;dedz  de(fd4Z<ddd5e(d6e(dedz  de(fd7Z=ddde(dedz  de(fd8Z>dd	d9dd:de(d;e;e?e;   z  dz  d<e$d=e;e@z  dedz  de(fd>ZAddde(dedz  de(fd?ZBddde(dedz  de(fd@ZCdAddBdCe(dDe(d;e;dedz  de(f
dEZDd	d	ddFdGZEdMdCe(dedz  de(fdHZFdI ZGy)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                    t        t        |             D ]  }| |   }ddlm}  ||      rd}t	        |      t        |t        j                  j                        rt        d      t        |t        j                        rt        d      t        |t        j                  t        j                  z        re|j                  }t        j                  |t        j                        rt        j                  |t        j                         rt        d|d      t#        |      r	 t        j$                  |      }|j                  }t        j                  |t        j                        s5t        j                  |t        j                         sd	|d}t        |      || |<    | S # t        $ r t        d      w xY w)
a  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          ]/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/scipy/_lib/_array_api.py_compliance_scipyrI   9   s    3v; !q	)E?)C S/!eRUU../VWWryy)NOOeRZZ"**45KKEMM%3r}}UBHH7U"9% CS !T U U!%(7e, KKEMM%3r}}UBHH7UGy QG H   ((F1IC!D M  7 !6 7 77s   +F//GrE   xpreturnc                     d}	 |j                  |j                  |             st        |      y# t        $ r t        |      w xY w)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   sI    
/Cvvbkk%()S/! * os	   +0 ArC   c                      t         d   st        S | D cg c]  }||	 }}t        |      }t        j                  | S c c}w )a1  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   sK    4 +,"(>E,=u>G>(G++W55	 ?s   ??)rJ   check_finitesubokr>   order)KACFcopyrT   rU   c                   |t        |       }t        |      rR|du rt        j                  | |||      } nI|rt        j                  | ||      } n.t        j
                  | ||      } n	 |j                  | ||      } |rt        | |       | S # t        $ r1 t        |j                  d            }|j                  | ||      } Y Iw xY w)a`  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<HHU%uEJEMM%uEBEJJuE?E	FJJuEJ=E
 eR L  	F(A7J&&uE&EE	Fs   ,B 7C
	C
rJ   xc                8    |t        |       }t        | d|      S )a3  
    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                   d}|rt        | |       t        |      rg|redt        |        dt        |       }|j                  |       r|j                  |      s)|j                  |       s|j                  |      rJ |       |j	                  |       } |j	                  |      }|r<d| j
                   d|j
                   }| j
                  |j
                  k(  sJ |       |r<d| j                   d|j                   }| j                  |j                  k(  sJ |       |j                  || j                        }| |fS )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      s.    "673 |7<.T']O=V$W)=F+BKK4H	QLP	Q J ZZFjj!G/~[X||w}},2d2,/~[X||w}},2d2,oogv||4G7?rd   c                     d}t        | t              r| n| f} t        |      }| D ]5  }t        |      }d|j                   d|j                   }||k(  r0J |        y )NTz!Namespaces do not match.
Actual: rk   )r6   tupler   __name__)rq   rr   rs   desired_spacearr	arr_spacert   s          rH   rl   rl      s{    !&%0VviF#G,M 0#C(	$--. /)2235 M)/4/)0rd    )rf   rg   rh   ri   err_msgrJ   c          	      L   d}|t        |       }t        | ||||||      \  } }t        |      r|j                  j	                  | ||      S t        |      r+|dk(  rd n|}|j                  j                  | |dddd|      S t        j                  j	                  | ||      S )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$#_[OFG r{zz,,VWg,NN	" "R-$Wzz&&vwQQRV38g ' G 	G ::(('(JJrd   )r   r   rf   rg   rh   ri   r}   rJ   c          	         d}
|	t        |       }	t        | ||	||||      \  } }|	j                  | j                  d      }|.|r,|	j	                  | j                        j
                  dz  dz  }n|d}t        |	      r |	j                  j                  | ||||      S t        |	      r+|dk(  rd n|}|	j                  j                  | |||dd	|
      S t        j                  j                  | ||||      S )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$#'[(OFG zz&,,(MNH| xx%))3.2	r{zz))&'/3W * F 	F	"!R-$Wzz&&vwT155g ' W 	W ::%%fgD+/ & B Brd   )rf   rg   rh   ri   r}   verboserJ   c          	         d}	|t        |       }t        | ||||||      \  } }t        |      r|j                  j	                  | |||      S t        |      rR| j                  j                  dk7  r| j                         } |j                  j                  dk7  r|j                         }t        j                  j	                  | |||      S )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$#_[OFG r{zz++FG4;W , N 	N	"==&ZZ\F>>%'kkmG::''07 ( J Jrd   c                 B    ddd| z  z  }}t        | |g|||ddd|S 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   _  H     CgX&$D67 * * $4UPU*$(* *rd   c                 B    ddd| z  z  }}t        | |g|||ddd|S r   r   r   s          rH   r   r   h  r   rd   paramc                     d| dS )Nz
Providing z$ is only supported for numpy arrays. )r   s    rH   r+   r+   q  s    y DEErd   c                 :    |j                  | j                  d      S Nr   )r   r>   rc   s     rH   r   r   u  s    ::agg122rd   c                    g }t        |       rj|dgz  }ddl}|j                  j                         }t	        d|      D ]  }|d| gz  } |j
                  j                  j                         r|dgz  }|S t        |       rDddl	}|j                  j                  j                         }t	        d|      D ]  }|d| gz  } |S t        |       rddl}|j                  d      }t	        d|      D ]  }|d| gz  } |j                  d      }t	        d|      D ]  }|d	| gz  } |j                  d
      }	t	        d|	      D ]  }|d| gz  } |S dg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                     t        |       rddl}|j                  S t        |       rddl}|j                  S t        |       r| S y)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     s;     r{{{bzyy|	rd   sourcec                   |t        |      n|}t        t        | j                              }|j	                  |      }||gz   }|j                  | |      S N)r   listr2   ndimpoppermute_dims)ra   r   rJ   axestemps        rH   r%   r%     sP    
 !#
	BaffD88FD4&=D??1d##rd   x1x2c               r    |t        | |      n|}|j                  |       }|j                  |dk\  ||       S )Nr   )r   abswhere)r   r   rJ   abs_x1s       rH   r#   r#     s:    $&JR	 BBVVBZF88B!GVfW--rd   c               p   |t        |       n|}t        |      r|j                  |       S |j                  |       }|j	                  d| j
                        }|j                  | dkD  ||      }|j                  | dk  | |      }|j                  |j                  |       |j                  |z  |      }|S )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               :   |t        |       n|}t        rdt        |d      r|j                  j	                  | |||      S |dk7  rt        d      |j                  |j                  |       | z  ||      dz  S t        j                  j                  | |||      S )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((c(RRax !  66"''!*q.th6GLL yy~~aSth~GGrd   c               D    |t        |       n|}|j                  | d      S )N))r   reshaperc   s     rH   r&   r&     s%      "z	rB::ard   c               ~    |t        |       n|}|j                  | j                  d      r|j                  |       S | S r   )r   r   r>   realrc   s     rH   r'   r'     s9      "z	rBAGG-?@2771:GaGrd   r   )r   rJ   rz   indicesc                   |t        |       n|}t        |      r|j                  | ||      S t        |      rt	        d      |j                  | ||      S )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|  g4 88	R	 !"VWW!!#w55rd   )ensure_writeableforce_floatingrJ   c                    |t        | n|}|D cg c]  }|t        |d      n| }}|D cg c]  }||	 }}|j                  d      j                  }|D cg c]  }|j                   }}	  |j                  | }|r$|j                  |d      r|j	                  ||      }|D ch c]  }|j                   }
}	 t        |
      dk7  rt        j                  |
 n|d   j                  }g }|D ]  }||j                  |       |j                  |k7  r%t        |      rddini } |j                  ||fi |}|j                  |k7  s| r|j                  ||d	      }|j                  |        |S c c}w c c}w c c}w # t        $ rV |D cg c]  }|j                  |d      s| nc c}w }	}|	r |j                  g |	| }n|r|}n |j                  | }Y ?w xY wc c}w # t        $ r}d}t        |      |d }~ww xY w)
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T#/Xc&sBTDT$(<SCOS<M< JJrN((M#01Ccii1F1,'bjj
; NN5-8E $11Ccii1F1)14V1A$$f-#A&,, 	 C ;JJsO
 99(0gt_"F!"//#u77C II#3))CT)2C

3" Jc U< 2  ,+1 >%!zz%<  > > >"BNN@L@-@E!E"BNNF+E, 2  )C!q()sQ   E9E>E>F25F +G*/G/ G'F103G'&G'/	H8HHc                    |t        |       n|}| j                  }|j                  ||j                        r|j	                  | |j
                        } | S |j                  |d      r|j	                  | |j                        } | S )Nr   )r   r>   r   float32r   	complex64
complex128)rz   rJ   	arr_dtypes      rH   xp_float_to_complexr   H  ss    !#	B		I 
zz)RZZ(iiR\\* J 
I	/iiR]]+Jrd   c                 P    t        |       r| j                         S | j                  S )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   