Skip to content
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

BUG: Adds asanyarray to start of linalg.cross #26667

Merged
merged 1 commit into from
Jun 14, 2024

Conversation

bmwoodruff
Copy link
Member

Currently linalg.cross fails when given two 3D lists. This adds asanyarray at the start of the code,
mimicking the other Array API compatible additions. This was discussed in PR #26640, with a bug fix requested.

Currently linalg.cross fails when given two 3D lists.
This adds `asanyarray` at the start of the code,
mimicing the other Array API compatible additions.
This was discussed in PR numpy#26640, with a bug fix requested.
@bmwoodruff bmwoodruff marked this pull request as draft June 12, 2024 09:11
@bmwoodruff
Copy link
Member Author

The function linalg.cross has a current bug. With the current development version, we get

>>> u = [1, 2, 3]
>>> v = [4, 5, 6]
>>> np.linalg.cross(u,v)
...
AttributeError: 'list' object has no attribute 'shape'

It's missing an asanyarray or asarray before asking for the shape. Which is preferrable?

After either change is made, we get:

>>> u = [1, 2, 3]
>>> v = [4, 5, 6]
>>> np.linalg.cross(u,v)
array([-3,  6, -3])

I also addded a test to check that lists are converted properly to arrays.

There is another issue happening, namely an IndexError can still occur, rather than an AxisError. Compare the behavoir of linalg.cross and cross.

>>> u = np.asarray([1, 2, 3])
>>> v = np.asarray([4, 5, 6])
>>> np.linalg.cross(u, v, axis=-2)
...
IndexError: tuple index out of range
>>> u = np.asarray([1, 2, 3])
>>> v = np.asarray([4, 5, 6])
>>> np.cross(u, v, axis=-2)
...
AxisError: axisa: axis -2 is out of bounds for array of dimension 1

I'm not sure if we want to call this a bug, or a feature, that different error messages are thrown. I had to deal with this while creating linalg.cross2d and noticed the issue. The issue comes by not using normalize_axes_index in linalg.cross before trying to access the shape. Fixing it would result in calling normalize_axis_index twice (as the check is done in cross).

I can addreses the inconsistency of error messages in this PR, or a separate, or just call it a feature. Either way, a user is being told they have an indexing issue, though I think the AxisError message is more informative.

Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and seems to work as advertised now. Thanks @bmwoodruff!

It's missing an asanyarray or asarray before asking for the shape. Which is preferrable?

It doesn't seem to matter either way. The code in _linalg.py uses a mix, so it seems to not have a consistent design. The return type is an ndarray in any case, subclasses don't get preserved.

The issue comes by not using normalize_axes_index in linalg.cross before trying to access the shape. Fixing it would result in calling normalize_axis_index twice (as the check is done in cross).

Consistency would be nice, AxisError seems better. It's less critical though, so let's do it in a separate PR after this one (can be backported, but not critical for 2.0.0, landing in 2.0.1 is fine). Calling normalize_axes_index twice doesn't sound like the end of the world, it's cheap.

@rgommers rgommers merged commit b52814f into numpy:main Jun 14, 2024
68 of 69 checks passed
@rgommers rgommers added this to the 2.1.0 release milestone Jun 14, 2024
@rgommers rgommers added the 09 - Backport-Candidate PRs tagged should be backported label Jun 14, 2024
@rgommers
Copy link
Member

@charris this one would be nice to backport for 2.0.0

charris pushed a commit to charris/numpy that referenced this pull request Jun 15, 2024
Currently linalg.cross fails when given two 3D lists.
This adds `asanyarray` at the start of the code,
mimicing the other Array API compatible additions.
This was discussed in PR numpy#26640, with a bug fix requested.
@charris charris removed component: numpy.linalg 09 - Backport-Candidate PRs tagged should be backported labels Jun 15, 2024
charris added a commit that referenced this pull request Jun 15, 2024
BUG: Adds asanyarray to start of linalg.cross (#26667)
ArvidJB pushed a commit to ArvidJB/numpy that referenced this pull request Nov 1, 2024
Currently linalg.cross fails when given two 3D lists.
This adds `asanyarray` at the start of the code,
mimicing the other Array API compatible additions.
This was discussed in PR numpy#26640, with a bug fix requested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants