rb-gsl 1.15.3.1 → 1.15.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. checksums.yaml +4 -4
  2. data/AUTHORS +2 -2
  3. data/ChangeLog +8 -0
  4. data/Rakefile +39 -96
  5. data/VERSION +1 -1
  6. data/ext/array.c +2 -2
  7. data/ext/block_source.c +1 -1
  8. data/ext/dirac.c +1 -1
  9. data/ext/eigen.c +13 -13
  10. data/ext/extconf.rb +17 -11
  11. data/ext/function.c +5 -5
  12. data/ext/gsl_narray.c +6 -6
  13. data/ext/histogram.c +7 -6
  14. data/ext/histogram2d.c +4 -4
  15. data/ext/interp.c +1 -1
  16. data/ext/linalg.c +13 -11
  17. data/ext/linalg_complex.c +8 -6
  18. data/ext/math.c +1 -1
  19. data/ext/matrix_complex.c +6 -6
  20. data/ext/matrix_source.c +10 -10
  21. data/ext/monte.c +2 -2
  22. data/ext/multimin.c +4 -4
  23. data/ext/multiroots.c +8 -8
  24. data/ext/nmf.c +6 -6
  25. data/ext/ntuple.c +4 -4
  26. data/ext/odeiv.c +2 -2
  27. data/ext/permutation.c +1 -1
  28. data/ext/poly2.c +6 -6
  29. data/ext/poly_source.c +9 -6
  30. data/ext/sf.c +31 -45
  31. data/ext/signal.c +2 -2
  32. data/ext/vector_complex.c +11 -10
  33. data/ext/vector_double.c +7 -4
  34. data/ext/vector_source.c +29 -26
  35. data/ext/wavelet.c +1 -1
  36. data/include/rb_gsl_common.h +12 -0
  37. data/include/rb_gsl_config.h +10 -1
  38. data/rdoc/blas.rdoc +4 -4
  39. data/rdoc/bspline.rdoc +8 -8
  40. data/rdoc/cheb.rdoc +9 -9
  41. data/rdoc/cholesky_complex.rdoc +1 -1
  42. data/rdoc/combi.rdoc +10 -10
  43. data/rdoc/complex.rdoc +12 -12
  44. data/rdoc/const.rdoc +21 -21
  45. data/rdoc/dht.rdoc +7 -7
  46. data/rdoc/diff.rdoc +7 -7
  47. data/rdoc/ehandling.rdoc +4 -4
  48. data/rdoc/eigen.rdoc +12 -12
  49. data/rdoc/fft.rdoc +27 -27
  50. data/rdoc/fit.rdoc +19 -19
  51. data/rdoc/function.rdoc +1 -1
  52. data/rdoc/graph.rdoc +3 -3
  53. data/rdoc/hist.rdoc +17 -17
  54. data/rdoc/hist2d.rdoc +5 -5
  55. data/rdoc/hist3d.rdoc +4 -4
  56. data/rdoc/index.rdoc +4 -4
  57. data/rdoc/integration.rdoc +17 -17
  58. data/rdoc/interp.rdoc +12 -12
  59. data/rdoc/intro.rdoc +4 -4
  60. data/rdoc/linalg.rdoc +21 -21
  61. data/rdoc/linalg_complex.rdoc +1 -1
  62. data/rdoc/math.rdoc +14 -14
  63. data/rdoc/matrix.rdoc +21 -21
  64. data/rdoc/min.rdoc +10 -10
  65. data/rdoc/monte.rdoc +4 -4
  66. data/rdoc/multimin.rdoc +13 -13
  67. data/rdoc/multiroot.rdoc +13 -13
  68. data/rdoc/narray.rdoc +10 -10
  69. data/rdoc/ndlinear.rdoc +5 -5
  70. data/rdoc/nonlinearfit.rdoc +18 -18
  71. data/rdoc/ntuple.rdoc +6 -6
  72. data/rdoc/odeiv.rdoc +13 -13
  73. data/rdoc/perm.rdoc +12 -12
  74. data/rdoc/poly.rdoc +18 -18
  75. data/rdoc/qrng.rdoc +10 -10
  76. data/rdoc/randist.rdoc +11 -11
  77. data/rdoc/ref.rdoc +50 -50
  78. data/rdoc/rng.rdoc +10 -10
  79. data/rdoc/rngextra.rdoc +5 -5
  80. data/rdoc/roots.rdoc +13 -13
  81. data/rdoc/sf.rdoc +36 -36
  82. data/rdoc/siman.rdoc +4 -4
  83. data/rdoc/sort.rdoc +7 -7
  84. data/rdoc/start.rdoc +1 -1
  85. data/rdoc/stats.rdoc +14 -14
  86. data/rdoc/sum.rdoc +5 -5
  87. data/rdoc/tensor.rdoc +4 -4
  88. data/rdoc/tut.rdoc +1 -1
  89. data/rdoc/use.rdoc +5 -5
  90. data/rdoc/vector.rdoc +29 -29
  91. data/rdoc/vector_complex.rdoc +6 -6
  92. data/rdoc/wavelet.rdoc +9 -9
  93. data/test/gsl/blas_test.rb +79 -0
  94. data/test/gsl/bspline_test.rb +63 -0
  95. data/test/gsl/cdf_test.rb +1512 -0
  96. data/test/gsl/cheb_test.rb +80 -0
  97. data/test/gsl/combination_test.rb +100 -0
  98. data/test/gsl/complex_test.rb +20 -0
  99. data/test/gsl/const_test.rb +29 -0
  100. data/test/gsl/deriv_test.rb +62 -0
  101. data/test/gsl/dht_test.rb +79 -0
  102. data/test/gsl/diff_test.rb +53 -0
  103. data/test/gsl/eigen_test.rb +563 -0
  104. data/test/gsl/err_test.rb +23 -0
  105. data/test/gsl/fit_test.rb +101 -0
  106. data/test/gsl/histo_test.rb +14 -0
  107. data/test/gsl/integration_test.rb +274 -0
  108. data/test/gsl/interp_test.rb +27 -0
  109. data/test/gsl/linalg_test.rb +463 -0
  110. data/test/gsl/matrix_nmf_test.rb +37 -0
  111. data/test/gsl/matrix_test.rb +77 -0
  112. data/test/gsl/min_test.rb +89 -0
  113. data/test/gsl/monte_test.rb +77 -0
  114. data/test/gsl/multifit_test.rb +753 -0
  115. data/test/gsl/multimin_test.rb +157 -0
  116. data/test/gsl/multiroot_test.rb +135 -0
  117. data/test/gsl/multiset_test.rb +52 -0
  118. data/test/gsl/odeiv_test.rb +275 -0
  119. data/test/gsl/poly_test.rb +338 -0
  120. data/test/gsl/qrng_test.rb +94 -0
  121. data/test/gsl/quartic_test.rb +28 -0
  122. data/test/gsl/randist_test.rb +122 -0
  123. data/test/gsl/rng_test.rb +303 -0
  124. data/test/gsl/roots_test.rb +78 -0
  125. data/test/gsl/sf_test.rb +2079 -0
  126. data/test/gsl/stats_test.rb +122 -0
  127. data/test/gsl/sum_test.rb +69 -0
  128. data/test/gsl/tensor_test.rb +396 -0
  129. data/test/gsl/vector_test.rb +223 -0
  130. data/test/gsl/wavelet_test.rb +130 -0
  131. data/test/gsl_test.rb +321 -0
  132. data/test/test_helper.rb +42 -0
  133. metadata +107 -150
  134. data/setup.rb +0 -1585
  135. data/tests/blas/amax.rb +0 -14
  136. data/tests/blas/asum.rb +0 -16
  137. data/tests/blas/axpy.rb +0 -25
  138. data/tests/blas/copy.rb +0 -23
  139. data/tests/blas/dot.rb +0 -23
  140. data/tests/bspline.rb +0 -53
  141. data/tests/cdf.rb +0 -1388
  142. data/tests/cheb.rb +0 -112
  143. data/tests/combination.rb +0 -123
  144. data/tests/complex.rb +0 -17
  145. data/tests/const.rb +0 -24
  146. data/tests/deriv.rb +0 -85
  147. data/tests/dht/dht1.rb +0 -17
  148. data/tests/dht/dht2.rb +0 -23
  149. data/tests/dht/dht3.rb +0 -23
  150. data/tests/dht/dht4.rb +0 -23
  151. data/tests/diff.rb +0 -78
  152. data/tests/eigen/eigen.rb +0 -220
  153. data/tests/eigen/gen.rb +0 -105
  154. data/tests/eigen/genherm.rb +0 -66
  155. data/tests/eigen/gensymm.rb +0 -68
  156. data/tests/eigen/nonsymm.rb +0 -53
  157. data/tests/eigen/nonsymmv.rb +0 -53
  158. data/tests/eigen/symm-herm.rb +0 -74
  159. data/tests/err.rb +0 -58
  160. data/tests/fit.rb +0 -124
  161. data/tests/gsl_test.rb +0 -118
  162. data/tests/gsl_test2.rb +0 -110
  163. data/tests/histo.rb +0 -12
  164. data/tests/integration/integration1.rb +0 -72
  165. data/tests/integration/integration2.rb +0 -71
  166. data/tests/integration/integration3.rb +0 -71
  167. data/tests/integration/integration4.rb +0 -71
  168. data/tests/interp.rb +0 -45
  169. data/tests/linalg/HH.rb +0 -64
  170. data/tests/linalg/LU.rb +0 -47
  171. data/tests/linalg/QR.rb +0 -77
  172. data/tests/linalg/SV.rb +0 -24
  173. data/tests/linalg/TDN.rb +0 -116
  174. data/tests/linalg/TDS.rb +0 -122
  175. data/tests/linalg/bidiag.rb +0 -73
  176. data/tests/linalg/cholesky.rb +0 -20
  177. data/tests/linalg/linalg.rb +0 -158
  178. data/tests/matrix/matrix_complex_test.rb +0 -36
  179. data/tests/matrix/matrix_nmf_test.rb +0 -39
  180. data/tests/matrix/matrix_test.rb +0 -48
  181. data/tests/min.rb +0 -99
  182. data/tests/monte/miser.rb +0 -31
  183. data/tests/monte/vegas.rb +0 -45
  184. data/tests/multifit/test_2dgauss.rb +0 -112
  185. data/tests/multifit/test_brown.rb +0 -90
  186. data/tests/multifit/test_enso.rb +0 -246
  187. data/tests/multifit/test_filip.rb +0 -155
  188. data/tests/multifit/test_gauss.rb +0 -97
  189. data/tests/multifit/test_longley.rb +0 -110
  190. data/tests/multifit/test_multifit.rb +0 -52
  191. data/tests/multimin.rb +0 -139
  192. data/tests/multiroot.rb +0 -131
  193. data/tests/multiset.rb +0 -52
  194. data/tests/narray/blas_dnrm2.rb +0 -20
  195. data/tests/odeiv.rb +0 -353
  196. data/tests/poly/poly.rb +0 -290
  197. data/tests/poly/special.rb +0 -65
  198. data/tests/qrng.rb +0 -131
  199. data/tests/quartic.rb +0 -29
  200. data/tests/randist.rb +0 -134
  201. data/tests/rng.rb +0 -305
  202. data/tests/roots.rb +0 -76
  203. data/tests/run-test.sh +0 -17
  204. data/tests/sf/gsl_test_sf.rb +0 -249
  205. data/tests/sf/test_airy.rb +0 -83
  206. data/tests/sf/test_bessel.rb +0 -306
  207. data/tests/sf/test_coulomb.rb +0 -17
  208. data/tests/sf/test_dilog.rb +0 -25
  209. data/tests/sf/test_gamma.rb +0 -209
  210. data/tests/sf/test_hyperg.rb +0 -356
  211. data/tests/sf/test_legendre.rb +0 -227
  212. data/tests/sf/test_mathieu.rb +0 -59
  213. data/tests/sf/test_mode.rb +0 -19
  214. data/tests/sf/test_sf.rb +0 -839
  215. data/tests/stats.rb +0 -174
  216. data/tests/stats_mt.rb +0 -16
  217. data/tests/sum.rb +0 -98
  218. data/tests/sys.rb +0 -323
  219. data/tests/tensor.rb +0 -419
  220. data/tests/vector/vector_complex_test.rb +0 -101
  221. data/tests/vector/vector_test.rb +0 -141
  222. data/tests/wavelet.rb +0 -142
@@ -0,0 +1,122 @@
1
+ require 'test_helper'
2
+
3
+ class RandistTest < GSL::TestCase
4
+
5
+ N = 100000
6
+ MULTI_DIM = 10
7
+
8
+ BINS = 100
9
+ STEPS = 100
10
+
11
+ R_GLOBAL = GSL::Rng.alloc
12
+
13
+ def test_shuffle
14
+ n = 10
15
+
16
+ count = GSL::Matrix.calloc(n, n)
17
+ x = GSL::Permutation.alloc(n)
18
+
19
+ N.times { |i|
20
+ n.times { |j| x[j] = j }
21
+ GSL::Ran.shuffle(R_GLOBAL, x)
22
+ n.times { |j| count.set(x[j], j, count[x[j], j] + 1) }
23
+ }
24
+
25
+ expected = N / 10.0
26
+
27
+ n.times { |i|
28
+ n.times { |j|
29
+ d = (count[i, j] - expected).abs
30
+
31
+ refute d > 1 && d / Math.sqrt(expected) > 5,
32
+ "gsl_ran_shuffle #{i},#{j} (#{count[i, j] / N} observed vs 0.1 expected)"
33
+ }
34
+ }
35
+ end
36
+
37
+ def _test_moments(name, arg, a, b, pp)
38
+ count, expected = 0, pp * N
39
+
40
+ N.times {
41
+ r = R_GLOBAL.send(*[name, arg].compact)
42
+ count += 1 if r < b && r > a
43
+ }
44
+
45
+ refute((count - expected).abs / Math.sqrt(expected) > 3,
46
+ "#{name}(#{arg}) [#{a},#{b}] (#{count.to_f / N} observed vs #{pp} expected)")
47
+ end
48
+
49
+ def _test_pdf(name, *args)
50
+ pdf = "#{name}_pdf"
51
+
52
+ a, b = -5.0, 5.0
53
+ dx = (b - a) / BINS
54
+
55
+ status = status_i = 0
56
+
57
+ count = GSL::Vector.calloc(BINS)
58
+ pp = GSL::Vector.calloc(BINS)
59
+
60
+ N.times { |i|
61
+ r = R_GLOBAL.send(name, *args)
62
+
63
+ if r < b && r > a
64
+ j = ((r - a) / dx).to_i
65
+ count[j] = count[j] + 1
66
+ end
67
+ }
68
+
69
+ BINS.times { |i|
70
+ x = a + i * dx
71
+ x = 0.0 if x.abs < 1e-10
72
+
73
+ sum = 0.0
74
+
75
+ STEPS.times { |j| sum += GSL::Ran.send(pdf, x + j * dx / STEPS, *args) }
76
+
77
+ pp[i] = 0.5 * (GSL::Ran.send(pdf, x, *args) + 2 * sum + GSL::Ran.send(pdf, x + dx - 1e-7, *args)) * dx / STEPS
78
+ }
79
+
80
+ BINS.times { |i|
81
+ x = a + i * dx
82
+ d = count[i] - N * pp[i]
83
+
84
+ status_i = (pp[i] == 0 ? count[i] != 0 : d < 1 && d / Math.sqrt(N * pp[i]) > 5) ? 1 : 0
85
+ status |= status_i
86
+
87
+ refute status_i == 1,
88
+ "#{name} [#{x},#{x + dx}) (#{count[i]}/#{N}=#{count.to_f / N} observed vs #{pp[i]} expected)"
89
+ }
90
+
91
+ assert status.zero?, "#{name}, sampling against pdf over range [#{a},#{b})"
92
+ end
93
+
94
+ def test_randist
95
+ _test_moments(:ugaussian, nil, 0.0, 100.0, 0.5)
96
+ _test_moments(:ugaussian, nil, -1.0, 1.0, 0.6826895)
97
+ _test_moments(:ugaussian, nil, 3.0, 3.5, 0.0011172689)
98
+ _test_moments(:ugaussian_tail, 3.0, 3.0, 3.5, 0.0011172689 / 0.0013498981)
99
+ _test_moments(:exponential, 2.0, 0.0, 1.0, 1 - Math.exp(-0.5))
100
+ _test_moments(:cauchy, 2.0, 0.0, 10000.0, 0.5)
101
+
102
+ _test_moments(:discrete, GSL::Ran::Discrete.alloc(GSL::Vector.alloc(0.59, 0.4, 0.01)), -0.5, 0.5, 0.59)
103
+ _test_moments(:discrete, GSL::Ran::Discrete.alloc(GSL::Vector.alloc(0.59, 0.4, 0.01)), 0.5, 1.5, 0.40)
104
+ _test_moments(:discrete, GSL::Ran::Discrete.alloc(GSL::Vector.alloc(0.59, 0.4, 0.01)), 1.5, 3.5, 0.01)
105
+
106
+ _test_moments(:discrete, GSL::Ran::Discrete.alloc(GSL::Vector.alloc(1, 9, 3, 4, 5, 8, 6, 7, 2, 0)), -0.5, 0.5, 1.0 / 45.0)
107
+ _test_moments(:discrete, GSL::Ran::Discrete.alloc(GSL::Vector.alloc(1, 9, 3, 4, 5, 8, 6, 7, 2, 0)), 8.5, 9.5, 0)
108
+
109
+ _test_pdf(:beta, 2.0, 3.0)
110
+ _test_pdf(:cauchy, 2.0)
111
+ _test_pdf(:chisq, 2.0)
112
+
113
+ _test_pdf(:exponential, 2.0)
114
+ _test_pdf(:exppow, 3.7, 0.3)
115
+ _test_pdf(:fdist, 3.0, 4.0)
116
+ _test_pdf(:flat, 3.0, 4.0)
117
+ _test_pdf(:gamma, 2.5, 2.17)
118
+ _test_pdf(:gaussian, 3.0)
119
+ _test_pdf(:ugaussian_tail, 0.1, 2.0)
120
+ end
121
+
122
+ end
@@ -0,0 +1,303 @@
1
+ require 'test_helper'
2
+
3
+ class RngTest < GSL::TestCase
4
+
5
+ N = 1000
6
+ N2 = 20000
7
+
8
+ BINS = 17
9
+ EXTRA = 10
10
+
11
+ {
12
+ 'rand' => [[1, 10000, 1910041713]],
13
+ 'randu' => [[1, 10000, 1623524161]],
14
+ 'cmrg' => [[1, 10000, 719452880]],
15
+ 'minstd' => [[1, 10000, 1043618065]],
16
+ 'mrg' => [[1, 10000, 2064828650]],
17
+ 'taus' => [[1, 10000, 2733957125]],
18
+ 'taus113' => [[1, 1000, 1925420673]],
19
+ 'transputer' => [[1, 10000, 1244127297]],
20
+ 'vax' => [[1, 10000, 3051034865]],
21
+
22
+ 'borosh13' => [[1, 10000, 2513433025]],
23
+ 'fishman18' => [[1, 10000, 330402013]],
24
+ 'fishman2x' => [[1, 10000, 540133597]],
25
+ 'knuthran2' => [[1, 10000, 1084477620]],
26
+ 'knuthran' => [[310952, 1009 * 2009 + 1, 461390032]],
27
+ 'lecuyer21' => [[1, 10000, 2006618587]],
28
+
29
+ 'waterman14' => [[1, 10000, 3776680385]],
30
+ 'coveyou' => [[6, 10000, 1416754246]],
31
+ 'fishman20' => [[6, 10000, 248127575]],
32
+ 'ranlux' => [[314159265, 10000, 12077992]],
33
+ 'ranlux389' => [[314159265, 10000, 165942]],
34
+ 'ranlxs0' => [[1, 10000, 11904320]],
35
+
36
+ 'ranlxs1' => [[1, 10000, 8734328]],
37
+ 'ranlxs2' => [[1, 10000, 6843140]],
38
+ 'ranlxd1' => [[1, 10000, 1998227290]],
39
+ 'ranlxd2' => [[1, 10000, 3949287736]],
40
+ 'slatec' => [[1, 10000, 45776]],
41
+ 'uni' => [[1, 10000, 9214]],
42
+ 'uni32' => [[1, 10000, 1155229825]],
43
+ 'zuf' => [[1, 10000, 3970]],
44
+
45
+ 'r250' => [[1, 10000, 1100653588]],
46
+ 'mt19937' => [[4357, 1000, 1186927261]],
47
+ 'mt19937_1999' => [[4357, 1000, 1030650439]],
48
+ 'mt19937_1998' => [[4357, 1000, 1309179303]],
49
+ 'tt800' => [[0, 10000, 2856609219]],
50
+
51
+ 'ran0' => [[0, 10000, 1115320064]],
52
+ 'ran1' => [[0, 10000, 1491066076]],
53
+ 'ran2' => [[0, 10000, 1701364455]],
54
+ 'ran3' => [[0, 10000, 186340785]],
55
+
56
+ 'ranmar' => [[1, 10000, 14428370]],
57
+
58
+ 'rand48' => [[0, 10000, 0xDE095043],
59
+ [1, 10000, 0xEDA54977]],
60
+
61
+ 'random_glibc2' => [[0, 10000, 1908609430]],
62
+ 'random8_glibc2' => [[0, 10000, 1910041713]],
63
+ 'random32_glibc2' => [[0, 10000, 1587395585]],
64
+ 'random64_glibc2' => [[0, 10000, 52848624]],
65
+ 'random128_glibc2' => [[0, 10000, 1908609430]],
66
+ 'random256_glibc2' => [[0, 10000, 179943260]],
67
+
68
+ 'random_bsd' => [[0, 10000, 1457025928]],
69
+ 'random8_bsd' => [[0, 10000, 1910041713]],
70
+ 'random32_bsd' => [[0, 10000, 1663114331]],
71
+ 'random64_bsd' => [[0, 10000, 864469165]],
72
+ 'random128_bsd' => [[0, 10000, 1457025928]],
73
+ 'random256_bsd' => [[0, 10000, 1216357476]],
74
+
75
+ 'random_libc5' => [[0, 10000, 428084942]],
76
+ 'random8_libc5' => [[0, 10000, 1910041713]],
77
+ 'random32_libc5' => [[0, 10000, 1967452027]],
78
+ 'random64_libc5' => [[0, 10000, 2106639801]],
79
+ 'random128_libc5' => [[0, 10000, 428084942]],
80
+ 'random256_libc5' => [[0, 10000, 116367984]],
81
+
82
+ 'ranf' => [[0, 10000, 2152890433],
83
+ [2, 10000, 339327233]]
84
+ }.each { |type, args|
85
+ args.each_with_index { |(seed, n, result), i|
86
+ define_method("test_#{type}_#{i}") {
87
+ r, k = GSL::Rng.alloc(type), nil
88
+ r.set(seed) if seed != 0
89
+
90
+ n.times { k = r.get }
91
+
92
+ assert k == result, "#{r.name}, #{n} steps (#{k} observed vs #{result} expected)"
93
+ }
94
+ }
95
+ }
96
+
97
+ GSL::Rng.types.each { |type|
98
+ define_method("test_float_#{type}") { _rng_float_test(type) }
99
+ define_method("test_state_#{type}") { _rng_state_test(type) }
100
+ define_method("test_parallel_state_#{type}") { _rng_parallel_state_test(type) }
101
+ define_method("test_read_write_#{type}") { _rng_read_write_test(type) }
102
+ define_method("test_generic_#{type}") { _generic_rng_test(type) }
103
+ }
104
+
105
+ def _rng_float_test(type)
106
+ ri = GSL::Rng.alloc(type)
107
+ rf = GSL::Rng.alloc(type)
108
+
109
+ status = k = 0
110
+
111
+ begin
112
+ k = ri.get
113
+ u = rf.get
114
+ end while k == 0
115
+
116
+ c = k / u
117
+
118
+ N2.times {
119
+ k = ri.get
120
+ u = rf.get
121
+
122
+ if c * k != u
123
+ status = 1
124
+ break
125
+ end
126
+ }
127
+
128
+ assert status.zero?, "#{ri.name}, ratio of int to double (#{c} observed vs #{k/u} expected)"
129
+ end
130
+
131
+ def _rng_state_test(type)
132
+ r = GSL::Rng.alloc(type)
133
+ r_save = GSL::Rng.alloc(type)
134
+
135
+ N.times { r.get }
136
+
137
+ GSL::Rng.memcpy(r_save, r)
138
+
139
+ test_a = GSL::Vector.alloc(N)
140
+ test_b = GSL::Vector.alloc(N)
141
+
142
+ N.times { |i| test_a[i] = r.get }
143
+
144
+ GSL::Rng.memcpy(r, r_save)
145
+ N.times { |i| test_b[i] = r.get }
146
+
147
+ assert((0...N).all? { |i| test_b[i] == test_a[i] }, "#{r.name}, random number state consistency")
148
+ end
149
+
150
+ def _rng_parallel_state_test(type)
151
+ r1 = GSL::Rng.alloc(type)
152
+ r2 = GSL::Rng.alloc(type)
153
+
154
+ test_a = GSL::Vector.alloc(N)
155
+ test_b = GSL::Vector.alloc(N)
156
+ test_c = GSL::Vector.alloc(N)
157
+ test_d = GSL::Vector.alloc(N)
158
+ test_e = GSL::Vector.alloc(N)
159
+ test_f = GSL::Vector.alloc(N)
160
+
161
+ N.times { r1.get }
162
+
163
+ GSL::Rng.memcpy(r2, r1)
164
+
165
+ N.times { |i|
166
+ test_a[i] = r1.get
167
+ test_b[i] = r2.get
168
+ test_c[i] = r1.uniform_int(1234)
169
+ test_d[i] = r2.uniform_int(1234)
170
+ test_e[i] = r1.uniform
171
+ test_f[i] = r2.uniform
172
+ }
173
+
174
+ assert((0...N).all? { |i| test_a[i] == test_b[i] }, "#{r1.name}, parallel random number state consistency (a/b)")
175
+ assert((0...N).all? { |i| test_c[i] == test_d[i] }, "#{r1.name}, parallel random number state consistency (c/d)")
176
+ assert((0...N).all? { |i| test_e[i] == test_f[i] }, "#{r1.name}, parallel random number state consistency (e/f)")
177
+ end
178
+
179
+ def _rng_read_write_test(type)
180
+ r = GSL::Rng.alloc(type)
181
+
182
+ test_a = GSL::Vector.alloc(N)
183
+ test_b = GSL::Vector.alloc(N)
184
+
185
+ N.times { r.get }
186
+
187
+ r.fwrite('test.dat')
188
+ N.times { |i| test_a[i] = r.get }
189
+
190
+ r.fread('test.dat')
191
+ N.times { |i| test_b[i] = r.get }
192
+
193
+ assert((0...N).all? { |i| test_b[i] == test_a[i] }, "#{r.name}, random number generator read and write")
194
+ ensure
195
+ File.delete('test.dat') if FileTest.exist?('test.dat')
196
+ end
197
+
198
+ def _generic_rng_test(type)
199
+ r = GSL::Rng.alloc(type)
200
+
201
+ name, ran_max, ran_min = r.name, r.max, r.min
202
+ kmax, kmin, sigma = 0, 1000, 0.0
203
+
204
+ kmax, status = _rng_max_test(r, ran_max)
205
+ assert status.zero?, "#{name}, observed vs theoretical maximum (#{kmax} vs #{ran_max})"
206
+
207
+ kmin, status = _rng_min_test(r, ran_min, ran_max)
208
+ assert status.zero?, "#{name}, observed vs theoretical minimum (#{kmin} vs #{ran_min})"
209
+
210
+ sigma, status = _rng_sum_test(r)
211
+ assert status.zero?, "#{r.name}, sum test within acceptable sigma (observed #{sigma} sigma)"
212
+
213
+ sigma, status = _rng_bin_test(r)
214
+ assert status.zero?, "#{r.name}, bin test within acceptable chisq (observed #{sigma} sigma)"
215
+
216
+ r.set(1)
217
+ kmax, status = _rng_max_test(r, ran_max)
218
+
219
+ r.set(1)
220
+ kmin, s = _rng_min_test(r, ran_min, ran_max)
221
+ status |= s
222
+
223
+ r.set(1)
224
+ sigma, s = _rng_sum_test(r)
225
+ status |= s
226
+
227
+ r.set(12345)
228
+ kmax, s = _rng_max_test(r, ran_max)
229
+ status |= s
230
+
231
+ r.set(12345)
232
+ kmin, s = _rng_min_test(r, ran_min, ran_max)
233
+ status |= s
234
+
235
+ r.set(12345)
236
+ sigma, s = _rng_sum_test(r)
237
+ status |= s
238
+
239
+ assert status.zero?, "#{r.name}, maximum and sum tests for non-default seeds"
240
+ end
241
+
242
+ def _rng_max_test(r, ran_max)
243
+ max = 0
244
+
245
+ N2.times {
246
+ k = r.get
247
+ max = k if k > max
248
+ }
249
+
250
+ actual_uncovered = ran_max - max
251
+ expect_uncovered = ran_max.to_f / N2.to_f
252
+
253
+ [max, max > ran_max || actual_uncovered > 7 * expect_uncovered ? 1 : 0]
254
+ end
255
+
256
+ def _rng_min_test(r, ran_min, ran_max)
257
+ min = 1000000000
258
+
259
+ N2.times {
260
+ k = r.get
261
+ min = k if k < min
262
+ }
263
+
264
+ actual_uncovered = min - ran_min
265
+ expect_uncovered = ran_max.to_f / N2.to_f
266
+
267
+ [min, min < ran_min || actual_uncovered > 7 * expect_uncovered ? 1 : 0]
268
+ end
269
+
270
+ def _rng_sum_test(r)
271
+ sum = 0.0
272
+
273
+ N2.times {
274
+ x = r.uniform - 0.5
275
+ sum += x
276
+ }
277
+
278
+ sum /= N2.to_f
279
+ sigma = sum * Math.sqrt(12.0 * N2)
280
+
281
+ [sigma, sigma.abs > 3 || sigma.abs < 0.003 ? 1 : 0]
282
+ end
283
+
284
+ def _rng_bin_test(r)
285
+ count = GSL::Vector.calloc(BINS + EXTRA)
286
+ chisq = 0.0
287
+
288
+ N2.times { count[r.uniform_int(BINS)] += 1 }
289
+
290
+ BINS.times { |i|
291
+ x = N2.to_f / BINS
292
+ d = count[i] - x
293
+ chisq += d * d / x
294
+ }
295
+
296
+ BINS.upto(EXTRA - 1) { |i|
297
+ assert count[i].zero?, "#{r.name}, wrote outside range in bin test (#{i} observed vs #{BINS - 1} expected)"
298
+ }
299
+
300
+ [sigma = Math.sqrt(chisq / BINS), sigma.abs > 3 || sigma.abs < 0.003 ? 1 : 0]
301
+ end
302
+
303
+ end
@@ -0,0 +1,78 @@
1
+ require 'test_helper'
2
+
3
+ class RootsTest < GSL::TestCase
4
+
5
+ EPSREL = 10.0 * GSL::DBL_EPSILON
6
+ EPSABS = 10.0 * GSL::DBL_EPSILON
7
+
8
+ MAX_ITERATIONS = 150
9
+
10
+ def _test_f(type, desc, f, lower, upper, correct)
11
+ s = GSL::Root::FSolver.alloc(type)
12
+ s.set(f, lower, upper)
13
+
14
+ status = iter = 0
15
+
16
+ begin
17
+ iter += 1
18
+ s.iterate
19
+
20
+ r = s.root
21
+ a = s.x_lower
22
+ b = s.x_upper
23
+
24
+ refute a > b, "interval is invalid (#{a},#{b})"
25
+ refute r < a || r > b, "r lies outside interval #{r} (#{a},#{b})"
26
+
27
+ status = GSL::Root.test_interval(a, b, EPSABS, EPSREL)
28
+ end while status == GSL::CONTINUE && iter < MAX_ITERATIONS
29
+
30
+ assert status.zero?, "#{s.name}, #{desc} (#{s.root} obs vs #{correct} expected)"
31
+ assert iter <= MAX_ITERATIONS, 'exceeded maximum number of iterations'
32
+
33
+ assert_tol r, correct, "incorrect precision (#{r} obs vs #{correct} expected)"
34
+ end
35
+
36
+ def _test_fdf(type, desc, fdf, root, correct)
37
+ s = GSL::Root::FdfSolver.alloc(type)
38
+ s.set(fdf, root)
39
+
40
+ status = iter = 0
41
+
42
+ begin
43
+ iter += 1
44
+ prev = s.root
45
+
46
+ begin
47
+ s.iterate
48
+ rescue GSL::ERROR::EBADFUNC
49
+ raise unless type == 'secant'
50
+ end
51
+
52
+ status = GSL::Root.test_delta(s.root, prev, EPSABS, EPSREL)
53
+ end while status == GSL::CONTINUE && iter < MAX_ITERATIONS
54
+
55
+ assert status.zero?, "#{s.name} #{desc} (#{s.root} obs vs #{correct} expected)"
56
+ assert iter <= MAX_ITERATIONS, 'exceeded maximum number of iterations'
57
+
58
+ assert_tol r = s.root, correct, "incorrect precision (#{r} obs vs #{correct} expected)"
59
+ end
60
+
61
+ def setup
62
+ @func = GSL::Function.alloc { |x| GSL.pow(x, 20.0) - 1 }
63
+ @fdf = GSL::Function_fdf.alloc(@func.f, lambda { |x| 20.0 * GSL.pow(x, 19.0) })
64
+ end
65
+
66
+ %w[bisection brent falsepos].each { |type|
67
+ define_method("test_f_#{type}") {
68
+ _test_f(type, 'x^20 - 1 [0.1, 2]', @func, 0.1, 2.0, 1.0)
69
+ }
70
+ }
71
+
72
+ %w[newton secant steffenson].each { |type|
73
+ define_method("test_fdf_#{type}") {
74
+ _test_fdf(type, 'x^{20} - 1 {0.9}', @fdf, 0.9, 1.0)
75
+ }
76
+ }
77
+
78
+ end