-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
254 lines (214 loc) · 6.1 KB
/
matrix.cpp
File metadata and controls
254 lines (214 loc) · 6.1 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
//A matrix class for use with calculations
#include "matrix.h"
#include <iostream>
#include <stdlib.h>
#define FILLING 10 //the horizontal filling for pretty printing the matrix
/* Overrides the != operrator
*/
bool Matrix::operator!= (Matrix param) {
if (rows != param.rows || cols != param.cols) return true;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (mat[i][j] != param[i][j]) return true;
}
}
return false;
}
/* Overrides the == operater enabling comparing of matrices
*/
bool Matrix::operator== (Matrix param) {
if (rows != param.rows || cols != param.cols) return false;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (mat[i][j] != param[i][j]) return false;
}
}
return true;
}
/* Overrides the equals operator enabling the equating of matrices
*/
void Matrix::operator= (Matrix param) {
rows = param.rows;
cols = param.cols;
mat = param.mat;
}
/* Overrides the *= operator performing matrix scalar multiplication
*/
void Matrix::operator*= (double param) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
mat[i][j] = mat[i][j] * param;
}
}
}
/* Overrides the *= operator performing matrix dot product
*/
void Matrix::operator*= (Matrix param) {
if (rows != param.cols || cols != param.rows) {
cerr << "Invalid Matrix dimensions in dot product\n";
cerr << "Rows of LHM must equal Columns of RHM\n";
cerr << "and Columns of LHM must equal Rows of RHM\n";
exit(EXIT_FAILURE);
}
Matrix temp(rows, param.cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < param.cols; ++j) {
double sum = 0;
for (int k = 0; k < cols; ++k) {
sum += mat[i][k] * param[k][j];
}
temp.insert(i, j, sum);
}
}
cols = param.cols;
mat = temp.mat;
}
/* Overrides the * operator performing matrix times scalar
*/
Matrix Matrix::operator* (double param) {
Matrix temp(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
temp.insert(i, j, mat[i][j] * param);
}
}
return temp;
}
/* Overrides the * operator performing matrix dot product
*/
Matrix Matrix::operator* (Matrix param) {
if (rows != param.cols || cols != param.rows) {
cerr << "Invalid Matrix dimensions in dot product\n";
cerr << "Rows of LHM must equal Columns of RHM\n";
cerr << "and Columns of LHM must equal Rows of RHM\n";
exit(EXIT_FAILURE);
}
Matrix temp(rows, param.cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < param.cols; ++j) {
double sum = 0;
for (int k = 0; k < cols; ++k) {
sum += mat[i][k] * param[k][j];
}
temp.insert(i, j, sum);
}
}
return temp;
}
/* Overrides the -= operator performing matrix addition
*/
void Matrix::operator-= (Matrix param) {
if (rows != param.rows || cols != param.cols) {
cerr << "Matrix Dimensions must be the same for subtraction\n";
exit(EXIT_FAILURE);
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
mat[i][j] = mat[i][j] - param[i][j];
}
}
}
/* Overrides the - operator performing matrix addition
*/
Matrix Matrix::operator- (Matrix param) {
if (rows != param.rows || cols != param.cols) {
cerr << "Matrix Dimensions must be the same for subtraction\n";
exit(EXIT_FAILURE);
}
Matrix temp(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
temp.insert(i, j, mat[i][j] - param[i][j]);
}
}
return temp;
}
/* Overrides the += operator performing matrix addition
*/
void Matrix::operator+= (Matrix param) {
if (rows != param.rows || cols != param.cols) {
cerr << "Matrix Dimensions must be the same for addition\n";
exit(EXIT_FAILURE);
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
mat[i][j] = mat[i][j] + param[i][j];
}
}
}
/* Overrides the + operator performing matrix addition
*/
Matrix Matrix::operator+ (Matrix param) {
if (rows != param.rows || cols != param.cols) {
cerr << "Matrix Dimensions must be the same for addition\n";
exit(EXIT_FAILURE);
}
Matrix temp(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
temp.insert(i, j, mat[i][j] + param[i][j]);
}
}
return temp;
}
/* Overrides the indexing operator
*/
vector<double> Matrix::operator[] (int param) {
return mat[param];
}
/* Matrix Class constructor
*/
Matrix::Matrix (int num_rows, int num_cols) {
rows = num_rows; cols = num_cols;
for (int i = 0; i < rows; ++i) {
vector<double> row; //new empty row
for (int j = 0; j < cols; ++j) {
row.push_back(0); // add zero for every column
}
mat.push_back(row); // add row into matrix
}
}
/* inserts value into the row, col coordinate.
*/
void Matrix::insert(int row, int col, double value) {
if (row >= rows || col >= cols) {
cerr << "Invalid row or column in insert\n";
exit(EXIT_FAILURE);
}
mat[row][col] = value;
}
/* Pretty Prints the matix with horizontal filling - beware large matrices
* can end up off screen.
*/
void Matrix::display(void) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout.width(FILLING);
cout << mat[i][j];
}
cout << '\n';
}
}
/*
int main(int argc, char ** argv) {
Matrix m (4, 4);
m.insert(2,2,2);
m.insert(2,1,2);
m.insert(1, 2, 2);
m.insert(1,1,1);
Matrix n(4, 4);
n.insert(2,2,2);
n.insert(2,1,2);
n.insert(1,1,1);
n.insert(1,2,2);
Matrix d(4,4);
//m*=10;
//d = m * 10;
cout << (m != n) << '\n';
m.display();
cout << '\n';
n.display();
cout << '\n';
d.display();
}
*/