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 TrackingUtilities {
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 // cppcheck-suppress knownConditionTrueFalse ; the condition depends on the template arguments of the instantiation
133 for (int j = 1; j < rhs.cols(); ++j) {
134 out << rhs(i, j);
135 }
136 out << "\n";
137 }
138 return out;
139 }
140
143 {
145 std::transform(begin(), end(), rhs.begin(), result.begin(), std::plus<T>());
146 return result;
147 }
148
151 {
153 std::transform(begin(), end(), rhs.begin(), result.begin(), std::minus<T>());
154 return result;
155 }
156
158 template <int O>
160 {
161 PlainMatrix<T, M, O> result{{}}; // Value initialize to zero
162 for (int m = 0; m < M; ++m) {
163 for (int o = 0; o < O; ++o) {
164 for (int n = 0; n < N; ++n) {
165 result(m, o) += operator()(m, n) * rhs(n, o);
166 }
167 }
168 }
169 return result;
170 }
171
174 {
176 std::transform(begin(), end(), result.begin(), [&rhs](const T & t) { return t / rhs; });
177 return result;
178 }
179
182 {
184 std::transform(rhs.begin(), rhs.end(), result.begin(), [&lhs](const T & t) {
185 return lhs / t;
186 });
187 return result;
188 }
189
191 template <int K, int L>
192 PlainMatrix<T, K, L> block(int i = 0, int j = 0) const
193 {
194 assert(K + i <= M && "Selected block reaches outside of the matrix");
195 assert(L + j <= N && "Selected block reaches outside of the matrix");
196
198 const int skipFront = j * M + i;
199 for (int iCol = 0; iCol < L; ++iCol) {
200 std::copy(begin() + skipFront + iCol * M,
201 begin() + skipFront + iCol * M + K,
202 result.data() + iCol * L);
203 }
204 return result;
205 }
206
208 template <int K>
210 {
211 return block<K, N>(0, 0);
212 }
213
215 template <int K>
217 {
218 return block<K, N>(M - K, 0);
219 }
220
222 static constexpr size_t size()
223 {
224 return static_cast<size_t>(M) * static_cast<size_t>(N);
225 }
226
228 static constexpr int rows()
229 {
230 return M;
231 }
232
234 static constexpr int cols()
235 {
236 return N;
237 }
238
239 private:
241 T* begin()
242 {
243 return data();
244 }
245
247 T* end()
248 {
249 return data() + size();
250 }
251
253 const T* begin() const
254 {
255 return data();
256 }
257
259 const T* end() const
260 {
261 return data() + size();
262 }
263
264 private:
266 std::array<T, M* N> m_values{};
267 };
268 }
270}
#define K(x)
macro autogenerated by FFTW
static const int S
Total number of elements.
Definition PlainMatrix.h:46
const T * end() const
The end const_iterator of the flattened values.
static constexpr size_t size()
Total number of values in the matrix.
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.
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.
static constexpr int cols()
Total number of columns in the matrix.
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.
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)
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.
friend std::ostream & operator<<(std::ostream &out, const PlainMatrix< T, M, N > &rhs)
Output operator for debugging purposes.
T * begin()
The begin iterator of the flattened values.
T * end()
The end iterator of the flattened values.
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.
PlainMatrix< T, M, N > operator/(T rhs) const
Elementwise division of the elements of the matrix by a number.
PlainMatrix< T, K, N > head() const
Get the K top rows of the matrix.
PlainMatrix< T, M, O > operator*(const PlainMatrix< T, N, O > &rhs) const
Naive matrix multiplication.
T operator[](int s) const
Constant flat element access at the given row i and column j.
PlainMatrix()=default
Default initializing of the matrix.
T & operator()(int i, int j=0)
Element access at the given row i and column j.
T & operator[](int s)
Flat element access at the given row i and column j.
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.
std::array< T, M *N > m_values
Memory of the flat value content.
const T * begin() const
The begin const_iterator of the flattened values.
Abstract base class for different kinds of events.