Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions array_api_tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,12 @@ 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):
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]
shape = tuple(shape)
Expand Down
25 changes: 25 additions & 0 deletions meta_tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading