-
Notifications
You must be signed in to change notification settings - Fork 34
/
matrix.cpp
549 lines (484 loc) · 13 KB
/
matrix.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* matrix.cpp
*/
#include <stdexcept>
#include "matrix.h"
#define EPS 1e-10
using std::ostream; using std::istream; using std::endl;
using std::domain_error;
/* PUBLIC MEMBER FUNCTIONS
********************************/
Matrix::Matrix(int rows, int cols) : rows_(rows), cols_(cols)
{
allocSpace();
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
p[i][j] = 0;
}
}
}
Matrix::Matrix(double** a, int rows, int cols) : rows_(rows), cols_(cols)
{
allocSpace();
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
p[i][j] = a[i][j];
}
}
}
Matrix::Matrix() : rows_(1), cols_(1)
{
allocSpace();
p[0][0] = 0;
}
Matrix::~Matrix()
{
for (int i = 0; i < rows_; ++i) {
delete[] p[i];
}
delete[] p;
}
Matrix::Matrix(const Matrix& m) : rows_(m.rows_), cols_(m.cols_)
{
allocSpace();
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
p[i][j] = m.p[i][j];
}
}
}
Matrix& Matrix::operator=(const Matrix& m)
{
if (this == &m) {
return *this;
}
if (rows_ != m.rows_ || cols_ != m.cols_) {
for (int i = 0; i < rows_; ++i) {
delete[] p[i];
}
delete[] p;
rows_ = m.rows_;
cols_ = m.cols_;
allocSpace();
}
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
p[i][j] = m.p[i][j];
}
}
return *this;
}
Matrix& Matrix::operator+=(const Matrix& m)
{
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
p[i][j] += m.p[i][j];
}
}
return *this;
}
Matrix& Matrix::operator-=(const Matrix& m)
{
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
p[i][j] -= m.p[i][j];
}
}
return *this;
}
Matrix& Matrix::operator*=(const Matrix& m)
{
Matrix temp(rows_, m.cols_);
for (int i = 0; i < temp.rows_; ++i) {
for (int j = 0; j < temp.cols_; ++j) {
for (int k = 0; k < cols_; ++k) {
temp.p[i][j] += (p[i][k] * m.p[k][j]);
}
}
}
return (*this = temp);
}
Matrix& Matrix::operator*=(double num)
{
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
p[i][j] *= num;
}
}
return *this;
}
Matrix& Matrix::operator/=(double num)
{
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
p[i][j] /= num;
}
}
return *this;
}
Matrix Matrix::operator^(int num)
{
Matrix temp(*this);
return expHelper(temp, num);
}
void Matrix::swapRows(int r1, int r2)
{
double *temp = p[r1];
p[r1] = p[r2];
p[r2] = temp;
}
Matrix Matrix::transpose()
{
Matrix ret(cols_, rows_);
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j < cols_; ++j) {
ret.p[j][i] = p[i][j];
}
}
return ret;
}
/* STATIC CLASS FUNCTIONS
********************************/
Matrix Matrix::createIdentity(int size)
{
Matrix temp(size, size);
for (int i = 0; i < temp.rows_; ++i) {
for (int j = 0; j < temp.cols_; ++j) {
if (i == j) {
temp.p[i][j] = 1;
} else {
temp.p[i][j] = 0;
}
}
}
return temp;
}
Matrix Matrix::solve(Matrix A, Matrix b)
{
// Gaussian elimination
for (int i = 0; i < A.rows_; ++i) {
if (A.p[i][i] == 0) {
// pivot 0 - throw error
throw domain_error("Error: the coefficient matrix has 0 as a pivot. Please fix the input and try again.");
}
for (int j = i + 1; j < A.rows_; ++j) {
for (int k = i + 1; k < A.cols_; ++k) {
A.p[j][k] -= A.p[i][k] * (A.p[j][i] / A.p[i][i]);
if (A.p[j][k] < EPS && A.p[j][k] > -1*EPS)
A.p[j][k] = 0;
}
b.p[j][0] -= b.p[i][0] * (A.p[j][i] / A.p[i][i]);
if (A.p[j][0] < EPS && A.p[j][0] > -1*EPS)
A.p[j][0] = 0;
A.p[j][i] = 0;
}
}
// Back substitution
Matrix x(b.rows_, 1);
x.p[x.rows_ - 1][0] = b.p[x.rows_ - 1][0] / A.p[x.rows_ - 1][x.rows_ - 1];
if (x.p[x.rows_ - 1][0] < EPS && x.p[x.rows_ - 1][0] > -1*EPS)
x.p[x.rows_ - 1][0] = 0;
for (int i = x.rows_ - 2; i >= 0; --i) {
int sum = 0;
for (int j = i + 1; j < x.rows_; ++j) {
sum += A.p[i][j] * x.p[j][0];
}
x.p[i][0] = (b.p[i][0] - sum) / A.p[i][i];
if (x.p[i][0] < EPS && x.p[i][0] > -1*EPS)
x.p[i][0] = 0;
}
return x;
}
Matrix Matrix::bandSolve(Matrix A, Matrix b, int k)
{
// optimized Gaussian elimination
int bandsBelow = (k - 1) / 2;
for (int i = 0; i < A.rows_; ++i) {
if (A.p[i][i] == 0) {
// pivot 0 - throw exception
throw domain_error("Error: the coefficient matrix has 0 as a pivot. Please fix the input and try again.");
}
for (int j = i + 1; j < A.rows_ && j <= i + bandsBelow; ++j) {
int k = i + 1;
while (k < A.cols_ && A.p[j][k]) {
A.p[j][k] -= A.p[i][k] * (A.p[j][i] / A.p[i][i]);
k++;
}
b.p[j][0] -= b.p[i][0] * (A.p[j][i] / A.p[i][i]);
A.p[j][i] = 0;
}
}
// Back substitution
Matrix x(b.rows_, 1);
x.p[x.rows_ - 1][0] = b.p[x.rows_ - 1][0] / A.p[x.rows_ - 1][x.rows_ - 1];
for (int i = x.rows_ - 2; i >= 0; --i) {
int sum = 0;
for (int j = i + 1; j < x.rows_; ++j) {
sum += A.p[i][j] * x.p[j][0];
}
x.p[i][0] = (b.p[i][0] - sum) / A.p[i][i];
}
return x;
}
// functions on VECTORS
double Matrix::dotProduct(Matrix a, Matrix b)
{
double sum = 0;
for (int i = 0; i < a.rows_; ++i) {
sum += (a(i, 0) * b(i, 0));
}
return sum;
}
// functions on AUGMENTED matrices
Matrix Matrix::augment(Matrix A, Matrix B)
{
Matrix AB(A.rows_, A.cols_ + B.cols_);
for (int i = 0; i < AB.rows_; ++i) {
for (int j = 0; j < AB.cols_; ++j) {
if (j < A.cols_)
AB(i, j) = A(i, j);
else
AB(i, j) = B(i, j - B.cols_);
}
}
return AB;
}
Matrix Matrix::gaussianEliminate()
{
Matrix Ab(*this);
int rows = Ab.rows_;
int cols = Ab.cols_;
int Acols = cols - 1;
int i = 0; // row tracker
int j = 0; // column tracker
// iterate through the rows
while (i < rows)
{
// find a pivot for the row
bool pivot_found = false;
while (j < Acols && !pivot_found)
{
if (Ab(i, j) != 0) { // pivot not equal to 0
pivot_found = true;
} else { // check for a possible swap
int max_row = i;
double max_val = 0;
for (int k = i + 1; k < rows; ++k)
{
double cur_abs = Ab(k, j) >= 0 ? Ab(k, j) : -1 * Ab(k, j);
if (cur_abs > max_val)
{
max_row = k;
max_val = cur_abs;
}
}
if (max_row != i) {
Ab.swapRows(max_row, i);
pivot_found = true;
} else {
j++;
}
}
}
// perform elimination as normal if pivot was found
if (pivot_found)
{
for (int t = i + 1; t < rows; ++t) {
for (int s = j + 1; s < cols; ++s) {
Ab(t, s) = Ab(t, s) - Ab(i, s) * (Ab(t, j) / Ab(i, j));
if (Ab(t, s) < EPS && Ab(t, s) > -1*EPS)
Ab(t, s) = 0;
}
Ab(t, j) = 0;
}
}
i++;
j++;
}
return Ab;
}
Matrix Matrix::rowReduceFromGaussian()
{
Matrix R(*this);
int rows = R.rows_;
int cols = R.cols_;
int i = rows - 1; // row tracker
int j = cols - 2; // column tracker
// iterate through every row
while (i >= 0)
{
// find the pivot column
int k = j - 1;
while (k >= 0) {
if (R(i, k) != 0)
j = k;
k--;
}
// zero out elements above pivots if pivot not 0
if (R(i, j) != 0) {
for (int t = i - 1; t >= 0; --t) {
for (int s = 0; s < cols; ++s) {
if (s != j) {
R(t, s) = R(t, s) - R(i, s) * (R(t, j) / R(i, j));
if (R(t, s) < EPS && R(t, s) > -1*EPS)
R(t, s) = 0;
}
}
R(t, j) = 0;
}
// divide row by pivot
for (int k = j + 1; k < cols; ++k) {
R(i, k) = R(i, k) / R(i, j);
if (R(i, k) < EPS && R(i, k) > -1*EPS)
R(i, k) = 0;
}
R(i, j) = 1;
}
i--;
j--;
}
return R;
}
void Matrix::readSolutionsFromRREF(ostream& os)
{
Matrix R(*this);
// print number of solutions
bool hasSolutions = true;
bool doneSearching = false;
int i = 0;
while (!doneSearching && i < rows_)
{
bool allZeros = true;
for (int j = 0; j < cols_ - 1; ++j) {
if (R(i, j) != 0)
allZeros = false;
}
if (allZeros && R(i, cols_ - 1) != 0) {
hasSolutions = false;
os << "NO SOLUTIONS" << endl << endl;
doneSearching = true;
} else if (allZeros && R(i, cols_ - 1) == 0) {
os << "INFINITE SOLUTIONS" << endl << endl;
doneSearching = true;
} else if (rows_ < cols_ - 1) {
os << "INFINITE SOLUTIONS" << endl << endl;
doneSearching = true;
}
i++;
}
if (!doneSearching)
os << "UNIQUE SOLUTION" << endl << endl;
// get solutions if they exist
if (hasSolutions)
{
Matrix particular(cols_ - 1, 1);
Matrix special(cols_ - 1, 1);
for (int i = 0; i < rows_; ++i) {
bool pivotFound = false;
bool specialCreated = false;
for (int j = 0; j < cols_ - 1; ++j) {
if (R(i, j) != 0) {
// if pivot variable, add b to particular
if (!pivotFound) {
pivotFound = true;
particular(j, 0) = R(i, cols_ - 1);
} else { // otherwise, add to special solution
if (!specialCreated) {
special = Matrix(cols_ - 1, 1);
specialCreated = true;
}
special(j, 0) = -1 * R(i, j);
}
}
}
os << "Special solution:" << endl << special << endl;
}
os << "Particular solution:" << endl << particular << endl;
}
}
Matrix Matrix::inverse()
{
Matrix I = Matrix::createIdentity(rows_);
Matrix AI = Matrix::augment(*this, I);
Matrix U = AI.gaussianEliminate();
Matrix IAInverse = U.rowReduceFromGaussian();
Matrix AInverse(rows_, cols_);
for (int i = 0; i < AInverse.rows_; ++i) {
for (int j = 0; j < AInverse.cols_; ++j) {
AInverse(i, j) = IAInverse(i, j + cols_);
}
}
return AInverse;
}
/* PRIVATE HELPER FUNCTIONS
********************************/
void Matrix::allocSpace()
{
p = new double*[rows_];
for (int i = 0; i < rows_; ++i) {
p[i] = new double[cols_];
}
}
Matrix Matrix::expHelper(const Matrix& m, int num)
{
if (num == 0) {
return createIdentity(m.rows_);
} else if (num == 1) {
return m;
} else if (num % 2 == 0) { // num is even
return expHelper(m * m, num/2);
} else { // num is odd
return m * expHelper(m * m, (num-1)/2);
}
}
/* NON-MEMBER FUNCTIONS
********************************/
Matrix operator+(const Matrix& m1, const Matrix& m2)
{
Matrix temp(m1);
return (temp += m2);
}
Matrix operator-(const Matrix& m1, const Matrix& m2)
{
Matrix temp(m1);
return (temp -= m2);
}
Matrix operator*(const Matrix& m1, const Matrix& m2)
{
Matrix temp(m1);
return (temp *= m2);
}
Matrix operator*(const Matrix& m, double num)
{
Matrix temp(m);
return (temp *= num);
}
Matrix operator*(double num, const Matrix& m)
{
return (m * num);
}
Matrix operator/(const Matrix& m, double num)
{
Matrix temp(m);
return (temp /= num);
}
ostream& operator<<(ostream& os, const Matrix& m)
{
for (int i = 0; i < m.rows_; ++i) {
os << m.p[i][0];
for (int j = 1; j < m.cols_; ++j) {
os << " " << m.p[i][j];
}
os << endl;
}
return os;
}
istream& operator>>(istream& is, Matrix& m)
{
for (int i = 0; i < m.rows_; ++i) {
for (int j = 0; j < m.cols_; ++j) {
is >> m.p[i][j];
}
}
return is;
}