-
Notifications
You must be signed in to change notification settings - Fork 3
/
Matrix_Calculator.cpp
311 lines (290 loc) · 8.8 KB
/
Matrix_Calculator.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
/*******************************************************************************
* Author: Baraa Hamodi *
* Description: A simple matrix class capable of scalar multiplication, *
* matrix addition, matrix multiplication, transposing a matrix, *
* and determining the RREF of a matrix. *
*******************************************************************************/
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <algorithm>
using namespace std;
class Matrix {
private:
int row, col, scalar;
int matrix[10][10];
int result[10][10];
public:
// Default Constructor.
Matrix() {
row = 0;
col = 0;
scalar = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] = 0;
result[i][j] = 0;
}
}
}
// Constructor.
Matrix(int r, int c, int s) {
row = r;
col = c;
scalar = s;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] = 0;
result[i][j] = 0;
}
}
}
// Operator overload.
int& operator()(int row, int col) {
return matrix[row][col];
}
// Get matrix size and populate all matrix elements.
void getInput() {
do {
cout << "Rows = ";
cin >> row;
cout << "Columns = ";
cin >> col;
if (row < 1 || col < 1 || row > 10 || col > 10) {
cout << "Invalid entries. Min size = 1x1 and max size = 10x10.\n";
}
} while (row < 1 || col < 1 || row > 10 || col > 10);
cout << "Remember to enter matrix entries from top-left to bottom-right.\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << "Matrix Entry (" << i << "," << j << ") = ";
cin >> matrix[i][j];
}
}
}
// Output the full matrix to the screen in matrix representation.
void displayMatrix() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << setw(3) << matrix[i][j];
}
cout << endl;
}
}
// Output the full resultant matrix to the screen in matrix representation.
void displayResultMatrix(int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << setw(3) << result[i][j];
}
cout << endl;
}
}
// Perform scalar multiplication.
void scalarMultiplication() {
cout << "Enter a scalar to multiply the matrix by: ";
cin >> scalar;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] *= scalar;
}
}
displayMatrix();
}
// Perform matrix addition.
void matrixAddition(Matrix m) {
if (row == m.row && col == m.col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] += m(i,j);
}
}
displayMatrix();
} else {
cout << "Cannot complete matrix addition with these matrices.\n";
}
}
// Perform matrix multiplication.
void matrixMultiplication(Matrix m) {
if (col == m.row) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < m.col; j++) {
for (int k = 0; k < m.row; k++) {
result[i][j] += matrix[i][k] * m(k,j);
}
}
}
displayResultMatrix(row, m.col);
} else {
cout << "Cannot complete matrix multiplication with these matrices.\n";
}
}
// Transpose matrix.
void transpose() {
int temp = row;
row = col;
col = temp;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] = matrix[j][i];
}
}
displayResultMatrix(row, col);
}
// Swaps two rows.
void swapRows(int row1, int row2) {
if (row1 < row && row2 < row) {
int temp[1][col];
for (int i = 0; i < col; i++) {
temp[0][i] = matrix[row1][i];
matrix[row1][i] = matrix[row2][i];
matrix[row2][i] = temp[0][i];
}
} else {
cout << "Could not access specified row to swap.\n";
}
}
// Compute the reduced row-echelon form of a matrix.
void matrixRREF() {
int lead = 0;
for (int r = 0; r < row; r++) {
if (lead >= col) {
return;
}
int i = r;
while (matrix[i][lead] == 0) {
i += 1;
if (i == row) {
i = r;
lead += 1;
if (col == lead) {
return;
}
}
}
swapRows(i, r);
int val = matrix[r][lead];
if (matrix[r][lead] != 0) {
for (int j = 0; j < col; j++) {
matrix[r][j] /= val;
}
}
for (int i = 0; i < row; i++) {
if (i != r) {
int val = matrix[i][lead];
for (int j = 0; j < col; j++) {
matrix[i][j] -= (val * matrix[r][j]);
}
}
}
lead += 1;
}
displayMatrix();
}
// TODO: Finds the determinant of a matrix.
int determinantFinder() {
if (col == row) {
int determinant;
if (row == 1) {
determinant = matrix[0][0];
} else if (row == 2) {
determinant = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);
} else {
for (int p = 0; p < row; p++) {
int h = 0,
k = 0;
for (int i = 1; i < row; i ++) {
for (int j = 0; j < n; j++) {
if (j == p) {
continue;
}
// temp[h][k] = matrix[i][j];
k++;
if (k == n - 1) {
h++;
k = 0;
}
}
}
//determinant = determinant + matrix[0][p] * pow(-1, p) * determinantFinder(temp, n -1);
}
//return determinant;
}
cout << "The determinant of this matrix is: " << determinant << endl;
} else {
cout << "Cannot find the determinant of this matrix. (Must be nxn)\n";
}
}
// TODO: Inverses a matrix.
void inverseMatrix() {
if (col == row) {
//TODO
} else {
cout << "Cannot invert this matrix. (Must be nxn)\n";
}
}
};
int main () {
cout << "Welcome to Matrix Calculator V1.1 By Baraa Hamodi!\n\n"
<< "Start by making your first matrix!\n";
Matrix a;
a.getInput();
int choice;
string proceed, remakeMatrix;
cout << "Now that you have this matrix:\n";
a.displayMatrix();
cout << "What would you like to do?\n"
<< "Enter 1 for matrix scalar multiplication.\n"
<< "Enter 2 for matrix addition.\n"
<< "Enter 3 for matrix multiplication.\n"
<< "Enter 4 to find the matrix determinant.\n"
<< "Enter 5 to transpose the matrix.\n"
<< "Enter 6 to find the inverse of your matrix.\n"
<< "Enter 7 to put your matrix in RREF.\n";
do {
cout << "Please enter your operation choice: ";
cin >> choice;
if (choice == 1) {
a.scalarMultiplication();
} else if (choice == 2) {
cout << "This will require a second matrix, please create one now.\n";
Matrix b;
b.getInput();
a.matrixAddition(b);
} else if (choice == 3) {
cout << "This will require a second matrix, please create one now.\n";
Matrix b;
b.getInput();
a.matrixMultiplication(b);
} else if (choice == 4) {
a.determinantFinder();
} else if (choice == 5) {
a.transpose();
} else if (choice == 6) {
a.inverseMatrix();
} else if (choice == 7) {
a.matrixRREF();
}
cout << "Continue? (Enter yes or no) ";
cin >> proceed;
transform(proceed.begin(), proceed.end(), proceed.begin(), ::tolower);
if (proceed == "yes") {
cout << "Now enter yes to create a new matrix or no to use current matrix.\n";
cin >> remakeMatrix;
transform(remakeMatrix.begin(), remakeMatrix.end(), remakeMatrix.begin(), ::tolower);
if (remakeMatrix == "yes") {
a.getInput();
}
}
} while(proceed == "yes");
system("PAUSE");
return EXIT_SUCCESS;
}
/*******************************************************************************
* Copyright(c) 2014-Present, Baraa Hamodi. Students, staff, and faculty *
* members at the University of Waterloo are granted a non-exclusive right to *
* copy, modify, or use this software for non-commercial teaching, learning, *
* and research purposes provided the author is acknowledged. *
*******************************************************************************/