1
1
#:include " common.fypp"
2
- #:set R_KINDS_TYPES = [KT for KT in REAL_KINDS_TYPES if KT[ 0 ] in [ " sp " , " dp " ]]
3
- #:set C_KINDS_TYPES = [KT for KT in CMPLX_KINDS_TYPES if KT[ 0 ] in [ " sp " , " dp " ] ]
4
- #:set CI_KINDS_TYPES = INT_KINDS_TYPES + C_KINDS_TYPES
2
+ #:set CI_KINDS_TYPES = INT_KINDS_TYPES + CMPLX_KINDS_TYPES
3
+ #:set IDX_CMPLX_KINDS_TYPES = [(i, CMPLX_KINDS[i], CMPLX_TYPES[i], CMPLX_INIT[i]) for i in range ( len (CMPLX_KINDS)) ]
4
+ #:set IDX_REAL_KINDS_TYPES = [(i, REAL_KINDS[i], REAL_TYPES[i], REAL_INIT[i]) for i in range ( len (REAL_KINDS))]
5
5
module stdlib_specialfunctions_gamma
6
- use iso_fortran_env, only : qp = > real128
7
6
use ieee_arithmetic, only: ieee_value, ieee_quiet_nan
8
- use stdlib_kinds, only : sp, dp, int8, int16, int32, int64
7
+ use stdlib_kinds, only : sp, dp, xdp, qp, int8, int16, int32, int64
9
8
use stdlib_error, only : error_stop
10
9
11
10
implicit none
12
11
private
13
12
14
- integer (int8), parameter :: max_fact_int8 = 6_int8
13
+ integer (int8), parameter :: max_fact_int8 = 6_int8
15
14
integer (int16), parameter :: max_fact_int16 = 8_int16
16
15
integer (int32), parameter :: max_fact_int32 = 13_int32
17
16
integer (int64), parameter :: max_fact_int64 = 21_int64
18
17
19
- #:for k1, t1 in R_KINDS_TYPES
18
+ #:for k1, t1 in REAL_KINDS_TYPES
20
19
${t1}$, parameter :: tol_${k1}$ = epsilon (1.0_ ${k1}$)
21
20
#:endfor
22
- real (qp), parameter :: tol_qp = epsilon (1.0_qp )
23
-
24
-
25
-
21
+
26
22
public :: gamma, log_gamma, log_factorial
27
23
public :: lower_incomplete_gamma, log_lower_incomplete_gamma
28
24
public :: upper_incomplete_gamma, log_upper_incomplete_gamma
@@ -33,7 +29,7 @@ module stdlib_specialfunctions_gamma
33
29
interface gamma
34
30
!! Gamma function for integer and complex numbers
35
31
!!
36
- #:for k1, t1 in CI_KINDS_TYPES
32
+ #:for k1, t1 in CI_KINDS_TYPES[: - 1 ]
37
33
module procedure gamma_${t1[0 ]}$${k1}$
38
34
#:endfor
39
35
end interface gamma
@@ -43,7 +39,7 @@ module stdlib_specialfunctions_gamma
43
39
interface log_gamma
44
40
!! Logarithm of gamma function
45
41
!!
46
- #:for k1, t1 in CI_KINDS_TYPES
42
+ #:for k1, t1 in CI_KINDS_TYPES[: - 1 ]
47
43
module procedure l_gamma_${t1[0 ]}$${k1}$
48
44
#:endfor
49
45
end interface log_gamma
@@ -64,12 +60,12 @@ module stdlib_specialfunctions_gamma
64
60
!! Lower incomplete gamma function
65
61
!!
66
62
#:for k1, t1 in INT_KINDS_TYPES
67
- #:for k2, t2 in R_KINDS_TYPES
63
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
68
64
module procedure ingamma_low_${t1[0 ]}$${k1}$${k2}$
69
65
#:endfor
70
66
#:endfor
71
67
72
- #:for k1, t1 in R_KINDS_TYPES
68
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
73
69
module procedure ingamma_low_${t1[0 ]}$${k1}$
74
70
#:endfor
75
71
end interface lower_incomplete_gamma
@@ -80,12 +76,12 @@ module stdlib_specialfunctions_gamma
80
76
!! Logarithm of lower incomplete gamma function
81
77
!!
82
78
#:for k1, t1 in INT_KINDS_TYPES
83
- #:for k2, t2 in R_KINDS_TYPES
79
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
84
80
module procedure l_ingamma_low_${t1[0 ]}$${k1}$${k2}$
85
81
#:endfor
86
82
#:endfor
87
83
88
- #:for k1, t1 in R_KINDS_TYPES
84
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
89
85
module procedure l_ingamma_low_${t1[0 ]}$${k1}$
90
86
#:endfor
91
87
end interface log_lower_incomplete_gamma
@@ -96,12 +92,12 @@ module stdlib_specialfunctions_gamma
96
92
!! Upper incomplete gamma function
97
93
!!
98
94
#:for k1, t1 in INT_KINDS_TYPES
99
- #:for k2, t2 in R_KINDS_TYPES
95
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
100
96
module procedure ingamma_up_${t1[0 ]}$${k1}$${k2}$
101
97
#:endfor
102
98
#:endfor
103
99
104
- #:for k1, t1 in R_KINDS_TYPES
100
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
105
101
module procedure ingamma_up_${t1[0 ]}$${k1}$
106
102
#:endfor
107
103
end interface upper_incomplete_gamma
@@ -112,12 +108,12 @@ module stdlib_specialfunctions_gamma
112
108
!! Logarithm of upper incomplete gamma function
113
109
!!
114
110
#:for k1, t1 in INT_KINDS_TYPES
115
- #:for k2, t2 in R_KINDS_TYPES
111
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
116
112
module procedure l_ingamma_up_${t1[0 ]}$${k1}$${k2}$
117
113
#:endfor
118
114
#:endfor
119
115
120
- #:for k1, t1 in R_KINDS_TYPES
116
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
121
117
module procedure l_ingamma_up_${t1[0 ]}$${k1}$
122
118
#:endfor
123
119
end interface log_upper_incomplete_gamma
@@ -128,12 +124,12 @@ module stdlib_specialfunctions_gamma
128
124
!! Regularized (normalized) lower incomplete gamma function, P
129
125
!!
130
126
#:for k1, t1 in INT_KINDS_TYPES
131
- #:for k2, t2 in R_KINDS_TYPES
127
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
132
128
module procedure regamma_p_${t1[0 ]}$${k1}$${k2}$
133
129
#:endfor
134
130
#:endfor
135
131
136
- #:for k1, t1 in R_KINDS_TYPES
132
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
137
133
module procedure regamma_p_${t1[0 ]}$${k1}$
138
134
#:endfor
139
135
end interface regularized_gamma_p
@@ -144,12 +140,12 @@ module stdlib_specialfunctions_gamma
144
140
!! Regularized (normalized) upper incomplete gamma function, Q
145
141
!!
146
142
#:for k1, t1 in INT_KINDS_TYPES
147
- #:for k2, t2 in R_KINDS_TYPES
143
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
148
144
module procedure regamma_q_${t1[0 ]}$${k1}$${k2}$
149
145
#:endfor
150
146
#:endfor
151
147
152
- #:for k1, t1 in R_KINDS_TYPES
148
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
153
149
module procedure regamma_q_${t1[0 ]}$${k1}$
154
150
#:endfor
155
151
end interface regularized_gamma_q
@@ -160,12 +156,12 @@ module stdlib_specialfunctions_gamma
160
156
! Incomplete gamma G function.
161
157
! Internal use only
162
158
!
163
- #:for k1, t1 in R_KINDS_TYPES
159
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
164
160
module procedure gpx_${t1[0 ]}$${k1}$ !for real p and x
165
161
#:endfor
166
162
167
163
#:for k1, t1 in INT_KINDS_TYPES
168
- #:for k2, t2 in R_KINDS_TYPES
164
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
169
165
module procedure gpx_${t1[0 ]}$${k1}$${k2}$ !for integer p and real x
170
166
#:endfor
171
167
#:endfor
@@ -178,7 +174,7 @@ module stdlib_specialfunctions_gamma
178
174
! Internal use only
179
175
!
180
176
#:for k1, t1 in INT_KINDS_TYPES
181
- #:for k2, t2 in R_KINDS_TYPES
177
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
182
178
module procedure l_gamma_${t1[0 ]}$${k1}$${k2}$
183
179
#:endfor
184
180
#:endfor
@@ -219,14 +215,12 @@ contains
219
215
220
216
221
217
222
- #:for k1, t1 in C_KINDS_TYPES
223
- #:if k1 == " sp"
224
- #:set k2 = " dp"
225
- #:elif k1 == " dp"
226
- #:set k2 = " qp"
227
- #:endif
228
- #:set t2 = " real({})" .format (k2)
229
-
218
+ #! Because the KIND lists are sorted by increasing accuracy,
219
+ #! gamma will use the next available more accurate KIND for the
220
+ #! internal more accurate solver.
221
+ #:for i, k1, t1, i1 in IDX_CMPLX_KINDS_TYPES[:- 1 ]
222
+ #:set k2 = CMPLX_KINDS[i + 1 ] if k1 == " sp" else CMPLX_KINDS[- 1 ]
223
+ #:set t2 = " real({})" .format (k2)
230
224
impure elemental function gamma_${t1[0 ]}$${k1}$(z) result(res)
231
225
${t1}$, intent (in ) :: z
232
226
${t1}$ :: res
@@ -255,8 +249,8 @@ contains
255
249
- 2.71994908488607704e-9_ ${k2}$]
256
250
! parameters from above referenced source.
257
251
258
- #:elif k1 == " dp "
259
- #! for double precision input, using quadruple precision for calculation
252
+ #:else
253
+ #! for double or extended precision input, using quadruple precision for calculation
260
254
261
255
integer , parameter :: n = 24
262
256
${t2}$, parameter :: r = 25.617904_ ${k2}$
@@ -290,8 +284,6 @@ contains
290
284
291
285
#:endif
292
286
293
-
294
-
295
287
if (abs (z % im) < tol_${k1}$) then
296
288
297
289
res = cmplx (gamma(z % re), kind = ${k1}$)
@@ -333,16 +325,13 @@ contains
333
325
334
326
#:endfor
335
327
336
-
337
-
338
-
339
328
#:for k1, t1 in INT_KINDS_TYPES
340
329
impure elemental function l_gamma_${t1[0 ]}$${k1}$(z) result(res)
341
330
!
342
331
! Logarithm of gamma function for integer input
343
332
!
344
333
${t1}$, intent (in ) :: z
345
- real :: res
334
+ real (dp) :: res
346
335
${t1}$ :: i
347
336
${t1}$, parameter :: zero = 0_ ${k1}$, one = 1_ ${k1}$, two = 2_ ${k1}$
348
337
@@ -361,7 +350,7 @@ contains
361
350
362
351
do i = one, z - one
363
352
364
- res = res + log (real (i))
353
+ res = res + log (real (i,dp ))
365
354
366
355
end do
367
356
@@ -374,7 +363,7 @@ contains
374
363
375
364
376
365
#:for k1, t1 in INT_KINDS_TYPES
377
- #:for k2, t2 in R_KINDS_TYPES
366
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
378
367
379
368
impure elemental function l_gamma_${t1[0 ]}$${k1}$${k2}$(z, x) result(res)
380
369
!
@@ -415,13 +404,12 @@ contains
415
404
416
405
417
406
418
- #:for k1, t1 in C_KINDS_TYPES
419
- #:if k1 == " sp"
420
- #:set k2 = " dp"
421
- #:elif k1 == " dp"
422
- #:set k2 = " qp"
423
- #:endif
424
- #:set t2 = " real({})" .format (k2)
407
+ #! Because the KIND lists are sorted by increasing accuracy,
408
+ #! gamma will use the next available more accurate KIND for the
409
+ #! internal more accurate solver.
410
+ #:for i, k1, t1, i1 in IDX_CMPLX_KINDS_TYPES[:- 1 ]
411
+ #:set k2 = CMPLX_KINDS[i + 1 ] if k1 == " sp" else CMPLX_KINDS[- 1 ]
412
+ #:set t2 = " real({})" .format (k2)
425
413
impure elemental function l_gamma_${t1[0 ]}$${k1}$(z) result (res)
426
414
!
427
415
! log_gamma function for any complex number, excluding negative whole number
@@ -436,7 +424,7 @@ contains
436
424
real (${k1}$) :: d
437
425
integer :: m, i
438
426
complex (${k2}$) :: zr, zr2, sum, s
439
- real (${k1}$), parameter :: z_limit = 10_ ${k1}$, zero_k1 = 0.0_ ${k1}$
427
+ real (${k1}$), parameter :: z_limit = 10.0_ ${k1}$, zero_k1 = 0.0_ ${k1}$
440
428
integer , parameter :: n = 20
441
429
${t2}$, parameter :: zero = 0.0_ ${k2}$, one = 1.0_ ${k2}$, &
442
430
pi = acos (- one), ln2pi = log (2 * pi)
@@ -524,14 +512,15 @@ contains
524
512
525
513
526
514
#:for k1, t1 in INT_KINDS_TYPES
515
+ #:set k2, t2 = REAL_KINDS[- 2 ], REAL_TYPES[- 2 ]
527
516
impure elemental function l_factorial_${t1[0 ]}$${k1}$(n) result(res)
528
517
!
529
518
! Log (n!)
530
519
!
531
520
${t1}$, intent (in ) :: n
532
- real (dp) :: res
521
+ ${t2}$ :: res
533
522
${t1}$, parameter :: zero = 0_ ${k1}$, one = 1_ ${k1}$, two = 2_ ${k1}$
534
- real (dp) , parameter :: zero_dp = 0.0_dp
523
+ ${t2}$ , parameter :: zero_${k2}$ = 0.0_ ${k2}$, one_${k2}$ = 1.0_ ${k2}$
535
524
536
525
if (n < zero) call error_stop(" Error(l_factorial): Logarithm of" &
537
526
// " factorial function argument must be non-negative" )
@@ -540,15 +529,15 @@ contains
540
529
541
530
case (zero)
542
531
543
- res = zero_dp
532
+ res = zero_${k2}$
544
533
545
534
case (one)
546
535
547
- res = zero_dp
536
+ res = zero_${k2}$
548
537
549
538
case (two : )
550
539
551
- res = l_gamma(n + 1 , 1.0_dp )
540
+ res = l_gamma(n + 1 , one_${k2}$ )
552
541
553
542
end select
554
543
end function l_factorial_${t1[0 ]}$${k1}$
@@ -557,14 +546,12 @@ contains
557
546
558
547
559
548
560
- #:for k1, t1 in R_KINDS_TYPES
561
- #:if k1 == " sp"
562
- #:set k2 = " dp"
563
- #:elif k1 == " dp"
564
- #:set k2 = " qp"
565
- #:endif
566
- #:set t2 = " real({})" .format (k2)
567
-
549
+ #! Because the KIND lists are sorted by increasing accuracy,
550
+ #! gamma will use the next available more accurate KIND for the
551
+ #! internal more accurate solver.
552
+ #:for i, k1, t1, i1 in IDX_REAL_KINDS_TYPES[:- 1 ]
553
+ #:set k2 = REAL_KINDS[i + 1 ] if k1 == " sp" else REAL_KINDS[- 1 ]
554
+ #:set t2 = REAL_TYPES[i + 1 ]
568
555
impure elemental function gpx_${t1[0 ]}$${k1}$(p, x) result(res)
569
556
!
570
557
! Approximation of incomplete gamma G function with real argument p.
@@ -685,7 +672,7 @@ contains
685
672
686
673
687
674
#:for k1, t1 in INT_KINDS_TYPES
688
- #:for k2, t2 in R_KINDS_TYPES
675
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
689
676
impure elemental function gpx_${t1[0 ]}$${k1}$${k2}$(p, x) result(res)
690
677
!
691
678
! Approximation of incomplete gamma G function with integer argument p.
@@ -732,7 +719,7 @@ contains
732
719
733
720
if (mod (n, 2 ) == 0 ) then
734
721
735
- a = (1 - p - n / 2 ) * x
722
+ a = (one - p - n / 2 ) * x
736
723
737
724
else
738
725
@@ -824,7 +811,7 @@ contains
824
811
825
812
826
813
827
- #:for k1, t1 in R_KINDS_TYPES
814
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
828
815
impure elemental function ingamma_low_${t1[0 ]}$${k1}$(p, x) result(res)
829
816
!
830
817
! Approximation of lower incomplete gamma function with real p.
@@ -861,7 +848,7 @@ contains
861
848
862
849
863
850
#:for k1, t1 in INT_KINDS_TYPES
864
- #:for k2, t2 in R_KINDS_TYPES
851
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
865
852
impure elemental function ingamma_low_${t1[0 ]}$${k1}$${k2}$(p, x) &
866
853
result(res)
867
854
!
@@ -901,7 +888,7 @@ contains
901
888
902
889
903
890
904
- #:for k1, t1 in R_KINDS_TYPES
891
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
905
892
impure elemental function l_ingamma_low_${t1[0 ]}$${k1}$(p, x) result(res)
906
893
907
894
${t1}$, intent (in ) :: p, x
@@ -938,7 +925,7 @@ contains
938
925
939
926
940
927
#:for k1, t1 in INT_KINDS_TYPES
941
- #:for k2, t2 in R_KINDS_TYPES
928
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
942
929
impure elemental function l_ingamma_low_${t1[0 ]}$${k1}$${k2}$(p, x) &
943
930
result(res)
944
931
@@ -970,7 +957,7 @@ contains
970
957
971
958
972
959
973
- #:for k1, t1 in R_KINDS_TYPES
960
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
974
961
impure elemental function ingamma_up_${t1[0 ]}$${k1}$(p, x) result(res)
975
962
!
976
963
! Approximation of upper incomplete gamma function with real p.
@@ -1008,7 +995,7 @@ contains
1008
995
1009
996
1010
997
#:for k1, t1 in INT_KINDS_TYPES
1011
- #:for k2, t2 in R_KINDS_TYPES
998
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
1012
999
impure elemental function ingamma_up_${t1[0 ]}$${k1}$${k2}$(p, x) &
1013
1000
result(res)
1014
1001
!
@@ -1050,7 +1037,7 @@ contains
1050
1037
1051
1038
1052
1039
1053
- #:for k1, t1 in R_KINDS_TYPES
1040
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
1054
1041
impure elemental function l_ingamma_up_${t1[0 ]}$${k1}$(p, x) result(res)
1055
1042
1056
1043
${t1}$, intent (in ) :: p, x
@@ -1088,7 +1075,7 @@ contains
1088
1075
1089
1076
1090
1077
#:for k1, t1 in INT_KINDS_TYPES
1091
- #:for k2, t2 in R_KINDS_TYPES
1078
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
1092
1079
impure elemental function l_ingamma_up_${t1[0 ]}$${k1}$${k2}$(p, x) &
1093
1080
result(res)
1094
1081
@@ -1129,7 +1116,7 @@ contains
1129
1116
1130
1117
1131
1118
1132
- #:for k1, t1 in R_KINDS_TYPES
1119
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
1133
1120
impure elemental function regamma_p_${t1[0 ]}$${k1}$(p, x) result(res)
1134
1121
!
1135
1122
! Approximation of regularized incomplete gamma function P(p,x) for real p
@@ -1164,7 +1151,7 @@ contains
1164
1151
1165
1152
1166
1153
#:for k1, t1 in INT_KINDS_TYPES
1167
- #:for k2, t2 in R_KINDS_TYPES
1154
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
1168
1155
impure elemental function regamma_p_${t1[0 ]}$${k1}$${k2}$(p, x) result(res)
1169
1156
!
1170
1157
! Approximation of regularized incomplete gamma function P(p,x) for integer p
@@ -1200,7 +1187,7 @@ contains
1200
1187
1201
1188
1202
1189
1203
- #:for k1, t1 in R_KINDS_TYPES
1190
+ #:for k1, t1 in REAL_KINDS_TYPES[: - 1 ]
1204
1191
impure elemental function regamma_q_${t1[0 ]}$${k1}$(p, x) result(res)
1205
1192
!
1206
1193
! Approximation of regularized incomplete gamma function Q(p,x) for real p
@@ -1235,7 +1222,7 @@ contains
1235
1222
1236
1223
1237
1224
#:for k1, t1 in INT_KINDS_TYPES
1238
- #:for k2, t2 in R_KINDS_TYPES
1225
+ #:for k2, t2 in REAL_KINDS_TYPES[: - 1 ]
1239
1226
impure elemental function regamma_q_${t1[0 ]}$${k1}$${k2}$(p, x) result(res)
1240
1227
!
1241
1228
! Approximation of regularized incomplet gamma function Q(p,x) for integer p
0 commit comments