extendmatrix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,357 @@
1
+ $:.unshift(File.dirname(__FILE__)+"/../lib")
2
+ require 'spec'
3
+ require 'spec/autorun'
4
+
5
+ require 'extendmatrix'
6
+
7
+ describe "Vector class extension:" do
8
+ before do
9
+ @v = Vector.[](1, 2, 3, 4)
10
+ end
11
+
12
+ it "[] method" do
13
+ v = @v.clone
14
+ v[1..2] = Vector[9, 9, 9, 9, 9]
15
+ v.should == Vector[1, 9, 9, 4]
16
+ end
17
+
18
+ it "Vector.concat method" do
19
+ Vector.concat(Vector[1], Vector[2, 3]).should == Vector[1, 2, 3]
20
+ Vector.concat(Vector[1], Vector[2, 3], Vector[4, 5]).should == Vector[1, 2, 3, 4, 5]
21
+ end
22
+
23
+ it "collect method" do
24
+ @v.collect!{|i| i * i}.should == Vector.[](1, 4, 9, 16)
25
+ @v.collect!{|i| 3 * i}.should == Vector.[](3, 12, 27, 48)
26
+ end
27
+
28
+ it "each method" do
29
+ r = []
30
+ @v.each{|i| r << i+1}
31
+ r.should == [2, 3, 4, 5]
32
+ end
33
+
34
+ it "max element" do
35
+ @v.max.should == 4
36
+ end
37
+
38
+ it "max element" do
39
+ @v.min.should == 1
40
+ end
41
+
42
+ it "norm method" do
43
+ v = Vector.[](3, 4)
44
+ v.norm.should == 5
45
+ end
46
+
47
+ it "method p_norm(1)" do
48
+ v = Vector.[](3, 4)
49
+ v.norm(1).should == 7
50
+ end
51
+
52
+ it "method p_norm(2)" do
53
+ v = Vector.[](3, 4)
54
+ v.norm(2).should == 5
55
+ end
56
+
57
+ it "method p_norm(3)" do
58
+ v = Vector.[](3, 4)
59
+ v.norm(3).should > 4.49
60
+ v.norm(3).should < 4.50
61
+ end
62
+
63
+ it "method p_norm(4)" do
64
+ v = Vector.[](3, 4)
65
+ v.norm(4).should > 4.28
66
+ v.norm(4).should < 4.29
67
+ end
68
+
69
+ it "[]= method" do
70
+ @v[3] = 10
71
+ @v.should == Vector.[](1, 2, 3, 10)
72
+ end
73
+
74
+ it "norm_inf" do
75
+ @v.norm_inf.should == 4
76
+ end
77
+ end
78
+
79
+ describe "Matrix class extension:" do
80
+ before do
81
+ @m = Matrix[[1, 2, 222], [2, 33, 4]]
82
+ end
83
+ it "to_s method" do
84
+ @m.to_s.size.should_not == 0
85
+ end
86
+ it "[] method" do
87
+ m = Matrix.new(4, 3){|i, j| i * 3 + j}
88
+ m[1, 2].should == 5
89
+ m[3, 1..2].should == Vector[10, 11]
90
+ m[0..1, 0..2].should == Matrix[[0, 1, 2], [3, 4, 5]]
91
+ end
92
+ it "row_sum method" do
93
+ @m.row_sum[0].should==225
94
+ end
95
+ it "column_sum method" do
96
+ @m.column_sum[0].should==3
97
+ end
98
+ it "total_sum method" do
99
+ @m.total_sum.should==264
100
+ end
101
+
102
+ it "[]= method" do
103
+ m = Matrix.new(3, 3){|i, j| i * 3 + j}
104
+ m[1, 2] = 9
105
+ m.should == Matrix[[0, 1, 2], [3, 4, 9], [6, 7, 8]]
106
+ m[1, 1..2] = Vector[8, 8]
107
+ m.should == Matrix[[0, 1, 2], [3, 8, 8], [6, 7, 8]]
108
+ m[0..1, 0..1] = Matrix[[0, 0, 0], [0, 0, 0]]
109
+ m.should == Matrix[[0, 0, 2], [0, 0, 8], [6, 7, 8]]
110
+ end
111
+
112
+ it "set method" do
113
+ n = Matrix.new(2, 3)
114
+ n.set(@m)
115
+ n.should == @m
116
+ end
117
+
118
+ it "set method and wrap value" do
119
+ @m.wrap = :torus
120
+ n = Matrix.new(2, 3)
121
+ n.set(@m)
122
+ n.wrap.should == :torus
123
+ end
124
+
125
+ it "wrap method" do
126
+ @m.wrap=:torus
127
+ @m[2, 3].should == 1
128
+ @m.wrap=:h_cylinder
129
+ @m[2, 0].should == 1
130
+ @m.wrap=:v_cylinder
131
+ @m[0, 3].should == 1
132
+ end
133
+
134
+ it "maximum length of a column" do
135
+ @m.max_len_column(1).should == 2
136
+ end
137
+
138
+ it "list of maximum lengths of columns" do
139
+ @m.cols_len.should == [1, 2, 3]
140
+ end
141
+
142
+ it "matrix each method" do
143
+ r = []
144
+ @m.each{|x| r << x + 3}
145
+ r.should == [4, 5, 225, 5, 36, 7]
146
+
147
+ s = 0
148
+ @m.each{|x| s += x}
149
+ s.should == 264
150
+ end
151
+
152
+ it "row! method" do
153
+ @m.row!(0){|x| x+x}.should == [2, 4, 444]
154
+ @m.should == Matrix[[2, 4, 444], [2, 33, 4]]
155
+ end
156
+
157
+ it "row_collect method" do
158
+ @m.row_collect(1){|x| x+10}.should == [12, 43, 14]
159
+ end
160
+
161
+ it "column_collect method" do
162
+ @m.column_collect(0){|x| x*3}.should == [3, 6]
163
+ end
164
+
165
+ it "row_collect! method, identicaly with row!" do
166
+ @m.row_collect!(0){|x| x+x}.should == [2, 4, 444]
167
+ @m.should == Matrix[[2, 4, 444], [2, 33, 4]]
168
+ end
169
+
170
+ it "column_collect! method" do
171
+ @m.column_collect!(2){|x| x+10}.should == [232, 14]
172
+ @m.should == Matrix[[1, 2, 232], [2, 33, 14]]
173
+ end
174
+
175
+ it "column= " do
176
+ m = Matrix.new(3, 3){|i, j| i * 3 + j + 1}
177
+ m.column= 1, Vector[1,1,1,1,1,1]
178
+ m.should == Matrix[[1, 1, 3],[4, 1, 6],[7, 1, 9]]
179
+ m.column= 2, Vector[9,9], 0..1
180
+ m.should == Matrix[[1, 1, 9],[4, 1, 9],[7, 1, 9]]
181
+ end
182
+
183
+ it "row= " do
184
+ m = Matrix.new(3, 3){|i, j| i * 3 + j + 1}
185
+ m.row= 1, Vector[1,1,1,1,1,1]
186
+ m.should == Matrix[[1, 2, 3],[1, 1, 1],[7, 8, 9]]
187
+ m.row= 2, Vector[9,9], 0..2
188
+ m.should == Matrix[[1, 2, 3],[1, 1, 1],[9, 9, 0]]
189
+ end
190
+
191
+ it "norm of a matrix" do
192
+ m = Matrix[[1, 2, 3], [1, 2, 3]]
193
+ m.norm.should == Math.sqrt(28)
194
+ end
195
+
196
+ it "test empty matrix" do
197
+ @m.empty?.should == false
198
+ n = Matrix[]
199
+ n.empty?.should == true
200
+ end
201
+
202
+ it "row2matrix" do
203
+ m = Matrix.new(4, 3){|i, j| i * 3 + j + 1}
204
+ m.row2matrix(1..2).should == Matrix[[4, 5, 6],[7, 8, 9]]
205
+ m.row2matrix(2).should == Matrix[[7, 8, 9]]
206
+ m.row2matrix(0..4).should == m
207
+ end
208
+
209
+ it "column2matrix" do
210
+ m = Matrix.new(4, 3){|i, j| i * 3 + j + 1}
211
+ m.column2matrix(1).should == Matrix[[2], [5], [8], [11]]
212
+ end
213
+
214
+ it "diag" do
215
+ m1 = Matrix[[1]]
216
+ m2 = Matrix[[2, 0], [0, 3]]
217
+ m3 = Matrix[[4, 0, 0], [0, 5, 0], [0, 0, 6]]
218
+ a1 = Matrix.new(6, 6){|i, j| i == j ? i + 1: 0}
219
+ Matrix.diag(m1, m2, m3).should == a1
220
+ Matrix.diag(m2).should == m2
221
+ a2 = Matrix[[2, 0, 0, 0],
222
+ [0, 3, 0, 0],
223
+ [0, 0, 2, 0],
224
+ [0, 0, 0, 3]]
225
+ Matrix.diag(m2, m2).should == a2
226
+ end
227
+
228
+ it "equal_in_delta" do
229
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
230
+ Matrix.equal_in_delta?(m, m).should == true
231
+ mm = m.clone
232
+ mm[0,0] += 2
233
+ Matrix.equal_in_delta?(m, mm, 0.001).should == false
234
+ Matrix.equal_in_delta?(m, mm, 2).should == true
235
+ end
236
+
237
+ it "diag_in_delta" do
238
+ Matrix.diag_in_delta?(Matrix.I(5), Matrix.new(4, 4){|i, j| i + j}).should == false
239
+ m = Matrix.new(5, 5){|i, j| i == j ? 1 + 0.001 * (i+1) : i + j}
240
+ Matrix.diag_in_delta?(Matrix.I(5), m, 0.01).should == true
241
+ end
242
+
243
+ it "LU " do
244
+ m = Matrix[[1, 4, 7],
245
+ [2, 5, 8],
246
+ [3, 6, 10]]
247
+ l = Matrix[[1, 0, 0],[2, 1, 0],[3, 2, 1]]
248
+ m.L.should == l
249
+ u = Matrix[[1, 4, 7],[0, -3, -6],[0, 0, 1]]
250
+ m.U.should == u
251
+ end
252
+
253
+ it "L " do
254
+ # e.g.: MC, Golub, 3.2 LU factorization, pg 94
255
+ m = Matrix[[3, 5],
256
+ [6, 7]]
257
+ l = Matrix[[1, 0],
258
+ [2, 1]]
259
+ m.L.should == l
260
+ end
261
+
262
+ it "U " do
263
+ # e.g.: MC, Golub, 3.2 LU factorization, pg 94
264
+ m = Matrix[[3, 5],
265
+ [6, 7]]
266
+ u = Matrix[[3, 5],
267
+ [0, -3]]
268
+ m.U.should == u
269
+ end
270
+
271
+ it "houseQR " do
272
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
273
+ Matrix.equal_in_delta?(m, m.houseQ * m.houseR).should == true
274
+ q = Matrix[[0.0776, 0.8330, 0.5329, 0.1264],
275
+ [0.3104, 0.4512, -0.8048, 0.2286],
276
+ [0.5433, 0.0694, 0.0108, -0.8365],
277
+ [0.7761, -0.3123, 0.2610, 0.4815]]
278
+ Matrix.equal_in_delta?(m.houseQ, q, 0.0001).should == true
279
+ end
280
+
281
+ it "houseR " do
282
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
283
+ r = Matrix[[12.88409, 14.59162, 16.29916],
284
+ [ 0, 1.04131, 2.082630],
285
+ [ 0, 0, 0],
286
+ [ 0, 0, 0]]
287
+ Matrix.equal_in_delta?(r, m.houseR, 1.0e-5).should == true
288
+ end
289
+
290
+ it "bidiagonalization" do
291
+ # MC, Golub, p252, Example 5.4.2
292
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
293
+ bidiag = Matrix[[12.884, 21.876, 0 ],
294
+ [0, 2.246, -0.613],
295
+ [0, 0, 0 ],
296
+ [0, 0, 0 ]]
297
+ Matrix.equal_in_delta?(bidiag, m.bidiagonal, 0.001).should == true
298
+ end
299
+
300
+ it "gram_schmidt" do
301
+ m = Matrix[[1, 1],
302
+ [0.001, 0],
303
+ [0, 0.001]]
304
+ gsQ = Matrix[[ 1, 0],
305
+ [0.001, -0.707107],
306
+ [ 0, 0.707100]]
307
+ Matrix.equal_in_delta?(gsQ, m.gram_schmidt[0], 0.001).should == true
308
+ Matrix.equal_in_delta?(m,m.gram_schmidt[0] * m.gram_schmidt[1], 1.0e-5).should == true
309
+ end
310
+
311
+ it "givens " do
312
+ m = Matrix.new(4, 3){|i, j| i * 3 + j +1}
313
+ Matrix.equal_in_delta?(m, m.givensQ * m.givensR, 0.001).should == true
314
+ end
315
+
316
+ it "hessenbergQR " do
317
+ hess = Matrix[[1, 2, 1, 2, 1],
318
+ [1, 3, 2, 3, 4],
319
+ [0, 2, 4, 3, 5],
320
+ [0, 0, 1, 4, 3],
321
+ [0, 0, 0, 6, 1]]
322
+ hessR = hess.hessenbergR
323
+ r = Matrix[[1.41421, 3.53553, 2.12132, 3.53553, 3.53553],
324
+ [ 0, -2.12132, -4.00693, -3.06412, -5.42115],
325
+ [ 0, 0, -1.20185, -3.51310, -2.31125],
326
+ [ 0, 0, 0, -6.30628, -1.54912],
327
+ [ 0, 0, 0, 0, 1.53929]]
328
+
329
+ Matrix.equal_in_delta?(r, hessR, 1.0e-5).should == true
330
+ Matrix.equal_in_delta?(hessR, hess.hessenbergQ.t * hess, 1.0e-5).should == true
331
+ end
332
+
333
+ it "hessenberg_form " do
334
+ m = Matrix[[1, 5, 7],[3, 0, 6],[4, 3, 1]]
335
+ h = Matrix[[1, 8.6, -0.2],[5, 4.96, -0.72],[0, 2.28, -3.96]]
336
+ Matrix.equal_in_delta?(h, m.hessenberg_form_H, 0.001).should == true
337
+ end
338
+
339
+ it "realSchur" do
340
+ m = Matrix.new(3, 3){1} + Matrix.diagonal(2, 2, 2)
341
+ e = Matrix[[5, 0, 0],[0, 2, 0],[0, 0, 2]]
342
+ Matrix.diag_in_delta?(m.realSchur, e, 1.0e-5).should == true
343
+ end
344
+
345
+ it "Classic Jacobi algorithm" do
346
+ m = Matrix[[3, 1, 1],[1, 3, 1],[1, 1, 3]]
347
+ v = Matrix[[2, 0, 0],[0, 5, 0],[0, 0, 2]]
348
+ Matrix.diag_in_delta?(v, m.cJacobiA, 0.01).should == true
349
+ a = Matrix[[1, 1, 1, 4],
350
+ [1, 1, 0, 5],
351
+ [1, 0, 1, 4],
352
+ [4, 5, 4, 1]]
353
+ e = Matrix[[-0.26828, 0, 0, 0], [0, -5.97550, 0, 0], [0, 0, 1.01373, 0], [0, 0, 0, 9.23004]]
354
+ Matrix.diag_in_delta?(e, a.cJacobiA, 1.0e-5).should == true
355
+ end
356
+ end
357
+
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extendmatrix
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Cosmin Bonchis
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain:
16
+ - |
17
+ -----BEGIN CERTIFICATE-----
18
+ MIIDMjCCAhqgAwIBAgIBADANBgkqhkiG9w0BAQUFADA/MREwDwYDVQQDDAhjbGJ1
19
+ c3RvczEVMBMGCgmSJomT8ixkARkWBWdtYWlsMRMwEQYKCZImiZPyLGQBGRYDY29t
20
+ MB4XDTEwMDMyOTIxMzg1NVoXDTExMDMyOTIxMzg1NVowPzERMA8GA1UEAwwIY2xi
21
+ dXN0b3MxFTATBgoJkiaJk/IsZAEZFgVnbWFpbDETMBEGCgmSJomT8ixkARkWA2Nv
22
+ bTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALf8JVMGqE7m5kYb+PNN
23
+ neZv2pcXV5fQCi6xkyG8bi2/SIFy/LyxuvLzEeOxBeaz1Be93bayIUquOIqw3dyw
24
+ /KXWa31FxuNuvAm6CN8fyeRYX/ou4cw3OIUUnIvB7RMNIu4wbgeM6htV/QEsNLrv
25
+ at1/mh9JpqawPrcjIOVMj4BIp67vmzJCaUf+S/H2uYtSO09F+YQE3tv85TPeRmqU
26
+ yjyXyTc/oJiw1cXskUL8UtMWZmrwNLHXuZWWIMzkjiz3UNdhJr/t5ROk8S2WPznl
27
+ 0bMy/PMIlAbqWolRn1zl2VFJ3TaXScbqImY8Wf4g62b/1ZSUlGrtnLNsCYXrWiso
28
+ UPUCAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFGu9
29
+ rrJ1H64qRmNNu3Jj/Qjvh0u5MA0GCSqGSIb3DQEBBQUAA4IBAQCV0Unka5isrhZk
30
+ GjqSDqY/6hF+G2pbFcbWUpjmC8NWtAxeC+7NGV3ljd0e1SLfoyBj4gnFtFmY8qX4
31
+ K02tgSZM0eDV8TpgFpWXzK6LzHvoanuahHLZEtk/+Z885lFene+nHadkem1n9iAB
32
+ cs96JO9/JfFyuXM27wFAwmfHCmJfPF09R4VvGHRAvb8MGzSVgk2i06OJTqkBTwvv
33
+ JHJdoyw3+8bw9RJ+jLaNoQ+xu+1pQdS2bb3m7xjZpufml/m8zFCtjYM/7qgkKR8z
34
+ /ZZt8lCiKfFArppRrZayE2FVsps4X6WwBdrKTMZ0CKSXTRctbEj1BAZ67eoTvBBt
35
+ rpP0jjs0
36
+ -----END CERTIFICATE-----
37
+
38
+ date: 2010-05-04 00:00:00 -04:00
39
+ default_executable:
40
+ dependencies:
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubyforge
43
+ prerelease: false
44
+ requirement: &id001 !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ segments:
49
+ - 2
50
+ - 0
51
+ - 4
52
+ version: 2.0.4
53
+ type: :development
54
+ version_requirements: *id001
55
+ - !ruby/object:Gem::Dependency
56
+ name: hoe
57
+ prerelease: false
58
+ requirement: &id002 !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ segments:
63
+ - 2
64
+ - 6
65
+ - 0
66
+ version: 2.6.0
67
+ type: :development
68
+ version_requirements: *id002
69
+ description: |-
70
+ The project consists of some enhancements to the Ruby "Matrix" module and includes: LU and QR (Householder, Givens, Gram Schmidt, Hessenberg) decompositions, bidiagonalization, eigenvalue and eigenvector calculations.
71
+ Include some aditional code to obtains marginal for rows and columns.
72
+
73
+ The original code can be found on the RubyForge repository at http://matrix.rubyforge.org/svn/trunk/ or the project's SVN repository can be checked out through anonymous access with the following command(s).
74
+
75
+ Work done by Cosmin Bonchis as a Google Summer of Code 2007 project for Ruby Central Inc.
76
+ email:
77
+ - cbonchis_info.uvt.ro
78
+ executables: []
79
+
80
+ extensions: []
81
+
82
+ extra_rdoc_files:
83
+ - History.txt
84
+ - Manifest.txt
85
+ - ORIGINAL_README.txt
86
+ - README.txt
87
+ files:
88
+ - History.txt
89
+ - Manifest.txt
90
+ - ORIGINAL_README.txt
91
+ - README.txt
92
+ - Rakefile
93
+ - lib/extendmatrix.rb
94
+ - spec/extendmatrix_spec.rb
95
+ has_rdoc: true
96
+ homepage: http://github.com/clbustos/extendmatrix
97
+ licenses: []
98
+
99
+ post_install_message:
100
+ rdoc_options:
101
+ - --main
102
+ - README.txt
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ segments:
110
+ - 0
111
+ version: "0"
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ requirements: []
120
+
121
+ rubyforge_project: ruby-statsample
122
+ rubygems_version: 1.3.6
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: "The project consists of some enhancements to the Ruby \"Matrix\" module and includes: LU and QR (Householder, Givens, Gram Schmidt, Hessenberg) decompositions, bidiagonalization, eigenvalue and eigenvector calculations"
126
+ test_files: []
127
+