-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
181 lines (171 loc) · 5.63 KB
/
Matrix.cpp
File metadata and controls
181 lines (171 loc) · 5.63 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
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
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ff first
#define ss second
#define ins insert
#define sz(x) (int)x.size()
#define dbg(x) cout << x << "\n";
const int N = 2e5 + 5;
const long long int mod = 1e9 + 7;
const long long int Mod = 998244353;
const long double Pi = acos(-1);
const long long int Inf = 4e18;
const long double Eps = 1e-9;
int dx[9] = {0, 1, -1, 0, 0, 1, 1, -1, -1};
int dy[9] = {0, 0, 0, 1, -1, 1, -1, 1, -1};
using namespace std;
#define int long long int
template <typename T> class Matrix{
public:
int n, m; // dims
vector <vector <int> > g;
// parameterized constructor
Matrix <T> (int rows = 1, int cols = 1){
n = rows;
m = cols;
g.resize(n);
for(int i = 0; i<n; i++) g[i].assign(m, 0);
}
// copy constructor
Matrix <T> (const Matrix &A){
n = A.n;
m = A.m;
g = A.g;
}
Matrix <T> Identity (const int &rows){
// square matrix of size rows * rows
Matrix <T> ret(rows, rows);
for(int i = 0; i<rows; i++) ret.g[i][i] = 1;
return ret;
}
Matrix <T> Ones (const int &rows, const int &cols){
// matrix of all ones
Matrix <T> ret(rows, cols);
for(int i = 0; i<rows; i++){
for(int j = 0; j<cols; j++) ret.g[i][j] = 1;
}
return ret;
}
void Show (){
// prints the matrix
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++) cout << g[i][j] << " ";
cout << "\n";
}
}
Matrix <T> operator + (int z){
// Adds constant z to each element of the matrix
Matrix <T> ret(n, m);
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++) ret.g[i][j] = this->g[i][j] + z;
}
return ret;
}
Matrix <T> operator + (const Matrix <T> &A){
/*
Matrix Addition
dims A = (n, m)
*/
Matrix <T> ret(n, m);
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++) ret.g[i][j] = g[i][j] + A.g[i][j];
}
return ret;
}
Matrix <T> operator - (int z){
// subtracts constant z from each element of the matrix
Matrix <T> ret(n, m);
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++) ret.g[i][j] = this->g[i][j] - z;
}
return ret;
}
Matrix <T> operator - (const Matrix <T> &A){
/*
matrix subtraction
dims A = (n, m)
*/
Matrix <T> ret(n, m);
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++) ret.g[i][j] = g[i][j] - A.g[i][j];
}
return ret;
}
Matrix <T> operator * (int z){
// multiplies each element of the matrix with z
Matrix <T> ret(n, m);
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++) ret.g[i][j] = this->g[i][j] * z;
}
return ret;
}
Matrix <T> Multiply (const Matrix <T> &A, const Matrix <T> &B){
/*
matrix multiplication
columns in A = rows in B
*/
assert(A.m == B.n);
Matrix <T> ret(A.n, B.m);
for(int i = 0; i<A.n; i++){
for(int j = 0; j<A.m; j++){
for(int k = 0; k<B.m; k++){
ret.g[i][k] = (ret.g[i][k] + (A.g[i][j] * B.g[j][k]) % mod) % mod;
}
}
}
return ret;
}
Matrix <T> operator / (int z){
// integer division of each matrix element by z
Matrix <T> ret(n, m);
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++) ret.g[i][j] = this->g[i][j] / z;
}
return ret;
}
Matrix <T> operator % (const int &md){
/*
modulo operation on each of the
matrix element
*/
Matrix <T> A = (*this);
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++) A.g[i][j] = (A.g[i][j] % md);
}
return A;
}
Matrix <T> MatExp (int b){
/*
matrix exponentiation
only for square matrices.
*/
Matrix <T> ret = Identity(n), A = *this;
assert(b >= 0);
while(b > 0){
if(b & 1) ret = Multiply(ret, A);
A = Multiply(A, A);
b >>= 1;
}
return ret;
}
};
void TestCase (){
// your code here
}
#undef int
int main(){
IOS;
int T = 1;
// cin >> T;
while(T--){
TestCase();
cout << "\n";
}
return 0;
}