Skip to content

Commit 75ca73a

Browse files
committed
Remove the allclose testing in linalg
Floating-point dtypes now only test shape and dtype in the stacking test. Trying to make the allclose test work is too difficult for linear algebra functions.
1 parent f439259 commit 75ca73a

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

array_api_tests/test_linalg.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@
4646

4747
def assert_equal(x, y):
4848
if x.dtype in dh.float_dtypes:
49-
assert_allclose(x, y)
49+
# It's too difficult to do an approximately equal test here because
50+
# different routines can give completely different answers, and even
51+
# when it does work, the elementwise comparisons are too slow. So for
52+
# floating-point dtypes only test the shape and dtypes.
53+
54+
# assert_allclose(x, y)
55+
56+
assert x.shape == y.shape, f"The input arrays do not have the same shapes ({x.shape} != {y.shape})"
57+
assert x.dtype == y.dtype, f"The input arrays do not have the same dtype ({x.dtype} != {y.dtype})"
5058
else:
5159
assert_exactly_equal(x, y)
5260

@@ -250,18 +258,17 @@ def test_eigh(x):
250258
assert eigenvectors.dtype == x.dtype, "eigh().eigenvectors did not return the correct dtype"
251259
assert eigenvectors.shape == x.shape, "eigh().eigenvectors did not return the correct shape"
252260

253-
# The order of the eigenvalues is not specified, so make sure the same
254-
# eigenvalues are compared against each other.
261+
# Note: _test_stacks here is only testing the shape and dtype. The actual
262+
# eigenvalues and eigenvectors may not be equal at all, since there is not
263+
# requirements about how eigh computes an eigenbasis, or about the order
264+
# of the eigenvalues
255265
_test_stacks(lambda x: linalg.eigh(x).eigenvalues, x,
256-
res=eigenvalues, dims=1, assert_equal=lambda a, b:
257-
assert_equal(xp.sort(a), xp.sort(b)))
266+
res=eigenvalues, dims=1)
258267

259-
# There are equivalent ways of representing eigenvectors, and algorithms
260-
# may not give the same eigenvectors on a stack vs. a matrix.
261268
# TODO: Test that eigenvectors are orthonormal.
262269

263-
# _test_stacks(lambda x: linalg.eigh(x).eigenvectors, x,
264-
# res=eigenvectors, dims=2)
270+
_test_stacks(lambda x: linalg.eigh(x).eigenvectors, x,
271+
res=eigenvectors, dims=2)
265272

266273
# TODO: Test that res actually corresponds to the eigenvalues and
267274
# eigenvectors of x
@@ -274,12 +281,14 @@ def test_eigvalsh(x):
274281
assert res.dtype == x.dtype, "eigvalsh() did not return the correct dtype"
275282
assert res.shape == x.shape[:-1], "eigvalsh() did not return the correct shape"
276283

277-
# The order of the eigenvalues is not specified, so make sure the same
278-
# eigenvalues are compared against each other.
279-
_test_stacks(linalg.eigvalsh, x, res=res, dims=1, assert_equal=lambda a,
280-
b: assert_equal(xp.sort(a), xp.sort(b)))
284+
# Note: _test_stacks here is only testing the shape and dtype. The actual
285+
# eigenvalues may not be equal at all, since there is not requirements or
286+
# about the order of the eigenvalues, and the stacking code may use a
287+
# different code path.
288+
_test_stacks(linalg.eigvalsh, x, res=res, dims=1)
281289

282290
# TODO: Should we test that the result is the same as eigh(x).eigenvalues?
291+
# (probably no because the spec doesn't actually require that)
283292

284293
# TODO: Test that res actually corresponds to the eigenvalues of x
285294

0 commit comments

Comments
 (0)