{ } Raw JSON

bundles / numpy 2.4.4 / numpy / testing / _private / utils / assert_allclose

function

numpy.testing._private.utils:assert_allclose

source: /numpy/testing/_private/utils.py :1663

Signature

def   assert_allclose ( actual desired rtol = 1e-07 atol = 0 equal_nan = True err_msg = '' verbose = True * strict = False )

Summary

Raises an AssertionError if two objects are not equal up to desired tolerance.

Extended Summary

Given two array_like objects, check that their shapes and all elements are equal (but see the Notes for the special handling of a scalar). An exception is raised if the shapes mismatch or any values conflict. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions.

The test is equivalent to allclose(actual, desired, rtol, atol), except that it is stricter: it doesn't broadcast its operands, and has tighter default tolerance values. It compares the difference between actual and desired to atol + rtol * abs(desired).

Parameters

actual : array_like

Array obtained.

desired : array_like

Array desired.

rtol : float, optional

Relative tolerance.

atol : float, optional

Absolute tolerance.

equal_nan : bool, optional.

If True, NaNs will compare equal.

err_msg : str, optional

The error message to be printed in case of failure.

verbose : bool, optional

If True, the conflicting values are appended to the error message.

strict : bool, optional

If True, raise an AssertionError when either the shape or the data type of the arguments does not match. The special handling of scalars mentioned in the Notes section is disabled.

Raises

: AssertionError

If actual and desired are not equal up to specified precision.

Notes

When one of actual and desired is a scalar and the other is array_like, the function performs the comparison as if the scalar were broadcasted to the shape of the array. Note that empty arrays are therefore considered equal to scalars. This behaviour can be disabled by setting strict==True.

Examples

x = [1e-5, 1e-3, 1e-1]
y = np.arccos(np.cos(x))
np.testing.assert_allclose(x, y, rtol=1e-5, atol=0)
As mentioned in the Notes section, `assert_allclose` has special handling for scalars. Here, the test checks that the value of `numpy.sin` is nearly zero at integer multiples of π.
x = np.arange(3) * np.pi
np.testing.assert_allclose(np.sin(x), 0, atol=1e-15)
Use `strict` to raise an ``AssertionError`` when comparing an array with one or more dimensions against a scalar.
np.testing.assert_allclose(np.sin(x), 0, atol=1e-15, strict=True)
The `strict` parameter also ensures that the array data types match:
y = np.zeros(3, dtype=np.float32)
np.testing.assert_allclose(np.sin(x), y, atol=1e-15, strict=True)

See also

assert_array_almost_equal_nulp
assert_array_max_ulp

Aliases

  • numpy.testing.assert_allclose

Referenced by