Skip to content

Commit 2b03ae6

Browse files
authored
Merge pull request numpy#15110 from tillahoffmann/expm1
MAINT: Fix expm1 instability for small complex numbers.
2 parents bba2bd2 + 2530847 commit 2b03ae6

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

numpy/core/src/umath/funcs.inc.src

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,9 @@ nc_exp2@c@(@ctype@ *x, @ctype@ *r)
360360
static void
361361
nc_expm1@c@(@ctype@ *x, @ctype@ *r)
362362
{
363-
@ftype@ a = npy_exp@c@(x->real);
364-
r->real = a*npy_cos@c@(x->imag) - 1.0@c@;
365-
r->imag = a*npy_sin@c@(x->imag);
363+
@ftype@ a = npy_sin@c@(x->imag / 2);
364+
r->real = npy_expm1@c@(x->real) * npy_cos@c@(x->imag) - 2 * a * a;
365+
r->imag = npy_exp@c@(x->real) * npy_sin@c@(x->imag);
366366
return;
367367
}
368368

numpy/core/tests/test_umath.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,12 @@ def test_special(self):
888888
assert_equal(ncu.expm1(np.inf), np.inf)
889889
assert_equal(ncu.expm1(-np.inf), -1.)
890890

891+
def test_complex(self):
892+
x = np.asarray(1e-12)
893+
assert_allclose(x, ncu.expm1(x))
894+
x = x.astype(np.complex128)
895+
assert_allclose(x, ncu.expm1(x))
896+
891897

892898
class TestHypot(object):
893899
def test_simple(self):

0 commit comments

Comments
 (0)