o
    5Bi1                     @  sH  U d Z ddlmZ ddlZddlZddlZddlmZ ddl	m
Z
mZmZ g dZejddZd	ed< ejd
dZeedZdd Zdd Zdd Z	d6dddddZddddZddddZdd Zdd Zdd  Z	!d7d"d#Zd$d% Zd&d' Z	!	d8d)d*Z 	!	d9d,d-Z!	!	d:d.d/Z"ddd0d1Z#d2d3 Z$d4d5 Z%dS );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)array_api_compat)is_array_api_objsizenumpy)array_namespace_asarrayr   SCIPY_ARRAY_APIFz
str | boolSCIPY_DEVICEcpu)r	   r
   c              	   C  s   t t| D ]t}| | }t|tjjrtdt|tjr!tdt|tjtj	frD|j
}t|tjsCt|tjsCtd|dqt|szzt|}W n tyZ   tdw |j
}t|tjsvt|tjsvd|d}t||| |< q| S )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   message r   U/var/www/html/Trade-python/venv/lib/python3.10/site-packages/scipy/_lib/_array_api.pycompliance_scipy%   s2   

r!   c                 C  s:   d}z| || st|W dS  ty   t|w )zCheck for NaNs or Infs.z#array must not contain infs or NaNsN)allisfinite
ValueErrorr   )r   xpmsgr   r   r    _check_finiteK   s   r'   c                  G  s,   t d stS dd | D } t| } tj|  S )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	   c                 S  s   g | ]}|d ur|qS )Nr   ).0r   r   r   r    
<listcomp>s   s    z#array_namespace.<locals>.<listcomp>)_GLOBAL_CONFIG	np_compatr!   r   r   )r   r   r   r    r   U   s
   
r   )r%   check_finitec                C  s   |du rt | }|jdv r(|du rtj| ||d} ntj| ||d} || } n#z
|j| ||d} W n tyJ   t |d}|j| ||d} Y nw |rRt| | | S )a  SciPy-specific replacement for `np.asarray` with `order` and `check_finite`.

    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.
    N>   r   !scipy._lib.array_api_compat.numpyT)orderr   )r   copy   )r   __name__r   r   asarrayr   r'   )r   r   r.   r/   r%   r,   
coerced_xpr   r   r    r   z   s    

r   r%   c                C  sD   |du rt | }|| } | j|k r |j| dd} t| ||d} | S )z9Recursively expand the dimension to have at least `ndim`.Nr   axisndimr%   )r   r2   r8   expand_dims
atleast_nd)xr8   r%   r   r   r    r:      s   

r:   c                C  s   |du rt | }t| d|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.
    NT)r/   r%   )r   r   r;   r%   r   r   r    r/      s   r/   c                 C  
   | j dv S )N)r   r-   r1   r4   r   r   r    is_numpy      
r?   c                 C  r=   )N)cupyz scipy._lib.array_api_compat.cupyr>   r4   r   r   r    is_cupy   r@   rB   c                 C  r=   )N)torchz!scipy._lib.array_api_compat.torchr>   r4   r   r   r    is_torch   r@   rD   Tc                 C  sr   d}|r	t | | ||}|rd}| j|jksJ ||r0d}| j|jks*J |t| || ||| j}|S )NTzDdtypes do not match.
Actual: {actual.dtype}
Desired: {desired.dtype}zDShapes do not match.
Actual: {actual.shape}
Desired: {desired.shape})_assert_matching_namespacer2   r   shape_check_scalarbroadcast_to)actualdesiredr%   check_namespacecheck_dtypecheck_shape__tracebackhide___msgr   r   r    _strict_check   s   

rP   c                 C  sZ   d}t | tr	| n| f} t|}| D ]}t|}d|j d|j }||ks*J |qd S )NTz!Namespaces do not match.
Actual: z

Desired: )r   tupler   r1   )rI   rJ   rN   desired_spacearr	arr_spacerO   r   r   r    rE      s   rE   c                 C  sr   d}|j dkst|sd S |d }dt|  dt| }|| r'||s5|| s1||r7J |d S d S )NTr   zTypes do not match:
 Actual: z
 Desired: )rF   r?   typeisscalar)rI   rJ   r%   rN   rO   r   r   r    rG      s   rG    c              	   C  s   d}|d u r
t | }t| |||||d}t|r!|jj| ||dS t|r:|dkr+d n|}|jj| |dddd|dS tjj| ||dS )NTrK   rL   rM   )err_msgrW   r   Frtolatol	equal_nanrL   r&   )r   rP   rB   testingassert_array_equalrD   assert_closer   )rI   rJ   rK   rL   rM   rY   r%   rN   r   r   r    xp_assert_equal  s   
ra   Hz>c	           
   	   C  s   d}	|d u r
t | }t| |||||d}t|r#|jj| ||||dS t|r<|dkr-d n|}|jj| |||dd|dS tjj| ||||dS )NTrX   )r[   r\   rY   rW   FrZ   )r   rP   rB   r^   assert_allcloserD   r`   r   )
rI   rJ   r[   r\   rK   rL   rM   rY   r%   rN   r   r   r    xp_assert_close  s$   
rd   c           	      C  s   d}|d u r
t | }t| |||||d}t|r"|jj| |||dS t|r:| jjdkr0|  } |jjdkr:| }t	jj| |||dS )NTrX   )rY   verboser   )
r   rP   rB   r^   assert_array_lessrD   devicerU   r   r   )	rI   rJ   rK   rL   rM   rY   re   r%   rN   r   r   r    xp_assert_less0  s$   


rh   c          	      C  s   |d u rt | }t| |d}|||j}t|d|d}|j||d}|j|dd}|jd d }|dkr?tj	dt
dd	 d
}||d d d f 8 }|j}||jdrX||}|| }|| }tdd t|jD }|j||dS )Nr4      r7   )r      r5   r   z!Degrees of freedom <= 0 for slice)
stacklevelg        complex floatingc                 s  s     | ]\}}|d kr|V  qdS )rj   Nr   )r(   r6   lengthr   r   r    	<genexpr>[  s    zcov.<locals>.<genexpr>)r   r/   result_typefloat64r:   r2   meanrF   warningswarnRuntimeWarningTisdtyper   conjrQ   	enumeratesqueeze)	r;   r%   Xr   avgfactX_Tcaxesr   r   r    covC  s*   
r   c                 C  s   d| dS )Nz
Providing z$ is only supported for numpy arrays.r   )paramr   r   r    xp_unsupported_param_msg_  s   r   c                 C  s   | | jdS )Nrl   )rv   r   r<   r   r   r    
is_complexc  s   r   )NNN)TTT)TTTrW   N)rb   r   TTTrW   N)TTTrW   TN)&__doc__
__future__r   osrr   r   r   
scipy._libr   scipy._lib.array_api_compatr   r   r+   __all__environgetr	   __annotations__r
   r*   r!   r'   r   r   r:   r/   r?   rB   rD   rP   rE   rG   ra   rd   rh   r   r   r   r   r   r   r    <module>   sR    &
&&



