Flow123d  release_3.0.0-1150-g11be08a
armor.hh
Go to the documentation of this file.
1 //#define ARMA_DONT_USE_WRAPPER
2 //#define ARMA_NO_DEBUG
3 #include <armadillo>
4 #include <array>
5 
6 namespace Armor {
7 
8 template <class Type, uint nRows, uint nCols>
9 class Mat
10 {
11 private:
12  std::array<std::array<Type, nRows>, nCols> data;
13  typedef typename arma::Mat<Type>::template fixed<nRows,nCols> ArmaType;
14 public:
15  Mat() {
16  for (uint i = 0; i < nRows * nCols; ++i) {
17  data[0][i] = 0;
18  }
19  }
20  Mat(std::initializer_list<std::initializer_list<Type>> list) {
21  const auto * listIt = list.begin();
22  const Type * it;
23  for (uint i = 0; i < nRows; ++i) {
24  it = (listIt + i)->begin();
25  for (uint j = 0; j < nCols; ++j) {
26  data[j][i] = *(it + j);
27  }
28  }
29  }
30  Mat(std::initializer_list<Type> list) {
31  const Type * it = list.begin();
32  for (uint i = 0; i < nRows * nCols; ++i) {
33  data[0][i] = *(it + i);
34  }
35  }
36  inline Mat(const Armor::Mat<Type, nRows, nCols> & other) {
37  for (uint i = 0; i < nRows * nCols; ++i) {
38  data[0][i] = other[i];
39  }
40  }
41  inline Mat(const ArmaType & other) {
42  for (uint i = 0; i < nRows * nCols; ++i) {
43  data[0][i] = other[i];
44  }
45  }
46  inline const Type * begin() const {
47  return data.data()->data();
48  }
49  inline Type * begin() {
50  return data.data()->data();
51  }
52  inline const Type * end() const {
53  return begin() + nCols * nRows;
54  }
55  inline Type * end() {
56  return begin() + nCols * nRows;
57  }
58  inline uint size() const {
59  return nRows * nCols;
60  }
61  inline Type * memptr() {
62  return begin();
63  }
64  inline const Type & operator[](uint index) const {
65  return data[0][index];
66  }
67  inline Type & operator[](uint index) {
68  return data[0][index];
69  }
70  inline const Type & operator()(uint row, uint col) const {
71  return data[col][row];
72  }
73  inline Type & operator()(uint row, uint col) {
74  return data[col][row];
75  }
76  inline ArmaType arma() const {
77  return ArmaType(begin());
78  }
80  for (uint i = 0; i < nRows * nCols; ++i) {
81  data[0][i] = other[i];
82  }
83  return *this;
84  }
85  inline const Mat<Type, nRows, nCols> & operator=(const ArmaType & other) {
86  for (uint i = 0; i < nRows * nCols; ++i) {
87  data[0][i] = other[i];
88  }
89  return *this;
90  }
91  inline const Mat<Type, nRows, nCols> & operator=(std::initializer_list<std::initializer_list<Type>> list) {
92  const auto * listIt = list.begin();
93  const Type * it;
94  for (uint i = 0; i < nRows; ++i) {
95  it = (listIt + i)->begin();
96  for (uint j = 0; j < nCols; ++j) {
97  data[j][i] = *(it + j);
98  }
99  }
100  return *this;
101  }
102  inline const Mat<Type, nRows, nCols> & operator=(std::initializer_list<Type> list) {
103  const Type * it = list.begin();
104  for (uint i = 0; i < nRows * nCols; ++i) {
105  data[0][i] = *(it + i);
106  }
107  return *this;
108  }
109  inline bool operator==(const ArmaType & other) {
110  const Type * first1 = begin();
111  const Type * last1 = end();
112  const Type * first2 = other.begin();
113  for (; first1 != last1; ++first1, ++first2) {
114  if (*first1 != *first2) {
115  return false;
116  }
117  }
118  return true;
119  }
120  inline bool operator==(const Mat<Type, nRows, nCols> & other) {
121  const Type * first1 = begin();
122  const Type * last1 = end();
123  const Type * first2 = other.begin();
124  for (; first1 != last1; ++first1, ++first2) {
125  if (*first1 != *first2) {
126  return false;
127  }
128  }
129  return true;
130  }
131 };
132 
133 template <class Type, uint nRows, uint nCols>
134 inline Type dot(const Mat<Type, nRows, nCols> & a, const Mat<Type, nRows, nCols> & b) {
135  return arma::dot(a.arma(), b.arma());
136 }
137 
138 template <class Type, uint nRows, uint nCols>
139 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator+(const Mat<Type, nRows, nCols> & a, const Mat<Type, nRows, nCols> & b) {
140  return a.arma() + b.arma();
141 }
142 
143 template <class Type, uint nRows, uint nCols>
144 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator-(const Mat<Type, nRows, nCols> & a, const Mat<Type, nRows, nCols> & b) {
145  return a.arma() - b.arma();
146 }
147 
148 template <class Type, uint resRows, uint commonDimension, uint resCols>
149 inline typename arma::Mat<Type>::template fixed<resRows,resCols> operator*(const Mat<Type, resRows, commonDimension> & a, const Mat<Type, commonDimension, resCols> & b) {
150  return a.arma() * b.arma();
151 }
152 
153 template <class Type, uint nRows, uint nCols>
154 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator%(const Mat<Type, nRows, nCols> & a, const Mat<Type, nRows, nCols> & b) {
155  return a.arma() % b.arma();
156 }
157 
158 template <class Type, uint nRows, uint nCols>
159 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator*(Type number, const Mat<Type, nRows, nCols> & a) {
160  return number * a.arma();
161 }
162 
163 template <class Type, uint nRows, uint nCols>
164 inline typename arma::Mat<Type>::template fixed<nRows,nCols> operator/(const Mat<Type, nRows, nCols> & a, Type number) {
165  return a.arma() / number;
166 }
167 
168 template <uint N>
170 
171 template <uint N, uint M>
173 
174 }
const Mat< Type, nRows, nCols > & operator=(const Mat< Type, nRows, nCols > &other)
Definition: armor.hh:79
const Mat< Type, nRows, nCols > & operator=(const ArmaType &other)
Definition: armor.hh:85
unsigned int uint
const Mat< Type, nRows, nCols > & operator=(std::initializer_list< std::initializer_list< Type >> list)
Definition: armor.hh:91
arma::Mat< Type >::template fixed< nRows, nCols > ArmaType
Definition: armor.hh:13
const Type * end() const
Definition: armor.hh:52
arma::Mat< Type >::template fixed< nRows, nCols > operator+(const Mat< Type, nRows, nCols > &a, const Mat< Type, nRows, nCols > &b)
Definition: armor.hh:139
std::array< std::array< Type, nRows >, nCols > data
Definition: armor.hh:12
Mat(std::initializer_list< Type > list)
Definition: armor.hh:30
arma::Mat< Type >::template fixed< nRows, nCols > operator%(const Mat< Type, nRows, nCols > &a, const Mat< Type, nRows, nCols > &b)
Definition: armor.hh:154
Definition: armor.hh:6
Type * memptr()
Definition: armor.hh:61
Type & operator[](uint index)
Definition: armor.hh:67
arma::Mat< Type >::template fixed< resRows, resCols > operator*(const Mat< Type, resRows, commonDimension > &a, const Mat< Type, commonDimension, resCols > &b)
Definition: armor.hh:149
const Mat< Type, nRows, nCols > & operator=(std::initializer_list< Type > list)
Definition: armor.hh:102
const Type * begin() const
Definition: armor.hh:46
Type & operator()(uint row, uint col)
Definition: armor.hh:73
arma::Mat< Type >::template fixed< nRows, nCols > operator/(const Mat< Type, nRows, nCols > &a, Type number)
Definition: armor.hh:164
bool operator==(const Mat< Type, nRows, nCols > &other)
Definition: armor.hh:120
arma::Mat< Type >::template fixed< nRows, nCols > operator-(const Mat< Type, nRows, nCols > &a, const Mat< Type, nRows, nCols > &b)
Definition: armor.hh:144
Mat(const Armor::Mat< Type, nRows, nCols > &other)
Definition: armor.hh:36
Type * end()
Definition: armor.hh:55
const Type & operator()(uint row, uint col) const
Definition: armor.hh:70
Type dot(const Mat< Type, nRows, nCols > &a, const Mat< Type, nRows, nCols > &b)
Definition: armor.hh:134
uint size() const
Definition: armor.hh:58
Mat(const ArmaType &other)
Definition: armor.hh:41
ArmaType arma() const
Definition: armor.hh:76
Mat(std::initializer_list< std::initializer_list< Type >> list)
Definition: armor.hh:20
bool operator==(const ArmaType &other)
Definition: armor.hh:109
Type * begin()
Definition: armor.hh:49
const Type & operator[](uint index) const
Definition: armor.hh:64