This is a pre-release version (latest).Go to latest (1.18.0)
{ } Raw JSON

bundles / scipy latest / scipy / _external / array_api_extra / testing / assert_close

function

scipy._external.array_api_extra.testing:assert_close

source: build-install/usr/lib/python3.14/site-packages/scipy/_external/array_api_extra/testing.py:717

Signature

def assert_close(actual: Array, desired: Array, *, rtol: float | Array | None = None, atol: float | Array = 0, equal_nan: bool = True, err_msg: str = '', verbose: bool = True, check_dtype: bool = True, check_shape: bool = True, check_scalar: bool = False, check_device: bool = True, xp: ArrayNamespace | None = None) → None

Summary

Check that two arrays are close, up to tolerance atol + rtol * abs(desired).

Extended Summary

This is an interface to numpy.testing.assert_allclose which accepts any standard-compatible array and performs additional array namespace, shape, and dtype checks.

Parameters

actual : Array

The array produced by the tested function.

desired : Array

The expected array (typically hardcoded).

rtol : float or Array, optional

Relative tolerance. Default: dtype-dependent.

atol : float or Array, optional

Absolute tolerance. Default: 0.

equal_nan : bool, default: True

Whether to consider NaNs in corresponding locations as equal.

err_msg : str, optional

Error message to display on failure.

verbose : bool, default: True

Whether to include the conflicting arrays in the error message on failure.

check_dtype : bool, default: True

Whether to check agreement between actual and desired dtypes.

check_shape : bool, default: True

Whether to check agreement between actual and desired shapes.

check_scalar : bool, default: False

NumPy only: whether to check agreement between actual and desired types — 0-D numpy.ndarray vs scalar (e.g. numpy.double).

check_device : bool, default: True

Whether to check agreement between actual and desired devices.

xp : array_namespace, optional

A standard-compatible namespace which actual and desired must match.

Raises

: AssertionError

If actual and desired are not equal up to the defined tolerance.

: ImportError

If numpy is not importable in the Python environment.

Notes

The default atol and rtol differ from xp.all(xpx.isclose(a, b)). For inexact dtypes, the default rtol is xp.finfo(actual.dtype).eps ** 0.5 * 4, which for float64 is roughly halfway between and the default for numpy.testing.assert_allclose, 1e-7. This gives a more reasonable default for lower precision dtypes, for example approximately 1e-3 for float32. For exact dtypes, the default 1e-7 is used.

Array arguments to atol and rtol must be valid input to float.

See also

array_api_extra.isclose

Similar function checking closeness, returning a bool.

assert_equal

Similar function for exact equality checks.

numpy.testing.assert_allclose

Similar function for NumPy arrays.

Aliases

  • scipy.differentiate.xpx.testing.assert_close