eff_math_fun.c

filter: Statementstmt 58.5% 75 / 128 Branchbran 46.9% 31 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 54.6% 106 / 194 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary EfficiencyCI.xs »
1 #include "eff_math_fun.h"
2 #include <stdio.h>
3 #include <math.h>
4  
5 #include "eff_ci.h"
6  
7 #ifndef DEBUG
8 # define DEBUG 0
9 #endif
10  
11 #define MY_kDBL_MAX (1.79769e+308)
12 #define MY_kMAXLGM (2.556348e305)
13 #define MY_kLS2PI (0.91893853320467274178)
14  
15 /* Logarithm of gamma function */
16 /* A[]: Stirling's formula expansion of log gamma
17 * B[], C[]: log gamma function between 2 and 3
18 */
19  
20 static double A[5] = {
21 8.11614167470508450300E-4,
22 -5.95061904284301438324E-4,
23 7.93650340457716943945E-4,
24 -2.77777777730099687205E-3,
25 8.33333333333331927722E-2
26 };
27  
28 static double B[6] = {
29 -1.37825152569120859100E3,
30 -3.88016315134637840924E4,
31 -3.31612992738871184744E5,
32 -1.16237097492762307383E6,
33 -1.72173700820839662146E6,
34 -8.53555664245765465627E5
35 };
36  
37 static double C[6] = {
38 /* 1.00000000000000000000E0, */
39 -3.51815701436523470549E2,
40 -1.70642106651881159223E4,
41 -2.20528590553854454839E5,
42 -1.13933444367982507207E6,
43 -2.53252307177582951285E6,
44 -2.01889141433532773231E6
45 };
46  
47 double polynomial_1eval(double x, double* a, unsigned int N);
48 double polynomial_eval(double x, double* a, unsigned int N);
49  
50 /*
51 * calculates a value of a polynomial of the form:
52 * a[0]x^N+a[1]x^(N-1) + ... + a[N]
53 */
54 double
55 3038 polynomial_eval(double x, double* a, unsigned int N)
56 {
57 double pom;
58 unsigned int i;
59 3038 ▶ if (N==0)
Branch: 1 branch
BranchTrueFalseTotal
if (N==0) 0 3038 3038
60 0 return a[0];
61 else {
62 3038 pom = a[0];
63 15190 ▶ for (i=1; i <= N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i=1; i <= N; i++) 12152 3038 15190
64 12152 pom = pom *x + a[i];
65 3038 return pom;
66 }
67 }
68  
69 /*
70 * calculates a value of a polynomial of the form:
71 * x^N+a[0]x^(N-1) + ... + a[N-1]
72 */
73 double
74 0 polynomial_1eval(double x, double* a, unsigned int N)
75 {
76 double pom;
77 unsigned int i;
78 0 ▶ if (N==0)
Branch: 1 branch
BranchTrueFalseTotal
if (N==0) 0 0 0
79 0 return a[0];
80 else {
81 0 pom = x + a[0];
82 0 ▶ for (i=1; i < N; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i=1; i < N; i++) 0 0 0
83 0 pom = pom *x + a[i];
84 0 return pom;
85 }
86 }
87  
88  
89  
90 double
91 4560 log_gamma(double x)
92 {
93 double p, q, u, w, z;
94 int i;
95  
96 4560 int sgngam = 1;
97  
98 4560 ▶ if (x >= MY_kDBL_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (x >= MY_kDBL_MAX) 0 4560 4560
99 0 return(MY_kDBL_MAX);
100  
101 4560 ▶ if( x < -34.0 )
Branch: 1 branch
BranchTrueFalseTotal
if( x < -34.0 ) 0 4560 4560
102 {
103 0 q = -x;
104 0 w = log_gamma(q);
105 0 p = floor(q);
106 0 ▶ if( p==q )//_unur_FP_same(p,q)
Branch: 1 branch
BranchTrueFalseTotal
if( p==q )//_unur_FP_same(p,q) 0 0 0
107 0 return(MY_kDBL_MAX);
108 0 i = (int) p;
109 0 ▶ if( (i & 1) == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if( (i & 1) == 0 ) 0 0 0
110 0 sgngam = -1;
111 else
112 0 sgngam = 1;
113 0 z = q - p;
114 0 ▶ if( z > 0.5 )
Branch: 1 branch
BranchTrueFalseTotal
if( z > 0.5 ) 0 0 0
115 {
116 0 p += 1.0;
117 0 z = p - q;
118 }
119 0 z = q * sin( M_PI * z );
120 0 ▶ if( z == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if( z == 0 ) 0 0 0
121 0 return(MY_kDBL_MAX);
122 0 z = log(M_PI) - log( z ) - w;
123 0 return( z );
124 }
125  
126 4560 ▶ if( x < 13.0 )
Branch: 1 branch
BranchTrueFalseTotal
if( x < 13.0 ) 1522 3038 4560
127 {
128 1522 z = 1.0;
129 1522 p = 0.0;
130 1522 u = x;
131 6082 ▶ while( u >= 3.0 )
Branch: 1 branch
BranchTrueFalseTotal
while( u >= 3.0 ) 4560 1522 6082
132 {
133 4560 p -= 1.0;
134 4560 u = x + p;
135 4560 z *= u;
136 }
137 1522 ▶ while( u < 2.0 )
Branch: 1 branch
BranchTrueFalseTotal
while( u < 2.0 ) 0 1522 1522
138 {
139 0 ▶ if( u == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if( u == 0 ) 0 0 0
140 0 return (MY_kDBL_MAX);
141 0 z /= u;
142 0 p += 1.0;
143 0 u = x + p;
144 }
145 1522 ▶ if( z < 0.0 )
Branch: 1 branch
BranchTrueFalseTotal
if( z < 0.0 ) 0 1522 1522
146 {
147 0 sgngam = -1;
148 0 z = -z;
149 }
150 else
151 1522 sgngam = 1;
152 1522 ▶ if( u == 2.0 )
Branch: 1 branch
BranchTrueFalseTotal
if( u == 2.0 ) 1522 0 1522
153 1522 return( log(z) );
154 0 p -= 2.0;
155 0 x = x + p;
156 0 p = x * polynomial_eval(x, B, 5 ) / polynomial_1eval( x, C, 6);
157 0 return( log(z) + p );
158 }
159  
160 3038 ▶ if( x > MY_kMAXLGM )
Branch: 1 branch
BranchTrueFalseTotal
if( x > MY_kMAXLGM ) 0 3038 3038
161 0 return( sgngam * MY_kDBL_MAX );
162  
163 3038 q = ( x - 0.5 ) * log(x) - x + MY_kLS2PI;
164 3038 ▶ if( x > 1.0e8 )
Branch: 1 branch
BranchTrueFalseTotal
if( x > 1.0e8 ) 0 3038 3038
165 0 return( q );
166  
167 3038 p = 1.0/(x*x);
168 3038 ▶ if( x >= 1000.0 )
Branch: 1 branch
BranchTrueFalseTotal
if( x >= 1000.0 ) 0 3038 3038
169 0 q += (( 7.9365079365079365079365e-4 * p
170 0 - 2.7777777777777777777778e-3) *p
171 0 + 0.0833333333333333333333) / x;
172 else
173 3038 q += polynomial_eval( p, A, 4 ) / x;
174 3038 return( q );
175 }
176  
177  
178 double
179 771 beta_ab(pTHX_ double a, double b, int k, int N)
180 {
181 int c1, c2;
182 /* Calculates the fraction of the area under the
183 * curve x^k*(1-x)^(N-k) between x=a and x=b */
184  
185 771 ▶ if (a == b) return 0; /* don't bother integrating over zero range */
Branch: 1 branch
BranchTrueFalseTotal
if (a == b) return 0; /* don't bother integrating over zero range */ 0 771 771
186 771 c1 = k+1;
187 771 c2 = N-k+1;
188 771 return ibetai(aTHX_ c1,c2,b)-ibetai(aTHX_ c1,c2,a);
189 }
190  
191 double
192 1542 ibetai(pTHX_ double a, double b, double x)
193 {
194 /* Calculates the incomplete beta function I_x(a,b); this is
195 * the incomplete beta function divided by the complete beta function */
196  
197 double bt;
198 1542 ▶ if (x < 0.0 || x > 1.0) {
Branch: 2 branches
BranchTrueFalseTotal
if (x < 0.0 || x > 1.0) { 1542 0 1542
if (x < 0.0 || x > 1.0) { 0 1542 1542
199 0 const char *err = "ibetai: Illegal x in routine ibetai: x = %g";
200 0 ▶ if (use_exceptions(aTHX))
Branch: 1 branch
BranchTrueFalseTotal
if (use_exceptions(aTHX)) 0 0 0
201 0 croak(err, x);
202 else
203 0 warn(err, x);
204 0 return 0;
205 }
206 1542 ▶ if (x == 0.0 || x == 1.0)
Branch: 2 branches
BranchTrueFalseTotal
if (x == 0.0 || x == 1.0) 1542 0 1542
if (x == 0.0 || x == 1.0) 23 1519 1542
207 23 bt=0.0;
208 else
209 1519 bt=exp(log_gamma(a+b)-log_gamma(a)-log_gamma(b)+a*log(x)+b*log(1.0-x));
210  
211 1542 ▶ if (x < (a+1.0)/(a+b+2.0))
Branch: 1 branch
BranchTrueFalseTotal
if (x < (a+1.0)/(a+b+2.0)) 779 763 1542
212 779 return bt*beta_cf(aTHX_ x,a,b)/a;
213 else
214 763 return 1.0-bt*beta_cf(aTHX_ 1-x,b,a)/b;
215 }
216  
217  
218 1542 double beta_cf(pTHX_ double x, double a, double b)
219 {
220 /* Continued fraction evaluation by modified Lentz's method
221 * used in calculation of incomplete Beta function. */
222  
223 1542 const int itmax = 5000;
224 1542 const double eps = 3.e-14;
225 1542 const double fpmin = 1.e-30;
226  
227 int m, m2;
228 double aa, c, d, del, qab, qam, qap;
229 double h;
230  
231 1542 qab = a+b;
232 1542 qap = a+1.0;
233 1542 qam = a-1.0;
234 1542 c = 1.0;
235 1542 d = 1.0 - qab*x/qap;
236  
237 1542 ▶ if (fabs(d) < fpmin)
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(d) < fpmin) 0 1542 1542
238 0 d = fpmin;
239  
240 1542 d = 1.0/d;
241 1542 h = d;
242 7793 ▶ for (m = 1; m <= itmax; m++) {
Branch: 1 branch
BranchTrueFalseTotal
for (m = 1; m <= itmax; m++) { 7793 0 7793
243 7793 m2 = m * 2;
244 7793 aa = m * (b-m) * x / ((qam + m2) * (a + m2));
245 7793 d = 1.0 + aa * d;
246 7793 ▶ if (fabs(d) < fpmin)
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(d) < fpmin) 0 7793 7793
247 0 d = fpmin;
248 7793 c = 1 + aa / c;
249 7793 ▶ if (fabs(c) < fpmin)
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(c) < fpmin) 0 7793 7793
250 0 c = fpmin;
251 7793 d = 1.0/d;
252 7793 h *= d*c;
253 7793 aa = -(a+m) * (qab +m) * x / ((a+m2)*(qap+m2));
254 7793 d = 1.0 + aa*d;
255 7793 ▶ if (fabs(d) < fpmin)
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(d) < fpmin) 0 7793 7793
256 0 d = fpmin;
257 7793 c = 1.0 + aa/c;
258 7793 ▶ if (fabs(c) < fpmin)
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(c) < fpmin) 0 7793 7793
259 0 c = fpmin;
260 7793 d = 1.0/d;
261 7793 del = d*c;
262 7793 h *= del;
263 7793 ▶ if (fabs(del-1) <= eps)
Branch: 1 branch
BranchTrueFalseTotal
if (fabs(del-1) <= eps) 1542 6251 7793
264 1542 break;
265 }
266  
267 if (DEBUG && m > itmax) {
268 const char *err = "beta_cf: a or b too big, or itmax too small, a=%g, b=%g, x=%g, h=%g, itmax=%d";
269 if (use_exceptions(aTHX))
270 croak(err, a,b,x,h,itmax);
271 else
272 warn(err, a,b,x,h,itmax);
273 }
274 1542 return h;
275 }
276  
277  
278 #undef MY_kDBL_MAX
279 #undef MY_kMAXLGM
280 #undef MY_kLS2PI
281  
↑ Summary EfficiencyCI.xs »