This is an automated email from the ASF dual-hosted git repository.
asf-gitbox-commits pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
The following commit(s) were added to refs/heads/master by this push:
new 6b2235b0 Digamma to handle reflection of negative half-integers
6b2235b0 is described below
commit 6b2235b086ec03905ae7d2970ebaea048e42d2ba
Author: Alex Herbert <[email protected]>
AuthorDate: Sun Nov 29 15:16:22 2026 +0000
Digamma to handle reflection of negative half-integers
---
.../apache/commons/numbers/gamma/BoostDigamma.java | 24 ++++++++++++++--------
.../commons/numbers/gamma/BoostDiTrigammaTest.java | 18 +++++++++++++---
src/conf/spotbugs/spotbugs-exclude-filter.xml | 5 +++++
3 files changed, 36 insertions(+), 11 deletions(-)
diff --git
a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostDigamma.java
b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostDigamma.java
index 2721cf52..78271911 100644
---
a/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostDigamma.java
+++
b/commons-numbers-gamma/src/main/java/org/apache/commons/numbers/gamma/BoostDigamma.java
@@ -88,15 +88,23 @@ final class BoostDigamma {
x = 1 - x;
// Argument reduction for tan:
double remainder = x - Math.floor(x);
- // Shift to negative if > 0.5:
- if (remainder > HALF) {
- remainder -= 1;
- } else if (remainder == 0) {
- // Digamma is undefined at negative integers.
- // As x -> -n: digamma(x) -> +infty from one side and -infty
from the other.
- return Double.NaN;
+ // Update from the Boost method to handle half-integers.
+ // 1 / tan(pi z) = cos(pi z) / sin(pi z)
+ // As z -> 0.5, sin(pi z) -> 1
+ // 1 / tan(pi z) = cos(pi z)
+ // Neither is 0.0 at z=0.5 as pi is inexact.
+ // So only compute when not a half-integer.
+ if (remainder != HALF) {
+ // Shift to negative if > 0.5:
+ if (remainder > HALF) {
+ remainder -= 1;
+ } else if (remainder == 0) {
+ // Digamma is undefined at negative integers.
+ // As x -> -n: digamma(x) -> +infty from one side and
-infty from the other.
+ return Double.NaN;
+ }
+ result = Math.PI / Math.tan(Math.PI * remainder);
}
- result = Math.PI / Math.tan(Math.PI * remainder);
} else if (x == 0) {
// Undefined at the pole:
// As -x -> 0: digamma(x) -> +infty
diff --git
a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/BoostDiTrigammaTest.java
b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/BoostDiTrigammaTest.java
index 608b7d93..175a4b15 100644
---
a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/BoostDiTrigammaTest.java
+++
b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/BoostDiTrigammaTest.java
@@ -353,9 +353,8 @@ class BoostDiTrigammaTest {
static Stream<Arguments> testDigammaSpot() {
// Boost uses tolerance = 3
- // 5 fail at tolerance = 0
- // 1 fail at tolerance = 1
- final int tolerance = 2;
+ // 4 fail at tolerance = 0
+ final int tolerance = 1;
return Stream.of(
// Result computed using mpmath.digamma (version 1.4.1)
Arguments.of(Double.MAX_VALUE, 709.78271289338399673222338991066,
0),
@@ -376,7 +375,20 @@ class BoostDiTrigammaTest {
Arguments.of(-0.125,
7.1959829284523046176757814502538535827603450463013, tolerance),
Arguments.of(-10.125,
9.9480538258660761287008034071425343357982429855241, tolerance),
Arguments.of(-10.875,
-5.1527360383841562620205965901515879492020193154231, tolerance),
+ // This fails at 1 ULP unless the half-integer case is handled.
+ // Negative half-integer uses the reflection pi / tan(pi * 0.5) =
pi * cos(pi * 0.5) -> 0.
+ // In double precision pi/2 is inexact and the result is digamma(1
- x) + 1.92e-16.
+ // This small addition can be added until digamma > 2 at
digamma(-7.5) adding 1 ULP error.
Arguments.of(-1.5,
0.70315664064524318722569033366791109947350706200623, tolerance),
+ // Additional spot values from mpmath (1.4.1) digamma
+ Arguments.of(-2.5, 1.1031566406452431872256903336679111,
tolerance),
+ Arguments.of(-3.5, 1.38887092635952890151140461938219681,
tolerance),
+ Arguments.of(-4.5, 1.61109314858175112373362684160441904,
tolerance),
+ Arguments.of(-5.5, 1.79291133039993294191544502342260085,
tolerance),
+ Arguments.of(-6.5, 1.9467574842460867880692911772687547,
tolerance),
+ Arguments.of(-7.5, 2.08009081757942012140262451060208803,
tolerance),
+ Arguments.of(-8.5, 2.19773787640294953316733039295502921,
tolerance),
+ Arguments.of(-9.5, 2.30300103429768637527259355084976605,
tolerance),
// Test cases from Rocco Romeo: Values near 0
Arguments.of(Math.scalb(1.0, -100),
-1.26765060022822940149670320537657721566490153286060651209008e30, tolerance),
Arguments.of(-Math.scalb(1.0, -100),
1.26765060022822940149670320537542278433509846713939348790992e30, tolerance),
diff --git a/src/conf/spotbugs/spotbugs-exclude-filter.xml
b/src/conf/spotbugs/spotbugs-exclude-filter.xml
index e17aebc5..fca225d2 100644
--- a/src/conf/spotbugs/spotbugs-exclude-filter.xml
+++ b/src/conf/spotbugs/spotbugs-exclude-filter.xml
@@ -79,6 +79,11 @@
<Method name="log1pmx"/>
<BugPattern name="FE_FLOATING_POINT_EQUALITY"/>
</Match>
+ <Match>
+ <Class name="org.apache.commons.numbers.gamma.BoostDigamma"/>
+ <Method name="digammaImp"/>
+ <BugPattern name="FE_FLOATING_POINT_EQUALITY"/>
+ </Match>
<Match>
<Or>