c_physics_engine
A custom physics engine written in C/C++
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1#ifndef matrix_hpp
2#define matrix_hpp
3
4#include <functional>
5#include <sstream>
6
7namespace cpp_physics {
8
9const std::out_of_range OOB = std::out_of_range("Row or column index out of bounds.");
10const std::out_of_range SUBMAT_OOB = std::out_of_range("Submatrix exceeds bounds of original matrix.");
11const std::invalid_argument SHAPE_MISMATCH = std::invalid_argument("Matrix-shapes don't match.");
12const std::invalid_argument INNERDIM_MISMATCH = std::invalid_argument("Inner matrix-dimensions don't align.");
13const std::invalid_argument DIM_MISMATCH = std::invalid_argument("Matrix-columns or -rows don't match.");
14const std::invalid_argument TOO_FEW = std::invalid_argument("Too few matrices, minimum 1.");
15const std::invalid_argument DIV_ZERO = std::invalid_argument("Division by 0.");
16
17class Matrix {
18private:
19 std::vector<float> data;
20 std::size_t rows;
21 std::size_t cols;
22
23public:
29 Matrix(std::size_t rows, std::size_t cols = 1);
30
36 static Matrix Identity(std::size_t size);
37
42 std::pair<std::size_t, std::size_t> Shape() const;
43
50 float Get(std::size_t row, std::size_t col) const;
51
56 void applyFunction(std::function<float(float)> func);
57
66 Matrix Submatrix(std::size_t rows, std::size_t cols, std::size_t row_start, std::size_t col_start) const;
67
74 static Matrix Combine(std::vector<Matrix> matrices, bool vertical = false);
75
80 float Sum() const;
81
88 static Matrix memwise_Mul(const Matrix& lhs, const Matrix& rhs);
89
94 Matrix memwise_iMul(const Matrix& other);
95
102 static Matrix memwise_Div(const Matrix& lhs, const Matrix& rhs);
103
108 Matrix memwise_iDiv(const Matrix& other);
109
114 Matrix operator+=(const Matrix& other);
120 Matrix operator+(const Matrix& other) const;
125 Matrix operator-=(const Matrix& other);
131 Matrix operator-(const Matrix& other) const;
136 Matrix operator*=(const Matrix& other);
142 Matrix operator*(const Matrix& other) const;
147 Matrix operator*=(float scalar);
153 Matrix operator*(float scalar) const;
158 Matrix operator/=(float scalar);
164 Matrix operator/(float scalar) const;
165
171 Matrix operator=(const Matrix& other);
172
177 Matrix operator-() const;
182 Matrix operator!() const;
183
190 float& operator[](std::size_t row, std::size_t col);
191
197 bool operator==(const Matrix& other);
203 bool operator!=(const Matrix& other);
204};
205
212Matrix operator*(float scalar, const Matrix& mat);
213
220std::ostream& operator<<(std::ostream& flux, const Matrix& mat);
221
222} // namespace cpp_physics
223
224#endif
Definition matrix.hpp:17
Matrix operator/(float scalar) const
Divide a matrix by a scalar.
Definition matrix.cpp:192
void applyFunction(std::function< float(float)> func)
Apply a function to all elements of the matrix.
Definition matrix.cpp:45
Matrix operator+(const Matrix &other) const
Add two matrices.
Definition matrix.cpp:136
Matrix memwise_iDiv(const Matrix &other)
Memberwise-divide this matrix by anoter one.
Definition matrix.cpp:124
Matrix operator*(const Matrix &other) const
Multiply two matrices.
Definition matrix.cpp:160
Matrix operator=(const Matrix &other)
Copy this matrix.
Definition matrix.cpp:199
static Matrix memwise_Mul(const Matrix &lhs, const Matrix &rhs)
Memberwise-multiply two matrices.
Definition matrix.cpp:103
Matrix operator/=(float scalar)
Divide this matrix by a scalar.
Definition matrix.cpp:186
float & operator[](std::size_t row, std::size_t col)
Access values inside the matrix.
Definition matrix.cpp:222
static Matrix Identity(std::size_t size)
Creates a new identity-matrix.
Definition matrix.cpp:7
Matrix operator-=(const Matrix &other)
Subtract another matrix from this one.
Definition matrix.cpp:143
Matrix(std::size_t rows, std::size_t cols=1)
Constructs a Matrix with the specified number of rows and columns.
Definition matrix.cpp:5
Matrix memwise_iMul(const Matrix &other)
Memberwise-multiply a matrix to this one.
Definition matrix.cpp:110
static Matrix memwise_Div(const Matrix &lhs, const Matrix &rhs)
Memberwise-divide two matrices.
Definition matrix.cpp:116
bool operator==(const Matrix &other)
Check if this matrices is equal to another matrix.
Definition matrix.cpp:227
bool operator!=(const Matrix &other)
Check if this matrices is different from another matrix.
Definition matrix.cpp:232
float Sum() const
Sum all elements in the matrix.
Definition matrix.cpp:97
Matrix operator!() const
Transpose this matrix.
Definition matrix.cpp:212
Matrix operator+=(const Matrix &other)
Add another matrix to this one.
Definition matrix.cpp:131
Matrix operator*=(const Matrix &other)
Multiply another matrix to this one.
Definition matrix.cpp:155
std::pair< std::size_t, std::size_t > Shape() const
return the shape of the matrix as a tuple of (rows, cols).
Definition matrix.cpp:38
Matrix Submatrix(std::size_t rows, std::size_t cols, std::size_t row_start, std::size_t col_start) const
Extract a submatrix from the matrix.
Definition matrix.cpp:49
float Get(std::size_t row, std::size_t col) const
Retrieves a value from the matrix at the specified row and column.
Definition matrix.cpp:40
static Matrix Combine(std::vector< Matrix > matrices, bool vertical=false)
Combine multiple matrices into a new matrix.
Definition matrix.cpp:60
Matrix operator-() const
Negate this matrix.
Definition matrix.cpp:206
Definition cpp-physics.hpp:6
const std::out_of_range OOB
Definition matrix.hpp:9
const std::invalid_argument DIM_MISMATCH
Definition matrix.hpp:13
std::ostream & operator<<(std::ostream &flux, const Matrix &mat)
Print a matrix.
Definition matrix.cpp:13
const std::invalid_argument TOO_FEW
Definition matrix.hpp:14
const std::invalid_argument SHAPE_MISMATCH
Definition matrix.hpp:11
const std::invalid_argument INNERDIM_MISMATCH
Definition matrix.hpp:12
Matrix operator*(float scalar, const Matrix &mat)
Scale a matrix.
Definition matrix.cpp:237
const std::out_of_range SUBMAT_OOB
Definition matrix.hpp:10
const std::invalid_argument DIV_ZERO
Definition matrix.hpp:15