Skip to content

BUG: fix np.linalg.pinv(rtol=None) for integer input#31250

Open
erhan1209 wants to merge 1 commit intonumpy:mainfrom
erhan1209:fix/pinv-rtol-none-integer-input
Open

BUG: fix np.linalg.pinv(rtol=None) for integer input#31250
erhan1209 wants to merge 1 commit intonumpy:mainfrom
erhan1209:fix/pinv-rtol-none-integer-input

Conversation

@erhan1209
Copy link
Copy Markdown

@erhan1209 erhan1209 commented Apr 15, 2026

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

PR summary

Fix np.linalg.pinv(..., rtol=None) for integer-dtype inputs.

When rtol=None, pinv currently computes the default tolerance using
finfo(a.dtype).eps. That fails for integer arrays because finfo requires an
inexact 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 regression
test is included to verify that integer input with rtol=None behaves the same
as 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.pinv
fails on integer input when rtol=None. I traced the issue to the tolerance
calculation and put together a small fix with a regression test.

AI Disclosure

AI tool used: OpenAI Codex.

How it was used:

  • to inspect the local checkout and identify a suitable issue to fix
  • to propose and apply the code change
  • to draft the regression test
  • to help write this PR text

AI-generated content:

  • the code change in numpy/linalg/_linalg.py
  • the regression test added to numpy/linalg/tests/test_linalg.py
  • this PR description text

All review, validation, and final submission decisions are made by me.

## 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: numpy.linalg.pinv crashes with ValueError when called with rtol=None on an integer-dtype array [DeepTest]

1 participant