MAINT: silence invalid warning for np.logaddexp(2) with NaN input (#32733)

This commit is contained in:
Matt Haberland authored and GitHub committed 2026-09-22 07:58:41 -06:00
1 parent 0714bda057
commit 698f04fbeb
4 files changed
+36 -19

No files matched your search

@@ -0,0 +1,4 @@
``logaddexp`` and ``logaddexp2`` no longer warn for NaN inputs
--------------------------------------------------------------
`numpy.logaddexp` and `numpy.logaddexp2` now propagate NaN inputs without
emitting an invalid floating-point warning.
@@ -471,11 +471,12 @@ NPY_INPLACE @type@ npy_logaddexp@c@(@type@ x, @type@ y)
return x + LOGE2;
}
else {
const @type@ ZERO = 0.0;
const @type@ tmp = x - y;
if (tmp > 0) {
if (isgreater(tmp, ZERO)) {
return x + npy_log1p@c@(npy_exp@c@(-tmp));
}
else if (tmp <= 0) {
else if (islessequal(tmp, ZERO)) {
return y + npy_log1p@c@(npy_exp@c@(tmp));
}
else {
@@ -492,11 +493,12 @@ NPY_INPLACE @type@ npy_logaddexp2@c@(@type@ x, @type@ y)
return x + 1;
}
else {
const @type@ ZERO = 0.0;
const @type@ tmp = x - y;
if (tmp > 0) {
if (isgreater(tmp, ZERO)) {
return x + npy_log2_1p@c@(npy_exp2@c@(-tmp));
}
else if (tmp <= 0) {
else if (islessequal(tmp, ZERO)) {
return y + npy_log2_1p@c@(npy_exp2@c@(tmp));
}
else {
+26 -14
View File
@@ -1399,7 +1399,7 @@ class TestExp2:
assert_almost_equal(np.exp2(yf), xf)
class TestLogAddExp2(_FilterInvalids):
class TestLogAddExp2:
# Need test for intermediate precisions
def test_logaddexp2_values(self):
x = [1, 2, 3, 4, 5]
@@ -1433,12 +1433,18 @@ class TestLogAddExp2(_FilterInvalids):
logzf = np.array(z, dtype=dt)
assert_equal(np.logaddexp2(logxf, logyf), logzf)
def test_nan(self):
assert_(np.isnan(np.logaddexp2(np.nan, np.inf)))
assert_(np.isnan(np.logaddexp2(np.inf, np.nan)))
assert_(np.isnan(np.logaddexp2(np.nan, 0)))
assert_(np.isnan(np.logaddexp2(0, np.nan)))
assert_(np.isnan(np.logaddexp2(np.nan, np.nan)))
@pytest.mark.parametrize("dtype", ['f', 'd', 'g'])
@pytest.mark.parametrize("x, y", [
(np.nan, np.inf),
(np.inf, np.nan),
(np.nan, 0),
(0, np.nan),
(np.nan, np.nan),
])
def test_nan(self, dtype, x, y):
x, y = np.array([x, y], dtype=dtype)
with np.errstate(invalid='raise'):
assert np.isnan(np.logaddexp2(x, y))
def test_reduce(self):
assert_equal(np.logaddexp2.identity, -np.inf)
@@ -2266,7 +2272,7 @@ class TestAVXFloat32Transcendental:
assert_array_almost_equal_nulp(np.sin(x_large), sin_true[::jj], nulp=2)
assert_array_almost_equal_nulp(np.cos(x_large), cos_true[::jj], nulp=2)
class TestLogAddExp(_FilterInvalids):
class TestLogAddExp:
def test_logaddexp_values(self):
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
@@ -2299,12 +2305,18 @@ class TestLogAddExp(_FilterInvalids):
logzf = np.array(z, dtype=dt)
assert_equal(np.logaddexp(logxf, logyf), logzf)
def test_nan(self):
assert_(np.isnan(np.logaddexp(np.nan, np.inf)))
assert_(np.isnan(np.logaddexp(np.inf, np.nan)))
assert_(np.isnan(np.logaddexp(np.nan, 0)))
assert_(np.isnan(np.logaddexp(0, np.nan)))
assert_(np.isnan(np.logaddexp(np.nan, np.nan)))
@pytest.mark.parametrize("dtype", ['f', 'd', 'g'])
@pytest.mark.parametrize("x, y", [
(np.nan, np.inf),
(np.inf, np.nan),
(np.nan, 0),
(0, np.nan),
(np.nan, np.nan),
])
def test_nan(self, dtype, x, y):
x, y = np.array([x, y], dtype=dtype)
with np.errstate(invalid='raise'):
assert np.isnan(np.logaddexp(x, y))
def test_reduce(self):
assert_equal(np.logaddexp.identity, -np.inf)
-1
View File
@@ -12,7 +12,6 @@ filterwarnings =
ignore:numpy.ufunc size changed
ignore::UserWarning:cpuinfo,
ignore: divide by zero encountered in log
ignore: invalid value encountered in log
# Matrix PendingDeprecationWarning.
ignore:the matrix subclass is not:PendingDeprecationWarning
ignore:Importing from numpy.matlib is:PendingDeprecationWarning