diff --git a/doc/release/upcoming_changes/24818.deprecation.rst b/doc/release/upcoming_changes/24818.deprecation.rst new file mode 100644 index 000000000000..fdc8bd4060e1 --- /dev/null +++ b/doc/release/upcoming_changes/24818.deprecation.rst @@ -0,0 +1,2 @@ +Arrays of 2-dimensional vectors for ``np.cross`` have been deprecated. +Use arrays of 3-dimensional vectors instead. diff --git a/numpy/_core/numeric.py b/numpy/_core/numeric.py index 6c3a3aae9ac7..50229151d6a4 100644 --- a/numpy/_core/numeric.py +++ b/numpy/_core/numeric.py @@ -1597,6 +1597,13 @@ def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None): "(dimension must be 2 or 3)") if a.shape[-1] not in (2, 3) or b.shape[-1] not in (2, 3): raise ValueError(msg) + if a.shape[-1] == 2 or b.shape[-1] == 2: + # Deprecated in NumPy 2.0, 2023-09-26 + warnings.warn( + "Arrays of 2-dimensional vectors are deprecated. Use arrays of " + "3-dimensional vectors instead. (deprecated in NumPy 2.0)", + DeprecationWarning, stacklevel=2 + ) # Create the output array shape = broadcast(a[..., 0], b[..., 0]).shape diff --git a/numpy/_core/tests/test_numeric.py b/numpy/_core/tests/test_numeric.py index 8668b96bf708..1887c1332779 100644 --- a/numpy/_core/tests/test_numeric.py +++ b/numpy/_core/tests/test_numeric.py @@ -3573,6 +3573,9 @@ def test_array_likes(self): class TestCross: + @pytest.mark.filterwarnings( + "ignore:.*2-dimensional vectors.*:DeprecationWarning" + ) def test_2x2(self): u = [1, 2] v = [3, 4] @@ -3582,6 +3585,9 @@ def test_2x2(self): cp = np.cross(v, u) assert_equal(cp, -z) + @pytest.mark.filterwarnings( + "ignore:.*2-dimensional vectors.*:DeprecationWarning" + ) def test_2x3(self): u = [1, 2] v = [3, 4, 5] @@ -3600,6 +3606,9 @@ def test_3x3(self): cp = np.cross(v, u) assert_equal(cp, -z) + @pytest.mark.filterwarnings( + "ignore:.*2-dimensional vectors.*:DeprecationWarning" + ) def test_broadcasting(self): # Ticket #2624 (Trac #2032) u = np.tile([1, 2], (11, 1)) @@ -3630,6 +3639,9 @@ def test_broadcasting(self): assert_equal(np.cross(v.T, u), -z) assert_equal(np.cross(u, u), 0) + @pytest.mark.filterwarnings( + "ignore:.*2-dimensional vectors.*:DeprecationWarning" + ) def test_broadcasting_shapes(self): u = np.ones((2, 1, 3)) v = np.ones((5, 3))