gsl 1.16.0.6 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +5 -13
- data/ChangeLog +6 -0
- data/Gemfile +1 -0
- data/README.md +77 -1
- data/Rakefile +7 -1
- data/examples/linalg/QR_solve_narray.rb +2 -1
- data/examples/linalg/SV_narray.rb +10 -0
- data/examples/linalg/chol_narray.rb +3 -0
- data/examples/wavelet/wavelet1.rb +4 -2
- data/ext/gsl_native/array.c +10 -0
- data/ext/gsl_native/bspline.c +15 -2
- data/ext/gsl_native/cheb.c +76 -10
- data/ext/gsl_native/common.c +33 -0
- data/ext/gsl_native/dht.c +26 -0
- data/ext/gsl_native/eigen.c +129 -0
- data/ext/gsl_native/extconf.rb +60 -6
- data/ext/gsl_native/fft.c +42 -0
- data/ext/gsl_native/function.c +18 -0
- data/ext/gsl_native/gsl.c +5 -0
- data/ext/gsl_native/gsl_nmatrix.c +204 -0
- data/ext/gsl_native/histogram.c +42 -0
- data/ext/gsl_native/include/rb_gsl.h +4 -0
- data/ext/gsl_native/include/rb_gsl_array.h +4 -0
- data/ext/gsl_native/include/rb_gsl_common.h +5 -0
- data/ext/gsl_native/include/rb_gsl_interp.h +4 -0
- data/ext/gsl_native/include/rb_gsl_with_nmatrix.h +28 -0
- data/ext/gsl_native/interp.c +14 -0
- data/ext/gsl_native/linalg.c +414 -22
- data/ext/gsl_native/math.c +64 -0
- data/ext/gsl_native/multifit.c +61 -0
- data/ext/gsl_native/randist.c +42 -0
- data/ext/gsl_native/sf.c +6 -0
- data/ext/gsl_native/sf_ellint.c +4 -4
- data/ext/gsl_native/sf_mathieu.c +48 -0
- data/ext/gsl_native/spline.c +30 -0
- data/ext/gsl_native/tamu_anova.c +1 -1
- data/gsl.gemspec +8 -1
- data/lib/gsl.rb +5 -0
- data/lib/gsl/version.rb +1 -1
- data/rdoc/nmatrix.rdoc +129 -0
- data/rdoc/ref.rdoc +1 -0
- data/test.sh +21 -0
- data/test/gsl/bspline_test.rb +15 -14
- data/test/gsl/combination_test.rb +17 -6
- data/test/gsl/dht_test.rb +42 -33
- data/test/gsl/nmatrix_tests/nmatrix_cheb_test.rb +34 -0
- data/test/gsl/nmatrix_tests/nmatrix_eigen_test.rb +28 -0
- data/test/gsl/nmatrix_tests/nmatrix_gsl_test.rb +65 -0
- data/test/gsl/nmatrix_tests/nmatrix_interp_test.rb +45 -0
- data/test/gsl/nmatrix_tests/nmatrix_linalg_test.rb +100 -0
- data/test/gsl/nmatrix_tests/nmatrix_stats_test.rb +88 -0
- data/test/gsl/nmatrix_tests/nmatrix_wavelet_test.rb +5 -0
- data/test/gsl/randist_test.rb +43 -23
- data/test/test_helper.rb +30 -2
- metadata +191 -51
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NMatrixGslTest < GSL::TestCase
|
4
|
+
def setup
|
5
|
+
@gsl_vector = GSL::Vector.alloc(2.354, 4.443, 6.776)
|
6
|
+
@nm_vector = NMatrix.new([3], [2.354, 4.443, 6.776], dtype: :float64)
|
7
|
+
|
8
|
+
@gsl_int_vector = GSL::Vector::Int[1,2,3,4,5]
|
9
|
+
@nm_int_vector = NMatrix.new([5], [1,2,3,4,5], dtype: :int32)
|
10
|
+
|
11
|
+
@gsl_complex_vector = GSL::Vector::Complex.alloc([[1,0], [2,0], [3,0]])
|
12
|
+
@nm_complex_vector = NMatrix.new([3], [1,2,3], dtype: :complex128)
|
13
|
+
|
14
|
+
@gsl_matrix = GSL::Matrix.alloc(
|
15
|
+
[1,2,3],
|
16
|
+
[4,5,6],
|
17
|
+
[7,8,9]
|
18
|
+
)
|
19
|
+
@nm_matrix = NMatrix.new([3,3], [1,2,3,4,5,6,7,8,9], dtype: :float64)
|
20
|
+
|
21
|
+
@gsl_int_matrix = GSL::Matrix::Int.alloc(
|
22
|
+
[1,2,3],
|
23
|
+
[4,5,6],
|
24
|
+
[7,8,9]
|
25
|
+
)
|
26
|
+
@nm_int_matrix = NMatrix.new([3,3], [1,2,3,4,5,6,7,8,9], dtype: :int32)
|
27
|
+
|
28
|
+
@gsl_complex_matrix = GSL::Matrix::Complex.alloc(2,2)
|
29
|
+
@gsl_complex_matrix.set(0,0, [1.1,1.1])
|
30
|
+
@gsl_complex_matrix.set(0,1, [2.2,2.2])
|
31
|
+
@gsl_complex_matrix.set(1,0, [3.3,3.3])
|
32
|
+
@gsl_complex_matrix.set(1,1, [4.4,4.4])
|
33
|
+
|
34
|
+
@nm_complex_matrix = NMatrix.new([2,2],
|
35
|
+
[Complex(1.1,1.1), Complex(2.2,2.2), Complex(3.3,3.3), Complex(4.4,4.4)], dtype: :complex128)
|
36
|
+
end
|
37
|
+
|
38
|
+
# GSL::Vector to 1D NMatrix
|
39
|
+
def test_gsl_vector_to_nmatrix
|
40
|
+
assert_equal @nm_vector , @gsl_vector.to_nm , 'floating point GSL::Vector to NMatrix'
|
41
|
+
assert_equal @nm_int_vector , @gsl_int_vector.to_nm , 'integer GSL::Vector to NMatrix'
|
42
|
+
assert_equal @nm_complex_vector, @gsl_complex_vector.to_nm, 'complex GSL::Vector to NMatrix'
|
43
|
+
end
|
44
|
+
|
45
|
+
# GSL::Matrix to NMatrix
|
46
|
+
def test_gsl_matrix_to_nmatrix
|
47
|
+
assert_equal @nm_matrix, @gsl_matrix.to_nm, 'floating point GSL::Matrix to 2D NMatrix'
|
48
|
+
assert_equal @nm_int_matrix, @gsl_int_matrix.to_nm, 'GSL::Matrix::Int to 2D NMatrix'
|
49
|
+
assert_equal @nm_complex_matrix, @gsl_complex_matrix.to_nm, 'GSL::Matrix::Complex to 2D NMatrix'
|
50
|
+
end
|
51
|
+
|
52
|
+
# NMatrix to GSL::Vector
|
53
|
+
def test_nmatrix_to_gsl_vector
|
54
|
+
assert_equal @gsl_vector , @nm_vector.to_gslv , 'floating point NMatrix to GSL::Vector'
|
55
|
+
assert_equal @gsl_int_vector , @nm_int_vector.to_gslv , 'int NMatrix to GSL::Vector::Int'
|
56
|
+
assert_equal @gsl_complex_vector, @nm_complex_vector.to_gslv, 'complex NMatrix to GSL::Vector::Complex'
|
57
|
+
end
|
58
|
+
|
59
|
+
# NMatrix to GSL::Matrix
|
60
|
+
def test_nmatrix_to_gsl_matrix
|
61
|
+
assert_equal @gsl_matrix , @nm_matrix.to_gslm , 'floating NMatrix to GSL::Matrix'
|
62
|
+
assert_equal @gsl_int_matrix , @nm_int_matrix.to_gslm , 'int NMatrix to GSL::Matrix::Int'
|
63
|
+
assert_equal @gsl_complex_matrix, @nm_complex_matrix.to_gslm, 'complex NMatrix to GSL::Matrix::Complex'
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NMatrixInterpTest < GSL::TestCase
|
4
|
+
def test_interp_init_eval
|
5
|
+
n = 10
|
6
|
+
x = NMatrix.new([n], dtype: :float64)
|
7
|
+
y = x.clone_structure
|
8
|
+
|
9
|
+
0.upto(n-1) do |i|
|
10
|
+
a = i.to_f
|
11
|
+
x[i] = i + 0.5*Math::sin(a)
|
12
|
+
y[i] = i + Math::cos(a*a)
|
13
|
+
end
|
14
|
+
|
15
|
+
interp = GSL::Interp.alloc("akima", n)
|
16
|
+
interp.init(x, y)
|
17
|
+
|
18
|
+
yi = []
|
19
|
+
xi = x[0]
|
20
|
+
r = []
|
21
|
+
while xi < x[9]
|
22
|
+
r << xi
|
23
|
+
xi += 0.01
|
24
|
+
end
|
25
|
+
|
26
|
+
yi = interp.eval(x,y,NMatrix.new([r.size], r, dtype: :float64))
|
27
|
+
|
28
|
+
assert_rel yi[1] , 1.0066, 0.001, 'yi[1]'
|
29
|
+
assert_rel yi.to_a.last, 9.7618, 0.001, 'yi.last'
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_spline_init_eval
|
33
|
+
n = 10
|
34
|
+
x = NMatrix.new([n], (1..10).to_a, dtype: :float64)
|
35
|
+
y = x.dup
|
36
|
+
|
37
|
+
spline = GSL::Spline.alloc(x, y)
|
38
|
+
|
39
|
+
xi = NMatrix.new([n], (1..9).map { |a| a += 0.5 }, dtype: :float64)
|
40
|
+
yi = spline.eval(xi)
|
41
|
+
|
42
|
+
assert yi.class == NMatrix
|
43
|
+
assert_rel yi[0], 1.5, 0.0001, 'yi[0]'
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NMatrixLinalgTest < GSL::TestCase
|
4
|
+
def setup
|
5
|
+
@nm = NMatrix.new([4,4],
|
6
|
+
[
|
7
|
+
0.18, 0.60, 0.57, 0.96,
|
8
|
+
0.41, 0.24, 0.99, 0.58,
|
9
|
+
0.14, 0.30, 0.97, 0.66,
|
10
|
+
0.51, 0.13, 0.19, 0.85
|
11
|
+
], dtype: :float64)
|
12
|
+
@b = NMatrix.new([4], [1,2,3,4], dtype: :float64)
|
13
|
+
@x_exp = NMatrix.new([4], [-4.05205022957397, -12.6056113959069, 1.66091162670884, 8.69376692879523], dtype: :float64)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_lu
|
17
|
+
lu, perm, signum = GSL::Linalg::LU.decomp(@nm)
|
18
|
+
|
19
|
+
lu_exp = NMatrix.new([4,4],
|
20
|
+
[0.51, 0.13, 0.19, 0.85,
|
21
|
+
0.352941176470588, 0.554117647058823, 0.502941176470588, 0.66,
|
22
|
+
0.803921568627451, 0.244515215852796, 0.71427813163482, -0.264713375796178,
|
23
|
+
0.274509803921569, 0.476999292285916, 0.949126848480345, 0.363093705877982],
|
24
|
+
dtype: :float64)
|
25
|
+
|
26
|
+
assert_enum_abs lu, lu_exp, 0.0001, "GSL::Linalg::LU.decomp(A) with NMatrix"
|
27
|
+
assert_enum_abs GSL::Linalg::LU.solve(lu, perm, @b), @x_exp, 0.0001, "GSL::Linalg::LU.solve(lu, perm, b) with NMatrix"
|
28
|
+
|
29
|
+
##########################################################################
|
30
|
+
|
31
|
+
nm = NMatrix.new([2,2], [1,1,14,1], dtype: :float64)
|
32
|
+
lu, perm, sign = GSL::Linalg::LU.decomp(nm)
|
33
|
+
inverted = NMatrix.new([2,2],
|
34
|
+
[
|
35
|
+
-0.076923, 0.076923,
|
36
|
+
1.076923, -0.076923
|
37
|
+
], dtype: :float64)
|
38
|
+
|
39
|
+
assert_enum_abs GSL::Linalg::LU.invert(lu, perm), inverted, 0.001, "GSL::Linalg::LU.invert(lu, perm) with NMatrix"
|
40
|
+
assert GSL::Linalg::LU.det(lu, sign) == -13, "GSL::Linalg::LU.det(lu, sign) with NMatrix"
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_qr
|
44
|
+
qr_answer = NMatrix.new([4,4],
|
45
|
+
[-0.692965, -0.454136, -1.06961 , -1.35144,
|
46
|
+
0.469664 , 0.564146, 0.72597 , 0.726696,
|
47
|
+
0.160373 , -0.159838, -0.781606, 0.063932,
|
48
|
+
0.584216 , 0.593044, -0.332286, 0.239865], dtype: :float64)
|
49
|
+
tau_answer = NMatrix.new([4], [1.25975, 1.45217, 1.80113, 0.0], dtype: :float64)
|
50
|
+
qr, tau = GSL::Linalg::QR.decomp(@nm)
|
51
|
+
|
52
|
+
assert_enum_abs qr_answer , qr , 0.001, "GSL::Linalg::QR.decomp(nmatrix)"
|
53
|
+
assert_enum_abs tau_answer, tau, 0.001, "GSL::Linalg::QR.decomp(nmatrix)"
|
54
|
+
|
55
|
+
assert_enum_abs GSL::Linalg::QR.solve(qr, tau, @b), @x_exp, 0.001,
|
56
|
+
"GSL::Linalg::QR.solve(qr, tau, b)"
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_sv
|
60
|
+
u_answer = NMatrix.new([4,4],
|
61
|
+
[-0.545591,-0.312561, 0.709796,-0.317529,
|
62
|
+
-0.5298 , 0.418583,-0.475268,-0.564111,
|
63
|
+
-0.524621, 0.436573, 0.112083, 0.722229,
|
64
|
+
-0.382642,-0.73246 ,-0.507688, 0.243598 ], dtype: :float64)
|
65
|
+
v_answer = NMatrix.new([4,4],
|
66
|
+
[-0.260024,-0.288729 ,-0.732277,-0.559279 ,
|
67
|
+
-0.294582,-0.0751933, 0.659393,-0.687581 ,
|
68
|
+
-0.630928, 0.762631 ,-0.12665 , 0.0654517,
|
69
|
+
-0.668983,-0.573912 , 0.113712, 0.458427 ], dtype: :float64)
|
70
|
+
s_answer = NMatrix.new([4], [2.24602,0.682566,0.423782,0.112813], dtype: :float64)
|
71
|
+
|
72
|
+
u, v, s = GSL::Linalg::SV.decomp(@nm)
|
73
|
+
|
74
|
+
assert_enum_abs u, u_answer, 0.001, "GSL::Linalg::SV.decomp(nmatrix) -> u"
|
75
|
+
assert_enum_abs v, v_answer, 0.001, "GSL::Linalg::SV.decomp(nmatrix) -> v"
|
76
|
+
assert_enum_abs s, s_answer, 0.001, "GSL::Linalg::SV.decomp(nmatrix) -> s"
|
77
|
+
|
78
|
+
assert_enum_abs GSL::Linalg::SV.solve(u, v, s, @b), @x_exp, 0.001,
|
79
|
+
"GSL::Linalg::SV.solve(u,v,s,b)"
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_cholesky
|
83
|
+
m = NMatrix.new([2,2], [4.0, 2, 2, 3], dtype: :float64)
|
84
|
+
b = NMatrix.new([2], [1,2], dtype: :float64)
|
85
|
+
|
86
|
+
cholesky = NMatrix.new([2,2], [2.0, 1.0, 1.0, 1.41421], dtype: :float64)
|
87
|
+
x_exp = NMatrix.new([2], [-0.125, 0.75], dtype: :float64)
|
88
|
+
|
89
|
+
c = GSL::Linalg::Cholesky
|
90
|
+
assert_enum_abs c.decomp(m) , cholesky, 0.001, "GSL::Linalg::Cholesky.decomp"
|
91
|
+
assert_enum_abs c.solve(cholesky, b), x_exp , 0.001, "GSL::Linalg::Cholesky.solve"
|
92
|
+
assert_enum_abs c.svx(cholesky, b) , x_exp , 0.001, "GSL::Linalg::Cholesky.svx"
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_hh
|
96
|
+
hh = GSL::Linalg::HH
|
97
|
+
assert_enum_abs hh.solve(@nm, @b), @x_exp, 0.001, "GSL::Linalg::HH.solve(m, b)"
|
98
|
+
assert_enum_abs hh.svx(@nm, @b) , @x_exp, 0.001, "GSL::Linalg::HH.svx(m, b)"
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class NMatrixStatsTest < GSL::TestCase
|
4
|
+
def setup
|
5
|
+
@lew = NMatrix.new([200],
|
6
|
+
[-213, -564, -35, -15, 141, 115, -420, -360, 203, -338, -431, 194,
|
7
|
+
-220, -513, 154, -125, -559, 92, -21, -579, -52, 99, -543, -175,
|
8
|
+
162, -457, -346, 204, -300, -474, 164, -107, -572, -8, 83, -541,
|
9
|
+
-224, 180, -420, -374, 201, -236, -531, 83, 27, -564, -112, 131,
|
10
|
+
-507, -254, 199, -311, -495, 143, -46, -579, -90, 136, -472, -338,
|
11
|
+
202, -287, -477, 169, -124, -568, 17, 48, -568, -135, 162, -430,
|
12
|
+
-422, 172, -74, -577, -13, 92, -534, -243, 194, -355, -465, 156,
|
13
|
+
-81, -578, -64, 139, -449, -384, 193, -198, -538, 110, -44, -577,
|
14
|
+
-6, 66, -552, -164, 161, -460, -344, 205, -281, -504, 134, -28,
|
15
|
+
-576, -118, 156, -437, -381, 200, -220, -540, 83, 11, -568, -160,
|
16
|
+
172, -414, -408, 188, -125, -572, -32, 139, -492, -321, 205, -262,
|
17
|
+
-504, 142, -83, -574, 0, 48, -571, -106, 137, -501, -266, 190,
|
18
|
+
-391, -406, 194, -186, -553, 83, -13, -577, -49, 103, -515, -280,
|
19
|
+
201, 300, -506, 131, -45, -578, -80, 138, -462, -361, 201, -211,
|
20
|
+
-554, 32, 74, -533, -235, 187, -372, -442, 182, -147, -566, 25,
|
21
|
+
68, -535, -244, 194, -351, -463, 174, -125, -570, 15, 72, -550,
|
22
|
+
-190, 172, -424, -385, 198, -218, -536, 96], dtype: :float64
|
23
|
+
)
|
24
|
+
|
25
|
+
@rawa = NMatrix.new([14],
|
26
|
+
[0.0421, 0.0941, 0.1064, 0.0242, 0.1331,
|
27
|
+
0.0773, 0.0243, 0.0815, 0.1186, 0.0356,
|
28
|
+
0.0728, 0.0999, 0.0614, 0.0479], dtype: :float64
|
29
|
+
)
|
30
|
+
|
31
|
+
@rawb = NMatrix.new([14],
|
32
|
+
[0.1081, 0.0986, 0.1566, 0.1961, 0.1125,
|
33
|
+
0.1942, 0.1079, 0.1021, 0.1583, 0.1673,
|
34
|
+
0.1675, 0.1856, 0.1688, 0.1512], dtype: :float64
|
35
|
+
)
|
36
|
+
|
37
|
+
@raww = NMatrix.new([14],
|
38
|
+
[0.0000, 0.0000, 0.0000, 3.000, 0.0000,
|
39
|
+
1.0000, 1.0000, 1.0000, 0.000, 0.5000,
|
40
|
+
7.0000, 5.0000, 4.0000, 0.123], dtype: :float64
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_stats
|
45
|
+
assert_rel GSL::Stats.mean(@lew), -177.435000000000, 1e-15, 'lew gsl_stats_mean'
|
46
|
+
assert_rel GSL::Stats.sd(@lew), 277.332168044316, 1e-15, 'lew gsl_stats_sd'
|
47
|
+
assert_rel GSL::Stats.lag1_autocorrelation(@lew), -0.307304800605679, 1e-14, 'lew autocorrelation'
|
48
|
+
|
49
|
+
rel = 1e-10
|
50
|
+
rawa_mean = GSL::Stats.mean(@rawa)
|
51
|
+
|
52
|
+
assert_rel GSL::Stats.variance_with_fixed_mean(@rawa, rawa_mean), 0.00113837428571429,
|
53
|
+
rel, 'gsl_stats_variance_with_fixed_mean'
|
54
|
+
|
55
|
+
assert_rel GSL::Stats.sd_with_fixed_mean(@rawa, rawa_mean), 0.0337398026922845,
|
56
|
+
rel, 'gsl_stats_sd_with_fixed_mean'
|
57
|
+
|
58
|
+
assert_rel GSL::Stats.variance(@rawb), 0.00124956615384615, rel, 'gsl_stats_variance'
|
59
|
+
assert_rel GSL::Stats.sd(@rawa), 0.0350134479659107, rel, 'gsl_stats_sd'
|
60
|
+
assert_rel GSL::Stats.absdev(@rawa), 0.0287571428571429, rel, 'gsl_stats_absdev'
|
61
|
+
assert_rel GSL::Stats.skew(@rawa), 0.0954642051479004, rel, 'gsl_stats_skew'
|
62
|
+
assert_rel GSL::Stats.kurtosis(@rawa), -1.38583851548909, rel, 'gsl_stats_kurtosis'
|
63
|
+
assert_rel GSL::Stats.covariance(@rawa,@rawb),-0.000139021538461539, rel, 'gsl_stats_covariance'
|
64
|
+
|
65
|
+
if GSL::GSL_VERSION >= '1.9.90'
|
66
|
+
assert_rel GSL::Stats.correlation(@rawa, @rawb), -0.112322712666074171, rel, 'gsl_stats_correlation'
|
67
|
+
assert_rel GSL::Stats.pvariance(@rawa, @rawb), 0.00123775384615385, rel, 'gsl_stats_pvariance'
|
68
|
+
assert_rel GSL::Stats.ttest(@rawa, @rawb), -5.67026326985851, rel, 'gsl_stats_ttest'
|
69
|
+
end
|
70
|
+
|
71
|
+
assert_rel GSL::Stats.max(@rawa), 0.1331, rel, 'gsl_stats_max'
|
72
|
+
assert_rel GSL::Stats.min(@rawa), 0.0242, rel, 'gsl_stats_min'
|
73
|
+
|
74
|
+
assert GSL::Stats.max_index(@rawa) == 4, 'gsl_stats_max'
|
75
|
+
assert GSL::Stats.min_index(@rawa) == 3, 'gsl_stats_min'
|
76
|
+
|
77
|
+
rawa_sorted = NMatrix.new([@rawa.size], @rawa.sort, dtype: :float64)
|
78
|
+
|
79
|
+
sorted = NMatrix.new([@rawa.size], @rawa.to_a.sort, dtype: :float64)
|
80
|
+
assert_rel GSL::Stats.median_from_sorted_data(sorted), 0.07505,
|
81
|
+
rel, 'gsl_stats_median_from_sorted_data'
|
82
|
+
|
83
|
+
assert_rel GSL::Stats.median_from_sorted_data(sorted[0..(sorted.size - 2)]), 0.0728,
|
84
|
+
rel, 'gsl_stats_median_from_sorted_data'
|
85
|
+
|
86
|
+
#TODO: Expose quantile_from_sorted_data
|
87
|
+
end
|
88
|
+
end
|
data/test/gsl/randist_test.rb
CHANGED
@@ -10,28 +10,34 @@ class RandistTest < GSL::TestCase
|
|
10
10
|
|
11
11
|
R_GLOBAL = GSL::Rng.alloc
|
12
12
|
|
13
|
+
@@use_nmatrix = false
|
14
|
+
|
13
15
|
def test_shuffle
|
14
16
|
n = 10
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
+
input = [GSL::Matrix.calloc(n, n)]
|
19
|
+
input << NMatrix.new([n,n], 0, dtype: :float64) if @@use_nmatrix
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
GSL::Ran.shuffle(R_GLOBAL, x)
|
22
|
-
n.times { |j| count.set(x[j], j, count[x[j], j] + 1) }
|
23
|
-
}
|
21
|
+
input.each do |count|
|
22
|
+
x = GSL::Permutation.alloc(n)
|
24
23
|
|
25
|
-
|
24
|
+
N.times { |i|
|
25
|
+
n.times { |j| x[j] = j }
|
26
|
+
GSL::Ran.shuffle(R_GLOBAL, x)
|
27
|
+
n.times { |j| count[x[j], j] += 1 }
|
28
|
+
}
|
29
|
+
|
30
|
+
expected = N / 10.0
|
26
31
|
|
27
|
-
|
28
|
-
|
29
|
-
|
32
|
+
n.times { |i|
|
33
|
+
n.times { |j|
|
34
|
+
d = (count[i, j] - expected).abs
|
30
35
|
|
31
|
-
|
32
|
-
|
36
|
+
refute d > 1 && d / Math.sqrt(expected) > 5,
|
37
|
+
"gsl_ran_shuffle #{i},#{j} (#{count[i, j] / N} observed vs 0.1 expected)"
|
38
|
+
}
|
33
39
|
}
|
34
|
-
|
40
|
+
end
|
35
41
|
end
|
36
42
|
|
37
43
|
def _test_moments(name, arg, a, b, pp)
|
@@ -54,8 +60,9 @@ class RandistTest < GSL::TestCase
|
|
54
60
|
|
55
61
|
status = status_i = 0
|
56
62
|
|
57
|
-
count =
|
58
|
-
|
63
|
+
count = @@use_nmatrix ? NMatrix.new([BINS], 0 ,dtype: :float64) :
|
64
|
+
GSL::Vector.calloc(BINS)
|
65
|
+
pp = @@use_nmatrix ? NMatrix.new([BINS], 0 ,dtype: :float64) : GSL::Vector.calloc(BINS)
|
59
66
|
|
60
67
|
N.times { |i|
|
61
68
|
r = R_GLOBAL.send(name, *args)
|
@@ -85,13 +92,13 @@ class RandistTest < GSL::TestCase
|
|
85
92
|
status |= status_i
|
86
93
|
|
87
94
|
refute status_i == 1,
|
88
|
-
"#{name} [#{x},#{x + dx}) (#{count[i]}/#{N}=#{count.to_f / N} observed vs #{pp[i]} expected)"
|
95
|
+
"#{name} [#{x},#{x + dx}) (#{count[i]}/#{N}=#{count[i].to_f / N} observed vs #{pp[i]} expected)"
|
89
96
|
}
|
90
97
|
|
91
98
|
assert status.zero?, "#{name}, sampling against pdf over range [#{a},#{b})"
|
92
99
|
end
|
93
100
|
|
94
|
-
def
|
101
|
+
def _test_randist
|
95
102
|
_test_moments(:ugaussian, nil, 0.0, 100.0, 0.5)
|
96
103
|
_test_moments(:ugaussian, nil, -1.0, 1.0, 0.6826895)
|
97
104
|
_test_moments(:ugaussian, nil, 3.0, 3.5, 0.0011172689)
|
@@ -99,12 +106,16 @@ class RandistTest < GSL::TestCase
|
|
99
106
|
_test_moments(:exponential, 2.0, 0.0, 1.0, 1 - Math.exp(-0.5))
|
100
107
|
_test_moments(:cauchy, 2.0, 0.0, 10000.0, 0.5)
|
101
108
|
|
102
|
-
|
103
|
-
|
104
|
-
_test_moments(:discrete, GSL::Ran::Discrete.alloc(
|
109
|
+
v = GSL::Vector.alloc(0.59, 0.4, 0.01)
|
110
|
+
|
111
|
+
_test_moments(:discrete, GSL::Ran::Discrete.alloc(v), -0.5, 0.5, 0.59)
|
112
|
+
_test_moments(:discrete, GSL::Ran::Discrete.alloc(v), 0.5, 1.5, 0.40)
|
113
|
+
_test_moments(:discrete, GSL::Ran::Discrete.alloc(v), 1.5, 3.5, 0.01)
|
114
|
+
|
115
|
+
v = GSL::Vector.alloc(1, 9, 3, 4, 5, 8, 6, 7, 2, 0)
|
105
116
|
|
106
|
-
_test_moments(:discrete, GSL::Ran::Discrete.alloc(
|
107
|
-
_test_moments(:discrete, GSL::Ran::Discrete.alloc(
|
117
|
+
_test_moments(:discrete, GSL::Ran::Discrete.alloc(v), -0.5, 0.5, 1.0 / 45.0)
|
118
|
+
_test_moments(:discrete, GSL::Ran::Discrete.alloc(v), 8.5, 9.5, 0)
|
108
119
|
|
109
120
|
_test_pdf(:beta, 2.0, 3.0)
|
110
121
|
_test_pdf(:cauchy, 2.0)
|
@@ -119,4 +130,13 @@ class RandistTest < GSL::TestCase
|
|
119
130
|
_test_pdf(:ugaussian_tail, 0.1, 2.0)
|
120
131
|
end
|
121
132
|
|
133
|
+
def test_randist
|
134
|
+
@@use_nmatrix = false
|
135
|
+
_test_randist
|
136
|
+
|
137
|
+
if ENV['NMATRIX']
|
138
|
+
@@use_nmatrix = true
|
139
|
+
_test_randist;
|
140
|
+
end
|
141
|
+
end
|
122
142
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,4 +1,13 @@
|
|
1
1
|
require 'test/unit'
|
2
|
+
begin
|
3
|
+
require 'narray'
|
4
|
+
rescue LoadError => e
|
5
|
+
end
|
6
|
+
|
7
|
+
begin
|
8
|
+
require 'nmatrix/nmatrix'
|
9
|
+
rescue LoadError => e
|
10
|
+
end
|
2
11
|
require 'gsl'
|
3
12
|
|
4
13
|
GSL::IEEE.env_setup
|
@@ -7,8 +16,9 @@ GSL::Rng.env_setup
|
|
7
16
|
class GSL::TestCase < Test::Unit::TestCase
|
8
17
|
|
9
18
|
def assert_factor(result, expected, factor, desc)
|
10
|
-
refute result == expected ? false :
|
11
|
-
|
19
|
+
refute result == expected ? false : (
|
20
|
+
expected.zero? ? result != expected :
|
21
|
+
(u = result / expected; u > factor || u < 1.0 / factor)),
|
12
22
|
'%s (%.18g observed vs %.18g expected)' % [desc, result, expected]
|
13
23
|
end
|
14
24
|
|
@@ -39,4 +49,22 @@ class GSL::TestCase < Test::Unit::TestCase
|
|
39
49
|
assert((a - b).abs < (self.class::EPSREL * GSL.MIN(a.abs, b.abs) + self.class::EPSABS), msg)
|
40
50
|
end
|
41
51
|
|
52
|
+
# Assert each element in an enumerable with absolute error.
|
53
|
+
def assert_enum_abs(result, expected, abserr, desc)
|
54
|
+
assert result.size == expected.size, 'size mismatch.'
|
55
|
+
|
56
|
+
r_enum = result.each
|
57
|
+
e_enum = expected.each
|
58
|
+
|
59
|
+
while true
|
60
|
+
begin
|
61
|
+
res_value = r_enum.next
|
62
|
+
exp_value = e_enum.next
|
63
|
+
assert_abs res_value, exp_value, abserr, "value mismatch. #{res_value} should be #{exp_value}"
|
64
|
+
rescue StopIteration
|
65
|
+
break
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
42
70
|
end
|