-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
136 lines (125 loc) · 3.74 KB
/
Copy pathmatrix.cpp
File metadata and controls
136 lines (125 loc) · 3.74 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
#include "matrix.h"
#include <string>
#include <sstream>
using namespace std;
Matrix::Matrix() {
buildMatrix();
}
Matrix::Matrix(vector<vector<double>> entries) {
data = entries;
rows = data.size();
if (data.size()) {
cols = data[0].size();
} else {
cols = 0;
}
}
Matrix Matrix::operator+=(const Matrix m) {
return *this + m;
}
Matrix Matrix::operator*=(const double constant) {
for (auto rowIt = data.begin(); rowIt != data.end(); rowIt++) {
for (auto colIt = rowIt->begin(); colIt != rowIt->end(); colIt++) {
*colIt *= constant;
}
}
return *this;
}
vector<double>& Matrix::operator[](int i) {
return data[i];
}
ostream& operator<<(ostream& os, const Matrix& m) {
for (auto itRow = m.data.begin(); itRow != m.data.end(); itRow++) {
os << "[";
for (auto itCol = itRow->begin(); itCol != itRow->end(); itCol++) {
os << *itCol;
if (itCol != itRow->end() - 1) {
os << " ";
}
}
os << "]" << endl;
}
return os;
}
Matrix operator+(Matrix l, Matrix r) {
if (!l.sameSize(r)) {
cout << "ERROR: can only add matrices with the same dimensions" << endl;
return l;
}
vector<vector<double>> data;
for (int i = 0; i < l.numRows(); i++) {
vector<double> row;
for (int j = 0; j < l.numCols(); j++) {
row.push_back(l[i][j] + r[i][j]);
}
data.push_back(row);
}
return Matrix(data);
}
Matrix operator*(Matrix l, Matrix r) {
if (!l.oppositeSize(r)) {
cout << "ERROR: can only multiply matrices with opposite dimensions" << endl;
return l;
}
vector<vector<double>> data;
for (int i = 0; i < l.numRows(); i++) {
vector<double> row;
for (int j = 0; j < r.numCols(); j++) {
int dotProduct = 0;
for (int comp = 0; comp < l.numCols(); comp++) {
dotProduct += (l[i][comp] * r[comp][j]);
}
row.push_back(dotProduct);
}
data.push_back(row);
}
return Matrix(data);
}
void Matrix::buildMatrix() {
cout << "Please enter the entries of the matrix row by row." << endl;
cout << "Remember to separate each entry with a space, and press enter to move onto the next row." << endl;
cout << "When you're finished, enter [d]." << endl;
string line;
int numCols;
while (1) {
while (2) {
int noError = 1;
cout << "Entering row " << data.size() + 1 << ": ";
getline(cin, line);
if (line.compare("d") == 0) {
break;
}
vector<double> row;
stringstream ss(line);
while (!ss.eof()){
string token;
ss >> token;
double num;
if (stringstream(token) >> num) {
row.push_back(num);
} else {
cout << "Oops! Entry '" << token << "' is invalid! (Only decimal values are accepted)" << endl;
noError = 0;
}
}
if (data.size() == 0) {
numCols = row.size();
}
if (row.size() != numCols && noError) {
cout << "Oops! All rows must be length " << numCols << endl;
} else if (noError) {
data.push_back(row);
}
}
cout << "Is this the correct matrix?" << endl;
cout << "Press [n] to re-enter the matrix, or anything to continue" << endl;
cout << *this;
getline(cin, line);
if (line.compare("n") != 0) {
break;
}
data.clear();
}
rows = data.size();
cols = numCols;
}