-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.hpp
254 lines (208 loc) · 4.58 KB
/
matrix.hpp
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
/**
@author Elliott Kopp
@file matrix.hpp
@date 3/6/2011
@brief The implementation of the Matrix class
*/
template<class T>
Matrix<T>::Matrix()
{
m_dimension = 0;
}
template<class T>
Matrix<T>::Matrix(const Matrix<T> &source)
{
int size = source.getDimension();
m_dimension = size;
m_data.setSize(size);
for(int i = 0; i < size; ++i)
{
m_data[i].setSize(size);
}
// Perform deep copy
for(int i = 0; i < size; ++i)
{
m_data[i] = source[i];
}
}
template<class T>
Matrix<T>::Matrix(int dimension)
{
if(dimension < 0)
{
throw Exception("Dimension must be greater than zero");
}
m_dimension = dimension;
m_data.setSize(dimension);
for(int i = 0; i < dimension; ++i)
{
m_data[i].setSize(dimension);
}
}
template<class T>
int Matrix<T>::getDimension() const
{
return m_dimension;
}
template<class T>
void Matrix<T>::setDimension(int dimension)
{
if(dimension < 0)
{
throw Exception("Dimension must be greater than zero");
}
m_dimension = dimension;
m_data.setSize(dimension);
for(int i = 0; i < m_dimension; ++i)
{
m_data[i].setSize(m_dimension);
}
return;
}
template<class T>
NVector<T> &Matrix<T>::operator[](int i) const
{
if(i < 0 || i >= m_dimension)
{
throw Exception("Index out of bounds");
}
return m_data[i];
}
template<class T>
Matrix<T> Matrix<T>::operator~() const
{
Matrix<T> transpose(m_dimension);
for(int i = 0; i < m_dimension; ++i)
{
for(int j = 0; j < m_dimension; ++j)
{
transpose[i][j] = m_data[j][i];
}
}
return transpose;
}
template<class T>
Matrix<T> Matrix<T>::operator+(const Matrix<T> &rhs) const
{
Matrix<T> temp(rhs.getDimension());
if(rhs.getDimension() != m_dimension)
{
throw Exception("The matricies must be the same dimension for "
"addition");
}
for(int i = 0; i < m_dimension; ++i)
{
for(int j = 0; j < m_dimension; ++j)
{
temp[i][j] = m_data[i][j] + rhs[i][j];
}
}
return temp;
}
template<class T>
Matrix<T> Matrix<T>::operator-(const Matrix<T> &rhs) const
{
if(m_dimension != rhs.getDimension())
{
throw Exception("The matricies must be the same dimension for "
"subtraction");
}
return (*this + -rhs); // Clever no?
}
template<class T>
Matrix<T> Matrix<T>::operator*(const Matrix<T> &rhs) const
{
Matrix<T> temp(rhs.getDimension());
if(m_dimension != rhs.getDimension())
{
throw Exception("The matricies must be the same dimension for "
"multiplication");
}
for(int i = 0; i < m_dimension; ++i)
{
for(int j = 0; j < m_dimension; ++j)
{
for(int k = 0; k < m_dimension; ++k)
{
temp[i][j] += m_data[i][k]*rhs[k][j];
}
}
}
return temp;
}
template<class T>
Matrix<T> Matrix<T>::operator*(const int &rhs) const
{
Matrix<T> temp(m_dimension);
for(int i = 0; i < m_dimension; ++i)
{
for(int j = 0; j < m_dimension; ++j)
{
temp[i][j] = rhs*m_data[i][j];
}
}
return temp;
}
template<class T>
NVector<T> Matrix<T>::operator*(const NVector<T> &rhs) const
{
NVector<T> temp(m_dimension);
if(m_dimension != rhs.getSize())
{
throw Exception("The matrix and vector must be the same dimension for "
"multiplication");
}
for(int i = 0; i < m_dimension; ++i)
{
for(int j = 0; j < m_dimension; ++j)
{
temp[i] += m_data[j][i]*rhs[j];
}
}
return temp;
}
template<class T>
Matrix<T> Matrix<T>::operator-() const
{
return (*this * -1);
}
template<class T>
ostream & operator<<(ostream &os, Matrix<T> const &obj)
{
for(int i = 0; i < obj.m_dimension; ++i)
{
os << obj.m_data[i] << "\n";
}
os << "\n";
return os;
}
template<class T>
istream & operator>>(istream &is, Matrix<T> &obj)
{
double temp = 0;
for(int i = 0; i < obj.m_dimension; ++i)
{
for(int j = 0; j < obj.m_dimension; ++j)
{
is >> temp;
obj.m_data[i][j] = static_cast<T>(temp);
}
}
return is;
}
template<class T>
Matrix<T> operator*(const int &lhs, const Matrix<T> &rhs)
{
int dimension = rhs.getDimension();
Matrix<T> temp(dimension);
// Could use scalar*vector here, but indexing over each element manually
// cuts down on function calls.
for(int i = 0; i < dimension; ++i)
{
for(int j = 0; j < dimension; ++j)
{
temp[i][j] = lhs*rhs[i][j];
}
}
return temp;
}