-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
415 lines (281 loc) · 9.82 KB
/
matrix.cpp
File metadata and controls
415 lines (281 loc) · 9.82 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
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
#include "matrix.h"
double EPSILON = std::numeric_limits<double>::epsilon() * 1.0e-100;
matrix::matrix(std::size_t m, std::size_t n){
this -> m = m;
this -> n = n;
this -> mat.resize(m);
for (int i = 0; i < m; i++){
mat[i].resize(n);
}
}
matrix::matrix(std::size_t n){
this -> m = n;
this -> n = n;
this -> mat.resize(n);
for (int i = 0; i < n; i++){
mat[i].resize(n);
}
}
std::size_t matrix::getM(){
return m;
}
std::size_t matrix::getN(){
return n;
}
std::vector< std::vector<double> >& matrix::getMat(){
std::vector< std::vector<double> > &a = mat;
return a;
}
double matrix::getVal(std::size_t row, std::size_t col){
return mat[row - 1][col - 1];
}
void matrix::matedit(double val, std::size_t row, std::size_t col){ //1 is an error, 0 is not an error
mat[row - 1][col - 1] = val;
}
void matrix::fillmat(double num){
for(int i = 0; i < m; i++){
std::fill(mat[i].begin(), mat[i].end(), num);
}
}
void matrix::fillrow(std::size_t row, double val){
for(std::size_t i = 0; i < n; i++){
mat[row - 1][i] = val;
}
}
void matrix::fillcol(std::size_t col, double val){
for(std::size_t i = 0; i < m; i++){
mat[i][col - 1] = val;
}
}
void matrix::clearmat(){
fillmat(0);
}
double matrix::findDet(){ //will find the determinant of a nxn matrix
if (m != n){
std::cerr << "Error: Determinant cannot be calculated. Row and Column have different dimensions for method: findDet()" << std::endl;
exit(-1);
}
double det = 0;
if (n == 1){ //if mat is a 1x1 matrix
det = mat[0][0];
return det;
}
if (n == 2){ //2x2 matrix
det = (mat[0][0]*mat[1][1])-(mat[0][1]*mat[1][0]);
return det;
}
if (n == 3){ //3x3 matrix, commonly used in many applications so a shortcut is made
det = mat[0][0]*(mat[1][1]*mat[2][2]-mat[1][2]*mat[2][1])-mat[0][1]*(mat[1][0]*mat[2][2]-mat[1][2]*mat[2][0])+mat[0][2]*(mat[1][0]*mat[2][1]-mat[1][1]*mat[2][0]);
return det;
}
//CODE FOR TAKING CARE OF MATRIX WHOSE DIMENSIONS EXCEEDS 3
for (int i = 0; i < n; i++){ //summation formula
int cx = 0;
int cy = 0;
matrix* auxMat = new matrix(n-1);//creates an temporary sub matrix
for (int y = 1; y < n; y++){ //row could be n-1
cx = 0; //reset x
for (int x = 0; x < n; x++){ //column could be n-1 //index doesnt match, n can be bigger than what the matrix is willing to accept
if (x == i){ //we will no calculate the column with determina
continue;
}
auxMat -> mat[cy][cx] = mat[y][x];
cx += 1;
if (cx == n-1) break; //if the matrix is filled up
}
cy += 1;
if (cy == n-1) break; //if the y is filled up
}
//Calculate the determinant
if (i%2==0){ //i is even
det += mat[0][i]*auxMat -> findDet();
}else{ //i is odd
det -= mat[0][i]*auxMat -> findDet();
}
delete auxMat;
}
return det;
}
void matrix::rowswap(std::size_t row1, std::size_t row2){ //swaps two rows of a matrix
if (row1 == row2) return void();
double temp = 0;
for(std::size_t i = 0; i < n; i++){
temp = mat[row1 - 1][i];
mat[row1-1][i] = mat[row2-1][i];
mat[row2-1][i] = temp;
}
}
int iszerocol(matrix* mat, std::size_t col){
for(std::size_t i = 1; i < mat->getM(); i++){
if(mat->getVal(i, col) != 0) return 0;
}
return 1;
}
void matrix::dispMat(){
for(std::size_t i = 0; i < m; i++){
std::cout << "[ ";
for(std::size_t j = 0; j < n; j++){
std::cout << mat[i][j] << ' ';
}
std::cout << "]" << std::endl;
}
}
matrix* ref(matrix* mat){ //makes a copy of a matrix in reduced row echelon form, will return original matrix if matrix is already in rref. Algorithm will be Gaussian elimination
matrix* rmat = new matrix(mat -> getM(), mat -> getN());
for(int i = 1; i <= rmat->getM(); i++){
for(int j = 1; j <= rmat->getN(); j++){
rmat -> matedit(mat -> getVal(i, j), i, j);
}
}
//Now to reduce rmat
std::vector< std::vector <double> > &temp = rmat -> getMat();
std::size_t x = 0, y = 0;
double* pivot = &temp[x][y]; //sets the pivot
std::size_t c = 1; //signifies what column we are at
bool begin = 0;
while(x < rmat->getM() && y < rmat -> getN()){
if(iszerocol(rmat, c)){
c++;
y++;
continue;
}
else if (begin == 0){ //we are at beginning, this step will be used to rowswap all of the rows to their proper place
for(std::size_t i = x; i < rmat -> getM(); i++){
if (std::abs(temp[i][y] - 0) > EPSILON){
begin = 1;
rmat -> rowswap(i+1, 1);
break;
}
}
}
else{ //we have found a nonzero value
while(std::abs(temp[x][y] - 0) < EPSILON){ //finding leading value
if (x == rmat -> getM() - 1) break; //we have encountered a zero row
x++;
}
pivot = &temp[x][y];
for(std::size_t i = x + 1; i < rmat -> getM(); i++){ //for each row
if(std::abs(temp[i][y] - 0) < EPSILON) continue;
double k = temp[i][y] / (*pivot);
for (std::size_t j = 0; j < rmat->getN(); j++){ //for each column on a given row
if (j == y){
temp[i][j] = 0;
continue;
}
temp[i][j] = -1*temp[x][j]*k + temp[i][j];
}
}
//row operations have been performed
//now we have finished converting the first column
//Moving on to next row and column
y++;
x++;
c++;
if (x > rmat -> getM() - 1 || y > rmat -> getN() - 1) break; //we have encountered a zero row
for(std::size_t i = x; i < rmat -> getM(); i++){
if (std::abs(temp[i][y] - 0) > EPSILON){
rmat -> rowswap(x+1, i+1); //rowswap operation has been performed
break;
}
}
}
}
//We have reached the end of the matrix
return rmat;
}
matrix* rref(matrix* mat){
matrix* rmat = ref(mat);
std::vector< std::vector <double> > &temp = rmat -> getMat();
std::size_t x = rmat -> getM() - 1; std::size_t y = rmat -> getN() - 1;
//Finding leading entry on last row
for(std::size_t i = rmat -> getN() - 1; i >= 0 && i <= rmat -> getN(); i--){
if(std::abs(temp[x][i] - 0) > EPSILON){
y = i;
break;
}
}
while(x < rmat -> getM() && y < rmat -> getN()){
for(std::size_t i = 0; i < rmat -> getN(); i++){ //recalculating pivot
if(std::abs(temp[x][i] - 0) > EPSILON){
y = i;
break;
}
}
if(std::abs(temp[x][y] - 0) < EPSILON){
x--;
continue;
}
double* pivot = &temp[x][y];
for(std::size_t i = x - 1; i >= 0 && i <= x; i--){
if(std::abs(temp[i][y] - 0) < EPSILON) continue;
double k = temp[i][y] / (*pivot);
for(std::size_t j = 0; j < rmat -> getN(); j++){
if (j == y){
temp[i][j] = 0;
continue;
}
temp[i][j] = (-1)*temp[x][j]*k + temp[i][j];
}
}
//Row operations have been performed
double pval = *pivot;
for(std::size_t i = 0; i < rmat -> getN(); i++){
if (std::abs(temp[x][i] - 0) < EPSILON) continue;
temp[x][i] = temp[x][i] / pval;
}
//Moving on to next row and column
y--;
x--;
}
return rmat;
}
std::size_t matrix::rank(){
matrix* rmat = rref(this);
std::vector< std::vector <double> > &temp = rmat -> getMat();
std::size_t nzrowctr = 0;
for(std::size_t i = 0; i < m; i++){
int iszero = 1;
for(std::size_t j = 0; j < n; j++){
if(std::abs(temp[i][j] - 0) > EPSILON){
iszero = 0;
break;
}
}
if (iszero == 0) nzrowctr++;
}
delete rmat;
return nzrowctr;
}
std::size_t matrix::nullity(){
return n - this->rank();
}
matrix* matadd(matrix* a, matrix* b){
if (a -> getM() != b -> getM() || a -> getN() != b -> getN()){
std::cerr << "Error: matrices have different dimensions" << std::endl;
exit(-1);
}
matrix* c = new matrix(a -> getM(), a -> getN());
for (std::size_t i = 1; i <= a -> getM(); i++){
for (std::size_t j = 1; j <= a -> getN(); j++){
c -> matedit(a -> getVal(i, j) + b -> getVal(i, j), i, j);
}
}
return c;
}
matrix* matmult(matrix* a, matrix* b){
if (a -> getN() != b -> getM()){
std::cerr << "Error: The number of columns in the first matrix is not equal to the number of rows in the second matrix. Multiplication of the two matrices is impossible" << std::endl;
exit(-1);
}
matrix* c = new matrix(a -> getM(), b -> getN());
for (std::size_t i = 1; i <= a -> getM(); i++){
for (std::size_t j = 1; j <= b -> getN(); j++){
double total = 0;
for(std::size_t k = 1; k <= a -> getN(); k++){
total += (a -> getVal(i, k) * b -> getVal(k, j));
}
c -> matedit(total, i, j);
}
}
return c;
}