larray.Array.allclose

Array.allclose(other: Any, rtol: float = 1e-05, atol: float = 1e-08, nans_equal: bool = True, check_axes: bool = False) bool[source]

Compare this array with another array and returns True if they are element-wise equal within a tolerance.

The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(other)) and the absolute difference atol are added together to compare against the absolute difference between this array and other.

NaN values are treated as equal if they are in the same place and if nans_equal=True.

Parameters
otherArray-like

Input array. asarray() is used on a non-Array input.

rtolfloat or int, optional

The relative tolerance parameter (see Notes). Defaults to 1e-05.

atolfloat or int, optional

The absolute tolerance parameter (see Notes). Defaults to 1e-08.

nans_equalboolean, optional

Whether to consider NaN values at the same positions in the two arrays as equal. By default, an array containing NaN values is never equal to another array, even if that other array also contains NaN values at the same positions. The reason is that a NaN value is different from anything, including itself. Defaults to True.

check_axesboolean, optional

Whether to check that the set of axes and their order is the same on both sides. Defaults to False. If False, two arrays with compatible axes (and the same data) will compare equal, even if some axis is missing on either side or if the axes are in a different order.

Returns
bool

Return True if the two arrays are equal within the given tolerance; False otherwise.

See also

Array.equals

Notes

If the following equation is element-wise True, then allclose returns True.

absolute(array1 - array2) <= (atol + rtol * absolute(array2))

The above equation is not symmetric in array1 and array2, so that array1.allclose(array2) might be different from array2.allclose(array1) in some rare cases.

Examples

>>> arr1 = Array([1e10, 1e-7], "a=a0,a1")
>>> arr2 = Array([1.00001e10, 1e-8], "a=a0,a1")
>>> arr1.allclose(arr2)
False
>>> arr1 = Array([1e10, 1e-8], "a=a0,a1")
>>> arr2 = Array([1.00001e10, 1e-9], "a=a0,a1")
>>> arr1.allclose(arr2)
True
>>> arr1 = Array([1e10, 1e-8], "a=a0,a1")
>>> arr2 = Array([1.0001e10, 1e-9], "a=a0,a1")
>>> arr1.allclose(arr2)
False
>>> arr1 = Array([1.0, nan], "a=a0,a1")
>>> arr2 = Array([1.0, nan], "a=a0,a1")
>>> arr1.allclose(arr2)
True
>>> arr1.allclose(arr2, nans_equal=False)
False