Haptic Controller
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1#pragma once
2#include <vector>
3#include "wiring.h"
4#include "stdexcept"
5
7class Matrix
8{
9public:
10 Matrix() = default;
11 ~Matrix() = default;
12
13 Matrix(size_t rows, size_t cols)
14 : mRows(rows),
15 mCols(cols),
16 mData(rows * cols)
17 {}
18
19 Matrix(size_t rows, size_t cols, std::vector<float> data)
20 : mRows(rows),
21 mCols(cols),
22 mData(data)
23 {}
24
25 float & operator()(size_t i, size_t j)
26 {
27 return mData.at(i * mCols + j);
28 }
29
30 float operator()(size_t i, size_t j) const
31 {
32 return mData.at(i * mCols + j);
33 }
34
37 {
38 auto M_T = Matrix{mCols, mRows};
39 for (size_t i = 0; i < mRows; ++i) {
40 for (size_t j = 0; j < mCols; ++j) {
41 M_T(j, i) = mData.at(i * mCols + j);
42 }
43 }
44 return M_T;
45 }
46
48 Matrix T() const
49 {
50 return transpose();
51 }
52
54 std::vector<float> operator*(const std::vector<float> & v) const
55 {
56 if (v.size() != mCols) {
57 Serial.println("Matrix multiplication error: vector size does not match matrix columns");
58 return {};
59 }
60 std::vector<float> v_(mRows);
61 for (size_t i = 0; i < mRows; ++i) {
62 auto sum = 0.f;
63 for (size_t j = 0; j < mCols; ++j) {
64 sum += v.at(j) * mData.at(i * mCols + j);
65 }
66 v_.at(i) = sum;
67 }
68 return v_;
69 }
70
72 std::vector<float> getData() const
73 {
74 return mData;
75 }
76
78 bool operator==(const Matrix & rhs) const
79 {
80 return mData == rhs.getData();
81 }
82
83private:
84
85 size_t mRows;
86 size_t mCols;
87 std::vector<float> mData;
88};
Class that treats a std::vector as a matrix and implements simple operations.
Definition matrix.hpp:8
Matrix()=default
Matrix T() const
Alias for transpose()
Definition matrix.hpp:48
float operator()(size_t i, size_t j) const
Definition matrix.hpp:30
bool operator==(const Matrix &rhs) const
Overloaded equality operator that returns true if matrices are equal value-wise.
Definition matrix.hpp:78
Matrix transpose() const
Gets the transpose of the matrix.
Definition matrix.hpp:36
float & operator()(size_t i, size_t j)
Definition matrix.hpp:25
std::vector< float > operator*(const std::vector< float > &v) const
Overloaded multiplication operator for matrix-matrix multiplication.
Definition matrix.hpp:54
Matrix(size_t rows, size_t cols, std::vector< float > data)
Definition matrix.hpp:19
Matrix(size_t rows, size_t cols)
Definition matrix.hpp:13
std::vector< float > getData() const
Gets the data vector of the matrix.
Definition matrix.hpp:72
~Matrix()=default