PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
matrix.h
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#ifndef UTIL_MATRIX_H
12#define UTIL_MATRIX_H
13
14#include "point.h"
15
16namespace util {
17
19struct Matrix3 {
20
27 float d_data[3][3]{};
28
35
36 d_data[0][0] = 0.;
37 d_data[0][1] = 0.;
38 d_data[0][2] = 0.;
39
40 d_data[1][0] = 0.;
41 d_data[1][1] = 0.;
42 d_data[1][2] = 0.;
43
44 d_data[2][0] = 0.;
45 d_data[2][1] = 0.;
46 d_data[2][2] = 0.;
47 };
48
54 Matrix3(const util::Point & diagonal) {
55
56 d_data[0][0] = diagonal.d_x;
57 d_data[0][1] = 0.;
58 d_data[0][2] = 0.;
59
60 d_data[1][0] = 0.;
61 d_data[1][1] = diagonal.d_y;
62 d_data[1][2] = 0.;
63
64 d_data[2][0] = 0.;
65 d_data[2][1] = 0.;
66 d_data[2][2] = diagonal.d_z;
67 };
68
76 Matrix3(const util::Point & a1, const util::Point & a2, const util::Point & a3) {
77
78 d_data[0][0] = a1.d_x;
79 d_data[0][1] = a1.d_y;
80 d_data[0][2] = a1.d_z,
81 d_data[1][0] = a2.d_x;
82 d_data[1][1] = a2.d_y;
83 d_data[1][2] = a2.d_z;
84 d_data[2][0] = a3.d_x;
85 d_data[2][1] = a3.d_y;
86 d_data[2][2] = a3.d_z;
87 };
88
94 Matrix3(const std::vector<std::vector<double>> &m) {
95 for (size_t i=0; i<3; i++)
96 for (size_t j=0; j<3; j++)
97 d_data[i][j] = m[i][j];
98 }
99
105 Matrix3(const Matrix3 &m) {
106 for (size_t i=0; i<3; i++)
107 for (size_t j=0; j<3; j++)
108 d_data[i][j] = m(i,j);
109 }
110
118 std::string printStr(int nt = 0, int lvl = 0) const {
119
120 std::string tabS = "";
121 for (int i = 0; i < nt; i++)
122 tabS += "\t";
123
124 std::ostringstream oss;
125 for (size_t i=0; i<3; i++)
126 oss << tabS << "[" << (*this)(i, 0) << ", " << (*this)(i, 1) << ", "
127 << (*this)(i, 2) << "]" << std::endl;
128 oss << std::endl;
129
130 return oss.str();
131 }
132
139 void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
140
147 Point operator()(size_t i) {
148 return Point(d_data[i]);
149 }
150
152 Point operator()(size_t i) const {
153 return Point(d_data[i]);
154 }
155
163 float &operator()(size_t i, size_t j) { return d_data[i][j]; }
164
166 const float &operator()(size_t i, size_t j) const { return d_data[i][j]; }
167
175
176 return {(*this)(0) * v, (*this)(1) * v, (*this)(2) * v};
177 }
178
180 std::vector<double> dot(const std::vector<double> &v) const {
181
182 auto r = std::vector<double>(3,0.);
183 for (size_t i=0; i<3; i++)
184 for (size_t j=0; j<3; j++)
185 r[i] += (*this)(i,j) * v[j];
186
187 return r;
188 }
189
195
196 Matrix3 m = Matrix3(*this);
197
198 m(0,1) = (*this)(1,0);
199 m(0,2) = (*this)(2,0);
200
201 m(1,0) = (*this)(0,1);
202 m(1,2) = (*this)(2,1);
203
204 m(2,0) = (*this)(0,2);
205 m(2,1) = (*this)(1,2);
206
207 return m;
208 }
209
215 double det() const {
216 return (*this)(0,0) * ((*this)(1,1) * (*this)(2,2) - (*this)(2,1) * (*this)(1,2)) -
217 (*this)(0,1) * ((*this)(1,0) * (*this)(2,2) - (*this)(2,0) * (*this)(1,2)) +
218 (*this)(0,2) * ((*this)(1,0) * (*this)(2,1) - (*this)(2,0) * (*this)(1,1));
219 }
220
226 Matrix3 inv() const {
227
228 Matrix3 m = Matrix3();
229
230 auto det_inv = 1. / this->det();
231
232 m(0,0) = det_inv *
233 ((*this)(1,1) * (*this)(2,2) - (*this)(2,1) * (*this)(1,2));
234 m(0,1) = -det_inv *
235 ((*this)(0,1) * (*this)(2,2) - (*this)(2,1) * (*this)(0,2));
236 m(0,2) = det_inv *
237 ((*this)(0,1) * (*this)(1,2) - (*this)(1,1) * (*this)(0,2));
238
239 m(1,0) = -det_inv *
240 ((*this)(1,0) * (*this)(2,2) - (*this)(2,0) * (*this)(1,2));
241 m(1,1) = det_inv *
242 ((*this)(0,0) * (*this)(2,2) - (*this)(2,0) * (*this)(0,2));
243 m(1,2) = -det_inv *
244 ((*this)(0,0) * (*this)(1,2) - (*this)(1,0) * (*this)(0,2));
245
246 m(2,0) = det_inv *
247 ((*this)(1,0) * (*this)(2,1) - (*this)(2,0) * (*this)(1,1));
248 m(2,1) = -det_inv *
249 ((*this)(0,0) * (*this)(2,1) - (*this)(2,0) * (*this)(0,1));
250 m(2,2) = det_inv *
251 ((*this)(0,0) * (*this)(1,1) - (*this)(1,0) * (*this)(0,1));
252
253 return m;
254 }
255};
256
259
273 float d_data[6]{};
274
281
282 d_data[0] = 0.;
283 d_data[1] = 0.;
284 d_data[2] = 0.;
285 d_data[3] = 0.;
286 d_data[4] = 0.;
287 d_data[5] = 0.;
288 };
289
295 SymMatrix3(const util::Point & diagonal) {
296
297 d_data[0] = diagonal.d_x;
298 d_data[1] = diagonal.d_y;
299 d_data[2] = diagonal.d_z;
300
301 d_data[3] = 0.;
302 d_data[4] = 0.;
303 d_data[5] = 0.;
304 };
305
311 SymMatrix3(const std::vector<double> &m) {
312
313 d_data[0] = m[0];
314 d_data[1] = m[1];
315 d_data[2] = m[2];
316 d_data[3] = m[3];
317 d_data[4] = m[4];
318 d_data[5] = m[5];
319 }
320
326 SymMatrix3(const std::vector<std::vector<double>> &m) {
327
328 d_data[0] = m[0][0];
329 d_data[1] = m[1][1];
330 d_data[2] = m[2][2];
331 d_data[3] = 0.5 * (m[1][2] + m[2][1]);
332 d_data[4] = 0.5 * (m[0][2] + m[2][0]);
333 d_data[5] = 0.5 * (m[0][1] + m[1][0]);
334 }
335
341 SymMatrix3(const Matrix3 &m) {
342
343 d_data[0] = m(0,0);
344 d_data[1] = m(1,1);
345 d_data[2] = m(2,2);
346 d_data[3] = 0.5 * (m(1,2) + m(2,1));
347 d_data[4] = 0.5 * (m(0,2) + m(2,0));
348 d_data[5] = 0.5 * (m(0,1) + m(1,0));
349 }
350
357
358 for (size_t i=0; i<6; i++)
359 d_data[i] = m.d_data[i];
360 }
361
369 std::string printStr(int nt = 0, int lvl = 0) const {
370
371 std::string tabS = "";
372 for (int i = 0; i < nt; i++)
373 tabS += "\t";
374
375 std::ostringstream oss;
376 for (size_t i=0; i<3; i++)
377 oss << tabS << "[" << (*this)(i, 0) << ", " << (*this)(i, 1) << ", "
378 << (*this)(i, 2) << "]" << std::endl;
379 oss << std::endl;
380
381 return oss.str();
382 }
383
390 void print(int nt = 0, int lvl = 0) const { std::cout << printStr(nt, lvl); }
391
398 Point operator()(size_t i) {
399 return {(*this)(i, 0), (*this)(i, 1), (*this)(i, 2)};
400 }
401
403 Point operator()(size_t i) const {
404 return {(*this)(i, 0), (*this)(i, 1), (*this)(i, 2)};
405 }
406
414 float &operator()(size_t i, size_t j) {
415 return d_data[i == j ? i : 6 - i - j];
416 }
417
419 const float &operator()(size_t i, size_t j) const {
420 return d_data[i == j ? i : 6 - i - j];
421 }
422
429 float &get(size_t i) {
430 return d_data[i];
431 }
432
434 const float &get(size_t i) const {
435 return d_data[i];
436 }
437
443 void copy(double m[6]) const {
444
445 for (size_t i=0; i<6; i++)
446 m[i] = d_data[i];
447 }
448
455
456 return {(*this)(0) * v, (*this)(1) * v, (*this)(2) * v};
457 }
458
460 std::vector<double> dot(const std::vector<double> &v) const {
461
462 auto r = std::vector<double>(3,0.);
463 for (size_t i=0; i<3; i++)
464 for (size_t j=0; j<3; j++)
465 r[i] += (*this)(i,j) * v[j];
466
467 return r;
468 }
469
475
476 return (*this);
477 }
478
484 double det() const {
485 return (*this)(0,0) * ((*this)(1,1) * (*this)(2,2) - (*this)(2,1) * (*this)(1,2)) -
486 (*this)(0,1) * ((*this)(1,0) * (*this)(2,2) - (*this)(2,0) * (*this)(1,2)) +
487 (*this)(0,2) * ((*this)(1,0) * (*this)(2,1) - (*this)(2,0) * (*this)(1,1));
488 }
489
495 SymMatrix3 inv() const {
496
498
499 auto det_inv = 1. / this->det();
500
501 m(0,0) = det_inv *
502 ((*this)(1,1) * (*this)(2,2) - (*this)(2,1) * (*this)(1,2));
503 m(0,1) = -det_inv *
504 ((*this)(0,1) * (*this)(2,2) - (*this)(2,1) * (*this)(0,2));
505 m(0,2) = det_inv *
506 ((*this)(0,1) * (*this)(1,2) - (*this)(1,1) * (*this)(0,2));
507
508 m(1,0) = -det_inv *
509 ((*this)(1,0) * (*this)(2,2) - (*this)(2,0) * (*this)(1,2));
510 m(1,1) = det_inv *
511 ((*this)(0,0) * (*this)(2,2) - (*this)(2,0) * (*this)(0,2));
512 m(1,2) = -det_inv *
513 ((*this)(0,0) * (*this)(1,2) - (*this)(1,0) * (*this)(0,2));
514
515 m(2,0) = det_inv *
516 ((*this)(1,0) * (*this)(2,1) - (*this)(2,0) * (*this)(1,1));
517 m(2,1) = -det_inv *
518 ((*this)(0,0) * (*this)(2,1) - (*this)(2,0) * (*this)(0,1));
519 m(2,2) = det_inv *
520 ((*this)(0,0) * (*this)(1,1) - (*this)(1,0) * (*this)(0,1));
521
522 return m;
523 }
524};
525
532bool checkMatrix(const std::vector<std::vector<double>> &m);
533
541std::vector<double> dot(const std::vector<std::vector<double>> &m, const
542std::vector<double> &v);
543
550std::vector<std::vector<double>> transpose(const
551std::vector<std::vector<double>> &m);
552
559double det(const std::vector<std::vector<double>> &m);
560
567std::vector<std::vector<double>>
568inv(const std::vector<std::vector<double>> &m);
569
570} // namespace util
571
572#endif // UTIL_MATRIX_H
Collection of methods useful in simulation.
double det(const std::vector< std::vector< double > > &m)
Computes the determinant of matrix.
Definition matrix.cpp:75
bool checkMatrix(const std::vector< std::vector< double > > &m)
Checks matrix.
Definition matrix.cpp:15
std::vector< std::vector< double > > inv(const std::vector< std::vector< double > > &m)
Computes the determinant of matrix.
Definition matrix.cpp:93
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:38
std::vector< std::vector< double > > transpose(const std::vector< std::vector< double > > &m)
Computes the tranpose of matrix.
Definition matrix.cpp:56
A structure to represent 3d matrices.
Definition matrix.h:19
Matrix3 transpose() const
Computes the tranpose of matrix.
Definition matrix.h:194
Matrix3()
Constructor.
Definition matrix.h:34
Matrix3(const Matrix3 &m)
Constructor.
Definition matrix.h:105
float & operator()(size_t i, size_t j)
Returns element of matrix.
Definition matrix.h:163
util::Point dot(const util::Point &v)
Computes the dot product between matrix and vector.
Definition matrix.h:174
Point operator()(size_t i) const
Returns row of matrix.
Definition matrix.h:152
const float & operator()(size_t i, size_t j) const
Returns element of matrix.
Definition matrix.h:166
void print(int nt=0, int lvl=0) const
Prints the information.
Definition matrix.h:139
Point operator()(size_t i)
Returns row of matrix.
Definition matrix.h:147
float d_data[3][3]
data
Definition matrix.h:27
Matrix3(const std::vector< std::vector< double > > &m)
Constructor.
Definition matrix.h:94
std::string printStr(int nt=0, int lvl=0) const
Prints the information.
Definition matrix.h:118
std::vector< double > dot(const std::vector< double > &v) const
Computes the dot product between matrix and vector.
Definition matrix.h:180
double det() const
Computes the determinant of matrix.
Definition matrix.h:215
Matrix3 inv() const
Computes the determinant of matrix.
Definition matrix.h:226
Matrix3(const util::Point &diagonal)
Constructor.
Definition matrix.h:54
Matrix3(const util::Point &a1, const util::Point &a2, const util::Point &a3)
Constructor.
Definition matrix.h:76
A structure to represent 3d vectors.
Definition point.h:30
double d_y
the y coordinate
Definition point.h:36
double d_z
the z coordinate
Definition point.h:39
double d_x
the x coordinate
Definition point.h:33
A structure to represent 3d matrices.
Definition matrix.h:258
float d_data[6]
data
Definition matrix.h:273
const float & operator()(size_t i, size_t j) const
Returns element of matrix.
Definition matrix.h:419
util::Point dot(const util::Point &v)
Computes the dot product of this matrix with another vector.
Definition matrix.h:454
SymMatrix3(const std::vector< std::vector< double > > &m)
Constructor.
Definition matrix.h:326
float & get(size_t i)
Returns row of matrix.
Definition matrix.h:429
void print(int nt=0, int lvl=0) const
Prints the information about the object.
Definition matrix.h:390
SymMatrix3(const util::Point &diagonal)
Constructor.
Definition matrix.h:295
SymMatrix3()
Constructor.
Definition matrix.h:280
Point operator()(size_t i) const
Returns row of matrix.
Definition matrix.h:403
const float & get(size_t i) const
Returns row of matrix.
Definition matrix.h:434
std::string printStr(int nt=0, int lvl=0) const
Returns the string containing printable information about the object.
Definition matrix.h:369
SymMatrix3(const std::vector< double > &m)
Constructor.
Definition matrix.h:311
double det() const
Computes the determinant of matrix.
Definition matrix.h:484
void copy(double m[6]) const
Copy.
Definition matrix.h:443
SymMatrix3(const SymMatrix3 &m)
Constructor.
Definition matrix.h:356
std::vector< double > dot(const std::vector< double > &v) const
Computes the dot product of this matrix with another vector.
Definition matrix.h:460
float & operator()(size_t i, size_t j)
Returns element of matrix.
Definition matrix.h:414
SymMatrix3(const Matrix3 &m)
Constructor.
Definition matrix.h:341
SymMatrix3 transpose() const
Computes the tranpose of matrix.
Definition matrix.h:474
SymMatrix3 inv() const
Computes the determinant of matrix.
Definition matrix.h:495
Point operator()(size_t i)
Returns row of matrix.
Definition matrix.h:398