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::TrackFindingCDC::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]

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 }
std::array< T, M *N > m_values
Memory of the flat value content.
Definition: PlainMatrix.h:265

◆ PlainMatrix() [2/2]

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]

T * begin ( )
inlineprivate

The begin iterator of the flattened values.

Definition at line 240 of file PlainMatrix.h.

241 {
242 return data();
243 }
T * data()
Access to the flat value array.
Definition: PlainMatrix.h:92

◆ begin() [2/2]

const T * begin ( ) const
inlineprivate

The begin const_iterator of the flattened values.

Definition at line 252 of file PlainMatrix.h.

253 {
254 return data();
255 }

◆ block()

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 191 of file PlainMatrix.h.

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
196 PlainMatrix<T, K, L> result;
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 }
#define K(x)
macro autogenerated by FFTW
T * begin()
The begin iterator of the flattened values.
Definition: PlainMatrix.h:240

◆ cols()

static constexpr int cols ( )
inlinestaticconstexpr

Total number of columns in the matrix.

Definition at line 233 of file PlainMatrix.h.

234 {
235 return N;
236 }

◆ Constant()

static PlainMatrix< T, M, N > Constant ( 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]

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]

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]

T * end ( )
inlineprivate

The end iterator of the flattened values.

Definition at line 246 of file PlainMatrix.h.

247 {
248 return data() + size();
249 }
static constexpr size_t size()
Total number of values in the matrix.
Definition: PlainMatrix.h:221

◆ end() [2/2]

const T * end ( ) const
inlineprivate

The end const_iterator of the flattened values.

Definition at line 258 of file PlainMatrix.h.

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

◆ head()

PlainMatrix< T, K, N > head ( ) const
inline

Get the K top rows of the matrix.

Definition at line 208 of file PlainMatrix.h.

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

◆ Identity()

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]

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]

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*()

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

Naive matrix multiplication.

Definition at line 158 of file PlainMatrix.h.

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 }
T & operator()(int i, int j=0)
Element access at the given row i and column j.
Definition: PlainMatrix.h:116

◆ operator+()

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

Elementwise addition of two matrices.

Definition at line 141 of file PlainMatrix.h.

142 {
143 PlainMatrix<T, M, N> result;
144 std::transform(begin(), end(), rhs.begin(), result.begin(), std::plus<T>());
145 return result;
146 }
T * end()
The end iterator of the flattened values.
Definition: PlainMatrix.h:246

◆ operator-()

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

Elementwise subtraction of two matrices.

Definition at line 149 of file PlainMatrix.h.

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

◆ operator/()

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

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

Definition at line 172 of file PlainMatrix.h.

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

◆ operator[]() [1/2]

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]

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()

static constexpr int rows ( )
inlinestaticconstexpr

Total number of rows in the matrix.

Definition at line 227 of file PlainMatrix.h.

228 {
229 return M;
230 }

◆ size()

static constexpr size_t size ( )
inlinestaticconstexpr

Total number of values in the matrix.

Definition at line 221 of file PlainMatrix.h.

222 {
223 return M * N;
224 }

◆ tail()

PlainMatrix< T, K, N > tail ( ) const
inline

Get the K bottom rows of the matrix.

Definition at line 215 of file PlainMatrix.h.

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

◆ Zero()

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 Function Documentation

◆ operator/

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

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

Definition at line 180 of file PlainMatrix.h.

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

◆ operator<<

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 for (int j = 1; j < rhs.cols(); ++j) {
133 out << rhs(i, j);
134 }
135 out << "\n";
136 }
137 return out;
138 }

Member Data Documentation

◆ m_values

std::array<T, M* N> m_values
private

Memory of the flat value content.

Definition at line 265 of file PlainMatrix.h.

◆ S

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: