Belle II Software development
PlainMatrix.test.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <tracking/trackFindingCDC/numerics/PlainMatrixUtil.h>
9#include <tracking/trackFindingCDC/numerics/PlainMatrix.h>
10
11#include <tracking/trackFindingCDC/numerics/EigenView.h>
12
13#include <gtest/gtest.h>
14
15using namespace Belle2;
16using namespace TrackFindingCDC;
17
18
19TEST(TrackFindingCDCTest, PlainMatrix_zero)
20{
22 {
24 for (int i = 0; i < result.rows(); ++i) {
25 for (int j = 0; j < result.cols(); ++j) {
26 EXPECT_EQ(0, (result(i, j)));
27 }
28 }
29 }
30
32 {
33 auto result = PlainMatrix<double, 5, 5>::Zero();
34 for (int i = 0; i < result.rows(); ++i) {
35 for (int j = 0; j < result.cols(); ++j) {
36 EXPECT_EQ(0, (result(i, j)));
37 }
38 }
39 }
40}
41
42
43TEST(TrackFindingCDCTest, PlainMatrix_id)
44{
46 for (int i = 0; i < result.rows(); ++i) {
47 for (int j = 0; j < result.cols(); ++j) {
48 if (i == j) {
49 EXPECT_EQ(1, (result(i, j)));
50 } else {
51 EXPECT_EQ(0, (result(i, j)));
52 }
53 }
54 }
55}
56
57
58TEST(TrackFindingCDCTest, PlainMatrix_diag)
59{
60 PlainMatrix<double, 5, 1> diagElements{{0, 1, 2, 3, 4}};
61 auto result = PlainMatrixUtil::Diag(diagElements);
62
63 EXPECT_EQ(5, result.cols());
64 EXPECT_EQ(5, result.rows());
65
66 for (int i = 0; i < result.rows(); ++i) {
67 for (int j = 0; j < result.cols(); ++j) {
68 if (i == j) {
69 EXPECT_EQ(i, result(i, j));
70 } else {
71 EXPECT_EQ(0, result(i, j));
72 }
73 }
74 }
75}
76
77
78TEST(TrackFindingCDCTest, PlainMatrix_constant)
79{
81 for (int i = 0; i < result.rows(); ++i) {
82 for (int j = 0; j < result.cols(); ++j) {
83 EXPECT_EQ(3, (result(i, j)));
84 }
85 }
86}
87
88
89TEST(TrackFindingCDCTest, PlainMatrix_aggregate_initialization)
90{
91 auto result = PlainMatrix<double, 2, 1>({ -1.0, 1.0});
92 EXPECT_EQ(-1, result(0, 0));
93 EXPECT_EQ(1, result(1, 0));
94
95 auto result2 = []() {
96 return PlainMatrix<double, 2, 1>({ -1.0, 1.0});
97 }();
98
99 EXPECT_EQ(-1, result2(0, 0));
100 EXPECT_EQ(1, result2(1, 0));
101}
102
103
104TEST(TrackFindingCDCTest, PlainMatrix_vstack)
105{
106
107 auto a = PlainMatrix<double, 2, 1>({1, 2});
108 auto b = PlainMatrix<double, 3, 1>({3, 4, 5});
109
110 auto result = PlainMatrixUtil::VStack(a, b);
111
112 EXPECT_EQ(1, result.cols());
113 EXPECT_EQ(5, result.rows());
114
115 EXPECT_EQ(1, result(0, 0));
116 EXPECT_EQ(2, result(1, 0));
117 EXPECT_EQ(3, result(2, 0));
118 EXPECT_EQ(4, result(3, 0));
119 EXPECT_EQ(5, result(4, 0));
120}
121
122TEST(TrackFindingCDCTest, PlainMatrix_hstack)
123{
124
125 auto a = PlainMatrix<double, 2, 1>({1, 2});
126 auto b = PlainMatrix<double, 2, 1>({3, 4});
127
128 auto result = PlainMatrixUtil::HStack(a, b);
129
130 EXPECT_EQ(2, result.cols());
131 EXPECT_EQ(2, result.rows());
132
133 EXPECT_EQ(1, result(0, 0));
134 EXPECT_EQ(2, result(1, 0));
135 EXPECT_EQ(3, result(0, 1));
136 EXPECT_EQ(4, result(1, 1));
137}
138
139
140
141TEST(TrackFindingCDCTest, PlainMatrix_blockstack)
142{
143
144 auto a = PlainMatrix<double, 2, 1>({1, 2});
145 auto b = PlainMatrix<double, 3, 1>({3, 4, 5});
146
147 auto result = PlainMatrixUtil::BlockStack(a, b);
148
149 EXPECT_EQ(2, result.cols());
150 EXPECT_EQ(5, result.rows());
151 EXPECT_EQ(1, result(0, 0));
152 EXPECT_EQ(2, result(1, 0));
153 EXPECT_EQ(0, result(2, 0));
154 EXPECT_EQ(0, result(3, 0));
155 EXPECT_EQ(0, result(4, 0));
156
157 EXPECT_EQ(0, result(0, 1));
158 EXPECT_EQ(0, result(1, 1));
159 EXPECT_EQ(3, result(2, 1));
160 EXPECT_EQ(4, result(3, 1));
161 EXPECT_EQ(5, result(4, 1));
162}
163
164TEST(TrackFindingCDCTest, PlainMatrix_element_access)
165{
167 a(0, 0) = 1;
168 a(1, 0) = 2;
169 a[2] = 3;
170
171 EXPECT_EQ(1, a[0]);
172 EXPECT_EQ(2, a(1, 0));
173 EXPECT_EQ(3, a(2));
174}
175
176TEST(TrackFindingCDCTest, PlainMatrix_minus)
177{
178 auto a = PlainMatrix<double, 2, 1>({1, 2});
179 auto b = PlainMatrix<double, 2, 1>({2, 3});
180
181 auto result = b - a;
182 for (int i = 0; i < result.rows(); ++i) {
183 for (int j = 0; j < result.cols(); ++j) {
184 EXPECT_EQ(1 , result(i, j));
185 }
186 }
187}
188
189TEST(TrackFindingCDCTest, PlainMatrix_plus)
190{
191 auto a = PlainMatrix<double, 2, 1>({1, 2});
192 auto b = PlainMatrix<double, 2, 1>({2, 1});
193
194 auto result = b + a;
195 for (int i = 0; i < result.rows(); ++i) {
196 for (int j = 0; j < result.cols(); ++j) {
197 EXPECT_EQ(3, result(i, j));
198 }
199 }
200}
201
202TEST(TrackFindingCDCTest, PlainMatrix_divide)
203{
204 auto a = PlainMatrix<double, 2, 1>({2, 2});
205
206 auto result = a / 2.0;
207 for (int i = 0; i < result.rows(); ++i) {
208 for (int j = 0; j < result.cols(); ++j) {
209 EXPECT_EQ(1, result(i, j));
210 }
211 }
212}
213
214
215TEST(TrackFindingCDCTest, PlainMatrix_inverse_divide)
216{
217 auto a = PlainMatrix<double, 2, 1>({2, 2});
218
219 auto result = 2.0 / a;
220 for (int i = 0; i < result.rows(); ++i) {
221 for (int j = 0; j < result.cols(); ++j) {
222 EXPECT_EQ(1, result(i, j));
223 }
224 }
225}
226
227TEST(TrackFindingCDCTest, PlainMatrix_muliplication)
228{
229 auto a = PlainMatrix<double, 2, 1>({2, 2});
230 auto b = PlainMatrix<double, 1, 2>({1.0 / 4.0, 1.0 / 4.0});
231
232 auto result = b * a;
233
234 EXPECT_EQ(1, result.cols());
235 EXPECT_EQ(1, result.rows());
236
237 EXPECT_EQ(1, result(0, 0));
238
239 auto result2 = a * b;
240
241 EXPECT_EQ(2, result2.cols());
242 EXPECT_EQ(2, result2.rows());
243
244 EXPECT_EQ(1.0 / 2.0, result2(0, 0));
245 EXPECT_EQ(1.0 / 2.0, result2(1, 0));
246 EXPECT_EQ(1.0 / 2.0, result2(0, 1));
247 EXPECT_EQ(1.0 / 2.0, result2(1, 1));
248}
249
250TEST(TrackFindingCDCTest, PlainMatrix_block)
251{
252 auto a = PlainMatrix<double, 2, 1>({1, 2});
253 auto b = PlainMatrix<double, 3, 1>({3, 4, 5});
254
255 auto result = PlainMatrixUtil::BlockStack(a, b);
256
257 auto aNew = result.block<2, 1>(0, 0);
258 auto bNew = result.block<3, 1>(2, 1);
259
260 EXPECT_TRUE(std::equal(a.data(), a.data() + a.size(), aNew.data()));
261 EXPECT_TRUE(std::equal(b.data(), b.data() + b.size(), bNew.data()));
262}
263
264TEST(TrackFindingCDCTest, PlainMatrixEigenMap_mapToEigen)
265{
266 auto a = PlainMatrix<double, 2, 1>({1, 2});
267 auto b = PlainMatrix<double, 3, 1>({3, 4, 5});
268
269 auto result = PlainMatrixUtil::BlockStack(a, b);
270 auto eigenMap = mapToEigen(result);
271
272 for (int i = 0; i < result.rows(); ++i) {
273 for (int j = 0; j < result.cols(); ++j) {
274 EXPECT_EQ(result(i, j) , eigenMap(i, j));
275 }
276 }
277
278 // Test write-through
279 eigenMap(0, 0) = 0;
280 EXPECT_EQ(0, eigenMap(0, 0)) << "write back test " << 0 << ", " << 0;
281 EXPECT_EQ(0, result(0, 0)) << "write back test " << 0 << ", " << 0;
282
283 // cppcheck-suppress unreadVariable
284 eigenMap = Eigen::Matrix<double, 5, 2>::Zero();
285
286 for (int i = 0; i < result.rows(); ++i) {
287 for (int j = 0; j < result.cols(); ++j) {
288 EXPECT_EQ(0, result(i, j)) << "write back test " << i << ", " << j;
289 }
290 }
291}
292
293TEST(TrackFindingCDCTest, PlainMatrixEigenMap_output_operator)
294{
295 auto a = PlainMatrix<double, 2, 1>({1, 2});
296 std::stringstream oss;
297 oss << a;
298}
A matrix implementation to be used as an interface typ through out the track finder.
Definition: PlainMatrix.h:40
static PlainMatrix< T, M, N > Constant(T t)
Construct a matrix with all elements set to a constant.
Definition: PlainMatrix.h:83
static PlainMatrix< T, M, N > Identity()
Construct an identity matrix.
Definition: PlainMatrix.h:74
static PlainMatrix< T, M, N > Zero()
Construct a matrix initialized with zeros.
Definition: PlainMatrix.h:67
Abstract base class for different kinds of events.
static PlainMatrix< T, K+M, N > VStack(const PlainMatrix< T, K, N > &a, const PlainMatrix< T, M, N > &b)
Construct a matrix from two independent blocks stacked vertically.
static PlainMatrix< T, M, M > Diag(const PlainMatrix< T, M, 1 > &diagEntries)
Construct a diagonal matrix - currently private as it is unused.
static PlainMatrix< T, M, L+N > HStack(const PlainMatrix< T, M, L > &a, const PlainMatrix< T, M, N > &b)
Construct a matrix from two independent blocks stacked horizontally.
static PlainMatrix< T, K+M, L+N > BlockStack(const PlainMatrix< T, K, L > &a, const PlainMatrix< T, M, N > &b)
Construct a matrix from two independent blocks stacked along the diagonal.