Skip to content
Merged
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
13 changes: 5 additions & 8 deletions array_api_tests/hypothesis_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,14 @@ def invertible_matrices(draw, dtypes=floating_dtypes, stack_shapes=shapes()):
stack_shape = draw(stack_shapes)
n = draw(integers(0, SQRT_MAX_ARRAY_SIZE // max(math.prod(stack_shape), 1)),)
dtype = draw(dtypes)
elements = one_of(
from_dtype(dtype, min_value=0.5, allow_nan=False, allow_infinity=False),
from_dtype(dtype, max_value=-0.5, allow_nan=False, allow_infinity=False),
elements = one_of( # avoid extreme condition numbers
from_dtype(dtype, min_value=0.5, max_value=50,
allow_nan=False, allow_infinity=False),
from_dtype(dtype, max_value=-0.5, min_value=-50,
allow_nan=False, allow_infinity=False),
)
d = draw(arrays(dtype, shape=(*stack_shape, 1, n), elements=elements))

# Functions that require invertible matrices may do anything when it is
# singular, including raising an exception, so we make sure the diagonals
# are sufficiently nonzero to avoid any numerical issues.
assert xp.all(xp.abs(d) >= 0.5)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xp.all(xp.abs(d) >= 0.5) was guaranteed by the definition of elements, yet this alone did not avoid numerical issues, so I went ahead and removed it. With the change above, I think the worst condition number possible is 100.


diag_mask = xp.arange(n) == xp.reshape(xp.arange(n), (n, 1))
return xp.where(diag_mask, d, xp.zeros_like(d))

Expand Down
3 changes: 2 additions & 1 deletion array_api_tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ def test_matrix_norm(x, kw):
_test_stacks(linalg.matrix_norm, x, **kw, dims=2 if keepdims else 0,
res=res)

matrix_power_n = shared(integers(-100, 100), key='matrix_power n')
# use moderate range of `n` to avoid numerical difficulties
matrix_power_n = shared(integers(-10, 10), key='matrix_power n')
@pytest.mark.unvectorized
@pytest.mark.xp_extension('linalg')
@given(
Expand Down
Loading