API: Adjust linalg.pinv and linalg.cholesky to Array API#25388
API: Adjust linalg.pinv and linalg.cholesky to Array API#25388ngoldbaum merged 1 commit intonumpy:mainfrom
linalg.pinv and linalg.cholesky to Array API#25388Conversation
a0e0fdf to
c56cc1b
Compare
c0c5104 to
cfcf694
Compare
numpy/linalg/_linalg.py
Outdated
| (symmetric if real-valued) and positive-definite. No checking is performed | ||
| to verify whether `a` is Hermitian or not. In addition, only the lower or | ||
| upper-triangular and diagonal elements of `a` are used. Only `L` or `U` | ||
| is actually returned. |
There was a problem hiding this comment.
I think all of these should be double backtick quotes, since they're not cross-references to numpy types.
There was a problem hiding this comment.
Right, I formatted them.
| -------------------------------------------------------------- | ||
|
|
||
| ``upper`` keyword parameter was added to `numpy.linalg.cholesky` and | ||
| ``rtol`` keyword parameter to `numpy.linalg.pinv` for Array API compatibility. |
There was a problem hiding this comment.
Weren't both added for array API compat?
I'd also mention that rtol doesn't have the array API default.
There was a problem hiding this comment.
That's correct, both of these were added to array-api-compat.
rtol doesn't have the default value because default for pinv is always taken from rcond (for backward compatibility) - I added a note about it.
There was a problem hiding this comment.
I'm trying to say it reads like only rtol was added for compat now. Maybe something like:
The ``upper`` and ``rtol`` keyword parameters were added to
`numpy.linalg.colesky` and `numpy.lingalg.ping`, respectively, to
improve array API compatibility.
There was a problem hiding this comment.
Ah, now I got it - I updated it.
numpy/linalg/_linalg.py
Outdated
| a, wrap = _makearray(a) | ||
| if rtol is not None: | ||
| if rcond != 1e-15: | ||
| raise ValueError("`rtol` and `rcond` can't be both set.") |
There was a problem hiding this comment.
What if someone sets rcond=1e-15 and rtol != None. In that case rtol would be ignored. Of course that's a pretty unusual thing to happen.
Still, it might be better to change the default value of rcond to None, and if both are unset, assume the user meant rcond=1e-15 to preserve backward compatibility. Also document that behavior.
There was a problem hiding this comment.
Or error if both are set to non-default values and leave the default alone.
There was a problem hiding this comment.
Sure! Changed to rcond=None.
cfcf694 to
a220d6a
Compare
mhvk
left a comment
There was a problem hiding this comment.
One little buglet and a comment.
Bit more generally, might it make sense to move towards the array API and deprecate rcond? (maybe just a FutureDeprecationWarning or so).
Also, might it be an idea to use rtol=np._NoValue as the default for now, to allow passing in rtol=None to opt in to the array API default?
Not sure about either, to be honest.
numpy/linalg/_linalg.py
Outdated
| over='ignore', divide='ignore', under='ignore'): | ||
| r = gufunc(a, signature=signature) | ||
| if upper: | ||
| r = r.T.conj() |
There was a problem hiding this comment.
This is only correct for 2-d arrays, while I think the input can be a stack of matrices. Replace with
r = matrix_transpose(r).conj()
numpy/linalg/_linalg.py
Outdated
| rtol : (...) array_like of float, optional | ||
| Same as `rcond`, but it's an Array API compatible parameter name. | ||
| Only `rcond` or `rtol` can be set at a time. If none of them are | ||
| provided then ``1e-15`` default is used. |
There was a problem hiding this comment.
Needs a .. versionadded:: 2.0.0
There was a problem hiding this comment.
Should note here that if None is passed in, the API standard default is used.
294e51b to
3f5cf73
Compare
@mhvk In my opinion it's a good idea - I added a PendingDeprecationWarning when |
| over='ignore', divide='ignore', under='ignore'): | ||
| r = gufunc(a, signature=signature) | ||
| if upper: | ||
| r = matrix_transpose(r).conj() |
There was a problem hiding this comment.
Now wondering about this, since this makes a copy of a potentially large matrix: an alternative would be to actually define cholesky_up (like done for eigenvalues, etc.), i.e., creating a new gufunc by adding to
numpy/numpy/linalg/umath_linalg.cpp
Lines 1988 to 1993 in 0032ede
numpy/numpy/linalg/umath_linalg.cpp
Line 4178 in 0032ede
numpy/numpy/linalg/umath_linalg.cpp
Lines 4390 to 4399 in 0032ede
There was a problem hiding this comment.
If it's Ok, let me create later a separate issue/PR that introduces a new ufunc.
There was a problem hiding this comment.
Sure, it is fine to have the support now and improve it later!
mhvk
left a comment
There was a problem hiding this comment.
Beyond the larger separate comment about maybe creating the appropriate gufunc to avoid copies, a few nits in-line.
numpy/linalg/_linalg.py
Outdated
| rtol : (...) array_like of float, optional | ||
| Same as `rcond`, but it's an Array API compatible parameter name. | ||
| Only `rcond` or `rtol` can be set at a time. If none of them are | ||
| provided then ``1e-15`` default is used. |
There was a problem hiding this comment.
Should note here that if None is passed in, the API standard default is used.
| improve array API compatibility. | ||
|
|
||
| For `numpy.linalg.pinv` if neither ``rcond`` nor ``rtol`` is specified, | ||
| the ``rcond``'s default is used. |
There was a problem hiding this comment.
Maybe worth stating that eventually we hope to deprecate rcond?
numpy/linalg/tests/test_linalg.py
Outdated
| def test_pinv_rtol_arg(): | ||
| a = np.array([[1, 2, 3], [4, 1, 1], [2, 3, 1]]) | ||
|
|
||
| with suppress_warnings() as sup: |
There was a problem hiding this comment.
Since the warning is intensional, I'd do
with pytest.warns(PendingDeprecationWarning, match="In the future `rcond`"):
There was a problem hiding this comment.
As the warning got removed it's not needed.
linalg.pinv and linalg.cholesky to Array APIlinalg.pinv and linalg.cholesky to Array API
3f5cf73 to
32e8eac
Compare
700a78f to
dfe7fd6
Compare
|
Thanks @mtsokol! |
Hi @rgommers @ngoldbaum,
This PR is (one of) the last Array API compatibility changes:
rtolparameter added tonp.linalg.pinvas an alternative torcondfor bc (both can't be provided at the same time) (rtolwill still diverge from Array API wrt. default value but at least it can be passed as a keyword argument)upperparameter added tonp.linalg.choleskyarray-api-skips.txtto remove redundant skips