Skip to content

Commit 50f53c0

Browse files
authored
feat: matrix was updated
1 parent cbc502a commit 50f53c0

File tree

2 files changed

+114
-81
lines changed

2 files changed

+114
-81
lines changed

math/math_matrix.cpp

Lines changed: 33 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,33 @@
1-
const int MOD = 1e9+7;
2-
template<typename T>
3-
class Matrix {
4-
public:
5-
vector<vector<T>> M;
6-
int row, col;
7-
8-
Matrix(const vector<vector<T>> &m) : M(m) {
9-
int n = (int) m.size();
10-
row = n;
11-
if(n == 0)
12-
col = 0;
13-
else
14-
col = (int) m[0].size();
15-
}
16-
17-
Matrix(int r, int c, bool iden = false) : row(r), col(c) {
18-
assert(0 <= row && 0 <= col);
19-
M.resize(row, vector<T>(col, T(0)));
20-
if(iden)
21-
for(int i = 0; i < r; i++)
22-
M[i][i] = T(1);
23-
}
24-
25-
typename vector<vector<T>>::iterator begin() { return M.begin(); }
26-
typename vector<vector<T>>::iterator end() { return M.end(); }
27-
int size() { return (int) M.size(); }
28-
vector<T>& operator [] (int i) { return M[i]; }
29-
30-
Matrix<T> operator * (Matrix<T> &other) const {
31-
assert(col==other.row);
32-
Matrix<T> product(row, other.col);
33-
for(int i = 0; i < row; i++) {
34-
for (int j = 0; j < other.col; j++) {
35-
T &ref = product[i][j];
36-
for (int k = 0; k < col; k++) {
37-
// ref = (ref + M[i][k] * other[k][j]) % MOD;
38-
ref += (M[i][k] * other[k][j]);
39-
}
40-
}
41-
}
42-
return product;
43-
}
44-
45-
Matrix<T> operator + (Matrix<T> &other) const {
46-
assert(row==other.row && col==other.col);
47-
Matrix<T> ans(row, col);
48-
for(int i = 0; i < row; ++i) {
49-
for(int j = 0; j < col; ++j) {
50-
// ans[i][j] = (M[i][j] + other[i][j]) % MOD;
51-
ans[i][j] = M[i][j] + other[i][j];
52-
}
53-
}
54-
return ans;
55-
}
56-
57-
Matrix<T> operator - (Matrix<T> &other) const {
58-
assert(row==other.row && col==other.col);
59-
Matrix<T> ans(row, col);
60-
for(int i = 0; i < row; ++i) {
61-
for(int j = 0; j < col; ++j) {
62-
// ans[i][j] = (M[i][j] - other[i][j] + MOD) % MOD;
63-
ans[i][j] = (M[i][j] - other[i][j]);
64-
}
65-
}
66-
return ans;
67-
}
68-
69-
};
70-
71-
template<typename T>
72-
string to_string(const Matrix<T> &mat) {
73-
return to_string(mat.M);
74-
}
75-
76-
template<typename T>
77-
using matrix = Matrix<T>;
78-
// Usage:
79-
// matrix<int> A(10, 20); or
80-
// vector<vector<int>> vector1 {{1, 2}, {2, 3}};
81-
// matrix A(vector1);
1+
// using int64 = long long;
2+
3+
const int mod = 1e9+7;
4+
5+
struct matrix {
6+
vector<vector<int>> v;
7+
int n, m;
8+
9+
matrix(int n, int m, bool o = false) : n(n), m(m), v(n, vector<int>(m)) {
10+
if (o) while (n--) v[n][n] = 1;
11+
}
12+
13+
matrix operator * (const matrix &o) {
14+
matrix ans(n, o.m);
15+
for (int i = 0; i < n; i++)
16+
for (int k = 0; k < m; k++) if (v[i][k])
17+
for (int j = 0; j < o.m; j++)
18+
ans[i][j] = (1LL*v[i][k]*o.v[k][j] + ans[i][j]) % mod;
19+
return ans;
20+
}
21+
22+
vector<int>& operator[] (int i) { return v[i]; }
23+
};
24+
25+
matrix fastpow(matrix b, int64 e) {
26+
matrix ans(b.n, b.m, true);
27+
while(e > 0) {
28+
if(e & 1) ans = ans * b;
29+
b = b * b;
30+
e /= 2;
31+
}
32+
return ans;
33+
}

math/math_matrix_full.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const int MOD = 1e9+7;
2+
template<typename T>
3+
class Matrix {
4+
public:
5+
vector<vector<T>> M;
6+
int row, col;
7+
8+
Matrix(const vector<vector<T>> &m) : M(m) {
9+
int n = (int) m.size();
10+
row = n;
11+
if(n == 0)
12+
col = 0;
13+
else
14+
col = (int) m[0].size();
15+
}
16+
17+
Matrix(int r, int c, bool iden = false) : row(r), col(c) {
18+
assert(0 <= row && 0 <= col);
19+
M.resize(row, vector<T>(col, T(0)));
20+
if(iden)
21+
for(int i = 0; i < r; i++)
22+
M[i][i] = T(1);
23+
}
24+
25+
typename vector<vector<T>>::iterator begin() { return M.begin(); }
26+
typename vector<vector<T>>::iterator end() { return M.end(); }
27+
int size() { return (int) M.size(); }
28+
vector<T>& operator [] (int i) { return M[i]; }
29+
30+
Matrix<T> operator * (Matrix<T> &other) const {
31+
assert(col==other.row);
32+
Matrix<T> product(row, other.col);
33+
for(int i = 0; i < row; i++) {
34+
for (int j = 0; j < other.col; j++) {
35+
T &ref = product[i][j];
36+
for (int k = 0; k < col; k++) {
37+
// ref = (ref + M[i][k] * other[k][j]) % MOD;
38+
ref += (M[i][k] * other[k][j]);
39+
}
40+
}
41+
}
42+
return product;
43+
}
44+
45+
Matrix<T> operator + (Matrix<T> &other) const {
46+
assert(row==other.row && col==other.col);
47+
Matrix<T> ans(row, col);
48+
for(int i = 0; i < row; ++i) {
49+
for(int j = 0; j < col; ++j) {
50+
// ans[i][j] = (M[i][j] + other[i][j]) % MOD;
51+
ans[i][j] = M[i][j] + other[i][j];
52+
}
53+
}
54+
return ans;
55+
}
56+
57+
Matrix<T> operator - (Matrix<T> &other) const {
58+
assert(row==other.row && col==other.col);
59+
Matrix<T> ans(row, col);
60+
for(int i = 0; i < row; ++i) {
61+
for(int j = 0; j < col; ++j) {
62+
// ans[i][j] = (M[i][j] - other[i][j] + MOD) % MOD;
63+
ans[i][j] = (M[i][j] - other[i][j]);
64+
}
65+
}
66+
return ans;
67+
}
68+
69+
};
70+
71+
template<typename T>
72+
string to_string(const Matrix<T> &mat) {
73+
return to_string(mat.M);
74+
}
75+
76+
template<typename T>
77+
using matrix = Matrix<T>;
78+
// Usage:
79+
// matrix<int> A(10, 20); or
80+
// vector<vector<int>> vector1 {{1, 2}, {2, 3}};
81+
// matrix A(vector1);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy