Module: MDS::Test::BundleMatrixInterface

Includes:
MatrixAssertions
Defined in:
lib/mds/test/bundles/bundle_matrix_interface.rb

Overview

Module containing standarized tests for matrix interfaces.

Instance Method Summary collapse

Methods included from MatrixAssertions

#assert_delta_matrices, #assert_equal_matrices

Instance Method Details

#test_addObject



144
145
146
147
148
149
150
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 144

def test_add
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 2.0, 3.0])
  b = MDS::Matrix.create_rows([1.0, 2.0, 3.0], [0.0, 0.0, 2.0])
  r = MDS::Matrix.create_rows([3.0, 5.0, 7.0], [1.0, 2.0, 5.0])
  m = a + b
  assert_delta_matrices(r, m)
end

#test_columnsObject



114
115
116
117
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 114

def test_columns
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 4.0, 3.0])
  assert_equal([[2.0, 1.0], [3.0, 4.0], [4.0, 3.0]], a.columns)
end

#test_createObject


Matrix creators




22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 22

def test_create
  m = MDS::Matrix.create(2, 3, 0.0)
  assert_equal(2, m.nrows)
  assert_equal(3, m.ncols)
  
  for i in 0..m.nrows-1 do
    for j in 0..m.ncols-1 do
      assert_instance_of(Float, m[i,j])
      assert_equal(0.0, m[i,j])
    end
  end
end

#test_create_blockObject



49
50
51
52
53
54
55
56
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 49

def test_create_block
  m = MDS::Matrix.create_block(2,2) do |i,j|
    i == j ? 0.0 : 1.0
  end
  r = MDS::Matrix.create(3, 3, 1.0)
  r[0,0] = 0.0; r[1,1] = 0.0;
  assert_equal_matrices(m, r)
end

#test_create_diagonalObject



42
43
44
45
46
47
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 42

def test_create_diagonal
  m = MDS::Matrix.create_diagonal(1.0, 2.0, 3.0)
  r = MDS::Matrix.create(3, 3, 0.0)
  r[0,0] = 1.0; r[1,1] = 2.0; r[2,2] = 3.0;
  assert_equal_matrices(m, r)
end

#test_create_identityObject



35
36
37
38
39
40
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 35

def test_create_identity
  m = MDS::Matrix.create_identity(3)
  r = MDS::Matrix.create(3, 3, 0.0)
  r[0,0] = 1.0; r[1,1] = 1.0; r[2,2] = 1.0;
  assert_equal_matrices(m, r)
end

#test_create_rowsObject



58
59
60
61
62
63
64
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 58

def test_create_rows
  m = MDS::Matrix.create_rows([1.0, 2.0, 3.0], [4.0, 5.0, 6.0])
  r = MDS::Matrix.create_block(2,3) do |i,j|
    (i*3 + j + 1).to_f
  end
  assert_equal_matrices(m, r)
end

#test_diagonalsObject



95
96
97
98
99
100
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 95

def test_diagonals
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 4.0, 3.0])
  r = MDS::Matrix.create_rows([2.0, 4.0])
  m = MDS::Matrix.create_rows(a.diagonals)
  assert_delta_matrices(r, m)
end

#test_edObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 160

def test_ed
  a = MDS::Matrix.create_rows(
    [0.0, 10.0, 2.0], 
    [10.0, 0.0, 20.0],
    [2.0, 20.0, 0.0]
  )
          
  eval_a, evec_a = a.ed
  
  assert_in_delta(23.2051, eval_a[0,0], 1e-3)
  assert_in_delta(-1.5954, eval_a[1,1], 1e-3)
  assert_in_delta(-21.6097, eval_a[2,2], 1e-3)
    
  r0 = MDS::Matrix.create_rows([0.3529, 0.6934, 0.6281])
  r1 = MDS::Matrix.create_rows([0.8948, -0.0541, -0.4430])
  r2 = MDS::Matrix.create_rows([-0.2732, 0.7184, -0.6396])
  
  v0 = evec_a.minor(0..2, 0..0)
  v1 = evec_a.minor(0..2, 1..1)
  v2 = evec_a.minor(0..2, 2..2)
  
  # Norm of vectors

  assert_in_delta(1.0, (v0.t * v0)[0,0], 1e-3)
  assert_in_delta(1.0, (v1.t * v1)[0,0], 1e-3)
  assert_in_delta(1.0, (v2.t * v2)[0,0], 1e-3)
  
  # Orthogonality of basis vectors

  assert_in_delta(0.0, (v0.t * v1)[0,0], 1e-3)
  assert_in_delta(0.0, (v0.t * v2)[0,0], 1e-3)
  assert_in_delta(0.0, (v1.t * v2)[0,0], 1e-3)
  
  # Orientation of basis vectors (up to 180° ambiguity)

  assert_in_delta(1.0, (r0 * v0)[0,0].abs, 1e-3)
  assert_in_delta(1.0, (r1 * v1)[0,0].abs, 1e-3)
  assert_in_delta(1.0, (r2 * v2)[0,0].abs, 1e-3)
end

#test_get_setObject


Matrix element accessors




70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 70

def test_get_set
  a = MDS::Matrix.create(2, 2, 1.0)

  a[0,0] = 1.0
  a[0,1] = 2.0
  a[1,0] = 3.0
  a[1,1] = 4.0
      
  assert_equal(1.0, a[0,0])
  assert_equal(2.0, a[0,1])
  assert_equal(3.0, a[1,0])
  assert_equal(4.0, a[1,1])
end

#test_minorObject



102
103
104
105
106
107
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 102

def test_minor
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 4.0, 3.0])
  r = MDS::Matrix.create_rows([4.0],[3.0])
  m = a.minor(0..1, 2..2)
  assert_delta_matrices(r, m)
end

#test_productObject


Matrix operations




128
129
130
131
132
133
134
135
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 128

def test_product
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 2.0, 3.0])
  b = MDS::Matrix.create_rows([3.0, 1.0], [1.0, 2.0], [3.0, -4.0])
  r = MDS::Matrix.create_rows([21.0, -8.0], [14.0, -7.0])
  
  m = a * b
  assert_delta_matrices(r, m)
end

#test_product_scalarObject



137
138
139
140
141
142
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 137

def test_product_scalar
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 2.0, 3.0])
  r = MDS::Matrix.create_rows([4.0, 6.0, 8.0], [2.0, 4.0, 6.0])
  m = a * 2.0
  assert_delta_matrices(r, m)
end

#test_rowsObject



119
120
121
122
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 119

def test_rows
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 4.0, 3.0])
  assert_equal([[2.0, 3.0, 4.0], [1.0, 4.0, 3.0]], a.rows)
end

#test_subObject



152
153
154
155
156
157
158
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 152

def test_sub
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 2.0, 3.0])
  b = MDS::Matrix.create_rows([1.0, 2.0, 2.0], [0.0, 0.0, 2.0])
  r = MDS::Matrix.create_rows([1.0, 1.0, 2.0], [1.0, 2.0, 1.0])
  m = a - b
  assert_delta_matrices(r, m)
end

#test_traceObject



109
110
111
112
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 109

def test_trace
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 4.0, 3.0])
  assert_equal(6.0, a.trace)
end

#test_transposeObject


Matrix views




88
89
90
91
92
93
# File 'lib/mds/test/bundles/bundle_matrix_interface.rb', line 88

def test_transpose
  a = MDS::Matrix.create_rows([2.0, 3.0, 4.0], [1.0, 2.0, 3.0])
  r = MDS::Matrix.create_rows([2.0, 1.0], [3.0, 2.0], [4.0, 3.0])
  m = a.t
  assert_delta_matrices(r, m)
end