8000 BUG: Increase accuracy of log1p for small inputs by mfkasim1 · Pull Request #22611 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Increase accuracy of log1p for small inputs #22611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
Prev Previous commit
Next Next commit
BUG: Handle overflow and underflow for log1p complex
  • Loading branch information
mfkasim1 committed Nov 18, 2022
commit c57b5a2fecefac096c0bf01fae3a7293f90be621
19 changes: 17 additions & 2 deletions numpy/core/src/umath/funcs.inc.src
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,24 @@ nc_log1p@c@(@ctype@ *x, @ctype@ *r)
{
@ftype@ xx = x->real;
@ftype@ yy = x->imag;
@ftype@ z = xx * (2 + xx) + yy * yy;
r->imag = npy_atan2@c@(yy, xx + 1);
r->real = 0.5 * npy_log1p@c@(z);

@ftype@ z0 = npy_hypot@c@(xx + 1, yy);

// special case for inputs close to 1
if (z0 < 1.5) {
@ftype@ z = xx * (2 + xx) + yy * yy;
if (z == 0) {
// handle underflow
r->real = xx;
}
else {
r->real = 0.5 * npy_log1p@c@(z);
}
}
else {
r->real = npy_log@c@(z0);
}
return;
}

Expand Down
12 changes: 11 additions & 1 deletion numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -1815,13 +1815,23 @@ def test_log1p_complex(self):
# these numbers are obtained from wolfram alpha
assert_almost_equal(ncu.log1p(1e-18 + 0.1j), 0.00497517 + 0.0996687j)
assert_almost_equal(ncu.log1p(0.1 + 1e-18j), 0.0953102 + 9.09091e-19j)
assert_almost_equal(ncu.log1p(0.5 + 0j), 0.40546510810816 + 0j)
assert_almost_equal(ncu.log1p(0.0 + 0.5j), 0.11157177565710 + 0.46364760900081j)

def test_extreme(self):
# these numbers are obtained from WarrenWeckesser and double checked with wolfram alpha
assert_almost_equal(ncu.log1p(-1 + 1e250j), 575.6462732485114 + 1.5707963267948966j)
assert_almost_equal(ncu.log1p(1e250 + 1j), 575.6462732485114 + 1e-250j)
assert_almost_equal(ncu.log1p(1e250 + 1e250j), 575.9928468387914 + 0.7853981633974483j)
assert_almost_equal(ncu.log1p(1e-250 + 1e250j), 575.6462732485114 + 1.5707963267948966j)
assert_almost_equal(ncu.log1p(1e-250 + 2e-250j), 1e-250 + 2e-250j)
assert_almost_equal(ncu.log1p(1e250 + 1e-250j), 575.6462732485114 + 0.0j)

def test_special(self):
with np.errstate(invalid="ignore", divide="ignore"):
assert_equal(ncu.log1p(np.nan + 1j), np.nan + np.nan * 1j)
assert_equal(ncu.log1p(np.inf + 1j), np.inf + 0j)
assert_equal(ncu.log1p(-np.inf + 1j), np.inf + np.pi * 1j)
assert_equal(ncu.log1p(np.inf * 1j), np.nan + np.nan * 1j)
assert_equal(ncu.log1p(-1 + 0j), -np.inf + 0j)

class TestExpm1:
Expand Down
0