-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.hpp
60 lines (51 loc) · 1.48 KB
/
util.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
// Standard libraries
#include "headers.hpp"
/* Templated code (must be visible to the compiler) */
template <typename T>
std::vector<T> getRange(T a, T b, int n) {
std::vector<T> range;
range.reserve(n);
double step = (b - a) / n;
while (a < (b - step/2)) {
range.push_back(a);
a += step;
}
return range;
}
template <typename T>
void printMatrix(std::vector< std::vector<T> > matrix) {
for (const auto& vector: matrix) {
for (const auto& element: vector) {
std::cout << element << '\t';
}
std::cout << '\n';
}
std::cout << std::flush; // std::endl;
}
template <typename T>
void printVector(std::vector<T> vector) {
for (const auto& element: vector) {
std::cout << element << '\t';
}
std::cout << std::endl;
}
template <typename T>
void transpose(std::vector< std::vector<T> > &input) {
std::vector< std::vector<T> > copy = input;
std::vector< std::vector<T> > matrix(copy[0].size(), std::vector<T>(copy.size()));
for (typename std::vector<T>::size_type i = 0; i < matrix.size(); i++) {
for (typename std::vector<T>::size_type j = 0; j < matrix[0].size(); j++) {
matrix[i][j] = copy[j][i];
}
}
input = matrix;
}
template <typename T>
bool checkConvergence(std::vector< std::vector<T> > &A, std::vector< std::vector<T> > &B, const double &delta) {
for (typename std::vector<T>::size_type i = 0; i < A.size(); i++) {
for (typename std::vector<T>::size_type j = 0; j < A[0].size(); j++) {
if (std::abs(A[i][j] - B[i][j]) > delta) return false;
}
}
return true;
}