Skip to content

Commit 3e968f1

Browse files
committed
Add notequal() array helper
1 parent c5229a7 commit 3e968f1

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

array_api_tests/array_helpers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ def exactly_equal(x, y):
126126

127127
return equal(x, y)
128128

129+
def notequal(x, y):
130+
"""
131+
Same as not_equal(x, y) except it gives False when both values are nan.
132+
133+
Note: this function does NOT distinguish +0 and -0.
134+
135+
This function implicitly assumes x and y have the same shape and dtype.
136+
"""
137+
if x.dtype in [float32, float64]:
138+
xnan = isnan(x)
139+
ynan = isnan(y)
140+
141+
both_nan = logical_and(xnan, ynan)
142+
# NOT both nan AND (both nan OR x != y)
143+
return logical_and(logical_not(both_nan), not_equal(x, y))
144+
145+
return not_equal(x, y)
146+
129147
def assert_exactly_equal(x, y):
130148
"""
131149
Test that the arrays x and y are exactly equal.
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1-
from ..array_helpers import exactly_equal
1+
from ..array_helpers import exactly_equal, notequal
22
import numpy as np
33

44
# TODO: These meta-tests currently only work with NumPy
55

66
def test_exactly_equal():
77
a = np.array([0, 0., -0., -0., np.nan, np.nan, 1, 1])
8-
b = np.array([0, -1, -0., 0., np.nan, 1, 1, 2])
8+
b = np.array([0, -1, -0., 0., np.nan, 1, 1, 2])
99

1010
res = np.array([True, False, True, False, True, False, True, False])
1111
np.testing.assert_equal(exactly_equal(a, b), res)
12+
13+
def test_notequal():
14+
a = np.array([0, 0., -0., -0., np.nan, np.nan, 1, 1])
15+
b = np.array([0, -1, -0., 0., np.nan, 1, 1, 2])
16+
17+
res = np.array([False, True, False, False, False, True, False, True])
18+
np.testing.assert_equal(notequal(a, b), res)

0 commit comments

Comments
 (0)