From bd18ed4668f4732115cbab6d69b8906148bdc3f1 Mon Sep 17 00:00:00 2001 From: Matt Haberland Date: Sat, 5 Sep 2026 12:31:52 -0700 Subject: [PATCH 1/2] Fix matrix transpose reference construction --- array_api_tests/test_linalg.py | 12 ++++++++---- meta_tests/test_linalg.py | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/array_api_tests/test_linalg.py b/array_api_tests/test_linalg.py index 29d7775b..7c8ce77b 100644 --- a/array_api_tests/test_linalg.py +++ b/array_api_tests/test_linalg.py @@ -516,10 +516,14 @@ def test_matrix_rank(x, kw): def _test_matrix_transpose(namespace, x): matrix_transpose = namespace.matrix_transpose res = matrix_transpose(x) - true_val = lambda a: _array_module.asarray([[a[i, j] for i in - range(a.shape[0])] for j in - range(a.shape[1])], - dtype=a.dtype) + def true_val(a): + expected = _array_module.empty((a.shape[1], a.shape[0]), + dtype=a.dtype, device=a.device) + for i in range(a.shape[0]): + for j in range(a.shape[1]): + expected[j, i] = a[i, j] + return expected + shape = list(x.shape) shape[-1], shape[-2] = shape[-2], shape[-1] shape = tuple(shape) diff --git a/meta_tests/test_linalg.py b/meta_tests/test_linalg.py index 82794b6c..1cdd2860 100644 --- a/meta_tests/test_linalg.py +++ b/meta_tests/test_linalg.py @@ -5,6 +5,31 @@ from array_api_tests .hypothesis_helpers import symmetric_matrices from array_api_tests import array_helpers as ah from array_api_tests import _array_module as xp +from array_api_tests.test_linalg import _test_matrix_transpose + + +@pytest.mark.parametrize('shape', [(2, 3), (2, 2, 3), (0, 3), (3, 0), (0, 0)]) +def test_matrix_transpose_without_nested_arrays(monkeypatch, shape): + size = 1 + for dim in shape: + size *= dim + x = xp.reshape(xp.arange(size, dtype=xp.int64), shape) + original_asarray = xp.asarray + + def asarray(obj, **kwargs): + def check_sequence(value): + if isinstance(value, (list, tuple)): + for item in value: + check_sequence(item) + else: + assert isinstance(value, (bool, int, float, complex)) + + if isinstance(obj, (list, tuple)): + check_sequence(obj) + return original_asarray(obj, **kwargs) + + monkeypatch.setattr(xp, 'asarray', asarray) + _test_matrix_transpose(xp, x) @pytest.mark.xp_extension('linalg') @given(x=symmetric_matrices(finite=True)) From b676d24809964240abd8e51b8f42b5c939dc7f32 Mon Sep 17 00:00:00 2001 From: Matt Haberland Date: Sat, 5 Sep 2026 12:33:48 -0700 Subject: [PATCH 2/2] Use stack to construct the expected transpose --- array_api_tests/test_linalg.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/array_api_tests/test_linalg.py b/array_api_tests/test_linalg.py index 7c8ce77b..acc4dc42 100644 --- a/array_api_tests/test_linalg.py +++ b/array_api_tests/test_linalg.py @@ -517,12 +517,10 @@ def _test_matrix_transpose(namespace, x): matrix_transpose = namespace.matrix_transpose res = matrix_transpose(x) def true_val(a): - expected = _array_module.empty((a.shape[1], a.shape[0]), - dtype=a.dtype, device=a.device) - for i in range(a.shape[0]): - for j in range(a.shape[1]): - expected[j, i] = a[i, j] - return expected + if 0 in a.shape: + return xp.empty((a.shape[1], a.shape[0]), + dtype=a.dtype, device=a.device) + return xp.stack([a[i, :] for i in range(a.shape[0])], axis=1) shape = list(x.shape) shape[-1], shape[-2] = shape[-2], shape[-1]