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,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ErrTest < GSL::TestCase
|
4
|
+
|
5
|
+
MAX_ERRS = 64
|
6
|
+
|
7
|
+
ERRORS = %w[
|
8
|
+
SUCCESS FAILURE CONTINUE EDOM ERANGE EFAULT EINVAL EFAILED
|
9
|
+
EFACTOR ESANITY ENOMEM EBADFUNC ERUNAWAY EMAXITER EZERODIV
|
10
|
+
EBADTOL ETOL EUNDRFLW EOVRFLW ELOSS EROUND EBADLEN ENOTSQR
|
11
|
+
ESING EDIVERGE EUNSUP EUNIMPL ECACHE ETABLE ENOPROG ENOPROGJ
|
12
|
+
ETOLF ETOLX ETOLG EOF
|
13
|
+
].map { |name| GSL.const_get(name) }
|
14
|
+
|
15
|
+
def test_number
|
16
|
+
assert ERRORS.uniq == ERRORS
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_message
|
20
|
+
assert ERRORS.map { |e| GSL.strerror(e) }.uniq.size == ERRORS.size
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class FitTest < GSL::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@x = GSL::Vector.alloc(1000)
|
7
|
+
@y = GSL::Vector.alloc(1000)
|
8
|
+
@w = GSL::Vector.alloc(1000)
|
9
|
+
|
10
|
+
@x.stride = 2
|
11
|
+
@w.stride = 3
|
12
|
+
@y.stride = 5
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_fit_1
|
16
|
+
n = 36
|
17
|
+
|
18
|
+
x = GSL::Vector.alloc(
|
19
|
+
0.2, 337.4, 118.2, 884.6, 10.1, 226.5, 666.3, 996.3,
|
20
|
+
448.6, 777.0, 558.2, 0.4, 0.6, 775.5, 666.9, 338.0,
|
21
|
+
447.5, 11.6, 556.0, 228.1, 995.8, 887.6, 120.2, 0.3,
|
22
|
+
0.3, 556.8, 339.1, 887.2, 999.0, 779.0, 11.1, 118.3,
|
23
|
+
229.2, 669.1, 448.9, 0.5
|
24
|
+
)
|
25
|
+
|
26
|
+
y = GSL::Vector.alloc(
|
27
|
+
0.1, 338.8, 118.1, 888.0, 9.2, 228.1, 668.5, 998.5,
|
28
|
+
449.1, 778.9, 559.2, 0.3, 0.1, 778.1, 668.8, 339.3,
|
29
|
+
448.9, 10.8, 557.7, 228.3, 998.0, 888.8, 119.6, 0.3,
|
30
|
+
0.6, 557.6, 339.3, 888.0, 998.5, 778.9, 10.2, 117.6,
|
31
|
+
228.9, 668.4, 449.2, 0.2
|
32
|
+
)
|
33
|
+
|
34
|
+
n.times { |i|
|
35
|
+
@x.set(i, x[i])
|
36
|
+
@w.set(i, 1.0)
|
37
|
+
@y.set(i, y[i])
|
38
|
+
}
|
39
|
+
|
40
|
+
c0, c1, cov00, cov01, cov11, sumsq = GSL::Fit.linear(@x, @y, n)
|
41
|
+
assert_rel c0, -0.262323073774029, 1e-10, 'norris gsl_fit_linear c0'
|
42
|
+
assert_rel c1, 1.00211681802045, 1e-10, 'norris gsl_fit_linear c1'
|
43
|
+
assert_rel cov00, GSL.pow(0.232818234301152, 2.0), 1e-10, 'norris gsl_fit_linear cov00'
|
44
|
+
assert_rel cov01, -7.74327536339570e-05, 1e-10, 'norris gsl_fit_linear cov01'
|
45
|
+
assert_rel cov11, GSL.pow(0.429796848199937E-03, 2.0), 1e-10, 'norris gsl_fit_linear cov11'
|
46
|
+
assert_rel sumsq, 26.6173985294224, 1e-10, 'norris gsl_fit_linear sumsq'
|
47
|
+
|
48
|
+
c0, c1, cov00, cov01, cov11, sumsq = GSL::Fit.wlinear(@x, @w, @y, n)
|
49
|
+
assert_rel c0, -0.262323073774029, 1e-10, 'norris gsl_fit_wlinear c0'
|
50
|
+
assert_rel c1, 1.00211681802045, 1e-10, 'norris gsl_fit_wlinear c1'
|
51
|
+
assert_rel cov00, 6.92384428759429e-02, 1e-10, 'norris gsl_fit_wlinear cov00'
|
52
|
+
assert_rel cov01, -9.89095016390515e-05, 1e-10, 'norris gsl_fit_wlinear cov01'
|
53
|
+
assert_rel cov11, 2.35960747164148e-07, 1e-10, 'norris gsl_fit_wlinear cov11'
|
54
|
+
assert_rel sumsq, 26.6173985294224, 1e-10, 'norris gsl_fit_wlinear sumsq'
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_fit_2
|
58
|
+
n = 11
|
59
|
+
x = GSL::Vector.alloc(60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70)
|
60
|
+
y = GSL::Vector.alloc(130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140)
|
61
|
+
|
62
|
+
n.times { |i|
|
63
|
+
@x.set(i, x[i])
|
64
|
+
@w.set(i, 1.0)
|
65
|
+
@y.set(i, y[i])
|
66
|
+
}
|
67
|
+
|
68
|
+
c1, cov11, sumsq = GSL::Fit.mul(@x, @y, n)
|
69
|
+
assert_rel c1, 2.07438016528926, 1e-10, 'noint1 gsl_fit_mul c1'
|
70
|
+
assert_rel cov11, GSL.pow(0.165289256198347E-01, 2.0), 1e-10, 'noint1 gsl_fit_mul cov11'
|
71
|
+
assert_rel sumsq, 127.272727272727, 1e-10, 'noint1 gsl_fit_mul sumsq'
|
72
|
+
|
73
|
+
c1, cov11, sumsq = GSL::Fit.wmul(@x, @w, @y, n)
|
74
|
+
assert_rel c1, 2.07438016528926, 1e-10, 'noint1 gsl_fit_wmul c1'
|
75
|
+
assert_rel cov11, 2.14661371686165e-05, 1e-10, 'noint1 gsl_fit_wmul cov11'
|
76
|
+
assert_rel sumsq, 127.272727272727, 1e-10, 'noint1 gsl_fit_wmul sumsq'
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_fit_3
|
80
|
+
n = 3
|
81
|
+
x = GSL::Vector.alloc(4, 5, 6)
|
82
|
+
y = GSL::Vector.alloc(3, 4, 4)
|
83
|
+
|
84
|
+
n.times { |i|
|
85
|
+
@x.set(i, x[i])
|
86
|
+
@w.set(i, 1.0)
|
87
|
+
@y.set(i, y[i])
|
88
|
+
}
|
89
|
+
|
90
|
+
c1, cov11, sumsq = GSL::Fit.mul(@x, @y, n)
|
91
|
+
assert_rel c1, 0.727272727272727, 1e-10, 'noint2 gsl_fit_mul c1'
|
92
|
+
assert_rel cov11, GSL.pow(0.420827318078432E-01, 2.0), 1e-10, 'noint2 gsl_fit_mul cov11'
|
93
|
+
assert_rel sumsq, 0.272727272727273, 1e-10, 'noint2 gsl_fit_mul sumsq'
|
94
|
+
|
95
|
+
c1, cov11, sumsq = GSL::Fit.wmul(@x, @w, @y, n)
|
96
|
+
assert_rel c1, 0.727272727272727, 1e-10, 'noint2 gsl_fit_wmul c1'
|
97
|
+
assert_rel cov11, 1.29870129870130e-02, 1e-10, 'noint2 gsl_fit_wmul cov11'
|
98
|
+
assert_rel sumsq, 0.272727272727273, 1e-10, 'noint2 gsl_fit_wmul sumsq'
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
@@ -0,0 +1,274 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class IntegrationTest < GSL::TestCase
|
4
|
+
|
5
|
+
def test_integration1
|
6
|
+
f = GSL::Function.alloc { |x| Math.exp(x) * Math.cos(x) }
|
7
|
+
|
8
|
+
xmin = 0.0
|
9
|
+
xmax = 1.0
|
10
|
+
limit = 1000
|
11
|
+
|
12
|
+
# QNG
|
13
|
+
assert f.integration_qng(xmin, xmax, 0.0, 1.0e-7)
|
14
|
+
assert f.integration_qng(xmin, xmax)
|
15
|
+
assert f.integration_qng([xmin, xmax])
|
16
|
+
assert f.integration_qng([xmin, xmax], [0.0, 1.0e-7])
|
17
|
+
assert f.integration_qng([xmin, xmax], 0.0, 1.0e-7)
|
18
|
+
assert f.integration_qng(xmin, xmax, [0.0, 1.0e-7])
|
19
|
+
|
20
|
+
# QAG
|
21
|
+
assert f.integration_qag(xmin, xmax, 0.0, 1.0e-7, limit, GSL::Integration::GAUSS15)
|
22
|
+
assert f.integration_qag([xmin, xmax], 0.0, 1.0e-7, limit, GSL::Integration::GAUSS15)
|
23
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15)
|
24
|
+
assert f.integration_qag(xmin, xmax, [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15)
|
25
|
+
assert f.integration_qag(xmin, xmax, GSL::Integration::GAUSS15)
|
26
|
+
assert f.integration_qag([xmin, xmax], GSL::Integration::GAUSS15)
|
27
|
+
|
28
|
+
w = GSL::Integration::Workspace.alloc
|
29
|
+
assert f.integration_qag(xmin, xmax, [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15, w)
|
30
|
+
assert f.integration_qag(xmin, xmax, 0.0, 1.0e-7, GSL::Integration::GAUSS15, w)
|
31
|
+
assert f.integration_qag([xmin, xmax], 0.0, 1.0e-7, GSL::Integration::GAUSS15, w)
|
32
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], GSL::Integration::GAUSS15, w)
|
33
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15, w)
|
34
|
+
assert f.integration_qag([xmin, xmax], w)
|
35
|
+
|
36
|
+
# QAGS
|
37
|
+
assert f.integration_qags(xmin, xmax)
|
38
|
+
assert f.integration_qags([xmin, xmax])
|
39
|
+
assert f.integration_qags(xmin, xmax, 0.0, 1e-7)
|
40
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7])
|
41
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7])
|
42
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7)
|
43
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7, limit)
|
44
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7], limit)
|
45
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7], limit)
|
46
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7, limit, w)
|
47
|
+
assert f.integration_qags(xmin, xmax, 0.0, 1e-7, limit, w)
|
48
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7], w)
|
49
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7], w)
|
50
|
+
|
51
|
+
assert f.integration_qags([xmin, xmax], limit)
|
52
|
+
assert f.integration_qags(xmin, xmax, limit)
|
53
|
+
assert f.integration_qags([xmin, xmax], w)
|
54
|
+
assert f.integration_qags(xmin, xmax, w)
|
55
|
+
assert f.integration_qags(xmin, xmax, limit, w)
|
56
|
+
assert f.integration_qags([xmin, xmax], limit, w)
|
57
|
+
|
58
|
+
# QAGP
|
59
|
+
assert f.integration_qagp([xmin, xmax])
|
60
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7])
|
61
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7)
|
62
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7, limit)
|
63
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7], limit)
|
64
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7, limit, w)
|
65
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7], w)
|
66
|
+
|
67
|
+
assert f.integration_qagp([xmin, xmax], limit)
|
68
|
+
assert f.integration_qagp([xmin, xmax], w)
|
69
|
+
assert f.integration_qagp([xmin, xmax], limit, w)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_integration2
|
73
|
+
f = GSL::Function.alloc { |x| Math.sin(x) / x }
|
74
|
+
|
75
|
+
xmin = 0.0
|
76
|
+
xmax = 2.0 * Math::PI
|
77
|
+
limit = 1000
|
78
|
+
|
79
|
+
# QNG
|
80
|
+
assert f.integration_qng(xmin, xmax, 0.0, 1.0e-7)
|
81
|
+
assert f.integration_qng(xmin, xmax)
|
82
|
+
assert f.integration_qng([xmin, xmax])
|
83
|
+
assert f.integration_qng([xmin, xmax], [0.0, 1.0e-7])
|
84
|
+
assert f.integration_qng([xmin, xmax], 0.0, 1.0e-7)
|
85
|
+
assert f.integration_qng(xmin, xmax, [0.0, 1.0e-7])
|
86
|
+
|
87
|
+
# QAG
|
88
|
+
assert f.integration_qag(xmin, xmax, 0.0, 1.0e-7, limit, GSL::Integration::GAUSS15)
|
89
|
+
assert f.integration_qag([xmin, xmax], 0.0, 1.0e-7, limit, GSL::Integration::GAUSS15)
|
90
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15)
|
91
|
+
assert f.integration_qag(xmin, xmax, [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15)
|
92
|
+
assert f.integration_qag(xmin, xmax, GSL::Integration::GAUSS15)
|
93
|
+
assert f.integration_qag([xmin, xmax], GSL::Integration::GAUSS15)
|
94
|
+
|
95
|
+
w = GSL::Integration::Workspace.alloc(2000)
|
96
|
+
assert f.integration_qag(xmin, xmax, [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15, w)
|
97
|
+
assert f.integration_qag(xmin, xmax, 0.0, 1.0e-7, GSL::Integration::GAUSS15, w)
|
98
|
+
assert f.integration_qag([xmin, xmax], 0.0, 1.0e-7, GSL::Integration::GAUSS15, w)
|
99
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], GSL::Integration::GAUSS15, w)
|
100
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15, w)
|
101
|
+
|
102
|
+
# QAGS
|
103
|
+
assert f.integration_qags(xmin, xmax)
|
104
|
+
assert f.integration_qags([xmin, xmax])
|
105
|
+
assert f.integration_qags(xmin, xmax, 0.0, 1e-7)
|
106
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7])
|
107
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7])
|
108
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7)
|
109
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7, limit)
|
110
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7], limit)
|
111
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7], limit)
|
112
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7, limit, w)
|
113
|
+
assert f.integration_qags(xmin, xmax, 0.0, 1e-7, limit, w)
|
114
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7], w)
|
115
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7], w)
|
116
|
+
|
117
|
+
assert f.integration_qags([xmin, xmax], limit)
|
118
|
+
assert f.integration_qags(xmin, xmax, limit)
|
119
|
+
assert f.integration_qags([xmin, xmax], w)
|
120
|
+
assert f.integration_qags(xmin, xmax, w)
|
121
|
+
assert f.integration_qags(xmin, xmax, limit, w)
|
122
|
+
assert f.integration_qags([xmin, xmax], limit, w)
|
123
|
+
|
124
|
+
# QAGP
|
125
|
+
assert f.integration_qagp([xmin, xmax])
|
126
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7])
|
127
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7)
|
128
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7, limit)
|
129
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7], limit)
|
130
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7, limit, w)
|
131
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7], w)
|
132
|
+
|
133
|
+
assert f.integration_qagp([xmin, xmax], limit)
|
134
|
+
assert f.integration_qagp([xmin, xmax], w)
|
135
|
+
assert f.integration_qagp([xmin, xmax], limit, w)
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_integration3
|
139
|
+
f = GSL::Function.alloc { |x| Math.exp(-x) / Math.sqrt(x) }
|
140
|
+
|
141
|
+
xmin = 0.0
|
142
|
+
xmax = 1.0
|
143
|
+
limit = 1000
|
144
|
+
|
145
|
+
# QNG
|
146
|
+
# XXX GSL::ERROR::ETOL: Ruby/GSL error code 14, failed to reach tolerance with
|
147
|
+
# highest-order rule (file qng.c, line 189), failed to reach the specified tolerance
|
148
|
+
#assert f.integration_qng(xmin, xmax, 0.0, 1.0e-7)
|
149
|
+
#assert f.integration_qng(xmin, xmax)
|
150
|
+
#assert f.integration_qng([xmin, xmax])
|
151
|
+
#assert f.integration_qng([xmin, xmax], [0.0, 1.0e-7])
|
152
|
+
#assert f.integration_qng([xmin, xmax], 0.0, 1.0e-7)
|
153
|
+
#assert f.integration_qng(xmin, xmax, [0.0, 1.0e-7])
|
154
|
+
|
155
|
+
# QAG
|
156
|
+
assert f.integration_qag(xmin, xmax, 0.0, 1.0e-7, limit, GSL::Integration::GAUSS15)
|
157
|
+
assert f.integration_qag([xmin, xmax], 0.0, 1.0e-7, limit, GSL::Integration::GAUSS15)
|
158
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15)
|
159
|
+
assert f.integration_qag(xmin, xmax, [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15)
|
160
|
+
assert f.integration_qag(xmin, xmax, GSL::Integration::GAUSS15)
|
161
|
+
assert f.integration_qag([xmin, xmax], GSL::Integration::GAUSS15)
|
162
|
+
|
163
|
+
w = GSL::Integration::Workspace.alloc(2000)
|
164
|
+
assert f.integration_qag(xmin, xmax, [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15, w)
|
165
|
+
assert f.integration_qag(xmin, xmax, 0.0, 1.0e-7, GSL::Integration::GAUSS15, w)
|
166
|
+
assert f.integration_qag([xmin, xmax], 0.0, 1.0e-7, GSL::Integration::GAUSS15, w)
|
167
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], GSL::Integration::GAUSS15, w)
|
168
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15, w)
|
169
|
+
|
170
|
+
# QAGS
|
171
|
+
assert f.integration_qags(xmin, xmax)
|
172
|
+
assert f.integration_qags([xmin, xmax])
|
173
|
+
assert f.integration_qags(xmin, xmax, 0.0, 1e-7)
|
174
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7])
|
175
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7])
|
176
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7)
|
177
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7, limit)
|
178
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7], limit)
|
179
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7], limit)
|
180
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7, limit, w)
|
181
|
+
assert f.integration_qags(xmin, xmax, 0.0, 1e-7, limit, w)
|
182
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7], w)
|
183
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7], w)
|
184
|
+
|
185
|
+
assert f.integration_qags([xmin, xmax], limit)
|
186
|
+
assert f.integration_qags(xmin, xmax, limit)
|
187
|
+
assert f.integration_qags([xmin, xmax], w)
|
188
|
+
assert f.integration_qags(xmin, xmax, w)
|
189
|
+
assert f.integration_qags(xmin, xmax, limit, w)
|
190
|
+
assert f.integration_qags([xmin, xmax], limit, w)
|
191
|
+
|
192
|
+
# QAGP
|
193
|
+
assert f.integration_qagp([xmin, xmax])
|
194
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7])
|
195
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7)
|
196
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7, limit)
|
197
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7], limit)
|
198
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7, limit, w)
|
199
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7], w)
|
200
|
+
|
201
|
+
assert f.integration_qagp([xmin, xmax], limit)
|
202
|
+
assert f.integration_qagp([xmin, xmax], w)
|
203
|
+
assert f.integration_qagp([xmin, xmax], limit, w)
|
204
|
+
end
|
205
|
+
|
206
|
+
def test_integration4
|
207
|
+
f = GSL::Function.alloc { |x| (1.0 - Math.exp(-x)) / Math.sqrt(x) }
|
208
|
+
|
209
|
+
xmin = 0.0
|
210
|
+
xmax = 0.2
|
211
|
+
limit = 1000
|
212
|
+
|
213
|
+
# QNG
|
214
|
+
# XXX GSL::ERROR::ETOL: Ruby/GSL error code 14, failed to reach tolerance with
|
215
|
+
# highest-order rule (file qng.c, line 189), failed to reach the specified tolerance
|
216
|
+
#assert f.integration_qng(xmin, xmax, 0.0, 1.0e-7)
|
217
|
+
#assert f.integration_qng(xmin, xmax)
|
218
|
+
#assert f.integration_qng([xmin, xmax])
|
219
|
+
#assert f.integration_qng([xmin, xmax], [0.0, 1.0e-7])
|
220
|
+
#assert f.integration_qng([xmin, xmax], 0.0, 1.0e-7)
|
221
|
+
#assert f.integration_qng(xmin, xmax, [0.0, 1.0e-7])
|
222
|
+
|
223
|
+
# QAG
|
224
|
+
assert f.integration_qag(xmin, xmax, 0.0, 1.0e-7, limit, GSL::Integration::GAUSS15)
|
225
|
+
assert f.integration_qag([xmin, xmax], 0.0, 1.0e-7, limit, GSL::Integration::GAUSS15)
|
226
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15)
|
227
|
+
assert f.integration_qag(xmin, xmax, [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15)
|
228
|
+
assert f.integration_qag(xmin, xmax, GSL::Integration::GAUSS15)
|
229
|
+
assert f.integration_qag([xmin, xmax], GSL::Integration::GAUSS15)
|
230
|
+
|
231
|
+
w = GSL::Integration::Workspace.alloc(2000)
|
232
|
+
assert f.integration_qag(xmin, xmax, [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15, w)
|
233
|
+
assert f.integration_qag(xmin, xmax, 0.0, 1.0e-7, GSL::Integration::GAUSS15, w)
|
234
|
+
assert f.integration_qag([xmin, xmax], 0.0, 1.0e-7, GSL::Integration::GAUSS15, w)
|
235
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], GSL::Integration::GAUSS15, w)
|
236
|
+
assert f.integration_qag([xmin, xmax], [0.0, 1.0e-7], limit, GSL::Integration::GAUSS15, w)
|
237
|
+
|
238
|
+
# QAGS
|
239
|
+
assert f.integration_qags(xmin, xmax)
|
240
|
+
assert f.integration_qags([xmin, xmax])
|
241
|
+
assert f.integration_qags(xmin, xmax, 0.0, 1e-7)
|
242
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7])
|
243
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7])
|
244
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7)
|
245
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7, limit)
|
246
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7], limit)
|
247
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7], limit)
|
248
|
+
assert f.integration_qags([xmin, xmax], 0.0, 1e-7, limit, w)
|
249
|
+
assert f.integration_qags(xmin, xmax, 0.0, 1e-7, limit, w)
|
250
|
+
assert f.integration_qags(xmin, xmax, [0.0, 1e-7], w)
|
251
|
+
assert f.integration_qags([xmin, xmax], [0.0, 1e-7], w)
|
252
|
+
|
253
|
+
assert f.integration_qags([xmin, xmax], limit)
|
254
|
+
assert f.integration_qags(xmin, xmax, limit)
|
255
|
+
assert f.integration_qags([xmin, xmax], w)
|
256
|
+
assert f.integration_qags(xmin, xmax, w)
|
257
|
+
assert f.integration_qags(xmin, xmax, limit, w)
|
258
|
+
assert f.integration_qags([xmin, xmax], limit, w)
|
259
|
+
|
260
|
+
# QAGP
|
261
|
+
assert f.integration_qagp([xmin, xmax])
|
262
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7])
|
263
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7)
|
264
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7, limit)
|
265
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7], limit)
|
266
|
+
assert f.integration_qagp([xmin, xmax], 0.0, 1e-7, limit, w)
|
267
|
+
assert f.integration_qagp([xmin, xmax], [0.0, 1e-7], w)
|
268
|
+
|
269
|
+
assert f.integration_qagp([xmin, xmax], limit)
|
270
|
+
assert f.integration_qagp([xmin, xmax], w)
|
271
|
+
assert f.integration_qagp([xmin, xmax], limit, w)
|
272
|
+
end
|
273
|
+
|
274
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class InterpTest < GSL::TestCase
|
4
|
+
|
5
|
+
def test_bsearch
|
6
|
+
x_array = GSL::Vector.alloc(0.0, 1.0, 2.0, 3.0, 4.0)
|
7
|
+
|
8
|
+
res = GSL::Interp.bsearch(x_array, 1.5, 0, 4)
|
9
|
+
refute res != 1, 'simple bsearch'
|
10
|
+
|
11
|
+
res = x_array.bsearch(4.0, 0, 4)
|
12
|
+
refute res != 3, 'upper endpoint bsearch'
|
13
|
+
|
14
|
+
res = GSL::Interp.bsearch(x_array, 0.0, 0, 4)
|
15
|
+
refute res != 0, 'lower endpoint bsearch'
|
16
|
+
|
17
|
+
res = GSL::Interp.bsearch(x_array, 2.0, 0, 4)
|
18
|
+
refute res != 2, 'degenerate bsearch'
|
19
|
+
|
20
|
+
res = GSL::Interp.bsearch(x_array, 10.0, 0, 4)
|
21
|
+
refute res != 3, 'out of bounds bsearch +'
|
22
|
+
|
23
|
+
res = GSL::Interp.bsearch(x_array, -10.0, 0, 4)
|
24
|
+
refute res != 0, 'out of bounds bsearch -'
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -0,0 +1,463 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class LinalgTest < GSL::TestCase
|
4
|
+
|
5
|
+
def _check(x, actual, eps, desc)
|
6
|
+
if x == actual
|
7
|
+
assert true
|
8
|
+
elsif actual.zero?
|
9
|
+
refute x.abs > eps, desc
|
10
|
+
else
|
11
|
+
refute((x - actual).abs / actual.abs > eps, desc)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def _create_general_matrix(size1, size2)
|
16
|
+
m = GSL::Matrix.alloc(size1, size2)
|
17
|
+
|
18
|
+
size1.times { |i|
|
19
|
+
size2.times { |j|
|
20
|
+
m.set(i, j, 1.0 / (i + j + 1.0))
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
m
|
25
|
+
end
|
26
|
+
|
27
|
+
def _create_hilbert_matrix(size)
|
28
|
+
_create_general_matrix(size, size)
|
29
|
+
end
|
30
|
+
|
31
|
+
def _create_vandermonde_matrix(size)
|
32
|
+
m = GSL::Matrix.alloc(size, size)
|
33
|
+
|
34
|
+
size.times { |i|
|
35
|
+
size.times { |j|
|
36
|
+
m.set(i, j, GSL.pow(i + 1.0, size - j - 1.0))
|
37
|
+
}
|
38
|
+
}
|
39
|
+
|
40
|
+
m
|
41
|
+
end
|
42
|
+
|
43
|
+
def setup
|
44
|
+
@hilb2 = _create_hilbert_matrix(2)
|
45
|
+
@hilb3 = _create_hilbert_matrix(3)
|
46
|
+
@hilb4 = _create_hilbert_matrix(4)
|
47
|
+
@hilb12 = _create_hilbert_matrix(12)
|
48
|
+
@vander2 = _create_vandermonde_matrix(2)
|
49
|
+
@vander3 = _create_vandermonde_matrix(3)
|
50
|
+
@vander4 = _create_vandermonde_matrix(4)
|
51
|
+
@vander12 = _create_vandermonde_matrix(12)
|
52
|
+
|
53
|
+
@hilb2_solution = GSL::Vector.alloc(-8.0, 18.0)
|
54
|
+
@hilb3_solution = GSL::Vector.alloc(27.0, -192.0, 210.0)
|
55
|
+
@hilb4_solution = GSL::Vector.alloc(-64.0, 900.0, -2520.0, 1820.0)
|
56
|
+
@hilb12_solution = GSL::Vector.alloc(
|
57
|
+
-1728.0, 245388.0, -8528520.0,
|
58
|
+
127026900.0, -1009008000.0, 4768571808.0,
|
59
|
+
-14202796608.0, 27336497760.0, -33921201600.0,
|
60
|
+
26189163000.0, -11437874448.0, 2157916488.0
|
61
|
+
)
|
62
|
+
|
63
|
+
@vander2_solution = GSL::Vector.alloc(1.0, 0.0)
|
64
|
+
@vander3_solution = GSL::Vector.alloc(0.0, 1.0, 0.0)
|
65
|
+
@vander4_solution = GSL::Vector.alloc(0.0, 0.0, 1.0, 0.0)
|
66
|
+
@vander12_solution = GSL::Vector.alloc(0.0, 0.0, 0.0, 0.0,
|
67
|
+
0.0, 0.0, 0.0, 0.0,
|
68
|
+
0.0, 0.0, 1.0, 0.0)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_matmult
|
72
|
+
a = GSL::Matrix.alloc([10.0, 5.0, 1.0, 20.0], 2, 2)
|
73
|
+
b = GSL::Matrix.alloc([10.0, 5.0, 2.0, 1.0, 3.0, 2.0], 2, 3)
|
74
|
+
c = a * b
|
75
|
+
|
76
|
+
refute((c[0, 0] - 105.0).abs > GSL::DBL_EPSILON)
|
77
|
+
refute((c[0, 1] - 65.0).abs > GSL::DBL_EPSILON)
|
78
|
+
refute((c[0, 2] - 30.0).abs > GSL::DBL_EPSILON)
|
79
|
+
refute((c[1, 0] - 30.0).abs > GSL::DBL_EPSILON)
|
80
|
+
refute((c[1, 1] - 65.0).abs > GSL::DBL_EPSILON)
|
81
|
+
refute((c[1, 2] - 42.0).abs > GSL::DBL_EPSILON)
|
82
|
+
end
|
83
|
+
|
84
|
+
def _test_bidiag_decomp_dim(m, eps, desc)
|
85
|
+
eps *= 2 * GSL::DBL_EPSILON
|
86
|
+
|
87
|
+
mm = m.size1
|
88
|
+
nn = m.size2
|
89
|
+
|
90
|
+
a = m.duplicate
|
91
|
+
b = GSL::Matrix.calloc(nn, nn)
|
92
|
+
|
93
|
+
u, v, d, sd = GSL::Linalg::Bidiag.unpack(*GSL::Linalg::Bidiag.decomp(a))
|
94
|
+
|
95
|
+
b.set_diagonal(d)
|
96
|
+
(nn - 1).times { |i| b[i, i + 1] = sd[i] }
|
97
|
+
|
98
|
+
a = u * b * v.trans
|
99
|
+
|
100
|
+
mm.times { |i|
|
101
|
+
nn.times { |j|
|
102
|
+
_check(aij = a[i, j], mij = m[i, j], eps,
|
103
|
+
'%s: (%d,%d)[%d,%d]: %22.18g %22.18g' % [desc, mm, nn, i, j, aij, mij])
|
104
|
+
}
|
105
|
+
}
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_bidiag_decomp
|
109
|
+
m53 = _create_general_matrix(5, 3)
|
110
|
+
m97 = _create_general_matrix(9, 7)
|
111
|
+
|
112
|
+
_test_bidiag_decomp_dim(m53, 64.0, 'bidiag_decomp m(5,3)')
|
113
|
+
_test_bidiag_decomp_dim(m97, 64.0, 'bidiag_decomp m(9,7)')
|
114
|
+
_test_bidiag_decomp_dim(@hilb2, 8.0, 'bidiag_decomp hilbert(2)')
|
115
|
+
_test_bidiag_decomp_dim(@hilb3, 64.0, 'bidiag_decomp hilbert(3)')
|
116
|
+
_test_bidiag_decomp_dim(@hilb4, 1024.0, 'bidiag_decomp hilbert(4)')
|
117
|
+
_test_bidiag_decomp_dim(@hilb12, 1024.0, 'bidiag_decomp hilbert(12)')
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_cholesky
|
121
|
+
m = GSL::Matrix.pascal(6)
|
122
|
+
|
123
|
+
c_exp = GSL::Matrix[[1, 0, 0, 0, 0, 0],
|
124
|
+
[1, 1, 0, 0, 0, 0],
|
125
|
+
[1, 2, 1, 0, 0, 0],
|
126
|
+
[1, 3, 3, 1, 0, 0],
|
127
|
+
[1, 4, 6, 4, 1, 0],
|
128
|
+
[1, 5, 10, 10, 5, 1]]
|
129
|
+
|
130
|
+
c = m.cholesky_decomp
|
131
|
+
a = c.lower
|
132
|
+
|
133
|
+
assert a == c_exp, "#{m.class}#cholesky_decomp"
|
134
|
+
assert a * a.trans == m, "#{m.class}#cholesky_decomp"
|
135
|
+
end
|
136
|
+
|
137
|
+
def _test_HH_solve_dim(m, actual, eps, desc)
|
138
|
+
eps *= GSL::DBL_EPSILON if eps > 1
|
139
|
+
|
140
|
+
dim = m.size1
|
141
|
+
|
142
|
+
x = GSL::Vector.indgen(dim) + 1
|
143
|
+
GSL::Linalg::HH.svx(m.duplicate, x)
|
144
|
+
|
145
|
+
dim.times { |i|
|
146
|
+
_check(si = x[i], ai = actual[i], eps,
|
147
|
+
'%s: %d[%d]: %22.18g %22.18g' % [desc, dim, i, si, ai])
|
148
|
+
}
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_HH_solve
|
152
|
+
_test_HH_solve_dim(@hilb2, @hilb2_solution, 8.0, 'HH_solve Hilbert(2)')
|
153
|
+
_test_HH_solve_dim(@hilb3, @hilb3_solution, 128.0, 'HH_solve Hilbert(3)')
|
154
|
+
_test_HH_solve_dim(@hilb4, @hilb4_solution, 2048.0, 'HH_solve Hilbert(4)')
|
155
|
+
_test_HH_solve_dim(@hilb12, @hilb12_solution, 0.5, 'HH_solve Hilbert(12)')
|
156
|
+
_test_HH_solve_dim(@vander2, @vander2_solution, 8.0, 'HH_solve Vander(2)')
|
157
|
+
_test_HH_solve_dim(@vander3, @vander3_solution, 64.0, 'HH_solve Vander(3)')
|
158
|
+
_test_HH_solve_dim(@vander4, @vander4_solution, 1024.0, 'HH_solve Vander(4)')
|
159
|
+
_test_HH_solve_dim(@vander12, @vander12_solution, 0.05, 'HH_solve Vander(12)')
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_LU
|
163
|
+
m = GSL::Matrix.alloc([0.18, 0.60, 0.57, 0.96], [0.41, 0.24, 0.99, 0.58],
|
164
|
+
[0.14, 0.30, 0.97, 0.66], [0.51, 0.13, 0.19, 0.85])
|
165
|
+
|
166
|
+
a = m.clone
|
167
|
+
assert m == a, "#{a.class}#LU_decomp: matrix not destroyed"
|
168
|
+
|
169
|
+
lu_exp = GSL::Matrix.alloc([0.51, 0.13, 0.19, 0.85],
|
170
|
+
[0.352941176470588, 0.554117647058823, 0.502941176470588, 0.66],
|
171
|
+
[0.803921568627451, 0.244515215852796, 0.71427813163482, -0.264713375796178],
|
172
|
+
[0.274509803921569, 0.476999292285916, 0.949126848480345, 0.363093705877982])
|
173
|
+
|
174
|
+
x_exp = GSL::Vector[-4.05205022957397, -12.6056113959069, 1.66091162670884, 8.69376692879523]
|
175
|
+
|
176
|
+
lu, perm, _sign = m.LU_decomp
|
177
|
+
assert lu == lu_exp, "#{a.class}#LU_decomp"
|
178
|
+
|
179
|
+
b = GSL::Vector[1, 2, 3, 4]
|
180
|
+
x = GSL::Linalg::LU.solve(lu, perm, b)
|
181
|
+
assert x == x_exp, "#{a.class}.LU_solve"
|
182
|
+
|
183
|
+
x = lu.solve(perm, b)
|
184
|
+
assert x == x_exp, "#{lu.class}#solve"
|
185
|
+
|
186
|
+
perm, _sign = m.LU_decomp!
|
187
|
+
assert m == lu_exp, "#{a.class}#LU_decomp!"
|
188
|
+
|
189
|
+
m = a.clone
|
190
|
+
|
191
|
+
x = GSL::Linalg::LU.solve(m, perm, b)
|
192
|
+
assert x == x_exp, "#{a.class}.LU_solve"
|
193
|
+
|
194
|
+
x = m.LU_solve(perm, b)
|
195
|
+
assert x == x_exp, "#{a.class}#LU_solve"
|
196
|
+
assert m == a, "#{a.class}#LU_solve: matrix not destroyed"
|
197
|
+
|
198
|
+
h = GSL::Matrix.hilbert(5)
|
199
|
+
invh = GSL::Matrix.invhilbert(5)
|
200
|
+
lu, perm, _sign = h.LU_decomp
|
201
|
+
|
202
|
+
a = GSL::Linalg::LU.invert(lu, perm)
|
203
|
+
assert a.equal?(invh, 1e-6), "#{h.class}#LU_invert, Hilbert matrix of order 5"
|
204
|
+
|
205
|
+
a = lu.invert(perm)
|
206
|
+
assert a.equal?(invh, 1e-6), "#{h.class}#LU_invert, Hilbert matrix of order 5"
|
207
|
+
|
208
|
+
a = h.inv
|
209
|
+
assert a.equal?(invh, 1e-6), "#{h.class}#LU_invert, Hilbert matrix of order 5"
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_QR
|
213
|
+
m = GSL::Matrix.alloc([0.18, 0.60, 0.57, 0.96], [0.41, 0.24, 0.99, 0.58],
|
214
|
+
[0.14, 0.30, 0.97, 0.66], [0.51, 0.13, 0.19, 0.85])
|
215
|
+
|
216
|
+
a = m.clone
|
217
|
+
assert m == a, "#{m.class}#QR_decomp: matrix not destroyed"
|
218
|
+
|
219
|
+
x_exp = GSL::Vector[-4.05205022957397, -12.6056113959069, 1.66091162670884, 8.69376692879523]
|
220
|
+
b = GSL::Vector[1, 2, 3, 4]
|
221
|
+
|
222
|
+
qr, tau = m.QR_decomp
|
223
|
+
|
224
|
+
x = m.QR_solve(b)
|
225
|
+
assert x == x_exp, "#{m.class}#QR_solve(b)"
|
226
|
+
|
227
|
+
x = GSL::Linalg::QR.solve(m, b)
|
228
|
+
assert x == x_exp, 'GSL::Linalg::QR::solve(b)'
|
229
|
+
|
230
|
+
tau = m.QR_decomp!
|
231
|
+
assert m != a, "#{m.class}#QR_decomp: matrix destroyed"
|
232
|
+
|
233
|
+
x = m.QR_solve(tau, b)
|
234
|
+
assert x == x_exp, "#{m.class}#QR_solve(tau, b)"
|
235
|
+
|
236
|
+
x = qr.solve(tau, b)
|
237
|
+
assert x == x_exp, "#{qr.class}#solve(tau, b)"
|
238
|
+
|
239
|
+
assert_raises(ArgumentError) { m.QR_solve(b) }
|
240
|
+
assert_raises(ArgumentError) { m.solve(b) }
|
241
|
+
|
242
|
+
x = m.solve(tau, b)
|
243
|
+
assert x == x_exp, "#{m.class}#solve(tau, b)"
|
244
|
+
|
245
|
+
m = a.clone
|
246
|
+
bb = b.clone
|
247
|
+
m.QR_svx(bb)
|
248
|
+
assert bb == x_exp, "#{m.class}#QR_svx(b)"
|
249
|
+
|
250
|
+
tau = GSL::Linalg::QR.decomp!(m)
|
251
|
+
bb = b.clone
|
252
|
+
m.QR_svx(tau, bb)
|
253
|
+
assert bb == x_exp, "#{m.class}#QR_svx(tau, b)"
|
254
|
+
assert_raises(ArgumentError) { m.QR_svx(bb) }
|
255
|
+
|
256
|
+
m = a.clone
|
257
|
+
qr, tau = m.QR_decomp
|
258
|
+
assert m == a, "#{m.class}#QR_decomp: matrix not destroyed"
|
259
|
+
|
260
|
+
x, r = m.QR_lssolve(b)
|
261
|
+
assert x == x_exp, "#{m.class}#QR_lssolve(b)"
|
262
|
+
|
263
|
+
r = m.QR_lssolve(b, x)
|
264
|
+
assert x == x_exp, "#{qr.class}#QR_lssolve(b, x)"
|
265
|
+
|
266
|
+
m.QR_lssolve(b, x, r)
|
267
|
+
assert x == x_exp, "#{qr.class}#QR_lssolve(b, x, r)"
|
268
|
+
|
269
|
+
x, r = qr.QR_lssolve(tau, b)
|
270
|
+
assert x == x_exp, "#{qr.class}#QR_lssolve(tau, b)"
|
271
|
+
|
272
|
+
r = qr.QR_lssolve(tau, b, x)
|
273
|
+
assert x == x_exp, "#{qr.class}#QR_lssolve(tau, b, x)"
|
274
|
+
|
275
|
+
qr.QR_lssolve(tau, b, x, r)
|
276
|
+
assert x == x_exp, "#{qr.class}#QR_lssolve(tau, b, x, r)"
|
277
|
+
assert_raises(ArgumentError) { qr.QR_lssolve(bb) }
|
278
|
+
end
|
279
|
+
|
280
|
+
def test_SV
|
281
|
+
a = GSL::Matrix.alloc([1, 2, 3, 4], 2, 2)
|
282
|
+
i = GSL::Matrix.identity(2)
|
283
|
+
ainv = a.inv
|
284
|
+
|
285
|
+
u, v, s = a.SV_decomp
|
286
|
+
sm = s.to_m_diagonal
|
287
|
+
sinv = s.map { |x| 1.0 / x }.to_m_diagonal
|
288
|
+
|
289
|
+
assert u * sm * v.trans == a, "#{a.class}#SV_decomp"
|
290
|
+
assert v * sinv * u.trans == ainv, "#{a.class}#SV_decomp"
|
291
|
+
|
292
|
+
assert u.trans * u == i, "#{a.class}#SV_decomp"
|
293
|
+
assert v.trans * v == i, "#{a.class}#SV_decomp"
|
294
|
+
|
295
|
+
assert a * v == u * sm, "#{a.class}#SV_decomp"
|
296
|
+
assert a.trans * u == v * sm, "#{a.class}#SV_decomp"
|
297
|
+
end
|
298
|
+
|
299
|
+
def _test_TDN_solve_dim(dim, d, a, b, actual, eps, desc)
|
300
|
+
eps *= GSL::DBL_EPSILON
|
301
|
+
|
302
|
+
abovediag = GSL::Vector.alloc(dim - 1)
|
303
|
+
belowdiag = GSL::Vector.alloc(dim - 1)
|
304
|
+
|
305
|
+
diag = GSL::Vector.alloc(dim)
|
306
|
+
diag.set_all(d)
|
307
|
+
|
308
|
+
rhs = GSL::Vector.indgen(dim) + 1
|
309
|
+
|
310
|
+
abovediag.set_all(a)
|
311
|
+
belowdiag.set_all(b)
|
312
|
+
|
313
|
+
x = GSL::Linalg.solve_tridiag(diag, abovediag, belowdiag, rhs)
|
314
|
+
|
315
|
+
dim.times { |i|
|
316
|
+
_check(si = x[i], ai = actual[i], eps,
|
317
|
+
'%s: %d[%d]: %22.18g %22.18g' % [desc, dim, i, si, ai])
|
318
|
+
}
|
319
|
+
end
|
320
|
+
|
321
|
+
def test_TDN_solve
|
322
|
+
actual = GSL::Vector.alloc(5)
|
323
|
+
|
324
|
+
actual[0] = -7.0 / 3.0
|
325
|
+
actual[1] = 5.0 / 3.0
|
326
|
+
actual[2] = 4.0 / 3.0
|
327
|
+
_test_TDN_solve_dim(3, 1.0, 2.0, 1.0, actual, 2.0, 'solve_TDN dim=2 A')
|
328
|
+
|
329
|
+
actual[0] = 0.75
|
330
|
+
actual[1] = 0.75
|
331
|
+
actual[2] = 2.625
|
332
|
+
_test_TDN_solve_dim(3, 1.0, 1.0 / 3.0, 1.0 / 2.0, actual, 2.0, 'solve_TDN dim=2 B')
|
333
|
+
|
334
|
+
actual[0] = 99.0 / 140.0
|
335
|
+
actual[1] = 41.0 / 35.0
|
336
|
+
actual[2] = 19.0 / 10.0
|
337
|
+
actual[3] = 72.0 / 35.0
|
338
|
+
actual[4] = 139.0 / 35.0
|
339
|
+
_test_TDN_solve_dim(5, 1.0, 1.0 / 4.0, 1.0 / 2.0, actual, 35.0 / 8.0, 'solve_TDN dim=5')
|
340
|
+
end
|
341
|
+
|
342
|
+
def _test_TDN_cyc_solve_dim(dim, d, a, b, actual, eps, desc)
|
343
|
+
eps *= GSL::DBL_EPSILON
|
344
|
+
|
345
|
+
abovediag = GSL::Vector.alloc(dim)
|
346
|
+
belowdiag = GSL::Vector.alloc(dim)
|
347
|
+
|
348
|
+
diag = GSL::Vector.alloc(dim)
|
349
|
+
rhs = GSL::Vector.indgen(dim) + 1
|
350
|
+
|
351
|
+
abovediag.set_all(a)
|
352
|
+
belowdiag.set_all(b)
|
353
|
+
|
354
|
+
diag.set_all(d)
|
355
|
+
|
356
|
+
x = GSL::Linalg.solve_cyc_tridiag(diag, abovediag, belowdiag, rhs)
|
357
|
+
dim.times { |i|
|
358
|
+
_check(si = x[i], ai = actual[i], eps,
|
359
|
+
'%s: %d[%d]: %22.18g %22.18g' % [desc, dim, i, si, ai])
|
360
|
+
}
|
361
|
+
end
|
362
|
+
|
363
|
+
def test_TDN_cyc_solve
|
364
|
+
actual = GSL::Vector.alloc(5)
|
365
|
+
|
366
|
+
actual[0] = 3.0 / 2.0
|
367
|
+
actual[1] = -1.0 / 2.0
|
368
|
+
actual[2] = 1.0 / 2.0
|
369
|
+
_test_TDN_cyc_solve_dim(3, 1.0, 2.0, 1.0, actual, 32.0, 'solve_TDN_cyc dim=2')
|
370
|
+
|
371
|
+
actual[0] = -5.0 / 22.0
|
372
|
+
actual[1] = -3.0 / 22.0
|
373
|
+
actual[2] = 29.0 / 22.0
|
374
|
+
actual[3] = -9.0 / 22.0
|
375
|
+
actual[4] = 43.0 / 22.0
|
376
|
+
_test_TDN_cyc_solve_dim(5, 3.0, 2.0, 1.0, actual, 66.0, 'solve_TDN_cyc dim=5')
|
377
|
+
end
|
378
|
+
|
379
|
+
def _test_TDS_solve_dim(dim, d, od, actual, eps, desc)
|
380
|
+
eps *= GSL::DBL_EPSILON
|
381
|
+
|
382
|
+
diag = GSL::Vector.alloc(dim)
|
383
|
+
diag.set_all(d)
|
384
|
+
|
385
|
+
rhs = GSL::Vector.indgen(dim) + 1
|
386
|
+
|
387
|
+
offdiag = GSL::Vector.alloc(dim - 1)
|
388
|
+
offdiag.set_all(od)
|
389
|
+
|
390
|
+
x = GSL::Linalg.solve_symm_tridiag(diag, offdiag, rhs)
|
391
|
+
dim.times { |i|
|
392
|
+
_check(si = x[i], ai = actual[i], eps,
|
393
|
+
'%s: %d[%d]: %22.18g %22.18g' % [desc, dim, i, si, ai])
|
394
|
+
}
|
395
|
+
end
|
396
|
+
|
397
|
+
def test_TDS_solve
|
398
|
+
actual = GSL::Vector[0.0, 2.0]
|
399
|
+
_test_TDS_solve_dim(2, 1.0, 0.5, actual, 8.0, 'solve_TDS dim=2 A')
|
400
|
+
|
401
|
+
actual = GSL::Vector[3.0 / 8.0, 15.0 / 8.0]
|
402
|
+
_test_TDS_solve_dim(2, 1.0, 1.0 / 3.0, actual, 8.0, 'solve_TDS dim=2 B')
|
403
|
+
|
404
|
+
actual = GSL::Vector[5.0 / 8.0, 9.0 / 8.0, 2.0, 15.0 / 8.0, 35.0 / 8.0]
|
405
|
+
_test_TDS_solve_dim(5, 1.0, 1.0 / 3.0, actual, 8.0, 'solve_TDS dim=5')
|
406
|
+
end
|
407
|
+
|
408
|
+
def _test_TDS_cyc_solve_one(dim, d, od, r, actual, eps, desc)
|
409
|
+
eps *= GSL::DBL_EPSILON
|
410
|
+
|
411
|
+
diag = d.duplicate
|
412
|
+
offdiag = od.duplicate
|
413
|
+
rhs = r.duplicate
|
414
|
+
|
415
|
+
x = GSL::Linalg.solve_symm_cyc_tridiag(diag, offdiag, rhs)
|
416
|
+
dim.times { |i|
|
417
|
+
_check(si = x[i], ai = actual[i], eps,
|
418
|
+
'%s: %d[%d]: %22.18g %22.18g' % [desc, dim, i, si, ai])
|
419
|
+
}
|
420
|
+
end
|
421
|
+
|
422
|
+
def test_TDS_cyc_solve
|
423
|
+
diag = GSL::Vector.alloc(1)
|
424
|
+
diag[0] = 2
|
425
|
+
|
426
|
+
offdiag = GSL::Vector.alloc(1)
|
427
|
+
offdiag[0] = 3
|
428
|
+
|
429
|
+
rhs = GSL::Vector.alloc(1)
|
430
|
+
rhs[0] = 7
|
431
|
+
|
432
|
+
actual = GSL::Vector.alloc(1)
|
433
|
+
actual[0] = 3.5
|
434
|
+
|
435
|
+
# XXX GSL::ERROR::EBADLEN: Ruby/GSL error code 19, size of cyclic system must be
|
436
|
+
# 3 or more (file tridiag.c, line 531), matrix/vector sizes are not conformant
|
437
|
+
#_test_TDS_cyc_solve_one(1, diag, offdiag, rhs, actual, 28.0, 'solve_TDS_cyc dim=1')
|
438
|
+
|
439
|
+
diag = GSL::Vector[1, 2]
|
440
|
+
offdiag = GSL::Vector[3, 4]
|
441
|
+
rhs = GSL::Vector[7, -7]
|
442
|
+
actual = GSL::Vector[-5, 4]
|
443
|
+
|
444
|
+
# XXX GSL::ERROR::EBADLEN: Ruby/GSL error code 19, size of cyclic system must be
|
445
|
+
# 3 or more (file tridiag.c, line 531), matrix/vector sizes are not conformant
|
446
|
+
#_test_TDS_cyc_solve_one(2, diag, offdiag, rhs, actual, 28.0, 'solve_TDS_cyc dim=2')
|
447
|
+
|
448
|
+
diag = GSL::Vector[1, 1, 1]
|
449
|
+
offdiag = GSL::Vector[3, 3, 3]
|
450
|
+
rhs = GSL::Vector[7, -7, 7]
|
451
|
+
actual = GSL::Vector[-2, 5, -2]
|
452
|
+
|
453
|
+
_test_TDS_cyc_solve_one(3, diag, offdiag, rhs, actual, 28.0, 'solve_TDS_cyc dim=3')
|
454
|
+
|
455
|
+
diag = GSL::Vector[4, 2, 1, 2, 4]
|
456
|
+
offdiag = GSL::Vector[1, 1, 1, 1, 1]
|
457
|
+
rhs = GSL::Vector[30, -24, 3, 21, -30]
|
458
|
+
actual = GSL::Vector[12, 3, -42, 42, -21]
|
459
|
+
|
460
|
+
_test_TDS_cyc_solve_one(5, diag, offdiag, rhs, actual, 35.0, 'solve_TDS_cyc dim=5')
|
461
|
+
end
|
462
|
+
|
463
|
+
end
|