This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
executable file
·400 lines (332 loc) · 10.9 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
#include "Matrix.h"
#include <math.h>
#include "MatrixBuilder.h"
using namespace std;
// -------------------- //
// --- Constructors --- //
// -------------------- //
// Default constructor sets the fields to their default states
Matrix::Matrix() {
setFields();
name = UNAMED;
}
// Alternate constructor sets the number of rows and columns and builds a
// matrix of appropriate size.
// @param int r - the number of rows
// @param int c - the number of columns
Matrix::Matrix(int r, int c) {
setFields(r, c);
name = UNAMED;
}
// Alternate constructor sets the number of rows and columns and fills an
// appropriately sized matrix with the specified value
// @param int r - the number of rows
// @param int c - the number of columns
// @param double value - set each element in the matrix to this value
Matrix::Matrix(int r, int c, double value) {
setFields(r, c, value);
name = UNAMED;
}
// Alternate constructor converts the input vector of vectors of doubles to a
// Matrix.
// @param const vector<vector<double>>& d - this Matrix's data
Matrix::Matrix(const vector<vector<double>>& d) {
if (!checkCols(d)) {
throw "ERROR: "
"Matrix::Matrix(const vector<vector<double>>&)\n"
"\tInconsistent number of columns in input argument.";
}
setFields(d);
name = UNAMED;
}
// Copy constructor copies the input Matrix.
// @param const Matrix& mat - Matrix from which to copy
Matrix::Matrix(const Matrix& mat) {
setFields(mat);
name = UNAMED;
}
// Copy constructor copies the input SubMatrix
// @param SubMatrix& sm - input SubMatrix from which to copy
Matrix::Matrix(SubMatrix& sm) {
setFields(sm);
name = UNAMED;
delete &sm;
}
// ------------------ //
// --- Destructor --- //
// ------------------ //
// Destructor sets all fields to their default states and removes the name
Matrix::~Matrix() {
deleteFields();
name = "";
}
// ---------------------------- //
// --- Assignment Operators --- //
// ---------------------------- //
// Assignment operator blows out the Matrix and replace it with the input vector
// @param const vector<vector<double>>& d - vector from which to copy
// @return Matrix& - this Matrix
Matrix& Matrix::operator=(const vector<vector<double>>& d) {
if (!checkCols(d)) {
throw "ERROR: "
"Matrix& Matrix::operator=(const vector<vector<double>>&)\n"
"\tInconsistent number of columns in input argument.";
}
deleteFields();
setFields(d);
return *this;
}
// Assignemnt operator blows out the Matrix and sets it to the RHS
// @param const Matrix& mat - Matrix from which to copy
// @return Matrix& - this Matrix
Matrix& Matrix::operator=(const Matrix& mat) {
if (this != &mat) {
deleteFields();
setFields(mat);
}
return *this;
}
// Assignemnt operator blows out the Matrix and sets it to the RHS
// @param SubMatrix& sm - SubMatrix frrom which to copy
// @return Matrix& - this Matrix
Matrix& Matrix::operator=(SubMatrix& sm) {
deleteFields();
setFields(sm);
delete &sm;
return *this;
}
// ---------------------------- //
// --- Accessors & Mutators --- //
// ---------------------------- //
// -- Matrix Name --- //
// Get the name of the Matrix
// @return const string& - the Matrix's name
const string& Matrix::getName() const {
return name;
}
// Set the name of the Matrix
// @param const string& n - the Matrix's name
void Matrix::setName(const string& n) {
name = n;
}
// ------------------------------- //
// --- Functions and Operators --- //
// ------------------------------- //
// --- Element support --- //
// Extract the value at the specified index
// @param int r - the row index
// @param int c - the column index
// @return double& - the value at this index
double& Matrix::operator()(int r, int c) {
if (r >= nRows || c >= nCols || r < 0 || c < 0) {
throw "ERROR: "
"double& Matrix::operator()(int, int)\n"
"\tAttempting to access elements outside the matrix range.";
}
return data[r][c];
}
const double& Matrix::operator()(int r, int c) const {
if (r >= nRows || c >= nCols || r < 0 || c < 0) {
throw "ERROR: "
"double& Matrix::operator()(int, int)\n"
"\tAttempting to access elements outside the matrix range.";
}
return data[r][c];
}
// Set the entire Matrix equal to the input value
// @param double value - the value to insert
void Matrix::operator=(double value) {
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
data[i][j] = value;
}
}
}
// --- SubMatrix Support --- //
// Generate a SubMatrix from this Matrix using the indicies
// @param int top - top index
// @param int down - bottom index
// @param int left - left index
// @param int right - right index
// @return SubMatrix& - the SubMatrix
SubMatrix& Matrix::operator()(int top, int down, int left, int right) {
if (down < top || right < left) {
throw "ERROR: "
"SubMatrix& Matrix::operator()(int, int, int, int)\n"
"\tUnordered Range.";
}
if (top < 0 || down >= nRows || left < 0 || right >= nCols) {
throw "ERROR: "
"SubMatrix& Matrix::operator()(int, int, int, int)\n"
"\tAttempting to access elements outside the Matrix range.";
}
return *(new SubMatrix(this, top, down, left, right));
}
// --- Query Support --- //
// Return the length of the greater dimension
int Matrix::length() const {
return nRows > nCols ? nRows : nCols;
}
// Return the size of the specified dimension.
// @param int dim - if 1, then return nRows
// - if 2, then return nCols
// - else, return the larger length
// @return int - the length
int Matrix::size(int dim) const {
if (dim == 1) {
return nRows;
} else if (dim == 2) {
return nCols;
} else {
return length();
}
}
// --- Mathematical Operations Support --- //
// Construct a Matix by multipliying this Matrix with another. This lives here in order to
// overload the multiplication operator, but all the work is offloaded to MatrixBuilder.
// @param const Matrix& RHS - the RHS Matix involved in the operation
// @return Matrix& - new Matrix formed from multiplying these two.
Matrix& Matrix::operator*(const Matrix& RHS) const {
return MatrixBuilder::BuildMatrixFromMultiplication(*this, RHS);
}
// Construct a Matix by multipliying this Matrix with a SubMatrix. This lives here in order to
// overload the multiplication operator, but all the work is offloaded to MatrixBuilder.
// @param SubMatrix& RHS - the RHS SubMatix involved in the operation
// @return Matrix& - new Matrix formed from multiplying these two.
Matrix& Matrix::operator*(SubMatrix& RHS) const {
Matrix& outMatrix = MatrixBuilder::BuildMatrixFromMultiplication(*this, RHS);
delete &RHS;
return outMatrix;
}
Matrix& Matrix::operator+(const Matrix& RHS) const {
return MatrixBuilder::BuildMatrixFromAddition(*this, RHS);
}
Matrix& Matrix::operator-(const Matrix& RHS) const {
return MatrixBuilder::BuildMatrixFromSubtraction(*this, RHS);
}
Matrix& Matrix::operator*(double RHS) const {
return MatrixBuilder::BuildMatrixFromMultiplication(*this, RHS);
}
// Calculate the vector 2-norm of this Matrix.
// @return double - the vector 2-norm of this Matrix.
// throws an error if the Matrix is not a vector
double Matrix::norm() const {
if (nRows != 1 && nCols != 1) {
throw "ERROR: "
"double Matrix::norm() const\n"
"You are asking for the norm of a matrix, but we only support the 2-norm of a vector.";
} else if (nRows == 1 && nCols == 1) {
return data[0][0];
} else if (nRows == 1 && nCols != 1) {
double norm2 = 0.0;
for (int n = 0; n < nCols; ++n) {
norm2 += pow(data[0][n], 2);
}
return sqrt(norm2);
} else { // nRows != 1 && nCols == 1
double norm2 = 0.0;
for (int n = 0; n < nRows; ++n) {
norm2 += pow(data[n][0], 2);
}
return sqrt(norm2);
}
}
// ---------------- //
// --- Printing --- //
// ---------------- //
// Print to an ostream
// @param ostream& streamer - print to this ostream
void Matrix::Print(ostream& streamer) const {
streamer << endl << name << endl;
streamer << "\tRows: " << nRows << endl;
streamer << "\tCols: " << nCols << endl;
for (vector<vector<double>>::const_iterator
i = data.begin(); i != data.end(); ++i) {
streamer << "\t\t[ ";
for (vector<double>::const_iterator
j = i->begin(); j != i->end(); ++j) {
streamer << *j << " ";
}
streamer << "]" << endl;
}
}
ostream& operator<<(ostream& streamer, const Matrix& mat) {
mat.Print(streamer);
return streamer;
}
// Print to an fstream
// @param ofstream& fileOut - print to this ofstream
void Matrix::Print(ofstream& fileOut) const {
fileOut << endl << name << endl;
fileOut << "\tRows: " << nRows << endl;
fileOut << "\tCols: " << nCols << endl;
for (vector<vector<double>>::const_iterator
i = data.begin(); i != data.end(); ++i) {
fileOut << "\t\t[ ";
for (vector<double>::const_iterator
j = i->begin(); j != i->end(); ++j) {
fileOut << *j << " ";
}
fileOut << "]" << endl;
}
}
ofstream& operator<<(ofstream& fileOut, const Matrix& mat) {
mat.Print(fileOut);
return fileOut;
}
// ------------------------ //
// --- Helper Functions --- //
// ------------------------ //
// --- Object Construction Helpers --- //
// Set the class fields
// @param int r - the number of rows
// @param int c - the number of columns
// @param double value - set each element in the matrix to this value
void Matrix::setFields(int r, int c, double value) {
nRows = r;
nCols = c;
data = vector<vector<double>>(r, vector<double>(c, value));
}
// Set the class fields
// @param const vector<vector<double>>& d - the input vector from which to copy
void Matrix::setFields(const vector<vector<double>>& d) {
data = d;
nRows = data.size();
nCols = data[0].size();
}
// Set the class fields
// @param const Matrix& mat - the input Matrix from which to copy
void Matrix::setFields(const Matrix& mat) {
data = mat.data;
nRows = data.size();
nCols = data[0].size();
}
// Set the class fields
// @param const SubMatrix& sm - the input SubMatrix from which to copy
void Matrix::setFields(SubMatrix& sm) {
nRows = sm.nRows;
nCols = sm.nCols;
data = vector<vector<double>>(sm.nRows, vector<double>(sm.nCols));
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
data[i][j] = sm(i, j);
}
}
}
// Delete the class fields
void Matrix::deleteFields() {
data.clear();
nRows = nCols = 0;
}
// Check that the number of elements in each vector is the same
// @param const vector<vector<double>>& - input vector of vectors
// @return bool - true if they're all the same, false otherwise.
bool Matrix::checkCols(const vector<vector<double>>& d) {
int checkNumberOfColumns = d[0].size();
for (int i = 1; i < d.size(); ++i) {
if (d[i].size() != checkNumberOfColumns) {
return false;
}
}
return true;
}