1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.special;
23
24 import org.hipparchus.CalculusFieldElement;
25 import org.hipparchus.Field;
26 import org.hipparchus.UnitTestUtils;
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.exception.MathIllegalStateException;
29 import org.hipparchus.util.Binary64;
30 import org.hipparchus.util.Binary64Field;
31 import org.hipparchus.util.FastMath;
32 import org.junit.jupiter.api.Test;
33
34 import static org.junit.jupiter.api.Assertions.assertEquals;
35 import static org.junit.jupiter.api.Assertions.assertThrows;
36 import static org.junit.jupiter.api.Assertions.assertTrue;
37
38
39
40 class GammaTest {
41
42 final Field<Binary64> field = Binary64Field.getInstance();
43 final Binary64 zero = field.getZero();
44 final Binary64 one = field.getOne();
45
46 private void testRegularizedGamma(double expected, double a, double x) {
47 double actualP = Gamma.regularizedGammaP(a, x);
48 double actualQ = Gamma.regularizedGammaQ(a, x);
49 UnitTestUtils.customAssertEquals(expected, actualP, 10e-15);
50 UnitTestUtils.customAssertEquals(actualP, 1.0 - actualQ, 10e-15);
51 }
52
53 private <T extends CalculusFieldElement<T>> void testRegularizedGammaField(double expected, T a, T x) {
54 T actualP = Gamma.regularizedGammaP(a, x);
55 T actualQ = Gamma.regularizedGammaQ(a, x);
56 UnitTestUtils.customAssertEquals(expected, actualP.getReal(), 10e-15);
57 UnitTestUtils.customAssertEquals(actualP.getReal(), actualQ.negate().add(1.0).getReal(), 10e-15);
58 }
59
60 private void testRegularizedGamma(double expected, double a, double x, double epsilon, int maxIterations) {
61 double actualP = Gamma.regularizedGammaP(a, x, epsilon, maxIterations);
62 double actualQ = Gamma.regularizedGammaQ(a, x, epsilon, maxIterations);
63 UnitTestUtils.customAssertEquals(expected, actualP, epsilon);
64 UnitTestUtils.customAssertEquals(actualP, 1.0 - actualQ, epsilon);
65 }
66
67 private <T extends CalculusFieldElement<T>> void testRegularizedGammaField(double expected, T a, T x, double epsilon, int maxIterations) {
68 T actualP = Gamma.regularizedGammaP(a, x, epsilon, maxIterations);
69 T actualQ = Gamma.regularizedGammaQ(a, x, epsilon, maxIterations);
70 UnitTestUtils.customAssertEquals(expected, actualP.getReal(), epsilon);
71 UnitTestUtils.customAssertEquals(actualP.getReal(), actualQ.negate().add(1.0).getReal(), epsilon);
72 }
73
74 private void testLogGamma(double expected, double x) {
75 double actual = Gamma.logGamma(x);
76 UnitTestUtils.customAssertEquals(expected, actual, 10e-15);
77 }
78
79 private <T extends CalculusFieldElement<T>> void testLogGammaField(T expected, T x) {
80 T actual = Gamma.logGamma(x);
81 UnitTestUtils.customAssertEquals(expected.getReal(), actual.getReal(), 10e-15);
82 }
83
84 @Test
85 void testRegularizedGammaNanPositive() {
86 testRegularizedGamma(Double.NaN, Double.NaN, 1.0);
87 }
88
89 @Test
90 void testRegularizedGammaNanPositiveField() {
91 testRegularizedGammaField(Double.NaN, new Binary64(Double.NaN), one);
92 }
93
94 @Test
95 void testRegularizedGammaPositiveNan() {
96 testRegularizedGamma(Double.NaN, 1.0, Double.NaN);
97 }
98
99 @Test
100 void testRegularizedGammaPositiveNanField() {
101 testRegularizedGammaField(Double.NaN, one, new Binary64(Double.NaN));
102 }
103
104 @Test
105 void testRegularizedGammaNegativePositive() {
106 testRegularizedGamma(Double.NaN, -1.5, 1.0);
107 }
108
109 @Test
110 void testRegularizedGammaNegativePositiveField() {
111 testRegularizedGammaField(Double.NaN, new Binary64(-1.5), one);
112 }
113
114 @Test
115 void testRegularizedGammaPositiveNegative() {
116 testRegularizedGamma(Double.NaN, 1.0, -1.0);
117 }
118
119 @Test
120 void testRegularizedGammaPositiveNegativeField() {
121 testRegularizedGammaField(Double.NaN, one, one.negate());
122 }
123
124 @Test
125 void testRegularizedGammaZeroPositive() {
126 testRegularizedGamma(Double.NaN, 0.0, 1.0);
127 }
128
129 @Test
130 void testRegularizedGammaZeroPositiveField() {
131 testRegularizedGammaField(Double.NaN, zero, one);
132 }
133
134 @Test
135 void testRegularizedGammaPositiveZero() {
136 testRegularizedGamma(0.0, 1.0, 0.0);
137 }
138
139 @Test
140 void testRegularizedGammaPositiveZeroField() {
141 testRegularizedGammaField(0.0, one, zero);
142 }
143
144 @Test
145 void testRegularizedGammaPositivePositive() {
146 testRegularizedGamma(0.632120558828558, 1.0, 1.0);
147 }
148
149 @Test
150 void testRegularizedGammaPositivePositiveField() {
151 testRegularizedGammaField(0.632120558828558, one, one);
152 }
153
154 @Test
155 void testRegularizedGammaPMaxNumberOfIterationsExceeded(){
156 assertThrows(MathIllegalStateException.class, () -> testRegularizedGamma(0.632120558828558, 1.0, 1.0, 1e-15, 1));
157 }
158
159 @Test
160 void testRegularizedGammaPMaxNumberOfIterationsExceededField(){
161 assertThrows(MathIllegalStateException.class, () -> testRegularizedGammaField(0.632120558828558, one, one, 1e-15, 1));
162 }
163
164 @Test
165 void testLogGammaNan() {
166 testLogGamma(Double.NaN, Double.NaN);
167 }
168
169 @Test
170 void testLogGammaNanField() {
171 testLogGammaField(new Binary64(Double.NaN), new Binary64(Double.NaN));
172 }
173
174 @Test
175 void testLogGammaNegative() {
176 testLogGamma(Double.NaN, -1.0);
177 }
178
179 @Test
180 void testLogGammaNegativeField() {
181 testLogGammaField(new Binary64(Double.NaN), one.negate());
182 }
183
184 @Test
185 void testLogGammaZero() {
186 testLogGamma(Double.NaN, 0.0);
187 }
188
189 @Test
190 void testLogGammaZeroField() {
191 testLogGammaField(new Binary64(Double.NaN), zero);
192 }
193
194 @Test
195 void testLogGammaPositive() {
196 testLogGamma(0.6931471805599457, 3.0);
197 }
198
199 @Test
200 void testLogGammaPositiveField() {
201 testLogGammaField(new Binary64(0.6931471805599457), new Binary64(3.0));
202 }
203
204 @Test
205 void testDigammaLargeArgs() {
206 double eps = 1e-8;
207 assertEquals(4.6001618527380874002, Gamma.digamma(100), eps);
208 assertEquals(3.9019896734278921970, Gamma.digamma(50), eps);
209 assertEquals(2.9705239922421490509, Gamma.digamma(20.), eps);
210 assertEquals(2.9958363947076465821, Gamma.digamma(20.5), eps);
211 assertEquals(2.2622143570941481605, Gamma.digamma(10.1), eps);
212 assertEquals(2.1168588189004379233, Gamma.digamma(8.8), eps);
213 assertEquals(1.8727843350984671394, Gamma.digamma(7), eps);
214 assertEquals(0.42278433509846713939, Gamma.digamma(2), eps);
215 assertEquals(-100.56088545786867450, Gamma.digamma(0.01), eps);
216 assertEquals(-4.0390398965921882955, Gamma.digamma(-0.8), eps);
217 assertEquals(4.2003210041401844726, Gamma.digamma(-6.3), eps);
218 }
219
220 @Test
221 void testDigammaLargeArgsField() {
222 double eps = 1e-8;
223 assertEquals(4.6001618527380874002, Gamma.digamma(new Binary64(100)).getReal(), eps);
224 assertEquals(3.9019896734278921970, Gamma.digamma(new Binary64(50)).getReal(), eps);
225 assertEquals(2.9705239922421490509, Gamma.digamma(new Binary64(20)).getReal(), eps);
226 assertEquals(2.9958363947076465821, Gamma.digamma(new Binary64(20.5)).getReal(), eps);
227 assertEquals(2.2622143570941481605, Gamma.digamma(new Binary64(10.1)).getReal(), eps);
228 assertEquals(2.1168588189004379233, Gamma.digamma(new Binary64(8.8)).getReal(), eps);
229 assertEquals(1.8727843350984671394, Gamma.digamma(new Binary64(7)).getReal(), eps);
230 assertEquals(0.42278433509846713939, Gamma.digamma(new Binary64(2)).getReal(), eps);
231 assertEquals(-100.56088545786867450, Gamma.digamma(new Binary64(0.01)).getReal(), eps);
232 assertEquals(-4.0390398965921882955, Gamma.digamma(new Binary64(-0.8)).getReal(), eps);
233 assertEquals(4.2003210041401844726, Gamma.digamma(new Binary64(-6.3)).getReal(), eps);
234 }
235
236 @Test
237 void testDigammaSmallArgs() {
238
239
240 double[] expected = {-10.423754940411076795, -100.56088545786867450, -1000.5755719318103005,
241 -10000.577051183514335, -100000.57719921568107, -1.0000005772140199687e6, -1.0000000577215500408e7,
242 -1.0000000057721564845e8, -1.0000000005772156633e9, -1.0000000000577215665e10, -1.0000000000057721566e11,
243 -1.0000000000005772157e12, -1.0000000000000577216e13, -1.0000000000000057722e14, -1.0000000000000005772e15, -1e+16,
244 -1e+17, -1e+18, -1e+19, -1e+20, -1e+21, -1e+22, -1e+23, -1e+24, -1e+25, -1e+26,
245 -1e+27, -1e+28, -1e+29, -1e+30};
246 for (double n = 1; n < 30; n++) {
247 checkRelativeError(String.format("Test %.0f: ", n), expected[(int) (n - 1)], Gamma.digamma(FastMath.pow(10.0, -n)), 1e-8);
248 }
249 }
250
251 @Test
252 void testDigammaSmallArgsField() {
253
254
255 double[] expected = {-10.423754940411076795, -100.56088545786867450, -1000.5755719318103005,
256 -10000.577051183514335, -100000.57719921568107, -1.0000005772140199687e6, -1.0000000577215500408e7,
257 -1.0000000057721564845e8, -1.0000000005772156633e9, -1.0000000000577215665e10, -1.0000000000057721566e11,
258 -1.0000000000005772157e12, -1.0000000000000577216e13, -1.0000000000000057722e14, -1.0000000000000005772e15, -1e+16,
259 -1e+17, -1e+18, -1e+19, -1e+20, -1e+21, -1e+22, -1e+23, -1e+24, -1e+25, -1e+26,
260 -1e+27, -1e+28, -1e+29, -1e+30};
261 for (double n = 1; n < 30; n++) {
262 checkRelativeError(String.format("Test %.0f: ", n), expected[(int) (n - 1)], Gamma.digamma(new Binary64(FastMath.pow(10.0, -n))).getReal(), 1e-8);
263 }
264 }
265
266 @Test
267 void testDigammaNonRealArgs() {
268 assertTrue(Double.isNaN(Gamma.digamma(Double.NaN)));
269 assertTrue(Double.isInfinite(Gamma.digamma(Double.POSITIVE_INFINITY)));
270 assertTrue(Double.isInfinite(Gamma.digamma(Double.NEGATIVE_INFINITY)));
271 }
272
273 @Test
274 void testDigammaNonRealArgsField() {
275 assertTrue(Gamma.digamma(new Binary64(Double.NaN)).isNaN());
276 assertTrue(Gamma.digamma(new Binary64(Double.POSITIVE_INFINITY)).isInfinite());
277 assertTrue(Gamma.digamma(new Binary64(Double.NEGATIVE_INFINITY)).isInfinite());
278 }
279
280 @Test
281 void testDigammaNegative() {
282 checkRelativeError("Test -1.5: ", 0.703156638, Gamma.digamma( -1.5), 1.0e-8);
283 checkRelativeError("Test -20.5: ", 3.044616880, Gamma.digamma( -20.5), 1.0e-8);
284 checkRelativeError("Test -101.5: ", 4.624976815, Gamma.digamma( -101.5), 1.0e-8);
285 checkRelativeError("Test -10000.5: ", 9.210440364, Gamma.digamma( -10000.5), 1.0e-8);
286 checkRelativeError("Test -1000000.5: ", 13.815511555, Gamma.digamma(-1000000.5), 1.0e-8);
287 }
288
289 @Test
290 void testDigammaNegativeField() {
291 checkRelativeError("Test -1.5: ", 0.703156638, Gamma.digamma(new Binary64( -1.5)).getReal(), 1.0e-8);
292 checkRelativeError("Test -20.5: ", 3.044616880, Gamma.digamma(new Binary64( -20.5)).getReal(), 1.0e-8);
293 checkRelativeError("Test -101.5: ", 4.624976815, Gamma.digamma(new Binary64( -101.5)).getReal(), 1.0e-8);
294 checkRelativeError("Test -10000.5: ", 9.210440364, Gamma.digamma(new Binary64( -10000.5)).getReal(), 1.0e-8);
295 checkRelativeError("Test -1000000.5: ", 13.815511555, Gamma.digamma(new Binary64(-1000000.5)).getReal(), 1.0e-8);
296 }
297
298 @Test
299 void testTrigamma() {
300 double eps = 1e-13;
301
302
303
304 double[] data = {
305 1e-6, 1.0000000000016449316627376e12,
306 1e-4, 1.0000000164469368793e8,
307 1e-3, 1.0000016425331958690e6,
308 1e-2, 10001.621213528313220,
309 1e-1, 101.43329915079275882,
310 1, 1.6449340668482264365,
311 2, 0.64493406684822643647,
312 3, 0.39493406684822643647,
313 4, 0.28382295573711532536,
314 5, 0.22132295573711532536,
315 10, 0.10516633568168574612,
316 20, 0.051270822935203119832,
317 50, 0.020201333226697125806,
318 100, 0.010050166663333571395
319 };
320 for (int i = data.length - 2; i >= 0; i -= 2) {
321 assertEquals(data[i + 1], Gamma.trigamma(data[i]), eps, String.format("trigamma %.0f", data[i]));
322 }
323 }
324
325 @Test
326 void testTrigammaField() {
327 double eps = 1e-13;
328
329
330
331 Binary64[] data = {
332 new Binary64(1e-6), new Binary64(1.00000000000164493166e12) ,
333 new Binary64(1e-4), new Binary64(1.0000000164469368793e8) ,
334 new Binary64(1e-3), new Binary64(1.0000016425331958690e6),
335 new Binary64(1e-2), new Binary64(10001.621213528313220),
336 new Binary64(1e-1), new Binary64(101.43329915079275882),
337 new Binary64(1), new Binary64(1.6449340668482264365),
338 new Binary64(2), new Binary64(0.64493406684822643647),
339 new Binary64(3), new Binary64(0.39493406684822643647),
340 new Binary64(4), new Binary64(0.28382295573711532536),
341 new Binary64(5), new Binary64(0.22132295573711532536),
342 new Binary64(10), new Binary64(0.10516633568168574612),
343 new Binary64(20), new Binary64(0.051270822935203119832),
344 new Binary64(50), new Binary64(0.020201333226697125806),
345 new Binary64(100), new Binary64(0.010050166663333571395)
346 };
347 for (int i = data.length - 2; i >= 0; i -= 2) {
348 assertEquals(data[i + 1].getReal(),
349 Gamma.trigamma(data[i]).getReal(), eps, String.format("trigamma %.6f", data[i].getReal()));
350 }
351 }
352
353 @Test
354 void testTrigammaSmallArg() {
355 double eps = 2;
356 assertEquals(1e16,
357 Gamma.trigamma(1e-8), eps, String.format("trigamma %.8f", 1e-8));
358 }
359
360 @Test
361 void testTrigammaSmallArgField() {
362 double eps = 2;
363 assertEquals(1e16,
364 Gamma.trigamma(new Binary64(1e-8)).getReal(), eps, String.format("trigamma %.8f", 1e-8));
365 }
366
367 @Test
368 void testTrigammaNonRealArgs() {
369 assertTrue(Double.isNaN(Gamma.trigamma(Double.NaN)));
370 assertTrue(Double.isInfinite(Gamma.trigamma(Double.POSITIVE_INFINITY)));
371 assertTrue(Double.isInfinite(Gamma.trigamma(Double.NEGATIVE_INFINITY)));
372 }
373
374 @Test
375 void testTrigammaNonRealArgsField() {
376 assertTrue(Gamma.trigamma(new Binary64(Double.NaN)).isNaN());
377 assertTrue(Gamma.trigamma(new Binary64(Double.POSITIVE_INFINITY)).isInfinite());
378 assertTrue(Gamma.trigamma(new Binary64(Double.NEGATIVE_INFINITY)).isInfinite());
379 }
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398 private static final double[][] LOG_GAMMA_REF = {
399 { 0.125 , 2.019418357553796 },
400 { 0.25 , 1.288022524698077 },
401 { 0.375 , .8630739822706475 },
402 { 0.5 , .5723649429247001 },
403 { 0.625 , .3608294954889402 },
404 { 0.75 , .2032809514312954 },
405 { 0.875 , .08585870722533433 },
406 { 0.890625 , .07353860936979656 },
407 { 0.90625 , .06169536624059108 },
408 { 0.921875 , .05031670080005688 },
409 { 0.9375 , 0.0393909017345823 },
410 { 0.953125 , .02890678734595923 },
411 { 0.96875 , .01885367233441289 },
412 { 0.984375 , .009221337197578781 },
413 { 1.0 , 0.0 },
414 { 1.015625 , - 0.00881970970573307 },
415 { 1.03125 , - .01724677500176807 },
416 { 1.046875 , - .02528981394675729 },
417 { 1.0625 , - .03295710029357782 },
418 { 1.078125 , - .04025658272400143 },
419 { 1.09375 , - .04719590272716985 },
420 { 1.109375 , - .05378241123619192 },
421 { 1.125 , - .06002318412603958 },
422 { 1.25 , - .09827183642181316 },
423 { 1.375 , - .1177552707410788 },
424 { 1.5 , - .1207822376352452 },
425 { 1.625 , - .1091741337567954 },
426 { 1.75 , - .08440112102048555 },
427 { 1.875 , - 0.0476726853991883 },
428 { 1.890625 , - .04229320615532515 },
429 { 1.90625 , - .03674470657266143 },
430 { 1.921875 , - .03102893865389552 },
431 { 1.9375 , - .02514761940298887 },
432 { 1.953125 , - .01910243184040138 },
433 { 1.96875 , - .01289502598016741 },
434 { 1.984375 , - .006527019770560387 },
435 { 2.0 , 0.0 },
436 { 2.015625 , .006684476830232185 },
437 { 2.03125 , .01352488366498562 },
438 { 2.046875 , .02051972208453692 },
439 { 2.0625 , .02766752152285702 },
440 { 2.078125 , 0.0349668385135861 },
441 { 2.09375 , .04241625596251728 },
442 { 2.109375 , .05001438244545164 },
443 { 2.125 , .05775985153034387 },
444 { 2.25 , .1248717148923966 },
445 { 2.375 , .2006984603774558 },
446 { 2.5 , .2846828704729192 },
447 { 2.625 , .3763336820249054 },
448 { 2.75 , .4752146669149371 },
449 { 2.875 , .5809359740231859 },
450 { 2.890625 , .5946142560817441 },
451 { 2.90625 , .6083932548009232 },
452 { 2.921875 , .6222723333588501 },
453 { 2.9375 , .6362508628423761 },
454 { 2.953125 , .6503282221022278 },
455 { 2.96875 , .6645037976116387 },
456 { 2.984375 , 0.678776983328359 },
457 { 3.0 , .6931471805599453 },
458 { 3.015625 , .7076137978322324 },
459 { 3.03125 , .7221762507608962 },
460 { 3.046875 , .7368339619260166 },
461 { 3.0625 , 0.751586360749556 },
462 { 3.078125 , .7664328833756681 },
463 { 3.09375 , .7813729725537568 },
464 { 3.109375 , .7964060775242092 },
465 { 3.125 , 0.811531653906724 },
466 { 3.25 , .9358019311087253 },
467 { 3.375 , 1.06569589786406 },
468 { 3.5 , 1.200973602347074 },
469 { 3.625 , 1.341414578068493 },
470 { 3.75 , 1.486815578593417 },
471 { 3.875 , 1.6369886482725 },
472 { 4.0 , 1.791759469228055 },
473 { 4.125 , 1.950965937095089 },
474 { 4.25 , 2.114456927450371 },
475 { 4.375 , 2.282091222188554 },
476 { 4.5 , 2.453736570842442 },
477 { 4.625 , 2.62926886637513 },
478 { 4.75 , 2.808571418575736 },
479 { 4.875 , 2.99153431107781 },
480 { 5.0 , 3.178053830347946 },
481 { 5.125 , 3.368031956881733 },
482 { 5.25 , 3.561375910386697 },
483 { 5.375 , 3.757997741998131 },
484 { 5.5 , 3.957813967618717 },
485 { 5.625 , 4.160745237339519 },
486 { 5.75 , 4.366716036622286 },
487 { 5.875 , 4.57565441552762 },
488 { 6.0 , 4.787491742782046 },
489 { 6.125 , 5.002162481906205 },
490 { 6.25 , 5.219603986990229 },
491 { 6.375 , 5.439756316011858 },
492 { 6.5 , 5.662562059857142 },
493 { 6.625 , 5.887966185430003 },
494 { 6.75 , 6.115915891431546 },
495 { 6.875 , 6.346360475557843 },
496 { 7.0 , 6.579251212010101 },
497 { 7.125 , 6.814541238336996 },
498 { 7.25 , 7.05218545073854 },
499 { 7.375 , 7.292140407056348 },
500 { 7.5 , 7.534364236758733 },
501 { 7.625 , 7.778816557302289 },
502 { 7.75 , 8.025458396315983 },
503 { 7.875 , 8.274252119110479 },
504 { 8.0 , 8.525161361065415 },
505 { 8.125 , 8.77815096449171 },
506 { 8.25 , 9.033186919605123 },
507 { 8.375 , 9.290236309282232 },
508 { 8.5 , 9.549267257300997 },
509 { 8.625 , 9.810248879795765 },
510 { 8.75 , 10.07315123968124 },
511 { 8.875 , 10.33794530382217 },
512 { 9.0 , 10.60460290274525 },
513 { 9.125 , 10.87309669270751 },
514 { 9.25 , 11.14340011995171 },
515 { 9.375 , 11.41548738699336 },
516 { 9.5 , 11.68933342079727 },
517 { 9.625 , 11.96491384271319 },
518 { 9.75 , 12.24220494005076 },
519 { 9.875 , 12.52118363918365 },
520 { 10.0 , 12.80182748008147 },
521 { 0.8 , .1520596783998376 },
522 { 100.0 , 359.1342053695754 },
523 { 1000.0 , 5905.220423209181 },
524 { 10000.0 , 82099.71749644238 },
525 { 100000.0 , 1051287.708973657 },
526 { 1000000.0 , 1.2815504569147612e+7 },
527 { 10000000.0 , 1.511809493694739e+8 },
528 { 1.e+8 , 1.7420680661038346e+9 },
529 { 1.e+9 , 1.972326582750371e+10 },
530 { 1.e+10 , 2.202585092888106e+11 },
531 };
532
533 @Test
534 void testLogGamma() {
535 final int ulps = 3;
536 for (int i = 0; i < LOG_GAMMA_REF.length; i++) {
537 final double[] data = LOG_GAMMA_REF[i];
538 final double x = data[0];
539 final double expected = data[1];
540 final double actual = Gamma.logGamma(x);
541 final double tol;
542 if (expected == 0.0) {
543 tol = 1E-15;
544 } else {
545 tol = ulps * FastMath.ulp(expected);
546 }
547 assertEquals(expected, actual, tol, Double.toString(x));
548 }
549 }
550
551 @Test
552 void testLogGammaField() {
553 final int ulps = 3;
554 for (int i = 0; i < LOG_GAMMA_REF.length; i++) {
555 final double[] data = LOG_GAMMA_REF[i];
556 final Binary64 x = new Binary64(data[0]);
557 final double expected = data[1];
558 final double actual = Gamma.logGamma(x).getReal();
559 final double tol;
560 if (expected == 0.0) {
561 tol = 1E-15;
562 } else {
563 tol = ulps * FastMath.ulp(expected);
564 }
565 assertEquals(expected, actual, tol, Double.toString(x.getReal()));
566 }
567 }
568
569 @Test
570 void testLogGammaPrecondition1() {
571 assertTrue(Double.isNaN(Gamma.logGamma(0.0)));
572 }
573
574 @Test
575 void testLogGammaPrecondition1Field() {
576 assertTrue(Gamma.logGamma(new Binary64(0.0)).isNaN());
577 }
578
579 @Test
580 void testLogGammaPrecondition2() {
581 assertTrue(Double.isNaN(Gamma.logGamma(-1.0)));
582 }
583
584 @Test
585 void testLogGammaPrecondition2Field() {
586 assertTrue(Gamma.logGamma(new Binary64(-1.0)).isNaN());
587 }
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610 private static final double[][] INV_GAMMA1P_M1_REF = {
611 { -0.5 , -.4358104164522437 },
612 { -0.375 , -.3029021533379859 },
613 { -0.25 , -0.183951060901737 },
614 { -0.125 , -.08227611018520711 },
615 { 0.0 , 0.0 },
616 { 0.125 , .06186116458306091 },
617 { 0.25 , .1032626513208373 },
618 { 0.375 , .1249687649039041 },
619 { 0.5 , .1283791670955126 },
620 { 0.625 , .1153565546592225 },
621 { 0.75 , 0.0880652521310173 },
622 { 0.875 , .04882730264547758 },
623 { 1.0 , 0.0 },
624 { 1.125 , -.05612340925950141 },
625 { 1.25 , -.1173898789433302 },
626 { 1.375 , -.1818408982517061 },
627 { 1.5 , -0.247747221936325 },
628 };
629
630 @Test
631 void testInvGamma1pm1() {
632
633 final int ulps = 3;
634 for (int i = 0; i < INV_GAMMA1P_M1_REF.length; i++) {
635 final double[] ref = INV_GAMMA1P_M1_REF[i];
636 final double x = ref[0];
637 final double expected = ref[1];
638 final double actual = Gamma.invGamma1pm1(x);
639 final double tol = ulps * FastMath.ulp(expected);
640 assertEquals(expected, actual, tol, Double.toString(x));
641 }
642 }
643
644 @Test
645 void testInvGamma1pm1Field() {
646
647 final int ulps = 3;
648 for (int i = 0; i < INV_GAMMA1P_M1_REF.length; i++) {
649 final double[] ref = INV_GAMMA1P_M1_REF[i];
650 final Binary64 x = new Binary64(ref[0]);
651 final double expected = ref[1];
652 final double actual = Gamma.invGamma1pm1(x).getReal();
653 final double tol = ulps * FastMath.ulp(expected);
654 assertEquals(expected, actual, tol, Double.toString(x.getReal()));
655 }
656 }
657
658 @Test
659 void testInvGamma1pm1Precondition1() {
660 assertThrows(MathIllegalArgumentException.class, () -> Gamma.invGamma1pm1(-0.51));
661 }
662
663 @Test
664 void testInvGamma1pm1Precondition2() {
665 assertThrows(MathIllegalArgumentException.class, () -> Gamma.invGamma1pm1(1.51));
666 }
667
668 @Test
669 void testInvGamma1pm1Precondition1Field() {
670 assertThrows(MathIllegalArgumentException.class, () -> Gamma.invGamma1pm1(new Binary64(-0.51)));
671 }
672
673 @Test
674 void testInvGamma1pm1Precondition2Field() {
675 assertThrows(MathIllegalArgumentException.class, () -> Gamma.invGamma1pm1(new Binary64(1.51)));
676 }
677
678 private static final double[][] LOG_GAMMA1P_REF = {
679 { - 0.5 , .5723649429247001 },
680 { - 0.375 , .3608294954889402 },
681 { - 0.25 , .2032809514312954 },
682 { - 0.125 , .08585870722533433 },
683 { 0.0 , 0.0 },
684 { 0.125 , - .06002318412603958 },
685 { 0.25 , - .09827183642181316 },
686 { 0.375 , - .1177552707410788 },
687 { 0.5 , - .1207822376352452 },
688 { 0.625 , - .1091741337567954 },
689 { 0.75 , - .08440112102048555 },
690 { 0.875 , - 0.0476726853991883 },
691 { 1.0 , 0.0 },
692 { 1.125 , .05775985153034387 },
693 { 1.25 , .1248717148923966 },
694 { 1.375 , .2006984603774558 },
695 { 1.5 , .2846828704729192 },
696 };
697
698 @Test
699 void testLogGamma1p() {
700
701 final int ulps = 3;
702 for (int i = 0; i < LOG_GAMMA1P_REF.length; i++) {
703 final double[] ref = LOG_GAMMA1P_REF[i];
704 final double x = ref[0];
705 final double expected = ref[1];
706 final double actual = Gamma.logGamma1p(x);
707 final double tol = ulps * FastMath.ulp(expected);
708 assertEquals(expected, actual, tol, Double.toString(x));
709 }
710 }
711
712 @Test
713 void testLogGamma1pField() {
714
715 final int ulps = 3;
716 for (int i = 0; i < LOG_GAMMA1P_REF.length; i++) {
717 final double[] ref = LOG_GAMMA1P_REF[i];
718 final Binary64 x = new Binary64(ref[0]);
719 final double expected = ref[1];
720 final double actual = Gamma.logGamma1p(x).getReal();
721 final double tol = ulps * FastMath.ulp(expected);
722 assertEquals(expected, actual, tol, Double.toString(x.getReal()));
723 }
724 }
725
726 @Test
727 void testLogGamma1pPrecondition1() {
728 assertThrows(MathIllegalArgumentException.class, () -> Gamma.logGamma1p(-0.51));
729 }
730
731 @Test
732 void testLogGamma1pPrecondition1Field() {
733 assertThrows(MathIllegalArgumentException.class, () -> Gamma.logGamma1p(new Binary64(-0.51)));
734 }
735
736 @Test
737 void testLogGamma1pPrecondition2() {
738 assertThrows(MathIllegalArgumentException.class, () -> Gamma.logGamma1p(1.51));
739 }
740
741 @Test
742 void testLogGamma1pPrecondition2Field() {
743 assertThrows(MathIllegalArgumentException.class, () -> Gamma.logGamma1p(new Binary64(1.51)));
744 }
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766 private static final double[][] GAMMA_REF = {
767 { - 19.875 , 4.920331854832504e-18 },
768 { - 19.75 , 3.879938752480031e-18 },
769 { - 19.625 , 4.323498423815027e-18 },
770 { - 19.5 , 5.811045977502237e-18 },
771 { - 19.375 , 9.14330910942125e-18 },
772 { - 19.25 , 1.735229114436739e-17 },
773 { - 19.125 , 4.653521565668223e-17 },
774 { - 18.875 , - 9.779159561479603e-17 },
775 { - 18.75 , - 7.662879036148061e-17 },
776 { - 18.625 , - 8.484865656736991e-17 },
777 { - 18.5 , - 1.133153965612936e-16 },
778 { - 18.375 , - 1.771516139950367e-16 },
779 { - 18.25 , - 3.340316045290721e-16 },
780 { - 18.125 , - 8.899859994340475e-16 },
781 { - 17.875 , 1.845816367229275e-15 },
782 { - 17.75 , 1.436789819277761e-15 },
783 { - 17.625 , 1.580306228567265e-15 },
784 { - 17.5 , 2.096334836383932e-15 },
785 { - 17.375 , 3.255160907158799e-15 },
786 { - 17.25 , 6.096076782655566e-15 },
787 { - 17.125 , 1.613099623974211e-14 },
788 { - 16.875 , - 3.29939675642233e-14 },
789 { - 16.75 , - 2.550301929218027e-14 },
790 { - 16.625 , - 2.785289727849803e-14 },
791 { - 16.5 , - 3.66858596367188e-14 },
792 { - 16.375 , - 5.655842076188414e-14 },
793 { - 16.25 , - 1.051573245008085e-13 },
794 { - 16.125 , - 2.762433106055837e-13 },
795 { - 15.875 , 5.567732026462681e-13 },
796 { - 15.75 , 4.271755731440195e-13 },
797 { - 15.625 , 4.630544172550298e-13 },
798 { - 15.5 , 6.053166840058604e-13 },
799 { - 15.375 , 9.261441399758529e-13 },
800 { - 15.25 , 1.708806523138138e-12 },
801 { - 15.125 , 4.454423383515037e-12 },
802 { - 14.875 , - 8.838774592009505e-12 },
803 { - 14.75 , - 6.728015277018307e-12 },
804 { - 14.625 , - 7.235225269609841e-12 },
805 { - 14.5 , - 9.382408602090835e-12 },
806 { - 14.375 , - 1.423946615212874e-11 },
807 { - 14.25 , - 2.605929947785661e-11 },
808 { - 14.125 , - 6.737315367566492e-11 },
809 { - 13.875 , 1.314767720561414e-10 },
810 { - 13.75 , 9.923822533602004e-11 },
811 { - 13.625 , 1.058151695680439e-10 },
812 { - 13.5 , 1.360449247303171e-10 },
813 { - 13.375 , 2.046923259368506e-10 },
814 { - 13.25 , 3.713450175594567e-10 },
815 { - 13.125 , 9.516457956687671e-10 },
816 { - 12.875 , - 1.8242402122789617e-9 },
817 { - 12.75 , - 1.3645255983702756e-9 },
818 { - 12.625 , - 1.4417316853645984e-9 },
819 { - 12.5 , - 1.836606483859281e-9 },
820 { - 12.375 , - 2.7377598594053765e-9 },
821 { - 12.25 , - 4.9203214826628017e-9 },
822 { - 12.125 , - 1.2490351068152569e-8 },
823 { - 11.875 , 2.3487092733091633e-8 },
824 { - 11.75 , 1.7397701379221012e-8 },
825 { - 11.625 , 1.8201862527728055e-8 },
826 { - 11.5 , 2.295758104824101e-8 },
827 { - 11.375 , 3.3879778260141535e-8 },
828 { - 11.25 , 6.027393816261931e-8 },
829 { - 11.125 , 1.5144550670134987e-7 },
830 { - 10.875 , - 2.7890922620546316e-7 },
831 { - 10.75 , - 2.044229912058469e-7 },
832 { - 10.625 , - 2.1159665188483867e-7 },
833 { - 10.5 , - 2.640121820547716e-7 },
834 { - 10.375 , - 3.8538247770911e-7 },
835 { - 10.25 , - 6.780818043294673e-7 },
836 { - 10.125 , - 1.6848312620525174e-6 },
837 { - 9.875 , 3.0331378349844124e-6 },
838 { - 9.75 , 2.1975471554628537e-6 },
839 { - 9.625 , 2.2482144262764103e-6 },
840 { - 9.5 , 2.772127911575102e-6 },
841 { - 9.375 , 3.998343206232017e-6 },
842 { - 9.25 , 6.95033849437704e-6 },
843 { - 9.125 , 1.7058916528281737e-5 },
844 { - 8.875 , - 2.9952236120471065e-5 },
845 { - 8.75 , - 2.1426084765762826e-5 },
846 { - 8.625 , - 2.163906385291045e-5 },
847 { - 8.5 , - 2.633521515996347e-5 },
848 { - 8.375 , - 3.748446755842515e-5 },
849 { - 8.25 , - 6.429063107298763e-5 },
850 { - 8.125 , - 1.5566261332057085e-4 },
851 { - 7.875 , 2.658260955691807e-4 },
852 { - 7.75 , 1.874782417004247e-4 },
853 { - 7.625 , 1.8663692573135265e-4 },
854 { - 7.5 , 2.238493288596895e-4 },
855 { - 7.375 , 3.1393241580181064e-4 },
856 { - 7.25 , 5.303977063521479e-4 },
857 { - 7.125 , .001264758733229638 },
858 { - 6.875 , - .002093380502607298 },
859 { - 6.75 , - .001452956373178292 },
860 { - 6.625 , - .001423106558701564 },
861 { - 6.5 , - .001678869966447671 },
862 { - 6.375 , - .002315251566538353 },
863 { - 6.25 , - .003845383371053072 },
864 { - 6.125 , - .009011405974261174 },
865 { - 5.875 , .01439199095542518 },
866 { - 5.75 , .009807455518953468 },
867 { - 5.625 , .009428080951397862 },
868 { - 5.5 , .01091265478190986 },
869 { - 5.375 , 0.014759728736682 },
870 { - 5.25 , 0.0240336460690817 },
871 { - 5.125 , .05519486159234969 },
872 { - 4.875 , - 0.0845529468631229 },
873 { - 4.75 , - .05639286923398244 },
874 { - 4.625 , - .05303295535161297 },
875 { - 4.5 , - .06001960130050425 },
876 { - 4.375 , - .07933354195966577 },
877 { - 4.25 , - .1261766418626789 },
878 { - 4.125 , - .2828736656607921 },
879 { - 3.875 , .4121956159577241 },
880 { - 3.75 , .2678661288614166 },
881 { - 3.625 , 0.24527741850121 },
882 { - 3.5 , .2700882058522691 },
883 { - 3.375 , .3470842460735378 },
884 { - 3.25 , .5362507279163854 },
885 { - 3.125 , 1.166853870850768 },
886 { - 2.875 , - 1.597258011836181 },
887 { - 2.75 , - 1.004497983230312 },
888 { - 2.625 , - .8891306420668862 },
889 { - 2.5 , - .9453087204829419 },
890 { - 2.375 , - 1.17140933049819 },
891 { - 2.25 , - 1.742814865728253 },
892 { - 2.125 , - 3.646418346408649 },
893 { - 1.875 , 4.59211678402902 },
894 { - 1.75 , 2.762369453883359 },
895 { - 1.625 , 2.333967935425576 },
896 { - 1.5 , 2.363271801207355 },
897 { - 1.375 , 2.782097159933201 },
898 { - 1.25 , 3.921333447888569 },
899 { - 1.125 , 7.748638986118379 },
900 { - 0.875 , - 8.610218970054413 },
901 { - 0.75 , - 4.834146544295877 },
902 { - 0.625 , - 3.792697895066561 },
903 { - 0.5 , - 3.544907701811032 },
904 { - 0.375 , - 3.825383594908152 },
905 { - 0.25 , - 4.901666809860711 },
906 { - 0.125 , - 8.717218859383175 },
907 { 0.125 , 7.533941598797612 },
908 { 0.25 , 3.625609908221908 },
909 { 0.375 , 2.370436184416601 },
910 { 0.5 , 1.772453850905516 },
911 { 0.625 , 1.434518848090557 },
912 { 0.75 , 1.225416702465178 },
913 { 0.875 , 1.089652357422897 },
914 { 1.0 , 1.0 },
915 { 1.125 , .9417426998497015 },
916 { 1.25 , 0.906402477055477 },
917 { 1.375 , .8889135691562253 },
918 { 1.5 , 0.886226925452758 },
919 { 1.625 , 0.896574280056598 },
920 { 1.75 , .9190625268488832 },
921 { 1.875 , .9534458127450348 },
922 { 2.0 , 1.0 },
923 { 2.125 , 1.059460537330914 },
924 { 2.25 , 1.133003096319346 },
925 { 2.375 , 1.22225615758981 },
926 { 2.5 , 1.329340388179137 },
927 { 2.625 , 1.456933205091972 },
928 { 2.75 , 1.608359421985546 },
929 { 2.875 , 1.78771089889694 },
930 { 3.0 , 2.0 },
931 { 3.125 , 2.251353641828193 },
932 { 3.25 , 2.549256966718529 },
933 { 3.375 , 2.902858374275799 },
934 { 3.5 , 3.323350970447843 },
935 { 3.625 , 3.824449663366426 },
936 { 3.75 , 4.422988410460251 },
937 { 3.875 , 5.139668834328703 },
938 { 4.0 , 6.0 },
939 { 4.125 , 7.035480130713102 },
940 { 4.25 , 8.28508514183522 },
941 { 4.375 , 9.797147013180819 },
942 { 4.5 , 11.63172839656745 },
943 { 4.625 , 13.86363002970329 },
944 { 4.75 , 16.58620653922594 },
945 { 4.875 , 19.91621673302373 },
946 { 5.0 , 24.0 },
947 { 5.125 , 29.02135553919155 },
948 { 5.25 , 35.21161185279968 },
949 { 5.375 , 42.86251818266609 },
950 { 5.5 , 52.34277778455352 },
951 { 5.625 , 64.11928888737773 },
952 { 5.75 , 78.78448106132322 },
953 { 5.875 , 97.09155657349066 },
954 { 6.0 , 120.0 },
955 { 6.125 , 148.7344471383567 },
956 { 6.25 , 184.8609622271983 },
957 { 6.375 , 230.3860352318302 },
958 { 6.5 , 287.8852778150443 },
959 { 6.625 , 360.6709999914997 },
960 { 6.75 , 453.0107661026085 },
961 { 6.875 , 570.4128948692577 },
962 { 7.0 , 720.0 },
963 { 7.125 , 910.9984887224346 },
964 { 7.25 , 1155.38101391999 },
965 { 7.375 , 1468.710974602918 },
966 { 7.5 , 1871.254305797788 },
967 { 7.625 , 2389.445374943686 },
968 { 7.75 , 3057.822671192607 },
969 { 7.875 , 3921.588652226146 },
970 { 8.0 , 5040.0 },
971 { 8.125 , 6490.864232147346 },
972 { 8.25 , 8376.512350919926 },
973 { 8.375 , 10831.74343769652 },
974 { 8.5 , 14034.40729348341 },
975 { 8.625 , 18219.5209839456 },
976 { 8.75 , 23698.12570174271 },
977 { 8.875 , 30882.5106362809 },
978 { 9.0 , 40320.0 },
979 { 9.125 , 52738.27188619719 },
980 { 9.25 , 69106.22689508938 },
981 { 9.375 , 90715.85129070834 },
982 { 9.5 , 119292.461994609 },
983 { 9.625 , 157143.3684865308 },
984 { 9.75 , 207358.5998902487 },
985 { 9.875 , 274082.281896993 },
986 { 10.0 , 362880.0 },
987 { 10.125 , 481236.7309615494 },
988 { 10.25 , 639232.5987795768 },
989 { 10.375 , 850461.1058503906 },
990 { 10.5 , 1133278.388948786 },
991 { 10.625 , 1512504.921682859 },
992 { 10.75 , 2021746.348929925 },
993 { 10.875 , 2706562.533732806 },
994 { 11.0 , 3628800.0 },
995 { 11.125 , 4872521.900985687 },
996 { 11.25 , 6552134.137490662 },
997 { 11.375 , 8823533.973197803 },
998 { 11.5 , 1.1899423083962249e+7 },
999 { 11.625 , 1.6070364792880382e+7 },
1000 { 11.75 , 2.1733773250996688e+7 },
1001 { 11.875 , 2.943386755434427e+7 },
1002 { 12.0 , 3.99168e+7 },
1003 { 12.125 , 5.420680614846578e+7 },
1004 { 12.25 , 7.371150904676994e+7 },
1005 { 12.375 , 1.0036769894512501e+8 },
1006 { 12.5 , 1.3684336546556586e+8 },
1007 { 12.625 , 1.8681799071723443e+8 },
1008 { 12.75 , 2.5537183569921107e+8 },
1009 { 12.875 , 3.4952717720783816e+8 },
1010 { 13.0 , 4.790016e+8 },
1011 { 13.125 , 6.572575245501475e+8 },
1012 { 13.25 , 9.029659858229319e+8 },
1013 { 13.375 , 1.2420502744459219e+9 },
1014 { 13.5 , 1.7105420683195732e+9 },
1015 { 13.625 , 2.3585771328050845e+9 },
1016 { 13.75 , 3.2559909051649416e+9 },
1017 { 13.875 , 4.500162406550916e+9 },
1018 { 14.0 , 6.2270208e+9 },
1019 { 14.125 , 8.626505009720685e+9 },
1020 { 14.25 , 1.196429931215385e+10 },
1021 { 14.375 , 1.66124224207142e+10 },
1022 { 14.5 , 2.309231792231424e+10 },
1023 { 14.625 , 3.213561343446927e+10 },
1024 { 14.75 , 4.476987494601794e+10 },
1025 { 14.875 , 6.243975339089396e+10 },
1026 { 15.0 , 8.71782912e+10 },
1027 { 15.125 , 1.218493832623047e+11 },
1028 { 15.25 , 1.704912651981923e+11 },
1029 { 15.375 , 2.388035722977667e+11 },
1030 { 15.5 , 3.348386098735565e+11 },
1031 { 15.625 , 4.699833464791132e+11 },
1032 { 15.75 , 6.603556554537646e+11 },
1033 { 15.875 , 9.287913316895475e+11 },
1034 { 16.0 , 1.307674368e+12 },
1035 { 16.125 , 1.842971921842358e+12 },
1036 { 16.25 , 2.599991794272433e+12 },
1037 { 16.375 , 3.671604924078163e+12 },
1038 { 16.5 , 5.189998453040126e+12 },
1039 { 16.625 , 7.343489788736144e+12 },
1040 { 16.75 , 1.040060157339679e+13 },
1041 { 16.875 , 1.474456239057157e+13 },
1042 { 17.0 , 2.0922789888e+13 },
1043 { 17.125 , 2.971792223970803e+13 },
1044 { 17.25 , 4.224986665692704e+13 },
1045 { 17.375 , 6.012253063177992e+13 },
1046 { 17.5 , 8.563497447516206e+13 },
1047 { 17.625 , 1.220855177377384e+14 },
1048 { 17.75 , 1.742100763543963e+14 },
1049 { 17.875 , 2.488144903408952e+14 },
1050 { 18.0 , 3.55687428096e+14 },
1051 { 18.125 , 5.08919418355e+14 },
1052 { 18.25 , 7.288101998319914e+14 },
1053 { 18.375 , 1.044628969727176e+15 },
1054 { 18.5 , 1.498612053315336e+15 },
1055 { 18.625 , 2.151757250127639e+15 },
1056 { 18.75 , 3.092228855290534e+15 },
1057 { 18.875 , 4.447559014843502e+15 },
1058 { 19.0 , 6.402373705728e+15 },
1059 { 19.125 , 9.224164457684374e+15 },
1060 { 19.25 , 1.330078614693384e+16 },
1061 { 19.375 , 1.919505731873686e+16 },
1062 { 19.5 , 2.772432298633372e+16 },
1063 { 19.625 , 4.007647878362728e+16 },
1064 { 19.75 , 5.797929103669752e+16 },
1065 { 19.875 , 8.39476764051711e+16 },
1066 { 20.0 , 1.21645100408832e+17 },
1067 { 20.5 , 5.406242982335075e+17 },
1068 { 21.0 , 2.43290200817664e+18 },
1069 { 21.5 , 1.10827981137869e+19 },
1070 { 22.0 , 5.109094217170944e+19 },
1071 { 22.5 , 2.382801594464184e+20 },
1072 { 23.0 , 1.124000727777608e+21 },
1073 { 23.5 , 5.361303587544415e+21 },
1074 { 24.0 , 2.585201673888498e+22 },
1075 { 24.5 , 1.259906343072938e+23 },
1076 { 25.0 , 6.204484017332395e+23 },
1077 { 25.5 , 3.086770540528697e+24 },
1078 { 26.0 , 1.551121004333099e+25 },
1079 { 26.5 , 7.871264878348176e+25 },
1080 { 27.0 , 4.032914611266056e+26 },
1081 { 27.5 , 2.085885192762267e+27 },
1082 { 28.0 , 1.088886945041835e+28 },
1083 { 28.5 , 5.736184280096234e+28 },
1084 { 29.0 , 3.048883446117139e+29 },
1085 { 29.5 , 1.634812519827427e+30 },
1086 { 30.0 , 8.841761993739702e+30 },
1087 { 30.5 , 4.822696933490909e+31 },
1088 { 31.0 , 2.65252859812191e+32 },
1089 { 31.5 , 1.470922564714727e+33 },
1090 { 32.0 , 8.222838654177922e+33 },
1091 { 32.5 , 4.633406078851391e+34 },
1092 { 33.0 , 2.631308369336935e+35 },
1093 { 33.5 , 1.505856975626702e+36 },
1094 { 34.0 , 8.683317618811885e+36 },
1095 { 34.5 , 5.044620868349451e+37 },
1096 { 35.0 , 2.952327990396041e+38 },
1097 { 35.5 , 1.740394199580561e+39 },
1098 { 36.0 , 1.033314796638614e+40 },
1099 { 36.5 , 6.178399408510991e+40 },
1100 { 37.0 , 3.719933267899013e+41 },
1101 { 37.5 , 2.255115784106512e+42 },
1102 { 38.0 , 1.376375309122634e+43 },
1103 { 38.5 , 8.456684190399419e+43 },
1104 { 39.0 , 5.230226174666011e+44 },
1105 { 39.5 , 3.255823413303776e+45 },
1106 { 40.0 , 2.039788208119745e+46 },
1107 { 40.5 , 1.286050248254992e+47 },
1108 { 41.0 , 8.159152832478976e+47 },
1109 { 41.5 , 5.208503505432716e+48 },
1110 { 42.0 , 3.345252661316381e+49 },
1111 { 42.5 , 2.161528954754577e+50 },
1112 { 43.0 , 1.40500611775288e+51 },
1113 { 43.5 , 9.186498057706953e+51 },
1114 { 44.0 , 6.041526306337383e+52 },
1115 { 44.5 , 3.996126655102524e+53 },
1116 { 45.0 , 2.658271574788449e+54 },
1117 { 45.5 , 1.778276361520623e+55 },
1118 { 46.0 , 1.196222208654802e+56 },
1119 { 46.5 , 8.091157444918836e+56 },
1120 { 47.0 , 5.502622159812088e+57 },
1121 { 47.5 , 3.762388211887259e+58 },
1122 { 48.0 , 2.586232415111682e+59 },
1123 { 48.5 , 1.787134400646448e+60 },
1124 { 49.0 , 1.241391559253607e+61 },
1125 { 49.5 , 8.667601843135273e+61 },
1126 { 50.0 , 6.082818640342675e+62 },
1127 { 50.5 , 4.290462912351959e+63 },
1128 { 51.0 , 3.041409320171338e+64 },
1129 { 51.5 , 2.16668377073774e+65 },
1130 { 52.0 , 1.551118753287382e+66 },
1131 { 52.5 , 1.115842141929936e+67 },
1132 { 53.0 , 8.065817517094388e+67 },
1133 { 53.5 , 5.858171245132164e+68 },
1134 { 54.0 , 4.274883284060025e+69 },
1135 { 54.5 , 3.134121616145708e+70 },
1136 { 55.0 , 2.308436973392413e+71 },
1137 { 55.5 , 1.70809628079941e+72 },
1138 { 56.0 , 1.269640335365828e+73 },
1139 { 56.5 , 9.479934358436728e+73 },
1140 { 57.0 , 7.109985878048635e+74 },
1141 { 57.5 , 5.356162912516752e+75 },
1142 { 58.0 , 4.052691950487721e+76 },
1143 { 58.5 , 3.079793674697132e+77 },
1144 { 59.0 , 2.350561331282878e+78 },
1145 { 59.5 , 1.801679299697822e+79 },
1146 { 60.0 , 1.386831185456898e+80 },
1147 { 60.5 , 1.071999183320204e+81 },
1148 { 61.0 , 8.320987112741391e+81 },
1149 { 61.5 , 6.485595059087236e+82 },
1150 { 62.0 , 5.075802138772249e+83 },
1151 { 62.5 , 3.98864096133865e+84 },
1152 { 63.0 , 3.146997326038794e+85 },
1153 { 63.5 , 2.492900600836656e+86 },
1154 { 64.0 , 1.98260831540444e+87 },
1155 { 64.5 , 1.582991881531277e+88 },
1156 { 65.0 , 1.268869321858841e+89 },
1157 { 65.5 , 1.021029763587673e+90 },
1158 { 66.0 , 8.247650592082472e+90 },
1159 { 66.5 , 6.687744951499262e+91 },
1160 { 67.0 , 5.443449390774431e+92 },
1161 { 67.5 , 4.447350392747009e+93 },
1162 { 68.0 , 3.647111091818868e+94 },
1163 { 68.5 , 3.001961515104231e+95 },
1164 { 69.0 , 2.48003554243683e+96 },
1165 { 69.5 , 2.056343637846398e+97 },
1166 { 70.0 , 1.711224524281413e+98 },
1167 { 70.5 , 1.429158828303247e+99 },
1168 { 71.0 , 1.19785716699699e+100 },
1169 { 71.5 , 1.00755697395379e+101 },
1170 { 72.0 , 8.50478588567862e+101 },
1171 { 72.5 , 7.20403236376959e+102 },
1172 { 73.0 , 6.12344583768861e+103 },
1173 { 73.5 , 5.22292346373295e+104 },
1174 { 74.0 , 4.47011546151268e+105 },
1175 { 74.5 , 3.83884874584372e+106 },
1176 { 75.0 , 3.30788544151939e+107 },
1177 { 75.5 , 2.85994231565357e+108 },
1178 { 76.0 , 2.48091408113954e+109 },
1179 { 76.5 , 2.15925644831845e+110 },
1180 { 77.0 , 1.88549470166605e+111 },
1181 { 77.5 , 1.65183118296361e+112 },
1182 { 78.0 , 1.45183092028286e+113 },
1183 { 78.5 , 1.2801691667968e+114 },
1184 { 79.0 , 1.13242811782063e+115 },
1185 { 79.5 , 1.00493279593549e+116 },
1186 { 80.0 , 8.94618213078298e+116 },
1187 { 80.5 , 7.98921572768712e+117 },
1188 { 81.0 , 7.15694570462638e+118 },
1189 { 81.5 , 6.43131866078814e+119 },
1190 { 82.0 , 5.79712602074737e+120 },
1191 { 82.5 , 5.24152470854233e+121 },
1192 { 83.0 , 4.75364333701284e+122 },
1193 { 83.5 , 4.32425788454742e+123 },
1194 { 84.0 , 3.94552396972066e+124 },
1195 { 84.5 , 3.6107553335971e+125 },
1196 { 85.0 , 3.31424013456535e+126 },
1197 { 85.5 , 3.05108825688955e+127 },
1198 { 86.0 , 2.81710411438055e+128 },
1199 { 86.5 , 2.60868045964056e+129 },
1200 { 87.0 , 2.42270953836727e+130 },
1201 { 87.5 , 2.25650859758909e+131 },
1202 { 88.0 , 2.10775729837953e+132 },
1203 { 88.5 , 1.97444502289045e+133 },
1204 { 89.0 , 1.85482642257398e+134 },
1205 { 89.5 , 1.74738384525805e+135 },
1206 { 90.0 , 1.65079551609085e+136 },
1207 { 90.5 , 1.56390854150595e+137 },
1208 { 91.0 , 1.48571596448176e+138 },
1209 { 91.5 , 1.41533723006289e+139 },
1210 { 92.0 , 1.3520015276784e+140 },
1211 { 92.5 , 1.29503356550754e+141 },
1212 { 93.0 , 1.24384140546413e+142 },
1213 { 93.5 , 1.19790604809448e+143 },
1214 { 94.0 , 1.15677250708164e+144 },
1215 { 94.5 , 1.12004215496834e+145 },
1216 { 95.0 , 1.08736615665674e+146 },
1217 { 95.5 , 1.05843983644508e+147 },
1218 { 96.0 , 1.03299784882391e+148 },
1219 { 96.5 , 1.01081004380505e+149 },
1220 { 97.0 , 9.9167793487095e+149 },
1221 { 97.5 , 9.75431692271873e+150 },
1222 { 98.0 , 9.61927596824821e+151 },
1223 { 98.5 , 9.51045899965076e+152 },
1224 { 99.0 , 9.42689044888325e+153 },
1225 { 99.5 , 9.367802114656e+154 },
1226 { 100.0 , 9.33262154439441e+155 },
1227 };
1228
1229 @Test
1230 void testGamma() {
1231
1232 for (int i = 0; i < GAMMA_REF.length; i++) {
1233 final double[] ref = GAMMA_REF[i];
1234 final double x = ref[0];
1235 final double expected = ref[1];
1236 final double actual = Gamma.gamma(x);
1237 final double absX = FastMath.abs(x);
1238 final int ulps;
1239 if (absX <= 8.0) {
1240 ulps = 3;
1241 } else if (absX <= 20.0) {
1242 ulps = 5;
1243 } else if (absX <= 30.0) {
1244 ulps = 50;
1245 } else if (absX <= 50.0) {
1246 ulps = 180;
1247 } else {
1248 ulps = 500;
1249 }
1250 final double tol = ulps * FastMath.ulp(expected);
1251 assertEquals(expected, actual, tol, Double.toString(x));
1252 }
1253 }
1254
1255 @Test
1256 void testGammaField() {
1257
1258 for (int i = 0; i < GAMMA_REF.length; i++) {
1259 final double[] ref = GAMMA_REF[i];
1260 final Binary64 x = new Binary64(ref[0]);
1261 final double expected = ref[1];
1262 final double actual = Gamma.gamma(x).getReal();
1263 final Binary64 absX = x.abs();
1264 final int ulps;
1265 if (absX.getReal() <= 8.0) {
1266 ulps = 3;
1267 } else if (absX.getReal() <= 20.0) {
1268 ulps = 5;
1269 } else if (absX.getReal() <= 30.0) {
1270 ulps = 50;
1271 } else if (absX.getReal() <= 50.0) {
1272 ulps = 180;
1273 } else {
1274 ulps = 500;
1275 }
1276 final double tol = ulps * FastMath.ulp(expected);
1277 assertEquals(expected, actual, tol, Double.toString(x.getReal()));
1278 }
1279 }
1280
1281 @Test
1282 void testGammaNegativeInteger() {
1283
1284 for (int i = -100; i <= 0; i++) {
1285 assertTrue(Double.isNaN(Gamma.gamma(i)), Integer.toString(i));
1286 }
1287 }
1288
1289 @Test
1290 void testGammaNegativeIntegerField() {
1291
1292 for (int i = -100; i <= 0; i++) {
1293 assertTrue(Gamma.gamma(new Binary64(i)).isNaN(), Integer.toString(i));
1294 }
1295 }
1296
1297 @Test
1298 void testGammaNegativeDouble() {
1299
1300
1301
1302 double previousGamma = Gamma.gamma(-18.5);
1303 for (double x = -19.5; x > -25; x -= 1.0) {
1304 double gamma = Gamma.gamma(x);
1305 assertEquals( (int) FastMath.signum(previousGamma),
1306 - (int) FastMath.signum(gamma));
1307
1308 previousGamma = gamma;
1309 }
1310 }
1311
1312 @Test
1313 void testGammaNegativeDoubleField() {
1314
1315
1316
1317 Binary64 previousGamma = Gamma.gamma(new Binary64(-18.5));
1318 for (Binary64 x = new Binary64(-19.5); x.getReal() > -25; x = x.subtract(1.0)) {
1319 Binary64 gamma = Gamma.gamma(x);
1320 assertEquals( (int) FastMath.signum(previousGamma.getReal()),
1321 - (int) FastMath.signum(gamma.getReal()));
1322
1323 previousGamma = gamma;
1324 }
1325 }
1326
1327 private void checkRelativeError(String msg, double expected, double actual,
1328 double tolerance) {
1329
1330 assertEquals(expected, actual, FastMath.abs(tolerance * actual), msg);
1331 }
1332 }