-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixOps.cpp
More file actions
157 lines (128 loc) · 3.82 KB
/
Copy pathmatrixOps.cpp
File metadata and controls
157 lines (128 loc) · 3.82 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
#include "matrixOps.h"
// Determines if a value lies on an closed interval
bool between(double lo, double hi, double x){
bool ans = false;
if (x >= lo && x <= hi){
ans = true;
}
return ans;
}
// Prints a matrix to the console
void displayMat(std::vector<std::vector<double>> A){
for (int i = 0; (double)i < A.size(); i++){
for (int j = 0; (double)j < A[i].size(); j++){
std::cout << A[i][j] << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
// Prints the first 5 rows and 10 columns of a matrix to the console
void headMat(std::vector<std::vector<double>> A){
for (int i = 0; i < std::min(5,(int)A.size()); i++){
for (int j = 0; j < std::min(10,(int)A[i].size()); j++){
std::cout << A[i][j] << " ";
}
std::cout << "\n";
}
std::cout << "\n";
}
// Prints the size of a matrix to the console as rows x cols
void displaySize(std::vector<std::vector<double>> A){
std::cout << A.size() << " x " << A[0].size() << std::endl;
std::cout << "\n";
}
// Returns the size of a matrix to the calling program as {rows, cols}
std::vector<int> getSize(std::vector<std::vector<double>> A){
// int rows = A.size();
// int cols = A[0].size();
int rows = A.size();
int cols = A[0].size();
std::vector<int> size(2);
size[0] = rows;
size[1] = cols;
return size;
};
// Performs matrix multiplication on A and B
std::vector<std::vector<double>> multiply(std::vector<std::vector<double>> A, std::vector<std::vector<double>> B){
std::vector<int> sizeA = getSize(A);
std::vector<int> sizeB = getSize(B);
if (sizeA[1] != sizeB[0]){
std::cout << "Matrix size mismatch." << std::endl;
return {};
}
int rows = sizeA[0];
int cols = sizeB[1];
int inner = sizeA[1];
std::vector<std::vector<double>> result;
double num;
for (int i = 0; i < rows; i++){
std::vector<double> row;
for (int j = 0; j < cols; j++){
num = 0;
for (int k = 0; k < inner; k++){
num += A[i][k]*B[k][j];
}
row.push_back(num);
}
result.push_back(row);
delete &row;
}
return result;
}
// Performs matrix tranpose on A
std::vector<std::vector<double>> transpose(std::vector<std::vector<double>> A){
std::vector<int> size = getSize(A);
std::vector<std::vector<double>> result;
std::vector<double> row;
for (int j = 0; j < size[1]; j++){
row = {};
for (int i = 0; i < size[0]; i++){
row.push_back(A[i][j]);
}
result.push_back(row);
}
return result;
}
// Creates a submatrix of A
std::vector<std::vector<double>> submatrix(std::vector<std::vector<double>> A, std::vector<int> rows, std::vector<int> cols){
int maxRows = getSize(A)[0];
int maxCols = getSize(A)[1];
int rowBegin = 0, rowEnd = maxRows, colBegin = 0, colEnd = maxCols;
if (!between(0,2,rows.size())){
std::cout << "'rows' may only contain 0, 1, or 2 elements." << std::endl;
} else if (rows[0] >= rows[1]){
std::cout << "rows[0] must be less than rows[1]" << std::endl;
}
if (rows.size()==1 && between(1, maxRows - 1, rows[0])){
rowBegin = rows[0];
rowEnd = rows[0] + 1;
}
if (rows.size()==2 && between(1, maxRows - 1, rows[1])){
rowEnd = rows[1];
}
if (!between(0,2,cols.size())){
std::cout << "'cols' may only contain 0, 1, or 2 elements." << std::endl;
} else if (cols[0] >= cols[1]){
std::cout << "cols[0] must be less than cols[1]" << std::endl;
}
if (cols.size()==1 && between(1, maxCols, cols[0])){
colBegin = cols[0];
colEnd = cols[0] + 1;
}
if (cols.size()==2 && between(0, maxCols, cols[1])){
colEnd = cols[1];
}
std::vector<std::vector<double>> result;
std::vector<double> row;
double num;
for (int i = rowBegin; i < rowEnd; i++){
row = {};
for (int j = colBegin; j < colEnd; j++){
num = A[i][j];
row.push_back(num);
}
result.push_back(row);
}
return result;
}