Debian Codegraph @

dwtest/test_gsl.praat

raw
1 # test_gsl.praat
2 # djmw 20071017, 20080317, 20120223
3
4 debug = 0
5 eps = 2.2204460492503131e-16
6 tol0 = 2.0*eps
7 tol_2= 4.0*eps
8 tol1 = 16.0*eps
9 tol2 = 256.0*eps
10 tol3 = 2048.0*eps
11 tol4 = 16384.0*eps
12 tol5 = 131072.0*eps
13 tol6 = 1048576.0*eps
14 sqrt_tol0 = sqrt(2.0*eps)
15
16 appendInfoLine: "test_gsl.praat"
17
18 @test_besselI
19 @test_besselK
20 @test_erfc
21 @test_erf
22 @test_lnGamma
23 @test_incompleteBeta
24 @test_incompleteGammaP
25 @test_sincpi
26
27 appendInfoLine: "test_gsl.praat OK"
28
29 procedure func_1arg: .func$, .arg, .r, .tol
30 assert$ = ""
31 .form$ = .func$ + "(" + string$(.arg) + ")"
32 .res = evaluate (.form$)
33 .result$ = .form$ + "=" + string$ (.res) + " (" + string$ (.r) + ")"
34 .denom = .res+.r
35 if .denom = 0
36 .denom = 1
37 endif
38 .z = abs ((.res-.r) / .denom)
39 if .z > .tol
40 assert$ = "!!Assertion failed: "
41 endif
42 if debug <> 0 or assert$ <> ""
43 .neps = .z / eps
44 appendInfoLine: tab$, tab$, assert$, .result$, "; n*eps=", fixed$(.neps,2)
45 endif
46 endproc
47
48 procedure func_1argzero: .func$, .arg, .tol
49 assert$ = ""
50 .form$ = .func$ + "(" + string$(.arg) + ")"
51 .res = evaluate (.form$)
52 .z = abs (.res)
53 if .z > .tol
54 assert$ = "!!Assertion failed: "
55 endif
56 if debug <> 0 or assert$ <> ""
57 .neps = .z / eps
58 appendInfoLine: tab$, tab$, assert$, " ", .form$, "=0, n*eps=", fixed$ (.neps,2)
59 endif
60 endproc
61
62 procedure func_2args: .func$, .arg1, .arg2, .r, .tol
63 .form$ = .func$ + "(" + string$(.arg1) + "," + string$ (.arg2) + ")"
64 .res = evaluate (.form$)
65 .result$ = .form$ + "=" + string$ (.res) + " (" + string$ (.r) + ")"
66 .denom = .res+.r
67 if .denom <> 0
68 .z = abs((.res-.r)/(.res+.r))
69 if debug = 1
70 .neps = .z/eps
71 appendInfoLine: tab$, tab$, .result$, "; ", fixed$(.neps,2), " ", .z
72 endif
73 assert abs((.res-.r)/(.res+.r)) < .tol; '.result$' ('.z')
74 else
75 if debug = 1
76 .z = abs(.res-.r)
77 .neps = .z / eps
78 appendInfoLine: tab$, tab$, .form$, "=", .res, " (",.r, "); ", fixed$ (.neps,2), " ", .z
79 endif
80 assert abs(.res-.r) < .tol; '.result$'
81 endif
82 endproc
83
84 procedure func_3args .func$ .arg1 .arg2 .arg3 .r .tol
85 .form$ = .func$ + "(" + string$(.arg1) + "," + string$ (.arg2) + "," + string$ (.arg3) + ")"
86 .res = evaluate (.form$)
87 .result$ = .form$ + "=" + string$ (.res) + " (" + string$ (.r) + ")"
88 .denom = .res+.r
89 if .denom <> 0
90 if debug = 1
91 .z = abs((.res-.r)/(.res+.r))
92 .neps = .z/eps
93 appendInfoLine: tab$, tab$, .result$, "; ", fixed$ (.neps,2), " ", .z
94 endif
95 assert abs((.res-.r)/(.res+.r)) < .tol; '.result$'
96 else
97 if debug = 1
98 .z = abs(.res-.r)
99 .neps = .z / eps
100 appendInfoLine: tab$, tab$, .result$, "; ", fixed$ (.neps,2), " ", .z
101 endif
102 assert abs(.res-.r) < .tol; '.result$'
103 endif
104 endproc
105
106 procedure test_besselI
107 appendInfoLine: tab$, "besselI:"
108 @func_2args: "besselI", 4, 0.1, 2.6054690212996573677e-07, tol0
109 @func_2args: "besselI", 5, 2.0, 0.009825679323131702321, tol0
110 @func_2args: "besselI", 100, 100.0, 4.641534941616199114e+21, tol2
111 endproc
112
113 procedure test_besselK
114 appendInfoLine: tab$, "besselK:"
115 @func_2args: "besselK", 4, 0.1, 479600.2497925682849, tol_2
116 @func_2args: "besselK", 5, 2.0, 9.431049100596467443, tol0
117 @func_2args: "besselK", 100, 100.0, 7.617129630494085416e-25, tol2
118 endproc
119
120 procedure test_erfc
121 appendInfoLine: tab$, "erfc:"
122 @func_1arg: "erfc", -10.0, 2.0, tol0
123 @func_1arg: "erfc", -5.0000002, 1.9999999999984625433, tol0
124 @func_1arg: "erfc", -5.0, 1.9999999999984625402, tol0
125 @func_1arg: "erfc", -1.0, 1.8427007929497148693, tol0
126 @func_1arg: "erfc", -0.5, 1.5204998778130465377, tol0
127 @func_1arg: "erfc", 1.0, 0.15729920705028513066, tol0
128 @func_1arg: "erfc", 3.0, 0.000022090496998585441373, tol1
129 @func_1arg: "erfc", 7.0, 4.183825607779414399e-23, tol2
130 @func_1arg: "erfc", 10.0, 2.0884875837625447570e-45, tol2
131 endproc
132
133 procedure test_erf
134 appendInfoLine: tab$, "erf:"
135 @func_1arg: "erf", -10.0, -1.0000000000000000000, tol0
136 @func_1arg: "erf", 0.5, 0.5204998778130465377, tol0
137 @func_1arg: "erf", 1.0, 0.8427007929497148693, tol0
138 @func_1arg: "erf", 10.0, 1.0000000000000000000, tol0
139 endproc
140
141 procedure test_lnGamma
142 appendInfoLine: tab$, "lnGamma:"
143 @func_1arg: "lnGamma", -0.1, 2.368961332728788655, tol0
144 @func_1arg: "lnGamma", -1.0/256.0, 5.547444766967471595, tol0
145 @func_1arg: "lnGamma", 1.0e-08, 18.420680738180208905, tol0
146 @func_1arg: "lnGamma", 0.1, 2.252712651734205, tol0
147 @func_1arg: "lnGamma", 1.0+1.0/256.0, -0.0022422226599611501448, tol0
148 @func_1arg: "lnGamma", 2.0+1.0/256.0, 0.0016564177556961728692, tol0
149 @func_1arg: "lnGamma", 100.0, 359.1342053695753, tol0
150 @func_1arg: "lnGamma", -1.0-1.0/65536.0, 11.090348438090047844, tol0
151 @func_1arg: "lnGamma", -1.0-1.0/268435456.0, 19.408121054103474300, tol0
152 @func_1arg: "lnGamma", -100.5, -364.9009683094273518, tol0
153 @func_1arg: "lnGamma", -100-1.0/65536.0, -352.6490910117097874, tol0
154 @func_1arg: "lnGamma", -1.5, ln(4*sqrt(pi)/3), tol1
155 @func_1arg: "lnGamma", 0.5, 0.5*ln(pi), tol0
156 @func_1arg: "lnGamma", 1, 0, tol0
157 @func_1arg: "lnGamma", 1.5, ln(sqrt(pi)/2), tol2
158 @func_1arg: "lnGamma", 2, 0, tol0
159 @func_1arg: "lnGamma", 2.5, ln(3*sqrt(pi)/4), tol1
160 @func_1arg: "lnGamma", 3, ln(2), tol_2
161 @func_1arg: "lnGamma", 3.5, ln(15*sqrt(pi)/8), tol_2
162 @func_1arg: "lnGamma", 4, ln(6), tol_2
163 endproc
164
165
166 # incompleteBeta
167 # Limiting values: $I_0(a,b)=0 I_1(a,b)=1$
168 # Symmetry: $I_x(a,b) = 1 - I_{1-x}(b,a)$
169
170 procedure test_incompleteBeta
171 appendInfoLine: tab$, "incompleteBeta:"
172 appendInfoLine: tab$, tab$, "incompleteBeta (a,b,0) = 0 & incompleteBeta (a,b,1) = 1:"
173 for .i to 20
174 .a = randomUniform (0, 50)
175 .b = randomUniform (0, 50)
176 @func_3args: "incompleteBeta", .a, .b, 0, 0, tol0
177 @func_3args: "incompleteBeta", .a, .b, 1, 1, tol0
178 endfor
179 appendInfoLine: tab$, tab$, "incompleteBeta(i, 1, 0.1)=10^(-i) & incompleteBeta(a,b,x)=1-incompleteBeta(b,a,1-x):"
180 for .i to 310
181 .a = .i
182 .b = 1
183 .x = 0.1
184 @func_3args: "incompleteBeta", .a, .b, .x ,10^(-.i), tol1*.i
185 # if i > 16 then 1-10^(-.i) equals 1!
186 @func_3args: "incompleteBeta", .b, .a, 1-.x, 1-10^(-.i), tol1*.i
187 endfor
188 endproc
189
190 procedure test_incompleteGammaP
191 appendInfoLine: tab$, "incompleteGammaP:"
192 appendInfoLine: tab$, tab$, "incompleteGammaP(a,0)=0 & incompleteGammaP(a,inf)=1:"
193 for .i to 20
194 .a = randomUniform (0, 100)
195 @func_2args: "incompleteGammaP", .a, 0, 0, tol0
196 @func_2args: "incompleteGammaP", .a, 10^200, 1, tol0
197 endfor
198 appendInfoLine: tab$, tab$, "incompleteGammaP(1,x)=1-exp(-x):"
199 emax = round(19*ln(10))
200 emin = 0.01 ; smaller values need larger eps
201 for .i to 50
202 .x = randomUniform (emin, emax)
203 @func_2args: "incompleteGammaP", 1, .x, 1-exp(-.x), tol3
204 .x = randomUniform (0, emin)
205 @func_2args: "incompleteGammaP", 1, .x, 1-exp(-.x), 1e-6
206 endfor
207 appendInfoLine: tab$, tab$, "incompleteGammaP(2,x)=(1-(1+x)*exp(-x)):"
208 for .i to 100
209 .x = randomUniform (emin, emax)
210 @func_2args: "incompleteGammaP", 2, .x, (1-(1+.x)*exp(-.x)), 2*tol4
211 endfor
212 appendInfoLine: tab$, tab$, "incompleteGammaP(3,x)=(1-(1+x+0.5*x^2)*exp(-x))/2:"
213 for .i to 50
214 .x = randomUniform (emin, emax)
215 @func_2args: "incompleteGammaP", 3, .x, 1-(1+.x+0.5*.x^2)*exp(-.x), 2*tol6
216 .x = randomUniform (0.0001, emin)
217 @func_2args: "incompleteGammaP", 3, .x, 1-(1+.x+0.5*.x^2)*exp(-.x), 7e-4
218 endfor
219 endproc
220
221 procedure test_sincpi
222 appendInfoLine: tab$, "sincpi:"
223 @func_1arg: "sincpi", 0, 1, tol0
224 for .i to 200
225 .arg = .i
226 @func_1argzero: "sincpi", .arg, tol0
227 .arg = -.arg
228 @func_1argzero: "sincpi", .arg, tol0
229 endfor
230 endproc
231
232 procedure gP
233 for .i to 10
234 .x = randomUniform(0,0.1)
235 .r1= incompleteGammaP(2, .x)
236 .r2 = 1-(1+.x)*exp(-.x)
237 .diff = .r1 - .r2
238 appendInfoLine: .x, " ", .diff, " ", .r1
239 endfor
240 endproc