-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixOperations.hpp
495 lines (419 loc) · 14.2 KB
/
MatrixOperations.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#ifndef MATRIX_OPERATIONS_HPP
#define MATRIX_OPERATIONS_HPP
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <functional>
#include <stdexcept>
#include <iostream>
#include <set>
#include <cmath>
#include <complex>
#include <algorithm>
#include <iomanip>
#include <omp.h>
#include <chrono>
#include <random>
#include <fstream>
using namespace std;
template <
typename T,
typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
class MatrixDefinition
{
private:
/* data */
std::vector<T> data;
size_t num_rows;
size_t num_cols;
void checkIndexValidity(size_t rows, size_t cols) const
{
if (rows < 0 or rows >= num_rows)
throw invalid_argument("Invalid number of rows");
if (cols < 0 or cols >= num_cols)
throw invalid_argument("Invalid number of columns");
};
// static pair<MatrixDefinition,MatrixDefinition> eigenSorting(MatrixDefinition eigenval,MatrixDefinition eigenvec);
public:
enum Axis
{
All,
Rows,
Cols
};
size_t numCols() const { return num_cols; }
size_t numRows() const { return num_rows; }
// Initialize empty matrix
MatrixDefinition()
{
num_rows = num_cols = 0;
}
~MatrixDefinition(){
};
// Initialize a square matrix of Dimension d
MatrixDefinition(size_t d)
{
MatrixDefinition(d, d);
}
MatrixDefinition(size_t r, size_t c) : num_rows(r), num_cols(c), data(r * c)
{
}
MatrixDefinition(size_t r, size_t c, const vector<T> &MatrixData) : num_rows(r), num_cols(c)
{
if (r * c != MatrixData.size())
throw invalid_argument("Please initialize the matrix with size compatible to vector data");
data = MatrixData;
}
template <std::size_t N>
MatrixDefinition(size_t r, size_t c, T (&data)[N])
{
if (N != r * c)
throw invalid_argument("Matrix dimension incompatible with its initiallizing vector.");
vector<T> v(data, data + N);
MatrixDefinition(r, c, v);
}
// Scalar Arithmetic Operations (POST Matrix)
friend MatrixDefinition operator+(const MatrixDefinition &matrix, double scalar_value)
{
MatrixDefinition resultingMatrix(matrix.num_rows, matrix.num_cols);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < matrix.num_rows; i++)
{
for (size_t j = 0; j < matrix.num_cols; j++)
{
resultingMatrix(i, j) = scalar_value + matrix(i, j);
}
}
return resultingMatrix;
}
friend MatrixDefinition operator-(const MatrixDefinition &matrix, double scalar_value)
{
return matrix + (-scalar_value);
}
friend MatrixDefinition operator*(const MatrixDefinition &matrix, double scalar_value)
{
MatrixDefinition resultingMatrix(matrix.num_rows, matrix.num_cols);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < matrix.num_rows; i++)
{
for (size_t j = 0; j < matrix.num_cols; j++)
{
resultingMatrix(i, j) = scalar_value * matrix(i, j);
}
}
return resultingMatrix;
}
friend MatrixDefinition operator/(const MatrixDefinition &matrix, double scalar_value)
{
MatrixDefinition resultingMatrix(matrix.num_rows, matrix.num_cols);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < matrix.num_rows; i++)
{
for (size_t j = 0; j < matrix.num_cols; j++)
{
resultingMatrix(i, j) = matrix(i, j) / scalar_value;
}
}
return resultingMatrix;
}
// Scalar Arithmetic Operations (PRE Matrix)
friend MatrixDefinition operator+(double scalar_value, const MatrixDefinition matrix)
{
return matrix + scalar_value;
}
friend MatrixDefinition operator-(double scalar_value, const MatrixDefinition matrix)
{
return matrix - scalar_value;
}
friend MatrixDefinition operator*(double scalar_value, const MatrixDefinition matrix)
{
return matrix * scalar_value;
}
friend MatrixDefinition operator/(double scalar_value, const MatrixDefinition &matrix)
{
MatrixDefinition resultingMatrix(matrix.num_rows, matrix.num_cols);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < matrix.num_rows; i++)
{
for (size_t j = 0; j < matrix.num_cols; j++)
{
resultingMatrix(i, j) = scalar_value / matrix(i, j);
}
}
return resultingMatrix;
}
// Scalar Assignment Operations
MatrixDefinition operator+=(double scalar_value)
{
#pragma omp parallel for
for (size_t i = 0; i < data.size(); i++)
{
data[i] += scalar_value;
}
return *this;
}
MatrixDefinition operator-=(double scalar_value)
{
#pragma omp parallel for
for (size_t i = 0; i < data.size(); i++)
{
data[i] -= scalar_value;
}
return *this;
}
MatrixDefinition operator*=(double scalar_value)
{
#pragma omp parallel for
for (size_t i = 0; i < data.size(); i++)
{
data[i] *= scalar_value;
}
return *this;
}
MatrixDefinition operator/=(double scalar_value)
{
#pragma omp parallel for
for (size_t i = 0; i < data.size(); i++)
{
data[i] /= scalar_value;
}
return *this;
}
// Matrix Operations
// Matrix Addition
MatrixDefinition operator+(const MatrixDefinition &new_matrix) const
{
if (num_rows != new_matrix.num_rows || num_cols != new_matrix.num_cols)
{
throw invalid_argument("Cannot operate on these Matrices because their sizes are LeftMatrix:" + to_string(num_rows) + "rows by " + to_string(num_cols) + "columns and RightMatrix:" + to_string(new_matrix.num_rows) + "rows by " + to_string(new_matrix.num_cols) + "columns.");
}
MatrixDefinition resultingMatrix(num_rows, num_cols);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < new_matrix.num_rows; i++)
{
for (size_t j = 0; j < new_matrix.num_cols; j++)
{
resultingMatrix(i, j) = operator()(i, j) + new_matrix(i, j);
}
}
return resultingMatrix;
}
// Matrix Subtraction
MatrixDefinition operator-(const MatrixDefinition &new_matrix) const
{
if (num_rows != new_matrix.num_rows || num_cols != new_matrix.num_cols)
{
throw invalid_argument("Cannot operate on these Matrices because their sizes are LeftMatrix:" + to_string(num_rows) + "rows by " + to_string(num_cols) + "columns and RightMatrix:" + to_string(new_matrix.num_rows) + "rows by " + to_string(new_matrix.num_cols) + "columns.");
}
MatrixDefinition resultingMatrix(num_rows, num_cols);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < new_matrix.num_rows; i++)
{
for (size_t j = 0; j < new_matrix.num_cols; j++)
{
resultingMatrix(i, j) = operator()(i, j) - new_matrix(i, j);
}
}
return resultingMatrix;
}
// Matrix Multiplication
MatrixDefinition operator*(const MatrixDefinition &new_matrix) const
{
if (num_cols != new_matrix.num_rows)
{
throw invalid_argument("Cannot operate on these Matrices because their sizes are LeftMatrix:" + to_string(num_rows) + "rows by " + to_string(num_cols) + "columns and RightMatrix:" + to_string(new_matrix.num_rows) + "rows by " + to_string(new_matrix.num_cols) + "columns.");
}
MatrixDefinition resultingMatrix = fillzeros(num_rows, new_matrix.num_cols);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < new_matrix.num_rows; i++)
{
for (size_t k = 0; k < new_matrix.num_cols; k++)
{
double temp = operator()(i, k);
for (size_t j = 0; j < resultingMatrix.num_cols; j++)
resultingMatrix(i, j) = temp * new_matrix(k, j);
}
}
return resultingMatrix;
}
// Matrix Assignment Operations
MatrixDefinition &operator+=(const MatrixDefinition &new_matrix)
{
if (num_rows != new_matrix.num_rows || num_cols != new_matrix.num_cols)
{
throw invalid_argument("Cannot operate on these Matrices because their sizes are LeftMatrix:" + to_string(num_rows) + "rows by " + to_string(num_cols) + "columns and RightMatrix:" + to_string(new_matrix.num_rows) + "rows by " + to_string(new_matrix.num_cols) + "columns.");
}
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < new_matrix.num_rows; i++)
{
for (size_t j = 0; j < new_matrix.num_cols; j++)
{
operator()(i, j) += new_matrix(i, j);
}
}
return *this;
}
MatrixDefinition &operator-=(const MatrixDefinition &new_matrix)
{
if (num_rows != new_matrix.num_rows || num_cols != new_matrix.num_cols)
{
throw invalid_argument("Cannot operate on these Matrices because their sizes are LeftMatrix:" + to_string(num_rows) + "rows by " + to_string(num_cols) + "columns and RightMatrix:" + to_string(new_matrix.num_rows) + "rows by " + to_string(new_matrix.num_cols) + "columns.");
}
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < new_matrix.num_rows; i++)
{
for (size_t j = 0; j < new_matrix.num_cols; j++)
{
operator()(i, j) -= new_matrix(i, j);
}
}
return *this;
}
MatrixDefinition &operator*=(const MatrixDefinition &new_matrix)
{
if (num_cols != new_matrix.num_rows)
{
throw invalid_argument("Cannot operate on these Matrices because their sizes are LeftMatrix:" + to_string(num_rows) + "rows by " + to_string(num_cols) + "columns and RightMatrix:" + to_string(new_matrix.num_rows) + "rows by " + to_string(new_matrix.num_cols) + "columns.");
}
MatrixDefinition resultingMatrix(num_rows, num_cols);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < new_matrix.num_rows; i++)
{
for (size_t k = 0; k < new_matrix.num_cols; k++)
{
resultingMatrix(i, k) = 0;
for (size_t j = 0; j < resultingMatrix.num_cols; j++)
resultingMatrix(i, k) = operator()(i, j) * new_matrix(j, k);
}
}
num_rows = resultingMatrix.num_rows;
num_cols = resultingMatrix.num_cols;
data = resultingMatrix.data;
return *this;
}
T &operator()(size_t i, size_t j)
{
checkIndexValidity(i, j);
return data[i * num_cols + j];
}
T operator()(size_t i, size_t j) const
{
checkIndexValidity(i, j);
return data[i * num_cols + j];
}
// Helper Functions
static MatrixDefinition fillMatrix(size_t r, size_t c, double value)
{
MatrixDefinition result(r, c, vector<T>(r * c, value));
return result;
}
static MatrixDefinition fillRandom(size_t r, size_t c)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> distr1(1, 100);
vector<T> V1(r * c, 0);
for (int i=0;i<V1.size();i++)
V1.at(i) = distr1(gen);
MatrixDefinition result(r, c, V1);
return result;
}
static MatrixDefinition fillzeros(size_t r, size_t c)
{
return fillMatrix(r, c, 0);
}
bool isSquare() const
{
return num_cols == num_rows;
}
MatrixDefinition subMatrix(size_t r, size_t c) const
{
MatrixDefinition resultingMatrix(num_rows - 1, num_cols - 1);
size_t si = 0;
#pragma omp parallel for
for (size_t i = 0; i < num_rows; i++)
{
size_t subj = 0;
if (i == r)
continue;
for (size_t j = 0; j < num_cols; j++)
{
if (j == c)
continue;
resultingMatrix(si, subj) = operator()(i, j);
subj++;
}
si++;
}
}
MatrixDefinition transpose() const
{
MatrixDefinition resultingMatrix(num_cols, num_rows);
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < resultingMatrix.num_rows; i++)
{
for (size_t j = 0; j < resultingMatrix.num_cols; j++)
{
resultingMatrix(j, i) = operator()(i, j);
}
}
return resultingMatrix;
}
double determinant() const
{
if (!isSquare())
{
throw runtime_error("Not a Square matrix cannot calculate determinant");
}
size_t num = num_rows;
double det = 0;
if (num == 2)
{
return (operator()(0, 0) * operator()(1, 1) - operator()(1, 0) * operator()(0, 1));
}
else
{
#pragma omp parallel for reduction(+ \
: det)
for (size_t i = 0; i < num; i++)
{
det += pow(-1, i) * operator()(0, i) * subMatrix(0, i).determinant();
}
}
return det;
}
void size()
{
cout << "rows" << num_rows << ", columns" << num_cols << endl;
// return 0;
}
void printMatrix()
{
for (int i = 0; i < num_rows; i++)
{
for (int j = 0; j < num_cols; j++)
{
cout << to_string(operator()(i, j)) << ",";
}
cout << endl;
}
}
void printMatrixCSV(int i, string c)
{
ostringstream filename;
filename << "Matrix_"<<c<<"for_iteration_"<<to_string(i)<<".csv";
std::ofstream out;
out.open(filename.str());
for (int i = 0; i < num_rows; i++)
{
for (int j = 0; j < num_cols; j++)
{
out << to_string(operator()(i, j)) << ",";
}
out << endl;
}
}
};
typedef MatrixDefinition<double> MatrixDefinitionD;
typedef MatrixDefinition<int> MatrixDefinitionI;
#endif