Skip to content

Commit

Permalink
BUG: Adds asanyarray to start of linalg.cross (#26667)
Browse files Browse the repository at this point in the history
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 #26640, with a bug fix requested.
  • Loading branch information
bmwoodruff authored and charris committed Jun 15, 2024
1 parent 8736d81 commit b31e195
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions numpy/linalg/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,9 @@ def cross(x1, x2, /, *, axis=-1):
numpy.cross
"""
x1 = asanyarray(x1)
x2 = asanyarray(x2)

if x1.shape[axis] != 3 or x2.shape[axis] != 3:
raise ValueError(
"Both input arrays must be (arrays of) 3-dimensional vectors, "
Expand Down
8 changes: 8 additions & 0 deletions numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2307,6 +2307,14 @@ def test_cross():

assert_equal(actual, expected)

# We test that lists are converted to arrays.
u = [1, 2, 3]
v = [4, 5, 6]
actual = np.linalg.cross(u, v)
expected = array([-3, 6, -3])

assert_equal(actual, expected)

with assert_raises_regex(
ValueError,
r"input arrays must be \(arrays of\) 3-dimensional vectors"
Expand Down

0 comments on commit b31e195

Please sign in to comment.