This repository was 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
Expand file tree
/
Copy pathSubMatrix.cpp
More file actions
executable file
·188 lines (152 loc) · 4.89 KB
/
SubMatrix.cpp
File metadata and controls
executable file
·188 lines (152 loc) · 4.89 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
#include "SubMatrix.h"
#include "MatrixBuilder.h"
using namespace std;
// -------------------- //
// --- Constructors --- //
// -------------------- //
// Constructor sets the data and the limits
// @param Matrix* mat - the pointer to the Matrix data
// @param int _top - top index
// @param int _down - bottom index
// @param int _left - left index
// @param int _right - right index
SubMatrix::SubMatrix(Matrix* mat, int _top, int _down, int _left, int _right) {
data = mat;
top = _top;
down = _down+1;
left = _left;
right = _right+1;
nRows = down - top;
nCols = right - left;
}
// ------------------ //
// --- Destructor --- //
// ------------------ //
// Destructor sets all to 0
SubMatrix::~SubMatrix() {
data = 0;
top = down = left = right = nRows = nCols = 0;
}
// ------------------------------- //
// --- Functions and Operators --- //
// ------------------------------- //
// Set the values to the values from the input Matrix
// @param const Matrix& mat - the input Matrix from which to copy
void SubMatrix::operator=(const Matrix& mat) {
if (nRows != mat.size(1) || nCols != mat.size(2)) {
throw "ERROR: "
"void SubMatrix::operator=(const Matrix&)\n"
"\tMatrices are not the same size.";
}
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
this->operator()(i, j) = mat(i, j);
}
}
delete this;
}
// Set the values to the values from the input SubMatrix
// @param const SubMatrix& sm - the input SubMatrix from which to copy
void SubMatrix::operator=(SubMatrix& sm) {
if (nRows != sm.nRows || nCols != sm.nCols) {
throw "ERROR: "
"void SubMatrix::operator=(SubMatrix&)\n"
"\tMatrices are not the same size.";
}
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
this->operator()(i, j) = sm(i, j);
}
}
delete &sm;
delete this;
}
// Set the values to the input double
// @param double - set values to this double
void SubMatrix::operator=(double d) {
for (int i = 0; i < nRows; ++i) {
for (int j = 0; j < nCols; ++j) {
this->operator()(i, j) = d;
}
}
delete this;
}
// Extract the value at the specified index of the SubMatrix.
// @param int i - the ith row
// @param int j - the jth row
// @return double& - reference to the double at the (i,j)th position of the SubMatrix.
double& SubMatrix::operator()(int i, int j) {
return data->operator()(i + top, j + left);
}
const double& SubMatrix::operator()(int i, int j) const {
return data->operator()(i + top, j + left);
}
// 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 SubMatrix::size(int dim) const {
if (dim == 1) {
return nRows;
} else if (dim == 2) {
return nCols;
} else {
return nRows > nCols ? nRows : nCols;
}
}
// --- Mathematical Operations Support --- //
// Construct a Matix by multipliying this SubMatrix with a Matrix. 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& SubMatrix::operator*(const Matrix& RHS) {
Matrix& outMatrix = MatrixBuilder::BuildMatrixFromMultiplication(*this, RHS);
delete this;
return outMatrix;
}
// Construct a Matix by multipliying this SubMatrix with another. 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& SubMatrix::operator*(SubMatrix& RHS) {
Matrix& outMatrix = MatrixBuilder::BuildMatrixFromMultiplication(*this, RHS);
delete &RHS;
delete this;
return outMatrix;
}
// ---------------- //
// --- Printing --- //
// ---------------- //
// Print to an ostream
// @param ostream& streamer - print to this ostream
void SubMatrix::Print(ostream& streamer) const {
for (int i = top; i < down; ++i) {
streamer << "\t\t[ ";
for (int j = left; j < right; ++j) {
streamer << data->operator()(i,j) << endl;
}
streamer << "]" << endl;
}
delete this;
}
ostream& operator<<(ostream& streamer, const SubMatrix& sm) {
sm.Print(streamer);
return streamer;
}
// Print to an fstream
// @param ofstream& fileOut - print to this ofstream
void SubMatrix::Print(fstream& outFile) const {
for (int i = top; i < down; ++i) {
outFile << "\t\t[ ";
for (int j = left; j < right; ++j) {
outFile << data->operator()(i,j) << endl;
}
outFile << "]" << endl;
}
delete this;
}
ofstream& operator<<(ofstream& outFile, const SubMatrix& sm) {
sm.Print(outFile);
return outFile;
}