Debian Codegraph @

melder/NUMspecfunc.cpp

raw
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"
32 #include "../dwsys/NUM2.h" // NUMridders
33
34 #include "../external/gsl/gsl_errno.h"
35 #include "../external/gsl/gsl_sf_bessel.h"
36 #include "../external/gsl/gsl_sf_gamma.h"
37 #include "../external/gsl/gsl_sf_erf.h"
38
39 double NUMlnGamma (double x) {
40 gsl_sf_result result;
41 int status = gsl_sf_lngamma_e (x, & result);
42 return ( status == GSL_SUCCESS ? result. val : undefined );
43 }
44
45 double NUMbeta (double z, double w) {
46 if (z <= 0.0 || w <= 0.0) return undefined;
47 return exp (NUMlnGamma (z) + NUMlnGamma (w) - NUMlnGamma (z + w));
48 }
49
50 double NUMincompleteBeta (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_crash (U"NUMincompleteBeta status ", status);
55 return undefined;
56 }
57 return result. val;
58 }
59
60 double NUMbinomialP (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 undefined;
63 if (k == n) return 1.0;
64 binomialQ = NUMincompleteBeta (k + 1, n - k, p);
65 if (isundef (binomialQ)) return undefined;
66 return 1.0 - binomialQ;
67 }
68
69 double NUMbinomialQ (double p, double k, double n) {
70 if (p < 0.0 || p > 1.0 || n <= 0.0 || k < 0.0 || k > n) return undefined;
71 if (k == 0.0) return 1.0;
72 return NUMincompleteBeta (k, n - k + 1, p);
73 }
74
75 struct binomial { double p, k, n; };
76
77 static double binomialP (double p, void *binomial_void) {
78 struct binomial *binomial = (struct binomial *) binomial_void;
79 return NUMbinomialP (p, binomial -> k, binomial -> n) - binomial -> p;
80 }
81
82 static double binomialQ (double p, void *binomial_void) {
83 struct binomial *binomial = (struct binomial *) binomial_void;
84 return NUMbinomialQ (p, binomial -> k, binomial -> n) - binomial -> p;
85 }
86
87 double NUMinvBinomialP (double p, double k, double n) {
88 static struct binomial binomial;
89 if (p < 0 || p > 1 || n <= 0 || k < 0 || k > n) return undefined;
90 if (k == n) return 1.0;
91 binomial. p = p;
92 binomial. k = k;
93 binomial. n = n;
94 return NUMridders (binomialP, 0.0, 1.0, & binomial);
95 }
96
97 double NUMinvBinomialQ (double p, double k, double n) {
98 static struct binomial binomial;
99 if (p < 0 || p > 1 || n <= 0 || k < 0 || k > n) return undefined;
100 if (k == 0) return 0.0;
101 binomial. p = p;
102 binomial. k = k;
103 binomial. n = n;
104 return NUMridders (binomialQ, 0.0, 1.0, & binomial);
105 }
106
107 /* Modified Bessel function I0. Abramowicz & Stegun, p. 378.*/
108 double NUMbessel_i0_f (double x) {
109 if (x < 0.0) return NUMbessel_i0_f (- 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 exp (x) / sqrt (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_f (double x) {
130 if (x < 0.0) return - NUMbessel_i1_f (- 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 exp (x) / sqrt (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 NUMbesselI (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 : undefined );
153 }
154
155 /* Modified Bessel function K0. Abramowicz & Stegun, p. 379. */
156 double NUMbessel_k0_f (double x) {
157 if (x <= 0.0) return 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 - log (x2) * NUMbessel_i0_f (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 exp (- x) / sqrt (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_f (double x) {
177 if (x <= 0.0) return 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 log (x2) * NUMbessel_i1_f (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 exp (- x) / sqrt (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_f (integer n, double x) {
196 double besselK = undefined;
197 Melder_assert (n >= 0 && x > 0);
198 double besselK_min2 = NUMbessel_k0_f (x);
199 if (n == 0) return besselK_min2;
200 double besselK_min1 = NUMbessel_k1_f (x);
201 if (n == 1) return besselK_min1;
202 Melder_assert (n >= 2);
203 double twoByX = 2.0 / x;
204 /*
205 Recursion formula.
206 */
207 for (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_assert (isdefined (besselK));
213 return besselK;
214 }
215
216 double NUMbesselK (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 : undefined );
220 }
221
222 double NUMsigmoid (double x)
223 { return x > 0.0 ? 1.0 / (1.0 + exp (- x)) : 1.0 - 1.0 / (1.0 + exp (x)); }
224
225 double NUMinvSigmoid (double x)
226 { return x <= 0.0 || x >= 1.0 ? undefined : log (x / (1.0 - x)); }
227
228 double NUMerfcc (double x) {
229 gsl_sf_result result;
230 int status = gsl_sf_erfc_e (x, & result);
231 return status == GSL_SUCCESS ? result. val : undefined;
232 }
233
234 double NUMgaussP (double z) {
235 return 1.0 - 0.5 * NUMerfcc (NUMsqrt1_2 * z);
236 }
237
238 double NUMgaussQ (double z) {
239 return 0.5 * NUMerfcc (NUMsqrt1_2 * z);
240 }
241
242 double NUMincompleteGammaP (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 : undefined;
246 }
247
248 double NUMincompleteGammaQ (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 : undefined;
252 }
253
254 double NUMchiSquareP (double chiSquare, double degreesOfFreedom) {
255 if (chiSquare < 0 || degreesOfFreedom <= 0) return undefined;
256 return NUMincompleteGammaP (0.5 * degreesOfFreedom, 0.5 * chiSquare);
257 }
258
259 double NUMchiSquareQ (double chiSquare, double degreesOfFreedom) {
260 if (chiSquare < 0 || degreesOfFreedom <= 0) return undefined;
261 return NUMincompleteGammaQ (0.5 * degreesOfFreedom, 0.5 * chiSquare);
262 }
263
264 double NUMcombinations (integer n, integer k) {
265 longdouble result = 1.0;
266 if (k > n / 2) k = n - k;
267 for (integer i = 1; i <= k; i ++) result *= n - i + 1;
268 for (integer i = 2; i <= k; i ++) result /= i;
269 return (double) result;
270 }
271
272 /* End of file NUMspecfunc.cpp */