-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix Template.cpp
419 lines (347 loc) · 9.01 KB
/
Matrix Template.cpp
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include<bits/stdc++.h>
using namespace std;
/// source : https://github.com/mridul1809/CompetitiveProgramming/blob/master/templates/Matrix.cpp
//set this macro when you need to use modulo in matrix operations
#define mod 1000000007
template <typename T>
class Matrix {
public:
vector < vector <T> > A;
int r,c;
//Default Constructor
Matrix()
{
this->r = 0;
this->c = 0;
}
//Matrix with given dimensions and random values
Matrix(int r,int c)
{
this->r = r;
this->c = c;
A.assign(r , vector <T> (c));
}
//Matrix with given value and dimensions
Matrix(int r,int c,const T &val)
{
this->r = r;
this->c = c;
A.assign(r , vector <T> (c , val));
}
//Identity matrix with given dimension
Matrix(int n)
{
this->r = this->c = n;
A.assign(n , vector <T> (n));
for(int i=0;i<n;i++)
A[i][i] = (T)1;
}
//Print the matrix
void display()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cout << A[i][j] << " ";
cout << endl;
}
}
//Input called to get input
//Put custom code
void input()
{
// for(int i=0;i<r;i++)
// for(int j=0;j<c;j++)
// define inout here
}
//Overloaded * operator to multiply 2 matrices
//with conformable dimensions
Matrix operator * (const Matrix<T> &B)
{
assert(c == B.r);
int i,j,k;
int x = r;
int y = c;
int z = B.c;
Matrix <T> C(x,z,0);
for(i=0 ; i<x ; i++)
for(j=0 ; j<z ; j++)
for(k=0 ; k<y ; k++)
{
C[i][j] = (C[i][j] + ( (long long )A[i][k] * (long long)B[k][j] ) );
#ifdef mod
C[i][j] %= mod;
#endif
}
return C;
}
//Overloaded *= operator to add 2 matrices
//of conformable dimensions
//and save result in first matrix
void operator *= (const Matrix<T> &B)
{
assert(c == B.r);
int i,j,k;
int x = r;
int y = c;
int z = B.c;
Matrix <T> C(x,z,0);
for(i=0 ; i<x ; i++)
for(j=0 ; j<z ; j++)
for(k=0 ; k<y ; k++)
{
C[i][j] = (C[i][j] + ( (long long)A[i][k] * (long long)B[k][j] ) );
#ifdef mod
C[i][j] %= mod;
#endif
}
this->r = C.r;
this->c = C.c;
this->A = C.A;
}
//Overloaded + operator to add 2 matrices
//with same dimensions
Matrix operator + (const Matrix<T> &B)
{
assert(r == B.r);
assert(c == B.c);
Matrix <T> C(r,c,0);
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
C[i][j] = A[i][j] + B[i][j];
#ifdef mod
C[i][j] %= mod;
#endif
}
return C;
}
//Overloaded += operator to add 2 matrices
//of same dimensions
//and save result in first matrix
void operator += (const Matrix<T> &B)
{
assert(r == B.r);
assert(c == B.c);
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
A[i][j] = A[i][j] + B[i][j];
#ifdef mod
A[i][j] %= mod;
#endif
}
}
//Overload unary - to get negative of a matrix
Matrix operator - ()
{
Matrix <T> C(r,c,0);
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
C[i][j] = -A[i][j];
#ifdef mod
C[i][j] %= mod;
if(C[i][j] < 0)
C[i][j] += mod;
#endif
}
return C;
}
//Overload binary - to subtract a matrix
//from other with same dimensions
Matrix operator - (const Matrix<T> &B)
{
assert(r == B.r);
assert(c == B.c);
Matrix <T> C(r,c,0);
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
C[i][j] = A[i][j] - B[i][j];
#ifdef mod
C[i][j] %= mod;
if(C[i][j] < 0)
C[i][j] += mod;
#endif
}
return C;
}
//Overload binary - to subtract a matrix
//from other with same dimensions
//and save result in first matrix
void operator -= (const Matrix<T> &B)
{
assert(r == B.r);
assert(c == B.c);
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
A[i][j] = A[i][j] - B[i][j];
#ifdef mod
A[i][j] %= mod;
if(A[i][j] < 0)
A[i][j] += mod;
#endif
}
}
//Overload ^ operator to raise a square matrix to a power
//Using binary matrix exponentiation
Matrix operator ^ (long long n)
{
assert(r == c);
int i,j;
Matrix <T> C(r);
Matrix <T> X(r,c,0);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
X[i][j] = A[i][j];
while(n)
{
if(n&1)
C *= X;
X *= X;
n /= 2;
}
return C;
}
//overloaded ^= operator to raise square matrix to power
//Using binary exponentiation and store the result
//in same matrix
void operator ^= (long long n)
{
assert(r == c);
int i,j;
Matrix <T> C(r) ;
Matrix <T> X(r,c,0);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
X[i][j] = A[i][j];
while(n)
{
if(n&1)
C *= (*this);
(*this) *= (*this);
n /= 2;
}
this->A = C.A;
}
//transpose operation
Matrix transpose()
{
Matrix <T> C(c,r);
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
C[j][i] = A[i][j];
return C;
}
//transpose inplace
void transposeInplace()
{
Matrix <T> C(c,r);
int i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
C[j][i] = A[i][j];
this->r = C.r;
this->c = C.c;
this->A = C.A;
}
//Overload to access/set elements without using dot operator
//Example :
// Matrix m(5,3);
// m.A[i][j] is valid to access
// m[i][j] is valid as well
vector<T>& operator [] (int i)
{
assert(i < r);
assert(i >= 0);
return A[i];
}
//Overload to access/set elements without using dot operator
//for accessing from cosnt objects
const vector<T>& operator [] (int i) const
{
assert(i < r);
assert(i >= 0);
return A[i];
}
//outstream has been overloaded to quickly print the matrix
//help quicken the debugging process
//eg) cout << M <<endl;
friend ostream& operator << (ostream &out,const Matrix<T> &M)
{
for (int i = 0; i < M.r; ++i) {
for (int j = 0; j < M.c; ++j) {
out << M[i][j] << " ";
}
out << '\n';
}
return out;
}
};
//Examples :
int main()
{
//Example of multiplication
//*
Matrix <int> A(2,3,1);
Matrix <int> B(3,2,2);
Matrix <int> C = A*B;
cout << C << endl;
//*=
A *= B;
cout << A << endl;
//Example of addition
//+
Matrix <int> D(2,3,1);
Matrix <int> E(2,3,2);
Matrix <int> F = D + E;
cout << F << endl;
//+=
D += E;
cout << D << endl;
//Example of subtraction
//- binary
Matrix <int> G(2,3,1);
Matrix <int> H(2,3,2);
Matrix <int> I = G - H;
cout << I << endl;
//- unary
I = -G;
cout << I << endl;
//-=
G -= H;
cout << G << endl;
//Example of power
Matrix <int> J(2,2);
//overloaded box operator to avoid writing M.A[i][j]
J[0][0] = 1; //same as J.A[0][0]
J[0][1] = 1; //same as J.A[0][1]
J[1][0] = 1; //same as J.A[1][0]
J[1][1] = 0; //same as J.A[1][1]
cout << J << endl;
//^ operator
Matrix <int> K = J^6;
cout << K << endl;
//^= operator
J ^= 6;
cout << J << endl;
//example of transpose
Matrix <int> L(2,4);
L[1][3] = 5;
L[1][1] = 9;
cout << L << endl;
//transpose the matrix
Matrix <int> M = L.transpose();
cout << M << endl;
//transpose inplace
L.transposeInplace();
cout << L << endl;
}