Gamma { public static double digamma(double x) { if (x > 0 && x <= S_LIMIT) { return -GAMMA - 1 / x; } if (x >= C_LIMIT) { double inv = 1 / (x * x); return FastMath.log(x) - 0.5 / x - inv * ((1.0 / 12) + inv * (1.0 / 120 - inv / 252)); } return digamma(x + 1) - 1 / x; } private  Gamma(); static double logGamma(double x); static double regularizedGammaP(double a, double x); static double regularizedGammaP(double a,
                                           double x,
                                           double epsilon,
                                           int maxIterations); static double regularizedGammaQ(double a, double x); static double regularizedGammaQ(final double a,
                                           double x,
                                           double epsilon,
                                           int maxIterations); static double digamma(double x); static double trigamma(double x); static double lanczos(final double x); static double invGamma1pm1(final double x); static double logGamma1p(final double x); static double gamma(final double x); static final double GAMMA; static final double LANCZOS_G; }
@Test public void testDigammaLargeArgs() { double eps = 1e-8; Assert.assertEquals(4.6001618527380874002, Gamma.digamma(100), eps); Assert.assertEquals(3.9019896734278921970, Gamma.digamma(50), eps); Assert.assertEquals(2.9705239922421490509, Gamma.digamma(20), eps); Assert.assertEquals(2.9958363947076465821, Gamma.digamma(20.5), eps); Assert.assertEquals(2.2622143570941481605, Gamma.digamma(10.1), eps); Assert.assertEquals(2.1168588189004379233, Gamma.digamma(8.8), eps); Assert.assertEquals(1.8727843350984671394, Gamma.digamma(7), eps); Assert.assertEquals(0.42278433509846713939, Gamma.digamma(2), eps); Assert.assertEquals(-100.56088545786867450, Gamma.digamma(0.01), eps); Assert.assertEquals(-4.0390398965921882955, Gamma.digamma(-0.8), eps); Assert.assertEquals(4.2003210041401844726, Gamma.digamma(-6.3), eps); }
@Test public void testDigammaSmallArgs() { double[] expected = {-10.423754940411076795, -100.56088545786867450, -1000.5755719318103005, -10000.577051183514335, -100000.57719921568107, -1.0000005772140199687e6, -1.0000000577215500408e7, -1.0000000057721564845e8, -1.0000000005772156633e9, -1.0000000000577215665e10, -1.0000000000057721566e11, -1.0000000000005772157e12, -1.0000000000000577216e13, -1.0000000000000057722e14, -1.0000000000000005772e15, -1e+16, -1e+17, -1e+18, -1e+19, -1e+20, -1e+21, -1e+22, -1e+23, -1e+24, -1e+25, -1e+26, -1e+27, -1e+28, -1e+29, -1e+30}; for (double n = 1; n < 30; n++) { checkRelativeError(String.format("Test %.0f: ", n), expected[(int) (n - 1)], Gamma.digamma(FastMath.pow(10.0, -n)), 1e-8); } }