-
Notifications
You must be signed in to change notification settings - Fork 3
/
tools.h
287 lines (232 loc) · 8.28 KB
/
tools.h
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. See the enclosed file LICENSE for a copy or if
* that was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright 2017 Max H. Gerlach
*
* */
/*
* tools.h
*
* Created on: Feb 10, 2011
* Author: gerlach
*/
#ifndef TOOLS_H_
#define TOOLS_H_
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstdlib>
#include <armadillo>
#include <unistd.h> // gethostname, getpid, sleep
#include "boost/mpl/assert.hpp" // for not_defined, as introduced below
//instantiate this to get a nice compile-time error message stating that
//something is undefined via BOOST_MPL_ASSERT(( not_defined<T> ));
template<typename T> struct not_defined : boost::mpl::false_ { };
//only use with small numbers!
inline uint32_t uint_pow(uint32_t base, uint32_t exponent) {
uint32_t result = 1;
for (uint32_t times = 0; times < exponent; ++times) {
result *= base;
}
return result;
}
template<typename T> inline
std::string numToString(T i) {
std::stringstream s;
s << i;
return s.str();
}
template<typename T> inline
std::string numToString(T i, uint32_t floatPrecision) {
std::stringstream s;
s.precision(int(floatPrecision));
s.setf(std::ios::scientific, std::ios::floatfield);
s << i;
return s.str();
}
// returns 0 if str cannot be parsed as a number
template<typename T> inline
T fromString(const std::string& str) {
std::stringstream s(str);
T i;
s >> i;
return i;
}
template<typename T> inline
T fromString(const char* str) {
return fromString<T>(std::string(str));
}
template<typename T> inline
void destroy(T*& pointer) {
delete pointer;
pointer = 0;
}
template<typename T> inline
void destroyAll(std::vector<T*>& vec) {
for (typename std::vector<T*>::iterator iter = vec.begin(); iter != vec.end(); ++iter) {
delete *iter;
*iter = 0;
}
}
template<typename T> inline
void printVector(const std::vector<T>& vec) {
for (uint32_t n = 0; n < vec.size(); ++n) {
std::cout << vec[n] << "\t";
}
std::cout << std::endl;
}
//compare two entries of a std::map by their mapped values using this function
template<typename K, typename V> inline
bool mapValueCompare(const std::pair<K, V>& a, const std::pair<K, V>& b) {
return (a.second < b.second);
}
template <typename BoostArray, typename Value>
void initArray(BoostArray& array, Value val) {
std::fill(array.data(), array.data() + array.num_elements(), val);
}
//
//template <typename BoostArray, typename STLVector>
//void copyVectorToArray(BoostArray& array, const STLVector& vector) {
// std::copy(vector.begin(), vector.end(), array.data());
//}
//rename a file to "$filename~"
//it is not a good idea to do this if we write to the original file name
//directly after the call (moving seems to take some time to have effect...)
inline void moveFileBackUp(const std::string& filename) {
std::string command = "mv " + filename + " " + filename + "~" + " &>/dev/null";
std::system(command.c_str());
}
//for small files:
inline void copyFileBackUp(const std::string& filename) {
std::ifstream input(filename.c_str(), std::ios::binary);
std::ofstream output((filename + "~").c_str(), std::ios::trunc | std::ios::binary);
output << input.rdbuf();
}
// Wrapper aroundPOSIX glob() that returns a vector<string>
std::vector<std::string> glob(const std::string& path);
// pass this as a default do-nothing function parameter in cases
// where no return value is expected
struct VoidNoOp {
void operator()() const { };
template<typename P1, typename... Params>
void operator()(P1 p1, Params... parameters) {
(void)(p1); // we do this just to remove warnings -- we need the recursion for that
operator()(parameters...);
}
// template<class A>
// void operator()(A a) const { (void)(a); }
// template<class A, class B>
// void operator()(A a, B b) const { (void)(a); (void)(b); }
// template<class A, class B, class C>
// void operator()(A a, B b, C c) const { (void)(a); (void)(b); (void)(c); }
};
typedef double num;
typedef std::complex<num> cpx;
typedef arma::Mat<num> MatNum;
typedef arma::Mat<cpx> MatCpx;
template<class Matrix>
inline void print_matrix_diff(Matrix mat1, Matrix mat2,
const std::string& name) {
//MatNum diff_wrapped = arma::abs(mat1 - mat2) / arma::abs(mat1);
MatNum diff = arma::abs(mat1 - mat2);
using arma::max; using arma::mean;
std::cout << name << ": mean = " << arma::mean(arma::mean(arma::abs(mat1))) << "\n";
std::cout << name << ": max diff = " << max(max(diff)) << "\n";
std::cout << name << ": mean diff = " << mean(mean(diff)) << "\n";
}
template<class Matrix>
inline void print_matrix_rel_diff(Matrix mat1, Matrix mat2,
const std::string& name) {
MatNum diff = arma::abs(mat1 - mat2) / arma::abs(mat1);
using arma::max; using arma::mean;
std::cout << name << ": max rel diff = " << max(max(diff)) << "\n";
std::cout << name << ": mean rel diff = " << mean(mean(diff)) << "\n";
}
template<typename Matrix> inline
void debugSaveMatrix(const Matrix& matrix, const std::string& basename) {
matrix.eval().save(basename + ".csv", arma::csv_ascii);
}
template<typename Matrix> inline
void debugSaveMatrixCpx(const Matrix& matrix, const std::string& basename) {
MatNum r = arma::real(matrix);
r.save(basename + "_real.csv", arma::csv_ascii);
MatNum i = arma::imag(matrix);
i.save(basename + "_imag.csv", arma::csv_ascii);
}
template<typename Val> inline
void debugSaveMatrixRealOrCpx(const arma::Mat<Val>& matrix, const std::string& basename) {
(void)matrix; (void)basename;
}
template<> inline
void debugSaveMatrixRealOrCpx(const arma::Mat<num>& matrix, const std::string& basename) {
debugSaveMatrix(matrix, basename);
}
template<> inline
void debugSaveMatrixRealOrCpx(const arma::Mat<cpx>& matrix, const std::string& basename) {
debugSaveMatrixCpx(matrix, basename);
}
// these can be called from the debugger)
void printMatrixReal(const arma::Mat<num>& mat);
void printMatrixComplex(const arma::Mat<cpx>& mat);
// setting the real part of a vector
template<typename VectorInRealPart>
void setVectorReal(arma::Col<cpx>& out, const VectorInRealPart& in) {
const uint32_t N = out.n_elem;
for (uint32_t i = 0; i < N; ++i) {
out[i].real(in[i]);
}
}
template<typename VectorInRealPart>
void setVectorReal(arma::Col<num>& out, const VectorInRealPart& in) {
const uint32_t N = out.n_elem;
for (uint32_t i = 0; i < N; ++i) {
out[i] = in[i];
}
}
// setting the imaginary part of a vector
template<typename VectorInImagPart>
void setVectorImag(arma::Col<cpx>& out, const VectorInImagPart& in) {
const uint32_t N = out.n_elem;
for (uint32_t i = 0; i < N; ++i) {
out[i].imag(in[i]);
}
}
template<typename VectorInImagPart>
void setVectorImag(arma::Col<num>& out, const VectorInImagPart& in) {
(void)out; (void)in;
}
// transpose a Cube in all three indices. The (linear) representation
// in memory of the result corresponds is the C-ordered representation
// of a Fortran-ordered original.
template<typename Val>
arma::Cube<Val> transpose_3d(const arma::Cube<Val>& orig) {
arma::Cube<Val> result(orig.n_slices,
orig.n_cols,
orig.n_rows); // flipped dimensions
for (uint32_t s = 0; s < orig.n_slices; ++s) {
for (uint32_t r = 0; r < orig.n_rows; ++r) {
for (uint32_t c = 0; c < orig.n_cols; ++c) {
result(s, c, r) = orig(r, c, s);
}
}
}
return result;
}
// given a container of numeric values, find the index of the element
// that is numerically nearest to a value to search for. This does
// *not* require the container to be sorted.
template<typename Container, typename Value>
std::size_t findNearest(const Container& c, Value target) {
auto it = std::min_element(std::begin(c), std::end(c),
[target](Value x, Value y)
{
return std::abs(x - target) < std::abs(y - target);
});
return std::distance(std::begin(c), it);
}
#endif /* TOOLS_H_ */