-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.hpp
399 lines (339 loc) · 10.4 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#pragma once
#include "common_header.hpp"
#include <vector>
#include <cmath>
#include <initializer_list>
#include <array>
template<int M, int N, typename ConstT>
requires ( std::same_as<ConstT, float> || std::same_as<ConstT, const float> )
class MatrixSlice {
public:
ConstT* data[M][N];
static constexpr bool NOT_CONST = std::same_as<ConstT, float>;
float& operator[](const std::pair<int, int>& index)
requires (NOT_CONST) {
return *data[index.first][index.second];
}
float operator[](const std::pair<int, int>& index) const {
return *data[index.first][index.second];
}
MatrixSlice& operator=(const MatrixSlice& slice) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
*data[i][j] = *slice.data[i][j];
}
}
}
// 语义上限制一下对const slice对象的内容的访问
// 怎么整?
// MatrixSlice& operator-=(const Matrix<M, N>& mat);
template <typename ConstT2>
MatrixSlice& operator-=(const MatrixSlice<M, N, ConstT2>& mat) {
for(int i = 0; i < M; i++)
for(int j = 0; j < N; j++)
*data[i][j] -= *mat.data[i][j];
return *this;
}
MatrixSlice& operator/=(float k) {
for(int i = 0; i < M; i++)
for(int j = 0; j < N; j++)
*data[i][j] /= k;
return *this;
}
MatrixSlice() = default;
MatrixSlice(const MatrixSlice&) = default;
MatrixSlice(MatrixSlice&&) = delete; // 如何直接 delete 一个外部的??
MatrixSlice& operator=(MatrixSlice&&) = delete;
};
namespace std {
template<int M, int N>
void swap(const MatrixSlice<M, N, float> slice1, const MatrixSlice<M, N, float>& slice2) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
swap(*slice1.data[i][j], *slice1.data[i][j]);
}
}
}
}
template<int M, int N>
class Matrix {
float data[M][N];
public:
static constexpr int ShapeM = M;
static constexpr int ShapeN = N;
Matrix(){
memset(data, 0, sizeof(data));
}
Matrix(const Matrix& other){
memcpy(data, other.data, sizeof(data));
}
// 破坏了静态检查。不过 API 会比较易用。
Matrix(const std::initializer_list<float>& init) requires (N == 1) {
if(init.size() != M) throw "unmatch size";
int i = 0;
for(auto val: init) {
data[i++][0] = val;
}
}
// 破坏了静态检查。不过 API 会比较易用。
Matrix(const std::initializer_list<std::initializer_list<float>>& init) {
if(init.size() != M) throw "unmatch size";
int i = 0, j = 0;
for(auto row: init) {
if(row.size() != N) throw "unmatch size";
for(auto val: row) {
data[i][j++] = val;
}
i++;
j = 0;
}
}
Matrix(const MatrixSlice<M, N, float>& slice) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
data[i][j] = slice[{i, j}];
}
}
}
template <typename ...Cols>
static Matrix hcat(const Cols&... cols) {
Matrix target;
Matrix::make_hcat<0, Cols...>(target, cols...);
return target;
}
template <int StartAt, typename FirstCol, typename ...Cols>
static void make_hcat(Matrix& target, const FirstCol& firstcol, const Cols&... cols)
requires (StartAt + FirstCol::ShapeN <= N && FirstCol::ShapeM == M) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < FirstCol::ShapeN; j++) {
target[{i, StartAt + j}] = firstcol[{i, j}];
}
}
make_hcat<StartAt + FirstCol::ShapeN, Cols...>(target, cols...);
}
template <int StartAt, typename FirstCol>
static void make_hcat(Matrix& target, const FirstCol& firstcol)
requires (StartAt + FirstCol::ShapeN == N && FirstCol::ShapeM == M) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < FirstCol::ShapeN; j++) {
target[{i, StartAt + j}] = firstcol[{i, j}];
}
}
}
float& operator[](const std::pair<int, int>& index) {
return data[index.first][index.second];
}
float operator[](const std::pair<int, int>& index) const {
return data[index.first][index.second];
}
//template<>
float& operator[](int index) requires (N == 1) {
return data[index][0];
}
// /template<>
float operator[](int index) const requires (N == 1) {
return data[index][0];
}
Matrix<N, M> transposed() {
Matrix<N, M> out;
for(int i = 0; i < M; i++)
for(int j = 0; j < N; j++)
out[{j, i}] = data[i][j];
return out;
}
template<int O>
Matrix<N, O> operator*(const Matrix<N, O>& rhs) const {
Matrix<M, O> mat;
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
for(int k = 0; k < O; k++) {
mat[{i, k}] += data[i][j] * rhs[{j,k}];
}
}
}
return mat;
}
Matrix& operator*=(float k) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
data[i][j] *= k;
}
}
return *this;
}
Matrix operator*(float k) const {
Matrix rst = *this;
rst *= k;
return rst;
}
Matrix& operator+=(const Matrix& rhs) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
data[i][j] += rhs.data[i][j];
}
}
return *this;
}
Matrix operator+(const Matrix& rhs) const {
auto lhs = *this;
return lhs += rhs;
}
Matrix operator-(const Matrix& rhs) const {
Matrix out = *this;
return out -= rhs;
}
Matrix& operator-=(const Matrix& rhs) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
data[i][j] -= rhs.data[i][j];
}
}
return *this;
}
template <typename ConstT>
Matrix& operator-=(const MatrixSlice<M, N, ConstT>& rhs) {
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
data[i][j] -= rhs[{i, j}];
}
}
return *this;
}
static Matrix Identity()
requires (M == N){
Matrix out;
for(int i = 0; i < M; i++) {
out[{i, i}] = 1;
}
return out;
};
float norm2_squared() const {
float sum = 0;
for(int i = 0; i < M; i++) {
for(int j = 0; j < N; j++) {
sum += data[i][j] * data[i][j];
}
}
return sum;
}
float norm2() const {
return sqrt(norm2_squared());
}
Matrix& operator/=(float x) {
return (*this) *= 1/x;
}
Matrix& normalize() requires ( N == 1 ) {
return (*this) /= norm2();
}
Matrix normalized() const requires ( N == 1 ) {
auto out = *this;
return out /= norm2();
}
Matrix inversed() const requires (M == N) {
Matrix L = *this;
Matrix R = Matrix::Identity();
for(int i = 0; i < N - 1; i++) {
// make sure R[i][i] != 0
int max_abs_index = i;
float max_abs_val = fabs(R[{i, i}]);
for(int j = i + 1; j < N; j++) {
// 只要别带来数值精度问题,都没啥关系。float:1e-38
if(max_abs_val > 1e-10) break;
double abs_val = fabs(R[{j, i}]);
if(abs_val > max_abs_val) {
max_abs_val = abs_val;
max_abs_index = j;
}
}
if(max_abs_index != i) {
std::swap(L.row(i), L.row(max_abs_index));
std::swap(R.row(i), R.row(max_abs_index));
}
float head = L[{i, i}];
for(int j = i + 1; j < N; j++) {
float div = L[{j, i}] / head;
// TODO
auto temp1 = (L.row(i) * div);
auto temp2 = (R.row(i) * div);
L.row(j) -= temp1.row(0);
R.row(j) -= temp2.row(0);
}
}
for(int i = N - 1; i > 0; i--) {
float head = L[{i, i}];
for(int j = i - 1; j >= 0; j--) {
float div = L[{j, i}] / head;
// auto temp1 = (L.row(i) * div);
auto temp2 = (R.row(i) * div);
// L.row(j) -= temp1.row(0);
R.row(j) -= temp2.row(0);
}
}
for(int i = 0; i < N; i++) {
float div = L[{i, i}];
// L.row(i) /= div;
R.row(i) /= div;
}
return R;
}
MatrixSlice<1, N, float> row(int i) {
MatrixSlice<1, N, float> out;
for(int j = 0; j < N; j++) {
out.data[0][j] = &data[i][j]; // TODO ????
}
return out;
}
MatrixSlice<M, 1, float> col(int j) {
MatrixSlice<M, 1, float> out;
for(int i = 0; i < M; i++) {
out[{i, 0}] = &(*this)[{i, j}];
}
return out;
}
};
template<int M, int N>
Matrix<M, N> operator*(const MatrixSlice<M, N, float>& slice, float k) {
return Matrix<M, N>(slice) * k;
}
// 如何只对某个成员函数进行特化?
using Vec2 = Matrix<2, 1>;
using Vec3 = Matrix<3, 1>;
using Vec4 = Matrix<4, 1>;
using Mat3 = Matrix<3, 3>;
using Mat4 = Matrix<4, 4>;
Vec3 cross_product(const Vec3& vec31, const Vec3& vec32) {
Vec3 mat;
mat[0] = vec31[1] * vec32[2] - vec32[1] * vec31[2];
mat[1] = vec31[2] * vec32[0] - vec32[2] * vec31[0];
mat[2] = vec31[0] * vec32[1] - vec32[0] * vec31[1];
return mat;
}
template <int dim>
float dot_product(const Matrix<dim, 1>& vec1, const Matrix<dim, 1>& vec2) {
float sum = 0;
for(int i = 0; i < dim; i++) {
sum += vec1[i] * vec2[i];
}
return sum;
};
Vec4 to_vec4_as_pos(const Vec3& vec) {
Vec4 mat;
for(int i = 0; i < 3; i++) mat[i] = vec[i];
mat[3] = 1;
return mat;
}
Vec4 to_vec4_as_dir(const Vec3& vec) {
Vec4 mat;
for(int i = 0; i < 3; i++) mat[i] = vec[i];
return mat;
}
Vec3 to_vec3_as_dir(const Vec4& vec) {
Vec3 mat;
for(int i = 0; i < 3; i++) mat[i] = vec[i];
return mat;
}
Vec3 to_vec3_as_pos(const Vec4& vec) {
Vec3 mat;
for(int i = 0; i < 3; i++) mat[i] = vec[i] / vec[3];
return mat;
}