GKD.RoboCtrl
载入中...
搜索中...
未找到
Matrix.hpp
1
11//TODO:优化矩阵乘法
12#pragma once
13
14#include <cmath>
15#include <cstdint>
16#include <array>
17#include <algorithm>
18
19namespace roboctrl::utils
20{
21template <int _rows, int _cols, typename T = float>
22 requires(std::is_arithmetic_v<T> && _rows > 0 && _cols > 0) //编译期检查是否为算数类型
23class Matrix
24{
25
26public:
31 constexpr Matrix() = default;
32
33 constexpr explicit Matrix(T val)
34 {
35 for (int i = 0; i < _rows; i++)
36 std::fill(data[i].begin(), data[i].end(), val);
37 }
38
43 constexpr explicit Matrix(const T *data) : Matrix()
44 {
45 for (int i = 0; i < _rows; i++)
46 for (int j = 0; j < _cols; j++)
47 this->data[i][j] = data[i * _cols + j];
48 }
49
54 constexpr Matrix(const Matrix &mat) : Matrix()
55 {
56 this->data = mat.data;
57 }
58
59 constexpr Matrix(Matrix &&mat) noexcept : Matrix()
60 {
61 this->data = std::move(mat.data);
62 }
63
64 template <typename Ty>
65 requires(std::is_arithmetic_v<Ty>)
66 constexpr Matrix(const Matrix<_rows, _cols, Ty> &mat) : Matrix()
67 {
68 for (int i = 0; i < _rows; i++)
69 for (int j = 0; j < _cols; j++)
70 data[i][j] = static_cast<T>(mat.data[i][j]);
71 }
72
76 ~Matrix() = default;
77
82 [[nodiscard]] constexpr uint32_t rows() const
83 {
84 return _rows;
85 }
86
91 [[nodiscard]] constexpr uint32_t cols() const
92 {
93 return _cols;
94 }
95
100 constexpr std::array<T, _cols> &operator[](int row)
101 {
102 return data[row];
103 }
104
105 constexpr const std::array<T, _cols> &operator[](int row) const
106 {
107 return data[row];
108 }
109
115 constexpr Matrix &operator=(const Matrix &mat)
116 {
117 data = mat.data;
118 return *this;
119 }
120
121 constexpr Matrix &operator=(Matrix &&mat) noexcept
122 {
123 data = std::move(mat.data);
124 return *this;
125 }
126
133 constexpr Matrix &operator+=(const Matrix &mat)
134 {
135 for (int i = 0; i < _rows; i++)
136 for (int j = 0; j < _cols; j++)
137 data[i][j] += mat.data[i][j];
138
139 return *this;
140 }
141
148 constexpr Matrix &operator-=(const Matrix &mat)
149 {
150 for (int i = 0; i < _rows; i++)
151 for (int j = 0; j < _cols; j++)
152 data[i][j] -= mat.data[i][j];
153
154 return *this;
155 }
156
163 template <typename Ty>
164 requires(std::is_arithmetic_v<Ty>)
165 constexpr Matrix &operator*=(Ty val)
166 {
167 for (int i = 0; i < _rows; i++)
168 for (int j = 0; j < _cols; j++)
169 data[i][j] *= val;
170
171 return *this;
172 }
173
181 template <typename Ty>
182 requires(std::is_arithmetic_v<Ty>)
183 constexpr Matrix &operator/=(Ty val)
184 {
185 for (int i = 0; i < _rows; i++)
186 for (int j = 0; j < _cols; j++)
187 data[i][j] /= val;
188
189 return *this;
190 }
191
198 constexpr Matrix operator+(const Matrix &mat) const
199 {
200 Matrix res;
201 for (int i = 0; i < _rows; i++)
202 for (int j = 0; j < _cols; j++)
203 res.data[i][j] = data[i][j] + mat.data[i][j];
204
205 return res;
206 }
207
214 constexpr Matrix operator-(const Matrix &mat) const
215 {
216 Matrix res;
217 for (int i = 0; i < _rows; i++)
218 for (int j = 0; j < _cols; j++)
219 res.data[i][j] = data[i][j] - mat.data[i][j];
220
221 return res;
222 }
223
230 template <typename Ty>
231 requires(std::is_arithmetic_v<Ty>)
232 constexpr Matrix operator*(Ty val) const
233 {
234 Matrix res;
235 for (int i = 0; i < _rows; i++)
236 for (int j = 0; j < _cols; j++)
237 res.data[i][j] = data[i][j] * val;
238
239 return res;
240 }
241
249 template <typename Ty>
250 requires(std::is_arithmetic_v<Ty>)
251 constexpr friend Matrix operator*(Ty val, const Matrix &mat)
252 {
253 return mat * val;
254 }
255
263 template <typename Ty>
264 requires(std::is_arithmetic_v<Ty>)
265 Matrix operator/(Ty val) const
266 {
267 Matrix res;
268 for (int i = 0; i < _rows; i++)
269 for (int j = 0; j < _cols; j++)
270 res.data[i][j] = data[i][j] / val;
271
272 return res;
273 }
274
281 template <int cols2>
284 {
286
287 for (int i = 0; i < _rows; i++)
288 for (int k = 0; k < _cols; k++)
289 for (int j = 0; j < cols2; j++)
290 res[i][j] += mat1[i][k] * mat2[k][j];
291
292 return res;
293 }
294
299 constexpr bool operator==(const Matrix &mat) const
300 {
301 return data == mat.data;
302 }
303
304 // Submatrix
305 template <int rows, int cols> constexpr Matrix<rows, cols, T> block(int start_row, int start_col) const
306 {
308 for (int i = 0; i < rows; i++)
309 std::copy(data[i + start_row].begin() + start_col, data[i + start_row].begin() + start_col + cols,
310 res.data[i].begin());
311 return res;
312 }
313
319 constexpr Matrix<1, _cols, T> row(int row) const
320 {
321 return block<1, _cols, T>(row, 0);
322 }
323
329 constexpr Matrix<_rows, 1, T> col(int col) const
330 {
331 return block<_rows, 1, T>(0, col);
332 }
333
339 constexpr Matrix<_cols, _rows> trans() const
340 {
342 for (int i = 0; i < _rows; i++)
343 for (int j = 0; j < _cols; j++)
344 res[j][i] = data[i][j];
345
346 return res;
347 }
348
353 constexpr Matrix clone() const
354 {
355 Matrix res;
356 res.data = data;
357 return res;
358 }
359
365 constexpr T trace() const
366 {
367 T res = 0;
368 for (int i = 0; i < fmin(_rows, _cols); i++)
369 res += data[i][i];
370 return res;
371 }
372
378 constexpr Matrix inv() const
379 {
381 static_assert(_cols == _rows, "Matrix must be square");
382
383 // 创建一个临时矩阵用于 LU 分解
384 Matrix temp = this->clone();
385
386 // 高斯-约当消元法
387 for (int k = 0; k < _cols; k++)
388 {
389 // 检查主元是否为 0
390 if (temp[k][k] == 0.0f)
391 {
392 return zeros();
393 }
394
395 // 归一化当前行
396 T pivot = temp[k][k];
397 for (int j = 0; j < _rows; j++)
398 {
399 temp[k][j] /= pivot;
400 res[k][j] /= pivot;
401 }
402
403 // 消去其他行的当前列
404 for (int i = 0; i < _rows; i++)
405 {
406 if (i != k)
407 {
408 T factor = temp[i][k];
409 for (int j = 0; j < _rows; j++)
410 {
411 temp[i][j] -= factor * temp[k][j];
412 res[i][j] -= factor * res[k][j];
413 }
414 }
415 }
416 }
417
418 return res;
419 }
420
421 /*==============================================================*/
422 // Static function
429 static constexpr Matrix zeros()
430 {
431 return Matrix(static_cast<T>(0));
432 }
433
440 static constexpr Matrix ones()
441 {
442 return Matrix(static_cast<T>(1));
443 }
444
451 static constexpr Matrix eye()
452 {
454
455 for (int i = 0; i < std::min(_rows, _cols); i++)
456 mat[i][i] = 1;
457
458 return mat;
459 }
460
468 static constexpr Matrix diag(Matrix<_rows, 1> vec)
469 {
471 for (int i = 0; i < std::min(_rows, _cols); i++)
472 {
473 res[i][i] = vec[i][0];
474 }
475 return res;
476 }
477
478private:
479 std::array<std::array<T, _cols>, _rows> data;
480
481};
482
483template <int r, int c> using Matrixf = Matrix<r, c, float>;
484} // namespace Power
constexpr Matrix(const T *data)
Constructor with input data
Definition Matrix.hpp:43
constexpr uint32_t cols() const
return the column size of the matrix
Definition Matrix.hpp:91
constexpr Matrix operator+(const Matrix &mat) const
Additonal operator
Definition Matrix.hpp:198
constexpr std::array< T, _cols > & operator[](int row)
Return the element of the matrix
Definition Matrix.hpp:100
constexpr uint32_t rows() const
returns the row size of the matrix
Definition Matrix.hpp:82
constexpr Matrix inv() const
Get the inverse of the matrix
Definition Matrix.hpp:378
static constexpr Matrix eye()
Returns a _rows * columns matrix
Definition Matrix.hpp:451
~Matrix()=default
Destructor
static constexpr Matrix zeros()
Returns a _rows x _cols zero matrix
Definition Matrix.hpp:429
constexpr Matrix< _rows, 1, T > col(int col) const
Return the specific row of the matrix
Definition Matrix.hpp:329
constexpr Matrix & operator=(const Matrix &mat)
Copy assignment of the matrix(row * size) instance
Definition Matrix.hpp:115
constexpr Matrix clone() const
Definition Matrix.hpp:353
constexpr Matrix(const Matrix &mat)
Copy Constructor
Definition Matrix.hpp:54
constexpr Matrix & operator-=(const Matrix &mat)
Substraction operator of two matrices(row * size)
Definition Matrix.hpp:148
constexpr Matrix< _cols, _rows > trans() const
Get the transpose of the matrix
Definition Matrix.hpp:339
constexpr T trace() const
Get the trace of the matrix
Definition Matrix.hpp:365
constexpr bool operator==(const Matrix &mat) const
Compare whether two matrices are identical
Definition Matrix.hpp:299
static constexpr Matrix ones()
Returns a _rows x _cols one matrix
Definition Matrix.hpp:440
constexpr Matrix< 1, _cols, T > row(int row) const
Return the specific row of the matrix
Definition Matrix.hpp:319
constexpr Matrix & operator+=(const Matrix &mat)
Additional operator of two matrices(row * size)
Definition Matrix.hpp:133
constexpr friend Matrix< _rows, cols2, T > operator*(const Matrix< _rows, _cols, T > &mat1, const Matrix< _cols, cols2, T > &mat2)
The matrix multiplication
Definition Matrix.hpp:282
constexpr Matrix()=default
Constructor without input data
constexpr Matrix operator-(const Matrix &mat) const
Substraction matrix
Definition Matrix.hpp:214
static constexpr Matrix diag(Matrix< _rows, 1 > vec)
Returns a _rows x _cols diagonal matrix
Definition Matrix.hpp:468
asio::awaitable< T > awaitable
协程任务类型。
Definition async.hpp:42
用于存放工具函数的命名空间。
Definition concepts.hpp:13