FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ENH: Return rank 0 for empty matrices in matrix_rank (#30422) by charris · Pull Request #31197 · numpy/numpy · GitHub

/ numpy Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 2 additions & 1 deletion numpy/linalg/_linalg.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2130,14 +2130,15 @@ def matrix_rank(A, tol=None, hermitian=False, *, rtol=None):
A = asarray(A)
if A.ndim < 2:
return int(not all(A == 0))

S = svd(A, compute_uv=False, hermitian=hermitian)

if tol is None:
if rtol is None:
rtol = max(A.shape[-2:]) * finfo(S.dtype).eps
else:
rtol = asarray(rtol)[..., newaxis]
tol = S.max(axis=-1, keepdims=True) * rtol
tol = S.max(axis=-1, keepdims=True, initial=0) * rtol
else:
tol = asarray(tol)[..., newaxis]

Expand Down
19 changes: 19 additions & 0 deletions numpy/linalg/tests/test_linalg.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2440,3 +2440,22 @@ def test_vector_norm_empty():
assert_equal(np.linalg.vector_norm(x, ord=1), 0)
assert_equal(np.linalg.vector_norm(x, ord=2), 0)
assert_equal(np.linalg.vector_norm(x, ord=np.inf), 0)

def test_empty_matrix_rank():
assert_equal(matrix_rank(np.zeros((0, 0))), 0)
assert_equal(matrix_rank(np.zeros((0, 5))), 0)
assert_equal(matrix_rank(np.zeros((5, 0))), 0)

result = matrix_rank(np.zeros((0, 5, 5)))
assert_equal(result.shape, (0,))
assert_equal(result.dtype, np.intp)

result = matrix_rank(np.zeros((3, 0, 5)))
assert_equal(result, np.array([0, 0, 0]))

result = matrix_rank(np.zeros((2, 5, 0)))
assert_equal(result, np.array([0, 0]))

result = matrix_rank(np.zeros((2, 3, 0, 4)))
assert_equal(result.shape, (2, 3))
assert_equal(result, np.zeros((2, 3), dtype=np.intp))
Loading

Back | FazBrowse Home | New Git URL