
    4[gH                       U d Z ddlmZ ddl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 g dZej.                  j1                  d	d
      Zded	<   ej.                  j1                  dd      ZeedZe
reZeej<                  z  Zd+dZd,dZ d-dZ!	 	 	 d.dd
d
d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d/dZ"ddd0dZ#ddd1dZ$d2dZ%d2dZ&d2dZ'd Z(	 d3dZ)d Z*d Z+	 	 d4dZ,	 	 d5dZ-	 	 d6d Z.ddd1d!Z/d7d"Z0d8d#Z1d9d$Z2d:d%Z3d;d&Z4	 	 d<dd	 	 	 	 	 	 	 	 	 d=d'Z5dd	 	 	 	 	 	 	 d>d(Z6ddd?d)Z7ddd1d*Z8y)@a6  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
    )annotationsN)
ModuleType)AnyLiteralTYPE_CHECKING)array_api_compat)is_array_api_objsizenumpydevice)array_namespace_asarrayr
   r   SCIPY_ARRAY_APIFz
str | boolSCIPY_DEVICEcpu)r   r   c                T   t        t        |             D ]w  }| |   }t        |t        j                  j
                        rt        d      t        |t        j                        rt        d      t        |t        j                  t        j                  f      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        |      || |<   z | 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
    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
isinstancenpmaMaskedArray	TypeErrormatrixndarraygenericdtype
issubdtypenumberbool_r	   
asanyarray)arraysiarrayr   messages        P/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/_lib/_array_api.pycompliance_scipyr'   /   sb    3v;q	eRUU../VWWryy)NOOebjj"**56KKEMM%3r}}UBHH7U"9% CS !T U U!%(7e, KKEMM%3r}}UBHH7UGy QG H   ((F1I1  2 M  7 !6 7 77s   FF'c                    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isfinite
ValueErrorr   )r$   xpmsgs      r&   _check_finiter.   U   sI    
/Cvvbkk%()S/! * os	   +0 Ac                     t         d   st        S | D cg c]  }||	 }}t        |      }t        j                  | S c c}w )a0  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_compatr'   r   r   )r"   r$   _arrayss      r&   r   r   _   sL    4 +,"(>&E,=u&G>w'G++W55	 ?s   ??)r,   check_finitesubokc                  |t        |       }|j                  dv rc|du rt        j                  | |||      } n3|rt        j                  | ||      } nt        j
                  | ||      } |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.
    >   r   !scipy._lib.array_api_compat.numpyT)orderr   r4   )r7   r   )r   copy   )r   __name__r   r$   r!   asarrayr   r.   )r$   r   r7   r8   r,   r3   r4   
coerced_xps           r&   r   r      s    0 
zU#	{{DD4<HHU%uEJEMM%uEBEJJuE?E 

5!	FJJuEJ=E
 eR L  	F(A7J&&uE&EE	Fs    B$ $7CCr,   c                   |t        |       }|j                  |       } | j                  |k  r!|j                  | d      } t	        | ||      } | S )z9Recursively expand the dimension to have at least `ndim`.r   axisndimr,   )r   r;   rB   expand_dims
atleast_nd)xrB   r,   s      r&   rD   rD      sN    	zQ


1Avv}NN11N%qt+H    c               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)r8   r,   )r   r   rE   r,   s     r&   r8   r8      s"    * 
zQADR((rF   c                    | j                   dv S )N)r   r6   r:   r=   s    r&   is_numpyrK          ;;HHHrF   c                    | j                   dv S )N)cupyz scipy._lib.array_api_compat.cupyrJ   r=   s    r&   is_cupyrO      s    ;;FFFrF   c                    | j                   dv S )N)torchz!scipy._lib.array_api_compat.torchrJ   r=   s    r&   is_torchrR      rL   rF   c                    | j                   dv S )N)z	jax.numpyzjax.experimental.array_apirJ   r=   s    r&   is_jaxrT      s    ;;EEErF   c                   d}|rt        | |       |j                  |      }|r<d| j                   d|j                   }| j                  |j                  k(  sJ |       |rId| j                   d|j                   }| j                  |j                  k(  sJ |       t	        | ||       |j                  || j                        }|S )NTzdtypes do not match.
Actual: 

Desired: zShapes do not match.
Actual: )_assert_matching_namespacer;   r   shape_check_scalarbroadcast_to)actualdesiredr,   check_namespacecheck_dtypecheck_shape__tracebackhide___msgs           r&   _strict_checkrb      s    "673jj!G/~[X||w}},2d2,/~[X||w}},2d2,fgr*oogv||4GNrF   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: rV   )r   tupler   r:   )r[   r\   r`   desired_spacearr	arr_spacera   s          r&   rW   rW     sv    !&%0VviF#G,M#C(	$--. /)2235 M)/4/) rF   c                   d}|j                   dk7  st        |      sy |d   }dt        |        dt        |       }|j                  |       r|j                  |      s*|j                  |       s|j                  |      rJ |       y y )NT zTypes do not match:
 Actual: z
 Desired: )rX   rK   typeisscalar)r[   r\   r,   r`   ra   s        r&   rY   rY     s     }}(2, bkG+DL>d7m_UDKKBKK$8KK'G0DMHLM F0D %9rF   c           	     D   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 )NTr]   r^   r_   )err_msg r   Frtolatol	equal_nanr^   r-   )r   rb   rO   testingassert_array_equalrR   assert_closer   )r[   r\   r]   r^   r_   rn   r,   r`   s           r&   xp_assert_equalrw   &  s    	zV$FGR(3NGr{zz,,VWg,NN	" "R-$Wzz&&vwQQRV38g ' G 	G ::(('(JJrF   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 )NTrm   )zreal floatingcomplex floatingg      ?   gHz>)rq   rr   rn   ro   Frp   )r   rb   isdtyper   finfoepsrO   rt   assert_allcloserR   rv   r   )r[   r\   rq   rr   r]   r^   r_   rn   r,   r`   floatings              r&   xp_assert_closer   9  s   	zV$FGR(3NG zz&,,(MNH| xx%))3.2	r{zz))&'/3W * F 	F	"!R-$Wzz&&vwT155g ' W 	W ::%%fgD+/ & B BrF   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 )NTrm   )rn   verboser   )
r   rb   rO   rt   assert_array_lessrR   r   rj   r   r   )	r[   r\   r]   r^   r_   rn   r   r,   r`   s	            r&   xp_assert_lessr   V  s    	zV$FGR(3NGr{zz++FG4;W , N 	N	"==&ZZ\F>>%'kkmG::''07 ( J JrF   c               N   |t        |       }t        | |      }|j                  ||j                        }t	        |d|      }|j                  ||      }|j                  |d      }|j                  d   dz
  }|dk  rt        j                  dt        d	       d
}||d d d f   z  }|j                  }|j                  |j                  d      r|j                  |      }||z  }||z  }t        d t!        |j                        D              }|j#                  ||      S )Nr=      rA   r      r?   r   z!Degrees of freedom <= 0 for slice)
stacklevelg        ry   c              3  2   K   | ]  \  }}|d k(  s|  yw)r   Nri   ).0r@   lengths      r&   	<genexpr>zcov.<locals>.<genexpr>  s     L*<,$!*<s   )r   r8   result_typefloat64rD   r;   meanrX   warningswarnRuntimeWarningTr{   r   conjrd   	enumeratesqueeze)	rE   r,   Xr   avgfactX_Tcaxess	            r&   covr   j  s   	zQQ2ANN1bjj)E11$A


1E
"A
''!!'
C771:>Dqy9$	4QWA
##C	zz#))/0ggcl	CAIAL)AGG*<LLD::ad:##rF   c                    d| dS )Nz
Providing z$ is only supported for numpy arrays.ri   )params    r&   xp_unsupported_param_msgr     s    y DEErF   c                :    |j                  | j                  d      S )Nry   )r{   r   rH   s     r&   
is_complexr     s    ::agg122rF   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:)rR   rQ   cudadevice_countr   backendsr   is_availablerO   rN   runtimegetDeviceCountrT   jax)
r,   devicesrQ   num_cudar#   rN   r   num_cpunum_gpunum_tpus
             r&   get_xp_devicesr     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 # 6MrF   c                z    t        |       rddl}|j                  S t        |       rddl}|j                  S ddl}|S )z
    Return the `scipy` namespace for alternative backends, where it exists,
    such as `cupyx.scipy` and `jax.scipy`. Useful for ad hoc dispatching.

    Default: return `scipy` (this package).
    r   N)rO   cupyxscipyrT   r   )r,   r   r   r   s       r&   scipy_namespace_forr     s4     r{{{bzyyLrF   c                   t        | |      }t        |d      r|j                  | |      S |j                  | |      \  } }|| k  |j	                  |      z  }|j                  |||       }|j                  dk(  r|d   S |S )Nminimumr   ri   )r   hasattrr   broadcast_arraysisnanwhererB   )x1x2r,   r#   ress        r&   
xp_minimumr     s    	R	 Br9zz"b!!  R(FB	bBHHRL A
((1b"
Chh!m3r7,,rF   c                 |t        |       n|}|j                  || j                        |j                  || j                        }}t        |d      r|j	                  | ||      S |j                  | ||      \  } }}|j                  | d      }||k  }||   ||<   ||kD  }||   ||<   |j                  dk(  r|d   S |S )Nr   clipT)r8   r   ri   )r   r;   r   r   r   r   rB   )	rE   minmaxr,   abyiaibs	            r&   xp_clipr     s      "z	rB::c:)2::c:+IqAr6wwq!Q!!!Q*GAq!


14
 A	
QBbEAbE	
QBbEAbEFFaK1R5&Q&rF   c                  |t        |      n|}t        t        | j                              }|j	                  |      }||gz   }|j                  | |      S )N)r   listr   rB   poppermute_dims)rE   sourcer,   r   temps        r&   xp_moveaxis_to_endr     sP    
 !#
	BaffD88FD4&=D??1d##rF   c              r    |t        | |      n|}|j                  |       }|j                  |dk\  ||       S )Nr   )r   absr   )r   r   r,   abs_x1s       r&   xp_copysignr     s:    $&JR	 BBVVBZF88B!GVfW--rF   c              Z   |t        |       n|}t        |      r|j                  |       S |j                  | |j                        }|j                  d| j                        }|j                  | dkD  ||      }|j                  | dk  | |      }|j                  | dk(  d|z  |      }|S )Nr   r   r   )r   rK   sign	full_likenanr;   r   r   )rE   r,   r   ones       r&   xp_signr     s    !z	rB|wwqz<<266"D
**Qagg*
&C88AE3%D88AEC4&D88AFAcE4(DKrF   )r"   zlist[ArrayLike]returnzlist[Array])r$   Arrayr,   r   r   None)r"   r   r   r   )NNN)r$   	ArrayLiker   r   r7   z"Literal['K', 'A', 'C', 'F'] | Noner8   zbool | Noner,   ModuleType | Noner3   boolr4   r   r   r   )rE   r   rB   intr,   r   r   r   )rE   r   r,   r   r   r   )r,   r   r   r   )TTT)TTTro   N)Nr   TTTro   N)TTTro   TN)r   r   r   str)rE   r   r,   r   r   r   )r,   r   r   zlist[str] | list[None])r,   r   r   r   )r   r   r   r   r   r   )NN)
r   int | float | Array | Noner   r   rE   r   r,   r   r   r   )rE   r   r   r   r,   r   r   r   )r   r   r   r   r,   r   r   r   )9__doc__
__future__r   osr   typesr   typingr   r   r   r   r   numpy.typingnpt
scipy._libr   scipy._lib.array_api_compatr	   r
   r1   r   __all__environgetr   __annotations__r   r0   r   r   r'   r.   r   r   rD   r8   rK   rO   rR   rT   rb   rW   rY   rw   r   r   r   r   r   r   r   r   r   r   r   r   ri   rF   r&   <module>r      s2   # 	   . .   '  < !jjnn->F Fzz~~ne4 '  E%I#L"6N 48 	0 !%"000 20 	0 0 0 0 
0f @D  /3 )6IGIF
 GK*	0M2 HL59K& IMGKB: GKBFJ( .2 $8F3!H.-  +/*.	' !%' (' (	'' ' */'4 !%		$	$	$ 		$ */		$ EI . 59 	rF   