1 // This is core/vnl/tests/test_matrix_interface.cxx
2 #include <iostream>
3 #include <vnl/vnl_matrix.h>
4 #include <vnl/vnl_matrix_fixed.h>
5
6 #include <testlib/testlib_test.h>
7
8 template< typename TValue >
square(TValue v)9 TValue square(TValue v)
10 {
11 return v * v;
12 }
13
14 // This function is used in testing later.
15 template< typename T >
sum_vector(const vnl_vector<T> & v)16 T sum_vector(const vnl_vector<T> &v) { return v.sum(); }
17
18 template< typename T >
sum_vector(const vnl_vector_fixed<T,2> & v)19 T sum_vector(const vnl_vector_fixed<T, 2> &v) { return v.sum(); }
20
21 template< class TVector >
sum(TVector v)22 typename TVector::element_type sum(TVector v)
23 {
24 return v.sum();
25 }
26
27 template< class TContainer >
test_common_interface()28 void test_common_interface()
29 {
30 TContainer m(2,2);
31 m.size();
32 m.rows();
33 m.cols();
34 m.columns();
35 m.put(0, 0, 0);
36 m.get(0, 0);
37 m[0];
38 m(0,0);
39 m.fill(0);
40 m.fill_diagonal(0);
41 vnl_vector<int> v(2, 1);
42 m.set_diagonal(v);
43 int data[4] = { 0, 1, 2, 3 };
44 m.set(data);
45 m.copy_in(data);
46 m.copy_out(data);
47 TContainer n(2,2);
48 n.fill(12);
49 m = n;
50 m += 4;
51 m -= 4;
52 m *= 4;
53 m /= 4;
54 m += n;
55 m -= n;
56 m *= n;
57 m.apply(square);
58 m.apply_rowwise( sum_vector );
59 m.apply_columnwise( sum_vector );
60 m.transpose();
61 m.conjugate_transpose();
62 m.update(n);
63 m.update(n,0);
64 m.update(n,0,0);
65 m.set_column(0, data); // OK: data is longer than one column
66 m.set_column(0, 0);
67 m.set_column(0, v);
68 m.set_row(0, data);
69
70 m.set_row(0, 0);
71 m.set_row(0, v);
72 m.extract(2,2);
73 m.extract(2,2,0);
74 m.extract(2,2,0,0);
75 vnl_matrix<int> e(2,2);
76 m.extract(e);
77 m.get_row(0);
78 m.get_column(0);
79 ///////////////////////////////////////////////
80 // Test `get_rows` and `get_columns` Methods //
81 ///////////////////////////////////////////////
82 {
83 typename TContainer::element_type data[4] = {1, 2, 3, 4};
84 unsigned int indices[2] = {1, 0};
85 vnl_vector<unsigned int> i(indices, 2);
86 TContainer matrix(2, 2);
87 matrix.copy_in(data);
88 #if ! defined(_MSC_VER) //This code is failing on VS15 in Release mode for vnl_matrix_fixed
89 //Removing temporarily so that this new test does not hold up other works.
90 TContainer matrix_lr(matrix);
91 matrix_lr.fliplr();
92 TContainer matrix_ud(matrix);
93 matrix_ud.flipud();
94 TEST("get_rows", matrix_lr.is_equal(matrix.get_columns(i), 10e-6), true);
95 TEST("get_columns", matrix_ud.is_equal(matrix.get_rows(i), 10e-6), true);
96 #endif
97 }
98 m.get_n_rows(0,1);
99 m.get_n_columns(0,1);
100 m.get_diagonal();
101 m.flatten_row_major();
102 m.flatten_column_major();
103 m.set_identity();
104 m.inplace_transpose();
105 #if ! defined(_MSC_VER) //This code is failing on VS15 in Release mode for vnl_matrix_fixed
106 //Removing temporarily so that this new test does not hold up other works.
107 m.flipud();
108 m.fliplr();
109 #endif
110
111 m.normalize_rows();
112 m.normalize_columns();
113 m.scale_row(0,10);
114 m.scale_column(0,10);
115 ////////////////////////
116 // Test `swap` Method //
117 ////////////////////////
118
119 {
120 const typename TContainer::element_type data1[4] = {0, 1, 2, 3};
121 TContainer l(2, 2);
122 l.copy_in(data1);
123 TContainer l_swap(l);
124
125 const typename TContainer::element_type data2[4] = {4, 5, 6, 7};
126 TContainer r(2, 2);
127 r.copy_in(data2);
128 TContainer r_swap(r);
129
130 l_swap.swap(r_swap);
131 TEST("swap left-right", l.is_equal(r_swap, 10e-6), true);
132 TEST("swap right-left", r.is_equal(l_swap, 10e-6), true);
133 }
134 m.array_one_norm();
135 m.array_two_norm();
136 m.array_inf_norm();
137 m.absolute_value_sum();
138 m.absolute_value_max();
139 m.operator_one_norm();
140 m.operator_inf_norm();
141 m.frobenius_norm();
142 m.fro_norm();
143 m.rms();
144 m.min_value();
145 m.max_value();
146 m.arg_min();
147 m.arg_max();
148 m.mean();
149 m.empty();
150 m.is_identity();
151 m.is_identity(10e-6);
152 m.is_zero();
153 m.is_zero(10e-6);
154 ////////////////////////////
155 // Test `is_equal` Method //
156 ////////////////////////////
157
158 {
159 TContainer l(2,2);
160 TContainer r(2,2);
161 l.copy_in(data);
162 r.copy_in(data);
163 TEST("is_equal (true)", l.is_equal(r, 10e-6), true);
164 r.put(0, 0, 25);
165 TEST("is_equal (false)", !l.is_equal(r, 10e-6), true);
166 }
167 m.is_finite();
168 m.has_nans();
169 m.assert_size(2,2);
170 m.assert_finite();
171 m.data_block();
172 m.as_ref();
173 typename TContainer::iterator it = m.begin();
174 it = m.end();
175 typename TContainer::const_iterator cit = m.begin();
176 cit = m.end();
177 }
178
179 static
test_container_interface()180 void test_container_interface()
181 {
182 std::cout << "Testing vnl_matrix<int>" << std::endl;
183 test_common_interface< vnl_matrix<int> >();
184
185 std::cout << "Testing vnl_matrix_fixed<int, 2, 2>" << std::endl;
186 test_common_interface< vnl_matrix_fixed<int, 2, 2> >();
187 }
188
189 TESTMAIN(test_container_interface);
190