NLMech  0.1.0
matrix.h
1 // Copyright (c) 2019 Prashant K. Jha
3 // Copyright (c) 2019 Patrick Diehl
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 
9 #ifndef UTIL_MATRIX_H
10 #define UTIL_MATRIX_H
11 
12 #include "point.h"
13 
14 namespace util {
15 
17 struct Matrix3 {
18 
25  float d_data[3][3]{};
26 
32  Matrix3() {
33 
34  d_data[0][0] = 0.;
35  d_data[0][1] = 0.;
36  d_data[0][2] = 0.;
37 
38  d_data[1][0] = 0.;
39  d_data[1][1] = 0.;
40  d_data[1][2] = 0.;
41 
42  d_data[2][0] = 0.;
43  d_data[2][1] = 0.;
44  d_data[2][2] = 0.;
45  };
46 
52  Matrix3(const util::Point3& diagonal) {
53 
54  d_data[0][0] = diagonal.d_x;
55  d_data[0][1] = 0.;
56  d_data[0][2] = 0.;
57 
58  d_data[1][0] = 0.;
59  d_data[1][1] = diagonal.d_y;
60  d_data[1][2] = 0.;
61 
62  d_data[2][0] = 0.;
63  d_data[2][1] = 0.;
64  d_data[2][2] = diagonal.d_z;
65  };
66 
74  Matrix3(const util::Point3& a1, const util::Point3& a2, const util::Point3& a3) {
75 
76  d_data[0][0] = a1.d_x;
77  d_data[0][1] = a1.d_y;
78  d_data[0][2] = a1.d_z,
79  d_data[1][0] = a2.d_x;
80  d_data[1][1] = a2.d_y;
81  d_data[1][2] = a2.d_z;
82  d_data[2][0] = a3.d_x;
83  d_data[2][1] = a3.d_y;
84  d_data[2][2] = a3.d_z;
85  };
86 
92  Matrix3(const std::vector<std::vector<double>> &m) {
93  for (size_t i=0; i<3; i++)
94  for (size_t j=0; j<3; j++)
95  d_data[i][j] = m[i][j];
96  }
97 
103  Matrix3(const Matrix3 &m) {
104  for (size_t i=0; i<3; i++)
105  for (size_t j=0; j<3; j++)
106  d_data[i][j] = m(i,j);
107  }
108 
116  std::string printStr(int nt = 0, int lvl = 0) const {
117 
118  std::string tabS = "";
119  for (int i = 0; i < nt; i++)
120  tabS += "\t";
121 
122  std::ostringstream oss;
123  for (size_t i=0; i<3; i++)
124  oss << tabS << "[" << (*this)(i, 0) << ", " << (*this)(i, 1) << ", "
125  << (*this)(i, 2) << "]" << std::endl;
126  oss << std::endl;
127 
128  return oss.str();
129  }
130 
137  void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
138 
144  Point3 operator()(size_t i) {
145  return Point3(d_data[i]);
146  }
147 
153  Point3 operator()(size_t i) const {
154  return Point3(d_data[i]);
155  }
156 
163  float &operator()(size_t i, size_t j) { return d_data[i][j]; }
164 
171  const float &operator()(size_t i, size_t j) const { return d_data[i][j]; }
172 
178  std::vector<double> dot(const std::vector<double> &v) const {
179 
180  auto r = std::vector<double>(3,0.);
181  for (size_t i=0; i<3; i++)
182  for (size_t j=0; j<3; j++)
183  r[i] += (*this)(i,j) * v[j];
184 
185  return r;
186  }
187 
195 
196  return {(*this)(0) * v, (*this)(1) * v, (*this)(2) * v};
197  }
198 
203  Matrix3 transpose() const {
204 
205  Matrix3 m = Matrix3(*this);
206 
207  m(0,1) = (*this)(1,0);
208  m(0,2) = (*this)(2,0);
209 
210  m(1,0) = (*this)(0,1);
211  m(1,2) = (*this)(2,1);
212 
213  m(2,0) = (*this)(0,2);
214  m(2,1) = (*this)(1,2);
215 
216  return m;
217  }
218 
224  double det() const {
225  return (*this)(0,0) * ((*this)(1,1) * (*this)(2,2) - (*this)(2,1) * (*this)(1,2)) -
226  (*this)(0,1) * ((*this)(1,0) * (*this)(2,2) - (*this)(2,0) * (*this)(1,2)) +
227  (*this)(0,2) * ((*this)(1,0) * (*this)(2,1) - (*this)(2,0) * (*this)(1,1));
228  }
229 
235  Matrix3 inv() const {
236 
237  Matrix3 m = Matrix3();
238 
239  auto det_inv = 1. / this->det();
240 
241  m(0,0) = det_inv *
242  ((*this)(1,1) * (*this)(2,2) - (*this)(2,1) * (*this)(1,2));
243  m(0,1) = -det_inv *
244  ((*this)(0,1) * (*this)(2,2) - (*this)(2,1) * (*this)(0,2));
245  m(0,2) = det_inv *
246  ((*this)(0,1) * (*this)(1,2) - (*this)(1,1) * (*this)(0,2));
247 
248  m(1,0) = -det_inv *
249  ((*this)(1,0) * (*this)(2,2) - (*this)(2,0) * (*this)(1,2));
250  m(1,1) = det_inv *
251  ((*this)(0,0) * (*this)(2,2) - (*this)(2,0) * (*this)(0,2));
252  m(1,2) = -det_inv *
253  ((*this)(0,0) * (*this)(1,2) - (*this)(1,0) * (*this)(0,2));
254 
255  m(2,0) = det_inv *
256  ((*this)(1,0) * (*this)(2,1) - (*this)(2,0) * (*this)(1,1));
257  m(2,1) = -det_inv *
258  ((*this)(0,0) * (*this)(2,1) - (*this)(2,0) * (*this)(0,1));
259  m(2,2) = det_inv *
260  ((*this)(0,0) * (*this)(1,1) - (*this)(1,0) * (*this)(0,1));
261 
262  return m;
263  }
264 };
265 
267 struct SymMatrix3 {
268 
282  float d_data[6]{};
283 
290 
291  d_data[0] = 0.;
292  d_data[1] = 0.;
293  d_data[2] = 0.;
294  d_data[3] = 0.;
295  d_data[4] = 0.;
296  d_data[5] = 0.;
297  };
298 
304  SymMatrix3(const util::Point3& diagonal) {
305 
306  d_data[0] = diagonal.d_x;
307  d_data[1] = diagonal.d_y;
308  d_data[2] = diagonal.d_z;
309 
310  d_data[3] = 0.;
311  d_data[4] = 0.;
312  d_data[5] = 0.;
313  };
314 
320  SymMatrix3(const std::vector<std::vector<double>> &m) {
321 
322  d_data[0] = m[0][0];
323  d_data[1] = m[1][1];
324  d_data[2] = m[2][2];
325  d_data[3] = 0.5 * (m[1][2] + m[2][1]);
326  d_data[4] = 0.5 * (m[0][2] + m[2][0]);
327  d_data[5] = 0.5 * (m[0][1] + m[1][0]);
328  }
329 
335  SymMatrix3(const std::vector<double> &m) {
336 
337  d_data[0] = m[0];
338  d_data[1] = m[1];
339  d_data[2] = m[2];
340  d_data[3] = m[3];
341  d_data[4] = m[4];
342  d_data[5] = m[5];
343  }
344 
350  SymMatrix3(const Matrix3 &m) {
351 
352  d_data[0] = m(0,0);
353  d_data[1] = m(1,1);
354  d_data[2] = m(2,2);
355  d_data[3] = 0.5 * (m(1,2) + m(2,1));
356  d_data[4] = 0.5 * (m(0,2) + m(2,0));
357  d_data[5] = 0.5 * (m(0,1) + m(1,0));
358  }
359 
365  SymMatrix3(const SymMatrix3 &m) {
366 
367  for (size_t i=0; i<6; i++)
368  d_data[i] = m.d_data[i];
369  }
370 
378  std::string printStr(int nt = 0, int lvl = 0) const {
379 
380  std::string tabS = "";
381  for (int i = 0; i < nt; i++)
382  tabS += "\t";
383 
384  std::ostringstream oss;
385  for (size_t i=0; i<3; i++)
386  oss << tabS << "[" << (*this)(i, 0) << ", " << (*this)(i, 1) << ", "
387  << (*this)(i, 2) << "]" << std::endl;
388  oss << std::endl;
389 
390  return oss.str();
391  }
392 
393 
400  void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
401 
402 
408  Point3 operator()(size_t i) {
409  return {(*this)(i, 0), (*this)(i, 1), (*this)(i, 2)};
410  }
411 
417  Point3 operator()(size_t i) const {
418  return {(*this)(i, 0), (*this)(i, 1), (*this)(i, 2)};
419  }
420 
427  float &operator()(size_t i, size_t j) {
428  return d_data[i == j ? i : 6 - i - j];
429  }
430 
431 
438  const float &operator()(size_t i, size_t j) const {
439  return d_data[i == j ? i : 6 - i - j];
440  }
441 
446  const float &get(size_t i) const {
447  return d_data[i];
448  }
449 
454  float &get(size_t i) {
455  return d_data[i];
456  }
457 
462  void copy(double m[6]) const {
463 
464  for (size_t i=0; i<6; i++)
465  m[i] = d_data[i];
466  }
467 
473  std::vector<double> dot(const std::vector<double> &v) const {
474 
475  auto r = std::vector<double>(3,0.);
476  for (size_t i=0; i<3; i++)
477  for (size_t j=0; j<3; j++)
478  r[i] += (*this)(i,j) * v[j];
479 
480  return r;
481  }
482 
489 
490  return {(*this)(0) * v, (*this)(1) * v, (*this)(2) * v};
491  }
492 
498 
499  return (*this);
500  }
501 
507  double det() const {
508  return (*this)(0,0) * ((*this)(1,1) * (*this)(2,2) - (*this)(2,1) * (*this)(1,2)) -
509  (*this)(0,1) * ((*this)(1,0) * (*this)(2,2) - (*this)(2,0) * (*this)(1,2)) +
510  (*this)(0,2) * ((*this)(1,0) * (*this)(2,1) - (*this)(2,0) * (*this)(1,1));
511  }
512 
518  SymMatrix3 inv() const {
519 
520  SymMatrix3 m = SymMatrix3();
521 
522  auto det_inv = 1. / this->det();
523 
524  m(0,0) = det_inv *
525  ((*this)(1,1) * (*this)(2,2) - (*this)(2,1) * (*this)(1,2));
526  m(0,1) = -det_inv *
527  ((*this)(0,1) * (*this)(2,2) - (*this)(2,1) * (*this)(0,2));
528  m(0,2) = det_inv *
529  ((*this)(0,1) * (*this)(1,2) - (*this)(1,1) * (*this)(0,2));
530 
531  m(1,0) = -det_inv *
532  ((*this)(1,0) * (*this)(2,2) - (*this)(2,0) * (*this)(1,2));
533  m(1,1) = det_inv *
534  ((*this)(0,0) * (*this)(2,2) - (*this)(2,0) * (*this)(0,2));
535  m(1,2) = -det_inv *
536  ((*this)(0,0) * (*this)(1,2) - (*this)(1,0) * (*this)(0,2));
537 
538  m(2,0) = det_inv *
539  ((*this)(1,0) * (*this)(2,1) - (*this)(2,0) * (*this)(1,1));
540  m(2,1) = -det_inv *
541  ((*this)(0,0) * (*this)(2,1) - (*this)(2,0) * (*this)(0,1));
542  m(2,2) = det_inv *
543  ((*this)(0,0) * (*this)(1,1) - (*this)(1,0) * (*this)(0,1));
544 
545  return m;
546  }
547 };
548 
555 bool checkMatrix(const std::vector<std::vector<double>> &m);
556 
564 std::vector<double> dot(const std::vector<std::vector<double>> &m, const
565 std::vector<double> &v);
566 
573 std::vector<std::vector<double>> transpose(const
574  std::vector<std::vector<double>> &m);
575 
582 double det(const std::vector<std::vector<double>> &m);
583 
590 std::vector<std::vector<double>>
591 inv(const std::vector<std::vector<double>> &m);
592 
593 } // namespace util
594 
595 #endif // UTIL_MATRIX_H
Collection of methods useful in simulation.
Definition: DataManager.h:50
std::vector< std::vector< double > > transpose(const std::vector< std::vector< double >> &m)
Computes the transpose of matrix.
Definition: matrix.cpp:42
double det(const std::vector< std::vector< double >> &m)
Computes the determinant of matrix.
Definition: matrix.cpp:69
std::vector< std::vector< double > > inv(const std::vector< std::vector< double >> &m)
Computes the determinant of matrix.
Definition: matrix.cpp:85
bool checkMatrix(const std::vector< std::vector< double >> &m)
Checks matrix.
Definition: matrix.cpp:12
std::vector< double > dot(const std::vector< std::vector< double >> &m, const std::vector< double > &v)
Computes the dot product between matrix and vector.
Definition: matrix.cpp:30
A structure to represent 3d matrices.
Definition: matrix.h:17
Matrix3 transpose() const
Computes the transpose of matrix.
Definition: matrix.h:203
Matrix3()
Constructor.
Definition: matrix.h:32
Matrix3(const Matrix3 &m)
Constructor.
Definition: matrix.h:103
Matrix3(const util::Point3 &a1, const util::Point3 &a2, const util::Point3 &a3)
Constructor.
Definition: matrix.h:74
const float & operator()(size_t i, size_t j) const
Access operator of the Matrix element M(i,j)
Definition: matrix.h:171
Point3 operator()(size_t i)
Access operator of the i-th element.
Definition: matrix.h:144
Point3 operator()(size_t i) const
Access operator of the i-th element.
Definition: matrix.h:153
void print(int nt=0, int lvl=0) const
Prints the information.
Definition: matrix.h:137
float d_data[3][3]
data
Definition: matrix.h:25
util::Point3 dot(const util::Point3 &v)
Computes the dot product of the matrix and the vector v.
Definition: matrix.h:194
Matrix3(const util::Point3 &diagonal)
Constructor.
Definition: matrix.h:52
std::string printStr(int nt=0, int lvl=0) const
Prints the information.
Definition: matrix.h:116
Matrix3(const std::vector< std::vector< double >> &m)
Constructor.
Definition: matrix.h:92
float & operator()(size_t i, size_t j)
Access operator of the Matrix element M(i,j)
Definition: matrix.h:163
double det() const
Computes the determinant of matrix.
Definition: matrix.h:224
Matrix3 inv() const
Computes the determinant of matrix.
Definition: matrix.h:235
std::vector< double > dot(const std::vector< double > &v) const
Computes the dot product between matrix and vector.
Definition: matrix.h:178
A structure to represent 3d vectors.
Definition: point.h:29
double d_x
the x coordinate
Definition: point.h:32
double d_y
the y coordinate
Definition: point.h:35
double d_z
the z coordinate
Definition: point.h:38
A structure to represent 3d matrices.
Definition: matrix.h:267
float d_data[6]
data
Definition: matrix.h:282
const float & get(size_t i) const
Return the i-th element.
Definition: matrix.h:446
void print(int nt=0, int lvl=0) const
Prints the information.
Definition: matrix.h:400
const float & operator()(size_t i, size_t j) const
Access operator of the Matrix element M(i,j)
Definition: matrix.h:438
std::vector< double > dot(const std::vector< double > &v) const
Computes the dot product of this matrix with another vector.
Definition: matrix.h:473
SymMatrix3()
Constructor.
Definition: matrix.h:289
Point3 operator()(size_t i) const
Access operator of the i-th element.
Definition: matrix.h:417
float & operator()(size_t i, size_t j)
Access operator of the Matrix element M(i,j)
Definition: matrix.h:427
Point3 operator()(size_t i)
Access operator of the i-th element.
Definition: matrix.h:408
std::string printStr(int nt=0, int lvl=0) const
Prints the information.
Definition: matrix.h:378
SymMatrix3(const std::vector< double > &m)
Constructor.
Definition: matrix.h:335
double det() const
Computes the determinant of matrix.
Definition: matrix.h:507
void copy(double m[6]) const
Copy.
Definition: matrix.h:462
SymMatrix3(const SymMatrix3 &m)
Constructor.
Definition: matrix.h:365
float & get(size_t i)
Return the i-th element.
Definition: matrix.h:454
util::Point3 dot(const util::Point3 &v)
Computes the dot product of this matrix with another vector.
Definition: matrix.h:488
SymMatrix3(const Matrix3 &m)
Constructor.
Definition: matrix.h:350
SymMatrix3 transpose() const
Computes the transpose of matrix.
Definition: matrix.h:497
SymMatrix3(const util::Point3 &diagonal)
Constructor.
Definition: matrix.h:304
SymMatrix3 inv() const
Computes the determinant of matrix.
Definition: matrix.h:518
SymMatrix3(const std::vector< std::vector< double >> &m)
Constructor.
Definition: matrix.h:320