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 5 commits
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
23 changes: 20 additions & 3 deletions numpy/core/src/umath/funcs.inc.src
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,26 @@ nc_log@c@(@ctype@ *x, @ctype@ *r)
static void
nc_log1p@c@(@ctype@ *x, @ctype@ *r)
{
@ftype@ l = npy_hypot@c@(x->real + 1,x->imag);
r->imag = npy_atan2@c@(x->imag, x->real + 1);
r->real = npy_log@c@(l);
@ftype@ xx = x->real;
@ftype@ yy = x->imag;
r->imag = npy_atan2@c@(yy, xx + 1);

@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
38 changes: 38 additions & 0 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -1808,6 +1808,44 @@ def test_special(self):
assert_equal(ncu.log1p(-2.), np.nan)
assert_equal(ncu.log1p(-np.inf), np.nan)

class TestLog1pComplex:
def test_log1p_complex(self):
assert_almost_equal(ncu.log1p(0.2 + 0.3j), ncu.log(1.2 + 0.3j))
assert_almost_equal(ncu.log1p(1e-19 + 1e-18j), 1e-19 + 1e-18j,
decimal=23)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that assert_almost_equal_nulp is the more convenient way of checking things here.

@WarrenWeckesser if you could want to dig into whether the implementation looks right, that would be great. I would still feel things would be much simpler if we can "steal" an implementation from elsewhere (that has a clearly BSD compatible license).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to centralise ideas (pytorch/pytorch#89214 (comment)), I agree. Now, it's not clear where is this function implemented for complex numbers really...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

# 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).real, 0.0953102)
assert_almost_equal(ncu.log1p(0.1 + 1e-18j).imag, 9.0909091e-19,
decimal=23)
assert_almost_equal(ncu.log1p(0.5 + 0j), 0.40546510810816 + 0j)
assert_almost_equal(ncu.log1p(0.0 + 0.5j), 0.111571776 + 0.463647609j)

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).real,
575.6462732485114)
assert_almost_equal(ncu.log1p(1e250 + 1j).imag,
1e-250, decimal=255)
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, decimal=255)
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(complex(0, np.inf)), np.inf + np.pi * 0.5j)
assert_equal(ncu.log1p(-1 + 0j), -np.inf + 0j)

class TestExpm1:
def test_expm1(self):
Expand Down
0