-
-
Notifications
You must be signed in to change notification settings - Fork 12.1k
API: Add rtol to matrix_rank and stable [Array API]
#25437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| `numpy.linalg.martrix_rank`, `numpy.sort` and `numpy.argsort` new parameters | ||
| ---------------------------------------------------------------------------- | ||
|
|
||
| New keyword parameters were added to improve array API compatibility: | ||
|
|
||
| * ``rtol`` keyword parameter was added to `numpy.linalg.martrix_rank`. | ||
|
|
||
| * ``stable`` keyword parameter was added to `numpy.sort` and `numpy.argsort`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1965,12 +1965,12 @@ def cond(x, p=None): | |
| return r | ||
|
|
||
|
|
||
| def _matrix_rank_dispatcher(A, tol=None, hermitian=None): | ||
| def _matrix_rank_dispatcher(A, tol=None, hermitian=None, *, rtol=None): | ||
| return (A,) | ||
|
|
||
|
|
||
| @array_function_dispatch(_matrix_rank_dispatcher) | ||
| def matrix_rank(A, tol=None, hermitian=False): | ||
| def matrix_rank(A, tol=None, hermitian=False, *, rtol=None): | ||
| """ | ||
| Return matrix rank of array using SVD method | ||
|
|
||
|
|
@@ -1998,6 +1998,11 @@ def matrix_rank(A, tol=None, hermitian=False): | |
| Defaults to False. | ||
|
|
||
| .. versionadded:: 1.14 | ||
| rtol : (...) array_like, float, optional | ||
| Parameter for the relative tolerance component. Only ``tol`` or | ||
| ``rtol`` can be set at a time. Defaults to ``max(M, N) * eps``. | ||
|
|
||
| .. versionadded:: 2.0.0 | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -2067,12 +2072,12 @@ def matrix_rank(A, tol=None, hermitian=False): | |
| if A.ndim < 2: | ||
| return int(not all(A == 0)) | ||
| S = svd(A, compute_uv=False, hermitian=hermitian) | ||
| if rtol is not None and tol is not None: | ||
| raise ValueError("`tol` and `rtol` can't be both set.") | ||
| if rtol is None: | ||
| rtol = max(A.shape[-2:]) * finfo(S.dtype).eps | ||
| if tol is None: | ||
| tol = ( | ||
| S.max(axis=-1, keepdims=True) * | ||
| max(A.shape[-2:]) * | ||
| finfo(S.dtype).eps | ||
| ) | ||
| tol = S.max(axis=-1, keepdims=True) * rtol | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this logic is incorrect. >>> import numpy as np
>>> x = np.zeros((4, 3, 2))
>>> rtol = np.zeros((4,))
>>> np.linalg.matrix_rank(x, tol=rtol)
array([0, 0, 0, 0])
>>> np.linalg.matrix_rank(x, rtol=rtol)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/aaronmeurer/Documents/numpy/numpy/linalg/_linalg.py", line 2083, in matrix_rank
return count_nonzero(S > tol, axis=-1)
^^^^^^^
ValueError: operands could not be broadcast together with shapes (4,2) (4,4)The broadcasting for
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's right - Here's a fix for it: #25877 |
||
| else: | ||
| tol = asarray(tol)[..., newaxis] | ||
| return count_nonzero(S > tol, axis=-1) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fact that this changes the default, does need it is own release note, as it is a breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this was not addressed, still need a release note nothing the BC break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it changes the default.
Before default
tolwas defined as:And right now it's:
As
rtol=Noneandtol=Noneby default, then the default didn't change. Or am I missing something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, sorry... it does change for
pinv, but not matrix rank.