-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.hpp
167 lines (156 loc) · 4.94 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
template <typename T>
struct Matrix : public vector<vector<T>> {
using Base = vector<vector<T>>;
using Base::front;
using Base::size;
using Base::vector;
Matrix(int h, int w) : Matrix(h, vector<T>(w)) {}
int sizeH() const { return size(); }
int sizeW() const { return front().size(); }
Matrix operator+() const { return *this; }
Matrix operator-() const {
Matrix res(*this);
for (auto &e : res) e = -e;
return res;
}
Matrix operator*(T k) const { return Mat(*this) *= k; }
Matrix operator/(T k) const { return Mat(*this) /= k; }
Matrix operator+(const Matrix &mat) const { return Mat(*this) += mat; }
Matrix operator-(const Matrix &mat) const { return Mat(*this) -= mat; }
Matrix operator*(const Matrix &mat) const {
int n = sizeH(), m = sizeW(), l = mat.sizeW();
Matrix res(n, l);
for (int i = 0; i < n; ++i) {
for (int k = 0; k < m; ++k) {
for (int j = 0; j < l; ++j) {
res[i][j] += (*this)[i][k] * mat[k][j];
}
}
}
return res;
}
Matrix operator*=(T k) {
for (auto &e : *this) e *= k;
return *this;
}
Matrix operator/=(T k) {
assert(k != 0);
*this *= 1 / k;
return *this;
}
Matrix &operator+=(const Matrix &mat) {
int h = sizeH(), w = sizeW();
for (int i = 0; i < h; ++i) {
for (int j = 0; j < w; ++j) {
(*this)[i][j] += mat[i][j];
}
}
return *this;
}
Matrix &operator-=(const Matrix &mat) { return *this += -mat; }
Matrix &operator*=(const Matrix &mat) { return *this = (*this) * mat; }
static Matrix identity(int n) {
Matrix res(n, n);
for (int i = 0; i < n; ++i) {
res[i][i] = 1;
}
return res;
}
Matrix pow(long long k) const {
Matrix res = identity(sizeH()), tmp = *this;
while (k > 0) {
if (k & 1) {
res *= tmp;
}
tmp *= tmp;
k >>= 1;
}
return res;
}
T det() const {
Matrix mat(*this);
int n = mat.sizeH();
T prod = 1;
for (int j0 = 0; j0 < n; ++j0) {
bool found = false;
for (int i0 = j0; i0 < n; ++i0) {
if (mat[i0][j0] != 0) {
if (i0 != j0) {
swap(mat[i0], mat[j0]);
prod *= -1;
}
prod *= mat[j0][j0];
T inv = 1 / mat[j0][j0];
for (auto &e : mat[j0]) e *= inv;
for (int i1 = 0; i1 < n; ++i1) {
if (j0 != i1) {
T mul = mat[i1][j0];
for (int j1 = j0; j1 < n; ++j1) {
mat[i1][j1] -= mul * mat[j0][j1];
}
}
}
found = true;
break;
}
}
if (!found) {
return 0;
}
}
return prod;
}
optional<Matrix> inv() const {
int n = (*this).sizeH();
Matrix mat(n, 2 * n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
mat[i][j] = (*this)[i][j];
}
mat[i][i + n] = 1;
}
for (int j0 = 0; j0 < n; ++j0) {
bool found = false;
for (int i0 = j0; i0 < n; ++i0) {
if (mat[i0][j0] != 0) {
if (i0 != j0) {
swap(mat[i0], mat[j0]);
}
T inv = 1 / mat[j0][j0];
for (auto &e : mat[j0]) e *= inv;
for (int i1 = 0; i1 < n; ++i1) {
if (j0 != i1) {
T mul = mat[i1][j0];
for (int j1 = j0; j1 < 2 * n; ++j1) {
mat[i1][j1] -= mul * mat[j0][j1];
}
}
}
found = true;
break;
}
}
if (!found) {
return nullopt;
}
}
Matrix res(n, n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
res[i][j] = mat[i][j + n];
}
}
return res;
}
friend ostream &operator<<(ostream &os, const Matrix &mat) {
for (auto itr1 = mat.begin(), end_itr1 = mat.end(); itr1 != end_itr1;) {
const auto &v = *itr1;
for (auto itr2 = v.begin(), end_itr2 = v.end(); itr2 != end_itr2;) {
os << *itr2;
if (++itr2 != end_itr2) os << " ";
}
if (++itr1 != end_itr1) os << "\n";
}
return os;
}
};