summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTill Hoffmann <till@spotify.com>2019-12-15 17:04:38 +0000
committerTill Hoffmann <till@spotify.com>2019-12-16 09:58:25 +0000
commit5d9cab95e6b955c400835c31c2769625d0234c03 (patch)
treea6a496428a6c681ed35764daaf840afee6ab78ea
parentbba2bd2012c6e1726e455c50995b685d12773c22 (diff)
downloadnumpy-5d9cab95e6b955c400835c31c2769625d0234c03.tar.gz
BUG: Fix expm1 instability for small complex nums.
-rw-r--r--numpy/core/src/umath/funcs.inc.src6
-rw-r--r--numpy/core/tests/test_umath.py6
2 files changed, 9 insertions, 3 deletions
diff --git a/numpy/core/src/umath/funcs.inc.src b/numpy/core/src/umath/funcs.inc.src
index 10ed66e50..397d2c1aa 100644
--- a/numpy/core/src/umath/funcs.inc.src
+++ b/numpy/core/src/umath/funcs.inc.src
@@ -360,9 +360,9 @@ nc_exp2@c@(@ctype@ *x, @ctype@ *r)
static void
nc_expm1@c@(@ctype@ *x, @ctype@ *r)
{
- @ftype@ a = npy_exp@c@(x->real);
- r->real = a*npy_cos@c@(x->imag) - 1.0@c@;
- r->imag = a*npy_sin@c@(x->imag);
+ @ftype@ a = npy_cos@c@(x->imag);
+ r->real = npy_expm1@c@(x->real) * a + (a - 1);
+ r->imag = npy_exp@c@(x->real) * npy_sin@c@(x->imag);
return;
}
diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py
index e892e81d2..ae1090c23 100644
--- a/numpy/core/tests/test_umath.py
+++ b/numpy/core/tests/test_umath.py
@@ -888,6 +888,12 @@ class TestExpm1(object):
assert_equal(ncu.expm1(np.inf), np.inf)
assert_equal(ncu.expm1(-np.inf), -1.)
+ def test_complex(self):
+ x = np.asarray(1e-12)
+ assert_allclose(x, ncu.expm1(x))
+ x = x.astype(np.complex128)
+ assert_allclose(x, ncu.expm1(x))
+
class TestHypot(object):
def test_simple(self):