test_gsl.praat 7.4 KB

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