a
    h1                     @  s8  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jjr0tdnt|tjrDtdt|tjtj	fr|j
}t|tjst|tjstd|dqt|szt|}W n ty   tdY n0 |j
}t|tjst|tjsd|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maZMaskedArray	TypeErrormatrixZndarrayZgenericdtypeZ
issubdtypenumberZbool_r   Z
asanyarray)arraysiarrayr   message r   Q/var/www/html/assistant/venv/lib/python3.9/site-packages/scipy/_lib/_array_api.pycompliance_scipy%   s,    



r   c                 C  s@   d}z| || st|W n ty:   t|Y n0 dS )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       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 rP|du r4tj| ||d} ntj| ||d} || } nFz|j| ||d} W n0 ty   t |d}|j| ||d} Y n0 |rt| | | 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'   Z
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   r-   r2   Zexpand_dims
atleast_nd)xr2   r   r   r   r   r3      s    

r3   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   r4   r   r   r   r   r*      s    r*   c                 C  s
   | j dv S )N)r   r(   r,   r.   r   r   r   is_numpy   s    r7   c                 C  s
   | j dv S )N)Zcupyz scipy._lib.array_api_compat.cupyr6   r.   r   r   r   is_cupy   s    r8   c                 C  s
   | j dv S )N)Ztorchz!scipy._lib.array_api_compat.torchr6   r.   r   r   r   is_torch   s    r9   Tc                 C  sr   d}|rt | | ||}|r8d}| j|jks8J ||r`d}| j|jksTJ |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_namespacer-   r   shape_check_scalarZbroadcast_to)actualdesiredr   check_namespacecheck_dtypecheck_shape__tracebackhide___msgr   r   r   _strict_check   s    

rD   c                 C  sZ   d}t | tr| n| f} t|}| D ]0}t|}d|j d|j }||ks$J |q$d S )NTz!Namespaces do not match.
Actual: z

Desired: )r   tupler   r,   )r=   r>   rB   Zdesired_spaceZarrZ	arr_spacerC   r   r   r   r:      s    r:   c                 C  sn   d}|j dkst|sd S |d }dt|  dt| }|| rN||sj|| sb||rjJ |d S )NTr   zTypes do not match:
 Actual: z
 Desired: )r;   r7   typeZisscalar)r=   r>   r   rB   rC   r   r   r   r<      s    r<    c              	   C  s   d}|d u rt | }t| |||||d}t|rB|jj| ||dS t|rt|dkrVd n|}|jj| |dddd|dS tjj| ||dS )NTr?   r@   rA   )err_msgrG   r   FrtolatolZ	equal_nanr@   r    )r   rD   r8   testingZassert_array_equalr9   assert_closer   )r=   r>   r?   r@   rA   rI   r   rB   r   r   r   xp_assert_equal  s    
rO   Hz>c	           
   	   C  s   d}	|d u rt | }t| |||||d}t|rF|jj| ||||dS t|rx|dkrZd n|}|jj| |||dd|dS tjj| ||||dS )NTrH   )rK   rL   rI   rG   FrJ   )r   rD   r8   rM   Zassert_allcloser9   rN   r   )
r=   r>   rK   rL   r?   r@   rA   rI   r   rB   r   r   r   xp_assert_close  s$    
rQ   c           	      C  s   d}|d u rt | }t| |||||d}t|rD|jj| |||dS t|rt| jjdkr`|  } |jjdkrt| }t	jj| |||dS )NTrH   )rI   verboser   )
r   rD   r8   rM   Zassert_array_lessr9   ZdevicerF   r   r   )	r=   r>   r?   r@   rA   rI   rR   r   rB   r   r   r   xp_assert_less0  s$    


rS   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r||}|| }|| }tdd t|jD }|j||dS )Nr.      r1   )r      r/   r   z!Degrees of freedom <= 0 for slice)
stacklevelg        complex floatingc                 s  s   | ]\}}|d kr|V  qdS )rU   Nr   )r"   r0   lengthr   r   r   	<genexpr>[  r$   zcov.<locals>.<genexpr>)r   r*   Zresult_typeZfloat64r3   r-   meanr;   warningswarnRuntimeWarningTisdtyper   ZconjrE   	enumerateZsqueeze)	r4   r   Xr   ZavgZfactZX_TcZaxesr   r   r   covC  s*    
rc   c                 C  s   d| dS )Nz
Providing z$ is only supported for numpy arrays.r   )paramr   r   r   xp_unsupported_param_msg_  s    re   c                 C  s   | | jdS )NrW   )r_   r   r5   r   r   r   
is_complexc  s    rf   )NNN)TTT)TTTrG   N)rP   r   TTTrG   N)TTTrG   TN)&__doc__
__future__r   osr[   r   r   Z
scipy._libr   Zscipy._lib.array_api_compatr   r   r&   __all__environgetr	   __annotations__r
   r%   r   r!   r   r   r3   r*   r7   r8   r9   rD   r:   r<   rO   rQ   rS   rc   re   rf   r   r   r   r   <module>   sP   &
& & 
  
  
  
