Belle II Software development
PlainMatrix.h
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#pragma once
9
10#include <ostream>
11#include <array>
12#include <algorithm>
13#include <cassert>
14
15namespace Belle2 {
20 namespace TrackFindingCDC {
21
39 template <class T, int M, int N>
41
42 static_assert(M > 0, "Dimensions must be greater zero");
43 static_assert(N > 0, "Dimensions must be greater zero");
44
46 static const int S = M * N;
47
48 public:
50 PlainMatrix() = default;
51
53 explicit PlainMatrix(std::initializer_list<T> values)
54 : m_values{}
55 {
56 assert(m_values.size() >= values.size());
57 std::copy(values.begin(), values.end(), m_values.begin());
58 }
59
61 explicit PlainMatrix(std::array<T, S> values)
62 : m_values(values)
63 {
64 }
65
68 {
69 PlainMatrix<T, M, N> result{{}}; // Value initialize to zero
70 return result;
71 }
72
75 {
76 PlainMatrix<T, M, N> result{{}}; // Value initialize to zero
77 for (int s = 0; s < M * N; s += M + 1)
78 result.m_values[s] = 1;
79 return result;
80 }
81
84 {
86 result.m_values.fill(t);
87 return result;
88 }
89
90 public:
92 T* data()
93 {
94 return m_values.data();
95 }
96
98 const T* data() const
99 {
100 return m_values.data();
101 }
102
104 T& operator[](int s)
105 {
106 return m_values[s];
107 }
108
110 T operator[](int s) const
111 {
112 return m_values[s];
113 }
114
116 T& operator()(int i, int j = 0)
117 {
118 return m_values.operator[](j * M + i);
119 }
120
122 T operator()(int i, int j = 0) const
123 {
124 return m_values.operator[](j * M + i);
125 }
126
128 friend std::ostream& operator<<(std::ostream& out, const PlainMatrix<T, M, N>& rhs)
129 {
130 for (int i = 0; i < rhs.rows(); ++i) {
131 out << rhs(i, 0) << ", ";
132 for (int j = 1; j < rhs.cols(); ++j) {
133 out << rhs(i, j);
134 }
135 out << "\n";
136 }
137 return out;
138 }
139
142 {
144 std::transform(begin(), end(), rhs.begin(), result.begin(), std::plus<T>());
145 return result;
146 }
147
150 {
152 std::transform(begin(), end(), rhs.begin(), result.begin(), std::minus<T>());
153 return result;
154 }
155
157 template <int O>
159 {
160 PlainMatrix<T, M, O> result{{}}; // Value initialize to zero
161 for (int m = 0; m < M; ++m) {
162 for (int o = 0; o < O; ++o) {
163 for (int n = 0; n < N; ++n) {
164 result(m, o) += operator()(m, n) * rhs(n, o);
165 }
166 }
167 }
168 return result;
169 }
170
173 {
175 std::transform(begin(), end(), result.begin(), [&rhs](const T & t) { return t / rhs; });
176 return result;
177 }
178
181 {
183 std::transform(rhs.begin(), rhs.end(), result.begin(), [&lhs](const T & t) {
184 return lhs / t;
185 });
186 return result;
187 }
188
190 template <int K, int L>
191 PlainMatrix<T, K, L> block(int i = 0, int j = 0) const
192 {
193 assert(K + i <= M && "Selected block reaches outside of the matrix");
194 assert(L + j <= N && "Selected block reaches outside of the matrix");
195
197 const int skipFront = j * M + i;
198 for (int iCol = 0; iCol < L; ++iCol) {
199 std::copy(begin() + skipFront + iCol * M,
200 begin() + skipFront + iCol * M + K,
201 result.data() + iCol * L);
202 }
203 return result;
204 }
205
207 template <int K>
209 {
210 return block<K, N>(0, 0);
211 }
212
214 template <int K>
216 {
217 return block<K, N>(M - K, 0);
218 }
219
221 static constexpr size_t size()
222 {
223 return M * N;
224 }
225
227 static constexpr int rows()
228 {
229 return M;
230 }
231
233 static constexpr int cols()
234 {
235 return N;
236 }
237
238 private:
240 T* begin()
241 {
242 return data();
243 }
244
246 T* end()
247 {
248 return data() + size();
249 }
250
252 const T* begin() const
253 {
254 return data();
255 }
256
258 const T* end() const
259 {
260 return data() + size();
261 }
262
263 private:
265 std::array<T, M* N> m_values;
266 };
267 }
269}
#define K(x)
macro autogenerated by FFTW
A matrix implementation to be used as an interface typ through out the track finder.
Definition: PlainMatrix.h:40
static const int S
Total number of elements.
Definition: PlainMatrix.h:46
const T * end() const
The end const_iterator of the flattened values.
Definition: PlainMatrix.h:258
static constexpr size_t size()
Total number of values in the matrix.
Definition: PlainMatrix.h:221
PlainMatrix(std::initializer_list< T > values)
Construct from initialiser list - also for value initialisation.
Definition: PlainMatrix.h:53
PlainMatrix< T, K, N > tail() const
Get the K bottom rows of the matrix.
Definition: PlainMatrix.h:215
static PlainMatrix< T, M, N > Constant(T t)
Construct a matrix with all elements set to a constant.
Definition: PlainMatrix.h:83
T operator()(int i, int j=0) const
Constant element access at the given row i and column j.
Definition: PlainMatrix.h:122
static constexpr int cols()
Total number of columns in the matrix.
Definition: PlainMatrix.h:233
PlainMatrix(std::array< T, S > values)
Construct from the given values.
Definition: PlainMatrix.h:61
static constexpr int rows()
Total number of rows in the matrix.
Definition: PlainMatrix.h:227
const T * data() const
Constant access to the flat value array.
Definition: PlainMatrix.h:98
PlainMatrix< T, K, L > block(int i=0, int j=0) const
Get the K x L block from the matrix starting at the element at position (i, j)
Definition: PlainMatrix.h:191
static PlainMatrix< T, M, N > Identity()
Construct an identity matrix.
Definition: PlainMatrix.h:74
PlainMatrix< T, M, N > operator+(const PlainMatrix< T, M, N > &rhs) const
Elementwise addition of two matrices.
Definition: PlainMatrix.h:141
friend std::ostream & operator<<(std::ostream &out, const PlainMatrix< T, M, N > &rhs)
Output operator for debugging purposes.
Definition: PlainMatrix.h:128
T * begin()
The begin iterator of the flattened values.
Definition: PlainMatrix.h:240
T * end()
The end iterator of the flattened values.
Definition: PlainMatrix.h:246
static PlainMatrix< T, M, N > Zero()
Construct a matrix initialized with zeros.
Definition: PlainMatrix.h:67
friend PlainMatrix< T, M, N > operator/(T lhs, PlainMatrix< T, M, N > &rhs)
Elementwise division of a number by the elements of the matrix.
Definition: PlainMatrix.h:180
PlainMatrix< T, M, N > operator/(T rhs) const
Elementwise division of the elements of the matrix by a number.
Definition: PlainMatrix.h:172
PlainMatrix< T, K, N > head() const
Get the K top rows of the matrix.
Definition: PlainMatrix.h:208
PlainMatrix< T, M, O > operator*(const PlainMatrix< T, N, O > &rhs) const
Naive matrix multiplication.
Definition: PlainMatrix.h:158
T operator[](int s) const
Constant flat element access at the given row i and column j.
Definition: PlainMatrix.h:110
PlainMatrix()=default
Default initializing of the matrix.
T & operator()(int i, int j=0)
Element access at the given row i and column j.
Definition: PlainMatrix.h:116
T & operator[](int s)
Flat element access at the given row i and column j.
Definition: PlainMatrix.h:104
T * data()
Access to the flat value array.
Definition: PlainMatrix.h:92
PlainMatrix< T, M, N > operator-(const PlainMatrix< T, M, N > &rhs) const
Elementwise subtraction of two matrices.
Definition: PlainMatrix.h:149
std::array< T, M *N > m_values
Memory of the flat value content.
Definition: PlainMatrix.h:265
const T * begin() const
The begin const_iterator of the flattened values.
Definition: PlainMatrix.h:252
Abstract base class for different kinds of events.