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) → NoneSummary
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: ArrayThe array produced by the tested function.
desired: ArrayThe expected array (typically hardcoded).
rtol: float or Array, optionalRelative tolerance. Default: dtype-dependent.
atol: float or Array, optionalAbsolute tolerance. Default: 0.
equal_nan: bool, default: TrueWhether to consider NaNs in corresponding locations as equal.
err_msg: str, optionalError message to display on failure.
verbose: bool, default: TrueWhether to include the conflicting arrays in the error message on failure.
check_dtype: bool, default: TrueWhether to check agreement between actual and desired dtypes.
check_shape: bool, default: TrueWhether to check agreement between actual and desired shapes.
check_scalar: bool, default: FalseNumPy only: whether to check agreement between actual and desired types — 0-D numpy.ndarray vs scalar (e.g. numpy.double).
check_device: bool, default: TrueWhether to check agreement between actual and desired devices.
xp: array_namespace, optionalA standard-compatible namespace which
actualanddesiredmust match.
Raises
: AssertionErrorIf
actualanddesiredare not equal up to the defined tolerance.: ImportErrorIf 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