Flow123d  release_3.0.0-1210-g1d9e2bd
armor.hh
Go to the documentation of this file.
1 #ifndef ARMOR_HH
2 #define ARMOR_HH
3 
4 //#define ARMA_DONT_USE_WRAPPER
5 //#define ARMA_NO_DEBUG
6 #include <armadillo>
7 #include <array>
8 #include "system/asserts.hh"
9 
10 namespace Armor {
11 
12 template <class Type, uint nRows, uint nCols>
13 class Mat
14 {
15 private:
16  Type * const data;
17  typedef typename arma::Mat<Type>::template fixed<nRows,nCols> ArmaType;
18 public:
19  Mat(Type * const mem)
20  : data(mem)
21  {}
22  inline Mat(const Armor::Mat<Type, nRows, nCols> & other)
23  : data(other.data)
24  {
25  for (uint i = 0; i < nRows * nCols; ++i) {
26  data[i] = other[i];
27  }
28  }
29  inline const Type * begin() const {
30  return data;
31  }
32  inline Type * begin() {
33  return data;
34  }
35  inline const Type * end() const {
36  return begin() + nCols * nRows;
37  }
38  inline Type * end() {
39  return begin() + nCols * nRows;
40  }
41  inline uint size() const {
42  return nRows * nCols;
43  }
44  inline Type * memptr() {
45  return begin();
46  }
47  inline const Type & operator[](uint index) const {
48  return data[index];
49  }
50  inline Type & operator[](uint index) {
51  return data[index];
52  }
53  inline const Type & operator()(uint row, uint col) const {
54  return data[row*nCols+col];
55  }
56  inline Type & operator()(uint row, uint col) {
57  return data[row*nCols+col];
58  }
59  inline ArmaType arma() const {
60  return ArmaType(begin());
61  }
63  for (uint i = 0; i < nRows * nCols; ++i) {
64  data[i] = other[i];
65  }
66  return *this;
67  }
68  inline const Mat<Type, nRows, nCols> & operator=(const ArmaType & other) {
69  for (uint i = 0; i < nRows * nCols; ++i) {
70  data[i] = other[i];
71  }
72  return *this;
73  }
74  inline const Mat<Type, nRows, nCols> & operator=(std::initializer_list<std::initializer_list<Type>> list) {
75  const auto * listIt = list.begin();
76  const Type * it;
77  for (uint i = 0; i < nRows; ++i) {
78  it = (listIt + i)->begin();
79  for (uint j = 0; j < nCols; ++j) {
80  data[i*nCols+j] = *(it + j);
81  }
82  }
83  return *this;
84  }
85  inline const Mat<Type, nRows, nCols> & operator=(std::initializer_list<Type> list) {
86  const Type * it = list.begin();
87  for (uint i = 0; i < nRows * nCols; ++i) {
88  data[i] = *(it + i);
89  }
90  return *this;
91  }
92  inline bool operator==(const ArmaType & other) {
93  const Type * first1 = begin();
94  const Type * last1 = end();
95  const Type * first2 = other.begin();
96  for (; first1 != last1; ++first1, ++first2) {
97  if (*first1 != *first2) {
98  return false;
99  }
100  }
101  return true;
102  }
103  inline bool operator==(const Mat<Type, nRows, nCols> & other) {
104  const Type * first1 = begin();
105  const Type * last1 = end();
106  const Type * first2 = other.begin();
107  for (; first1 != last1; ++first1, ++first2) {
108  if (*first1 != *first2) {
109  return false;
110  }
111  }
112  return true;
113  }
114 };
115 
116 
117 /**
118  * Array of Armor::Mat with given shape. Provides contiguous storage for the data and access to the array elements.
119  * The shape of the matrices is specified at run time, so the class Array is independent of additional template parameters.
120  * However, to access the array elements, one must use the templated method get().
121  */
122 template<class Type>
123 class Array {
124 public:
125  /**
126  * Construct array of Armor matrices.
127  * @param size Number of matrices in the array.
128  * @param nr Number of rows in each matrix.
129  * @param nc Number of columnts in each matrix.
130  */
131  Array(uint size, uint nr, uint nc = 1)
132  : nRows(nr),
133  nCols(nc),
134  data(size*nRows*nCols, 0)
135  {}
136 
137  /**
138  * Change number of elements in the array, while keeping the shape of arrays.
139  * @param size New size of array.
140  */
141  inline void resize(uint size)
142  {
143  data.resize(size*nRows*nCols);
144  }
145 
146  /**
147  * Insert new matrix to the end of the array.
148  * @param p Vector of values for the new matrix.
149  */
150  inline void push_back(const std::vector<Type> &p)
151  {
152  ASSERT_DBG( p.size() == nRows*nCols );
153  for (auto i : p) data.push_back( i );
154  }
155 
156  /**
157  * Return matrix at given position in array. The returned object is a Armor::Mat
158  * pointing to the respective data block in the Array's storage.
159  * @param i Index of matrix.
160  */
161  template<uint nr, uint nc = 1>
162  inline Mat<Type,nr,nc> get(uint i) const
163  {
164  ASSERT_DBG( (nr == nRows) && (nc == nCols) );
165  return Mat<Type,nr,nc>( (Type *)data.data() + i*nRows*nCols );
166  }
167 
168 private:
172 };
173 
174 
175 template <class Type, uint nRows, uint nCols>
176 inline Type dot(const Mat<Type, nRows, nCols> & a, const Mat<Type, nRows, nCols> & b) {
177  return arma::dot(a.arma(), b.arma());
178 }
179 
180 template <class Type, uint nRows, uint nCols>
181 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator+(const Mat<Type, nRows, nCols> & a, const Mat<Type, nRows, nCols> & b) {
182  return a.arma() + b.arma();
183 }
184 
185 template <class Type, uint nRows, uint nCols>
186 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator-(const Mat<Type, nRows, nCols> & a, const Mat<Type, nRows, nCols> & b) {
187  return a.arma() - b.arma();
188 }
189 
190 template <class Type, uint resRows, uint commonDimension, uint resCols>
191 inline typename arma::Mat<Type>::template fixed<resRows,resCols> operator*(const Mat<Type, resRows, commonDimension> & a, const Mat<Type, commonDimension, resCols> & b) {
192  return a.arma() * b.arma();
193 }
194 
195 template <class Type, uint nRows, uint nCols>
196 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator%(const Mat<Type, nRows, nCols> & a, const Mat<Type, nRows, nCols> & b) {
197  return a.arma() % b.arma();
198 }
199 
200 template <class Type, uint nRows, uint nCols>
201 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator*(Type number, const Mat<Type, nRows, nCols> & a) {
202  return number * a.arma();
203 }
204 
205 template <class Type, uint nRows, uint nCols>
206 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator/(const Mat<Type, nRows, nCols> & a, Type number) {
207  return a.arma() / number;
208 }
209 
210 template <uint N>
212 
213 template <uint N, uint M>
215 
217 
218 }
219 
220 #endif
const Mat< Type, nRows, nCols > & operator=(const Mat< Type, nRows, nCols > &other)
Definition: armor.hh:62
const Mat< Type, nRows, nCols > & operator=(const ArmaType &other)
Definition: armor.hh:68
void resize(uint size)
Definition: armor.hh:141
unsigned int uint
const Mat< Type, nRows, nCols > & operator=(std::initializer_list< std::initializer_list< Type >> list)
Definition: armor.hh:74
arma::Mat< Type >::template fixed< nRows, nCols > ArmaType
Definition: armor.hh:17
const Type * end() const
Definition: armor.hh:35
arma::Mat< Type >::template fixed< nRows, nCols > operator+(const Mat< Type, nRows, nCols > &a, const Mat< Type, nRows, nCols > &b)
Definition: armor.hh:181
Array(uint size, uint nr, uint nc=1)
Definition: armor.hh:131
arma::Mat< Type >::template fixed< nRows, nCols > operator%(const Mat< Type, nRows, nCols > &a, const Mat< Type, nRows, nCols > &b)
Definition: armor.hh:196
Definitions of ASSERTS.
Definition: armor.hh:10
Type * memptr()
Definition: armor.hh:44
Type & operator[](uint index)
Definition: armor.hh:50
arma::Mat< Type >::template fixed< resRows, resCols > operator*(const Mat< Type, resRows, commonDimension > &a, const Mat< Type, commonDimension, resCols > &b)
Definition: armor.hh:191
const Mat< Type, nRows, nCols > & operator=(std::initializer_list< Type > list)
Definition: armor.hh:85
const Type * begin() const
Definition: armor.hh:29
Type & operator()(uint row, uint col)
Definition: armor.hh:56
void push_back(const std::vector< Type > &p)
Definition: armor.hh:150
Mat(Type *const mem)
Definition: armor.hh:19
arma::Mat< Type >::template fixed< nRows, nCols > operator/(const Mat< Type, nRows, nCols > &a, Type number)
Definition: armor.hh:206
uint nRows
Definition: armor.hh:169
bool operator==(const Mat< Type, nRows, nCols > &other)
Definition: armor.hh:103
std::vector< Type > data
Definition: armor.hh:171
arma::Mat< Type >::template fixed< nRows, nCols > operator-(const Mat< Type, nRows, nCols > &a, const Mat< Type, nRows, nCols > &b)
Definition: armor.hh:186
Mat(const Armor::Mat< Type, nRows, nCols > &other)
Definition: armor.hh:22
Type * end()
Definition: armor.hh:38
const Type & operator()(uint row, uint col) const
Definition: armor.hh:53
Type dot(const Mat< Type, nRows, nCols > &a, const Mat< Type, nRows, nCols > &b)
Definition: armor.hh:176
uint size() const
Definition: armor.hh:41
Type *const data
Definition: armor.hh:16
uint nCols
Definition: armor.hh:170
ArmaType arma() const
Definition: armor.hh:59
#define ASSERT_DBG(expr)
Definition: asserts.hh:349
bool operator==(const ArmaType &other)
Definition: armor.hh:92
Type * begin()
Definition: armor.hh:32
const Type & operator[](uint index) const
Definition: armor.hh:47