Belle II Software development
PlainMatrix< T, M, N > Class Template Reference

A matrix implementation to be used as an interface typ through out the track finder. More...

#include <PlainMatrix.h>

Public Member Functions

 PlainMatrix ()=default
 Default initializing of the matrix.
 
 PlainMatrix (std::initializer_list< T > values)
 Construct from initialiser list - also for value initialisation.
 
 PlainMatrix (std::array< T, S > values)
 Construct from the given values.
 
T * data ()
 Access to the flat value array.
 
const T * data () const
 Constant access to the flat value array.
 
T & operator[] (int s)
 Flat element access at the given row i and column j.
 
operator[] (int s) const
 Constant flat element access at the given row i and column j.
 
T & operator() (int i, int j=0)
 Element access at the given row i and column j.
 
operator() (int i, int j=0) const
 Constant element access at the given row i and column j.
 
PlainMatrix< T, M, N > operator+ (const PlainMatrix< T, M, N > &rhs) const
 Elementwise addition of two matrices.
 
PlainMatrix< T, M, N > operator- (const PlainMatrix< T, M, N > &rhs) const
 Elementwise subtraction of two matrices.
 
template<int O>
PlainMatrix< T, M, O > operator* (const PlainMatrix< T, N, O > &rhs) const
 Naive matrix multiplication.
 
PlainMatrix< T, M, N > operator/ (T rhs) const
 Elementwise division of the elements of the matrix by a number.
 
template<int K, int L>
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)
 
template<int K>
PlainMatrix< T, K, N > head () const
 Get the K top rows of the matrix.
 
template<int K>
PlainMatrix< T, K, N > tail () const
 Get the K bottom rows of the matrix.
 

Static Public Member Functions

static PlainMatrix< T, M, N > Zero ()
 Construct a matrix initialized with zeros.
 
static PlainMatrix< T, M, N > Identity ()
 Construct an identity matrix.
 
static PlainMatrix< T, M, N > Constant (T t)
 Construct a matrix with all elements set to a constant.
 
static constexpr size_t size ()
 Total number of values in the matrix.
 
static constexpr int rows ()
 Total number of rows in the matrix.
 
static constexpr int cols ()
 Total number of columns in the matrix.
 

Private Member Functions

T * begin ()
 The begin iterator of the flattened values.
 
T * end ()
 The end iterator of the flattened values.
 
const T * begin () const
 The begin const_iterator of the flattened values.
 
const T * end () const
 The end const_iterator of the flattened values.
 

Private Attributes

std::array< T, M *N > m_values {}
 Memory of the flat value content.
 

Static Private Attributes

static const int S = M * N
 Total number of elements.
 

Friends

std::ostream & operator<< (std::ostream &out, const PlainMatrix< T, M, N > &rhs)
 Output operator for debugging purposes.
 
PlainMatrix< T, M, N > operator/ (T lhs, PlainMatrix< T, M, N > &rhs)
 Elementwise division of a number by the elements of the matrix.
 

Detailed Description

template<class T, int M, int N>
class Belle2::TrackingUtilities::PlainMatrix< T, M, N >

A matrix implementation to be used as an interface typ through out the track finder.

This represents a fixed sized matrix with dimensions known at compile time to replace the Eigen::Matrix as vocabulary type at object interfaces. Because of its limited feature set it significantly reduces the compilation time of the whole cdc track finding as the Eigen headers do not have to be included in each translation unit.

It exposes only a limited number of methods for basic interactions. For all the linear algebra algorithms use the mapToEigen function in EigenView.h But it is advised to use the linear algebra only in .cc such that transitive includes do not suffer from Eigens heavily templated implementation.

The arrangement of element in this matrix is in column-major order (same as the default for Eigen).

Definition at line 40 of file PlainMatrix.h.

Constructor & Destructor Documentation

◆ PlainMatrix() [1/2]

template<class T, int M, int N>
PlainMatrix ( std::initializer_list< T > values)
inlineexplicit

Construct from initialiser list - also for value initialisation.

Definition at line 53 of file PlainMatrix.h.

54 : m_values{}
55 {
56 assert(m_values.size() >= values.size());
57 std::copy(values.begin(), values.end(), m_values.begin());
58 }

◆ PlainMatrix() [2/2]

template<class T, int M, int N>
PlainMatrix ( std::array< T, S > values)
inlineexplicit

Construct from the given values.

Definition at line 61 of file PlainMatrix.h.

62 : m_values(values)
63 {
64 }

Member Function Documentation

◆ begin() [1/2]

template<class T, int M, int N>
T * begin ( )
inlineprivate

The begin iterator of the flattened values.

Definition at line 241 of file PlainMatrix.h.

242 {
243 return data();
244 }

◆ begin() [2/2]

template<class T, int M, int N>
const T * begin ( ) const
inlineprivate

The begin const_iterator of the flattened values.

Definition at line 253 of file PlainMatrix.h.

254 {
255 return data();
256 }

◆ block()

template<class T, int M, int N>
template<int K, int L>
PlainMatrix< T, K, L > block ( int i = 0,
int j = 0 ) const
inline

Get the K x L block from the matrix starting at the element at position (i, j)

Definition at line 192 of file PlainMatrix.h.

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
197 PlainMatrix<T, K, L> result;
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 }
#define K(x)
macro autogenerated by FFTW

◆ cols()

template<class T, int M, int N>
static constexpr int cols ( )
inlinestaticconstexpr

Total number of columns in the matrix.

Definition at line 234 of file PlainMatrix.h.

235 {
236 return N;
237 }

◆ Constant()

template<class T, int M, int N>
static PlainMatrix< T, M, N > Constant ( T t)
inlinestatic

Construct a matrix with all elements set to a constant.

Definition at line 83 of file PlainMatrix.h.

84 {
85 PlainMatrix<T, M, N> result;
86 result.m_values.fill(t);
87 return result;
88 }

◆ data() [1/2]

template<class T, int M, int N>
T * data ( )
inline

Access to the flat value array.

Definition at line 92 of file PlainMatrix.h.

93 {
94 return m_values.data();
95 }

◆ data() [2/2]

template<class T, int M, int N>
const T * data ( ) const
inline

Constant access to the flat value array.

Definition at line 98 of file PlainMatrix.h.

99 {
100 return m_values.data();
101 }

◆ end() [1/2]

template<class T, int M, int N>
T * end ( )
inlineprivate

The end iterator of the flattened values.

Definition at line 247 of file PlainMatrix.h.

248 {
249 return data() + size();
250 }

◆ end() [2/2]

template<class T, int M, int N>
const T * end ( ) const
inlineprivate

The end const_iterator of the flattened values.

Definition at line 259 of file PlainMatrix.h.

260 {
261 return data() + size();
262 }

◆ head()

template<class T, int M, int N>
template<int K>
PlainMatrix< T, K, N > head ( ) const
inline

Get the K top rows of the matrix.

Definition at line 209 of file PlainMatrix.h.

210 {
211 return block<K, N>(0, 0);
212 }

◆ Identity()

template<class T, int M, int N>
static PlainMatrix< T, M, N > Identity ( )
inlinestatic

Construct an identity matrix.

Definition at line 74 of file PlainMatrix.h.

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 }

◆ operator()() [1/2]

template<class T, int M, int N>
T & operator() ( int i,
int j = 0 )
inline

Element access at the given row i and column j.

Definition at line 116 of file PlainMatrix.h.

117 {
118 return m_values.operator[](j * M + i);
119 }

◆ operator()() [2/2]

template<class T, int M, int N>
T operator() ( int i,
int j = 0 ) const
inline

Constant element access at the given row i and column j.

Definition at line 122 of file PlainMatrix.h.

123 {
124 return m_values.operator[](j * M + i);
125 }

◆ operator*()

template<class T, int M, int N>
template<int O>
PlainMatrix< T, M, O > operator* ( const PlainMatrix< T, N, O > & rhs) const
inline

Naive matrix multiplication.

Definition at line 159 of file PlainMatrix.h.

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 }

◆ operator+()

template<class T, int M, int N>
PlainMatrix< T, M, N > operator+ ( const PlainMatrix< T, M, N > & rhs) const
inline

Elementwise addition of two matrices.

Definition at line 142 of file PlainMatrix.h.

143 {
144 PlainMatrix<T, M, N> result;
145 std::transform(begin(), end(), rhs.begin(), result.begin(), std::plus<T>());
146 return result;
147 }

◆ operator-()

template<class T, int M, int N>
PlainMatrix< T, M, N > operator- ( const PlainMatrix< T, M, N > & rhs) const
inline

Elementwise subtraction of two matrices.

Definition at line 150 of file PlainMatrix.h.

151 {
152 PlainMatrix<T, M, N> result;
153 std::transform(begin(), end(), rhs.begin(), result.begin(), std::minus<T>());
154 return result;
155 }

◆ operator/()

template<class T, int M, int N>
PlainMatrix< T, M, N > operator/ ( T rhs) const
inline

Elementwise division of the elements of the matrix by a number.

Definition at line 173 of file PlainMatrix.h.

174 {
175 PlainMatrix<T, M, N> result;
176 std::transform(begin(), end(), result.begin(), [&rhs](const T & t) { return t / rhs; });
177 return result;
178 }

◆ operator[]() [1/2]

template<class T, int M, int N>
T & operator[] ( int s)
inline

Flat element access at the given row i and column j.

Definition at line 104 of file PlainMatrix.h.

105 {
106 return m_values[s];
107 }

◆ operator[]() [2/2]

template<class T, int M, int N>
T operator[] ( int s) const
inline

Constant flat element access at the given row i and column j.

Definition at line 110 of file PlainMatrix.h.

111 {
112 return m_values[s];
113 }

◆ rows()

template<class T, int M, int N>
static constexpr int rows ( )
inlinestaticconstexpr

Total number of rows in the matrix.

Definition at line 228 of file PlainMatrix.h.

229 {
230 return M;
231 }

◆ size()

template<class T, int M, int N>
static constexpr size_t size ( )
inlinestaticconstexpr

Total number of values in the matrix.

Definition at line 222 of file PlainMatrix.h.

223 {
224 return static_cast<size_t>(M) * static_cast<size_t>(N);
225 }

◆ tail()

template<class T, int M, int N>
template<int K>
PlainMatrix< T, K, N > tail ( ) const
inline

Get the K bottom rows of the matrix.

Definition at line 216 of file PlainMatrix.h.

217 {
218 return block<K, N>(M - K, 0);
219 }

◆ Zero()

template<class T, int M, int N>
static PlainMatrix< T, M, N > Zero ( )
inlinestatic

Construct a matrix initialized with zeros.

Definition at line 67 of file PlainMatrix.h.

68 {
69 PlainMatrix<T, M, N> result{{}}; // Value initialize to zero
70 return result;
71 }

Friends And Related Symbol Documentation

◆ operator/

template<class T, int M, int N>
PlainMatrix< T, M, N > operator/ ( T lhs,
PlainMatrix< T, M, N > & rhs )
friend

Elementwise division of a number by the elements of the matrix.

Definition at line 181 of file PlainMatrix.h.

182 {
183 PlainMatrix<T, M, N> result;
184 std::transform(rhs.begin(), rhs.end(), result.begin(), [&lhs](const T & t) {
185 return lhs / t;
186 });
187 return result;
188 }

◆ operator<<

template<class T, int M, int N>
std::ostream & operator<< ( std::ostream & out,
const PlainMatrix< T, M, N > & rhs )
friend

Output operator for debugging purposes.

Definition at line 128 of file PlainMatrix.h.

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 }

Member Data Documentation

◆ m_values

template<class T, int M, int N>
std::array<T, M* N> m_values {}
private

Memory of the flat value content.

Definition at line 266 of file PlainMatrix.h.

266{};

◆ S

template<class T, int M, int N>
const int S = M * N
staticprivate

Total number of elements.

Definition at line 46 of file PlainMatrix.h.


The documentation for this class was generated from the following file: