stick 1.2.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.
Files changed (56) hide show
  1. data/CHANGES +7 -0
  2. data/COPYING +344 -0
  3. data/README +110 -0
  4. data/lib/stick/constants.rb +3 -0
  5. data/lib/stick/constants/cgs.rb +151 -0
  6. data/lib/stick/constants/mks.rb +158 -0
  7. data/lib/stick/constants/number.rb +33 -0
  8. data/lib/stick/constants/typeless_cgs.rb +141 -0
  9. data/lib/stick/constants/typeless_mks.rb +142 -0
  10. data/lib/stick/currency.rb +8 -0
  11. data/lib/stick/mapcar.rb +61 -0
  12. data/lib/stick/matrix.rb +1022 -0
  13. data/lib/stick/quaternion.rb +562 -0
  14. data/lib/stick/times.rb +441 -0
  15. data/lib/stick/units.rb +112 -0
  16. data/lib/stick/units/base.rb +980 -0
  17. data/lib/stick/units/currency.rb +159 -0
  18. data/lib/stick/units/data/binary/base.rb +4 -0
  19. data/lib/stick/units/data/cex.rb +5 -0
  20. data/lib/stick/units/data/currency-default.rb +5 -0
  21. data/lib/stick/units/data/currency-standard.rb +2 -0
  22. data/lib/stick/units/data/currency/base.rb +89 -0
  23. data/lib/stick/units/data/iec.rb +5 -0
  24. data/lib/stick/units/data/iec_binary/base.rb +6 -0
  25. data/lib/stick/units/data/si.rb +7 -0
  26. data/lib/stick/units/data/si/base.rb +9 -0
  27. data/lib/stick/units/data/si/derived.rb +26 -0
  28. data/lib/stick/units/data/si/extra.rb +22 -0
  29. data/lib/stick/units/data/uk.rb +10 -0
  30. data/lib/stick/units/data/uk/base.rb +22 -0
  31. data/lib/stick/units/data/units-default.rb +11 -0
  32. data/lib/stick/units/data/units-standard.rb +5 -0
  33. data/lib/stick/units/data/us.rb +10 -0
  34. data/lib/stick/units/data/us/base.rb +23 -0
  35. data/lib/stick/units/data/xmethods.rb +5 -0
  36. data/lib/stick/units/data/xmethods/cached.rb +84 -0
  37. data/lib/stick/units/data/xmethods/mapping.rb +87 -0
  38. data/lib/stick/units/loaders.rb +98 -0
  39. data/lib/stick/units/units.rb +109 -0
  40. data/meta/MANIFEST +76 -0
  41. data/meta/ROLLRC +2 -0
  42. data/meta/icli.yaml +16 -0
  43. data/meta/project.yaml +18 -0
  44. data/task/clobber/package +10 -0
  45. data/task/publish +57 -0
  46. data/task/release +10 -0
  47. data/task/setup +1616 -0
  48. data/task/test +25 -0
  49. data/test/spec_matrix.rb +342 -0
  50. data/test/test_currency.rb +26 -0
  51. data/test/test_matrix.rb +359 -0
  52. data/test/test_units.rb +205 -0
  53. data/work/TODO +20 -0
  54. data/work/bytes.rb +231 -0
  55. data/work/multipliers.rb +195 -0
  56. metadata +138 -0
data/task/test ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ratch
2
+
3
+ # Run unit tests
4
+
5
+ live = ARGV.delete('--live')
6
+
7
+ unless live
8
+ $LOAD_PATH.unshift(File.expand_path('lib'))
9
+ end
10
+
11
+ main :test do
12
+ if find = argv[0]
13
+ unless file?(find)
14
+ find = File.join(find, '**', 'test_*.rb')
15
+ end
16
+ else
17
+ find = 'test/**/test_*.rb'
18
+ end
19
+
20
+ Dir.glob(find).each do |file|
21
+ next if dir?(file)
22
+ load file
23
+ end
24
+ end
25
+
@@ -0,0 +1,342 @@
1
+ require 'stick/matrix'
2
+
3
+ describe "Vector class extension:" do
4
+ setup do
5
+ @v = Vector.[](1, 2, 3, 4)
6
+ end
7
+
8
+ it "[] method" do
9
+ v = @v.clone
10
+ v[1..2] = Vector[9, 9, 9, 9, 9]
11
+ v.should == Vector[1, 9, 9, 4]
12
+ end
13
+
14
+ it "Vector.concat method" do
15
+ Vector.concat(Vector[1], Vector[2, 3]).should == Vector[1, 2, 3]
16
+ Vector.concat(Vector[1], Vector[2, 3], Vector[4, 5]).should == Vector[1, 2, 3, 4, 5]
17
+ end
18
+
19
+ it "collect method" do
20
+ @v.collect!{|i| i * i}.should == Vector.[](1, 4, 9, 16)
21
+ @v.collect!{|i| 3 * i}.should == Vector.[](3, 12, 27, 48)
22
+ end
23
+
24
+ it "each method" do
25
+ r = []
26
+ @v.each{|i| r << i+1}
27
+ r.should == [2, 3, 4, 5]
28
+ end
29
+
30
+ it "max element" do
31
+ @v.max.should == 4
32
+ end
33
+
34
+ it "max element" do
35
+ @v.min.should == 1
36
+ end
37
+
38
+ it "norm method" do
39
+ v = Vector.[](3, 4)
40
+ v.norm.should == 5
41
+ end
42
+
43
+ it "method p_norm(1)" do
44
+ v = Vector.[](3, 4)
45
+ v.norm(1).should == 7
46
+ end
47
+
48
+ it "method p_norm(2)" do
49
+ v = Vector.[](3, 4)
50
+ v.norm(2).should == 5
51
+ end
52
+
53
+ it "method p_norm(3)" do
54
+ v = Vector.[](3, 4)
55
+ v.norm(3).should > 4.49
56
+ v.norm(3).should < 4.50
57
+ end
58
+
59
+ it "method p_norm(4)" do
60
+ v = Vector.[](3, 4)
61
+ v.norm(4).should > 4.28
62
+ v.norm(4).should < 4.29
63
+ end
64
+
65
+ it "[]= method" do
66
+ @v[3] = 10
67
+ @v.should == Vector.[](1, 2, 3, 10)
68
+ end
69
+
70
+ it "norm_inf" do
71
+ @v.norm_inf.should == 4
72
+ end
73
+ end
74
+
75
+ describe "Matrix class extension:" do
76
+ setup do
77
+ @m = Matrix[[1, 2, 222], [2, 33, 4]]
78
+ end
79
+
80
+ it "[] method" do
81
+ m = Matrix.new(4, 3){|i, j| i * 3 + j}
82
+ m[1, 2].should == 5
83
+ m[3, 1..2].should == Vector[10, 11]
84
+ m[0..1, 0..2].should == Matrix[[0, 1, 2], [3, 4, 5]]
85
+ end
86
+
87
+ it "[]= method" do
88
+ m = Matrix.new(3, 3){|i, j| i * 3 + j}
89
+ m[1, 2] = 9
90
+ m.should == Matrix[[0, 1, 2], [3, 4, 9], [6, 7, 8]]
91
+ m[1, 1..2] = Vector[8, 8]
92
+ m.should == Matrix[[0, 1, 2], [3, 8, 8], [6, 7, 8]]
93
+ m[0..1, 0..1] = Matrix[[0, 0, 0], [0, 0, 0]]
94
+ m.should == Matrix[[0, 0, 2], [0, 0, 8], [6, 7, 8]]
95
+ end
96
+
97
+ it "set method" do
98
+ n = Matrix.new(2, 3)
99
+ n.set(@m)
100
+ n.should == @m
101
+ end
102
+
103
+ it "set method and wrap value" do
104
+ @m.wrap = :torus
105
+ n = Matrix.new(2, 3)
106
+ n.set(@m)
107
+ n.wrap.should == :torus
108
+ end
109
+
110
+ it "wrap method" do
111
+ @m.wrap=:torus
112
+ @m[2, 3].should == 1
113
+ @m.wrap=:h_cylinder
114
+ @m[2, 0].should == 1
115
+ @m.wrap=:v_cylinder
116
+ @m[0, 3].should == 1
117
+ end
118
+
119
+ it "maximum length of a column" do
120
+ @m.max_len_column(1).should == 2
121
+ end
122
+
123
+ it "list of maximum lengths of columns" do
124
+ @m.cols_len.should == [1, 2, 3]
125
+ end
126
+
127
+ it "matrix each method" do
128
+ r = []
129
+ @m.each{|x| r << x + 3}
130
+ r.should == [4, 5, 225, 5, 36, 7]
131
+
132
+ s = 0
133
+ @m.each{|x| s += x}
134
+ s.should == 264
135
+ end
136
+
137
+ it "row! method" do
138
+ @m.row!(0){|x| x+x}.should == [2, 4, 444]
139
+ @m.should == Matrix[[2, 4, 444], [2, 33, 4]]
140
+ end
141
+
142
+ it "row_collect method" do
143
+ @m.row_collect(1){|x| x+10}.should == [12, 43, 14]
144
+ end
145
+
146
+ it "column_collect method" do
147
+ @m.column_collect(0){|x| x*3}.should == [3, 6]
148
+ end
149
+
150
+ it "row_collect! method, identicaly with row!" do
151
+ @m.row_collect!(0){|x| x+x}.should == [2, 4, 444]
152
+ @m.should == Matrix[[2, 4, 444], [2, 33, 4]]
153
+ end
154
+
155
+ it "column_collect! method" do
156
+ @m.column_collect!(2){|x| x+10}.should == [232, 14]
157
+ @m.should == Matrix[[1, 2, 232], [2, 33, 14]]
158
+ end
159
+
160
+ it "column= " do
161
+ m = Matrix.new(3, 3){|i, j| i * 3 + j + 1}
162
+ m.column= 1, Vector[1,1,1,1,1,1]
163
+ m.should == Matrix[[1, 1, 3],[4, 1, 6],[7, 1, 9]]
164
+ m.column= 2, Vector[9,9], 0..1
165
+ m.should == Matrix[[1, 1, 9],[4, 1, 9],[7, 1, 9]]
166
+ end
167
+
168
+ it "row= " do
169
+ m = Matrix.new(3, 3){|i, j| i * 3 + j + 1}
170
+ m.row= 1, Vector[1,1,1,1,1,1]
171
+ m.should == Matrix[[1, 2, 3],[1, 1, 1],[7, 8, 9]]
172
+ m.row= 2, Vector[9,9], 0..2
173
+ m.should == Matrix[[1, 2, 3],[1, 1, 1],[9, 9, 0]]
174
+ end
175
+
176
+ it "norm of a matrix" do
177
+ m = Matrix[[1, 2, 3], [1, 2, 3]]
178
+ m.norm.should == Math.sqrt(28)
179
+ end
180
+
181
+ it "test empty matrix" do
182
+ @m.empty?.should == false
183
+ n = Matrix[]
184
+ n.empty?.should == true
185
+ end
186
+
187
+ it "row2matrix" do
188
+ m = Matrix.new(4, 3){|i, j| i * 3 + j + 1}
189
+ m.row2matrix(1..2).should == Matrix[[4, 5, 6],[7, 8, 9]]
190
+ m.row2matrix(2).should == Matrix[[7, 8, 9]]
191
+ m.row2matrix(0..4).should == m
192
+ end
193
+
194
+ it "column2matrix" do
195
+ m = Matrix.new(4, 3){|i, j| i * 3 + j + 1}
196
+ m.column2matrix(1).should == Matrix[[2], [5], [8], [11]]
197
+ end
198
+
199
+ it "diag" do
200
+ m1 = Matrix[[1]]
201
+ m2 = Matrix[[2, 0], [0, 3]]
202
+ m3 = Matrix[[4, 0, 0], [0, 5, 0], [0, 0, 6]]
203
+ a1 = Matrix.new(6, 6){|i, j| i == j ? i + 1: 0}
204
+ Matrix.diag(m1, m2, m3).should == a1
205
+ Matrix.diag(m2).should == m2
206
+ a2 = Matrix[[2, 0, 0, 0],
207
+ [0, 3, 0, 0],
208
+ [0, 0, 2, 0],
209
+ [0, 0, 0, 3]]
210
+ Matrix.diag(m2, m2).should == a2
211
+ end
212
+
213
+ it "equal_in_delta" do
214
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
215
+ Matrix.equal_in_delta?(m, m).should == true
216
+ mm = m.clone
217
+ mm[0,0] += 2
218
+ Matrix.equal_in_delta?(m, mm, 0.001).should == false
219
+ Matrix.equal_in_delta?(m, mm, 2).should == true
220
+ end
221
+
222
+ it "diag_in_delta" do
223
+ Matrix.diag_in_delta?(Matrix.I(5), Matrix.new(4, 4){|i, j| i + j}).should == false
224
+ m = Matrix.new(5, 5){|i, j| i == j ? 1 + 0.001 * (i+1) : i + j}
225
+ Matrix.diag_in_delta?(Matrix.I(5), m, 0.01).should == true
226
+ end
227
+
228
+ it "LU " do
229
+ m = Matrix[[1, 4, 7],
230
+ [2, 5, 8],
231
+ [3, 6, 10]]
232
+ l = Matrix[[1, 0, 0],[2, 1, 0],[3, 2, 1]]
233
+ m.L.should == l
234
+ u = Matrix[[1, 4, 7],[0, -3, -6],[0, 0, 1]]
235
+ m.U.should == u
236
+ end
237
+
238
+ it "L " do
239
+ # e.g.: MC, Golub, 3.2 LU factorization, pg 94
240
+ m = Matrix[[3, 5],
241
+ [6, 7]]
242
+ l = Matrix[[1, 0],
243
+ [2, 1]]
244
+ m.L.should == l
245
+ end
246
+
247
+ it "U " do
248
+ # e.g.: MC, Golub, 3.2 LU factorization, pg 94
249
+ m = Matrix[[3, 5],
250
+ [6, 7]]
251
+ u = Matrix[[3, 5],
252
+ [0, -3]]
253
+ m.U.should == u
254
+ end
255
+
256
+ it "houseQR " do
257
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
258
+ Matrix.equal_in_delta?(m, m.houseQ * m.houseR).should == true
259
+ q = Matrix[[0.0776, 0.8330, 0.5329, 0.1264],
260
+ [0.3104, 0.4512, -0.8048, 0.2286],
261
+ [0.5433, 0.0694, 0.0108, -0.8365],
262
+ [0.7761, -0.3123, 0.2610, 0.4815]]
263
+ Matrix.equal_in_delta?(m.houseQ, q, 0.0001).should == true
264
+ end
265
+
266
+ it "houseR " do
267
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
268
+ r = Matrix[[12.88409, 14.59162, 16.29916],
269
+ [ 0, 1.04131, 2.082630],
270
+ [ 0, 0, 0],
271
+ [ 0, 0, 0]]
272
+ Matrix.equal_in_delta?(r, m.houseR, 1.0e-5).should == true
273
+ end
274
+
275
+ it "bidiagonalization" do
276
+ # MC, Golub, p252, Example 5.4.2
277
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
278
+ bidiag = Matrix[[12.884, 21.876, 0 ],
279
+ [0, 2.246, -0.613],
280
+ [0, 0, 0 ],
281
+ [0, 0, 0 ]]
282
+ Matrix.equal_in_delta?(bidiag, m.bidiagonal, 0.001).should == true
283
+ end
284
+
285
+ it "gram_schmidt" do
286
+ m = Matrix[[1, 1],
287
+ [0.001, 0],
288
+ [0, 0.001]]
289
+ gsQ = Matrix[[ 1, 0],
290
+ [0.001, -0.707107],
291
+ [ 0, 0.707100]]
292
+ Matrix.equal_in_delta?(gsQ, m.gram_schmidt[0], 0.001).should == true
293
+ Matrix.equal_in_delta?(m,m.gram_schmidt[0] * m.gram_schmidt[1], 1.0e-5).should == true
294
+ end
295
+
296
+ it "givens " do
297
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
298
+ Matrix.equal_in_delta?(m, m.givensQ * m.givensR, 0.001).should == true
299
+ end
300
+
301
+ it "hessenbergQR " do
302
+ hess = Matrix[[1, 2, 1, 2, 1],
303
+ [1, 3, 2, 3, 4],
304
+ [0, 2, 4, 3, 5],
305
+ [0, 0, 1, 4, 3],
306
+ [0, 0, 0, 6, 1]]
307
+ hessR = hess.hessenbergR
308
+ r = Matrix[[1.41421, 3.53553, 2.12132, 3.53553, 3.53553],
309
+ [ 0, -2.12132, -4.00693, -3.06412, -5.42115],
310
+ [ 0, 0, -1.20185, -3.51310, -2.31125],
311
+ [ 0, 0, 0, -6.30628, -1.54912],
312
+ [ 0, 0, 0, 0, 1.53929]]
313
+
314
+ Matrix.equal_in_delta?(r, hessR, 1.0e-5).should == true
315
+ Matrix.equal_in_delta?(hessR, hess.hessenbergQ.t * hess, 1.0e-5).should == true
316
+ end
317
+
318
+ it "hessenberg_form " do
319
+ m = Matrix[[1, 5, 7],[3, 0, 6],[4, 3, 1]]
320
+ h = Matrix[[1, 8.6, -0.2],[5, 4.96, -0.72],[0, 2.28, -3.96]]
321
+ Matrix.equal_in_delta?(h, m.hessenberg_form_H, 0.001).should == true
322
+ end
323
+
324
+ it "realSchur" do
325
+ m = Matrix.new(3, 3){1} + Matrix.diagonal(2, 2, 2)
326
+ e = Matrix[[5, 0, 0],[0, 2, 0],[0, 0, 2]]
327
+ Matrix.diag_in_delta?(m.realSchur, e, 1.0e-5).should == true
328
+ end
329
+
330
+ it "Classic Jacobi algorithm" do
331
+ m = Matrix[[3, 1, 1],[1, 3, 1],[1, 1, 3]]
332
+ v = Matrix[[2, 0, 0],[0, 5, 0],[0, 0, 2]]
333
+ Matrix.diag_in_delta?(v, m.cJacobiA, 0.01).should == true
334
+ a = Matrix[[1, 1, 1, 4],
335
+ [1, 1, 0, 5],
336
+ [1, 0, 1, 4],
337
+ [4, 5, 4, 1]]
338
+ e = Matrix[[-0.26828, 0, 0, 0], [0, -5.97550, 0, 0], [0, 0, 1.01373, 0], [0, 0, 0, 9.23004]]
339
+ Matrix.diag_in_delta?(e, a.cJacobiA, 1.0e-5).should == true
340
+ end
341
+ end
342
+
@@ -0,0 +1,26 @@
1
+ $:.unshift(File.dirname(__FILE__) + '../lib')
2
+
3
+ require 'test/unit'
4
+ require 'stick/currency'
5
+
6
+ class TestUnits < Test::Unit::TestCase
7
+
8
+ include ::Stick
9
+
10
+ def test_typerror
11
+ assert_raises(TypeError) do
12
+ assert_equal(1.usd, 1.try.to(:usd))
13
+ end
14
+ end
15
+
16
+ def test_cex
17
+ e = 1.twd().unit
18
+ r = 1.usd(:cex).to(twd(:cex))
19
+ assert_equal( e, r.unit )
20
+ end
21
+
22
+ def test_usd_to_tws
23
+ assert_equal( 1.tws, 1.usd.to(tws) )
24
+ end
25
+
26
+ end
@@ -0,0 +1,359 @@
1
+ #>ruby tests/test_xmatrix.rb
2
+
3
+ require 'test/unit' unless defined? $ZENTEST and $ZENTEST
4
+ require 'stick/matrix'
5
+
6
+ class TestVector < Test::Unit::TestCase
7
+ @@v = Vector.[](1, 2, 3, 4)
8
+
9
+ def test_setvalue
10
+ v = @@v.clone
11
+ v[1..2] = Vector[9, 9, 9, 9, 9]
12
+ assert_equal Vector[1, 9, 9, 4], v
13
+ end
14
+
15
+ def test_concat
16
+ assert_equal Vector[1, 2, 3], Vector.concat(Vector[1], Vector[2, 3])
17
+ assert_equal Vector[1, 2, 3, 4, 5], Vector.concat(Vector[1], Vector[2, 3], Vector[4, 5])
18
+ end
19
+
20
+ def test_collect
21
+ v = @@v.clone
22
+ assert_equal Vector.[](1, 4, 9, 16), v.collect!{|i| i * i}
23
+ assert_equal Vector.[](3, 12, 27, 48), v.collect!{|i| 3 * i}
24
+ end
25
+
26
+ def test_each
27
+ r = []
28
+ @@v.each{|i| r << i+1}
29
+ assert_equal [2, 3, 4, 5], r
30
+ end
31
+
32
+ def test_max
33
+ assert_equal 4, @@v.max
34
+ end
35
+
36
+ def test_min
37
+ assert_equal 1, @@v.min
38
+ end
39
+
40
+ def test_norm
41
+ v = Vector[3, 4]
42
+ assert_equal 5, v.norm
43
+ end
44
+
45
+ def test_p_norm1
46
+ v = Vector.[](3, 4)
47
+ assert_equal 7, v.norm(1)
48
+ end
49
+
50
+ def test_p_norm2
51
+ v = Vector.[](3, 4)
52
+ assert_equal 5, v.norm(2)
53
+ end
54
+
55
+ def test_p_norm3
56
+ v = Vector.[](3, 4)
57
+ assert_in_delta 4.497941, v.norm(3), 1.0e-5
58
+ end
59
+
60
+ def test_p_norm4
61
+ v = Vector.[](3, 4)
62
+ assert_in_delta 4.284572, v.norm(4), 1.0e-5
63
+ end
64
+
65
+ def test_setValue # []=
66
+ v = @@v.clone
67
+ v[3] = 10
68
+ assert_equal Vector.[](1, 2, 3, 10), v
69
+ end
70
+
71
+ def test_norm_inf
72
+ assert_equal 4, @@v.norm_inf
73
+ end
74
+ end
75
+
76
+ class TestMatrix < Test::Unit::TestCase
77
+
78
+ def test_id
79
+ m = Matrix.new(4, 3){|i, j| i * 3 + j}
80
+ assert_equal 5, m[1, 2]
81
+ assert_equal Vector[10, 11], m[3, 1..2]
82
+ assert_equal Matrix[[0, 1, 2], [3, 4, 5]], m[0..1, 0..2]
83
+ end
84
+
85
+ def test_id=
86
+ m = Matrix.new(3, 3){|i, j| i * 3 + j}
87
+ m[1, 2] = 9
88
+ assert_equal Matrix[[0, 1, 2], [3, 4, 9], [6, 7, 8]], m
89
+ m[1, 1..2] = Vector[8, 8]
90
+ assert_equal Matrix[[0, 1, 2], [3, 8, 8], [6, 7, 8]], m
91
+ m[0..1, 0..1] = Matrix[[0, 0, 0], [0, 0, 0]]
92
+ assert_equal Matrix[[0, 0, 2], [0, 0, 8], [6, 7, 8]], m
93
+ end
94
+
95
+ def test_set
96
+ m = Matrix[[1, 2, 222], [2, 33, 4]]
97
+ n = Matrix.new(2,3)
98
+ n.set(m)
99
+ assert_equal m, n
100
+ m.wrap = :torus
101
+ n.set(m)
102
+ assert_equal :torus, n.wrap
103
+ end
104
+
105
+ def test_wrap
106
+ m = Matrix[[1, 2, 3], [4, 5, 6]]
107
+ m.wrap=:torus
108
+ assert_equal 1, m[2, 3]
109
+ m.wrap=:h_cylinder
110
+ assert_equal 1, m[2, 0]
111
+ m.wrap=:v_cylinder
112
+ assert_equal 1, m[0, 3]
113
+ end
114
+
115
+ def test_max_len_column
116
+ m = Matrix[[1, 22, 3], [4, 5, 666666]]
117
+ assert_equal 6, m.max_len_column(2)
118
+ end
119
+
120
+ #list of maximum lengths of columns
121
+ def test_cols_len
122
+ m = Matrix[[1, 2, 222], [2, 33, 4]]
123
+ assert_equal [1, 2, 3], m.cols_len
124
+ m = Matrix[[1, 12345, 222], [2, 3, 4]]
125
+ assert_equal [1, 5, 3], m.cols_len
126
+ end
127
+
128
+ def test_each
129
+ m = Matrix[[1, 2, 3], [4, 5, 6]]
130
+ r = []
131
+ m.each{|x| r << x + 3}
132
+ assert_equal [4, 5, 6, 7, 8, 9], r
133
+ end
134
+
135
+ def test_row!
136
+ m = Matrix[[1, 2, 1], [2, 1, 2]]
137
+ assert_equal [1, 4, 1], m.row!(0) {|x| x*x}
138
+ m = Matrix[[1, 2, 1], [2, 1, 2]]
139
+ assert_equal Vector[1, 2, 1], m.row!(0)
140
+ end
141
+
142
+ def test_row_collect
143
+ m = Matrix[[1, 2, 1], [2, 1, 2]]
144
+ assert_equal [4, 1, 4], m.row_collect(1){|x| x*x}
145
+ end
146
+
147
+ def test_column_collect
148
+ m = Matrix[[1, 2, 3], [4, 5, 6]]
149
+ assert_equal [7, 10], m.column_collect(1){|x| x+5}
150
+ m = Matrix[[1, 2, 222], [2, 3, 4]]
151
+ assert_equal [1, 4], m.column_collect(0){|x| x*x}
152
+ end
153
+
154
+ def test_row_collect!
155
+ m = Matrix[[1, 2, 3], [4, 5, 6]]
156
+ m.row_collect!(1){|x| x*2}
157
+ assert_equal Matrix[[1, 2, 3], [8, 10, 12]], m
158
+ end
159
+
160
+ def test_column_collect!
161
+ m = Matrix[[1, 2, 3], [4, 5, 6]]
162
+ m.column_collect!(1){|x| x*2}
163
+ assert_equal Matrix[[1, 4, 3], [4, 10, 6]], m
164
+ end
165
+
166
+ def test_column=
167
+ m = Matrix.new(3, 3){|i, j| i * 3 + j + 1}
168
+ m.column= 1, Vector[1,1,1,1,1,1]
169
+ assert_equal Matrix[[1, 1, 3],[4, 1, 6],[7, 1, 9]], m
170
+ m.column= 2, Vector[9,9], 0..1
171
+ assert_equal Matrix[[1, 1, 9],[4, 1, 9],[7, 1, 9]], m
172
+ end
173
+
174
+ def test_row=
175
+ m = Matrix.new(3, 3){|i, j| i * 3 + j + 1}
176
+ m.row= 1, Vector[1,1,1,1,1,1]
177
+ assert_equal Matrix[[1, 2, 3],[1, 1, 1],[7, 8, 9]], m
178
+ m.row= 2, Vector[9,9], 0..2
179
+ assert_equal Matrix[[1, 2, 3],[1, 1, 1],[9, 9, 0]], m
180
+ end
181
+
182
+ def test_norm
183
+ m = Matrix[[1, 2, 1], [2, 1, 2]]
184
+ assert_equal Math.sqrt(15), m.norm
185
+ end
186
+
187
+ def test_empty
188
+ m = Matrix[[1, 2, 3], [4, 5, 6]]
189
+ assert_equal false, m.empty?
190
+ m = Matrix[]
191
+ assert_equal true, m.empty?
192
+ end
193
+
194
+ def test_row2matrix
195
+ m = Matrix.new(4, 3){|i, j| i * 3 + j + 1}
196
+ assert_equal Matrix[[4, 5, 6],[7, 8, 9]], m.row2matrix(1..2)
197
+ assert_equal Matrix[[7, 8, 9]], m.row2matrix(2)
198
+ assert_equal m, m.row2matrix(0..4)
199
+ end
200
+
201
+ def test_column2matrix
202
+ m = Matrix.new(4, 3){|i, j| i * 3 + j + 1}
203
+ assert_equal Matrix[[2], [5], [8], [11]], m.column2matrix(1)
204
+ end
205
+
206
+ def test_diag
207
+ m1 = Matrix[[1]]
208
+ m2 = Matrix[[2, 0], [0, 3]]
209
+ m3 = Matrix[[4, 0, 0], [0, 5, 0], [0, 0, 6]]
210
+ a1 = Matrix.new(6, 6){|i, j| i == j ? i + 1: 0}
211
+ assert_equal a1, Matrix.diag(m1, m2, m3)
212
+ assert_equal m2, Matrix.diag(m2)
213
+ a2 = Matrix[[2, 0, 0, 0],
214
+ [0, 3, 0, 0],
215
+ [0, 0, 2, 0],
216
+ [0, 0, 0, 3]]
217
+ assert_equal a2, Matrix.diag(m2, m2)
218
+ end
219
+
220
+ def test_equal_in_delta
221
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
222
+ assert_equal true, Matrix.equal_in_delta?(m, m)
223
+ mm = m.clone
224
+ mm[0,0] += 2
225
+ assert_equal false, Matrix.equal_in_delta?(m, mm, 0.001)
226
+ assert_equal true, Matrix.equal_in_delta?(m, mm, 2)
227
+ end
228
+
229
+ def test_diag_in_delta
230
+ assert_equal false, Matrix.diag_in_delta?(Matrix.I(5), Matrix.new(4, 4){|i, j| i + j})
231
+ m = Matrix.new(5, 5){|i, j| i == j ? 1 + 0.001 * (i+1) : i + j}
232
+ assert_equal true, Matrix.diag_in_delta?(Matrix.I(5), m, 0.01)
233
+ end
234
+
235
+ def test_LU
236
+ m = Matrix[[1, 4, 7],
237
+ [2, 5, 8],
238
+ [3, 6, 10]]
239
+ l = Matrix[[1, 0, 0],[2, 1, 0],[3, 2, 1]]
240
+ assert_equal l, m.L
241
+ u = Matrix[[1, 4, 7],[0, -3, -6],[0, 0, 1]]
242
+ assert_equal u, m.U
243
+ end
244
+
245
+ def test_L
246
+ # e.g.: MC, Golub, 3.2 LU factorization, pg 94
247
+ m = Matrix[[3, 5],
248
+ [6, 7]]
249
+ l = Matrix[[1, 0],
250
+ [2, 1]]
251
+ assert_equal l, m.L
252
+ end
253
+
254
+ def test_U
255
+ # e.g.: MC, Golub, 3.2 LU factorization, pg 94
256
+ m = Matrix[[3, 5],
257
+ [6, 7]]
258
+ u = Matrix[[3, 5],
259
+ [0, -3]]
260
+ assert_equal u, m.U
261
+
262
+ end
263
+
264
+ def test_houseQR
265
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
266
+ assert_equal true, Matrix.equal_in_delta?(m, m.houseQ * m.houseR)
267
+ q = Matrix[[0.0776, 0.8330, 0.5329, 0.1264],
268
+ [0.3104, 0.4512, -0.8048, 0.2286],
269
+ [0.5433, 0.0694, 0.0108, -0.8365],
270
+ [0.7761, -0.3123, 0.2610, 0.4815]]
271
+ assert_equal true, Matrix.equal_in_delta?(m.houseQ, q, 0.0001)
272
+ end
273
+
274
+ def test_houseR
275
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
276
+ r = Matrix[[12.88409, 14.59162, 16.29916],
277
+ [ 0, 1.04131, 2.082630],
278
+ [ 0, 0, 0],
279
+ [ 0, 0, 0]]
280
+ assert_equal true, Matrix.equal_in_delta?(r, m.houseR, 1.0e-5)
281
+ end
282
+
283
+ def test_bidiagonalization # MC, Golub, p252, Example 5.4.2
284
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
285
+ bidiag = Matrix[[12.884, 21.876, 0 ],
286
+ [0, 2.246, -0.613],
287
+ [0, 0, 0 ],
288
+ [0, 0, 0 ]]
289
+ assert_equal true, Matrix.equal_in_delta?(bidiag, m.bidiagonal, 0.001)
290
+ end
291
+
292
+ def test_gram_schmidt
293
+ m = Matrix[[1, 1],
294
+ [0.001, 0],
295
+ [0, 0.001]]
296
+ gsQ = Matrix[[ 1, 0],
297
+ [0.001, -0.707107],
298
+ [ 0, 0.707100]]
299
+ assert_equal true, Matrix.equal_in_delta?(gsQ, m.gram_schmidt[0], 0.001)
300
+ assert_equal true, Matrix.equal_in_delta?(m,m.gram_schmidt[0] * m.gram_schmidt[1], 1.0e-5)
301
+ end
302
+
303
+ def test_givens
304
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
305
+ assert_equal true, Matrix.equal_in_delta?(m, m.givensQ * m.givensR, 0.001)
306
+ end
307
+
308
+ def test_hessenbergQR
309
+ hess = Matrix[[1, 2, 1, 2, 1],
310
+ [1, 3, 2, 3, 4],
311
+ [0, 2, 4, 3, 5],
312
+ [0, 0, 1, 4, 3],
313
+ [0, 0, 0, 6, 1]]
314
+ hessR = hess.hessenbergR
315
+ r = Matrix[[1.41421, 3.53553, 2.12132, 3.53553, 3.53553],
316
+ [ 0, -2.12132, -4.00693, -3.06412, -5.42115],
317
+ [ 0, 0, -1.20185, -3.51310, -2.31125],
318
+ [ 0, 0, 0, -6.30628, -1.54912],
319
+ [ 0, 0, 0, 0, 1.53929]]
320
+
321
+ assert_equal true, Matrix.equal_in_delta?(r, hessR, 1.0e-5)
322
+ assert_equal true, Matrix.equal_in_delta?(hessR, hess.hessenbergQ.t * hess, 1.0e-5)
323
+ end
324
+
325
+ def test_hessenberg_form
326
+ m = Matrix[[1, 5, 7],[3, 0, 6],[4, 3, 1]]
327
+ h = Matrix[[1, 8.6, -0.2],[5, 4.96, -0.72],[0, 2.28, -3.96]]
328
+ assert_equal true, Matrix.equal_in_delta?(h, m.hessenberg_form_H, 0.001)
329
+ end
330
+
331
+ def test_realSchur
332
+ m = Matrix.new(3, 3){1} + Matrix.diagonal(2, 2, 2)
333
+ e = Matrix[[5, 0, 0],[0, 2, 0],[0, 0, 2]]
334
+ assert_equal true, Matrix.diag_in_delta?(m.realSchur, e, 1.0e-5)
335
+ end
336
+
337
+ def test_cJacobi
338
+ m = Matrix[[3, 1, 1],[1, 3, 1],[1, 1, 3]]
339
+ v = Matrix[[2, 0, 0],[0, 5, 0],[0, 0, 2]]
340
+ assert_equal true, Matrix.diag_in_delta?(v, m.cJacobiA, 0.01)
341
+ a = Matrix[[1, 1, 1, 4],
342
+ [1, 1, 0, 5],
343
+ [1, 0, 1, 4],
344
+ [4, 5, 4, 1]]
345
+ e = Matrix[[-0.26828, 0, 0, 0], [0, -5.97550, 0, 0], [0, 0, 1.01373, 0], [0, 0, 0, 9.23004]]
346
+ assert_equal true, Matrix.diag_in_delta?(e, a.cJacobiA, 1.0e-5)
347
+ end
348
+
349
+
350
+ def test_eigenvaluesJacobi
351
+ a = Matrix[[1, 1, 1, 4],
352
+ [1, 1, 0, 5],
353
+ [1, 0, 1, 4],
354
+ [4, 5, 4, 1]]
355
+ eigens = Vector[-0.268280385530705, -5.97550058143538, 1.01373431639199, 9.2300466505741]
356
+ assert_in_delta 0, (eigens - a.eigenvaluesJacobi).norm, 1.0e-10
357
+ end
358
+ end
359
+