mdarray 0.5.4-java → 0.5.5-java
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +35 -16
- data/README.md +180 -234
- data/lib/colt/matrix/algebra.rb +466 -0
- data/lib/colt/matrix/colt_matrix.rb +161 -220
- data/lib/colt/matrix/creation.rb +374 -0
- data/lib/colt/matrix/{matrix_hierarchy.rb → hierarchy.rb} +78 -99
- data/lib/colt/matrix/property.rb +363 -0
- data/lib/mdarray/views.rb +46 -1
- data/test/colt/matrix/test_complete.rb +29 -0
- data/test/colt/{test_matrix.rb → matrix/test_creation.rb} +26 -3
- data/test/colt/{test_double_matrix2d.rb → matrix/test_matrix1d_floatingalgebra.rb} +86 -84
- data/test/colt/matrix/test_matrix2d_fixpointalgebra.rb +531 -0
- data/test/colt/matrix/test_matrix2d_floatingalgebra.rb +693 -0
- data/test/colt/matrix/test_operations.rb +120 -0
- data/test/colt/matrix/test_properties.rb +649 -0
- data/test/colt/test_complete.rb +1 -1
- data/test/complete.rb +12 -7
- data/test/mdarray/test_views.rb +55 -0
- data/vendor/{incanter.jar → mdarray.jar} +0 -0
- data/version.rb +1 -1
- metadata +22 -16
- data/lib/colt/matrix/matrix2D_floating_algebra.rb +0 -325
- data/test/colt/test_float_matrix2d.rb +0 -171
- data/vendor/commons-compiler.jar +0 -0
- data/vendor/janino.jar +0 -0
@@ -0,0 +1,693 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
5
|
+
# and distribute this software and its documentation, without fee and without a signed
|
6
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
7
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
8
|
+
# distributions.
|
9
|
+
#
|
10
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
11
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
12
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
13
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
14
|
+
#
|
15
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
16
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
17
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
18
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
19
|
+
# OR MODIFICATIONS.
|
20
|
+
##########################################################################################
|
21
|
+
|
22
|
+
require 'rubygems'
|
23
|
+
require "test/unit"
|
24
|
+
require 'shoulda'
|
25
|
+
|
26
|
+
require 'mdarray'
|
27
|
+
|
28
|
+
class MDMatrixTest < Test::Unit::TestCase
|
29
|
+
|
30
|
+
context "Colt Matrix" do
|
31
|
+
|
32
|
+
setup do
|
33
|
+
|
34
|
+
|
35
|
+
end
|
36
|
+
#=begin
|
37
|
+
#-------------------------------------------------------------------------------------
|
38
|
+
#
|
39
|
+
#-------------------------------------------------------------------------------------
|
40
|
+
|
41
|
+
should "do basic matrix algebra" do
|
42
|
+
|
43
|
+
a = MDMatrix.double([4, 4])
|
44
|
+
a.fill(2.5)
|
45
|
+
|
46
|
+
b = MDMatrix.fromfunction("double", [4, 4]) { |x, y| x + y }
|
47
|
+
|
48
|
+
c = a + b
|
49
|
+
b.reset_traversal
|
50
|
+
c.each do |val|
|
51
|
+
assert_equal(2.5 + b.next, val)
|
52
|
+
end
|
53
|
+
|
54
|
+
c = a * 2
|
55
|
+
a.reset_traversal
|
56
|
+
c.each do |val|
|
57
|
+
assert_equal(a.next * 2, val)
|
58
|
+
end
|
59
|
+
|
60
|
+
c = 2 * a
|
61
|
+
a.reset_traversal
|
62
|
+
c.each do |val|
|
63
|
+
assert_equal(a.next * 2, val)
|
64
|
+
end
|
65
|
+
|
66
|
+
c = a - 2
|
67
|
+
a.reset_traversal
|
68
|
+
c.each do |val|
|
69
|
+
assert_equal(a.next - 2, val)
|
70
|
+
end
|
71
|
+
|
72
|
+
c = 2 - a
|
73
|
+
a.reset_traversal
|
74
|
+
c.each do |val|
|
75
|
+
assert_equal(2 - a.next, val)
|
76
|
+
end
|
77
|
+
|
78
|
+
# Need to test/implement matrix division
|
79
|
+
assert_raise ( RuntimeError ) { c = a / a }
|
80
|
+
assert_raise ( RuntimeError ) { c = a / b }
|
81
|
+
|
82
|
+
p "Matrix division"
|
83
|
+
b.generate_non_singular!
|
84
|
+
c = a / b
|
85
|
+
c.print
|
86
|
+
printf("\n\n")
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
#-------------------------------------------------------------------------------------
|
91
|
+
#
|
92
|
+
#-------------------------------------------------------------------------------------
|
93
|
+
|
94
|
+
should "get and set values for double Matrix" do
|
95
|
+
|
96
|
+
a = MDMatrix.double([4, 4])
|
97
|
+
a[0, 0] = 1
|
98
|
+
assert_equal(1, a[0, 0])
|
99
|
+
assert_equal(0.0, a[0, 1])
|
100
|
+
|
101
|
+
a.fill(2.5)
|
102
|
+
assert_equal(2.5, a[3, 3])
|
103
|
+
|
104
|
+
b = MDMatrix.double([4, 4])
|
105
|
+
b.fill(a)
|
106
|
+
assert_equal(2.5, b[1, 3])
|
107
|
+
|
108
|
+
# fill the matrix with the value of a Proc evaluation. The argument to the
|
109
|
+
# Proc is the content of the array at the given index, i.e, x = b[i] for all i.
|
110
|
+
func = Proc.new { |x| x ** 2 }
|
111
|
+
b.fill(func)
|
112
|
+
assert_equal(2.5 ** 2, b[0, 3])
|
113
|
+
assert_equal(2.5 ** 2, b[1, 1])
|
114
|
+
|
115
|
+
# fill the Matrix with the value of method apply from a given class.
|
116
|
+
# In general this solution is more efficient than the above solution with
|
117
|
+
# Proc.
|
118
|
+
class DoubleFunc
|
119
|
+
def self.apply(x)
|
120
|
+
x/2
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
b.fill(DoubleFunc)
|
125
|
+
assert_equal(3.125, b[2,0])
|
126
|
+
|
127
|
+
# defines a class with a method apply with two arguments
|
128
|
+
class DoubleFunc2
|
129
|
+
def self.apply(x, y)
|
130
|
+
(x + y) ** 2
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# fill array a with the value ot the result of a function to each cell;
|
135
|
+
# x[row,col] = function(x[row,col],y[row,col]).
|
136
|
+
tmp = a[0,1]
|
137
|
+
a.fill(b, DoubleFunc2)
|
138
|
+
assert_equal((tmp + b[0, 1]) ** 2, a[0, 1])
|
139
|
+
|
140
|
+
tens = MDMatrix.init_with("double", [5, 3], 10.0)
|
141
|
+
assert_equal(10.0, tens[2, 2])
|
142
|
+
|
143
|
+
typed_arange = MDMatrix.typed_arange("double", 0, 20, 2)
|
144
|
+
assert_equal(0, typed_arange[0])
|
145
|
+
assert_equal(2, typed_arange[1])
|
146
|
+
assert_equal(4, typed_arange[2])
|
147
|
+
|
148
|
+
typed_arange.reshape!([5, 2])
|
149
|
+
|
150
|
+
val = typed_arange.reduce(Proc.new { |x, y| x + y }, Proc.new { |x| x })
|
151
|
+
assert_equal(90, val)
|
152
|
+
|
153
|
+
val = typed_arange.reduce(Proc.new { |x, y| x + y }, Proc.new { |x| x * x})
|
154
|
+
assert_equal(1140, val)
|
155
|
+
|
156
|
+
val = typed_arange.reduce(Proc.new { |x, y| x + y }, Proc.new { |x| x },
|
157
|
+
Proc.new { |x| x > 8 })
|
158
|
+
assert_equal(70, val)
|
159
|
+
|
160
|
+
linspace = MDMatrix.linspace("double", 0, 10, 50)
|
161
|
+
assert_equal(0.20408163265306123, linspace[1])
|
162
|
+
assert_equal(1.0204081632653061, linspace[5])
|
163
|
+
|
164
|
+
# set the value of all cells that are bigger than 5 to 1.0
|
165
|
+
linspace.fill_cond(Proc.new { |x| x > 5 }, 1.0)
|
166
|
+
assert_equal(1.0, linspace[25])
|
167
|
+
assert_equal(1.0, linspace[27])
|
168
|
+
assert_not_equal(1.0, linspace[24])
|
169
|
+
|
170
|
+
# set the value of all cells that are smaller than 5 to the square value
|
171
|
+
linspace.fill_cond(Proc.new { |x| x < 5 }, Proc.new { |x| x * x })
|
172
|
+
assert_equal(0.20408163265306123 ** 2, linspace[1])
|
173
|
+
assert_equal(1.0204081632653061 ** 2, linspace[5])
|
174
|
+
|
175
|
+
ones = MDMatrix.ones("double", [3, 5])
|
176
|
+
assert_equal(1.0, ones[2, 1])
|
177
|
+
|
178
|
+
arange = MDMatrix.arange(0, 10)
|
179
|
+
assert_equal(0, arange[0])
|
180
|
+
assert_equal(1, arange[1])
|
181
|
+
|
182
|
+
end
|
183
|
+
|
184
|
+
#-------------------------------------------------------------------------------------
|
185
|
+
#
|
186
|
+
#-------------------------------------------------------------------------------------
|
187
|
+
|
188
|
+
should "test 2d double matrix functions" do
|
189
|
+
|
190
|
+
b = MDMatrix.double([3], [1.5, 1, 1.3])
|
191
|
+
|
192
|
+
pos = MDArray.double([3, 3], [4, 12, -16, 12, 37, -43, -16, -43, 98])
|
193
|
+
matrix = MDMatrix.from_mdarray(pos)
|
194
|
+
|
195
|
+
# Cholesky decomposition from wikipedia example
|
196
|
+
chol = matrix.chol
|
197
|
+
assert_equal(2, chol[0, 0])
|
198
|
+
assert_equal(6, chol[1, 0])
|
199
|
+
assert_equal(-8, chol[2, 0])
|
200
|
+
assert_equal(5, chol[2, 1])
|
201
|
+
assert_equal(3, chol[2, 2])
|
202
|
+
|
203
|
+
matrix = MDMatrix.double([2, 2], [2, 3, 2, 1])
|
204
|
+
# Eigenvalue decomposition
|
205
|
+
eig = matrix.eig
|
206
|
+
p "eigen decomposition"
|
207
|
+
p "eigenvalue matrix"
|
208
|
+
eig[0].print
|
209
|
+
printf("\n\n")
|
210
|
+
p "imaginary parts of the eigenvalues"
|
211
|
+
eig[1].print
|
212
|
+
printf("\n\n")
|
213
|
+
p "real parts of the eigenvalues"
|
214
|
+
eig[2].print
|
215
|
+
printf("\n\n")
|
216
|
+
p "eigenvector matrix"
|
217
|
+
eig[3].print
|
218
|
+
printf("\n\n")
|
219
|
+
|
220
|
+
lu = matrix.lu
|
221
|
+
p "lu decomposition"
|
222
|
+
p "is non singular: #{lu[0]}"
|
223
|
+
p "determinant: #{lu[1]}"
|
224
|
+
p "pivot vector: #{lu[2]}"
|
225
|
+
p "lower triangular matrix"
|
226
|
+
lu[3].print
|
227
|
+
printf("\n\n")
|
228
|
+
p "upper triangular matrix"
|
229
|
+
lu[4].print
|
230
|
+
printf("\n\n")
|
231
|
+
|
232
|
+
# Returns the condition of matrix A, which is the ratio of largest to
|
233
|
+
# smallest singular value.
|
234
|
+
p "condition of matrix"
|
235
|
+
p matrix.cond
|
236
|
+
|
237
|
+
# Solves the upper triangular system U*x=b;
|
238
|
+
p "solving the equation by backward_solve"
|
239
|
+
solve = lu[4].backward_solve(b)
|
240
|
+
solve.print
|
241
|
+
printf("\n\n")
|
242
|
+
|
243
|
+
# Solves the lower triangular system U*x=b;
|
244
|
+
p "solving the equation by forward_solve"
|
245
|
+
solve = lu[3].forward_solve(b)
|
246
|
+
solve.print
|
247
|
+
printf("\n\n")
|
248
|
+
|
249
|
+
qr = matrix.qr
|
250
|
+
p "QR decomposition"
|
251
|
+
p "Matrix has full rank: #{qr[0]}"
|
252
|
+
p "Orthogonal factor Q:"
|
253
|
+
qr[1].print
|
254
|
+
printf("\n\n")
|
255
|
+
p "Upper triangular factor, R"
|
256
|
+
qr[2].print
|
257
|
+
printf("\n\n")
|
258
|
+
|
259
|
+
matrix = MDMatrix.double([5, 5], [2.0, 0.0, 8.0, 6.0, 0.0,\
|
260
|
+
1.0, 6.0, 0.0, 1.0, 7.0,\
|
261
|
+
5.0, 0.0, 7.0, 4.0, 0.0,\
|
262
|
+
7.0, 0.0, 8.0, 5.0, 0.0,\
|
263
|
+
0.0, 10.0, 0.0, 0.0, 7.0])
|
264
|
+
|
265
|
+
svd = matrix.svd
|
266
|
+
p "Singular value decomposition"
|
267
|
+
p "operation success? #{svd[0]}" # 0 success; < 0 ith value is illegal; > 0 not converge
|
268
|
+
p "cond: #{svd[1]}"
|
269
|
+
p "norm2: #{svd[2]}"
|
270
|
+
p "rank: #{svd[3]}"
|
271
|
+
assert_equal(17.91837085874625, svd[4][0])
|
272
|
+
assert_equal(15.17137188041607, svd[4][1])
|
273
|
+
assert_equal(3.5640020352605677, svd[4][2])
|
274
|
+
assert_equal(1.9842281528992616, svd[4][3])
|
275
|
+
assert_equal(0.3495556671751232, svd[4][4])
|
276
|
+
|
277
|
+
p "Diagonal matrix of singular values"
|
278
|
+
# svd[5].print
|
279
|
+
printf("\n\n")
|
280
|
+
p "left singular vectors U"
|
281
|
+
svd[6].print
|
282
|
+
printf("\n\n")
|
283
|
+
p "right singular vectors V"
|
284
|
+
svd[7].print
|
285
|
+
printf("\n\n")
|
286
|
+
|
287
|
+
m = MDArray.typed_arange("double", 0, 16)
|
288
|
+
m.reshape!([4, 4])
|
289
|
+
matrix1 = MDMatrix.from_mdarray(m)
|
290
|
+
# mat2 = matrix.chol
|
291
|
+
matrix1.print
|
292
|
+
printf("\n\n")
|
293
|
+
|
294
|
+
p "Transposing the above matrix"
|
295
|
+
matrix1.transpose.print
|
296
|
+
printf("\n\n")
|
297
|
+
|
298
|
+
p "getting regions from the above matrix"
|
299
|
+
p "specification is '0:0, 1:3'"
|
300
|
+
matrix1.region(:spec => "0:0, 1:3").print
|
301
|
+
printf("\n\n")
|
302
|
+
|
303
|
+
p "specification is '0:3:2, 1:3'"
|
304
|
+
matrix1.region(:spec => "0:3:2, 1:3").print
|
305
|
+
printf("\n\n")
|
306
|
+
|
307
|
+
p "flipping dim 0 of the matrix"
|
308
|
+
matrix1.flip(0).print
|
309
|
+
printf("\n\n")
|
310
|
+
|
311
|
+
m = MDArray.typed_arange("double", 16, 32)
|
312
|
+
m.reshape!([4, 4])
|
313
|
+
matrix2 = MDMatrix.from_mdarray(m)
|
314
|
+
matrix2.print
|
315
|
+
printf("\n\n")
|
316
|
+
|
317
|
+
p "matrix multiplication of square matrices"
|
318
|
+
result = matrix1 * matrix2
|
319
|
+
result.print
|
320
|
+
printf("\n\n")
|
321
|
+
|
322
|
+
p "matrix multiplication of rec matrices"
|
323
|
+
array1 = MDMatrix.double([2, 3], [1, 2, 3, 4, 5, 6])
|
324
|
+
array2 = MDMatrix.double([3, 2], [1, 2, 3, 4, 5, 6])
|
325
|
+
mult = array1 * array2
|
326
|
+
mult.print
|
327
|
+
printf("\n\n")
|
328
|
+
|
329
|
+
p "matrix multiplication of rec matrices passing alpha and beta parameters."
|
330
|
+
p "C = alpha * A x B + beta*C"
|
331
|
+
mult = array1.mult(array2, 2, 2)
|
332
|
+
mult.print
|
333
|
+
printf("\n\n")
|
334
|
+
|
335
|
+
p "matrix multiplication of rec matrices passing alpha, beta and return (C) parameters."
|
336
|
+
p "C = alpha * A x B + beta*C"
|
337
|
+
result = MDMatrix.double([2, 2], [2, 2, 2, 2])
|
338
|
+
mult = array1.mult(array2, 2, 2, false, false, result)
|
339
|
+
mult.print
|
340
|
+
printf("\n\n")
|
341
|
+
|
342
|
+
p "matrix multiplication by vector"
|
343
|
+
array1 = MDMatrix.double([2, 3], [1, 2, 3, 4, 5, 6])
|
344
|
+
vector = MDMatrix.double([3], [4, 5, 6])
|
345
|
+
mult = array1 * vector
|
346
|
+
mult.print
|
347
|
+
printf("\n\n")
|
348
|
+
|
349
|
+
result = matrix1.kron(matrix2)
|
350
|
+
p "Kronecker multiplication"
|
351
|
+
result.print
|
352
|
+
printf("\n\n")
|
353
|
+
|
354
|
+
print "determinant is: #{result.det}"
|
355
|
+
printf("\n\n")
|
356
|
+
|
357
|
+
p "norm1"
|
358
|
+
p result.norm1
|
359
|
+
|
360
|
+
p "norm2"
|
361
|
+
p result.norm2
|
362
|
+
|
363
|
+
p "Returns the Frobenius norm of matrix A, which is Sqrt(Sum(A[i,j]^2))"
|
364
|
+
p result.normF
|
365
|
+
|
366
|
+
p "Returns the infinity norm of matrix A, which is the maximum absolute row sum."
|
367
|
+
p result.norm_infinity
|
368
|
+
|
369
|
+
power3 = result ** 3
|
370
|
+
power3.print
|
371
|
+
printf("\n\n")
|
372
|
+
|
373
|
+
p result.trace
|
374
|
+
|
375
|
+
trap_lower = result.trapezoidal_lower
|
376
|
+
trap_lower.print
|
377
|
+
printf("\n\n")
|
378
|
+
|
379
|
+
p result.vector_norm2
|
380
|
+
|
381
|
+
result.normalize!
|
382
|
+
result.print
|
383
|
+
printf("\n\n")
|
384
|
+
|
385
|
+
p "summing all values of result: #{result.sum}"
|
386
|
+
|
387
|
+
result.mdarray.print
|
388
|
+
|
389
|
+
end
|
390
|
+
#=end
|
391
|
+
|
392
|
+
#-------------------------------------------------------------------------------------
|
393
|
+
#
|
394
|
+
#-------------------------------------------------------------------------------------
|
395
|
+
|
396
|
+
should "get and set values for float Matrix" do
|
397
|
+
|
398
|
+
a = MDMatrix.float([4, 4])
|
399
|
+
a[0, 0] = 1
|
400
|
+
assert_equal(1, a[0, 0])
|
401
|
+
assert_equal(0.0, a[0, 1])
|
402
|
+
|
403
|
+
a.fill(2.5)
|
404
|
+
assert_equal(2.5, a[3, 3])
|
405
|
+
|
406
|
+
b = MDMatrix.float([4, 4])
|
407
|
+
b.fill(a)
|
408
|
+
assert_equal(2.5, b[1, 3])
|
409
|
+
|
410
|
+
# fill the matrix with the value of a Proc evaluation. The argument to the
|
411
|
+
# Proc is the content of the array at the given index, i.e, x = b[i] for all i.
|
412
|
+
func = Proc.new { |x| x ** 2 }
|
413
|
+
b.fill(func)
|
414
|
+
assert_equal(6.25, b[0, 3])
|
415
|
+
b.print
|
416
|
+
printf("\n\n")
|
417
|
+
|
418
|
+
p "defining function in a class"
|
419
|
+
|
420
|
+
# fill the Matrix with the value of method apply from a given class.
|
421
|
+
# In general this solution is more efficient than the above solution with
|
422
|
+
# Proc.
|
423
|
+
class Func
|
424
|
+
def self.apply(x)
|
425
|
+
x/2
|
426
|
+
end
|
427
|
+
end
|
428
|
+
|
429
|
+
b.fill(Func)
|
430
|
+
assert_equal(3.125, b[2,0])
|
431
|
+
b.print
|
432
|
+
printf("\n\n")
|
433
|
+
|
434
|
+
# defines a class with a method apply with two arguments
|
435
|
+
class Func2
|
436
|
+
def self.apply(x, y)
|
437
|
+
(x + y) ** 2
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
# fill array a with the value the result of a function to each cell;
|
442
|
+
# x[row,col] = function(x[row,col],y[row,col]).
|
443
|
+
a.fill(b, Func2)
|
444
|
+
a.print
|
445
|
+
|
446
|
+
tens = MDMatrix.init_with("float", [5, 3], 10.0)
|
447
|
+
tens.print
|
448
|
+
printf("\n\n")
|
449
|
+
|
450
|
+
typed_arange = MDMatrix.typed_arange("float", 0, 20, 2)
|
451
|
+
typed_arange.print
|
452
|
+
printf("\n\n")
|
453
|
+
|
454
|
+
typed_arange.reshape!([5, 2])
|
455
|
+
p "printing typed_arange"
|
456
|
+
typed_arange.print
|
457
|
+
printf("\n\n")
|
458
|
+
|
459
|
+
p "reducing the value of typed_arange by summing all value"
|
460
|
+
val = typed_arange.reduce(Proc.new { |x, y| x + y }, Proc.new { |x| x })
|
461
|
+
p val
|
462
|
+
assert_equal(90, val)
|
463
|
+
|
464
|
+
p "reducing the value of typed_arange by summing the square of all value"
|
465
|
+
val = typed_arange.reduce(Proc.new { |x, y| x + y }, Proc.new { |x| x * x})
|
466
|
+
p val
|
467
|
+
assert_equal(1140, val)
|
468
|
+
|
469
|
+
p "reducing the value of typed_arange by summing all value larger than 8"
|
470
|
+
val = typed_arange.reduce(Proc.new { |x, y| x + y }, Proc.new { |x| x },
|
471
|
+
Proc.new { |x| x > 8 })
|
472
|
+
p val
|
473
|
+
|
474
|
+
|
475
|
+
linspace = MDMatrix.linspace("float", 0, 10, 50)
|
476
|
+
linspace.print
|
477
|
+
printf("\n\n")
|
478
|
+
|
479
|
+
# set the value of all cells that are bigger than 5 to 1.0
|
480
|
+
linspace.fill_cond(Proc.new { |x| x > 5 }, 1.0)
|
481
|
+
linspace.print
|
482
|
+
printf("\n\n")
|
483
|
+
|
484
|
+
# set the value of all cells that are smaller than 5 to the square value
|
485
|
+
linspace.fill_cond(Proc.new { |x| x < 5 }, Proc.new { |x| x * x })
|
486
|
+
linspace.print
|
487
|
+
printf("\n\n")
|
488
|
+
|
489
|
+
ones = MDMatrix.ones("float", [3, 5])
|
490
|
+
ones.print
|
491
|
+
printf("\n\n")
|
492
|
+
|
493
|
+
end
|
494
|
+
|
495
|
+
#-------------------------------------------------------------------------------------
|
496
|
+
#
|
497
|
+
#-------------------------------------------------------------------------------------
|
498
|
+
|
499
|
+
should "test 2d float matrix functions" do
|
500
|
+
|
501
|
+
b = MDMatrix.float([3], [1.5, 1, 1.3])
|
502
|
+
|
503
|
+
pos = MDArray.float([3, 3], [2, -1, 0, -1, 2, -1, 0, -1, 2])
|
504
|
+
matrix = MDMatrix.from_mdarray(pos)
|
505
|
+
result = matrix.chol
|
506
|
+
p "Cholesky decomposition"
|
507
|
+
result.print
|
508
|
+
printf("\n\n")
|
509
|
+
|
510
|
+
eig = matrix.eig
|
511
|
+
p "eigen decomposition"
|
512
|
+
p "eigenvalue matrix"
|
513
|
+
eig[0].print
|
514
|
+
printf("\n\n")
|
515
|
+
p "imaginary parts of the eigenvalues"
|
516
|
+
eig[1].print
|
517
|
+
printf("\n\n")
|
518
|
+
p "real parts of the eigenvalues"
|
519
|
+
eig[2].print
|
520
|
+
printf("\n\n")
|
521
|
+
p "eigenvector matrix"
|
522
|
+
eig[3].print
|
523
|
+
printf("\n\n")
|
524
|
+
|
525
|
+
lu = matrix.lu
|
526
|
+
p "lu decomposition"
|
527
|
+
p "is non singular: #{lu[0]}"
|
528
|
+
p "determinant: #{lu[1]}"
|
529
|
+
p "pivot vector: #{lu[2]}"
|
530
|
+
p "lower triangular matrix"
|
531
|
+
lu[3].print
|
532
|
+
printf("\n\n")
|
533
|
+
p "upper triangular matrix"
|
534
|
+
lu[4].print
|
535
|
+
printf("\n\n")
|
536
|
+
|
537
|
+
# Returns the condition of matrix A, which is the ratio of largest to
|
538
|
+
# smallest singular value.
|
539
|
+
p "condition of matrix"
|
540
|
+
p matrix.cond
|
541
|
+
|
542
|
+
# Solves the upper triangular system U*x=b;
|
543
|
+
p "solving the equation by backward_solve"
|
544
|
+
solve = lu[4].backward_solve(b)
|
545
|
+
solve.print
|
546
|
+
printf("\n\n")
|
547
|
+
|
548
|
+
# Solves the lower triangular system U*x=b;
|
549
|
+
p "solving the equation by forward_solve"
|
550
|
+
solve = lu[3].forward_solve(b)
|
551
|
+
solve.print
|
552
|
+
printf("\n\n")
|
553
|
+
|
554
|
+
qr = matrix.qr
|
555
|
+
p "QR decomposition"
|
556
|
+
p "Matrix has full rank: #{qr[0]}"
|
557
|
+
p "Orthogonal factor Q:"
|
558
|
+
qr[1].print
|
559
|
+
printf("\n\n")
|
560
|
+
p "Upper triangular factor, R"
|
561
|
+
qr[2].print
|
562
|
+
printf("\n\n")
|
563
|
+
|
564
|
+
svd = matrix.svd
|
565
|
+
p "Singular value decomposition"
|
566
|
+
p "operation success? #{svd[0]}" # 0 success; < 0 ith value is illegal; > 0 not converge
|
567
|
+
p "cond: #{svd[1]}"
|
568
|
+
p "norm2: #{svd[2]}"
|
569
|
+
p "rank: #{svd[3]}"
|
570
|
+
p "singular values"
|
571
|
+
p svd[4]
|
572
|
+
p "Diagonal matrix of singular values"
|
573
|
+
# svd[5].print
|
574
|
+
printf("\n\n")
|
575
|
+
p "left singular vectors U"
|
576
|
+
svd[6].print
|
577
|
+
printf("\n\n")
|
578
|
+
p "right singular vectors V"
|
579
|
+
svd[7].print
|
580
|
+
printf("\n\n")
|
581
|
+
|
582
|
+
m = MDArray.typed_arange("float", 0, 16)
|
583
|
+
m.reshape!([4, 4])
|
584
|
+
matrix1 = MDMatrix.from_mdarray(m)
|
585
|
+
# mat2 = matrix.chol
|
586
|
+
matrix1.print
|
587
|
+
printf("\n\n")
|
588
|
+
|
589
|
+
p "Transposing the above matrix"
|
590
|
+
matrix1.transpose.print
|
591
|
+
printf("\n\n")
|
592
|
+
|
593
|
+
p "getting regions from the above matrix"
|
594
|
+
p "specification is '0:0, 1:3'"
|
595
|
+
matrix1.region(:spec => "0:0, 1:3").print
|
596
|
+
printf("\n\n")
|
597
|
+
|
598
|
+
p "specification is '0:3:2, 1:3'"
|
599
|
+
matrix1.region(:spec => "0:3:2, 1:3").print
|
600
|
+
printf("\n\n")
|
601
|
+
|
602
|
+
p "flipping dim 0 of the matrix"
|
603
|
+
matrix1.flip(0).print
|
604
|
+
printf("\n\n")
|
605
|
+
|
606
|
+
m = MDArray.typed_arange("float", 16, 32)
|
607
|
+
m.reshape!([4, 4])
|
608
|
+
matrix2 = MDMatrix.from_mdarray(m)
|
609
|
+
matrix2.print
|
610
|
+
printf("\n\n")
|
611
|
+
|
612
|
+
result = matrix1 * matrix2
|
613
|
+
p "matrix multiplication"
|
614
|
+
result.print
|
615
|
+
printf("\n\n")
|
616
|
+
|
617
|
+
result = matrix1.kron(matrix2)
|
618
|
+
p "Kronecker multiplication"
|
619
|
+
result.print
|
620
|
+
printf("\n\n")
|
621
|
+
|
622
|
+
print "determinant is: #{result.det}"
|
623
|
+
printf("\n\n")
|
624
|
+
|
625
|
+
p "norm1"
|
626
|
+
p result.norm1
|
627
|
+
|
628
|
+
p "norm2"
|
629
|
+
p result.norm2
|
630
|
+
|
631
|
+
p "Returns the Frobenius norm of matrix A, which is Sqrt(Sum(A[i,j]^2))"
|
632
|
+
p result.normF
|
633
|
+
|
634
|
+
p "Returns the infinity norm of matrix A, which is the maximum absolute row sum."
|
635
|
+
p result.norm_infinity
|
636
|
+
|
637
|
+
power3 = result ** 3
|
638
|
+
power3.print
|
639
|
+
printf("\n\n")
|
640
|
+
|
641
|
+
p result.trace
|
642
|
+
|
643
|
+
trap_lower = result.trapezoidal_lower
|
644
|
+
trap_lower.print
|
645
|
+
printf("\n\n")
|
646
|
+
|
647
|
+
p result.vector_norm2
|
648
|
+
|
649
|
+
result.normalize!
|
650
|
+
result.print
|
651
|
+
printf("\n\n")
|
652
|
+
|
653
|
+
p "summing all values of result: #{result.sum}"
|
654
|
+
|
655
|
+
result.mdarray.print
|
656
|
+
|
657
|
+
end
|
658
|
+
|
659
|
+
#=end
|
660
|
+
end
|
661
|
+
|
662
|
+
end
|
663
|
+
|
664
|
+
=begin
|
665
|
+
|
666
|
+
Example of SVD decomposition with PColt. Check if working!
|
667
|
+
|
668
|
+
A =2.0 0.0 8.0 6.0 0.0
|
669
|
+
1.0 6.0 0.0 1.0 7.0
|
670
|
+
5.0 0.0 7.0 4.0 0.0
|
671
|
+
7.0 0.0 8.0 5.0 0.0
|
672
|
+
0.0 10.0 0.0 0.0 7.0
|
673
|
+
|
674
|
+
u = MDMatrix.double([5, 5], [-0.542255, 0.0649957, 0.821617, 0.105747, -0.124490,\
|
675
|
+
-0.101812, -0.593461, -0.112552, 0.788123, 0.0602700,\
|
676
|
+
-0.524953, 0.0593817, -0.212969, -0.115742, 0.813724,\
|
677
|
+
-0.644870, 0.0704063, -0.508744, -0.0599027, -0.562829,\
|
678
|
+
-0.0644952, -0.796930, 0.0900097, -0.592195, -0.0441263])
|
679
|
+
|
680
|
+
vt = MDMatrix.double([5, 5], [-0.464617, 0.0215065, -0.868509, 0.000799554, -0.171349,\
|
681
|
+
-0.0700860, -0.759988, 0.0630715, -0.601346, -0.227841,\
|
682
|
+
-0.735094, 0.0987971, 0.284009, -0.223485, 0.565040,\
|
683
|
+
-0.484392, 0.0254474, 0.398866, 0.332684, -0.703523,\
|
684
|
+
-0.0649698, -0.641520, -0.0442743, 0.691201, 0.323284])
|
685
|
+
|
686
|
+
S =
|
687
|
+
(00) 17.91837085874625
|
688
|
+
(11) 15.17137188041607
|
689
|
+
(22) 3.5640020352605677
|
690
|
+
(33) 1.9842281528992616
|
691
|
+
(44) 0.3495556671751232
|
692
|
+
|
693
|
+
=end
|