-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
352 lines (318 loc) · 8.14 KB
/
Matrix.cpp
File metadata and controls
352 lines (318 loc) · 8.14 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "Matrix.h"
#include <iostream>
#include <cstddef>
#include <stdlib.h>
#include <string>
using namespace std;
//default constructor
template <class T>
Matrix<T>::Matrix()
{
numRows = 0;
numCols = 0;
}
//main constructor
template <class T>
Matrix<T>::Matrix(int r, int c)
{
if(r <= 0 || c <= 0)
cout << "can't make an array with negative/zero number of rows/columns" << endl;
else
{
numRows = r;
numCols = c;
data = allocateData();
}
}
template <class T>
T** Matrix<T>::allocateData() const
{
T** array = new T*[numRows]; //creat the array that stores the rows
for(int i = 0; i < numRows; i++)
array[i] = new T[numCols]();
//create each row, which is num columns long, and initializes elements to 0
return array;
}
template <class T>
Matrix<T>::~Matrix()
{
for( int i = 0; i < numRows; i++)
delete[] data[i]; //delete each row
delete[] data; //delete the array of rows
}
//copy constructor
template <class T>
Matrix<T>::Matrix(const Matrix& copy)
{
numRows = copy.numRows;
numCols = copy.numCols;
data = allocateData();
for(int i = 0; i < copy.numRows; i++)
for (int j = 0; j < copy.numCols; j++)
data[i][j] = copy.data[i][j];
}
template <class T>
Matrix<T>& Matrix<T>::operator =(const Matrix& rhs)
{
Matrix copy(rhs);
numRows = copy.numRows;
numCols = copy.numCols;
data = allocateData();
swapData(copy);
}
template <class T>
bool Matrix<T>::operator ==(const Matrix<T>& rhs) const
{
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
if(data[i][j] != rhs[i][j])
return false;
return true;
}
template <class T>
void Matrix<T>::swapData(Matrix<T>& temp)
{
T** tempData = data;
data = temp.data;
temp.data = tempData;
}
template <class T>
ostream& operator <<(ostream& os, Matrix<T>& rhs)
{
return os <<rhs.printMatrix();
}
template <class T>
Matrix<T>& Matrix<T>::operator *= (const Matrix<T>& rhs)
{
if(numCols != rhs.numRows)
throw "left hand side's number of columns != right hand side's number of rows";
Matrix<T> output = Matrix(numRows, rhs.numCols);
for(int i = 0; i < numRows; i++)
for(int j = 0; j < rhs.numCols; j++)
for(int k = 0; k < numCols; k++)
output[i][j] += data[i][j] * rhs[k][j];
data = output.data;
return *this;
}
template <class T>
const Matrix<T> Matrix<T>::operator *(const Matrix<T>& rhs) const
{
if(numCols != rhs.numRows)
throw "left hand side's number of columns != right hand side's number of rows";
Matrix<T> output = Matrix(numRows, rhs.numCols);
for(int i = 0; i < numRows; i++)
for(int j = 0; j < rhs.numCols; j++)
for(int k = 0; k < numCols; k++)
output[i][j] += data[i][j] * rhs[k][j];
return output;
//return Matrix(*this) *= rhs; why does this cause invalid frees / reads?
}
template <class T>
Matrix<T>& Matrix<T>::operator *= (const T& scalar)
{
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
data[i][j] *= scalar;
return *this;
}
template <class T>
const Matrix<T> Matrix<T>::operator * (const T& scalar) const
{
Matrix<T> output(numRows, numCols);
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
output[i][j] = scalar * data[i][j];
return output;
}
template <class T>
Matrix<T>& Matrix<T>::operator += (const Matrix<T>& rhs)
{
if(numRows != rhs.numRows || numCols != rhs.numCols)
throw "the rows and columns don't match";
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
data[i][j] += rhs[i][j];
return *this;
}
template <class T>
Matrix<T>& Matrix<T>::operator -= (const Matrix<T>& rhs)
{
if(numRows != rhs.numRows || numCols != rhs.numCols)
throw "the rows and columns don't match";
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
data[i][j] -= rhs[i][j];
return *this;
}
template <class T>
const Matrix<T> Matrix<T>::operator +(const Matrix<T>& rhs)const
{
if(numRows != rhs.numRows || numCols != rhs.numCols)
throw "the rows and columns don't match";
Matrix<T> output = Matrix(numRows, numCols);
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
output[i][j] = data[i][j] + rhs[i][j];
return output;
}
template <class T>
const Matrix<T> Matrix<T>::operator -(const Matrix<T>& rhs)const
{
if(numRows != rhs.numRows || numCols != rhs.numCols)
throw "the rows and columns don't match";
Matrix<T> output = Matrix(numRows, numCols);
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
output[i][j] = data[i][j] - rhs[i][j];
return output;
}
template <class T>
T*& Matrix<T>::operator [](const int& row)const
{
//change the index to size_t?
return data[row];
}
template <class T>
const Matrix<T>& Matrix<T>::pad()
{
int newSize = 2;
while( (newSize < numRows || newSize < numCols) && newSize > 0)//>0 so that if the int rolls over it ends
newSize*=2;
T** oldData = data;
int oldRows = numRows;
int oldCols = numCols;
numRows = numCols = newSize;
data = allocateData();
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
{
if(i >= oldRows || j >= oldCols)
if(i == j)
data[i][j] = 1;
else
data[i][j] = 0;
else
data[i][j] = oldData[i][j];
}
for(int i = 0; i < oldRows; i++)
delete [] oldData[i];
delete [] oldData;
return *this;
}
template <class T>
const Matrix<T>* Matrix<T>::transpose()
{
Matrix<T>* transposed = new Matrix<T>(*this);
for(int i = 0; i < numRows; i++)
for(int j = i; j < numCols; j++)//j starts at i because it swaps the top and bottom triangle
swap((*transposed)[i][j], (*transposed)[j][i]);
return transposed;
}
template <class T>
Matrix<T>& Matrix<T>::inverse()
{
if(numRows != numCols)
throw "not a square matrix";
if(numRows == 1 && numCols == 1)
return *this;
int unPadRows = numRows;
int unPadCols = numCols;
pad();
int halfRow = numRows / 2;
int halfCol = numCols / 2;
Matrix<T> B = Matrix<T>(halfRow, halfCol);
Matrix<T> C = Matrix<T>(halfRow, halfCol);
Matrix<T> Ct = Matrix<T>(halfRow, halfCol);
Matrix<T> D = Matrix<T>(halfRow, halfCol);
//split matricies
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
{
if(i < halfRow && j < halfCol) //top left
B[i][j] = data[i][j];
else if(i < halfRow && j >= halfCol) //top right
Ct[i][j - halfCol] = data[i][j];
else if(i >= halfRow && j < halfCol) // bottom left
C[i - halfRow][j] = data[i][j];
else if(i >= halfRow && j >= halfCol) //bottom right
D[i - halfRow][j - halfCol] = data[i][j];
}
Matrix<T> Binv = B.inverse(); //recursion
Matrix<T> W = C * Binv;
Matrix<T> Wt = *W.transpose(); //could also compute with Binv * CT, but I think this is faster
Matrix<T> X = W * Ct;
Matrix<T> S = D - X;
Matrix<T> V = S.inverse(); //recursion
Matrix<T> Y = V * W;
Matrix<T> Yt = *Y.transpose();
Matrix<T> L/*T*/ = Yt * -1; //T is the template class name
Matrix<T> U = Y * -1;
Matrix<T> Z = Wt * Y;
Matrix<T> R = Binv + Z;
//assemble matricies
for(int i = 0; i < numRows; i++)
delete[] data[i];
delete[] data;
numRows = unPadRows;
numCols = unPadCols;
data = allocateData();
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
{
if(i < halfRow && j < halfCol) //top left
data[i][j] = R[i][j];
else if(i < halfRow && j >= halfCol) //top right
data[i][j] = L[i][j - halfCol];
else if(i >= halfRow && j < halfCol) // bottom left
data[i][j] = U[i - halfRow][j];
else if(i >= halfRow && j >= halfCol) //bottom right
data[i][j] = V[i - halfRow][j - halfCol];
}
return *this;
}
template <class T>
void Matrix<T>::fillMatrix(string type, T value)
{
for(int i = 0; i < numRows; i++)
for(int j = 0; j < numCols; j++)
{
if(type == "full")
data[i][j] = value;
if(type == "diagonal")
{
if(i == j)
data[i][j] = value;
else
data[i][j] = 0;
}
if(type == "upper-triangle")
{
if(j > i)
data[i][j] = value;
else
data[i][j] = 0;
}
if(type == "lower-triangle")
{
if(i > j)
data[i][j] = value;
else
data[i][j] = 0;
}
if(type == "random")
data[i][j] = (rand() % (int)value) + 1;
}
}
template <class T>
string Matrix<T>::printMatrix() const
{
string out = "";
for(int i = 0; i < numRows; i++)
{
out += "| ";
for(int j = 0; j < numCols; j++)
out += to_string(data[i][j]) + " ";
out += "|\n";
}
return out += "\n";
}