-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Matrix.hpp
280 lines (244 loc) · 7.11 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#ifndef MATRIX_HPP
#define MATRIX_HPP
#include "Vector.hpp"
#include <assert.h>
template< typename NUM, int ROWS, int COLS >
class Matrix {
public:
union {
NUM c[ROWS * COLS];
Vector< NUM, COLS > rows[ROWS];
};
NUM &operator()(unsigned int row, unsigned int col) {
return c[row * COLS + col];
}
NUM const &operator()(unsigned int row, unsigned int col) const {
return c[row * COLS + col];
}
};
template< typename NUM, int ROWS1, int COLS1, int COLS2 >
Matrix< NUM, ROWS1, COLS2 > operator*(Matrix< NUM, ROWS1, COLS1 > const &a, Matrix< NUM, COLS1, COLS2 > const &b) {
Matrix< NUM, ROWS1, COLS2 > ret;
for (unsigned int r = 0; r < ROWS1; ++r) {
for (unsigned int c = 0; c < COLS2; ++c) {
ret(r,c) = 0;
for (unsigned int i = 0; i < COLS1; ++i) {
ret(r,c) += a(r,i) * b(i, c);
}
}
}
return ret;
}
template< int SUBROWS, int SUBCOLS, typename NUM, int ROWS, int COLS >
Matrix< NUM, SUBROWS, SUBCOLS > submatrix(Matrix< NUM, ROWS, COLS > const &mat, unsigned int row, unsigned int col) {
assert(row + SUBROWS <= ROWS);
assert(col + SUBCOLS <= COLS);
Matrix< NUM, SUBROWS, SUBCOLS > ret;
for (unsigned int r = 0; r < SUBROWS; ++r) {
for (unsigned int c = 0; c < SUBCOLS; ++c) {
ret(r,c) = mat(row+r,col+c);
}
}
return ret;
}
template< typename NUM, int ROWS, int COLS >
Vector< NUM, ROWS > operator*(Matrix< NUM, ROWS, COLS > const &a, Vector< NUM, COLS > const &b) {
return make_vector(a * make_matrix(b));
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > operator+(Matrix< NUM, ROWS, COLS > const &a, Matrix< NUM, ROWS, COLS > const &b) {
Matrix< NUM, ROWS, COLS > ret;
for (unsigned int i = 0; i < ROWS * COLS; ++i) {
ret.c[i] = a.c[i] + b.c[i];
}
return ret;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > operator-(Matrix< NUM, ROWS, COLS > const &a, Matrix< NUM, ROWS, COLS > const &b) {
Matrix< NUM, ROWS, COLS > ret;
for (unsigned int i = 0; i < ROWS * COLS; ++i) {
ret.c[i] = a.c[i] - b.c[i];
}
return ret;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > operator*(NUM const &a, Matrix< NUM, ROWS, COLS > const &b) {
Matrix< NUM, ROWS, COLS > ret;
for (unsigned int i = 0; i < ROWS * COLS; ++i) {
ret.c[i] = a * b.c[i];
}
return ret;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > &operator*=(Matrix< NUM, ROWS, COLS > &b, NUM const &a) {
for (unsigned int i = 0; i < ROWS * COLS; ++i) {
b.c[i] *= a;
}
return b;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > operator*(Matrix< NUM, ROWS, COLS > const &a, NUM const &b) {
Matrix< NUM, ROWS, COLS > ret;
for (unsigned int i = 0; i < ROWS * COLS; ++i) {
ret.c[i] = a.c[i] * b;
}
return ret;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > &operator+=(Matrix< NUM, ROWS, COLS > &a, Matrix< NUM, ROWS, COLS > const &b) {
for (unsigned int i = 0; i < ROWS * COLS; ++i) {
a.c[i] += b.c[i];
}
return a;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > operator/(Matrix< NUM, ROWS, COLS > const &a, NUM const &b) {
Matrix< NUM, ROWS, COLS > ret;
NUM inv = 1 / b;
for (unsigned int i = 0; i < ROWS * COLS; ++i) {
ret.c[i] = a.c[i] * inv;
}
return ret;
}
template< typename NUM, int ROWS >
Matrix< NUM, ROWS, 1 > make_matrix(Vector< NUM, ROWS > const &vec) {
Matrix< NUM, ROWS, 1 > ret;
for (unsigned int i = 0; i < ROWS; ++i) {
ret.c[i] = vec.c[i];
}
return ret;
}
template< typename NUM, int ROWS >
Vector< NUM, ROWS > make_vector(Matrix< NUM, ROWS, 1 > const &mat) {
Vector< NUM, ROWS > ret;
for (unsigned int i = 0; i < ROWS; ++i) {
ret.c[i] = mat.c[i];
}
return ret;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, COLS, ROWS > transpose(Matrix< NUM, ROWS, COLS > const &mat) {
Matrix< NUM, COLS, ROWS > ret;
for (unsigned int r = 0; r < ROWS; ++r) {
for (unsigned int c = 0; c < COLS; ++c) {
ret(c,r) = mat(r,c);
}
}
return ret;
}
template< typename NUM, int ROWS1, int ROWS2, int COLS1, int COLS2 >
Matrix< NUM, ROWS1 + ROWS2, COLS1 + COLS2 > make_matrix(
Matrix< NUM, ROWS1, COLS1 > const &m00, Matrix< NUM, ROWS1, COLS2 > const &m01,
Matrix< NUM, ROWS2, COLS1 > const &m10, Matrix< NUM, ROWS2, COLS2 > const &m11) {
Matrix< NUM, ROWS1 + ROWS2, COLS1 + COLS2 > ret;
for (unsigned int r = 0; r < ROWS1; ++r) {
for (unsigned int c = 0; c < COLS1; ++c) {
ret(r,c) = m00(r,c);
}
}
for (unsigned int r = 0; r < ROWS1; ++r) {
for (unsigned int c = 0; c < COLS2; ++c) {
ret(r,c+COLS1) = m01(r,c);
}
}
for (unsigned int r = 0; r < ROWS2; ++r) {
for (unsigned int c = 0; c < COLS1; ++c) {
ret(r+ROWS1,c) = m10(r,c);
}
}
for (unsigned int r = 0; r < ROWS2; ++r) {
for (unsigned int c = 0; c < COLS2; ++c) {
ret(r+ROWS1,c+COLS1) = m11(r,c);
}
}
return ret;
}
template< typename NUM, int ROWS1, int ROWS2, int COLS >
Matrix< NUM, ROWS1 + ROWS2, COLS > make_matrix(
Matrix< NUM, ROWS1, COLS > const &m0,
Matrix< NUM, ROWS2, COLS > const &m1) {
Matrix< NUM, ROWS1 + ROWS2, COLS > ret;
for (unsigned int r = 0; r < ROWS1; ++r) {
for (unsigned int c = 0; c < COLS; ++c) {
ret(r,c) = m0(r,c);
}
}
for (unsigned int r = 0; r < ROWS2; ++r) {
for (unsigned int c = 0; c < COLS; ++c) {
ret(r+ROWS1,c) = m1(r,c);
}
}
return ret;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > make_matrix(NUM const *in) {
Matrix< NUM, ROWS, COLS > ret;
for (unsigned int i = 0; i < ROWS*COLS; ++i) {
ret.c[i] = in[i];
}
return ret;
}
template< typename NUM, int ROWS, int COLS >
Matrix< NUM, ROWS, COLS > make_matrix(NUM const &in = NUM(0)) {
Matrix< NUM, ROWS, COLS > ret;
for (unsigned int i = 0; i < ROWS*COLS; ++i) {
ret.c[i] = in;
}
return ret;
}
template< typename NUM, int RC >
Matrix< NUM, RC, RC > diag(Vector< NUM, RC > const &vec) {
Matrix< NUM, RC, RC > ret;
for (unsigned int i = 0; i < RC * RC; ++i) {
ret.c[i] = 0;
}
for (unsigned int i = 0; i < RC; ++i) {
ret(i,i) = vec.c[i];
}
return ret;
}
template< typename NUM, int RC >
Matrix< NUM, RC, RC > diag(NUM const &num) {
Matrix< NUM, RC, RC > ret;
for (unsigned int i = 0; i < RC * RC; ++i) {
ret.c[i] = 0;
}
for (unsigned int i = 0; i < RC; ++i) {
ret(i,i) = num;
}
return ret;
}
template< typename NUM >
Matrix< NUM, 3, 3 > cross_product_matrix(Vector< NUM, 3 > const &vec) {
Matrix< NUM, 3, 3 > ret;
ret(0,0) = 0; ret(0,1) = -vec.z; ret(0,2) = vec.y;
ret(1,0) = vec.z; ret(1,1) = 0; ret(1,2) = -vec.x;
ret(2,0) = -vec.y; ret(2,1) = vec.x; ret(2,2) = 0;
return ret;
}
template< typename NUM, unsigned int ROWS, unsigned int COLS >
Matrix< NUM, ROWS, COLS > identity_matrix() {
Matrix< NUM, ROWS, COLS > ret;
for (unsigned int i = 0; i < ROWS * COLS; ++i) {
ret.c[i] = 0;
}
for (unsigned int i = 0; i < ROWS && i < COLS; ++i) {
ret(i,i) = 1;
}
return ret;
}
#include <iostream>
template< typename NUM, int ROWS, int COLS >
std::ostream &operator<<(std::ostream &o, Matrix< NUM, ROWS, COLS > const &mat) {
o << '[';
for (unsigned int r = 0; r < ROWS; ++r) {
if (r) o << ";";
for (unsigned int c = 0; c < COLS; ++c) {
if (c) o << ", ";
o << mat(r,c);
}
}
o << ']';
return o;
}
#endif //MATRIX_HPP