-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.hxx
More file actions
260 lines (225 loc) · 5.93 KB
/
Matrix.hxx
File metadata and controls
260 lines (225 loc) · 5.93 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
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
#include "Matrix.hpp"
#include "Vector.hpp"
#include <cassert>
template<typename T>
Matrix<T>::Matrix()
{
data_ = nullptr;
num_rows_ = 0;
num_cols_ = 0;
}
template<typename T>
Matrix<T>::Matrix(size_t num_rows, size_t num_cols)
{
// Ensure proper dimensions
assert(num_rows > 0);
assert(num_cols > 0);
// Initialize matrix with specified dimensions and all entries 0
num_rows_ = num_rows;
num_cols_ = num_cols;
data_ = new T*[num_rows];
for (int i=0; i < num_rows; i++)
{
data_[i] = new T[num_cols];
for (int j=0; j < num_cols; j++)
{
data_[i][j] = T(); // Call default constructor for objects of type T
}
}
}
/* template<typename T>
Matrix<T>::Matrix(const T** data, size_t num_rows, size_t num_cols)
{
// Passing in 2D array, and dimensions
// Check dimensions are not empty
assert(num_rows > 0);
assert(num_cols > 0);
num_rows_ = num_rows;
num_cols_ = num_cols;
// Initialize new matrix
data_ = new T*[num_rows_];
// Copy over values from T** data
for (int i=0; i < num_rows_; i++)
{
data_[i] = new T[num_cols_];
for (int j=0; j < num_cols_; j++)
{
data_[i][j] = data[i][j];
}
}
} */
template<typename T>
Matrix<T>::Matrix(const Matrix& m1)
{
num_rows_ = m1.num_rows_;
num_cols_ = m1.num_cols_;
data_ = new T*[num_rows_];
for (int i=0; i < num_rows_; i++)
{
data_[i] = new T[num_cols_];
for (int j=0; j < num_cols_; j++)
{
data_[i][j] = m1.data_[i][j];
}
}
}
template<typename T>
Matrix<T>::~Matrix()
{
for (int i=0; i < num_cols_; i++) // Since data_ points to an array of pointers, need to
{ // deallocate the data they point to first, then deallocate
delete[] data_[i]; // what data_ points to
}
delete[] data_;
data_ = nullptr;
}
template<typename T>
int Matrix<T>::GetNumRows()
{
return num_rows_;
}
template<typename T>
int Matrix<T>::GetNumCols()
{
return num_cols_;
}
template<typename T>
void Matrix<T>::AppendRow(const Vector<T>& v)
{
assert(v.GetSize() == GetNumCols());
// Copy current data to use in new matrix
T** old_data = data_;
// Delete old matrix to free up memory
for(int i=0; i < num_rows_; i++)
{
delete[] data_[i];
}
delete[] data_;
// Allocate memory for new matrix with one more row
data_ = new T*[num_rows_+1];
for (int i=0; i < num_rows_; i++)
{
// Allocate new memory for each row
data_[i] = new T[num_cols_];
// Copy entries from old matrix into new one
for (int j=0; j < num_cols_; j++)
{
data_[i][j] = old_data[i][j];
}
// Clear each row in old matrix
delete[] old_data[i];
}
// Append values from vector into last row
for (int i=0; i < v.GetSize(); i++)
{
data_[num_rows_][i] = v[i];
}
delete[] old_data;
num_rows_++;
}
template<typename T>
void Matrix<T>::AppendCol(const Vector<T>& v)
{
assert(v.GetSize() == GetNumRows());
T** old_data = data_;
for (int i=0; i < num_rows_; i++)
{
// Clear ith vector in old matrix
delete[] data_[i];
// Extend rows by one
data_[i] = new T[num_cols_ + 1];
// Copy data from old matrix
for (int j=0; j < num_cols_; j++)
{
data_[i][j] = old_data[i][j];
}
// Add the ith entry of the vector into the last entry of the current row
data_[i][num_cols_] = v[i];
delete[] old_data[i];
}
delete[] old_data;
num_cols_++;
}
template<typename T>
T& Matrix<T>::operator()(int i, int j)
{
assert(i > 0);
assert(i < num_rows_ + 1);
assert(j > 0);
assert(j < num_cols_ + 1);
return data_[i-1][j-1];
}
template<typename T>
Matrix<T>& Matrix<T>::operator=(const Matrix& m1)
{
// Delete existing matrix entries first, then allocate new memory
for (int i=0; i < num_cols_; i++)
{
delete[] data_[i];
}
delete[] data_;
data_ = new T*[num_rows_];
// Create new matrix and fill with entries of m1
num_rows_ = m1.num_rows_;
num_cols_ = m1.num_cols_;
for (int i=0; i < num_rows_; i++)
{
data_[i] = new T[num_cols_];
for (int j=0; j < num_cols_; j++)
{
data_[i][j] = m1.data_[i][j];
}
}
}
template<typename T>
Matrix<T> Matrix<T>::operator+(const Matrix& m1) const
{
Matrix<T> sum = Matrix<T>(*this);
for (int i=0; i < sum.GetNumRows(); i++)
{
for (int j=0; j < sum.GetNumCols(); j++)
{
sum.data_[i][j] = sum.data_[i][j] + m1.data_[i][j];
}
}
return sum;
}
template<typename T>
Matrix<T> Matrix<T>::operator-(const Matrix& m1) const
{
Matrix<T> difference = Matrix<T>(*this);
for (int i=0; i < difference.GetNumRows(); i++)
{
difference.data_[i] = new T[num_cols_];
for (int j=0; j < difference.GetNumCols(); j++)
{
difference.data_[i][j] = difference.data_[i][j] - m1.data_[i][j];
}
}
return difference;
}
template<typename T>
Matrix<T> Matrix<T>::operator*(double scalar) const
{
for (int i=0; i < num_rows_; i++)
{
for (int j=0; j < num_cols_; j++)
{
data_[i][j] = data_[i][j] * scalar;
}
}
}
template<typename T>
Vector<T> Matrix<T>::operator*(const Vector<T>& v) const
{
assert(v.GetSize() == GetNumCols());
Vector<T> prod = Vector<T>(v.GetSize()); // initialize new vector with same size as v, all 0
for (int i=0; i < v.GetSize(); i++)
{
for (int j=0; j < GetNumCols(); j++)
{
prod.entries_[i] = prod.entries_[i] + (data_[i][j] * v.entries_[j]);
}
}
return prod;
}