Belle II Software  release-05-02-19
PlainMatrix.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <ostream>
13 #include <array>
14 #include <algorithm>
15 #include <cassert>
16 
17 namespace Belle2 {
22  namespace TrackFindingCDC {
23 
41  template <class T, int M, int N>
42  class PlainMatrix {
43 
44  static_assert(M > 0, "Dimensions must be greater zero");
45  static_assert(N > 0, "Dimensions must be greater zero");
46 
48  static const int S = M * N;
49 
50  public:
52  PlainMatrix() = default;
53 
55  explicit PlainMatrix(std::initializer_list<T> values)
57  {
58  assert(m_values.size() >= values.size());
59  std::copy(values.begin(), values.end(), m_values.begin());
60  }
61 
63  explicit PlainMatrix(std::array<T, S> values)
64  : m_values(values)
65  {
66  }
67 
69  static PlainMatrix<T, M, N> Zero()
70  {
71  PlainMatrix<T, M, N> result{{}}; // Value initialize to zero
72  return result;
73  }
74 
76  static PlainMatrix<T, M, N> Identity()
77  {
78  PlainMatrix<T, M, N> result{{}}; // Value initialize to zero
79  for (int s = 0; s < M * N; s += M + 1)
80  result.m_values[s] = 1;
81  return result;
82  }
83 
85  static PlainMatrix<T, M, N> Constant(T t)
86  {
87  PlainMatrix<T, M, N> result;
88  result.m_values.fill(t);
89  return result;
90  }
91 
92  public:
94  T* data()
95  {
96  return m_values.data();
97  }
98 
100  const T* data() const
101  {
102  return m_values.data();
103  }
104 
106  T& operator[](int s)
107  {
108  return m_values[s];
109  }
110 
112  T operator[](int s) const
113  {
114  return m_values[s];
115  }
116 
118  T& operator()(int i, int j = 0)
119  {
120  return m_values.operator[](j * M + i);
121  }
122 
124  T operator()(int i, int j = 0) const
125  {
126  return m_values.operator[](j * M + i);
127  }
128 
130  friend std::ostream& operator<<(std::ostream& out, const PlainMatrix<T, M, N>& rhs)
131  {
132  for (int i = 0; i < rhs.rows(); ++i) {
133  out << rhs(i, 0) << ", ";
134  for (int j = 1; j < rhs.cols(); ++j) {
135  out << rhs(i, j);
136  }
137  out << "\n";
138  }
139  return out;
140  }
141 
143  PlainMatrix<T, M, N> operator+(const PlainMatrix<T, M, N>& rhs) const
144  {
145  PlainMatrix<T, M, N> result;
146  std::transform(begin(), end(), rhs.begin(), result.begin(), std::plus<T>());
147  return result;
148  }
149 
152  {
153  PlainMatrix<T, M, N> result;
154  std::transform(begin(), end(), rhs.begin(), result.begin(), std::minus<T>());
155  return result;
156  }
157 
159  template <int O>
161  {
162  PlainMatrix<T, M, O> result{{}}; // Value initialize to zero
163  for (int m = 0; m < M; ++m) {
164  for (int o = 0; o < O; ++o) {
165  for (int n = 0; n < N; ++n) {
166  result(m, o) += operator()(m, n) * rhs(n, o);
167  }
168  }
169  }
170  return result;
171  }
172 
174  PlainMatrix<T, M, N> operator/(T rhs) const
175  {
176  PlainMatrix<T, M, N> result;
177  std::transform(begin(), end(), result.begin(), [&rhs](const T & t) { return t / rhs; });
178  return result;
179  }
180 
183  {
184  PlainMatrix<T, M, N> result;
185  std::transform(rhs.begin(), rhs.end(), result.begin(), [&lhs](const T & t) {
186  return lhs / t;
187  });
188  return result;
189  }
190 
192  template <int K, int L>
193  PlainMatrix<T, K, L> block(int i = 0, int j = 0) const
194  {
195  assert(K + i <= M && "Selected block reaches outside of the matrix");
196  assert(L + j <= N && "Selected block reaches outside of the matrix");
197 
198  PlainMatrix<T, K, L> result;
199  const int skipFront = j * M + i;
200  for (int iCol = 0; iCol < L; ++iCol) {
201  std::copy(begin() + skipFront + iCol * M,
202  begin() + skipFront + iCol * M + K,
203  result.data() + iCol * L);
204  }
205  return result;
206  }
207 
209  template <int K>
210  PlainMatrix<T, K, N> head() const
211  {
212  return block<K, N>(0, 0);
213  }
214 
216  template <int K>
217  PlainMatrix<T, K, N> tail() const
218  {
219  return block<K, N>(M - K, 0);
220  }
221 
223  static constexpr size_t size()
224  {
225  return M * N;
226  }
227 
229  static constexpr int rows()
230  {
231  return M;
232  }
233 
235  static constexpr int cols()
236  {
237  return N;
238  }
239 
240  private:
242  T* begin()
243  {
244  return data();
245  }
246 
248  T* end()
249  {
250  return data() + size();
251  }
252 
254  const T* begin() const
255  {
256  return data();
257  }
258 
260  const T* end() const
261  {
262  return data() + size();
263  }
264 
265  private:
267  std::array<T, M* N> m_values;
268  };
269  }
271 }
Belle2::TrackFindingCDC::PlainMatrix::Zero
static PlainMatrix< T, M, N > Zero()
Construct a matrix initialized with zeros.
Definition: PlainMatrix.h:77
Belle2::TrackFindingCDC::PlainMatrix::cols
static constexpr int cols()
Total number of columns in the matrix.
Definition: PlainMatrix.h:243
Belle2::TrackFindingCDC::PlainMatrix::end
T * end()
The end iterator of the flattened values.
Definition: PlainMatrix.h:256
Belle2::TrackFindingCDC::PlainMatrix::operator[]
T & operator[](int s)
Flat element access at the given row i and column j.
Definition: PlainMatrix.h:114
Belle2::TrackFindingCDC::PlainMatrix::head
PlainMatrix< T, K, N > head() const
Get the K top rows of the matrix.
Definition: PlainMatrix.h:218
Belle2::TrackFindingCDC::PlainMatrix::begin
T * begin()
The begin iterator of the flattened values.
Definition: PlainMatrix.h:250
Belle2::TrackFindingCDC::PlainMatrix::operator<<
friend std::ostream & operator<<(std::ostream &out, const PlainMatrix< T, M, N > &rhs)
Output operator for debugging purposes.
Definition: PlainMatrix.h:138
Belle2::TrackFindingCDC::PlainMatrix::block
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:201
Belle2::TrackFindingCDC::PlainMatrix::tail
PlainMatrix< T, K, N > tail() const
Get the K bottom rows of the matrix.
Definition: PlainMatrix.h:225
Belle2::TrackFindingCDC::PlainMatrix::data
T * data()
Access to the flat value array.
Definition: PlainMatrix.h:102
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::PlainMatrix::m_values
std::array< T, M *N > m_values
Memory of the flat value content.
Definition: PlainMatrix.h:275
Belle2::TrackFindingCDC::PlainMatrix::operator+
PlainMatrix< T, M, N > operator+(const PlainMatrix< T, M, N > &rhs) const
Elementwise addition of two matrices.
Definition: PlainMatrix.h:151
Belle2::TrackFindingCDC::PlainMatrix::rows
static constexpr int rows()
Total number of rows in the matrix.
Definition: PlainMatrix.h:237
Belle2::TrackFindingCDC::PlainMatrix::Constant
static PlainMatrix< T, M, N > Constant(T t)
Construct a matrix with all elements set to a constant.
Definition: PlainMatrix.h:93
Belle2::TrackFindingCDC::PlainMatrix::operator*
PlainMatrix< T, M, O > operator*(const PlainMatrix< T, N, O > &rhs) const
Naive matrix multiplication.
Definition: PlainMatrix.h:168
Belle2::TrackFindingCDC::PlainMatrix::operator/
PlainMatrix< T, M, N > operator/(T rhs) const
Elementwise division of the elements of the matrix by a number.
Definition: PlainMatrix.h:182
Belle2::TrackFindingCDC::PlainMatrix::operator-
PlainMatrix< T, M, N > operator-(const PlainMatrix< T, M, N > &rhs) const
Elementwise subtraction of two matrices.
Definition: PlainMatrix.h:159
Belle2::TrackFindingCDC::PlainMatrix
A matrix implementation to be used as an interface typ through out the track finder.
Definition: PlainMatrix.h:50
Belle2::TrackFindingCDC::PlainMatrix::size
static constexpr size_t size()
Total number of values in the matrix.
Definition: PlainMatrix.h:231
Belle2::TrackFindingCDC::PlainMatrix::S
static const int S
Total number of elements.
Definition: PlainMatrix.h:56
Belle2::TrackFindingCDC::PlainMatrix::PlainMatrix
PlainMatrix()=default
Default initializing of the matrix.
Belle2::TrackFindingCDC::PlainMatrix::Identity
static PlainMatrix< T, M, N > Identity()
Construct an identity matrix.
Definition: PlainMatrix.h:84
Belle2::TrackFindingCDC::PlainMatrix::operator()
T & operator()(int i, int j=0)
Element access at the given row i and column j.
Definition: PlainMatrix.h:126
Belle2::TrackFindingCDC::PlainMatrix::PlainMatrix
PlainMatrix(std::initializer_list< T > values)
Construct from initialiser list - also for value initialisation.
Definition: PlainMatrix.h:63