38std::vector<double>
util::dot(
const std::vector<std::vector<double>> &m,
const
39std::vector<double> &v) {
42 size_t row_size = m.size();
43 size_t col_size = m[0].size();
45 assert((col_size == v.size()) &&
"Column size of matrix must match row of vector for dot product");
47 std::vector<double> r(row_size, 0.);
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];
75double util::det(
const std::vector<std::vector<double>> &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");
81 size_t row_size = m.size();
84 else if (row_size == 2)
85 return m[0][0] * m[1][1] - m[0][1] * m[1][0];
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]);
93util::inv(
const std::vector<std::vector<double>> &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");
99 size_t row_size = m.size();
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.);
106 n[0][0] = 1. / m[0][0];
109 }
else if (row_size == 2) {
111 auto det_inv = 1. /
det(m);
113 n[0][0] = det_inv * m[1][1];
114 n[1][1] = det_inv * m[0][0];
116 n[0][1] = -det_inv * m[0][1];
117 n[1][0] = -det_inv * m[1][0];
122 auto det_inv = 1. /
det(m);
125 (m[1][1] * m[2][2] - m[2][1] * m[1][2]);
127 (m[0][1] * m[2][2] - m[2][1] * m[0][2]);
129 (m[0][1] * m[1][2] - m[1][1] * m[0][2]);
132 (m[1][0] * m[2][2] - m[2][0] * m[1][2]);
134 (m[0][0] * m[2][2] - m[2][0] * m[0][2]);
136 (m[0][0] * m[1][2] - m[1][0] * m[0][2]);
139 (m[1][0] * m[2][1] - m[2][0] * m[1][1]);
141 (m[0][0] * m[2][1] - m[2][0] * m[0][1]);
143 (m[0][0] * m[1][1] - m[1][0] * m[0][1]);
std::vector< double > dot(const std::vector< std::vector< double > > &m, const std::vector< double > &v)
Computes the dot product between matrix and vector.