From 217a657bf276808311d212a5f790e117dc90c912 Mon Sep 17 00:00:00 2001 From: Davis Rens <166565828+davrens101@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:30:22 -0800 Subject: [PATCH 1/4] Fix division by zero when integrating Tabulated1D with discontinuities ENDF-6 TAB1 functions represent physical discontinuities (such as reaction thresholds or resonance boundaries) by repeating coordinate points (x_i == x_{i+1}) across region boundaries. In Tabulated1D._integrate(), computing slopes or logarithmic ratios on zero-width bins resulted in division by zero (producing NaN and runtime warnings) in both integral() and integrate(). Check for zero-width bins in _integrate() and return zero contribution across the zero-width interval. Signed-off-by: Davis Rens <166565828+davrens101@users.noreply.github.com> --- src/endf/function.py | 53 +++++++++++++++++++++++++++++++++--------- tests/test_function.py | 15 ++++++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/endf/function.py b/src/endf/function.py index 6fc543d..d0de652 100644 --- a/src/endf/function.py +++ b/src/endf/function.py @@ -345,14 +345,35 @@ def _integrate(p, x0, y0, x1, y1): elif p == 2: # Linear-linear - m = (y1 - y0)/(x1 - x0) - return (y0 - m*x0)*(x1 - x0) + 0.5*m*(x1**2 - x0**2) + dx = x1 - x0 + if np.isscalar(dx): + if dx == 0.: + return 0. + m = (y1 - y0)/dx + return (y0 - m*x0)*dx + 0.5*m*(x1**2 - x0**2) + else: + out = np.zeros_like(dx, dtype=float) + nz = dx != 0. + m = (y1[nz] - y0[nz])/dx[nz] + out[nz] = (y0[nz] - m*x0[nz])*dx[nz] + 0.5*m*(x1[nz]**2 - x0[nz]**2) + return out elif p == 3: # Linear-log - logx = np.log(x1/x0) - m = (y1 - y0)/logx - return y0*(x1 - x0) + m*(x1*(logx - 1) + x0) + dx = x1 - x0 + if np.isscalar(dx): + if dx == 0.: + return 0. + logx = np.log(x1/x0) + m = (y1 - y0)/logx + return y0*dx + m*(x1*(logx - 1) + x0) + else: + out = np.zeros_like(dx, dtype=float) + nz = dx != 0. + logx = np.log(x1[nz]/x0[nz]) + m = (y1[nz] - y0[nz])/logx + out[nz] = y0[nz]*dx[nz] + m*(x1[nz]*(logx - 1) + x0[nz]) + return out elif p == 4: # Log-linear @@ -363,12 +384,22 @@ def _integrate(p, x0, y0, x1, y1): elif p == 5: # Log-log - logx = np.log(x1/x0) - z = np.log(y1/y0) + logx - factor = np.ones_like(z, dtype=float) - np.divide(np.expm1(z), z, out=factor, where=z != 0.) - return y0*x0*logx*factor - + dx = x1 - x0 + if np.isscalar(dx): + if dx == 0.: + return 0. + logx = np.log(x1/x0) + z = np.log(y1/y0) + logx + factor = np.expm1(z)/z if z != 0. else 1. + return y0*x0*logx*factor + else: + logx = np.zeros_like(dx, dtype=float) + nz = dx != 0. + logx[nz] = np.log(x1[nz]/x0[nz]) + z = np.log(y1/y0) + logx + factor = np.ones_like(z, dtype=float) + np.divide(np.expm1(z), z, out=factor, where=z != 0.) + return y0*x0*logx*factor else: raise ValueError(f"Unknown interpolation rule {p}") diff --git a/tests/test_function.py b/tests/test_function.py index ed55bae..98d3ed0 100644 --- a/tests/test_function.py +++ b/tests/test_function.py @@ -53,3 +53,18 @@ def test_integrate_clipped_partial_range(): assert f.integrate(0., 1.5) == approx(0.5) assert f.integrate(1.5, 0.) == approx(-0.5) + + +def test_integrate_discontinuous(): + # Step discontinuity at x=2.0 + x = [1.0, 2.0, 2.0, 3.0] + y = [1.0, 2.0, 5.0, 6.0] + breakpoints = [2, 4] + interpolation = [2, 2] + f = Tabulated1D(x, y, breakpoints, interpolation) + + assert f.integral() == approx([0.0, 1.5, 1.5, 7.0]) + assert f.integrate(1.0, 3.0) == approx(7.0) + assert f.integrate(1.0, 2.0) == approx(1.5) + assert f.integrate(2.0, 3.0) == approx(5.5) + assert f.integrate(1.5, 2.5) == approx(3.5) From bb939876164070a72803dcf1297b7df24d081ffd Mon Sep 17 00:00:00 2001 From: davrens101 Date: Wed, 11 Nov 2026 20:49:57 -0800 Subject: [PATCH 2/4] Apply reviewer-suggested zero-width-bin early return in _integrate() Simplify Tabulated1D._integrate() to return 0. at the top when x1 == x0, avoiding division by zero on zero-width bins (ENDF-6 TAB1 discontinuities) without changing the remaining per-interpolation logic. Keep regression test test_integrate_discontinuous. --- src/endf/function.py | 54 ++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 39 deletions(-) diff --git a/src/endf/function.py b/src/endf/function.py index d0de652..8264eea 100644 --- a/src/endf/function.py +++ b/src/endf/function.py @@ -339,6 +339,12 @@ def get_interpolation(indx: int) -> int: @staticmethod def _integrate(p, x0, y0, x1, y1): + # Zero-width bins contribute nothing to the integral; this avoids + # division by zero across repeated tabulated coordinates (e.g. ENDF-6 + # TAB1 discontinuities). + if x1 == x0: + return 0. + if p == 1: # Histogram return y0*(x1 - x0) @@ -346,34 +352,15 @@ def _integrate(p, x0, y0, x1, y1): elif p == 2: # Linear-linear dx = x1 - x0 - if np.isscalar(dx): - if dx == 0.: - return 0. - m = (y1 - y0)/dx - return (y0 - m*x0)*dx + 0.5*m*(x1**2 - x0**2) - else: - out = np.zeros_like(dx, dtype=float) - nz = dx != 0. - m = (y1[nz] - y0[nz])/dx[nz] - out[nz] = (y0[nz] - m*x0[nz])*dx[nz] + 0.5*m*(x1[nz]**2 - x0[nz]**2) - return out + m = (y1 - y0)/dx + return (y0 - m*x0)*dx + 0.5*m*(x1**2 - x0**2) elif p == 3: # Linear-log dx = x1 - x0 - if np.isscalar(dx): - if dx == 0.: - return 0. - logx = np.log(x1/x0) - m = (y1 - y0)/logx - return y0*dx + m*(x1*(logx - 1) + x0) - else: - out = np.zeros_like(dx, dtype=float) - nz = dx != 0. - logx = np.log(x1[nz]/x0[nz]) - m = (y1[nz] - y0[nz])/logx - out[nz] = y0[nz]*dx[nz] + m*(x1[nz]*(logx - 1) + x0[nz]) - return out + logx = np.log(x1/x0) + m = (y1 - y0)/logx + return y0*dx + m*(x1*(logx - 1) + x0) elif p == 4: # Log-linear @@ -385,21 +372,10 @@ def _integrate(p, x0, y0, x1, y1): elif p == 5: # Log-log dx = x1 - x0 - if np.isscalar(dx): - if dx == 0.: - return 0. - logx = np.log(x1/x0) - z = np.log(y1/y0) + logx - factor = np.expm1(z)/z if z != 0. else 1. - return y0*x0*logx*factor - else: - logx = np.zeros_like(dx, dtype=float) - nz = dx != 0. - logx[nz] = np.log(x1[nz]/x0[nz]) - z = np.log(y1/y0) + logx - factor = np.ones_like(z, dtype=float) - np.divide(np.expm1(z), z, out=factor, where=z != 0.) - return y0*x0*logx*factor + logx = np.log(x1/x0) + z = np.log(y1/y0) + logx + factor = np.expm1(z)/z if z != 0. else 1. + return y0*x0*logx*factor else: raise ValueError(f"Unknown interpolation rule {p}") From cb330bbd42eaa7aaae8d52e0a044a57a3f49c43a Mon Sep 17 00:00:00 2001 From: davrens101 <166565828+davrens101@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:44:22 -0800 Subject: [PATCH 3/4] Fix zero-width-bin guard for array bounds in _integrate The reviewer-requested early return 'if x1 == x0: return 0.' works for scalar bounds (integrate) but crashes for array bounds (integral) because x1 == x0 returns an ambiguous array. Handle both scalar and array zero-width bins so integral() over ENDF-6 TAB1 step discontinuities no longer raises ValueError. --- src/endf/function.py | 52 +++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/src/endf/function.py b/src/endf/function.py index 8264eea..3b94c5e 100644 --- a/src/endf/function.py +++ b/src/endf/function.py @@ -339,46 +339,58 @@ def get_interpolation(indx: int) -> int: @staticmethod def _integrate(p, x0, y0, x1, y1): - # Zero-width bins contribute nothing to the integral; this avoids - # division by zero across repeated tabulated coordinates (e.g. ENDF-6 - # TAB1 discontinuities). - if x1 == x0: - return 0. + # Zero-width bins (ENDF-6 TAB1 step discontinuities) contribute nothing + # to the integral and would otherwise divide by zero below. Handle both + # scalar bounds (``integrate``) and array bounds (``integral``). + x0 = np.asarray(x0, dtype=float) + x1 = np.asarray(x1, dtype=float) + y0 = np.asarray(y0, dtype=float) + y1 = np.asarray(y1, dtype=float) + dx = x1 - x0 + nonzero = dx != 0 + out = np.zeros_like(dx, dtype=float) + if not np.any(nonzero): + return out.item() if out.ndim == 0 else out + + x0n, x1n, y0n, y1n = x0[nonzero], x1[nonzero], y0[nonzero], y1[nonzero] if p == 1: # Histogram - return y0*(x1 - x0) + out[nonzero] = y0n * (x1n - x0n) elif p == 2: # Linear-linear - dx = x1 - x0 - m = (y1 - y0)/dx - return (y0 - m*x0)*dx + 0.5*m*(x1**2 - x0**2) + dxn = x1n - x0n + m = (y1n - y0n) / dxn + out[nonzero] = (y0n - m * x0n) * dxn + 0.5 * m * (x1n**2 - x0n**2) elif p == 3: # Linear-log - dx = x1 - x0 - logx = np.log(x1/x0) - m = (y1 - y0)/logx - return y0*dx + m*(x1*(logx - 1) + x0) + dxn = x1n - x0n + logx = np.log(x1n / x0n) + m = (y1n - y0n) / logx + out[nonzero] = y0n * dxn + m * (x1n * (logx - 1) + x0n) elif p == 4: # Log-linear - logy = np.log(y1/y0) + logy = np.log(y1n / y0n) factor = np.ones_like(logy, dtype=float) np.divide(np.expm1(logy), logy, out=factor, where=logy != 0.) - return y0*(x1 - x0)*factor + out[nonzero] = y0n * (x1n - x0n) * factor elif p == 5: # Log-log - dx = x1 - x0 - logx = np.log(x1/x0) - z = np.log(y1/y0) + logx - factor = np.expm1(z)/z if z != 0. else 1. - return y0*x0*logx*factor + dxn = x1n - x0n + logx = np.log(x1n / x0n) + z = np.log(y1n / y0n) + logx + factor = np.expm1(z) / z if z != 0. else 1. + out[nonzero] = y0n * x0n * logx * factor + else: raise ValueError(f"Unknown interpolation rule {p}") + return out.item() if out.ndim == 0 else out + @classmethod def from_ace(cls, ace, idx=0, convert_units=True): """Create a Tabulated1D object from an ACE table. From 921b1838558ddcafdda6ef036373914e4405b5ef Mon Sep 17 00:00:00 2001 From: davrens101 <166565828+davrens101@users.noreply.github.com> Date: Fri, 13 Nov 2026 11:53:08 -0800 Subject: [PATCH 4/4] Fix ambiguous-array condition in log-log _integrate branch p5 The p5 branch used 'np.expm1(z) / z if z != 0. else 1.', where z is always an array after masked indexing (x0n/x1n/y0n/y1n). For array bounds with 2+ p5 intervals this raises 'truth value of an array is ambiguous'. Switch to the same np.divide(out=..., where=...) pattern already used for p4, preserving the 1.0-at-z==0 limit and scalar fast-path via out.item(). --- src/endf/function.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/endf/function.py b/src/endf/function.py index 3b94c5e..58f84e7 100644 --- a/src/endf/function.py +++ b/src/endf/function.py @@ -383,7 +383,8 @@ def _integrate(p, x0, y0, x1, y1): dxn = x1n - x0n logx = np.log(x1n / x0n) z = np.log(y1n / y0n) + logx - factor = np.expm1(z) / z if z != 0. else 1. + factor = np.ones_like(z, dtype=float) + np.divide(np.expm1(z), z, out=factor, where=z != 0.) out[nonzero] = y0n * x0n * logx * factor else: