GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: Fix matvec/vecmat in-place aliasing (out=input produces zeros) by Ijtihed · Pull Request #31150 · numpy/numpy

Conversation

@Ijtihed

@Ijtihed Ijtihed commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

PR summary

Fixes #31144.

np.matvec(A, b, out=b) returns all zeros because matvec and vecmat are missing the ~NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE output flag that matmul already has. Without it the iterator ends up skipping the overlap check when out is the same array as an input so no temporary copy is made and BLAS gemv clobbers the input buffer.

This PR adds the same fix to matvec and vecmat and adds regression testing for both.

First time committer introduction

Hi, I'm Ijtihed Kilani (ijtihed.com); I'm an early-career software engineer based in Helsinki, Finland. I work mainly on physics-informed simulations for robotics and autonomy, currently at Supercell working on new games. I've used NumPy heavily for linear algebra in simulation code/studies.

AI Disclosure

This fix was made with help from Claude in Cursor. I used it to look over any assumptions I might have missed and draft the basic code changes which I then looked over, then rewrote and verified. All changes were reviewed and verified by me before submission.

Made with Cursor

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@Ijtihed
BUG: fix matvec/vecmat in-place aliasing (out=input produces zeros)
89f6cd0
Clear NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE on the output operand of
matvec and vecmat, matching the existing matmul fix. Without this,
the iterator skips the overlap copy when out aliases an input, and
BLAS gemv clobbers the input buffer.

Closes numpygh-31144

Made-with: Cursor
@Ijtihed Ijtihed mentioned this pull request Apr 4, 2026
BUG: matvec(A, b, out=b) zeros out b #31144
Closed
jorenham
jorenham reviewed Apr 4, 2026
View reviewed changes

@jorenham jorenham left a comment

Copy link
Copy Markdown
Member

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.

Thanks for your contribution.

Could you revert the file permission changes?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jorenham jorenham added the 00 - Bug label Apr 4, 2026
@Ijtihed
MAINT: revert unintended file permission changes
6882f23
Made-with: Cursor
@Ijtihed

Ijtihed commented Apr 4, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Thanks for your contribution.

Could you revert the file permission changes?

Done. I hope everything was done properly 😄

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tylerjereddy
tylerjereddy reviewed Apr 4, 2026
View reviewed changes
Comment thread numpy/_core/tests/test_multiarray.py Outdated
expected = np.vecmat(b, a)
c = np.vecmat(b, a, out=b)
assert_(c is b)
assert_equal(c, expected)

@tylerjereddy tylerjereddy Apr 4, 2026

Copy link
Copy Markdown
Contributor

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.

Both tests do indeed fail before and pass after the patch.

Note for other NumPy devs--the original problem was only reproducible for me on x86_64 Linux with OpenBLAS, not with ARM Mac/Accelerate, in case that's relevant.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/tests/test_multiarray.py Outdated
expected = np.matvec(a, b)
c = np.matvec(a, b, out=b)
assert_(c is b)
assert_equal(c, expected)

@tylerjereddy tylerjereddy Apr 4, 2026

Copy link
Copy Markdown
Contributor

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 suppose we could try to parametrize the two new tests, but the complexity of the argument reversal probably negates any potential simplification.

I guess these are both very similar to the original test_matmul_out() above.

Minor/debatable points might be preference for assert over the old assert_ that should no longer be needed in new tests, and maybe assert_allclose given we're comparing floats here (bit of a stretch that it matters, but if they do or could someday follow different codepaths...).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Ijtihed Ijtihed Apr 5, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

happy to switch to assert and assert_allclose if you think that might be better. I was (like you mentioned) just matching the style of test_matmul_out

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Ijtihed Ijtihed Apr 6, 2026

Copy link
Copy Markdown
Contributor Author

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.

should I make the edits or leave as is? im okay with either

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Apr 7, 2026

Copy link
Copy Markdown
Member

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.

You could for example parametrize it with a lambda A, x: np.vecmat(A, x) and lambda A, x: np.matvec(x, A).

I would also prefer if you move these to test_ufunc.py, there seem to be more tests for these there, and that file is at least slightly smaller.

But otherwise this looks good and really we could basically put it in as is.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Ijtihed reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@Ijtihed Ijtihed Apr 7, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

should be done now, thanks for the quick feedback

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@seberg seberg added the 09 - Backport-Candidate PRs tagged should be backported label Apr 7, 2026
Ijtihed added a commit to Ijtihed/numpy that referenced this pull request Apr 7, 2026
@Ijtihed
TST: parametrize matvec/vecmat out-aliasing test, move to test_ufunc
20c489a
Addresses reviewer feedback on numpygh-31150:
- Remove test_matvec_out/test_vecmat_out from test_multiarray.py
- Add parametrized test_matvec_vecmat_out in test_ufunc.py using lambdas
- Switch to bare assert and assert_allclose

Made-with: Cursor
@Ijtihed Ijtihed mentioned this pull request Apr 7, 2026
TST: parametrize matvec/vecmat out-aliasing test, move to test_ufunc #31180
Closed
@Ijtihed
TST: parametrize matvec/vecmat out-aliasing test, move to test_ufunc
2f17d06
Made-with: Cursor
seberg
seberg approved these changes Apr 7, 2026
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

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.

Thanks, LGTM now and an important fix if someone uses in-place operators here.
(Just waiting for CI.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Ijtihed reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@seberg
seberg merged commit d12adf8 into numpy:main Apr 7, 2026
93 of 94 checks passed

Uh oh!

There was an error while loading. Please reload this page.

charris pushed a commit to charris/numpy that referenced this pull request Apr 10, 2026
@Ijtihed @charris
BUG: Fix matvec/vecmat in-place aliasing (out=input produces zeros) (n…
e0f39f3
…umpy#31150)

`np.matvec(A, b, out=b)` returns all zeros because matvec and vecmat are missing the ~NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE output flag that matmul already has. Without it the iterator ends up skipping the overlap check when out is the same array as an input so no temporary copy is made and BLAS gemv clobbers the input buffer.

This PR adds the same fix to matvec and vecmat and adds regression testing for both.
@charris charris mentioned this pull request Apr 10, 2026
BUG: Fix matvec/vecmat in-place aliasing (out=input produces zeros) (#31150) #31208
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Apr 10, 2026
charris added a commit that referenced this pull request Apr 11, 2026
@charris
Merge pull request #31208 from charris/backport-31150
367faef
BUG: Fix matvec/vecmat in-place aliasing (out=input produces zeros) (#31150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jorenham jorenham jorenham left review comments
@tylerjereddy tylerjereddy tylerjereddy left review comments
@seberg seberg seberg approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

BUG: matvec(A, b, out=b) zeros out b

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@Ijtihed @seberg @jorenham @tylerjereddy @charris
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.