Skip to content

Commit 5d0b701

Browse files
authored
ENH: draw dtype parameter in the test for linalg.trace (#441)
Reviewed at #441
1 parent 65bd900 commit 5d0b701

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

array_api_tests/test_linalg.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,8 @@ def test_tensordot(x1, x2, kw):
920920
x=arrays(dtype=numeric_dtypes, shape=matrix_shapes()),
921921
# offset may produce an overflow if it is too large. Supporting offsets
922922
# that are way larger than the array shape isn't very important.
923-
kw=kwargs(offset=integers(-MAX_ARRAY_SIZE, MAX_ARRAY_SIZE))
923+
kw=kwargs(offset=integers(-MAX_ARRAY_SIZE, MAX_ARRAY_SIZE),
924+
dtype=sampled_from(dh.numeric_dtypes))
924925
)
925926
def test_trace(x, kw):
926927
res = linalg.trace(x, **kw)
@@ -939,10 +940,11 @@ def test_trace(x, kw):
939940
n, m = x.shape[-2:]
940941
ph.assert_result_shape('trace', x.shape, res.shape, expected=x.shape[:-2])
941942

942-
def true_trace(x_stack, offset=0):
943+
def true_trace(x_stack, offset=0, dtype=None):
943944
# Note: the spec does not specify that offset must be within the
944945
# bounds of the matrix. A large offset should just produce a size 0
945946
# diagonal in the last dimension (trace 0). See test_diagonal().
947+
out_dtype = x.dtype if dtype is None else dtype
946948
if offset < 0:
947949
diag_size = min(n, m, max(n + offset, 0))
948950
elif offset == 0:
@@ -954,8 +956,8 @@ def true_trace(x_stack, offset=0):
954956
x_stack_diag = [x_stack[i, i + offset] for i in range(diag_size)]
955957
else:
956958
x_stack_diag = [x_stack[i - offset, i] for i in range(diag_size)]
957-
result = xp.asarray(xp.stack(x_stack_diag) if x_stack_diag else [], dtype=x.dtype)
958-
return _array_module.sum(result)
959+
result = xp.asarray(xp.stack(x_stack_diag) if x_stack_diag else [], dtype=out_dtype)
960+
return _array_module.sum(result, dtype=dtype)
959961

960962

961963
_test_stacks(linalg.trace, x, **kw, res=res, dims=0, true_val=true_trace)

0 commit comments

Comments
 (0)