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
commit 153bc7a589a856d22221249527fe4620632cc630 Author: Alex Herbert <[email protected]> AuthorDate: Wed Oct 21 14:24:41 2026 +0100 Update trigamma reference data Data computed using WolframAlpha uses the decimal representation of the floating-point argument. Change to MATLAB which can use the floating-point argument converted to an equivalent rational N*2^e. --- .../apache/commons/numbers/gamma/TrigammaTest.java | 130 +++++++++++---------- 1 file changed, 70 insertions(+), 60 deletions(-) diff --git a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java index be916fc6..3871f509 100644 --- a/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java +++ b/commons-numbers-gamma/src/test/java/org/apache/commons/numbers/gamma/TrigammaTest.java @@ -16,83 +16,93 @@ */ package org.apache.commons.numbers.gamma; +import java.util.stream.Stream; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; /** * Tests for {@link Trigamma}. */ class TrigammaTest { @ParameterizedTest - @CsvSource({ - // Negative integers are poles of the trigamma function - "-1e300, Infinity", - "-1e18, Infinity", - "-1e16, Infinity", - "-0x1p53, Infinity", - "-63, Infinity", - "-62, Infinity", - "-2, Infinity", - "-1, Infinity", - // computed using webMathematica. For example, to compute trigamma($i) = Polygamma(1, $i), use - // - // http://functions.wolfram.com/webMathematica/Evaluated.jsp?name=PolyGamma2&plottype=0&vars={%221%22,%22$i%22}&digits=20 - "-98765.4321, 10.332673372988805424", - "-100.5, 9.8597034918700861520", - "-63.0001, 1.0000000327412051737e8", - "-62.0001, 1.0000000327386856554e8", - "-50.5, 9.8499971860824842274", - "-20.5, 9.8219943446498794821", - "-10.5, 9.7787577398148123845", - "-5.5, 9.7033198653394003812", - "-2.5, 9.5392466449891237539", - "-2.0001, 1.0000000289494954257e8", - "-1.0001, 1.0000000264497454070e8", - "-0.5, 8.9348022005446793094", - "-1e-1, 101.92253995947720352", - "-1e-2, 10001.669304101071825", - "-1e-3, 1.0000016473414317771e6", - "-1e-4, 1.0000000164517451070e8", - "-1e-5, 1.0000000001644958108e10", - "1e-11, 1e22", - "1e-10, 1e20", - "1e-9, 1.0000000000000000016e18", - "1e-8, 1.0000000000000001645e16", - "1e-7, 1.0000000000000164493e14", - "1e-6, 1.0000000000016449317e12", - "1e-5, 1.0000000001644910026e10", - "1e-4, 1.0000000164469368793e8", - "1e-3, 1.0000016425331958690e6", - "1e-2, 10001.621213528313220", - "1e-1, 101.43329915079275882", - "1, 1.6449340668482264365", - "1.5, 0.93480220054467930942", - "2, 0.64493406684822643647", - "2.5, 0.49035775610023486497", - "3, 0.39493406684822643647", - "3.5, 0.33035775610023486497", - "4, 0.28382295573711532536", - "4.5, 0.24872510303901037518", - "5, 0.22132295573711532536", - "7.5, 0.14261589669670379977", - "10, 0.10516633568168574612", - "20, 0.051270822935203119832", - "50, 0.020201333226697125806", - "100, 0.010050166663333571395", - "12345.6789, 0.000081003281325733214110", - }) + @MethodSource void testTrigamma(double value, double expected) { - final double eps = 1e-9; // Allowed relative error. + // Javadoc states: 1e-8 relative error or absolute error (whichever is larger). + // Current test data passes at 1e-9, but not 1e-10. + final double eps = 1e-9; final double actual = Trigamma.value(value); if (Double.isFinite(expected)) { - Assertions.assertEquals(expected, actual, expected * eps, () -> "trigamma: " + value); + final double delta = Math.max(expected * eps, eps); + Assertions.assertEquals(expected, actual, delta, () -> "trigamma: " + value); } else { Assertions.assertEquals(expected, actual, () -> "trigamma: " + value); } } + static Stream<Arguments> testTrigamma() { + final double inf = Double.POSITIVE_INFINITY; + return Stream.of( + // Negative integers are poles of the trigamma function + Arguments.of(-1e300, inf), + Arguments.of(-1e18, inf), + Arguments.of(-1e16, inf), + Arguments.of(-0x1p53, inf), + Arguments.of(-63, inf), + Arguments.of(-62, inf), + Arguments.of(-2, inf), + Arguments.of(-1, inf), + // Computed using MATLAB R2026a: + // digits(32) + // vpa(psi(1, sym(x, 'f')) + Arguments.of(-98765.4321, 10.332673372912188148264022057244), + Arguments.of(-100.5, 9.8597034918700861519982038331294), + Arguments.of(-63.0001, 100000003.26748120604737038124534), + Arguments.of(-62.0001, 100000003.26722925421431309128454), + Arguments.of(-50.5, 9.8499971860824842274155224962237), + Arguments.of(-20.5, 9.8219943446498794820737114698026), + Arguments.of(-10.5, 9.7787577398148123844967599803581), + Arguments.of(-5.5, 9.703319865339400381194594333564), + Arguments.of(-2.5, 9.5392466449891237538616899443825), + Arguments.of(-2.0001, 100000002.89452748018853896347721), + Arguments.of(-1.0001, 100000002.64499656752351264863645), + Arguments.of(-0.5, 8.9348022005446793094172454999381), + Arguments.of(-1e-1, 101.92253995947719243202249211742), + Arguments.of(-1e-2, 10001.669304101071408182825296132), + Arguments.of(-1e-3, 1000001.6473414317354171881001598), + Arguments.of(-1e-4, 100000001.64517450111835638165592), + Arguments.of(-1e-5, 10000000001.644956472249912465609), + Arguments.of(1e-11, 10000000000000001210062.259295271), + Arguments.of(1e-10, 99999999999999992715.20547096706), + Arguments.of(1e-9, 999999999999999877.08175114888441), + Arguments.of(1e-8, 10000000000000001.226482826204519), + Arguments.of(1e-7, 100000000000001.65398420407170102), + Arguments.of(1e-6, 1000000000001.6450221665140153103), + Arguments.of(1e-5, 10000000001.644908389973777786412), + Arguments.of(1e-4, 100000001.64469367834879712175657), + Arguments.of(1e-3, 1000001.6425331958273446695041488), + Arguments.of(1e-2, 10001.621213528312803789235020429), + Arguments.of(1e-1, 101.43329915079274770465203965087), + Arguments.of(1, 1.644934066848226436472415166646), + Arguments.of(1.5, 0.93480220054467930941724549993808), + Arguments.of(2, 0.64493406684822643647241516664603), + Arguments.of(2.5, 0.49035775610023486497280105549363), + Arguments.of(3, 0.39493406684822643647241516664603), + Arguments.of(3.5, 0.33035775610023486497280105549363), + Arguments.of(4, 0.28382295573711532536130405553491), + Arguments.of(4.5, 0.24872510303901037517688268814669), + Arguments.of(5, 0.22132295573711532536130405553491), + Arguments.of(7.5, 0.14261589669670379977007418110509), + Arguments.of(10, 0.10516633568168574612220100690806), + Arguments.of(20, 0.051270822935203119831536294588382), + Arguments.of(50, 0.020201333226697125805970645065733), + Arguments.of(100, 0.010050166663333571395245668465701), + Arguments.of(12345.6789, 0.00008100328132573320889689179458686) + ); + } + @Test void testTrigammaZero() { Assertions.assertEquals(Double.POSITIVE_INFINITY, Trigamma.value(0), 0d);
