o
    YÚÁie  ã                   @   sŠ   U d Z ddlmZmZ ddlmZmZmZ	 g Z
ee ed< ddœdd„Zd	d
œdd„Zd	d
œdd„Zd	d
œdd„Zddd„Zddd„ZdS )aÜ  
Extra testing functions that forbid 0d-input, see #21044

While the xp_assert_* functions generally aim to follow the conventions of the
underlying `xp` library, NumPy in particular is inconsistent in its handling
of scalars vs. 0d-arrays, see https://github.com/numpy/numpy/issues/24897.

For example, this means that the following operations (as of v2.0.1) currently
return scalars, even though a 0d-array would often be more appropriate:

    import numpy as np
    np.array(0) * 2     # scalar, not 0d array
    - np.array(0)       # scalar, not 0d-array
    np.sin(np.array(0)) # scalar, not 0d array
    np.mean([1, 2, 3])  # scalar, not 0d array

Libraries like CuPy tend to return a 0d-array in scenarios like those above,
and even `xp.asarray(0)[()]` remains a 0d-array there. To deal with the reality
of the inconsistencies present in NumPy, as well as 20+ years of code on top,
the `xp_assert_*` functions here enforce consistency in the only way that
doesn't go against the tide, i.e. by forbidding 0d-arrays as the return type.

However, when scalars are not generally the expected NumPy return type,
it remains preferable to use the assert functions from
the `scipy._lib._array_api` module, which have less surprising behaviour.
é    )Úarray_namespaceÚis_numpy)Úxp_assert_closeÚxp_assert_equalÚxp_assert_lessÚ__all__N)Úxpc                K   sN   d}|d u r
t | ƒ}| |¡}t|ƒr|jdksd S d}| | ¡s%J |ƒ‚d S )NT© aÂ  Result is a NumPy 0d-array. Many SciPy functions intend to follow the convention of many NumPy functions, returning a scalar when a 0d-array would be correct. The specialized `xp_assert_*` functions in the `scipy._lib._array_api_no_0d` module err on the side of caution and do not accept 0d-arrays by default. If the correct result may legitimately be a 0d-array, pass `check_0d=True`, or use the `xp_assert_*` functions from `scipy._lib._array_api`.)r   Úasarrayr   ÚshapeÚisscalar)ÚactualÚdesiredr   ÚkwargsÚ__tracebackhide__Ú_msgr	   r	   úB/tmp/pip-target-1s0edx8b/lib/python/scipy/_lib/_array_api_no_0d.pyÚ_check_scalar#   s   
r   F)Úcheck_0dc                K   ó0   d}|st | |fi |¤Ž t| |fd|i|¤ŽS ©NTr   )r   Úxp_assert_equal_base©r   r   r   r   r   r	   r	   r   r   ;   s   r   c                K   r   r   )r   Úxp_assert_close_baser   r	   r	   r   r   F   ó   r   c                K   r   r   )r   Úxp_assert_less_baser   r	   r	   r   r   O   r   r   é   c                 O   ó6   ddd|   }}t | |g|¢R ||dddœ|¤ŽS ©zPBackwards compatible replacement. In new code, use xp_assert_close instead.
    r   g      ø?é
   F)ÚatolÚrtolÚcheck_dtypeÚcheck_shape©r   ©r   r   ÚdecimalÚargsÚkwdsr!   r    r	   r	   r   Úassert_array_almost_equalX   ó   þÿþr)   é   c                 O   r   r   r$   r%   r	   r	   r   Úassert_almost_equala   r*   r,   )r   )r+   )Ú__doc__Úscipy._lib._array_apir   r   r   r   r   r   r   r   r   ÚlistÚstrÚ__annotations__r   r)   r,   r	   r	   r	   r   Ú<module>   s    	
		