-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.hpp
213 lines (185 loc) · 5.8 KB
/
matrix.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#pragma once
namespace nall {
template<typename T, u32 Rows, u32 Cols>
struct Matrix {
static_assert(Rows > 0 && Cols > 0);
Matrix() = default;
Matrix(const Matrix&) = default;
Matrix(const initializer_list<T>& source) {
u32 index = 0;
for(auto& value : source) {
if(index >= Rows * Cols) break;
values[index / Cols][index % Cols] = value;
}
}
operator array_span<T>() { return {values, Rows * Cols}; }
operator array_view<T>() const { return {values, Rows * Cols}; }
//1D matrices (for polynomials, etc)
auto operator[](u32 row) -> T& { return values[row][0]; }
auto operator[](u32 row) const -> T { return values[row][0]; }
//2D matrices
auto operator()(u32 row, u32 col) -> T& { return values[row][col]; }
auto operator()(u32 row, u32 col) const -> T { return values[row][col]; }
//operators
auto operator+() const -> Matrix {
Matrix result;
for(u32 row : range(Rows)) {
for(u32 col : range(Cols)) {
result(row, col) = +target(row, col);
}
}
return result;
}
auto operator-() const -> Matrix {
Matrix result;
for(u32 row : range(Rows)) {
for(u32 col : range(Cols)) {
result(row, col) = -target(row, col);
}
}
return result;
}
auto operator+(const Matrix& source) const -> Matrix {
Matrix result;
for(u32 row : range(Rows)) {
for(u32 col : range(Cols)) {
result(row, col) = target(row, col) + source(row, col);
}
}
return result;
}
auto operator-(const Matrix& source) const -> Matrix {
Matrix result;
for(u32 row : range(Rows)) {
for(u32 col : range(Cols)) {
result(row, col) = target(row, col) - source(row, col);
}
}
return result;
}
auto operator*(T source) const -> Matrix {
Matrix result;
for(u32 row : range(Rows)) {
for(u32 col : range(Cols)) {
result(row, col) = target(row, col) * source;
}
}
return result;
}
auto operator/(T source) const -> Matrix {
Matrix result;
for(u32 row : range(Rows)) {
for(u32 col : range(Cols)) {
result(row, col) = target(row, col) / source;
}
}
return result;
}
//warning: matrix multiplication is not commutative!
template<u32 SourceRows, u32 SourceCols>
auto operator*(const Matrix<T, SourceRows, SourceCols>& source) const -> Matrix<T, Rows, SourceCols> {
static_assert(Cols == SourceRows);
Matrix<T, Rows, SourceCols> result;
for(u32 y : range(Rows)) {
for(u32 x : range(SourceCols)) {
T sum{};
for(u32 z : range(Cols)) {
sum += target(y, z) * source(z, x);
}
result(y, x) = sum;
}
}
return result;
}
template<u32 SourceRows, u32 SourceCols>
auto operator/(const Matrix<T, SourceRows, SourceCols>& source) const -> maybe<Matrix<T, Rows, SourceCols>> {
static_assert(Cols == SourceRows && SourceRows == SourceCols);
if(auto inverted = source.invert()) return operator*(inverted());
return {};
}
auto& operator+=(const Matrix& source) { return *this = operator+(source); }
auto& operator-=(const Matrix& source) { return *this = operator-(source); }
auto& operator*=(T source) { return *this = operator*(source); }
auto& operator/=(T source) { return *this = operator/(source); }
template<u32 SourceRows, u32 SourceCols>
auto& operator*=(const Matrix<T, SourceRows, SourceCols>& source) { return *this = operator*(source); }
//matrix division is not always possible (when matrix cannot be inverted), so operator/= is not provided
//algorithm: Gauss-Jordan
auto invert() const -> maybe<Matrix> {
static_assert(Rows == Cols);
Matrix source = *this;
Matrix result = identity();
const auto add = [&](u32 targetRow, u32 sourceRow, T factor = 1) {
for(u32 col : range(Cols)) {
result(targetRow, col) += result(sourceRow, col) * factor;
source(targetRow, col) += source(sourceRow, col) * factor;
}
};
const auto sub = [&](u32 targetRow, u32 sourceRow, T factor = 1) {
for(u32 col : range(Cols)) {
result(targetRow, col) -= result(sourceRow, col) * factor;
source(targetRow, col) -= source(sourceRow, col) * factor;
}
};
const auto mul = [&](u32 row, T factor) {
for(u32 col : range(Cols)) {
result(row, col) *= factor;
source(row, col) *= factor;
}
};
for(u32 i : range(Cols)) {
if(source(i, i) == 0) {
for(u32 row : range(Rows)) {
if(source(row, i) != 0) {
add(i, row);
break;
}
}
//matrix is not invertible:
if(source(i, i) == 0) return {};
}
mul(i, T{1} / source(i, i));
for(u32 row : range(Rows)) {
if(row == i) continue;
sub(row, i, source(row, i));
}
}
return result;
}
auto transpose() const -> Matrix<T, Cols, Rows> {
Matrix<T, Cols, Rows> result;
for(u32 row : range(Rows)) {
for(u32 col : range(Cols)) {
result(col, row) = target(row, col);
}
}
return result;
}
static auto identity() -> Matrix {
static_assert(Rows == Cols);
Matrix result;
for(u32 row : range(Rows)) {
for(u32 col : range(Cols)) {
result(row, col) = row == col;
}
}
return result;
}
//debugging function: do not use in production code
template<u32 Pad = 0>
auto _print() const -> void {
for(u32 row : range(Rows)) {
nall::print("[ ");
for(u32 col : range(Cols)) {
nall::print(pad(target(row, col), Pad, ' '), " ");
}
nall::print("]\n");
}
}
protected:
//same as operator(), but with easier to read syntax inside Matrix class
auto target(u32 row, u32 col) -> T& { return values[row][col]; }
auto target(u32 row, u32 col) const -> T { return values[row][col]; }
T values[Rows][Cols]{};
};
}