BUG: fix np.linalg.pinv(rtol=None) for integer input#31250
Open
erhan1209 wants to merge 1 commit intonumpy:mainfrom
Open
BUG: fix np.linalg.pinv(rtol=None) for integer input#31250erhan1209 wants to merge 1 commit intonumpy:mainfrom
erhan1209 wants to merge 1 commit intonumpy:mainfrom
Conversation
## Summary - fix `np.linalg.pinv(..., rtol=None)` for integer-dtype inputs - compute the default tolerance from the promoted inexact linalg dtype instead of the original integer dtype - add a regression test covering integer input with `rtol=None` ## Testing - `python -m py_compile numpy/linalg/_linalg.py numpy/linalg/tests/test_linalg.py` - verified the patched `pinv` returns the same result as default `pinv(a)` for an `int32` matrix
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
np.linalg.pinv(..., rtol=None)for integer-dtype inputsrtol=NoneTesting
python -m py_compile numpy/linalg/_linalg.py numpy/linalg/tests/test_linalg.pypinvreturns the same result as defaultpinv(a)for anint32matrixPR summary
Fix
np.linalg.pinv(..., rtol=None)for integer-dtype inputs.When
rtol=None,pinvcurrently computes the default tolerance usingfinfo(a.dtype).eps. That fails for integer arrays becausefinforequires aninexact dtype, so calls such as
np.linalg.pinv(np.array([[1, 2], [3, 4]], dtype=np.int32), rtol=None)raise
ValueError.This PR computes the default tolerance from the promoted inexact linalg dtype
instead, which matches the actual computation path used by
pinv. A regressiontest is included to verify that integer input with
rtol=Nonebehaves the sameas the default
pinv(a)call.Closes gh-30917.
First time committer introduction
I’m a NumPy user contributing this fix after noticing that
np.linalg.pinvfails on integer input when
rtol=None. I traced the issue to the tolerancecalculation and put together a small fix with a regression test.
AI Disclosure
AI tool used: OpenAI Codex.
How it was used:
AI-generated content:
numpy/linalg/_linalg.pynumpy/linalg/tests/test_linalg.pyAll review, validation, and final submission decisions are made by me.