larray.Array.equals

Array.equals(other, rtol=0, atol=0, nans_equal=False, check_axes=False) bool[source]

Compare this array with another array and returns True if they have the same axes and elements, False otherwise.

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 0.

atolfloat or int, optional

The absolute tolerance parameter (see Notes). Defaults to 0.

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 False.

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 this array is equal to other.

Notes

For finite values, equals uses the following equation to test whether two values are equal:

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

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

Examples

>>> arr1 = ndtest((2, 3))
>>> arr1
a\b  b0  b1  b2
 a0   0   1   2
 a1   3   4   5
>>> arr2 = arr1.copy()
>>> arr2.equals(arr1)
True
>>> arr2['b1'] += 1
>>> arr2.equals(arr1)
False
>>> arr3 = arr1.set_labels('a', ['x0', 'x1'])
>>> arr3.equals(arr1)
False

Test equality between two arrays within a given tolerance range. Return True if absolute(array1 - array2) <= (atol + rtol * absolute(array2)).

>>> arr1 = Array([6., 8.], "a=a0,a1")
>>> arr1
a   a0   a1
   6.0  8.0
>>> arr2 = Array([5.999, 8.001], "a=a0,a1")
>>> arr2
a     a0     a1
   5.999  8.001
>>> arr2.equals(arr1)
False
>>> arr2.equals(arr1, atol=0.01)
True
>>> arr2.equals(arr1, rtol=0.01)
True

Arrays with NaN values

>>> arr1 = ndtest((2, 3), dtype=float)
>>> arr1['a1', 'b1'] = nan
>>> arr1
a\b   b0   b1   b2
 a0  0.0  1.0  2.0
 a1  3.0  nan  5.0
>>> arr2 = arr1.copy()
>>> # 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.
>>> arr2.equals(arr1)
False
>>> # set flag nans_equal to True to overwrite this behavior
>>> arr2.equals(arr1, nans_equal=True)
True

Arrays with the same data but different axes

>>> arr1 = ndtest((2, 2))
>>> arr1
a\b  b0  b1
 a0   0   1
 a1   2   3
>>> arr2 = arr1.transpose()
>>> arr2
b\a  a0  a1
 b0   0   2
 b1   1   3
>>> arr2.equals(arr1)
True
>>> arr2.equals(arr1, check_axes=True)
False
>>> arr2 = arr1.expand('c=c0,c1')
>>> arr2
 a  b\c  c0  c1
a0   b0   0   0
a0   b1   1   1
a1   b0   2   2
a1   b1   3   3
>>> arr2.equals(arr1)
True
>>> arr2.equals(arr1, check_axes=True)
False