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.exception.LocalizedCoreFormats;
27 import org.hipparchus.exception.MathIllegalArgumentException;
28 import org.hipparchus.exception.MathIllegalStateException;
29 import org.hipparchus.util.ContinuedFraction;
30 import org.hipparchus.util.FastMath;
31 import org.hipparchus.util.FieldContinuedFraction;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class Gamma {
64
65
66
67 public static final double GAMMA = 0.577215664901532860606512090082;
68
69
70
71
72
73 public static final double LANCZOS_G = 607.0 / 128.0;
74
75
76 private static final double DEFAULT_EPSILON = 10e-15;
77
78
79 private static final double[] LANCZOS = {
80 0.99999999999999709182,
81 57.156235665862923517,
82 -59.597960355475491248,
83 14.136097974741747174,
84 -0.49191381609762019978,
85 .33994649984811888699e-4,
86 .46523628927048575665e-4,
87 -.98374475304879564677e-4,
88 .15808870322491248884e-3,
89 -.21026444172410488319e-3,
90 .21743961811521264320e-3,
91 -.16431810653676389022e-3,
92 .84418223983852743293e-4,
93 -.26190838401581408670e-4,
94 .36899182659531622704e-5,
95 };
96
97
98 private static final double HALF_LOG_2_PI = 0.5 * FastMath.log(2.0 * FastMath.PI);
99
100
101 private static final double SQRT_TWO_PI = 2.506628274631000502;
102
103
104
105 private static final double C_LIMIT = 49;
106
107
108 private static final double S_LIMIT = 1e-8;
109
110
111
112
113
114
115
116 private static final double INV_GAMMA1P_M1_A0 = .611609510448141581788E-08;
117
118
119 private static final double INV_GAMMA1P_M1_A1 = .624730830116465516210E-08;
120
121
122 private static final double INV_GAMMA1P_M1_B1 = .203610414066806987300E+00;
123
124
125 private static final double INV_GAMMA1P_M1_B2 = .266205348428949217746E-01;
126
127
128 private static final double INV_GAMMA1P_M1_B3 = .493944979382446875238E-03;
129
130
131 private static final double INV_GAMMA1P_M1_B4 = -.851419432440314906588E-05;
132
133
134 private static final double INV_GAMMA1P_M1_B5 = -.643045481779353022248E-05;
135
136
137 private static final double INV_GAMMA1P_M1_B6 = .992641840672773722196E-06;
138
139
140 private static final double INV_GAMMA1P_M1_B7 = -.607761895722825260739E-07;
141
142
143 private static final double INV_GAMMA1P_M1_B8 = .195755836614639731882E-09;
144
145
146 private static final double INV_GAMMA1P_M1_P0 = .6116095104481415817861E-08;
147
148
149 private static final double INV_GAMMA1P_M1_P1 = .6871674113067198736152E-08;
150
151
152 private static final double INV_GAMMA1P_M1_P2 = .6820161668496170657918E-09;
153
154
155 private static final double INV_GAMMA1P_M1_P3 = .4686843322948848031080E-10;
156
157
158 private static final double INV_GAMMA1P_M1_P4 = .1572833027710446286995E-11;
159
160
161 private static final double INV_GAMMA1P_M1_P5 = -.1249441572276366213222E-12;
162
163
164 private static final double INV_GAMMA1P_M1_P6 = .4343529937408594255178E-14;
165
166
167 private static final double INV_GAMMA1P_M1_Q1 = .3056961078365221025009E+00;
168
169
170 private static final double INV_GAMMA1P_M1_Q2 = .5464213086042296536016E-01;
171
172
173 private static final double INV_GAMMA1P_M1_Q3 = .4956830093825887312020E-02;
174
175
176 private static final double INV_GAMMA1P_M1_Q4 = .2692369466186361192876E-03;
177
178
179 private static final double INV_GAMMA1P_M1_C = -.422784335098467139393487909917598E+00;
180
181
182 private static final double INV_GAMMA1P_M1_C0 = .577215664901532860606512090082402E+00;
183
184
185 private static final double INV_GAMMA1P_M1_C1 = -.655878071520253881077019515145390E+00;
186
187
188 private static final double INV_GAMMA1P_M1_C2 = -.420026350340952355290039348754298E-01;
189
190
191 private static final double INV_GAMMA1P_M1_C3 = .166538611382291489501700795102105E+00;
192
193
194 private static final double INV_GAMMA1P_M1_C4 = -.421977345555443367482083012891874E-01;
195
196
197 private static final double INV_GAMMA1P_M1_C5 = -.962197152787697356211492167234820E-02;
198
199
200 private static final double INV_GAMMA1P_M1_C6 = .721894324666309954239501034044657E-02;
201
202
203 private static final double INV_GAMMA1P_M1_C7 = -.116516759185906511211397108401839E-02;
204
205
206 private static final double INV_GAMMA1P_M1_C8 = -.215241674114950972815729963053648E-03;
207
208
209 private static final double INV_GAMMA1P_M1_C9 = .128050282388116186153198626328164E-03;
210
211
212 private static final double INV_GAMMA1P_M1_C10 = -.201348547807882386556893914210218E-04;
213
214
215 private static final double INV_GAMMA1P_M1_C11 = -.125049348214267065734535947383309E-05;
216
217
218 private static final double INV_GAMMA1P_M1_C12 = .113302723198169588237412962033074E-05;
219
220
221 private static final double INV_GAMMA1P_M1_C13 = -.205633841697760710345015413002057E-06;
222
223
224
225
226 private Gamma() {}
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251 public static double logGamma(double x) {
252 double ret;
253
254 if (Double.isNaN(x) || (x <= 0.0)) {
255 ret = Double.NaN;
256 } else if (x < 0.5) {
257 return logGamma1p(x) - FastMath.log(x);
258 } else if (x <= 2.5) {
259 return logGamma1p((x - 0.5) - 0.5);
260 } else if (x <= 8.0) {
261 final int n = (int) FastMath.floor(x - 1.5);
262 double prod = 1.0;
263 for (int i = 1; i <= n; i++) {
264 prod *= x - i;
265 }
266 return logGamma1p(x - (n + 1)) + FastMath.log(prod);
267 } else {
268 double sum = lanczos(x);
269 double tmp = x + LANCZOS_G + .5;
270 ret = ((x + .5) * FastMath.log(tmp)) - tmp +
271 HALF_LOG_2_PI + FastMath.log(sum / x);
272 }
273
274 return ret;
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 public static <T extends CalculusFieldElement<T>> T logGamma(T x) {
302 final Field<T> field = x.getField();
303 T ret;
304
305 if (x.isNaN() || (x.getReal() <= 0.0)) {
306 ret = field.getOne().multiply(Double.NaN);
307 }
308 else if (x.getReal() < 0.5) {
309 return logGamma1p(x).subtract(x.log());
310 }
311 else if (x.getReal() <= 2.5) {
312 return logGamma1p(x.subtract(1));
313 }
314 else if (x.getReal() <= 8.0) {
315 final int n = (int) x.subtract(1.5).floor().getReal();
316 T prod = field.getOne();
317 for (int i = 1; i <= n; i++) {
318 prod = prod.multiply(x.subtract(i));
319 }
320 return logGamma1p(x.subtract(n + 1)).add(prod.log());
321 }
322 else {
323 T sum = lanczos(x);
324 T tmp = x.add(LANCZOS_G + .5);
325 ret = x.add(.5).multiply(tmp.log()).subtract(tmp).add(HALF_LOG_2_PI).add(sum.divide(x).log());
326 }
327
328 return ret;
329 }
330
331
332
333
334
335
336
337
338
339 public static double regularizedGammaP(double a, double x) {
340 return regularizedGammaP(a, x, DEFAULT_EPSILON, Integer.MAX_VALUE);
341 }
342
343
344
345
346
347
348
349
350
351
352 public static <T extends CalculusFieldElement<T>> T regularizedGammaP(T a, T x) {
353 return regularizedGammaP(a, x, DEFAULT_EPSILON, Integer.MAX_VALUE);
354 }
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384 public static double regularizedGammaP(double a,
385 double x,
386 double epsilon,
387 int maxIterations) {
388 double ret;
389
390 if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) {
391 ret = Double.NaN;
392 } else if (x == 0.0) {
393 ret = 0.0;
394 } else if (x >= a + 1) {
395
396
397 ret = 1.0 - regularizedGammaQ(a, x, epsilon, maxIterations);
398 } else {
399
400 double n = 0.0;
401 double an = 1.0 / a;
402 double sum = an;
403 while (FastMath.abs(an/sum) > epsilon &&
404 n < maxIterations &&
405 sum < Double.POSITIVE_INFINITY) {
406
407 n += 1.0;
408 an *= x / (a + n);
409
410
411 sum += an;
412 }
413 if (n >= maxIterations) {
414 throw new MathIllegalStateException(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, maxIterations);
415 } else if (Double.isInfinite(sum)) {
416 ret = 1.0;
417 } else {
418 ret = FastMath.exp(-x + (a * FastMath.log(x)) - logGamma(a)) * sum;
419 }
420 }
421
422 return ret;
423 }
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454 public static <T extends CalculusFieldElement<T>> T regularizedGammaP(T a,
455 T x,
456 double epsilon,
457 int maxIterations) {
458 final Field<T> field = x.getField();
459 final T zero = field.getZero();
460 final T one = field.getOne();
461
462 T ret;
463
464 if (a.isNaN() || x.isNaN() || (a.getReal() <= 0.0) || (x.getReal() < 0.0)) {
465 ret = one.multiply(Double.NaN);
466 }
467 else if (x.getReal() == 0.0) {
468 ret = zero;
469 }
470 else if (x.getReal() >= a.add(1).getReal()) {
471
472
473 ret = one.subtract(regularizedGammaQ(a, x, epsilon, maxIterations));
474 }
475 else {
476
477 double n = 0.0;
478 T an = one.divide(a);
479 T sum = an;
480 while (an.divide(sum).abs().getReal() > epsilon &&
481 n < maxIterations &&
482 sum.getReal() < Double.POSITIVE_INFINITY) {
483
484 n += 1.0;
485 an = an.multiply(x.divide(a.add(n)));
486
487
488 sum = sum.add(an);
489 }
490 if (n >= maxIterations) {
491 throw new MathIllegalStateException(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, maxIterations);
492 }
493 else if (sum.isInfinite()) {
494 ret = one;
495 }
496 else {
497 ret = a.multiply(x.log()).subtract(logGamma(a)).subtract(x).exp().multiply(sum);
498 }
499 }
500
501 return ret;
502 }
503
504
505
506
507
508
509
510
511
512 public static double regularizedGammaQ(double a, double x) {
513 return regularizedGammaQ(a, x, DEFAULT_EPSILON, Integer.MAX_VALUE);
514 }
515
516
517
518
519
520
521
522
523
524
525 public static <T extends CalculusFieldElement<T>> T regularizedGammaQ(T a, T x) {
526 return regularizedGammaQ(a, x, DEFAULT_EPSILON, Integer.MAX_VALUE);
527 }
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554 public static double regularizedGammaQ(final double a,
555 double x,
556 double epsilon,
557 int maxIterations) {
558 double ret;
559
560 if (Double.isNaN(a) || Double.isNaN(x) || (a <= 0.0) || (x < 0.0)) {
561 ret = Double.NaN;
562 } else if (x == 0.0) {
563 ret = 1.0;
564 } else if (x < a + 1.0) {
565
566
567 ret = 1.0 - regularizedGammaP(a, x, epsilon, maxIterations);
568 } else {
569
570 ContinuedFraction cf = new ContinuedFraction() {
571
572
573 @Override
574 protected double getA(int n, double x) {
575 return ((2.0 * n) + 1.0) - a + x;
576 }
577
578
579 @Override
580 protected double getB(int n, double x) {
581 return n * (a - n);
582 }
583 };
584
585 ret = 1.0 / cf.evaluate(x, epsilon, maxIterations);
586 ret = FastMath.exp(-x + (a * FastMath.log(x)) - logGamma(a)) * ret;
587 }
588
589 return ret;
590 }
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618 public static <T extends CalculusFieldElement<T>> T regularizedGammaQ(final T a,
619 T x,
620 double epsilon,
621 int maxIterations) {
622 final Field<T> field = x.getField();
623 final T one = field.getOne();
624
625 T ret;
626
627 if (a.isNaN() || x.isNaN() || a.getReal() <= 0.0 || x.getReal() < 0.0) {
628 ret = field.getOne().multiply(Double.NaN);
629 }
630 else if (x.getReal() == 0.0) {
631 ret = one;
632 }
633 else if (x.getReal() < a.add(1.0).getReal()) {
634
635
636 ret = one.subtract(regularizedGammaP(a, x, epsilon, maxIterations));
637 }
638 else {
639
640 FieldContinuedFraction cf = new FieldContinuedFraction() {
641
642
643 @Override
644 @SuppressWarnings("unchecked")
645 public <C extends CalculusFieldElement<C>> C getA(final int n, final C x) {
646 return x.subtract((C) a).add((2.0 * n) + 1.0);
647 }
648
649
650 @Override
651 @SuppressWarnings("unchecked")
652 public <C extends CalculusFieldElement<C>> C getB(final int n, final C x) {
653 return (C) a.subtract(n).multiply(n);
654 }
655 };
656
657 ret = one.divide(cf.evaluate(x, epsilon, maxIterations));
658 ret = a.multiply(x.log()).subtract(logGamma(a)).subtract(x).exp().multiply(ret);
659 }
660
661 return ret;
662 }
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683 public static double digamma(double x) {
684 if (Double.isNaN(x) || Double.isInfinite(x)) {
685 return x;
686 }
687
688 if (x > 0 && x <= S_LIMIT) {
689
690
691 return -GAMMA - 1 / x;
692 }
693 if (x >= C_LIMIT) {
694
695 double inv = 1 / (x * x);
696
697
698
699 return FastMath.log(x) - 0.5 / x - inv * ((1.0 / 12) + inv * (1.0 / 120 - inv * (1.0 / 252 + inv *
700 (1.0 / 240 - inv * (5.0 / 660 + inv * (691.0 / 32760 - inv / 12))))));
701 }
702
703 return digamma(x + 1) - 1 / x;
704 }
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726 public static <T extends CalculusFieldElement<T>> T digamma(T x) {
727 if (x.isNaN() || x.isInfinite()) {
728 return x;
729 }
730
731 if (x.getReal() > 0 && x.getReal() <= S_LIMIT) {
732
733
734 return x.pow(-1).negate().subtract(GAMMA);
735 }
736
737 if (x.getReal() >= C_LIMIT) {
738
739 T inv = x.square().reciprocal();
740
741
742
743 return x.log().subtract(x.pow(-1).multiply(0.5)).add(
744 inv.multiply(
745 inv.multiply(
746 inv.multiply(
747 inv.multiply(
748 inv.multiply(
749 inv.multiply(inv.divide(-12.)
750 .add(691. / 32760))
751 .subtract(5. / 660))
752 .add(1.0 / 240))
753 .subtract(1.0 / 252))
754 .add(1.0 / 120))
755 .subtract(1.0 / 12)));
756 }
757
758 return digamma(x.add(1.)).subtract(x.pow(-1));
759 }
760
761
762
763
764
765
766
767
768
769
770
771 public static double trigamma(double x) {
772 if (Double.isNaN(x) || Double.isInfinite(x)) {
773 return x;
774 }
775
776 if (x > 0 && x <= S_LIMIT) {
777 return 1 / (x * x);
778 }
779
780 if (x >= C_LIMIT) {
781 double inv = 1 / (x * x);
782
783
784
785
786 return 1 / x + inv * 0.5 + inv / x * (1.0 / 6 - inv * (1.0 / 30 + inv * (1.0 / 42 - inv * (1.0 / 30 + inv *
787 (5.0 / 66 - inv * (691. / 2730 + inv * 7. / 15))))));
788 }
789
790 return trigamma(x + 1) + 1 / (x * x);
791 }
792
793
794
795
796
797
798
799
800
801
802
803
804 public static <T extends CalculusFieldElement<T>> T trigamma(T x) {
805 if (x.isNaN() || x.isInfinite()) {
806 return x;
807 }
808
809 if (x.getReal() > 0 && x.getReal() <= S_LIMIT) {
810
811
812 return x.square().reciprocal();
813 }
814
815 if (x.getReal() >= C_LIMIT) {
816
817 T inv = x.square().reciprocal();
818 T invCub = inv.multiply(x.reciprocal());
819
820
821
822
823 return x.pow(-1).add(
824 inv.multiply(0.5)).add(
825 invCub.multiply(
826 inv.multiply(
827 inv.multiply(
828 inv.multiply(
829 inv.multiply(
830 inv.multiply(inv.multiply(7. / 6)
831 .subtract(691. / 2730))
832 .add(5. / 66))
833 .subtract(1.0 / 30))
834 .add(1.0 / 42))
835 .subtract(1.0 / 30))
836 .add(1.0 / 6)));
837 }
838
839 return trigamma(x.add(1.)).add(x.square().reciprocal());
840 }
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861 public static double lanczos(final double x) {
862 double sum = 0.0;
863 for (int i = LANCZOS.length - 1; i > 0; --i) {
864 sum += LANCZOS[i] / (x + i);
865 }
866 return sum + LANCZOS[0];
867 }
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889 public static <T extends CalculusFieldElement<T>> T lanczos(final T x) {
890 final Field<T> field = x.getField();
891 T sum = field.getZero();
892 for (int i = LANCZOS.length - 1; i > 0; --i) {
893 sum = sum.add(x.add(i).pow(-1.).multiply(LANCZOS[i]));
894 }
895 return sum.add(LANCZOS[0]);
896 }
897
898
899
900
901
902
903
904
905
906
907
908
909 public static double invGamma1pm1(final double x) {
910
911 if (x < -0.5) {
912 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
913 x, -0.5);
914 }
915 if (x > 1.5) {
916 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
917 x, 1.5);
918 }
919
920 final double ret;
921 final double t = x <= 0.5 ? x : (x - 0.5) - 0.5;
922 if (t < 0.0) {
923 final double a = INV_GAMMA1P_M1_A0 + t * INV_GAMMA1P_M1_A1;
924 double b = INV_GAMMA1P_M1_B8;
925 b = INV_GAMMA1P_M1_B7 + t * b;
926 b = INV_GAMMA1P_M1_B6 + t * b;
927 b = INV_GAMMA1P_M1_B5 + t * b;
928 b = INV_GAMMA1P_M1_B4 + t * b;
929 b = INV_GAMMA1P_M1_B3 + t * b;
930 b = INV_GAMMA1P_M1_B2 + t * b;
931 b = INV_GAMMA1P_M1_B1 + t * b;
932 b = 1.0 + t * b;
933
934 double c = INV_GAMMA1P_M1_C13 + t * (a / b);
935 c = INV_GAMMA1P_M1_C12 + t * c;
936 c = INV_GAMMA1P_M1_C11 + t * c;
937 c = INV_GAMMA1P_M1_C10 + t * c;
938 c = INV_GAMMA1P_M1_C9 + t * c;
939 c = INV_GAMMA1P_M1_C8 + t * c;
940 c = INV_GAMMA1P_M1_C7 + t * c;
941 c = INV_GAMMA1P_M1_C6 + t * c;
942 c = INV_GAMMA1P_M1_C5 + t * c;
943 c = INV_GAMMA1P_M1_C4 + t * c;
944 c = INV_GAMMA1P_M1_C3 + t * c;
945 c = INV_GAMMA1P_M1_C2 + t * c;
946 c = INV_GAMMA1P_M1_C1 + t * c;
947 c = INV_GAMMA1P_M1_C + t * c;
948 if (x > 0.5) {
949 ret = t * c / x;
950 } else {
951 ret = x * ((c + 0.5) + 0.5);
952 }
953 } else {
954 double p = INV_GAMMA1P_M1_P6;
955 p = INV_GAMMA1P_M1_P5 + t * p;
956 p = INV_GAMMA1P_M1_P4 + t * p;
957 p = INV_GAMMA1P_M1_P3 + t * p;
958 p = INV_GAMMA1P_M1_P2 + t * p;
959 p = INV_GAMMA1P_M1_P1 + t * p;
960 p = INV_GAMMA1P_M1_P0 + t * p;
961
962 double q = INV_GAMMA1P_M1_Q4;
963 q = INV_GAMMA1P_M1_Q3 + t * q;
964 q = INV_GAMMA1P_M1_Q2 + t * q;
965 q = INV_GAMMA1P_M1_Q1 + t * q;
966 q = 1.0 + t * q;
967
968 double c = INV_GAMMA1P_M1_C13 + (p / q) * t;
969 c = INV_GAMMA1P_M1_C12 + t * c;
970 c = INV_GAMMA1P_M1_C11 + t * c;
971 c = INV_GAMMA1P_M1_C10 + t * c;
972 c = INV_GAMMA1P_M1_C9 + t * c;
973 c = INV_GAMMA1P_M1_C8 + t * c;
974 c = INV_GAMMA1P_M1_C7 + t * c;
975 c = INV_GAMMA1P_M1_C6 + t * c;
976 c = INV_GAMMA1P_M1_C5 + t * c;
977 c = INV_GAMMA1P_M1_C4 + t * c;
978 c = INV_GAMMA1P_M1_C3 + t * c;
979 c = INV_GAMMA1P_M1_C2 + t * c;
980 c = INV_GAMMA1P_M1_C1 + t * c;
981 c = INV_GAMMA1P_M1_C0 + t * c;
982
983 if (x > 0.5) {
984 ret = (t / x) * ((c - 0.5) - 0.5);
985 } else {
986 ret = x * c;
987 }
988 }
989
990 return ret;
991 }
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005 public static <T extends CalculusFieldElement<T>> T invGamma1pm1(final T x) {
1006 final T one = x.getField().getOne();
1007
1008 if (x.getReal() < -0.5) {
1009 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
1010 x, -0.5);
1011 }
1012 if (x.getReal() > 1.5) {
1013 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
1014 x, 1.5);
1015 }
1016
1017 final T ret;
1018 final T t = x.getReal() <= 0.5 ? x : x.subtract(1);
1019 if (t.getReal() < 0.0) {
1020 final T a = one.newInstance(INV_GAMMA1P_M1_A0).add(t.multiply(INV_GAMMA1P_M1_A1));
1021 T b = one.newInstance(INV_GAMMA1P_M1_B8);
1022 b = t.multiply(b).add(INV_GAMMA1P_M1_B7);
1023 b = t.multiply(b).add(INV_GAMMA1P_M1_B6);
1024 b = t.multiply(b).add(INV_GAMMA1P_M1_B5);
1025 b = t.multiply(b).add(INV_GAMMA1P_M1_B4);
1026 b = t.multiply(b).add(INV_GAMMA1P_M1_B3);
1027 b = t.multiply(b).add(INV_GAMMA1P_M1_B2);
1028 b = t.multiply(b).add(INV_GAMMA1P_M1_B1);
1029 b = t.multiply(b).add(1.);
1030
1031 T c = one.newInstance(INV_GAMMA1P_M1_C13).add(t.multiply(a.divide(b)));
1032 c = t.multiply(c).add(INV_GAMMA1P_M1_C12);
1033 c = t.multiply(c).add(INV_GAMMA1P_M1_C11);
1034 c = t.multiply(c).add(INV_GAMMA1P_M1_C10);
1035 c = t.multiply(c).add(INV_GAMMA1P_M1_C9);
1036 c = t.multiply(c).add(INV_GAMMA1P_M1_C8);
1037 c = t.multiply(c).add(INV_GAMMA1P_M1_C7);
1038 c = t.multiply(c).add(INV_GAMMA1P_M1_C6);
1039 c = t.multiply(c).add(INV_GAMMA1P_M1_C5);
1040 c = t.multiply(c).add(INV_GAMMA1P_M1_C4);
1041 c = t.multiply(c).add(INV_GAMMA1P_M1_C3);
1042 c = t.multiply(c).add(INV_GAMMA1P_M1_C2);
1043 c = t.multiply(c).add(INV_GAMMA1P_M1_C1);
1044 c = t.multiply(c).add(INV_GAMMA1P_M1_C);
1045 if (x.getReal() > 0.5) {
1046 ret = t.multiply(c).divide(x);
1047 }
1048 else {
1049 ret = x.multiply(c.add(1));
1050 }
1051 }
1052 else {
1053 T p = one.newInstance(INV_GAMMA1P_M1_P6);
1054 p = t.multiply(p).add(INV_GAMMA1P_M1_P5);
1055 p = t.multiply(p).add(INV_GAMMA1P_M1_P4);
1056 p = t.multiply(p).add(INV_GAMMA1P_M1_P3);
1057 p = t.multiply(p).add(INV_GAMMA1P_M1_P2);
1058 p = t.multiply(p).add(INV_GAMMA1P_M1_P1);
1059 p = t.multiply(p).add(INV_GAMMA1P_M1_P0);
1060
1061 T q = one.newInstance(INV_GAMMA1P_M1_Q4);
1062 q = t.multiply(q).add(INV_GAMMA1P_M1_Q3);
1063 q = t.multiply(q).add(INV_GAMMA1P_M1_Q2);
1064 q = t.multiply(q).add(INV_GAMMA1P_M1_Q1);
1065 q = t.multiply(q).add(1.);
1066
1067 T c = one.newInstance(INV_GAMMA1P_M1_C13).add(t.multiply(p.divide(q)));
1068 c = t.multiply(c).add(INV_GAMMA1P_M1_C12);
1069 c = t.multiply(c).add(INV_GAMMA1P_M1_C11);
1070 c = t.multiply(c).add(INV_GAMMA1P_M1_C10);
1071 c = t.multiply(c).add(INV_GAMMA1P_M1_C9);
1072 c = t.multiply(c).add(INV_GAMMA1P_M1_C8);
1073 c = t.multiply(c).add(INV_GAMMA1P_M1_C7);
1074 c = t.multiply(c).add(INV_GAMMA1P_M1_C6);
1075 c = t.multiply(c).add(INV_GAMMA1P_M1_C5);
1076 c = t.multiply(c).add(INV_GAMMA1P_M1_C4);
1077 c = t.multiply(c).add(INV_GAMMA1P_M1_C3);
1078 c = t.multiply(c).add(INV_GAMMA1P_M1_C2);
1079 c = t.multiply(c).add(INV_GAMMA1P_M1_C1);
1080 c = t.multiply(c).add(INV_GAMMA1P_M1_C0);
1081
1082 if (x.getReal() > 0.5) {
1083 ret = t.divide(x).multiply(c.subtract(1));
1084 }
1085 else {
1086 ret = x.multiply(c);
1087 }
1088 }
1089
1090 return ret;
1091 }
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103 public static double logGamma1p(final double x)
1104 throws MathIllegalArgumentException {
1105
1106 if (x < -0.5) {
1107 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
1108 x, -0.5);
1109 }
1110 if (x > 1.5) {
1111 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
1112 x, 1.5);
1113 }
1114
1115 return -FastMath.log1p(invGamma1pm1(x));
1116 }
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129 public static <T extends CalculusFieldElement<T>> T logGamma1p(final T x)
1130 throws MathIllegalArgumentException {
1131
1132 if (x.getReal() < -0.5) {
1133 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
1134 x, -0.5);
1135 }
1136 if (x.getReal() > 1.5) {
1137 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE,
1138 x, 1.5);
1139 }
1140
1141 return invGamma1pm1(x).log1p().negate();
1142 }
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153 public static double gamma(final double x) {
1154
1155 if ((x == FastMath.rint(x)) && (x <= 0.0)) {
1156 return Double.NaN;
1157 }
1158
1159 final double ret;
1160 final double absX = FastMath.abs(x);
1161 if (absX <= 20.0) {
1162 if (x >= 1.0) {
1163
1164
1165
1166
1167
1168
1169
1170
1171 double prod = 1.0;
1172 double t = x;
1173 while (t > 2.5) {
1174 t -= 1.0;
1175 prod *= t;
1176 }
1177 ret = prod / (1.0 + invGamma1pm1(t - 1.0));
1178 } else {
1179
1180
1181
1182
1183
1184
1185
1186 double prod = x;
1187 double t = x;
1188 while (t < -0.5) {
1189 t += 1.0;
1190 prod *= t;
1191 }
1192 ret = 1.0 / (prod * (1.0 + invGamma1pm1(t)));
1193 }
1194 } else {
1195 final double y = absX + LANCZOS_G + 0.5;
1196 final double gammaAbs = SQRT_TWO_PI / absX *
1197 FastMath.pow(y, absX + 0.5) *
1198 FastMath.exp(-y) * lanczos(absX);
1199 if (x > 0.0) {
1200 ret = gammaAbs;
1201 } else {
1202
1203
1204
1205
1206
1207
1208
1209
1210 ret = -FastMath.PI /
1211 (x * FastMath.sin(FastMath.PI * x) * gammaAbs);
1212 }
1213 }
1214 return ret;
1215 }
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226 public static <T extends CalculusFieldElement<T>> T gamma(final T x) {
1227 final T one = x.getField().getOne();
1228
1229 if ((x.getReal() == x.rint().getReal()) && (x.getReal() <= 0.0)) {
1230 return one.multiply(Double.NaN);
1231 }
1232
1233 final T ret;
1234 final T absX = x.abs();
1235 if (absX.getReal() <= 20.0) {
1236 if (x.getReal() >= 1.0) {
1237
1238
1239
1240
1241
1242
1243
1244
1245 T prod = one;
1246 T t = x;
1247 while (t.getReal() > 2.5) {
1248 t = t.subtract(1.0);
1249 prod = prod.multiply(t);
1250 }
1251 ret = prod.divide(invGamma1pm1(t.subtract(1.0)).add(1.0));
1252 }
1253 else {
1254
1255
1256
1257
1258
1259
1260
1261 T prod = x;
1262 T t = x;
1263 while (t.getReal() < -0.5) {
1264 t = t.add(1.0);
1265 prod = prod.multiply(t);
1266 }
1267 ret = prod.multiply(invGamma1pm1(t).add(1)).reciprocal();
1268 }
1269 }
1270 else {
1271 final T y = absX.add(LANCZOS_G + 0.5);
1272 final T gammaAbs = absX.reciprocal().multiply(SQRT_TWO_PI).multiply(y.pow(absX.add(0.5)))
1273 .multiply(y.negate().exp()).multiply(lanczos(absX));
1274 if (x.getReal() > 0.0) {
1275 ret = gammaAbs;
1276 }
1277 else {
1278
1279
1280
1281
1282
1283
1284
1285
1286 ret = x.multiply(x.multiply(FastMath.PI).sin()).multiply(gammaAbs).reciprocal().multiply(-FastMath.PI);
1287 }
1288 }
1289 return ret;
1290 }
1291 }