PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
matrix.cpp
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#include "matrix.h"
12#include "io.h"
13#include <cassert>
14
15bool util::checkMatrix(const std::vector<std::vector<double>> &m) {
16
17 size_t row_size = m.size();
18 // if (row_size > 3) {
19 // std::cerr << "Error: Determinant of matrix above size 3 is not "
20 // "implemented\n";
21 // exit(1);
22 // }
23
24 if (m.size() != m[0].size()) {
25 std::ostringstream oss;
26 oss << "Error in matrix = [";
27 for (auto a : m)
28 oss << util::io::printStr(a) << "\n";
29 oss << "].\n";
30 std::cout << oss.str();
31 // exit(1);
32 return false;
33 }
34
35 return true;
36}
37
38std::vector<double> util::dot(const std::vector<std::vector<double>> &m, const
39std::vector<double> &v) {
40
41 //checkMatrix(m);
42 size_t row_size = m.size();
43 size_t col_size = m[0].size();
44
45 assert((col_size == v.size()) && "Column size of matrix must match row of vector for dot product");
46
47 std::vector<double> r(row_size, 0.);
48
49 for (size_t i=0; i<row_size; i++)
50 for (size_t j = 0; j < col_size; j++)
51 r[i] += m[i][j] * v[j];
52
53 return r;
54}
55
56std::vector<std::vector<double>> util::transpose(const
57std::vector<std::vector<double>> &m) {
58
59 //checkMatrix(m);
60
61 size_t row_size = m.size();
62 size_t col_size = m[0].size();
63
64 std::vector<std::vector<double>> n(col_size);
65
66 for (size_t i=0; i<row_size; i++) {
67 n[i].resize(row_size);
68 for (size_t j=0; j<=col_size; j++)
69 n[j][i] = m[i][j];
70 }
71
72 return n;
73}
74
75double util::det(const std::vector<std::vector<double>> &m) {
76
77 //checkMatrix(m);
78 assert((m.size() == m[0].size()) && "Matrix must be a square matrix");
79 assert((m.size() <= 3) && "Square of matrix of size 3 or below");
80
81 size_t row_size = m.size();
82 if (row_size == 1)
83 return m[0][0];
84 else if (row_size == 2)
85 return m[0][0] * m[1][1] - m[0][1] * m[1][0];
86 else
87 return m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) -
88 m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]) +
89 m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
90}
91
92std::vector<std::vector<double>>
93util::inv(const std::vector<std::vector<double>> &m) {
94
95 //checkMatrix(m);
96 assert((m.size() == m[0].size()) && "Matrix must be a square matrix");
97 assert((m.size() <= 3) && "Square of matrix of size 3 or below");
98
99 size_t row_size = m.size();
100
101 std::vector<std::vector<double>> n(row_size);
102 for (size_t i =0; i<row_size; i++)
103 n[i] = std::vector<double>(row_size, 0.);
104
105 if (row_size == 1) {
106 n[0][0] = 1. / m[0][0];
107
108 return n;
109 } else if (row_size == 2) {
110
111 auto det_inv = 1. / det(m);
112
113 n[0][0] = det_inv * m[1][1];
114 n[1][1] = det_inv * m[0][0];
115
116 n[0][1] = -det_inv * m[0][1];
117 n[1][0] = -det_inv * m[1][0];
118
119 return n;
120 } else {
121
122 auto det_inv = 1. / det(m);
123
124 n[0][0] = det_inv *
125 (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
126 n[0][1] = -det_inv *
127 (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
128 n[0][2] = det_inv *
129 (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
130
131 n[1][0] = -det_inv *
132 (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
133 n[1][1] = det_inv *
134 (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
135 n[1][2] = -det_inv *
136 (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
137
138 n[2][0] = det_inv *
139 (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
140 n[2][1] = -det_inv *
141 (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
142 n[2][2] = det_inv *
143 (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
144
145 return n;
146 }
147}
std::string printStr(const T &msg, int nt=print_default_tab)
Returns formatted string for output.
Definition io.h:54
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