Skip to content

Commit c98c14b

Browse files
RobPasMuepyansys-ci-botpre-commit-ci[bot]
authored
fix: handle properly np.cross() - 2d ops deprecated in Numpy 2.X (#1419)
Co-authored-by: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent e8017a6 commit c98c14b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

doc/changelog.d/1419.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
handle properly ``np.cross()`` - 2d ops deprecated in Numpy 2.X

src/ansys/geometry/core/math/vector.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
from ansys.geometry.core.misc.units import UNITS
3636
from ansys.geometry.core.typing import Real, RealSequence
3737

38+
_NUMPY_MAJOR_VERSION = int(np.__version__[0])
39+
"""Major version of the numpy library."""
40+
3841

3942
class Vector3D(np.ndarray):
4043
"""Provides for managing and creating a 3D vector.
@@ -190,7 +193,7 @@ def get_angle_between(self, v: "Vector3D") -> Quantity:
190193

191194
@check_input_types
192195
def cross(self, v: "Vector3D") -> "Vector3D":
193-
"""Get the cross product of ``Vector3D`` objects."""
196+
"""Return the cross product of ``Vector3D`` objects."""
194197
return np.cross(self, v).view(Vector3D)
195198

196199
@check_input_types
@@ -323,9 +326,16 @@ def is_zero(self) -> bool:
323326
return all([comp == 0 for comp in self])
324327

325328
@check_input_types
326-
def cross(self, v: "Vector2D"):
329+
def cross(self, v: "Vector2D") -> Real:
327330
"""Return the cross product of ``Vector2D`` objects."""
328-
return np.cross(self, v)
331+
if _NUMPY_MAJOR_VERSION >= 2:
332+
# See https://github.com/numpy/numpy/issues/26620 and more specifically
333+
# https://github.com/numpy/numpy/issues/26620#issuecomment-2150748569
334+
return self[..., 0] * v[..., 1] - self[..., 1] * v[..., 0]
335+
else: # pragma: no cover
336+
# Coverage is measured with the latest version of numpy
337+
# so this code is not covered
338+
return np.cross(self, v)
329339

330340
@check_input_types
331341
def is_perpendicular_to(self, other_vector: "Vector2D") -> bool:
@@ -415,7 +425,7 @@ def __sub__(self, other: "Vector2D") -> "Vector2D":
415425
"""Subtraction operation overload for 2D vectors."""
416426
return np.subtract(self, other).view(Vector2D)
417427

418-
def __mod__(self, other: "Vector2D") -> "Vector2D":
428+
def __mod__(self, other: "Vector2D") -> Real:
419429
"""Overload % operator with cross product."""
420430
return self.cross(other)
421431

0 commit comments

Comments
 (0)