-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
298 lines (249 loc) · 6.79 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
/*
* Actual implementation of the Matrix object methos
*
* Copyright Giorgio Bella 2022-2022
*/
#include "Matrix.h"
#include <math.h>
#define MAX_DIM 1000
//determinat
long Matrix::det() {
long det = 0;
if (!this->isSquareMatrix()) return 0;
else if (this->collumDim == 1)
return this->mat[0][0];
else if (this->collumDim == 2)
return (this->mat[0][0] * this->mat[1][1]) - (this->mat[0][1] * this->mat[1][0]);
else {
double submatrix[MAX_DIM][MAX_DIM];
for (int x = 0; x < this->collumDim; x++) {
int subi = 0;
for (int i = 1; i < this ->collumDim; i++) {
int subj = 0;
for (int j = 0; j <this->collumDim; j++) {
if (j == x)
continue;
submatrix[subi][subj] = this->mat[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * mat[0][x] * this->det(submatrix, this->collumDim - 1));
}
}
return det;
}
/*
* det overload to call the function recursively
*/
long Matrix::det( double matrix[MAX_DIM][MAX_DIM], int dim) {
long det = 0;
if (!this->isSquareMatrix()) return 0;
else if (this->collumDim == 1)
return this->mat[0][0];
else if (this->collumDim == 2)
return (this->mat[0][0] * this->mat[1][1]) - (this->mat[0][1] * this->mat[1][0]);
else {
double submatrix[MAX_DIM][MAX_DIM];
for (int x = 0; x < this->collumDim; x++) {
int subi = 0;
for (int i = 1; i < this->collumDim; i++) {
int subj = 0;
for (int j = 0; j < this->collumDim; j++) {
if (j == x)
continue;
submatrix[subi][subj] = this->mat[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * mat[0][x] * this->det(submatrix, this->collumDim - 1));
}
}
return det;
}
/*
* if a matrix has the same collums and rows
*/
bool Matrix::isSquareMatrix() {
if (this->collumDim == this->rowsDim) return true;
else return false;
}
/*
* Find the trace of a matrix
*/
long Matrix::tr() {
long traceSum = 0;
if (!this->isSquareMatrix()) return;
else if (this->collumDim == 1) return mat[0][0];
else if (this->collumDim == 2) return mat[0][0] + mat[1][1];
else {
for (int i = 0; i < this->collumDim; i++) {
traceSum += mat[i][i];
}
return traceSum;
}
}
/*
* Multiply the matrix by a scalar number
*/
Matrix Matrix::scalarMultiplication(long number) {
double resMat[MAX_DIM][MAX_DIM];
for (int i = 0; i < this->rowsDim; i++) {
for (int j = 0; j < this->collumDim; j++) {
resMat[i][j] = this->mat[i][j] * number;
}
}
return Matrix(this->rowsDim, this->collumDim, resMat);
}
/*
* Gaussian Elemination algorithm
*/
Matrix Matrix::gaussianElimination() {
/* performing Gaussian elimination */
double resMat[MAX_DIM][MAX_DIM];
for (int i = 0; i < this->rowsDim - 1; i++)
{
for (int j = i + 1; j < this->collumDim; j++)
{
float f = this ->mat[j][i] /this-> mat[i][i];
int iterations = 0;
if (this->rowsDim > this->collumDim) iterations = this->rowsDim;
else iterations = this->collumDim;
for (int k = 0; k < iterations; k++)
{
resMat[j][k] = this ->mat[j][k] - f * this ->mat[i][k];
}
}
}
return Matrix(this->rowsDim, this->collumDim, resMat);
}
/*
* Reduce the matrixs to row inchelon form
*/
Matrix Matrix::rowInchelonForm() {
double resMat[MAX_DIM][MAX_DIM];
for (int i = 0; i < this->rowsDim; i++)
{
for (int j = i + 1; j < this->collumDim; j++)
{
if (fabsf(this->mat[i][i]) < fabsf(this->mat[j][i]))
{
int iterations = 0;
if (this->rowsDim > this->collumDim) iterations = this->rowsDim;
else iterations = this->collumDim;
for (int k = 0; k < iterations; k++)
{
/* swapping mat[i][k] and mat[j][k] */
resMat[i][k] = this->mat[i][k] + this->mat[j][k];
resMat[j][k] = this->mat[i][k] - this->mat[j][k];
resMat[i][k] = this->mat[i][k] - this->mat[j][k];
}
}
}
}
return Matrix(this->rowsDim, this->collumDim, resMat);
}
/*
* Compute the inverse of a matrix
*/
Matrix Matrix::inverse() {
long det = 0;
if (!this->isSquareMatrix()) return;
det = this->det();
if (this->collumDim == 2 && det !=0) {
const long temp = mat[0][0];
mat[0][0] = mat[1][1];
mat[1][1] = temp;
mat[0][1] = -mat[0][1];
mat[1][0] = -mat[1][0];
return this->scalarMultiplication(1/det);
}
else if (this ->collumDim > 2 && det !=0) {
// generalize with gaussJordan algo
double res [MAX_DIM][MAX_DIM];
Matrix adjointMatrix = this->adjoint();
for (int i = 0; i < this->collumDim; i++) {
for (int j = 0; i < this->collumDim; j++) {
res[i][j] = adjointMatrix.mat[i][j] * 1 / det;
}
}
return Matrix(this->collumDim, this->collumDim, res);
}
}
Matrix Matrix::traspose() {
double resMat[MAX_DIM][MAX_DIM];
for (int i = 0; i < this->rowsDim; i++) {
for (int j = 0; i < this->collumDim; j++) {
this -> mat[i][j] = this -> mat[j][i];
}
}
return Matrix(this->collumDim, this->rowsDim, resMat);
}
Matrix Matrix::getCofactorMaxtrix(int row, int collum) {
if (!this->isSquareMatrix()) return;
int rowCounter, collumCounter = 0;
double cofactorMatrix[MAX_DIM][MAX_DIM];
for (int i = 0; i < this->rowsDim; i++) {
for (int j = 0; j < this->collumDim; j++) {
if (i != row && j != collum) {
cofactorMatrix[rowCounter][collumCounter ++] = this->mat[i][j];
if (j == this->collumDim - 1) {
rowCounter ++;
collumCounter = 0;
}
}
}
}
return Matrix(cofactorMatrix);
}
Matrix Matrix::adjoint() {
double adjointMatrix[MAX_DIM][MAX_DIM];
if (!this->isSquareMatrix()) return;
else {
int sign = 1;
for (int i = 0; i < this->rowsDim; i++) {
for (int j = 0; j < this->rowsDim; j++) {
Matrix cofactorMatrix = this->getCofactorMaxtrix(i, j);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adjointMatrix[i][j] = sign * this->det(cofactorMatrix.mat, this->rowsDim - 1);
}
}
return Matrix(adjointMatrix);
}
}
Matrix Matrix::sumMatricies(Matrix m1, Matrix m2) {
if (m1.rowsDim != m2.rowsDim && m1.collumDim != m2.collumDim) return;
double resMat[MAX_DIM][MAX_DIM];
for (int i = 0; i < m1.rowsDim; i++) {
for (int j = 0; j < m1.rowsDim; j++) {
resMat[i][j] = m1.mat[i][j] + m2.mat[i][j];
}
}
return Matrix(m1.rowsDim, m1.collumDim, resMat);
}
Matrix Matrix::multiplyMatricies(Matrix m1, Matrix m2) {
if (m1.collumDim != m2.rowsDim) return;
double res[MAX_DIM][MAX_DIM];
for (int i = 0; i < m1.collumDim; i++) {
for (int j = 0; j < m2.rowsDim; j++) {
res[i][j] = 0;
for (int k = 0; k < m2.rowsDim; k++) {
res[i][j] += m1.mat[i][k] * m2.mat[k][j];
}
}
}
return Matrix(m1.rowsDim, m2.collumDim, res);
}
Matrix Matrix::getSubMatrix(int fromRow, int toRow, int fromCol, int toCol){
if (fromRow > this->rowsDim || fromCol > this->collumDim) return;
double res[MAX_DIM][MAX_DIM];
for (int i = 0; i < this->collumDim; i++) {
for (int j = 0; j < this->rowsDim; j++) {
if (fromCol >= i && fromRow >= j && toCol <=i && toRow<=j) {
res[i][j] = this->mat[i][j];
}
}
}
return Matrix(res);
}