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.
- checksums.yaml +4 -4
- data/AUTHORS +2 -2
- data/ChangeLog +8 -0
- data/Rakefile +39 -96
- data/VERSION +1 -1
- data/ext/array.c +2 -2
- data/ext/block_source.c +1 -1
- data/ext/dirac.c +1 -1
- data/ext/eigen.c +13 -13
- data/ext/extconf.rb +17 -11
- data/ext/function.c +5 -5
- data/ext/gsl_narray.c +6 -6
- data/ext/histogram.c +7 -6
- data/ext/histogram2d.c +4 -4
- data/ext/interp.c +1 -1
- data/ext/linalg.c +13 -11
- data/ext/linalg_complex.c +8 -6
- data/ext/math.c +1 -1
- data/ext/matrix_complex.c +6 -6
- data/ext/matrix_source.c +10 -10
- data/ext/monte.c +2 -2
- data/ext/multimin.c +4 -4
- data/ext/multiroots.c +8 -8
- data/ext/nmf.c +6 -6
- data/ext/ntuple.c +4 -4
- data/ext/odeiv.c +2 -2
- data/ext/permutation.c +1 -1
- data/ext/poly2.c +6 -6
- data/ext/poly_source.c +9 -6
- data/ext/sf.c +31 -45
- data/ext/signal.c +2 -2
- data/ext/vector_complex.c +11 -10
- data/ext/vector_double.c +7 -4
- data/ext/vector_source.c +29 -26
- data/ext/wavelet.c +1 -1
- data/include/rb_gsl_common.h +12 -0
- data/include/rb_gsl_config.h +10 -1
- data/rdoc/blas.rdoc +4 -4
- data/rdoc/bspline.rdoc +8 -8
- data/rdoc/cheb.rdoc +9 -9
- data/rdoc/cholesky_complex.rdoc +1 -1
- data/rdoc/combi.rdoc +10 -10
- data/rdoc/complex.rdoc +12 -12
- data/rdoc/const.rdoc +21 -21
- data/rdoc/dht.rdoc +7 -7
- data/rdoc/diff.rdoc +7 -7
- data/rdoc/ehandling.rdoc +4 -4
- data/rdoc/eigen.rdoc +12 -12
- data/rdoc/fft.rdoc +27 -27
- data/rdoc/fit.rdoc +19 -19
- data/rdoc/function.rdoc +1 -1
- data/rdoc/graph.rdoc +3 -3
- data/rdoc/hist.rdoc +17 -17
- data/rdoc/hist2d.rdoc +5 -5
- data/rdoc/hist3d.rdoc +4 -4
- data/rdoc/index.rdoc +4 -4
- data/rdoc/integration.rdoc +17 -17
- data/rdoc/interp.rdoc +12 -12
- data/rdoc/intro.rdoc +4 -4
- data/rdoc/linalg.rdoc +21 -21
- data/rdoc/linalg_complex.rdoc +1 -1
- data/rdoc/math.rdoc +14 -14
- data/rdoc/matrix.rdoc +21 -21
- data/rdoc/min.rdoc +10 -10
- data/rdoc/monte.rdoc +4 -4
- data/rdoc/multimin.rdoc +13 -13
- data/rdoc/multiroot.rdoc +13 -13
- data/rdoc/narray.rdoc +10 -10
- data/rdoc/ndlinear.rdoc +5 -5
- data/rdoc/nonlinearfit.rdoc +18 -18
- data/rdoc/ntuple.rdoc +6 -6
- data/rdoc/odeiv.rdoc +13 -13
- data/rdoc/perm.rdoc +12 -12
- data/rdoc/poly.rdoc +18 -18
- data/rdoc/qrng.rdoc +10 -10
- data/rdoc/randist.rdoc +11 -11
- data/rdoc/ref.rdoc +50 -50
- data/rdoc/rng.rdoc +10 -10
- data/rdoc/rngextra.rdoc +5 -5
- data/rdoc/roots.rdoc +13 -13
- data/rdoc/sf.rdoc +36 -36
- data/rdoc/siman.rdoc +4 -4
- data/rdoc/sort.rdoc +7 -7
- data/rdoc/start.rdoc +1 -1
- data/rdoc/stats.rdoc +14 -14
- data/rdoc/sum.rdoc +5 -5
- data/rdoc/tensor.rdoc +4 -4
- data/rdoc/tut.rdoc +1 -1
- data/rdoc/use.rdoc +5 -5
- data/rdoc/vector.rdoc +29 -29
- data/rdoc/vector_complex.rdoc +6 -6
- data/rdoc/wavelet.rdoc +9 -9
- data/test/gsl/blas_test.rb +79 -0
- data/test/gsl/bspline_test.rb +63 -0
- data/test/gsl/cdf_test.rb +1512 -0
- data/test/gsl/cheb_test.rb +80 -0
- data/test/gsl/combination_test.rb +100 -0
- data/test/gsl/complex_test.rb +20 -0
- data/test/gsl/const_test.rb +29 -0
- data/test/gsl/deriv_test.rb +62 -0
- data/test/gsl/dht_test.rb +79 -0
- data/test/gsl/diff_test.rb +53 -0
- data/test/gsl/eigen_test.rb +563 -0
- data/test/gsl/err_test.rb +23 -0
- data/test/gsl/fit_test.rb +101 -0
- data/test/gsl/histo_test.rb +14 -0
- data/test/gsl/integration_test.rb +274 -0
- data/test/gsl/interp_test.rb +27 -0
- data/test/gsl/linalg_test.rb +463 -0
- data/test/gsl/matrix_nmf_test.rb +37 -0
- data/test/gsl/matrix_test.rb +77 -0
- data/test/gsl/min_test.rb +89 -0
- data/test/gsl/monte_test.rb +77 -0
- data/test/gsl/multifit_test.rb +753 -0
- data/test/gsl/multimin_test.rb +157 -0
- data/test/gsl/multiroot_test.rb +135 -0
- data/test/gsl/multiset_test.rb +52 -0
- data/test/gsl/odeiv_test.rb +275 -0
- data/test/gsl/poly_test.rb +338 -0
- data/test/gsl/qrng_test.rb +94 -0
- data/test/gsl/quartic_test.rb +28 -0
- data/test/gsl/randist_test.rb +122 -0
- data/test/gsl/rng_test.rb +303 -0
- data/test/gsl/roots_test.rb +78 -0
- data/test/gsl/sf_test.rb +2079 -0
- data/test/gsl/stats_test.rb +122 -0
- data/test/gsl/sum_test.rb +69 -0
- data/test/gsl/tensor_test.rb +396 -0
- data/test/gsl/vector_test.rb +223 -0
- data/test/gsl/wavelet_test.rb +130 -0
- data/test/gsl_test.rb +321 -0
- data/test/test_helper.rb +42 -0
- metadata +107 -150
- data/setup.rb +0 -1585
- data/tests/blas/amax.rb +0 -14
- data/tests/blas/asum.rb +0 -16
- data/tests/blas/axpy.rb +0 -25
- data/tests/blas/copy.rb +0 -23
- data/tests/blas/dot.rb +0 -23
- data/tests/bspline.rb +0 -53
- data/tests/cdf.rb +0 -1388
- data/tests/cheb.rb +0 -112
- data/tests/combination.rb +0 -123
- data/tests/complex.rb +0 -17
- data/tests/const.rb +0 -24
- data/tests/deriv.rb +0 -85
- data/tests/dht/dht1.rb +0 -17
- data/tests/dht/dht2.rb +0 -23
- data/tests/dht/dht3.rb +0 -23
- data/tests/dht/dht4.rb +0 -23
- data/tests/diff.rb +0 -78
- data/tests/eigen/eigen.rb +0 -220
- data/tests/eigen/gen.rb +0 -105
- data/tests/eigen/genherm.rb +0 -66
- data/tests/eigen/gensymm.rb +0 -68
- data/tests/eigen/nonsymm.rb +0 -53
- data/tests/eigen/nonsymmv.rb +0 -53
- data/tests/eigen/symm-herm.rb +0 -74
- data/tests/err.rb +0 -58
- data/tests/fit.rb +0 -124
- data/tests/gsl_test.rb +0 -118
- data/tests/gsl_test2.rb +0 -110
- data/tests/histo.rb +0 -12
- data/tests/integration/integration1.rb +0 -72
- data/tests/integration/integration2.rb +0 -71
- data/tests/integration/integration3.rb +0 -71
- data/tests/integration/integration4.rb +0 -71
- data/tests/interp.rb +0 -45
- data/tests/linalg/HH.rb +0 -64
- data/tests/linalg/LU.rb +0 -47
- data/tests/linalg/QR.rb +0 -77
- data/tests/linalg/SV.rb +0 -24
- data/tests/linalg/TDN.rb +0 -116
- data/tests/linalg/TDS.rb +0 -122
- data/tests/linalg/bidiag.rb +0 -73
- data/tests/linalg/cholesky.rb +0 -20
- data/tests/linalg/linalg.rb +0 -158
- data/tests/matrix/matrix_complex_test.rb +0 -36
- data/tests/matrix/matrix_nmf_test.rb +0 -39
- data/tests/matrix/matrix_test.rb +0 -48
- data/tests/min.rb +0 -99
- data/tests/monte/miser.rb +0 -31
- data/tests/monte/vegas.rb +0 -45
- data/tests/multifit/test_2dgauss.rb +0 -112
- data/tests/multifit/test_brown.rb +0 -90
- data/tests/multifit/test_enso.rb +0 -246
- data/tests/multifit/test_filip.rb +0 -155
- data/tests/multifit/test_gauss.rb +0 -97
- data/tests/multifit/test_longley.rb +0 -110
- data/tests/multifit/test_multifit.rb +0 -52
- data/tests/multimin.rb +0 -139
- data/tests/multiroot.rb +0 -131
- data/tests/multiset.rb +0 -52
- data/tests/narray/blas_dnrm2.rb +0 -20
- data/tests/odeiv.rb +0 -353
- data/tests/poly/poly.rb +0 -290
- data/tests/poly/special.rb +0 -65
- data/tests/qrng.rb +0 -131
- data/tests/quartic.rb +0 -29
- data/tests/randist.rb +0 -134
- data/tests/rng.rb +0 -305
- data/tests/roots.rb +0 -76
- data/tests/run-test.sh +0 -17
- data/tests/sf/gsl_test_sf.rb +0 -249
- data/tests/sf/test_airy.rb +0 -83
- data/tests/sf/test_bessel.rb +0 -306
- data/tests/sf/test_coulomb.rb +0 -17
- data/tests/sf/test_dilog.rb +0 -25
- data/tests/sf/test_gamma.rb +0 -209
- data/tests/sf/test_hyperg.rb +0 -356
- data/tests/sf/test_legendre.rb +0 -227
- data/tests/sf/test_mathieu.rb +0 -59
- data/tests/sf/test_mode.rb +0 -19
- data/tests/sf/test_sf.rb +0 -839
- data/tests/stats.rb +0 -174
- data/tests/stats_mt.rb +0 -16
- data/tests/sum.rb +0 -98
- data/tests/sys.rb +0 -323
- data/tests/tensor.rb +0 -419
- data/tests/vector/vector_complex_test.rb +0 -101
- data/tests/vector/vector_test.rb +0 -141
- 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
|