Debian Codegraph @

melder/NUMspecfunc.cpp

raw symbols SCIP
1 /* NUMspecfunc.cpp
2 *
3 * Copyright (C) 1992-2008,2011,2012,2014,2015,2017,2018,2025 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * pb 2002/03/07 GPL
21 * pb 2003/06/19 ridders3 replaced with ridders
22 * pb 2003/07/09 gsl
23 * pb 2003/08/27 NUMfisherQ: underflow and iteration excess should not return undefined
24 * pb 2006/08/02 NUMinvSigmoid
25 * pb 2008/01/19 double
26 * pb 2008/09/21 NUMshift
27 * pb 2008/09/22 NUMscale
28 * pb 2011/03/29 C++
29 */
30
31 #include "melder.h"cxx . . $ `<file>/melder/melder.h`/
32 #include "../dwsys/NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ // NUMridders
33
34 #include "../external/gsl/gsl_errno.h"cxx . . $ `<file>/external/gsl/gsl_errno.h`/
35 #include "../external/gsl/gsl_sf_bessel.h"cxx . . $ `<file>/external/gsl/gsl_sf_bessel.h`/
36 #include "../external/gsl/gsl_sf_gamma.h"cxx . . $ `<file>/external/gsl/gsl_sf_gamma.h`/
37 #include "../external/gsl/gsl_sf_erf.h"cxx . . $ `<file>/external/gsl/gsl_sf_erf.h`/
38
39 double NUMlnGammacxx . . $ NUMlnGamma(447c67c6676f75c3). (double x) {
40 gsl_sf_result result;
41 int status = gsl_sf_lngamma_e (x, & result);
42 return ( status == GSL_SUCCESS ? result. val : undefinedcxx . . $ undefined. );
43 }
44
45 double NUMbetacxx . . $ NUMbeta(d8e7e0733fa41c87). (double z, double w) {
46 if (z <= 0.0 || w <= 0.0) return undefinedcxx . . $ undefined.;
47 return expcxx . . $ exp(fee4f1d22bc8e617). (NUMlnGammacxx . . $ NUMlnGamma(447c67c6676f75c3). (z) + NUMlnGammacxx . . $ NUMlnGamma(447c67c6676f75c3). (w) - NUMlnGammacxx . . $ NUMlnGamma(447c67c6676f75c3). (z + w));
48 }
49
50 double NUMincompleteBetacxx . . $ NUMincompleteBeta(7678bba071383ab). (double a, double b, double x) {
51 gsl_sf_result result;
52 int status = gsl_sf_beta_inc_e (a, b, x, & result);
53 if (status != GSL_SUCCESS && status != GSL_EUNDRFLW && status != GSL_EMAXITER) {
54 Melder_crashcxx . . $ `melder/melder_error.h:98:9`! (U"NUMincompleteBeta status ", status);
55 return undefinedcxx . . $ undefined.;
56 }
57 return result. val;
58 }
59
60 double NUMbinomialPcxx . . $ NUMbinomialP(7678bba071383ab). (double p, double k, double n) {
61 double binomialQ;
62 if (p < 0.0 || p > 1.0 || n <= 0.0 || k < 0.0 || k > n) return undefinedcxx . . $ undefined.;
63 if (k == n) return 1.0;
64 binomialQ = NUMincompleteBetacxx . . $ NUMincompleteBeta(7678bba071383ab). (k + 1, n - k, p);
65 if (isundefcxx . . $ isundef(f8197d536af80c96). (binomialQ)) return undefinedcxx . . $ undefined.;
66 return 1.0 - binomialQ;
67 }
68
69 double NUMbinomialQcxx . . $ NUMbinomialQ(7678bba071383ab). (double p, double k, double n) {
70 if (p < 0.0 || p > 1.0 || n <= 0.0 || k < 0.0 || k > n) return undefinedcxx . . $ undefined.;
71 if (k == 0.0) return 1.0;
72 return NUMincompleteBetacxx . . $ NUMincompleteBeta(7678bba071383ab). (k, n - k + 1, p);
73 }
74
75 struct binomialcxx . . $ binomial# { double pcxx . . $ binomial#p., kcxx . . $ binomial#k., ncxx . . $ binomial#n.; };
76
77 static double binomialPcxx . . $ binomialP(81b005370e6958c4). (double p, void *binomial_void) {
78 struct binomialcxx . . $ binomial# *binomial = (struct binomialcxx . . $ binomial# *) binomial_void;
79 return NUMbinomialPcxx . . $ NUMbinomialP(7678bba071383ab). (p, binomial -> kcxx . . $ binomial#k., binomial -> ncxx . . $ binomial#n.) - binomial -> pcxx . . $ binomial#p.;
80 }
81
82 static double binomialQcxx . . $ binomialQ(81b005370e6958c4). (double p, void *binomial_void) {
83 struct binomialcxx . . $ binomial# *binomial = (struct binomialcxx . . $ binomial# *) binomial_void;
84 return NUMbinomialQcxx . . $ NUMbinomialQ(7678bba071383ab). (p, binomial -> kcxx . . $ binomial#k., binomial -> ncxx . . $ binomial#n.) - binomial -> pcxx . . $ binomial#p.;
85 }
86
87 double NUMinvBinomialPcxx . . $ NUMinvBinomialP(7678bba071383ab). (double p, double k, double n) {
88 static struct binomialcxx . . $ binomial# binomial;
89 if (p < 0 || p > 1 || n <= 0 || k < 0 || k > n) return undefinedcxx . . $ undefined.;
90 if (k == n) return 1.0;
91 binomial. pcxx . . $ binomial#p. = p;
92 binomial. kcxx . . $ binomial#k. = k;
93 binomial. ncxx . . $ binomial#n. = n;
94 return NUMridderscxx . . $ NUMridders(25f8e7b9d159e245). (binomialPcxx . . $ binomialP(81b005370e6958c4)., 0.0, 1.0, & binomial);
95 }
96
97 double NUMinvBinomialQcxx . . $ NUMinvBinomialQ(7678bba071383ab). (double p, double k, double n) {
98 static struct binomialcxx . . $ binomial# binomial;
99 if (p < 0 || p > 1 || n <= 0 || k < 0 || k > n) return undefinedcxx . . $ undefined.;
100 if (k == 0) return 0.0;
101 binomial. pcxx . . $ binomial#p. = p;
102 binomial. kcxx . . $ binomial#k. = k;
103 binomial. ncxx . . $ binomial#n. = n;
104 return NUMridderscxx . . $ NUMridders(25f8e7b9d159e245). (binomialQcxx . . $ binomialQ(81b005370e6958c4)., 0.0, 1.0, & binomial);
105 }
106
107 /* Modified Bessel function I0. Abramowicz & Stegun, p. 378.*/
108 double NUMbessel_i0_fcxx . . $ NUMbessel_i0_f(447c67c6676f75c3). (double x) {
109 if (x < 0.0) return NUMbessel_i0_fcxx . . $ NUMbessel_i0_f(447c67c6676f75c3). (- x);
110 if (x < 3.75) {
111 /* Formula 9.8.1. Accuracy 1.6e-7. */
112 double t = x / 3.75;
113 t *= t;
114 return 1.0 + t * (3.5156229 + t * (3.0899424 + t * (1.2067492
115 + t * (0.2659732 + t * (0.0360768 + t * 0.0045813)))));
116 }
117 /*
118 otherwise: x >= 3.75
119 */
120 /* Formula 9.8.2. Accuracy of the polynomial factor 1.9e-7. */
121 double t = 3.75 / x; /* <= 1.0 */
122 return expcxx . . $ exp(fee4f1d22bc8e617). (x) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (x) * (0.39894228 + t * (0.01328592
123 + t * (0.00225319 + t * (-0.00157565 + t * (0.00916281
124 + t * (-0.02057706 + t * (0.02635537 + t * (-0.01647633
125 + t * 0.00392377))))))));
126 }
127
128 /* Modified Bessel function I1. Abramowicz & Stegun, p. 378. */
129 double NUMbessel_i1_fcxx . . $ NUMbessel_i1_f(447c67c6676f75c3). (double x) {
130 if (x < 0.0) return - NUMbessel_i1_fcxx . . $ NUMbessel_i1_f(447c67c6676f75c3). (- x);
131 if (x < 3.75) {
132 /* Formula 9.8.3. Accuracy of the polynomial factor 8e-9. */
133 double t = x / 3.75;
134 t *= t;
135 return x * (0.5 + t * (0.87890594 + t * (0.51498869 + t * (0.15084934
136 + t * (0.02658733 + t * (0.00301532 + t * 0.00032411))))));
137 }
138 /*
139 otherwise: x >= 3.75
140 */
141 /* Formula 9.8.4. Accuracy of the polynomial factor 2.2e-7. */
142 double t = 3.75 / x; /* <= 1.0 */
143 return expcxx . . $ exp(fee4f1d22bc8e617). (x) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (x) * (0.39894228 + t * (-0.03988024
144 + t * (-0.00362018 + t * (0.00163801 + t * (-0.01031555
145 + t * (0.02282967 + t * (-0.02895312 + t * (0.01787654
146 + t * (-0.00420059)))))))));
147 }
148
149 double NUMbesselIcxx . . $ NUMbesselI(9976ed6c9a6c4849). (integercxx . . $ integer# n, double x) {
150 gsl_sf_result result;
151 int status = gsl_sf_bessel_In_e (n, x, & result);
152 return ( status == GSL_SUCCESS ? result. val : undefinedcxx . . $ undefined. );
153 }
154
155 /* Modified Bessel function K0. Abramowicz & Stegun, p. 379. */
156 double NUMbessel_k0_fcxx . . $ NUMbessel_k0_f(447c67c6676f75c3). (double x) {
157 if (x <= 0.0) return undefinedcxx . . $ undefined.;
158 if (x <= 2.0) {
159 /* Formula 9.8.5. Accuracy 1e-8. */
160 double x2 = 0.5 * x, t = x2 * x2;
161 return - logcxx . . $ log(fee4f1d22bc8e617). (x2) * NUMbessel_i0_fcxx . . $ NUMbessel_i0_f(447c67c6676f75c3). (x) + (-0.57721566 + t * (0.42278420
162 + t * (0.23069756 + t * (0.03488590 + t * (0.00262698
163 + t * (0.00010750 + t * 0.00000740))))));
164 }
165 /*
166 otherwise: 2 < x < positive infinity
167 */
168 /* Formula 9.8.6. Accuracy of the polynomial factor 1.9e-7. */
169 double t = 2.0 / x; /* < 1.0 */
170 return expcxx . . $ exp(fee4f1d22bc8e617). (- x) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (x) * (1.25331414 + t * (-0.07832358
171 + t * (0.02189568 + t * (-0.01062446 + t * (0.00587872
172 + t * (-0.00251540 + t * 0.00053208))))));
173 }
174
175 /* Modified Bessel function K1. Abramowicz & Stegun, p. 379. */
176 double NUMbessel_k1_fcxx . . $ NUMbessel_k1_f(447c67c6676f75c3). (double x) {
177 if (x <= 0.0) return undefinedcxx . . $ undefined.;
178 if (x <= 2.0) {
179 /* Formula 9.8.7. Accuracy of the polynomial factor 8e-9. */
180 double x2 = 0.5 * x, t = x2 * x2;
181 return logcxx . . $ log(fee4f1d22bc8e617). (x2) * NUMbessel_i1_fcxx . . $ NUMbessel_i1_f(447c67c6676f75c3). (x) + (1.0 / x) * (1.0 + t * (0.15443144
182 + t * (-0.67278579 + t * (-0.18156897 + t * (-0.01919402
183 + t * (-0.00110404 + t * (-0.00004686)))))));
184 }
185 /*
186 otherwise: 2 < x < positive infinity
187 */
188 /* Formula 9.8.8. Accuracy of the polynomial factor 2.2e-7. */
189 double t = 2.0 / x; /* < 1.0 */
190 return expcxx . . $ exp(fee4f1d22bc8e617). (- x) / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (x) * (1.25331414 + t * (0.23498619
191 + t * (-0.03655620 + t * (0.01504268 + t * (-0.00780353
192 + t * (0.00325614 + t * (-0.00068245)))))));
193 }
194
195 double NUMbesselK_fcxx . . $ NUMbesselK_f(9976ed6c9a6c4849). (integercxx . . $ integer# n, double x) {
196 double besselK = undefinedcxx . . $ undefined.;
197 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n >= 0 && x > 0);
198 double besselK_min2 = NUMbessel_k0_fcxx . . $ NUMbessel_k0_f(447c67c6676f75c3). (x);
199 if (n == 0) return besselK_min2;
200 double besselK_min1 = NUMbessel_k1_fcxx . . $ NUMbessel_k1_f(447c67c6676f75c3). (x);
201 if (n == 1) return besselK_min1;
202 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n >= 2);
203 double twoByX = 2.0 / x;
204 /*
205 Recursion formula.
206 */
207 for (integercxx . . $ integer# i = 1; i < n; i ++) {
208 besselK = besselK_min2 + twoByX * i * besselK_min1;
209 besselK_min2 = besselK_min1;
210 besselK_min1 = besselK;
211 }
212 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (isdefined (besselK));
213 return besselK;
214 }
215
216 double NUMbesselKcxx . . $ NUMbesselK(9976ed6c9a6c4849). (integercxx . . $ integer# n, double x) {
217 gsl_sf_result result;
218 int status = gsl_sf_bessel_Kn_e ((int) n, x, & result);
219 return ( status == GSL_SUCCESS ? result. val : undefinedcxx . . $ undefined. );
220 }
221
222 double NUMsigmoidcxx . . $ NUMsigmoid(447c67c6676f75c3). (double x)
223 { return x > 0.0 ? 1.0 / (1.0 + expcxx . . $ exp(fee4f1d22bc8e617). (- x)) : 1.0 - 1.0 / (1.0 + expcxx . . $ exp(fee4f1d22bc8e617). (x)); }
224
225 double NUMinvSigmoidcxx . . $ NUMinvSigmoid(447c67c6676f75c3). (double x)
226 { return x <= 0.0 || x >= 1.0 ? undefinedcxx . . $ undefined. : logcxx . . $ log(fee4f1d22bc8e617). (x / (1.0 - x)); }
227
228 double NUMerfcccxx . . $ NUMerfcc(447c67c6676f75c3). (double x) {
229 gsl_sf_result result;
230 int status = gsl_sf_erfc_e (x, & result);
231 return status == GSL_SUCCESS ? result. val : undefinedcxx . . $ undefined.;
232 }
233
234 double NUMgaussPcxx . . $ NUMgaussP(447c67c6676f75c3). (double z) {
235 return 1.0 - 0.5 * NUMerfcccxx . . $ NUMerfcc(447c67c6676f75c3). (NUMsqrt1_2cxx . . $ NUMsqrt1_2. * z);
236 }
237
238 double NUMgaussQcxx . . $ NUMgaussQ(447c67c6676f75c3). (double z) {
239 return 0.5 * NUMerfcccxx . . $ NUMerfcc(447c67c6676f75c3). (NUMsqrt1_2cxx . . $ NUMsqrt1_2. * z);
240 }
241
242 double NUMincompleteGammaPcxx . . $ NUMincompleteGammaP(d8e7e0733fa41c87). (double a, double x) {
243 gsl_sf_result result;
244 int status = gsl_sf_gamma_inc_P_e (a, x, & result);
245 return status == GSL_SUCCESS ? result. val : undefinedcxx . . $ undefined.;
246 }
247
248 double NUMincompleteGammaQcxx . . $ NUMincompleteGammaQ(d8e7e0733fa41c87). (double a, double x) {
249 gsl_sf_result result;
250 int status = gsl_sf_gamma_inc_Q_e (a, x, & result);
251 return status == GSL_SUCCESS ? result. val : undefinedcxx . . $ undefined.;
252 }
253
254 double NUMchiSquarePcxx . . $ NUMchiSquareP(d8e7e0733fa41c87). (double chiSquare, double degreesOfFreedom) {
255 if (chiSquare < 0 || degreesOfFreedom <= 0) return undefinedcxx . . $ undefined.;
256 return NUMincompleteGammaPcxx . . $ NUMincompleteGammaP(d8e7e0733fa41c87). (0.5 * degreesOfFreedom, 0.5 * chiSquare);
257 }
258
259 double NUMchiSquareQcxx . . $ NUMchiSquareQ(d8e7e0733fa41c87). (double chiSquare, double degreesOfFreedom) {
260 if (chiSquare < 0 || degreesOfFreedom <= 0) return undefinedcxx . . $ undefined.;
261 return NUMincompleteGammaQcxx . . $ NUMincompleteGammaQ(d8e7e0733fa41c87). (0.5 * degreesOfFreedom, 0.5 * chiSquare);
262 }
263
264 double NUMcombinationscxx . . $ NUMcombinations(f630275aa3e30a1e). (integercxx . . $ integer# n, integercxx . . $ integer# k) {
265 longdoublecxx . . $ longdouble# result = 1.0;
266 if (k > n / 2) k = n - k;
267 for (integercxx . . $ integer# i = 1; i <= k; i ++) result *= n - i + 1;
268 for (integercxx . . $ integer# i = 2; i <= k; i ++) result /= i;
269 return (double) result;
270 }
271
272 /* End of file NUMspecfunc.cpp */