testing.assert_close¶
- array_api_extra.testing.assert_close(actual, desired, *, rtol=None, atol=0, equal_nan=True, err_msg='', verbose=True, check_dtype=True, check_shape=True, check_scalar=False, xp=None)¶
Check that two arrays are close, up to tolerance
atol + rtol * abs(desired).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.ndarrayvs scalar (e.g.numpy.double).xp (array_namespace, optional) – A standard-compatible namespace which actual and desired must match.
- Raises:
ImportError – If
numpyis not importable in the Python environment.- Return type:
See also
assert_equalSimilar function for exact equality checks.
array_api_extra.iscloseSimilar function checking closeness, returning a bool.
numpy.testing.assert_allcloseSimilar function for NumPy arrays.
Notes
The default atol and rtol differ from
xp.all(xpx.isclose(a, b)). For inexact dtypes, the default rtol isxp.finfo(actual.dtype).eps ** 0.5 * 4, which forfloat64is roughly halfway between \(\sqrt{\epsilon}\) and the default fornumpy.testing.assert_allclose(),1e-7. This gives a more reasonable default for lower precision dtypes, for example approximately1e-3forfloat32. For exact dtypes, the default1e-7is used.Array arguments to atol and rtol must be valid input to
float.