Skip to content

BUG: np.cross() warns about arrays of vectors when used with two simple 2-d vectors #26620

@megies

Description

@megies

Describe the issue:

np.cross() warns about arrays of vectors when used with simply two 2-d vectors (this use case is even in the "Examples" section of np.cross() docstring). I believe, that check whether a warning should be shown is missing a check for number of dimensions of the input.

numpy/numpy/_core/numeric.py

Lines 1645 to 1653 in 42e9aa2

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
)

I feel like there should be a change, either

  • change the warning message to something like '2-dimensional vectors are deprecated. Use 3-dimensional vectors instead.' without the 'Arrays of..' parts, or..
  • the check whether the warning should be shown or not should also check if a.ndim > 1 or b.ndim > 1

Tbh, I'm a bit confused what that warning is implying as recommended usage anyway. Is the user supposed to add an all-zero third dimension for 2-d vectors and then calculate cross product in 3-d and then extract the valid part from it? That seems kinda odd-looking, I guess if that really is the case it looks like this prompts to look at #13718 again potentially?

So instead of..

x = [[1, 2], [1, 2]]
y = [[4, 5], [4, 5]]
result = np.cross(x, y)

user should do something like..?

x = [[1, 2, 0], [1, 2, 0]]
y = [[4, 5, 0], [4, 5, 0]]
result = np.cross(x, y)[:, -1]

Reproduce the code example:

import numpy as np

x = [1, 2]
y = [4, 5]
# the next line shows a warning, even though it's even from the docstring examples section
np.cross(x, y)

Error message:

DeprecationWarning: Arrays of 2-dimensional vectors are deprecated. Use arrays of 3-dimensional vectors instead. (deprecated in NumPy 2.0)

Python and NumPy Versions:

2.0.0rc2
3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0]

Runtime Environment:

[{'numpy_version': '2.0.0rc2',
  'python': '3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) '
            '[GCC 12.3.0]',
  'uname': uname_result(system='Linux', node='mother', release='5.10.0-25-amd64', version='#1 SMP Debian 5.10.191-1 (2023-08-16)', machine='x86_64')},
 {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                      'found': ['SSSE3',
                                'SSE41',
                                'POPCNT',
                                'SSE42',
                                'AVX',
                                'F16C',
                                'FMA3',
                                'AVX2',
                                'AVX512F',
                                'AVX512CD',
                                'AVX512_SKX',
                                'AVX512_CLX',
                                'AVX512_CNL',
                                'AVX512_ICL'],
                      'not_found': ['AVX512_KNL', 'AVX512_KNM', 'AVX512_SPR']}},
 {'architecture': 'SkylakeX',
  'filepath': '/home/megies/miniconda3/envs/np2/lib/libopenblasp-r0.3.27.so',
  'internal_api': 'openblas',
  'num_threads': 8,
  'prefix': 'libopenblas',
  'threading_layer': 'pthreads',
  'user_api': 'blas',
  'version': '0.3.27'}]

Context for the issue:

In our testsuite, we also have a test runner with pytest -W error to make sure our code is not causing deprecation warnings in order to keep our code base work with upcoming releases of dependencies. And that's where this deprecation warning came up.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions