-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.hpp
More file actions
156 lines (126 loc) · 4.86 KB
/
Matrix.hpp
File metadata and controls
156 lines (126 loc) · 4.86 KB
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
#ifndef __MATRIX_H__
#define __MATRIX_H__
#include "Vector.hpp"
namespace tinyMath {
template<class T, int n> struct dt; // 计算行列式所需要的
template<class T, int nrows, int ncols> struct mat
{
vec<T, ncols> rows[nrows] = {{}};
mat() = default;
vec<T, ncols>& operator[](const int idx) {
assert(idx >= 0 && idx < nrows);
return rows[idx];
}
const vec<T, ncols>& operator[](const int idx) const {
assert(idx >= 0 && idx < nrows);
return rows[idx];
}
vec<T, nrows> col(const int idx) const {
assert(idx >= 0 && idx < ncols);
vec<T, nrows> ret;
for(int i = nrows; i--; ret[i] = rows[i][idx]);
return ret;
}
void set_col(const int idx, const vec<T, nrows>& v) {
assert(idx >= 0 && idx < ncols);
for(int i = nrows; i--; rows[i][idx]=v[i]);
}
static mat<T, nrows, ncols> identity() {
mat<T, nrows, ncols> ret;
for(int i=0; i < nrows; i++)
for(int j = 0; j < ncols; j++)
if(i == j) ret[i][j] = 1;
else ret[i][j] = 0;
return ret;
}
double det() const {
return dt<T, ncols>::det(*this);
}
mat<T, nrows-1,ncols-1> get_minor(const int row, const int col) const {
mat<T, nrows-1,ncols-1> ret;
for (int i=nrows-1; i--; )
for (int j=ncols-1;j--; ret[i][j]=rows[i<row?i:i+1][j<col?j:j+1]);
return ret;
}
double cofactor(const int row, const int col) const {
return get_minor(row,col).det()*((row+col)%2 ? -1 : 1);
}
mat<T, nrows,ncols> adjugate() const {
mat<T, nrows,ncols> ret;
for (int i=nrows; i--; )
for (int j=ncols; j--; ret[i][j]=cofactor(i,j));
return ret;
}
mat<T, nrows,ncols> invert_transpose() const {
mat<T, nrows,ncols> ret = adjugate();
return ret/(ret[0]*rows[0]);
}
mat<T, nrows,ncols> invert() const {
return invert_transpose().transpose();
}
mat<T, ncols,nrows> transpose() const {
mat<T, ncols,nrows> ret;
for (int i=ncols; i--; ret[i]=this->col(i));
return ret;
}
};
template<class T, int nrows,int ncols> vec<T, nrows> operator*(const mat<T, nrows,ncols>& lhs, const vec<T, ncols>& rhs) {
vec<T, nrows> ret;
for (int i=nrows; i--; ret[i]=lhs[i]*rhs);
return ret;
}
template<class T, int R1,int C1,int C2>mat<T, R1,C2> operator*(const mat<T, R1,C1>& lhs, const mat<T, C1,C2>& rhs) {
mat<T, R1,C2> result;
for (int i=R1; i--; )
for (int j=C2; j--; result[i][j]=lhs[i]*rhs.col(j));
return result;
}
template<class T, int nrows,int ncols>mat<T, nrows,ncols> operator*(const mat<T, nrows,ncols>& lhs, const double& val) {
mat<T, nrows,ncols> result;
for (int i=nrows; i--; result[i] = lhs[i]*val);
return result;
}
template<class T, int nrows,int ncols>mat<T, nrows,ncols> operator/(const mat<T, nrows,ncols>& lhs, const double& val) {
mat<T, nrows,ncols> result;
for (int i=nrows; i--; result[i] = lhs[i]/val);
return result;
}
template<class T, int nrows,int ncols>mat<T, nrows, ncols> operator+(const mat<T, nrows,ncols>& lhs, const mat<T, nrows,ncols>& rhs) {
mat<T, nrows,ncols> result;
for (int i=nrows; i--; )
for (int j=ncols; j--; result[i][j]=lhs[i][j]+rhs[i][j]);
return result;
}
template<class T, int nrows,int ncols>mat<T, nrows,ncols> operator-(const mat<T, nrows,ncols>& lhs, const mat<T, nrows,ncols>& rhs) {
mat<T, nrows,ncols> result;
for (int i=nrows; i--; )
for (int j=ncols; j--; result[i][j]=lhs[i][j]-rhs[i][j]);
return result;
}
template<class T, int nrows,int ncols> std::ostream& operator<<(std::ostream& out, const mat<T, nrows,ncols>& m) {
for (int i=0; i<nrows; i++) out << m[i] << std::endl;
return out;
}
template<class T, int n> struct dt {
static double det(const mat<T, n, n>& src) {
double ret = 0;
for (int i=n; i--; ret += src[0][i]*src.cofactor(0,i));
return ret;
}
};
template<class T> struct dt<T, 1> {
static double det(const mat<T, 1,1>& src) {
return src[0][0];
}
};
typedef mat<double, 4, 4> mat4d;
typedef mat<float, 4, 4> mat4f;
typedef mat<int, 4, 4> mat4i;
typedef mat<double, 3, 3> mat3d;
typedef mat<float, 3, 3> mat3f;
typedef mat<int, 3, 3> mat3i;
typedef mat<double, 2, 2> mat2d;
typedef mat<float, 2, 2> mat2f;
typedef mat<int, 2, 2> mat2i;
}
#endif