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,37 @@
1
+ require 'test_helper'
2
+
3
+ class MatrixNmfTest < GSL::TestCase
4
+
5
+ def setup
6
+ @m1 = GSL::Matrix.alloc([6, 7, 8], [2, 3, 4], [3, 4, 5])
7
+ @m2 = GSL::Matrix.alloc([6, 7, 8], [2, 3, 4], [3, 4, 7])
8
+ end
9
+
10
+ def test_difcost
11
+ assert_equal 0, GSL::Matrix::NMF.difcost(@m1, @m1)
12
+ assert_equal 4, GSL::Matrix::NMF.difcost(@m1, @m2)
13
+ end
14
+
15
+ def test_nmf
16
+ [2, 3, 4, 5].each { |cols|
17
+ res = GSL::Matrix::NMF.nmf(@m1, cols)
18
+ assert_equal [3, cols], res[0].size
19
+ assert_equal [cols, 3], res[1].size
20
+
21
+ cost = GSL::Matrix::NMF.difcost(@m1, res[0] * res[1])
22
+ assert cost <= 0.0001, "Cols: #{cols}, Delta: #{cost}"
23
+ }
24
+ end
25
+
26
+ def test_matrix_nmf
27
+ [2, 3, 4, 5].each { |cols|
28
+ res = @m1.nmf(cols)
29
+ assert_equal [3, cols], res[0].size
30
+ assert_equal [cols, 3], res[1].size
31
+
32
+ cost = GSL::Matrix::NMF.difcost(@m1, res[0] * res[1])
33
+ assert cost <= 0.0001, "Cols: #{cols}, Delta: #{cost}"
34
+ }
35
+ end
36
+
37
+ end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ class MatrixTest < GSL::TestCase
4
+
5
+ def test_ispos_neg
6
+ m = GSL::Matrix::Int.alloc([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 3)
7
+ assert_equal 0, m.ispos
8
+ assert_equal false, m.ispos?
9
+ assert_equal 0, m.isneg
10
+ assert_equal false, m.isneg?
11
+
12
+ m += 1
13
+ assert_equal 1, m.ispos
14
+ assert_equal true, m.ispos?
15
+ assert_equal 0, m.isneg
16
+ assert_equal false, m.isneg?
17
+
18
+ m -= 100
19
+ assert_equal 0, m.ispos
20
+ assert_equal false, m.ispos?
21
+ assert_equal 1, m.isneg
22
+ assert_equal true, m.isneg?
23
+ end
24
+
25
+ def test_isnonneg
26
+ m = GSL::Matrix::Int.alloc([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 3)
27
+ assert_equal 1, m.isnonneg
28
+ assert_equal true, m.isnonneg?
29
+ assert_equal 0, m.isneg
30
+ assert_equal false, m.isneg?
31
+
32
+ m -= 100
33
+ assert_equal 0, m.isnonneg
34
+ assert_equal false, m.isnonneg?
35
+ assert_equal 1, m.isneg
36
+ assert_equal true, m.isneg?
37
+
38
+ m += 200
39
+ assert_equal 1, m.isnonneg
40
+ assert_equal true, m.isnonneg?
41
+ assert_equal 1, m.ispos
42
+ assert_equal true, m.ispos?
43
+ end
44
+
45
+ def test_eye
46
+ z = GSL::Complex[1, 0]
47
+ m = GSL::Matrix::Complex.eye(2, z)
48
+
49
+ assert_equal z, m[0, 0]
50
+ assert_equal GSL::Complex[0, 0], m[0, 1]
51
+ assert_equal GSL::Complex[0, 0], m[1, 0]
52
+ assert_equal z, m[1, 1]
53
+ end
54
+
55
+ def test_set_row
56
+ z0 = GSL::Complex[1, 0]
57
+ z1 = GSL::Complex[2, 0]
58
+
59
+ m = GSL::Matrix::Complex[2, 2]
60
+ m.set_row(0, z0, z1)
61
+
62
+ assert_equal z0, m[0, 0]
63
+ assert_equal z1, m[0, 1]
64
+ end
65
+
66
+ def test_set_col
67
+ z0 = GSL::Complex[1, 0]
68
+ z1 = GSL::Complex[2, 0]
69
+
70
+ m = GSL::Matrix::Complex[2, 2]
71
+ m.set_col(0, z0, z1)
72
+
73
+ assert_equal z0, m[0, 0]
74
+ assert_equal z1, m[1, 0]
75
+ end
76
+
77
+ end
@@ -0,0 +1,89 @@
1
+ require 'test_helper'
2
+
3
+ class MinTest < GSL::TestCase
4
+
5
+ EPSABS = 0.001
6
+ EPSREL = 0.001
7
+
8
+ MAX_ITERATIONS = 100
9
+
10
+ def _test_f(type, desc, f, x_lower, mid, x_upper, min)
11
+ s = GSL::Min::FMinimizer.alloc(type)
12
+ s.set(@f[f], mid, x_lower, x_upper)
13
+
14
+ status = iterations = 0
15
+
16
+ begin
17
+ iterations += 1
18
+ status = s.iterate
19
+
20
+ m = s.x_minimum
21
+ a = s.x_lower
22
+ b = s.x_upper
23
+
24
+ refute a > b, 'interval is invalid (%g,%g)' % [a, b]
25
+ refute m < a || m > b, 'm lies outside interval %g (%g,%g)' % [m, a, b]
26
+
27
+ break if status == 1
28
+
29
+ status = GSL::Min.test_interval(a, b, EPSABS, EPSREL)
30
+ end while status == GSL::CONTINUE && iterations < MAX_ITERATIONS
31
+
32
+ assert status.zero?, '%s, %s (%g obs vs %g expected)' % [s.name, desc, s.x_minimum, min]
33
+
34
+ assert_tol m, min, 'incorrect precision (%g obs vs %g expected)' % [m, min]
35
+ end
36
+
37
+ def _test_f_e(type, desc, f, x_lower, mid, x_upper, min)
38
+ s = GSL::Min::FMinimizer.alloc(type)
39
+
40
+ assert_raises(GSL::ERROR::EINVAL, '%s, %s' % [s.name, desc]) {
41
+ s.set(@f[f], mid, x_lower, x_upper)
42
+ }
43
+
44
+ status = iterations = 0
45
+
46
+ begin
47
+ iterations += 1
48
+ s.iterate
49
+
50
+ _ = s.x_minimum
51
+ a = s.x_lower
52
+ b = s.x_upper
53
+
54
+ status = GSL::Min.test_interval(a, b, EPSABS, EPSREL)
55
+ rescue
56
+ end while status == GSL::CONTINUE && iterations < MAX_ITERATIONS
57
+
58
+ assert status.zero?, '%s, %s' % [s.name, desc]
59
+ end
60
+
61
+ def setup
62
+ @f = [GSL::Function.alloc { |x| Math.cos(x) }]
63
+ @f << GSL::Function.alloc { |x| GSL.pow(x, 4.0) - 1 }
64
+ @f << GSL::Function.alloc { |x| Math.sqrt(x.abs) }
65
+ @f << GSL::Function.alloc { |x| x < 1.0 ? 1 : -Math.exp(-x) }
66
+ @f << GSL::Function.alloc { |x| x - 30.0 / (1.0 + 1e5 * GSL.pow(x - 0.8, 2.0)) }
67
+ end
68
+
69
+ %w[goldensection brent quad_golden].each { |type|
70
+ {
71
+ 'cos(x) [0 (3) 6]' => [0, 0.0, 3.0, 6.0, GSL::M_PI],
72
+ 'x^4 - 1 [-3 (-1) 17]' => [1, -3.0, -1.0, 17.0, 0.0],
73
+ 'sqrt(|x|) [-2 (-1) 1.5]' => [2, -2.0, -1.0, 1.5, 0.0],
74
+ 'func3(x) [-2 (3) 4]' => [3, -2.0, 3.0, 4.0, 1.0],
75
+ 'func4(x) [0 (0.782) 1]' => [4, 0, 0.782, 1.0, 0.8]
76
+ }.each_with_index { |(desc, args), i|
77
+ define_method("test_f_#{type}_#{i}") { _test_f(type, desc, *args) }
78
+ }
79
+
80
+ {
81
+ 'invalid range check [4, 0]' => [0, 4.0, 3.0, 0.0, GSL::M_PI],
82
+ 'invalid range check [1, 1]' => [0, 1.0, 1.0, 1.0, GSL::M_PI],
83
+ 'invalid range check [-1, 1]' => [0, -1.0, 0.0, 1.0, GSL::M_PI]
84
+ }.each_with_index { |(desc, args), i|
85
+ define_method("test_f_e_#{type}_#{i}") { _test_f_e(type, desc, *args) }
86
+ }
87
+ }
88
+
89
+ end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ class MonteTest < GSL::TestCase
4
+
5
+ DIM = 1
6
+
7
+ def test_miser
8
+ return unless GSL::Monte::Miser.method_defined?(:params_get)
9
+
10
+ miser = GSL::Monte::Miser.alloc(DIM)
11
+ params = miser.params_get
12
+
13
+ params.estimate_frac = 99
14
+ miser.params_set(params)
15
+ assert_abs miser.estimate_frac, 99, 1e-5, 'miser_estimate_frac'
16
+
17
+ params.min_calls = 9
18
+ miser.params_set(params)
19
+ assert_int miser.min_calls, 9, 'miser_min_calls'
20
+
21
+ params.min_calls_per_bisection = 7
22
+ miser.params_set(params)
23
+ assert_int miser.min_calls_per_bisection, 7, 'miser_min_calls_per_bisection'
24
+
25
+ params.alpha = 3
26
+ miser.params_set(params)
27
+ assert_abs miser.alpha, 3, 1e-5, 'miser_alpha'
28
+
29
+ params.dither = 4
30
+ miser.params_set(params)
31
+ assert_abs miser.dither, 4, 1e-5, 'miser_dither'
32
+ end
33
+
34
+ def test_vegas
35
+ return unless GSL::Monte::Vegas.method_defined?(:params_get)
36
+
37
+ vegas = GSL::Monte::Vegas.alloc(DIM)
38
+ params = vegas.params_get
39
+
40
+ params.alpha = 1
41
+ vegas.params_set(params)
42
+ assert_abs vegas.alpha, 1, 1e-5, 'vegas_alpha'
43
+
44
+ params.iterations = 4
45
+ vegas.params_set(params)
46
+ assert_int vegas.iterations, 4, 'vegas_iterations'
47
+
48
+ params.stage = 3
49
+ vegas.params_set(params)
50
+ assert_int vegas.stage, 3, 'vegas_stage'
51
+
52
+ params.mode = GSL::Monte::Vegas::MODE_IMPORTANCE
53
+ vegas.params_set(params)
54
+ assert_int vegas.mode, GSL::Monte::Vegas::MODE_IMPORTANCE, 'vegas_mode MODE_IMPORTANCE'
55
+
56
+ params.mode = GSL::Monte::Vegas::MODE_IMPORTANCE_ONLY
57
+ vegas.params_set(params)
58
+ assert_int vegas.mode, GSL::Monte::Vegas::MODE_IMPORTANCE_ONLY, 'vegas_mode MODE_IMPORTANCE_ONLY'
59
+
60
+ params.mode = GSL::Monte::Vegas::MODE_STRATIFIED
61
+ vegas.params_set(params)
62
+ assert_int vegas.mode, GSL::Monte::Vegas::MODE_STRATIFIED, 'vegas_mode MODE_STRATIFIED'
63
+
64
+ params.verbose = 0
65
+ vegas.params_set(params)
66
+ assert_int vegas.verbose, 0, 'vegas_verbose 0'
67
+
68
+ params.verbose = 1
69
+ vegas.params_set(params)
70
+ assert_int vegas.verbose, 1, 'vegas_verbose 1'
71
+
72
+ params.verbose = -1
73
+ vegas.params_set(params)
74
+ assert_int vegas.verbose, -1, 'vegas_verbose -1'
75
+ end
76
+
77
+ end
@@ -0,0 +1,753 @@
1
+ require 'test_helper'
2
+
3
+ class MultifitTest < GSL::TestCase
4
+
5
+ def _test_lmder(fdf, x, xx, f, cov)
6
+ s = GSL::MultiFit::FdfSolver.alloc('lmsder', fdf.n, fdf.p)
7
+ s.set(fdf, x)
8
+
9
+ 20.times { |i|
10
+ s.iterate
11
+
12
+ fdf.p.times { |j|
13
+ assert_rel s.x[j], xx[fdf.p * i + j], 1e-5, "lmsder, iter=#{i}, x#{j}"
14
+ }
15
+
16
+ assert_rel GSL::Blas.dnrm2(s.f), f[i], 1e-5, "lmsder, iter=#{i}, f"
17
+ }
18
+
19
+ covar = s.covar(0.0)
20
+
21
+ fdf.p.times { |i|
22
+ fdf.p.times { |j|
23
+ assert_rel covar[i, j], cov[i * fdf.p + j], 1e-7, "gsl_multifit_covar cov(#{i},#{j})"
24
+ }
25
+ }
26
+ end
27
+
28
+ def _test_fdf(name, fdf, x, x_final, f_sumsq, sigma)
29
+ s = GSL::MultiFit::FdfSolver.alloc('lmsder', fdf.n, fdf.p)
30
+ s.set(fdf, x)
31
+
32
+ 1000.times {
33
+ s.iterate
34
+
35
+ status = s.test_delta(0.0, 1e-7)
36
+ break if status != GSL::CONTINUE
37
+ }
38
+
39
+ covar = s.covar(0.0)
40
+
41
+ fdf.p.times { |i|
42
+ assert_rel s.x[i], x_final[i], 1e-5, "#{name}, lmsder, x#{i}"
43
+ }
44
+
45
+ s2 = GSL.pow(GSL::Blas.dnrm2(s.f), 2.0)
46
+ assert_rel s2, f_sumsq, 1e-5, "#{name}, lmsder, |f|^2"
47
+
48
+ fdf.p.times { |i|
49
+ ei = Math.sqrt(s2 / (fdf.n - fdf.p)) * Math.sqrt(covar[i, i])
50
+ assert_rel ei, sigma[i], 1e-4, "#{name}, sigma(#{i})"
51
+ }
52
+ end
53
+
54
+ def test_2dgauss
55
+ maxiter = 10
56
+ n = 33
57
+
58
+ point = Struct.new(:x, :y)
59
+
60
+ # model: a * exp(-((x - x0) ** 2 + (y - y0) ** 2) / 2 / sigma ** 2)
61
+ gauss_f = lambda { |x, t, y, s, f|
62
+ # x: parameters as a Vecor
63
+ # t: observed points as an Array
64
+ # y: observed data as a GSL::Vector
65
+ # s: errorbar
66
+ # f: result
67
+ a = x[0]
68
+ x0 = x[1]
69
+ y0 = x[2]
70
+ sigma2 = x[3] ** 2
71
+
72
+ y.size.times { |i|
73
+ f.set(i, (a * Math.exp(-((t[i].x - x0) ** 2 + (t[i].y - y0) ** 2) / 2 / sigma2) - y[i]) / s[i])
74
+ }
75
+
76
+ GSL::SUCCESS
77
+ }
78
+
79
+ gauss_df = lambda { |x, t, y, s, df|
80
+ a = x[0]
81
+ x0 = x[1]
82
+ y0 = x[2]
83
+ sigma = x[3]
84
+ sigma2 = sigma ** 2
85
+
86
+ y.size.times { |i|
87
+ dx = t[i].x - x0; dx2 = dx ** 2
88
+ dy = t[i].y - y0; dy2 = dy ** 2
89
+
90
+ f = a * Math.exp(-(dx2 + dy2) / 2 / sigma2)
91
+
92
+ df.set(i, 0, f / a / s[i])
93
+ df.set(i, 1, f * dx / sigma2 / s[i])
94
+ df.set(i, 2, f * dy / sigma2 / s[i])
95
+ df.set(i, 3, f * (dx2 + dy2) / sigma2 / sigma / s[i])
96
+ }
97
+
98
+ GSL::SUCCESS
99
+ }
100
+
101
+ # goal
102
+ xgoal = GSL::Vector.alloc([1, 0, 0, 1])
103
+ parname = %w[a x0 y0 si]
104
+
105
+ # data
106
+ t = []
107
+ tmin = -10.0
108
+ tmax = 10.0
109
+
110
+ n.times { |j|
111
+ n.times { |i|
112
+ t << point.new(tmin + (tmax - tmin) * i / (n - 1), tmin + (tmax - tmin) * j / (n - 1))
113
+ }
114
+ }
115
+
116
+ stdev = xgoal[0] * 0.1
117
+
118
+ s = GSL::Vector.alloc(Array.new(t.size, stdev)) # error bar of each datum
119
+ r = GSL::Rng.alloc
120
+ e = GSL::Vector.alloc(t.size)
121
+
122
+ t.size.times { |i|
123
+ e[i] = -r.gaussian(stdev) # perturbation to data
124
+ }
125
+
126
+ y = GSL::Vector.alloc(t.size)
127
+ n = GSL::Vector.alloc(Array.new(t.size, 1.0))
128
+ gauss_f.call(xgoal, t, e, n, y) # data: y = model - e
129
+
130
+ # fitting
131
+ x = GSL::Vector.alloc([0.5, 0.1, -0.1, 2.0]) # initial guess
132
+ fdf = GSL::MultiFit::Function_fdf.alloc(gauss_f, gauss_df, x.size)
133
+ fdf.set_data(t, y, s)
134
+
135
+ solver = GSL::MultiFit::FdfSolver.alloc(GSL::MultiFit::FdfSolver::LMSDER, t.size, x.size)
136
+ solver.set(fdf, x)
137
+
138
+ #solver.print_state(0)
139
+
140
+ maxiter.times { |i|
141
+ solver.iterate
142
+
143
+ status = solver.test_delta(1e-6, 1e-6)
144
+ #solver.print_state(i + 1)
145
+
146
+ break if status != GSL::CONTINUE
147
+ }
148
+
149
+ # results
150
+ covar = solver.covar(0.0)
151
+ xresult = solver.position
152
+ dof = t.size - xresult.size
153
+ chi2 = GSL.pow_2(solver.f.dnrm2)
154
+ xsigma = GSL::Vector.alloc(xresult.size)
155
+
156
+ xresult.size.times { |i|
157
+ xsigma[i] = Math.sqrt(chi2 / dof * covar[i, i]) * 2.0
158
+ # allow resulting parameters to differ two times than standard error
159
+ }
160
+
161
+ desc = "a*exp(-((x-x0)**2+(y-y0)**2)/2/si**2), chi2/N:%.3g" % (chi2 / t.size)
162
+
163
+ xresult.size.times { |i|
164
+ assert_rel xresult[i], xgoal[i], xsigma[i], '%s: %-2.2s' % [desc, parname[i]]
165
+
166
+ refute((xresult[i] - xgoal[i]).abs > xsigma[i],
167
+ '%s: %-2.2s is %s +- %s' % [desc, parname[i], xresult[i], xsigma[i]])
168
+ }
169
+ end
170
+
171
+ def test_brown
172
+ brown_N = 20
173
+ brown_P = 4
174
+
175
+ brown_X = GSL::Matrix.alloc(
176
+ [24.3485677, 4.71448798, -2.19486633, 2.69405755],
177
+ [22.4116222, 3.93075538, -1.42344852, 2.5233557],
178
+ [17.88886, 2.9290853, 0.125174936, -3.96823353],
179
+ [17.3237176, 2.99606803, 2.03285653, 2.28992327],
180
+ [17.0906508, 3.02485425, 0.296995153, 0.0876226126],
181
+ [16.578006, 3.1036312, -0.18617941, 0.103262914],
182
+ [15.692993, 3.33088442, 0.0706406887, 1.05923955],
183
+ [14.3232177, 3.85604218, -2.3762839, -3.09486813],
184
+ [14.1279266, 3.97896121, 0.446109351, 1.40023753],
185
+ [13.6081961, 4.16435075, -1.51250057, -1.52510626],
186
+ [13.4295245, 4.22697223, -0.196985195, 0.532009293],
187
+ [13.0176117, 4.3579261, -0.353131208, 0.301377627],
188
+ [12.2713535, 4.62398535, -0.00183585584, 0.894170703],
189
+ [11.0316144, 5.13967727, -2.38978772, -2.89510064],
190
+ [10.8807981, 5.24558004, 0.230495952, 1.27315117],
191
+ [10.4029264, 5.41141257, -1.5116632, -1.47615921],
192
+ [10.2574435, 5.46211045, -0.299855732, 0.451893162],
193
+ [9.87863876, 5.57914292, -0.368885288, 0.358086545],
194
+ [9.1894983, 5.82082741, -0.230157969, 0.621476534],
195
+ [8.00589008, 6.27788753, -1.46022815, -1.33468082]
196
+ )
197
+
198
+ brown_F = GSL::Vector.alloc(
199
+ 2474.05541, 1924.69004, 1280.63194, 1244.81867,
200
+ 1190.53739, 1159.34935, 1108.44426, 1090.11073,
201
+ 1015.92942, 1002.43533, 971.221084, 949.589435,
202
+ 911.359899, 906.522994, 840.525729, 833.950164,
203
+ 807.557511, 791.00924, 761.09598, 726.787783
204
+ )
205
+
206
+ brown_cov = GSL::Matrix.alloc(
207
+ [ 1.8893186910e-01, -4.7099989571e-02, 5.2154168404e-01, 1.6608168209e-02],
208
+ [-4.7099989571e-02, 1.1761534388e-02, -1.2987843074e-01, -4.1615942391e-03],
209
+ [ 5.2154168404e-01, -1.2987843074e-01, 1.4653936514e+00, 1.5738321686e-02],
210
+ [ 1.6608168209e-02, -4.1615942391e-03, 1.5738321686e-02, 4.2348042340e-02]
211
+ )
212
+
213
+ brown_x0 = GSL::Vector.alloc(25, 5, -5, -1)
214
+
215
+ brown_f = lambda { |x, t, y, f|
216
+ brown_N.times { |i|
217
+ ti = 0.2 * (i + 1)
218
+ ui = x[0] + x[1] * ti - Math.exp(ti)
219
+ vi = x[2] + x[3] * Math.sin(ti) - Math.cos(ti)
220
+ f[i] = ui * ui + vi * vi
221
+ }
222
+
223
+ GSL::SUCCESS
224
+ }
225
+
226
+ brown_df = lambda { |x, t, y, df|
227
+ brown_N.times { |i|
228
+ ti = 0.2 * (i + 1)
229
+ ui = x[0] + x[1] * ti - Math.exp(ti)
230
+ vi = x[2] + x[3] * Math.sin(ti) - Math.cos(ti)
231
+
232
+ df.set(i, 0, 2.0 * ui)
233
+ df.set(i, 1, 2.0 * ui * ti)
234
+ df.set(i, 2, 2.0 * vi)
235
+ df.set(i, 3, 2.0 * vi * Math.sin(ti))
236
+ }
237
+
238
+ GSL::SUCCESS
239
+ }
240
+
241
+ fdf = GSL::MultiFit::Function_fdf.alloc(brown_f, brown_df, brown_P)
242
+ fdf.set_data(GSL::Vector.alloc(brown_N), GSL::Vector.alloc(brown_N))
243
+
244
+ _test_lmder(fdf, brown_x0, brown_X.vector_view, brown_F, brown_cov.vector_view)
245
+ end
246
+
247
+ def test_enso
248
+ enso_N = 168
249
+ enso_P = 9
250
+
251
+ enso_x0 = GSL::Vector.alloc(10.0, 3.0, 0.5, 44.0, -1.5, 0.5, 26.0, 0.1, 1.5)
252
+
253
+ enso_x = GSL::Vector.alloc(
254
+ 1.0510749193E+01, 3.0762128085E+00, 5.3280138227E-01,
255
+ 4.4311088700E+01, -1.6231428586E+00, 5.2554493756E-01,
256
+ 2.6887614440E+01, 2.1232288488E-01, 1.4966870418E+00
257
+ )
258
+
259
+ enso_sumsq = 7.8853978668E+02
260
+
261
+ enso_sigma = GSL::Vector.alloc(
262
+ 1.7488832467E-01, 2.4310052139E-01, 2.4354686618E-01,
263
+ 9.4408025976E-01, 2.8078369611E-01, 4.8073701119E-01,
264
+ 4.1612939130E-01, 5.1460022911E-01, 2.5434468893E-01
265
+ )
266
+
267
+ enso_F = GSL::Vector.alloc(
268
+ 12.90000, 11.30000, 10.60000, 11.20000, 10.90000, 7.50000, 7.70000,
269
+ 11.70000, 12.90000, 14.30000, 10.90000, 13.70000, 17.10000, 14.00000,
270
+ 15.30000, 8.50000, 5.70000, 5.50000, 7.60000, 8.60000, 7.30000,
271
+ 7.60000, 12.70000, 11.00000, 12.70000, 12.90000, 13.00000, 10.90000,
272
+ 10.40000, 10.20000, 8.00000, 10.90000, 13.60000, 10.50000, 9.20000,
273
+ 12.40000, 12.70000, 13.30000, 10.10000, 7.80000, 4.80000, 3.00000,
274
+ 2.50000, 6.30000, 9.70000, 11.60000, 8.60000, 12.40000, 10.50000,
275
+ 13.30000, 10.40000, 8.10000, 3.70000, 10.70000, 5.10000, 10.40000,
276
+ 10.90000, 11.70000, 11.40000, 13.70000, 14.10000, 14.00000, 12.50000,
277
+ 6.30000, 9.60000, 11.70000, 5.00000, 10.80000, 12.70000, 10.80000,
278
+ 11.80000, 12.60000, 15.70000, 12.60000, 14.80000, 7.80000, 7.10000,
279
+ 11.20000, 8.10000, 6.40000, 5.20000, 12.00000, 10.20000, 12.70000,
280
+ 10.20000, 14.70000, 12.20000, 7.10000, 5.70000, 6.70000, 3.90000,
281
+ 8.50000, 8.30000, 10.80000, 16.70000, 12.60000, 12.50000, 12.50000,
282
+ 9.80000, 7.20000, 4.10000, 10.60000, 10.10000, 10.10000, 11.90000,
283
+ 13.60000, 16.30000, 17.60000, 15.50000, 16.00000, 15.20000, 11.20000,
284
+ 14.30000, 14.50000, 8.50000, 12.00000, 12.70000, 11.30000, 14.50000,
285
+ 15.10000, 10.40000, 11.50000, 13.40000, 7.50000, 0.60000, 0.30000,
286
+ 5.50000, 5.00000, 4.60000, 8.20000, 9.90000, 9.20000, 12.50000,
287
+ 10.90000, 9.90000, 8.90000, 7.60000, 9.50000, 8.40000, 10.70000,
288
+ 13.60000, 13.70000, 13.70000, 16.50000, 16.80000, 17.10000, 15.40000,
289
+ 9.50000, 6.10000, 10.10000, 9.30000, 5.30000, 11.20000, 16.60000,
290
+ 15.60000, 12.00000, 11.50000, 8.60000, 13.80000, 8.70000, 8.60000,
291
+ 8.60000, 8.70000, 12.80000, 13.20000, 14.00000, 13.40000, 14.80000
292
+ )
293
+
294
+ enso_f = lambda { |x, t, y, f|
295
+ b = x
296
+
297
+ enso_N.times { |i|
298
+ ti, pi = t[i], GSL::M_PI
299
+
300
+ yy = b[0]
301
+ yy += b[1] * Math.cos(2.0 * pi * ti / 12)
302
+ yy += b[2] * Math.sin(2.0 * pi * ti / 12)
303
+ yy += b[4] * Math.cos(2.0 * pi * ti / b[3])
304
+ yy += b[5] * Math.sin(2.0 * pi * ti / b[3])
305
+ yy += b[7] * Math.cos(2.0 * pi * ti / b[6])
306
+ yy += b[8] * Math.sin(2.0 * pi * ti / b[6])
307
+
308
+ f[i] = y[i] - yy
309
+ }
310
+
311
+ GSL::SUCCESS
312
+ }
313
+
314
+ enso_df = lambda { |x, t, y, df|
315
+ b = x
316
+
317
+ enso_N.times { |i|
318
+ ti, pi = t[i], GSL::M_PI
319
+
320
+ df.set(i, 0, -1.0)
321
+ df.set(i, 1, -Math.cos(2.0 * pi * ti / 12))
322
+ df.set(i, 2, -Math.sin(2.0 * pi * ti / 12))
323
+ df.set(i, 3, -b[4] * (2.0 * pi * ti / (b[3] * b[3])) * Math.sin(2 * pi * ti / b[3]) + b[5] * (2 * pi * ti / (b[3] * b[3])) * Math.cos(2 * pi * ti / b[3]))
324
+ df.set(i, 4, -Math.cos(2 * pi * ti / b[3]))
325
+ df.set(i, 5, -Math.sin(2 * pi * ti / b[3]))
326
+ df.set(i, 6, -b[7] * (2 * pi * ti / (b[6] * b[6])) * Math.sin(2 * pi * ti / b[6]) + b[8] * (2 * pi * ti / (b[6] * b[6])) * Math.cos(2 * pi * ti / b[6]))
327
+ df.set(i, 7, -Math.cos(2 * pi * ti / b[6]))
328
+ df.set(i, 8, -Math.sin(2 * pi * ti / b[6]))
329
+ }
330
+
331
+ GSL::SUCCESS
332
+ }
333
+
334
+ fdf = GSL::MultiFit::Function_fdf.alloc(enso_f, enso_df, enso_P)
335
+
336
+ #fdf.set_data(GSL::Vector.alloc(1..168), enso_F)
337
+ fdf.set_data(GSL::Vector.indgen(168, 1), enso_F)
338
+
339
+ _test_fdf('nist-ENSO', fdf, enso_x0, enso_x, enso_sumsq, enso_sigma)
340
+ end
341
+
342
+ def test_filip
343
+ filip_n = 82
344
+ filip_p = 11
345
+
346
+ filip_x = GSL::Vector.alloc(
347
+ -6.860120914, -4.324130045, -4.358625055, -4.358426747, -6.955852379,
348
+ -6.661145254, -6.355462942, -6.118102026, -7.115148017, -6.815308569,
349
+ -6.519993057, -6.204119983, -5.853871964, -6.109523091, -5.79832982,
350
+ -5.482672118, -5.171791386, -4.851705903, -4.517126416, -4.143573228,
351
+ -3.709075441, -3.499489089, -6.300769497, -5.953504836, -5.642065153,
352
+ -5.031376979, -4.680685696, -4.329846955, -3.928486195, -8.56735134,
353
+ -8.363211311, -8.107682739, -7.823908741, -7.522878745, -7.218819279,
354
+ -6.920818754, -6.628932138, -6.323946875, -5.991399828, -8.781464495,
355
+ -8.663140179, -8.473531488, -8.247337057, -7.971428747, -7.676129393,
356
+ -7.352812702, -7.072065318, -6.774174009, -6.478861916, -6.159517513,
357
+ -6.835647144, -6.53165267, -6.224098421, -5.910094889, -5.598599459,
358
+ -5.290645224, -4.974284616, -4.64454848, -4.290560426, -3.885055584,
359
+ -3.408378962, -3.13200249, -8.726767166, -8.66695597, -8.511026475,
360
+ -8.165388579, -7.886056648, -7.588043762, -7.283412422, -6.995678626,
361
+ -6.691862621, -6.392544977, -6.067374056, -6.684029655, -6.378719832,
362
+ -6.065855188, -5.752272167, -5.132414673, -4.811352704, -4.098269308,
363
+ -3.66174277, -3.2644011
364
+ )
365
+
366
+ filip_y = GSL::Vector.alloc(
367
+ 0.8116, 0.9072, 0.9052, 0.9039, 0.8053, 0.8377, 0.8667, 0.8809, 0.7975,
368
+ 0.8162, 0.8515, 0.8766, 0.8885, 0.8859, 0.8959, 0.8913, 0.8959, 0.8971,
369
+ 0.9021, 0.909, 0.9139, 0.9199, 0.8692, 0.8872, 0.89, 0.891, 0.8977,
370
+ 0.9035, 0.9078, 0.7675, 0.7705, 0.7713, 0.7736, 0.7775, 0.7841, 0.7971,
371
+ 0.8329, 0.8641, 0.8804, 0.7668, 0.7633, 0.7678, 0.7697, 0.77, 0.7749,
372
+ 0.7796, 0.7897, 0.8131, 0.8498, 0.8741, 0.8061, 0.846, 0.8751, 0.8856,
373
+ 0.8919, 0.8934, 0.894, 0.8957, 0.9047, 0.9129, 0.9209, 0.9219, 0.7739,
374
+ 0.7681, 0.7665, 0.7703, 0.7702, 0.7761, 0.7809, 0.7961, 0.8253, 0.8602,
375
+ 0.8809, 0.8301, 0.8664, 0.8834, 0.8898, 0.8964, 0.8963, 0.9074, 0.9119,
376
+ 0.9228
377
+ )
378
+
379
+ work = GSL::MultiFit::Workspace.alloc(filip_n, filip_p)
380
+
381
+ expected_c = GSL::Vector.alloc(
382
+ -1467.48961422980, -2772.17959193342, -2316.37108160893,
383
+ -1127.97394098372, -354.478233703349, -75.1242017393757,
384
+ -10.8753180355343, -1.06221498588947, -0.670191154593408e-01,
385
+ -0.246781078275479e-02, -0.402962525080404e-04
386
+ )
387
+
388
+ expected_sd = GSL::Vector.alloc(
389
+ 298.084530995537, 559.779865474950, 466.477572127796,
390
+ 227.204274477751, 71.6478660875927, 15.2897178747400,
391
+ 2.23691159816033, 0.221624321934227, 0.142363763154724e-01,
392
+ 0.535617408889821e-03, 0.896632837373868e-05
393
+ )
394
+
395
+ expected_chisq = 0.795851382172941e-03
396
+
397
+ xx = GSL::Matrix.alloc(filip_n, filip_p)
398
+
399
+ filip_n.times { |i|
400
+ filip_p.times { |j|
401
+ xx.set(i, j, GSL.pow(filip_x[i], j))
402
+ }
403
+ }
404
+
405
+ c, cov, chisq, _ = GSL::MultiFit.linear(xx, filip_y, work)
406
+
407
+ assert_rel c[0], expected_c[0], 1e-7, 'filip gsl_fit_multilinear c0'
408
+ assert_rel c[1], expected_c[1], 1e-7, 'filip gsl_fit_multilinear c1'
409
+ assert_rel c[2], expected_c[2], 1e-7, 'filip gsl_fit_multilinear c2'
410
+ assert_rel c[3], expected_c[3], 1e-7, 'filip gsl_fit_multilinear c3'
411
+ assert_rel c[4], expected_c[4], 1e-7, 'filip gsl_fit_multilinear c4'
412
+ assert_rel c[5], expected_c[5], 1e-7, 'filip gsl_fit_multilinear c5'
413
+ assert_rel c[6], expected_c[6], 1e-7, 'filip gsl_fit_multilinear c6'
414
+ assert_rel c[7], expected_c[7], 1e-7, 'filip gsl_fit_multilinear c7'
415
+ assert_rel c[8], expected_c[8], 1e-7, 'filip gsl_fit_multilinear c8'
416
+ assert_rel c[9], expected_c[9], 1e-7, 'filip gsl_fit_multilinear c9'
417
+ assert_rel c[10], expected_c[10], 1e-7, 'filip gsl_fit_multilinear c10'
418
+
419
+ diag = cov.diagonal
420
+
421
+ assert_rel diag[0], GSL.pow(expected_sd[0],2.0), 1e-6, 'filip gsl_fit_multilinear cov00'
422
+ assert_rel diag[1], GSL.pow(expected_sd[1],2.0), 1e-6, 'filip gsl_fit_multilinear cov11'
423
+ assert_rel diag[2], GSL.pow(expected_sd[2],2.0), 1e-6, 'filip gsl_fit_multilinear cov22'
424
+ assert_rel diag[3], GSL.pow(expected_sd[3],2.0), 1e-6, 'filip gsl_fit_multilinear cov33'
425
+ assert_rel diag[4], GSL.pow(expected_sd[4],2.0), 1e-6, 'filip gsl_fit_multilinear cov44'
426
+ assert_rel diag[5], GSL.pow(expected_sd[5],2.0), 1e-6, 'filip gsl_fit_multilinear cov55'
427
+ assert_rel diag[6], GSL.pow(expected_sd[6],2.0), 1e-6, 'filip gsl_fit_multilinear cov66'
428
+ assert_rel diag[7], GSL.pow(expected_sd[7],2.0), 1e-6, 'filip gsl_fit_multilinear cov77'
429
+ assert_rel diag[8], GSL.pow(expected_sd[8],2.0), 1e-6, 'filip gsl_fit_multilinear cov88'
430
+ assert_rel diag[9], GSL.pow(expected_sd[9],2.0), 1e-6, 'filip gsl_fit_multilinear cov99'
431
+ assert_rel diag[10], GSL.pow(expected_sd[10],2.0), 1e-6, 'filip gsl_fit_multilinear cov1010'
432
+
433
+ assert_rel chisq, expected_chisq, 1e-7, 'filip gsl_fit_multilinear chisq'
434
+
435
+ expected_c = GSL::Vector.alloc(
436
+ -1467.48961422980, -2772.17959193342, -2316.37108160893, -1127.97394098372,
437
+ -354.478233703349, -75.1242017393757, -10.8753180355343, -1.06221498588947,
438
+ -0.670191154593408e-01, -0.246781078275479e-02, -0.402962525080404e-04
439
+ )
440
+
441
+ expected_cov = GSL::Matrix.alloc(
442
+ [ 7.9269341767252183262588583867942e9, 1.4880416622254098343441063389706e10,
443
+ 1.2385811858111487905481427591107e10, 6.0210784406215266653697715794241e9,
444
+ 1.8936652526181982747116667336389e9, 4.0274900618493109653998118587093e8,
445
+ 5.8685468011819735806180092394606e7, 5.7873451475721689084330083708901e6,
446
+ 3.6982719848703747920663262917032e5, 1.3834818802741350637527054170891e4,
447
+ 2.301758578713219280719633494302e2 ],
448
+ [ 1.4880416622254098334697515488559e10, 2.7955091668548290835529555438088e10,
449
+ 2.3286604504243362691678565997033e10, 1.132895006796272983689297219686e10,
450
+ 3.5657281653312473123348357644683e9, 7.5893300392314445528176646366087e8,
451
+ 1.1066654886143524811964131660002e8, 1.0921285448484575110763947787775e7,
452
+ 6.9838139975394769253353547606971e5, 2.6143091775349597218939272614126e4,
453
+ 4.3523386330348588614289505633539e2 ],
454
+ [ 1.2385811858111487890788272968677e10, 2.3286604504243362677757802422747e10,
455
+ 1.9412787917766676553608636489674e10, 9.4516246492862131849077729250098e9,
456
+ 2.9771226694709917550143152097252e9, 6.3413035086730038062129508949859e8,
457
+ 9.2536164488309401636559552742339e7, 9.1386304643423333815338760248027e6,
458
+ 5.8479478338916429826337004060941e5, 2.1905933113294737443808429764554e4,
459
+ 3.6493161325305557266196635180155e2 ],
460
+ [ 6.0210784406215266545770691532365e9, 1.1328950067962729823273441573365e10,
461
+ 9.4516246492862131792040001429636e9, 4.6053152992000107509329772255094e9,
462
+ 1.4517147860312147098138030287038e9, 3.0944988323328589376402579060072e8,
463
+ 4.5190223822292688669369522708712e7, 4.4660958693678497534529855690752e6,
464
+ 2.8599340736122198213681258676423e5, 1.0720394998549386596165641244705e4,
465
+ 1.7870937745661967319298031044424e2 ],
466
+ [ 1.8936652526181982701620450132636e9, 3.5657281653312473058825073094524e9,
467
+ 2.9771226694709917514149924058297e9, 1.451714786031214708936087401632e9,
468
+ 4.5796563896564815123074920050827e8, 9.7693972414561515534525103622773e7,
469
+ 1.427717861635658545863942948444e7, 1.4120161287735817621354292900338e6,
470
+ 9.0484361228623960006818614875557e4, 3.394106783764852373199087455398e3,
471
+ 5.6617406468519495376287407526295e1 ],
472
+ [ 4.0274900618493109532650887473599e8, 7.589330039231444534478894935778e8,
473
+ 6.3413035086730037947153564986653e8, 3.09449883233285893390542947998e8,
474
+ 9.7693972414561515475770399055121e7, 2.0855726248311948992114244257719e7,
475
+ 3.0501263034740400533872858749566e6, 3.0187475839310308153394428784224e5,
476
+ 1.9358204633534233524477930175632e4, 7.2662989867560017077361942813911e2,
477
+ 1.2129002231061036467607394277965e1 ],
478
+ [ 5.868546801181973559370854830868e7, 1.1066654886143524778548044386795e8,
479
+ 9.2536164488309401413296494869777e7, 4.5190223822292688587853853162072e7,
480
+ 1.4277178616356585441556046753562e7, 3.050126303474040051574715592746e6,
481
+ 4.4639982579046340884744460329946e5, 4.4212093985989836047285007760238e4,
482
+ 2.8371395028774486687625333589972e3, 1.0656694507620102300567296504381e2,
483
+ 1.7799982046359973175080475654123e0 ],
484
+ [ 5.7873451475721688839974153925406e6, 1.0921285448484575071271480643397e7,
485
+ 9.1386304643423333540728480344578e6, 4.4660958693678497427674903565664e6,
486
+ 1.4120161287735817596182229182587e6, 3.0187475839310308117812257613082e5,
487
+ 4.4212093985989836021482392757677e4, 4.3818874017028389517560906916315e3,
488
+ 2.813828775753142855163154605027e2, 1.0576188138416671883232607188969e1,
489
+ 1.7676976288918295012452853715408e-1 ],
490
+ [ 3.6982719848703747742568351456818e5, 6.9838139975394768959780068745979e5,
491
+ 5.8479478338916429616547638954781e5, 2.8599340736122198128717796825489e5,
492
+ 9.0484361228623959793493985226792e4, 1.9358204633534233490579641064343e4,
493
+ 2.8371395028774486654873647731797e3, 2.8138287757531428535592907878017e2,
494
+ 1.8081118503579798222896804627964e1, 6.8005074291434681866415478598732e-1,
495
+ 1.1373581557749643543869665860719e-2 ],
496
+ [ 1.3834818802741350562839757244708e4, 2.614309177534959709397445440919e4,
497
+ 2.1905933113294737352721470167247e4, 1.0720394998549386558251721913182e4,
498
+ 3.3941067837648523632905604575131e3, 7.2662989867560016909534954790835e2,
499
+ 1.0656694507620102282337905013451e2, 1.0576188138416671871337685672492e1,
500
+ 6.8005074291434681828743281967838e-1, 2.5593857187900736057022477529078e-2,
501
+ 4.2831487599116264442963102045936e-4 ],
502
+ [ 2.3017585787132192669801658674163e2, 4.3523386330348588381716460685124e2,
503
+ 3.6493161325305557094116270974735e2, 1.7870937745661967246233792737255e2,
504
+ 5.6617406468519495180024059284629e1, 1.2129002231061036433003571679329e1,
505
+ 1.7799982046359973135014027410646e0, 1.7676976288918294983059118597214e-1,
506
+ 1.137358155774964353146460100337e-2, 4.283148759911626442000316269063e-4,
507
+ 7.172253875245080423800933453952e-6 ]
508
+ )
509
+
510
+ expected_chisq = 0.795851382172941E-03
511
+
512
+ filip_n.times { |i|
513
+ filip_p.times { |j|
514
+ xx.set(i, j, GSL.pow(filip_x[i], j))
515
+ }
516
+ }
517
+
518
+ w = GSL::Vector.alloc(filip_n)
519
+ w.set_all(1.0)
520
+
521
+ c, cov, _, _ = GSL::MultiFit.wlinear(xx, w, filip_y, work)
522
+
523
+ filip_p.times { |i|
524
+ assert_rel c[i], expected_c[i], 1e-7, "filip gsl_fit_multilinear c#{i}"
525
+ }
526
+
527
+ filip_p.times { |i|
528
+ filip_p.times { |j|
529
+ assert_rel cov[i, j], expected_cov[i, j], 1e-6, "filip gsl_fit_wmultilinear cov(#{i},#{j})"
530
+ }
531
+ }
532
+ end
533
+
534
+ def test_gauss
535
+ maxiter = 10
536
+ n = 1000
537
+
538
+ # model: a * exp(-(x - x0) ** 2 / 2 / sigma ** 2)
539
+ gauss_p = 3
540
+ gauss_f = lambda { |x, t, y, s, f|
541
+ # x: parameters as a Vecor
542
+ # t: observed points as a GSL::Vector
543
+ # y: observed data as a GSL::Vector
544
+ # s: errorbar
545
+ # f: result
546
+ a = x[0]
547
+ x0 = x[1]
548
+ sigma2 = x[2] ** 2
549
+
550
+ y.size.times { |i|
551
+ f.set(i, (a * Math.exp(-(t[i] - x0) ** 2 / 2 / sigma2) - y[i]) / s[i])
552
+ }
553
+
554
+ GSL::SUCCESS
555
+ }
556
+
557
+ gauss_df = lambda { |x, t, y, s, df|
558
+ a = x[0]
559
+ x0 = x[1]
560
+ sigma = x[2]
561
+ sigma2 = sigma ** 2
562
+
563
+ y.size.times { |i|
564
+ dx = t[i] - x0
565
+ dx2 = dx ** 2
566
+ f = a * Math.exp(-dx2 / 2 / sigma2)
567
+
568
+ df.set(i, 0, f / a / s[i])
569
+ df.set(i, 1, f * dx / sigma2 / s[i])
570
+ df.set(i, 2, f * dx2 / sigma2 / sigma / s[i])
571
+ }
572
+
573
+ GSL::SUCCESS
574
+ }
575
+
576
+ # goal
577
+ xgoal = GSL::Vector.alloc([1, 0, 1])
578
+ parname = %w[a x0 si]
579
+
580
+ # data
581
+ t = GSL::Vector.alloc(n) # positions of data
582
+ tmin = -10.0
583
+ tmax = 10.0
584
+
585
+ t.size.times { |i|
586
+ t[i] = tmin + (tmax - tmin) * i / (n - 1)
587
+ }
588
+
589
+ stdev = xgoal[0] * 0.1
590
+
591
+ s = GSL::Vector.alloc(Array.new(t.size, stdev)) # error bar of each datum
592
+ r = GSL::Rng.alloc
593
+ e = GSL::Vector.alloc(t.size)
594
+
595
+ t.size.times { |i|
596
+ e[i] = -r.gaussian(stdev) # perturbation to data
597
+ }
598
+
599
+ y = GSL::Vector.alloc(t.size)
600
+ n = GSL::Vector.alloc(Array.new(t.size, 1.0))
601
+ gauss_f.call(xgoal, t, e, n, y) # data: y = model - e
602
+
603
+ # fitting
604
+ x = GSL::Vector.alloc([0.5, 0.1, 2]) # initial guess
605
+
606
+ fdf = GSL::MultiFit::Function_fdf.alloc(gauss_f, gauss_df, gauss_p)
607
+ fdf.set_data(t, y, s)
608
+
609
+ solver = GSL::MultiFit::FdfSolver.alloc(GSL::MultiFit::FdfSolver::LMSDER, t.size, gauss_p)
610
+ solver.set(fdf, x)
611
+
612
+ #solver.print_state(0)
613
+
614
+ maxiter.times { |i|
615
+ solver.iterate
616
+
617
+ status = solver.test_delta(1e-6, 1e-6)
618
+ #solver.print_state(i + 1)
619
+
620
+ break if status != GSL::CONTINUE
621
+ }
622
+
623
+ # results
624
+ covar = solver.covar(0.0)
625
+ xresult = solver.position
626
+ dof = t.size - gauss_p
627
+ chi2 = GSL.pow_2(solver.f.dnrm2)
628
+ xsigma = GSL::Vector.alloc(xresult.size)
629
+
630
+ xresult.size.times { |i|
631
+ xsigma[i] = Math.sqrt(chi2 / dof * covar[i, i]) * 2.0
632
+ # resulting parameters to differ two times than standard error
633
+ }
634
+
635
+ desc = 'a*exp(-(x-x0)**2/2/si**2), chi2/N:%.3g' % (chi2 / t.size)
636
+
637
+ xresult.size.times { |i|
638
+ assert_rel xresult[i], xgoal[i], xsigma[i], '%s: %-2.2s' % [desc, parname[i]]
639
+ refute((xresult[i] - xgoal[i]).abs > xsigma[i], desc)
640
+ }
641
+ end
642
+
643
+ def test_longley
644
+ longley_n = 16
645
+ longley_p = 7
646
+
647
+ longley_x = GSL::Vector.alloc(
648
+ 1, 83.0, 234289, 2356, 1590, 107608, 1947,
649
+ 1, 88.5, 259426, 2325, 1456, 108632, 1948,
650
+ 1, 88.2, 258054, 3682, 1616, 109773, 1949,
651
+ 1, 89.5, 284599, 3351, 1650, 110929, 1950,
652
+ 1, 96.2, 328975, 2099, 3099, 112075, 1951,
653
+ 1, 98.1, 346999, 1932, 3594, 113270, 1952,
654
+ 1, 99.0, 365385, 1870, 3547, 115094, 1953,
655
+ 1, 100.0, 363112, 3578, 3350, 116219, 1954,
656
+ 1, 101.2, 397469, 2904, 3048, 117388, 1955,
657
+ 1, 104.6, 419180, 2822, 2857, 118734, 1956,
658
+ 1, 108.4, 442769, 2936, 2798, 120445, 1957,
659
+ 1, 110.8, 444546, 4681, 2637, 121950, 1958,
660
+ 1, 112.6, 482704, 3813, 2552, 123366, 1959,
661
+ 1, 114.2, 502601, 3931, 2514, 125368, 1960,
662
+ 1, 115.7, 518173, 4806, 2572, 127852, 1961,
663
+ 1, 116.9, 554894, 4007, 2827, 130081, 1962
664
+ )
665
+
666
+ longley_y = GSL::Vector.alloc(
667
+ 60323, 61122, 60171, 61187, 63221, 63639, 64989, 63761,
668
+ 66019, 67857, 68169, 66513, 68655, 69564, 69331, 70551
669
+ )
670
+
671
+ work = GSL::MultiFit::Workspace.alloc(longley_n, longley_p)
672
+
673
+ x = GSL::Matrix.alloc(longley_x, longley_n, longley_p).view
674
+ y = longley_y.view
675
+
676
+ expected_c = GSL::Vector.alloc(
677
+ -3482258.63459582,
678
+ 15.0618722713733,
679
+ -0.358191792925910e-01,
680
+ -2.02022980381683,
681
+ -1.03322686717359,
682
+ -0.511041056535807e-01,
683
+ 1829.15146461355
684
+ )
685
+
686
+ expected_sd = GSL::Vector.alloc(
687
+ 890420.383607373,
688
+ 84.9149257747669,
689
+ 0.334910077722432e-01,
690
+ 0.488399681651699,
691
+ 0.214274163161675,
692
+ 0.226073200069370,
693
+ 455.478499142212
694
+ )
695
+
696
+ expected_chisq = 836424.055505915
697
+
698
+ c, cov, chisq, _ = GSL::MultiFit.linear(x, y, work)
699
+
700
+ 7.times { |i|
701
+ assert_rel c[i], expected_c[i], 1e-10, "longley gsl_fit_multilinear c#{i}"
702
+ }
703
+
704
+ diag = cov.diagonal
705
+
706
+ assert_rel diag[0], GSL.pow(expected_sd[0],2.0), 1e-10, 'longley gsl_fit_multilinear cov00'
707
+ assert_rel diag[1], GSL.pow(expected_sd[1],2.0), 1e-10, 'longley gsl_fit_multilinear cov11'
708
+ assert_rel diag[2], GSL.pow(expected_sd[2],2.0), 1e-10, 'longley gsl_fit_multilinear cov22'
709
+ assert_rel diag[3], GSL.pow(expected_sd[3],2.0), 1e-10, 'longley gsl_fit_multilinear cov33'
710
+ assert_rel diag[4], GSL.pow(expected_sd[4],2.0), 1e-10, 'longley gsl_fit_multilinear cov44'
711
+ assert_rel diag[5], GSL.pow(expected_sd[5],2.0), 1e-10, 'longley gsl_fit_multilinear cov55'
712
+ assert_rel diag[6], GSL.pow(expected_sd[6],2.0), 1e-10, 'longley gsl_fit_multilinear cov66'
713
+
714
+ assert_rel chisq, expected_chisq, 1e-10, 'longley gsl_fit_multilinear chisq'
715
+
716
+ expected_cov = GSL::Matrix.alloc(
717
+ [ 8531122.56783558, -166.727799925578, 0.261873708176346, 3.91188317230983,
718
+ 1.1285582054705, -0.889550869422687, -4362.58709870581 ],
719
+ [ -166.727799925578, 0.0775861253030891, -1.98725210399982e-05, -0.000247667096727256,
720
+ -6.82911920718824e-05, 0.000136160797527761, 0.0775255245956248 ],
721
+ [ 0.261873708176346, -1.98725210399982e-05, 1.20690316701888e-08, 1.66429546772984e-07,
722
+ 3.61843600487847e-08, -6.78805814483582e-08, -0.00013158719037715 ],
723
+ [ 3.91188317230983, -0.000247667096727256, 1.66429546772984e-07, 2.56665052544717e-06,
724
+ 6.96541409215597e-07, -9.00858307771567e-07, -0.00197260370663974 ],
725
+ [ 1.1285582054705, -6.82911920718824e-05, 3.61843600487847e-08, 6.96541409215597e-07,
726
+ 4.94032602583969e-07, -9.8469143760973e-08, -0.000576921112208274 ],
727
+ [ -0.889550869422687, 0.000136160797527761, -6.78805814483582e-08, -9.00858307771567e-07,
728
+ -9.8469143760973e-08, 5.49938542664952e-07, 0.000430074434198215 ],
729
+ [ -4362.58709870581, 0.0775255245956248, -0.00013158719037715, -0.00197260370663974,
730
+ -0.000576921112208274, 0.000430074434198215, 2.23229587481535 ]
731
+ )
732
+
733
+ expected_chisq = 836424.055505915
734
+
735
+ w = GSL::Vector.alloc(longley_n)
736
+ w.set_all(1.0)
737
+
738
+ c, cov, chisq, _ = GSL::MultiFit.wlinear(x, w, y, work)
739
+
740
+ 7.times { |i|
741
+ assert_rel c[i], expected_c[i], 1e-10, "longley gsl_fit_wmultilinear c#{i}"
742
+ }
743
+
744
+ longley_p.times { |i|
745
+ longley_p.times { |j|
746
+ assert_rel cov[i, j], expected_cov[i, j], 1e-7, "longley gsl_fit_wmultilinear cov(#{i},#{j})"
747
+ }
748
+ }
749
+
750
+ assert_rel chisq, expected_chisq, 1e-10, 'longley gsl_fit_wmultilinear chisq'
751
+ end
752
+
753
+ end