Skip to content

Commit bc4fd56

Browse files
eirikurjA-CGray
andauthored
Update abs complex safe routine for numpy2 (#13)
* fix for numpy2 * bump version * format * Simplify array abs --------- Co-authored-by: Alasdair Gray <alachris@umich.edu>
1 parent c4b5323 commit bc4fd56

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

complexify/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.1"
1+
__version__ = "0.1.2"

complexify/cs.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@
55

66
import numpy as np
77

8+
if np.__version__[0] == "2":
9+
NumPy2 = True
10+
else:
11+
NumPy2 = False
12+
813

914
def abs(x): # noqa: A001
1015
"""
11-
complex-step safe version of numpy.abs function.
16+
Complex-step safe version of numpy.abs function.
1217
1318
Parameters
1419
----------
1520
x : ndarray
16-
array value to be computed on
21+
Array value to be computed on.
1722
1823
Returns
1924
-------
2025
ndarray
26+
Absolute value.
2127
"""
2228
if isinstance(x, np.ndarray):
23-
return x * np.sign(x)
29+
return np.where(x.real < 0.0, -x, x)
2430
elif x.real < 0.0:
2531
return -x
2632
return x

0 commit comments

Comments
 (0)