-
Notifications
You must be signed in to change notification settings - Fork 1
/
beta_gamma_psi.F
2081 lines (1778 loc) · 67.1 KB
/
beta_gamma_psi.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
!--------------------------------------------------------------------------------------------------!
! CP2K: A general program to perform molecular dynamics simulations !
! Copyright 2000-2024 CP2K developers group <https://cp2k.org> !
! !
! SPDX-License-Identifier: GPL-2.0-or-later !
!--------------------------------------------------------------------------------------------------!
! **************************************************************************************************
MODULE beta_gamma_psi
! not tested in the case where dp would stand for single precision
! Routines for the beta function are untested
USE kinds, ONLY: dp
#include "./base/base_uses.f90"
IMPLICIT NONE
PRIVATE
PUBLIC :: psi
CONTAINS
! **************************************************************************************************
!> \brief ...
!> \param i ...
!> \return ...
! **************************************************************************************************
FUNCTION ipmpar(i) RESULT(fn_val)
!-----------------------------------------------------------------------
! IPMPAR PROVIDES THE INTEGER MACHINE CONSTANTS FOR THE COMPUTER
! THAT IS USED. IT IS ASSUMED THAT THE ARGUMENT I IS AN INTEGER
! HAVING ONE OF THE VALUES 1-10. IPMPAR(I) HAS THE VALUE ...
! INTEGERS.
! ASSUME INTEGERS ARE REPRESENTED IN THE N-DIGIT, BASE-A FORM
! SIGN ( X(N-1)*A**(N-1) + ... + X(1)*A + X(0) )
! WHERE 0 .LE. X(I) .LT. A FOR I=0,...,N-1.
! IPMPAR(1) = A, THE BASE (radix).
! IPMPAR(2) = N, THE NUMBER OF BASE-A DIGITS (digits).
! IPMPAR(3) = A**N - 1, THE LARGEST MAGNITUDE (huge).
! FLOATING-POINT NUMBERS.
! IT IS ASSUMED THAT THE SINGLE AND DOUBLE PRECISION FLOATING
! POINT ARITHMETICS HAVE THE SAME BASE, SAY B, AND THAT THE
! NONZERO NUMBERS ARE REPRESENTED IN THE FORM
! SIGN (B**E) * (X(1)/B + ... + X(M)/B**M)
! WHERE X(I) = 0,1,...,B-1 FOR I=1,...,M,
! X(1) .GE. 1, AND EMIN .LE. E .LE. EMAX.
! IPMPAR(4) = B, THE BASE.
! SINGLE-PRECISION
! IPMPAR(5) = M, THE NUMBER OF BASE-B DIGITS.
! IPMPAR(6) = EMIN, THE SMALLEST EXPONENT E.
! IPMPAR(7) = EMAX, THE LARGEST EXPONENT E.
! DOUBLE-PRECISION
! IPMPAR(8) = M, THE NUMBER OF BASE-B DIGITS.
! IPMPAR(9) = EMIN, THE SMALLEST EXPONENT E.
! IPMPAR(10) = EMAX, THE LARGEST EXPONENT E.
!-----------------------------------------------------------------------
INTEGER, INTENT(IN) :: i
INTEGER :: fn_val
SELECT CASE (i)
CASE (1)
fn_val = RADIX(i)
CASE (2)
fn_val = DIGITS(i)
CASE (3)
fn_val = HUGE(i)
CASE (4)
fn_val = RADIX(1.0)
CASE (5)
fn_val = DIGITS(1.0)
CASE (6)
fn_val = MINEXPONENT(1.0)
CASE (7)
fn_val = MAXEXPONENT(1.0)
CASE (8)
fn_val = DIGITS(1.0e0_dp)
CASE (9)
fn_val = MINEXPONENT(1.0e0_dp)
CASE (10)
fn_val = MAXEXPONENT(1.0e0_dp)
CASE DEFAULT
CPABORT("unknown case")
END SELECT
END FUNCTION ipmpar
! **************************************************************************************************
!> \brief ...
!> \param i ...
!> \return ...
! **************************************************************************************************
FUNCTION dpmpar(i) RESULT(fn_val)
!-----------------------------------------------------------------------
! DPMPAR PROVIDES THE DOUBLE PRECISION MACHINE CONSTANTS FOR
! THE COMPUTER BEING USED. IT IS ASSUMED THAT THE ARGUMENT
! I IS AN INTEGER HAVING ONE OF THE VALUES 1, 2, OR 3. IF THE
! DOUBLE PRECISION ARITHMETIC BEING USED HAS M BASE B DIGITS AND
! ITS SMALLEST AND LARGEST EXPONENTS ARE EMIN AND EMAX, THEN
! DPMPAR(1) = B**(1 - M), THE MACHINE PRECISION,
! DPMPAR(2) = B**(EMIN - 1), THE SMALLEST MAGNITUDE,
! DPMPAR(3) = B**EMAX*(1 - B**(-M)), THE LARGEST MAGNITUDE.
!-----------------------------------------------------------------------
INTEGER, INTENT(IN) :: i
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: one = 1._dp
! Local variable
SELECT CASE (i)
CASE (1)
fn_val = EPSILON(one)
CASE (2)
fn_val = TINY(one)
CASE (3)
fn_val = HUGE(one)
END SELECT
END FUNCTION dpmpar
! **************************************************************************************************
!> \brief ...
!> \param l ...
!> \return ...
! **************************************************************************************************
FUNCTION dxparg(l) RESULT(fn_val)
!--------------------------------------------------------------------
! IF L = 0 THEN DXPARG(L) = THE LARGEST POSITIVE W FOR WHICH
! DEXP(W) CAN BE COMPUTED.
!
! IF L IS NONZERO THEN DXPARG(L) = THE LARGEST NEGATIVE W FOR
! WHICH THE COMPUTED VALUE OF DEXP(W) IS NONZERO.
!
! NOTE... ONLY AN APPROXIMATE VALUE FOR DXPARG(L) IS NEEDED.
!--------------------------------------------------------------------
INTEGER, INTENT(IN) :: l
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: one = 1._dp
! Local variable
IF (l == 0) THEN
fn_val = LOG(HUGE(one))
ELSE
fn_val = LOG(TINY(one))
END IF
END FUNCTION dxparg
! **************************************************************************************************
!> \brief ...
!> \param a ...
!> \return ...
! **************************************************************************************************
FUNCTION alnrel(a) RESULT(fn_val)
!-----------------------------------------------------------------------
! EVALUATION OF THE FUNCTION LN(1 + A)
!-----------------------------------------------------------------------
REAL(dp), INTENT(IN) :: a
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: half = 0.5e0_dp, one = 1.e0_dp, p1 = -.129418923021993e+01_dp, &
p2 = .405303492862024e+00_dp, p3 = -.178874546012214e-01_dp, &
q1 = -.162752256355323e+01_dp, q2 = .747811014037616e+00_dp, &
q3 = -.845104217945565e-01_dp, two = 2.e0_dp, zero = 0.e0_dp
REAL(dp) :: t, t2, w, x
!--------------------------
IF (ABS(a) <= 0.375e0_dp) THEN
t = a/(a + two)
t2 = t*t
w = (((p3*t2 + p2)*t2 + p1)*t2 + one)/(((q3*t2 + q2)*t2 + q1)*t2 + one)
fn_val = two*t*w
ELSE
x = one + a
IF (a < zero) x = (a + half) + half
fn_val = LOG(x)
END IF
END FUNCTION alnrel
! **************************************************************************************************
!> \brief ...
!> \param a ...
!> \return ...
! **************************************************************************************************
FUNCTION gam1(a) RESULT(fn_val)
!-----------------------------------------------------------------------
! COMPUTATION OF 1/GAMMA(A+1) - 1 FOR -0.5 <= A <= 1.5
!-----------------------------------------------------------------------
REAL(dp), INTENT(IN) :: a
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: p(7) = (/.577215664901533e+00_dp, -.409078193005776e+00_dp, &
-.230975380857675e+00_dp, .597275330452234e-01_dp, .766968181649490e-02_dp, &
-.514889771323592e-02_dp, .589597428611429e-03_dp/), q(5) = (/.100000000000000e+01_dp, &
.427569613095214e+00_dp, .158451672430138e+00_dp, .261132021441447e-01_dp, &
.423244297896961e-02_dp/), r(9) = (/-.422784335098468e+00_dp, -.771330383816272e+00_dp, &
-.244757765222226e+00_dp, .118378989872749e+00_dp, .930357293360349e-03_dp, &
-.118290993445146e-01_dp, .223047661158249e-02_dp, .266505979058923e-03_dp, &
-.132674909766242e-03_dp/)
REAL(dp), PARAMETER :: s1 = .273076135303957e+00_dp, s2 = .559398236957378e-01_dp
REAL(dp) :: bot, d, t, top, w
t = a
d = a - 0.5e0_dp
IF (d > 0.0e0_dp) t = d - 0.5e0_dp
IF (t > 0.e0_dp) THEN
top = (((((p(7)*t + p(6))*t + p(5))*t + p(4))*t + p(3))*t + p(2))*t + p(1)
bot = (((q(5)*t + q(4))*t + q(3))*t + q(2))*t + 1.0e0_dp
w = top/bot
IF (d > 0.0e0_dp) THEN
fn_val = (t/a)*((w - 0.5e0_dp) - 0.5e0_dp)
ELSE
fn_val = a*w
END IF
ELSE IF (t < 0.e0_dp) THEN
top = (((((((r(9)*t + r(8))*t + r(7))*t + r(6))*t + r(5))*t &
+ r(4))*t + r(3))*t + r(2))*t + r(1)
bot = (s2*t + s1)*t + 1.0e0_dp
w = top/bot
IF (d > 0.0e0_dp) THEN
fn_val = t*w/a
ELSE
fn_val = a*((w + 0.5e0_dp) + 0.5e0_dp)
END IF
ELSE
fn_val = 0.0e0_dp
END IF
END FUNCTION gam1
! **************************************************************************************************
!> \brief ...
!> \param a ...
!> \param b ...
!> \return ...
! **************************************************************************************************
FUNCTION algdiv(a, b) RESULT(fn_val)
!-----------------------------------------------------------------------
! COMPUTATION OF LN(GAMMA(B)/GAMMA(A+B)) WHEN B >= 8
! --------
! IN THIS ALGORITHM, DEL(X) IS THE FUNCTION DEFINED BY
! LN(GAMMA(X)) = (X - 0.5)*LN(X) - X + 0.5*LN(2*PI) + DEL(X).
!-----------------------------------------------------------------------
REAL(dp), INTENT(IN) :: a, b
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: c0 = .833333333333333e-01_dp, c1 = -.277777777760991e-02_dp, &
c2 = .793650666825390e-03_dp, c3 = -.595202931351870e-03_dp, &
c4 = .837308034031215e-03_dp, c5 = -.165322962780713e-02_dp
REAL(dp) :: c, d, h, s11, s3, s5, s7, s9, t, u, v, &
w, x, x2
IF (a > b) THEN
h = b/a
c = 1.0e0_dp/(1.0e0_dp + h)
x = h/(1.0e0_dp + h)
d = a + (b - 0.5e0_dp)
ELSE
h = a/b
c = h/(1.0e0_dp + h)
x = 1.0e0_dp/(1.0e0_dp + h)
d = b + (a - 0.5e0_dp)
END IF
! SET SN = (1 - X**N)/(1 - X)
x2 = x*x
s3 = 1.0e0_dp + (x + x2)
s5 = 1.0e0_dp + (x + x2*s3)
s7 = 1.0e0_dp + (x + x2*s5)
s9 = 1.0e0_dp + (x + x2*s7)
s11 = 1.0e0_dp + (x + x2*s9)
! SET W = DEL(B) - DEL(A + B)
t = (1.0e0_dp/b)**2
w = ((((c5*s11*t + c4*s9)*t + c3*s7)*t + c2*s5)*t + c1*s3)*t + c0
w = w*(c/b)
! COMBINE THE RESULTS
u = d*alnrel(a/b)
v = a*(LOG(b) - 1.0e0_dp)
IF (u > v) THEN
fn_val = (w - v) - u
ELSE
fn_val = (w - u) - v
END IF
END FUNCTION algdiv
! **************************************************************************************************
!> \brief ...
!> \param x ...
!> \return ...
! **************************************************************************************************
FUNCTION rexp(x) RESULT(fn_val)
!-----------------------------------------------------------------------
! EVALUATION OF THE FUNCTION EXP(X) - 1
!-----------------------------------------------------------------------
REAL(dp), INTENT(IN) :: x
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: p1 = .914041914819518e-09_dp, p2 = .238082361044469e-01_dp, &
q1 = -.499999999085958e+00_dp, q2 = .107141568980644e+00_dp, &
q3 = -.119041179760821e-01_dp, q4 = .595130811860248e-03_dp
REAL(dp) :: e
IF (ABS(x) < 0.15e0_dp) THEN
fn_val = x*(((p2*x + p1)*x + 1.0e0_dp)/((((q4*x + q3)*x + q2)*x + q1)*x + 1.0e0_dp))
RETURN
END IF
IF (x < 0.0e0_dp) THEN
IF (x > -37.0e0_dp) THEN
fn_val = (EXP(x) - 0.5e0_dp) - 0.5e0_dp
ELSE
fn_val = -1.0e0_dp
END IF
ELSE
e = EXP(x)
fn_val = e*(0.5e0_dp + (0.5e0_dp - 1.0e0_dp/e))
END IF
END FUNCTION rexp
! **************************************************************************************************
!> \brief ...
!> \param a ...
!> \param b ...
!> \param x ...
!> \param y ...
!> \param w ...
!> \param eps ...
!> \param ierr ...
! **************************************************************************************************
SUBROUTINE bgrat(a, b, x, y, w, eps, ierr)
!-----------------------------------------------------------------------
! ASYMPTOTIC EXPANSION FOR IX(A,B) WHEN A IS LARGER THAN B.
! THE RESULT OF THE EXPANSION IS ADDED TO W. IT IS ASSUMED
! THAT A <= 15 AND B <= 1. EPS IS THE TOLERANCE USED.
! IERR IS A VARIABLE THAT REPORTS THE STATUS OF THE RESULTS.
!-----------------------------------------------------------------------
REAL(dp), INTENT(IN) :: a, b, x, y
REAL(dp), INTENT(INOUT) :: w
REAL(dp), INTENT(IN) :: eps
INTEGER, INTENT(OUT) :: ierr
REAL(dp), PARAMETER :: half = 0.5e0_dp, one = 1.e0_dp, &
quarter = 0.25e0_dp, zero = 0.e0_dp
INTEGER :: i, n
REAL(dp) :: bm1, bp2n, c(30), cn, coef, d(30), dj, &
j, l, lnx, n2, nu, q, r, s, sum, t, &
t2, tol, u, v, z
bm1 = (b - half) - half
nu = a + half*bm1
IF (y > 0.375e0_dp) THEN
lnx = LOG(x)
ELSE
lnx = alnrel(-y)
END IF
z = -nu*lnx
IF (b*z == zero) THEN
! THE EXPANSION CANNOT BE COMPUTED
ierr = 1
RETURN
END IF
! COMPUTATION OF THE EXPANSION
! SET R = EXP(-Z)*Z**B/GAMMA(B)
r = b*(one + gam1(b))*EXP(b*LOG(z))
r = r*EXP(a*lnx)*EXP(half*bm1*lnx)
u = algdiv(b, a) + b*LOG(nu)
u = r*EXP(-u)
IF (u == zero) THEN
! THE EXPANSION CANNOT BE COMPUTED
ierr = 1
RETURN
END IF
CALL grat1(b, z, r, q=q, eps=eps)
tol = 15.0e0_dp*eps
v = quarter*(one/nu)**2
t2 = quarter*lnx*lnx
l = w/u
j = q/r
sum = j
t = one
cn = one
n2 = zero
DO n = 1, 30
bp2n = b + n2
j = (bp2n*(bp2n + one)*j + (z + bp2n + one)*t)*v
n2 = n2 + 2.0e0_dp
t = t*t2
cn = cn/(n2*(n2 + one))
c(n) = cn
s = zero
IF (.NOT. (n == 1)) THEN
coef = b - n
DO i = 1, n - 1
s = s + coef*c(i)*d(n - i)
coef = coef + b
END DO
END IF
d(n) = bm1*cn + s/n
dj = d(n)*j
sum = sum + dj
IF (sum <= zero) THEN
! THE EXPANSION CANNOT BE COMPUTED
ierr = 1
RETURN
END IF
IF (ABS(dj) <= tol*(sum + l)) EXIT
END DO
! ADD THE RESULTS TO W
ierr = 0
w = w + u*sum
END SUBROUTINE bgrat
! **************************************************************************************************
!> \brief ...
!> \param a ...
!> \param x ...
!> \param r ...
!> \param p ...
!> \param q ...
!> \param eps ...
! **************************************************************************************************
SUBROUTINE grat1(a, x, r, p, q, eps)
!-----------------------------------------------------------------------
! EVALUATION OF P(A,X) AND Q(A,X) WHERE A <= 1 AND
! THE INPUT ARGUMENT R HAS THE VALUE E**(-X)*X**A/GAMMA(A)
!-----------------------------------------------------------------------
REAL(dp), INTENT(IN) :: a, x, r
REAL(dp), INTENT(OUT), OPTIONAL :: p, q
REAL(dp), INTENT(IN) :: eps
REAL(dp), PARAMETER :: half = 0.5e0_dp, one = 1.e0_dp, &
quarter = 0.25e0_dp, three = 3.e0_dp, &
two = 2.e0_dp, zero = 0.e0_dp
REAL(dp) :: a2n, a2nm1, an, b2n, b2nm1, c, g, h, j, &
l, pp, qq, sum, t, tol, w, z
IF (a*x == zero) THEN
IF (x <= a) THEN
pp = zero
qq = one
ELSE
pp = one
qq = zero
END IF
IF (PRESENT(p)) p = pp
IF (PRESENT(q)) q = qq
RETURN
END IF
IF (a == half) THEN
IF (x < quarter) THEN
pp = ERF(SQRT(x))
qq = half + (half - pp)
ELSE
qq = ERFC(SQRT(x))
pp = half + (half - qq)
END IF
IF (PRESENT(p)) p = pp
IF (PRESENT(q)) q = qq
RETURN
END IF
IF (x < 1.1e0_dp) THEN
! TAYLOR SERIES FOR P(A,X)/X**A
an = three
c = x
sum = x/(a + three)
tol = three*eps/(a + one)
an = an + one
c = -c*(x/an)
t = c/(a + an)
sum = sum + t
DO WHILE (ABS(t) > tol)
an = an + one
c = -c*(x/an)
t = c/(a + an)
sum = sum + t
END DO
j = a*x*((sum/6.0e0_dp - half/(a + two))*x + one/(a + one))
z = a*LOG(x)
h = gam1(a)
g = one + h
IF ((x < quarter .AND. z > -.13394e0_dp) .OR. a < x/2.59e0_dp) THEN
l = rexp(z)
qq = ((half + (half + l))*j - l)*g - h
IF (qq <= zero) THEN
pp = one
qq = zero
ELSE
pp = half + (half - qq)
END IF
ELSE
w = EXP(z)
pp = w*g*(half + (half - j))
qq = half + (half - pp)
END IF
ELSE
! CONTINUED FRACTION EXPANSION
tol = 8.0e0_dp*eps
a2nm1 = one
a2n = one
b2nm1 = x
b2n = x + (one - a)
c = one
DO
a2nm1 = x*a2n + c*a2nm1
b2nm1 = x*b2n + c*b2nm1
c = c + one
a2n = a2nm1 + (c - a)*a2n
b2n = b2nm1 + (c - a)*b2n
a2nm1 = a2nm1/b2n
b2nm1 = b2nm1/b2n
a2n = a2n/b2n
b2n = one
IF (ABS(a2n - a2nm1/b2nm1) < tol*a2n) EXIT
END DO
qq = r*a2n
pp = half + (half - qq)
END IF
IF (PRESENT(p)) p = pp
IF (PRESENT(q)) q = qq
END SUBROUTINE grat1
! **************************************************************************************************
!> \brief ...
!> \param mu ...
!> \param x ...
!> \return ...
! **************************************************************************************************
FUNCTION esum(mu, x) RESULT(fn_val)
!-----------------------------------------------------------------------
! EVALUATION OF EXP(MU + X)
!-----------------------------------------------------------------------
INTEGER, INTENT(IN) :: mu
REAL(dp), INTENT(IN) :: x
REAL(dp) :: fn_val
REAL(dp) :: w
IF (x > 0.0e0_dp) THEN
IF (mu > 0 .OR. mu + x < 0.0_dp) THEN
w = mu
fn_val = EXP(w)*EXP(x)
ELSE
w = mu + x
fn_val = EXP(w)
END IF
ELSE
IF (mu < 0 .OR. mu + x < 0.0_dp) THEN
w = mu
fn_val = EXP(w)*EXP(x)
ELSE
w = mu + x
fn_val = EXP(w)
END IF
END IF
END FUNCTION esum
! **************************************************************************************************
!> \brief ...
!> \param x ...
!> \return ...
! **************************************************************************************************
FUNCTION rlog1(x) RESULT(fn_val)
!-----------------------------------------------------------------------
! EVALUATION OF THE FUNCTION X - LN(1 + X)
!-----------------------------------------------------------------------
! A = RLOG (0.7)
! B = RLOG (4/3)
!------------------------
REAL(dp), INTENT(IN) :: x
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: a = .566749439387324e-01_dp, b = .456512608815524e-01_dp, &
p0 = .333333333333333e+00_dp, p1 = -.224696413112536e+00_dp, &
p2 = .620886815375787e-02_dp, q1 = -.127408923933623e+01_dp, q2 = .354508718369557e+00_dp
REAL(dp) :: r, t, u, up2, w, w1
IF (x < -0.39e0_dp .OR. x > 0.57e0_dp) THEN
w = (x + 0.5e0_dp) + 0.5e0_dp
fn_val = x - LOG(w)
RETURN
END IF
! ARGUMENT REDUCTION
IF (x < -0.18e0_dp) THEN
u = (x + 0.3e0_dp)/0.7e0_dp
up2 = u + 2.0e0_dp
w1 = a - u*0.3e0_dp
ELSEIF (x > 0.18e0_dp) THEN
t = 0.75e0_dp*x
u = t - 0.25e0_dp
up2 = t + 1.75e0_dp
w1 = b + u/3.0e0_dp
ELSE
u = x
up2 = u + 2.0e0_dp
w1 = 0.0e0_dp
END IF
! SERIES EXPANSION
r = u/up2
t = r*r
w = ((p2*t + p1)*t + p0)/((q2*t + q1)*t + 1.0e0_dp)
fn_val = r*(u - 2.0e0_dp*t*w) + w1
END FUNCTION rlog1
! **************************************************************************************************
!> \brief ...
!> \param a ...
!> \return ...
! **************************************************************************************************
FUNCTION gamln1(a) RESULT(fn_val)
!-----------------------------------------------------------------------
! EVALUATION OF LN(GAMMA(1 + A)) FOR -0.2 .LE. A .LE. 1.25
!-----------------------------------------------------------------------
REAL(dp), INTENT(IN) :: a
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: p0 = .577215664901533e+00_dp, p1 = .844203922187225e+00_dp, &
p2 = -.168860593646662e+00_dp, p3 = -.780427615533591e+00_dp, &
p4 = -.402055799310489e+00_dp, p5 = -.673562214325671e-01_dp, &
p6 = -.271935708322958e-02_dp, q1 = .288743195473681e+01_dp, &
q2 = .312755088914843e+01_dp, q3 = .156875193295039e+01_dp, q4 = .361951990101499e+00_dp, &
q5 = .325038868253937e-01_dp, q6 = .667465618796164e-03_dp, r0 = .422784335098467e+00_dp, &
r1 = .848044614534529e+00_dp, r2 = .565221050691933e+00_dp, r3 = .156513060486551e+00_dp, &
r4 = .170502484022650e-01_dp, r5 = .497958207639485e-03_dp
REAL(dp), PARAMETER :: s1 = .124313399877507e+01_dp, s2 = .548042109832463e+00_dp, &
s3 = .101552187439830e+00_dp, s4 = .713309612391000e-02_dp, s5 = .116165475989616e-03_dp
REAL(dp) :: w, x
IF (a < 0.6e0_dp) THEN
w = ((((((p6*a + p5)*a + p4)*a + p3)*a + p2)*a + p1)*a + p0)/ &
((((((q6*a + q5)*a + q4)*a + q3)*a + q2)*a + q1)*a + 1.0e0_dp)
fn_val = -a*w
ELSE
x = (a - 0.5e0_dp) - 0.5e0_dp
w = (((((r5*x + r4)*x + r3)*x + r2)*x + r1)*x + r0)/ &
(((((s5*x + s4)*x + s3)*x + s2)*x + s1)*x + 1.0e0_dp)
fn_val = x*w
END IF
END FUNCTION gamln1
! **************************************************************************************************
!> \brief ...
!> \param xx ...
!> \return ...
! **************************************************************************************************
FUNCTION psi(xx) RESULT(fn_val)
!---------------------------------------------------------------------
! EVALUATION OF THE DIGAMMA FUNCTION
! -----------
! PSI(XX) IS ASSIGNED THE VALUE 0 WHEN THE DIGAMMA FUNCTION CANNOT
! BE COMPUTED.
! THE MAIN COMPUTATION INVOLVES EVALUATION OF RATIONAL CHEBYSHEV
! APPROXIMATIONS PUBLISHED IN MATH. COMP. 27, 123-127(1973) BY
! CODY, STRECOK AND THACHER.
!---------------------------------------------------------------------
! PSI WAS WRITTEN AT ARGONNE NATIONAL LABORATORY FOR THE FUNPACK
! PACKAGE OF SPECIAL FUNCTION SUBROUTINES. PSI WAS MODIFIED BY
! A.H. MORRIS (NSWC).
!---------------------------------------------------------------------
REAL(dp), INTENT(IN) :: xx
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: dx0 = 1.461632144968362341262659542325721325e0_dp, p1(7) = (/ &
.895385022981970e-02_dp, .477762828042627e+01_dp, .142441585084029e+03_dp, &
.118645200713425e+04_dp, .363351846806499e+04_dp, .413810161269013e+04_dp, &
.130560269827897e+04_dp/), p2(4) = (/-.212940445131011e+01_dp, -.701677227766759e+01_dp, &
-.448616543918019e+01_dp, -.648157123766197e+00_dp/), piov4 = .785398163397448e0_dp, q1(6)&
= (/.448452573429826e+02_dp, .520752771467162e+03_dp, .221000799247830e+04_dp, &
.364127349079381e+04_dp, .190831076596300e+04_dp, .691091682714533e-05_dp/)
REAL(dp), PARAMETER :: q2(4) = (/.322703493791143e+02_dp, .892920700481861e+02_dp, &
.546117738103215e+02_dp, .777788548522962e+01_dp/)
INTEGER :: i, m, n, nq
REAL(dp) :: aug, den, sgn, upper, w, x, xmax1, xmx0, &
xsmall, z
!---------------------------------------------------------------------
! PIOV4 = PI/4
! DX0 = ZERO OF PSI TO EXTENDED PRECISION
!---------------------------------------------------------------------
!---------------------------------------------------------------------
! COEFFICIENTS FOR RATIONAL APPROXIMATION OF
! PSI(X) / (X - X0), 0.5 <= X <= 3.0
!---------------------------------------------------------------------
!---------------------------------------------------------------------
! COEFFICIENTS FOR RATIONAL APPROXIMATION OF
! PSI(X) - LN(X) + 1 / (2*X), X > 3.0
!---------------------------------------------------------------------
!---------------------------------------------------------------------
! MACHINE DEPENDENT CONSTANTS ...
! XMAX1 = THE SMALLEST POSITIVE FLOATING POINT CONSTANT
! WITH ENTIRELY INTEGER REPRESENTATION. ALSO USED
! AS NEGATIVE OF LOWER BOUND ON ACCEPTABLE NEGATIVE
! ARGUMENTS AND AS THE POSITIVE ARGUMENT BEYOND WHICH
! PSI MAY BE REPRESENTED AS ALOG(X).
! XSMALL = ABSOLUTE ARGUMENT BELOW WHICH PI*COTAN(PI*X)
! MAY BE REPRESENTED BY 1/X.
!---------------------------------------------------------------------
xmax1 = ipmpar(3)
xmax1 = MIN(xmax1, 1.0e0_dp/dpmpar(1))
xsmall = 1.e-9_dp
!---------------------------------------------------------------------
x = xx
aug = 0.0e0_dp
IF (x < 0.5e0_dp) THEN
!---------------------------------------------------------------------
! X .LT. 0.5, USE REFLECTION FORMULA
! PSI(1-X) = PSI(X) + PI * COTAN(PI*X)
!---------------------------------------------------------------------
IF (ABS(x) <= xsmall) THEN
IF (x == 0.0e0_dp) THEN
! ERROR RETURN
fn_val = 0.0e0_dp
RETURN
END IF
!---------------------------------------------------------------------
! 0 .LT. ABS(X) .LE. XSMALL. USE 1/X AS A SUBSTITUTE
! FOR PI*COTAN(PI*X)
!---------------------------------------------------------------------
aug = -1.0e0_dp/x
x = 1.0e0_dp - x
ELSE
!---------------------------------------------------------------------
! REDUCTION OF ARGUMENT FOR COTAN
!---------------------------------------------------------------------
w = -x
sgn = piov4
IF (w <= 0.0e0_dp) THEN
w = -w
sgn = -sgn
END IF
!---------------------------------------------------------------------
! MAKE AN ERROR EXIT IF X .LE. -XMAX1
!---------------------------------------------------------------------
IF (w >= xmax1) THEN
! ERROR RETURN
fn_val = 0.0e0_dp
RETURN
END IF
nq = INT(w)
w = w - nq
nq = INT(w*4.0e0_dp)
w = 4.0e0_dp*(w - nq*.25e0_dp)
!---------------------------------------------------------------------
! W IS NOW RELATED TO THE FRACTIONAL PART OF 4.0 * X.
! ADJUST ARGUMENT TO CORRESPOND TO VALUES IN FIRST
! QUADRANT AND DETERMINE SIGN
!---------------------------------------------------------------------
n = nq/2
IF ((n + n) /= nq) w = 1.0e0_dp - w
z = piov4*w
m = n/2
IF ((m + m) /= n) sgn = -sgn
!---------------------------------------------------------------------
! DETERMINE FINAL VALUE FOR -PI*COTAN(PI*X)
!---------------------------------------------------------------------
n = (nq + 1)/2
m = n/2
m = m + m
IF (m /= n) THEN
aug = sgn*((SIN(z)/COS(z))*4.0e0_dp)
ELSE
!---------------------------------------------------------------------
! CHECK FOR SINGULARITY
!---------------------------------------------------------------------
IF (z == 0.0e0_dp) THEN
! ERROR RETURN
fn_val = 0.0e0_dp
RETURN
END IF
!---------------------------------------------------------------------
! USE COS/SIN AS A SUBSTITUTE FOR COTAN, AND
! SIN/COS AS A SUBSTITUTE FOR TAN
!---------------------------------------------------------------------
aug = sgn*((COS(z)/SIN(z))*4.0e0_dp)
END IF
x = 1.0e0_dp - x
END IF
END IF
IF (x <= 3.0e0_dp) THEN
!---------------------------------------------------------------------
! 0.5 .LE. X .LE. 3.0
!---------------------------------------------------------------------
den = x
upper = p1(1)*x
DO i = 1, 5
den = (den + q1(i))*x
upper = (upper + p1(i + 1))*x
END DO
den = (upper + p1(7))/(den + q1(6))
xmx0 = x - dx0
fn_val = den*xmx0 + aug
RETURN
END IF
!---------------------------------------------------------------------
! IF X .GE. XMAX1, PSI = LN(X)
!---------------------------------------------------------------------
IF (x < xmax1) THEN
!---------------------------------------------------------------------
! 3.0 .LT. X .LT. XMAX1
!---------------------------------------------------------------------
w = 1.0e0_dp/(x*x)
den = w
upper = p2(1)*w
DO i = 1, 3
den = (den + q2(i))*w
upper = (upper + p2(i + 1))*w
END DO
aug = upper/(den + q2(4)) - 0.5e0_dp/x + aug
END IF
fn_val = aug + LOG(x)
END FUNCTION psi
! **************************************************************************************************
!> \brief ...
!> \param a0 ...
!> \param b0 ...
!> \return ...
! **************************************************************************************************
FUNCTION betaln(a0, b0) RESULT(fn_val)
!-----------------------------------------------------------------------
! EVALUATION OF THE LOGARITHM OF THE BETA FUNCTION
!-----------------------------------------------------------------------
! E = 0.5*LN(2*PI)
!--------------------------
REAL(dp), INTENT(IN) :: a0, b0
REAL(dp) :: fn_val
REAL(dp), PARAMETER :: e = .918938533204673e0_dp
INTEGER :: i, n
REAL(dp) :: a, b, c, h, u, v, w, z
!--------------------------
a = MIN(a0, b0)
b = MAX(a0, b0)
!-----------------------------------------------------------------------
! PROCEDURE WHEN A .GE. 8
!-----------------------------------------------------------------------
IF (a >= 8.0e0_dp) THEN
w = bcorr(a, b)
h = a/b
c = h/(1.0e0_dp + h)
u = -(a - 0.5e0_dp)*LOG(c)
v = b*alnrel(h)
IF (u > v) THEN
fn_val = (((-0.5e0_dp*LOG(b) + e) + w) - v) - u
ELSE
fn_val = (((-0.5e0_dp*LOG(b) + e) + w) - u) - v
END IF
!-----------------------------------------------------------------------
! PROCEDURE WHEN A .LT. 1
!-----------------------------------------------------------------------
ELSEIF (a < 1.0e0_dp) THEN
IF (b < 8.0e0_dp) THEN
fn_val = LOG_GAMMA(a) + (LOG_GAMMA(b) - LOG_GAMMA(a + b))
ELSE
fn_val = LOG_GAMMA(a) + algdiv(a, b)
END IF
!-----------------------------------------------------------------------
! PROCEDURE WHEN 1 .LE. A .LT. 8
!-----------------------------------------------------------------------
ELSEIF (a <= 2.0e0_dp) THEN
IF (b <= 2.0e0_dp) THEN
fn_val = LOG_GAMMA(a) + LOG_GAMMA(b) - gsumln(a, b)
RETURN
END IF
w = 0.0e0_dp
IF (b < 8.0e0_dp) THEN
! REDUCTION OF B WHEN B .LT. 8
n = INT(b - 1.0e0_dp)
z = 1.0e0_dp
DO i = 1, n
b = b - 1.0e0_dp
z = z*(b/(a + b))
END DO
fn_val = w + LOG(z) + (LOG_GAMMA(a) + (LOG_GAMMA(b) - gsumln(a, b)))
RETURN
END IF
fn_val = LOG_GAMMA(a) + algdiv(a, b)
! REDUCTION OF A WHEN B .LE. 1000
ELSEIF (b <= 1000.0e0_dp) THEN
n = INT(a - 1.0e0_dp)
w = 1.0e0_dp
DO i = 1, n
a = a - 1.0e0_dp
h = a/b
w = w*(h/(1.0e0_dp + h))
END DO
w = LOG(w)
IF (b >= 8.0e0_dp) THEN
fn_val = w + LOG_GAMMA(a) + algdiv(a, b)
RETURN
END IF
! REDUCTION OF B WHEN B .LT. 8
n = INT(b - 1.0e0_dp)
z = 1.0e0_dp
DO i = 1, n
b = b - 1.0e0_dp
z = z*(b/(a + b))
END DO
fn_val = w + LOG(z) + (LOG_GAMMA(a) + (LOG_GAMMA(b) - gsumln(a, b)))
ELSE
! REDUCTION OF A WHEN B .GT. 1000
n = INT(a - 1.0e0_dp)
w = 1.0e0_dp
DO i = 1, n
a = a - 1.0e0_dp
w = w*(a/(1.0e0_dp + a/b))
END DO
fn_val = (LOG(w) - n*LOG(b)) + (LOG_GAMMA(a) + algdiv(a, b))
END IF