29 #ifndef NEMOSYS_AUXILIARYFUNCTIONS_H_ 30 #define NEMOSYS_AUXILIARYFUNCTIONS_H_ 57 using time_t = std::chrono::time_point<std::chrono::system_clock>;
67 return std::chrono::duration_cast<std::chrono::milliseconds>(
stopTime -
100 template <
typename T>
101 std::vector<T>
flatten(
const std::vector<std::vector<T>> &v);
104 template <
typename T>
105 std::vector<std::vector<T>>
fold(
const std::vector<T> &v,
int dim);
108 template <
typename T>
109 std::vector<T>
operator+(
const std::vector<T> &x,
const std::vector<T> &y);
111 template <
typename T>
112 std::vector<T>
operator-(
const std::vector<T> &x,
const std::vector<T> &y);
115 template <
typename T>
116 std::vector<T>
operator*(T a,
const std::vector<T> &x);
119 template <
typename T>
120 std::vector<T>
hadamard(
const std::vector<T> &x,
const std::vector<T> &y);
123 template <
typename T>
124 T
l2_Norm(
const std::vector<T> &x);
126 template <
typename T>
129 template <
typename T>
132 template <
typename T>
133 bool hasZero(
const std::vector<T> &x);
135 template <
typename T>
136 std::vector<T>
getMinMax(
const std::vector<T> &x);
139 template <
typename T>
141 const std::vector<T> &yminmax);
143 template <
typename T>
145 const std::vector<T> &yminmax);
147 template <
typename T>
150 template <
typename T>
151 std::vector<bool>
cellsToRefine(
const std::vector<T> &values, T tol);
153 template <
typename T>
157 template <
typename T>
160 inline std::string
trim_fname(
const std::string &name,
const std::string &ext);
162 inline std::string
find_ext(
const std::string &fname);
164 inline void toLower(std::string &str);
166 inline void toUpper(std::string &str);
168 inline std::string
findToStr(
const std::string &str,
const std::string &ptrn);
170 inline std::string
findFromStr(
const std::string &str,
const std::string &ptrn);
174 inline std::string
find_name(
const std::string &fname);
176 template <
typename T>
177 void printVec(
const std::vector<T> &v);
179 template <
typename T>
180 bool valInVec(
const std::vector<T> &v, T val);
182 template <
typename A,
typename B>
183 std::pair<B, A>
flip_pair(
const std::pair<A, B> &p);
185 template <
typename A,
typename B>
186 std::multimap<B, A>
flip_map(
const std::map<A, B> &src);
188 template <
typename T>
189 bool isInBBox(
const std::vector<T> &crd,
const std::vector<T> &bb);
191 template <
typename A,
typename B>
196 inline std::shared_ptr<char>
strToChar(
const std::string &strng);
198 inline std::vector<std::string>
Tokenize(
const std::string &lineIn,
200 inline std::vector<std::string>
Tokenize(
const std::string &lineIn,
201 const std::string &delim);
203 template <
typename A,
typename B>
210 template <
typename T>
211 std::vector<T>
flatten(
const std::vector<std::vector<T>> &v) {
212 std::size_t size = 0;
213 for (
const auto &sub : v) size += sub.size();
215 std::vector<T> result;
216 result.reserve(size);
217 for (
const auto &sub : v) result.insert(result.end(), sub.begin(), sub.end());
222 template <
typename T>
223 std::vector<std::vector<T>>
fold(
const std::vector<T> &v,
int dim) {
224 if (v.size() % dim != 0) {
225 std::cerr <<
"Size must be divisible by dim for folding" << std::endl;
229 std::vector<std::vector<T>> result(v.size() / dim);
230 for (std::size_t i = 0; i < result.size(); ++i) {
231 result[i].resize(dim);
232 for (std::size_t j = 0; j < dim; ++j) result[i][j] = v[i * dim + j];
238 template <
typename T>
241 for (
const auto &i : x) result += i * i;
243 return std::sqrt(result);
247 template <
typename T>
248 std::vector<T>
operator+(
const std::vector<T> &x,
const std::vector<T> &y) {
249 if (x.size() != y.size()) {
250 std::cerr <<
"Vectors must be same length for addition" << std::endl;
254 std::vector<T> result(x.size());
255 for (std::size_t i = 0; i < x.size(); ++i) result[i] = x[i] + y[i];
260 template <
typename T>
261 std::vector<T>
operator-(
const std::vector<T> &x,
const std::vector<T> &y) {
262 if (x.size() != y.size()) {
263 std::cerr <<
"Vectors must be same length for subtraction" << std::endl;
267 std::vector<T> result(x.size());
268 for (std::size_t i = 0; i < x.size(); ++i) result[i] = x[i] - y[i];
273 template <
typename T>
274 std::vector<T>
operator*(T a,
const std::vector<T> &x) {
275 std::vector<T> result(x.size());
276 for (std::size_t i = 0; i < x.size(); ++i) result[i] = a * x[i];
280 template <
typename T>
281 std::vector<T>
hadamard(
const std::vector<T> &x,
const std::vector<T> &y) {
282 if (x.size() != y.size()) {
283 std::cerr <<
"Vectors must be same length for Hadamard product" 288 std::vector<T> result(x.size());
289 for (std::size_t i = 0; i < x.size(); ++i) result[i] = x[i] * y[i];
294 template <
typename T>
300 std::shared_ptr<char>
strToChar(
const std::string &strng) {
301 std::shared_ptr<char> tab(
new char[strng.length() + 1],
302 std::default_delete<char[]>());
303 std::strcpy(tab.get(), strng.c_str());
308 std::vector<std::string>
Tokenize(
const std::string &lineIn,
310 std::vector<std::string> rtrnTokens;
312 std::string intermediate;
313 std::stringstream checkStr(lineIn);
316 while (std::getline(checkStr, intermediate, delim)) {
317 rtrnTokens.push_back(intermediate);
323 std::vector<std::string>
Tokenize(
const std::string &lineIn,
324 const std::string &delim) {
325 std::vector<std::string> rtrnTokens;
326 std::vector<char> delimStr;
327 delimStr.reserve(delim.size());
329 for (
int i = 0; i < delim.size(); i++) delimStr.push_back(delim[i]);
331 std::string intermediate;
332 std::stringstream checkStr(lineIn);
336 while (std::getline(checkStr, intermediate, delimStr[iter])) {
337 rtrnTokens.push_back(intermediate);
344 template <
typename T>
346 std::transform(x.begin(), x.end(), x.begin(), reciprocal<T>);
349 template <
typename T>
351 for (
const auto &i : x)
352 if (i == 0)
return true;
357 template <
typename T>
360 for (std::size_t i = 0; i < x.size(); ++i)
361 if (!std::isinf(x[i]))
364 auto minmax = std::minmax_element(tmp.begin(), tmp.end());
366 return {*minmax.first, *minmax.second};
371 template <
typename T>
373 const std::vector<T> &yminmax) {
374 if (std::isinf(x))
return yminmax[1];
375 return yminmax[0] + (yminmax[1] - yminmax[0]) * (x - xminmax[0]) /
376 (xminmax[1] - xminmax[0]);
380 template <
typename T>
382 const std::vector<T> &yminmax) {
387 template <
typename T>
390 for (
const auto &i : x) ave += i;
394 for (
const auto &i : x) stdev += (i - ave) * (i - ave);
395 stdev = std::sqrt(stdev / x.size());
401 template <
typename T>
403 std::vector<bool> result(values.size(),
false);
404 for (std::size_t i = 0; i < values.size(); ++i)
405 if (values[i] > tol) result[i] =
true;
410 template <
typename T>
413 if (std::abs(dev / mean) >= 1.0) dev = 0.99 * mean;
418 std::vector<bool> result(values.size(),
false);
419 for (std::size_t i = 0; i < values.size(); ++i)
420 if (values[i] > th || values[i] < tl) result[i] =
true;
425 template <
typename T>
428 if (dev > 1) dev = 1;
430 T max = *std::max_element(values.begin(), values.end());
431 T hl = (1 - dev) * max;
433 std::vector<bool> result(values.size(),
false);
434 for (std::size_t i = 0; i < values.size(); ++i)
435 if (values[i] > hl) result[i] =
true;
440 std::string
trim_fname(
const std::string &fname,
const std::string &ext) {
441 std::string::size_type end = fname.find_last_of(
'.');
442 if (end != std::string::npos) {
443 return fname.substr(0, end).append(ext);
445 std::cerr <<
"Error finding file extension for " << fname << std::endl;
452 std::string::size_type last = fname.find_last_of(
'.');
453 if (last != std::string::npos) {
454 return fname.substr(last);
456 std::cerr <<
"Error finding file extension for " << fname << std::endl;
463 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
468 std::transform(str.begin(), str.end(), str.begin(), ::toupper);
472 std::string
findToStr(
const std::string &str,
const std::string &ptrn) {
473 return str.substr(0, str.find_first_of(ptrn));
477 std::string
findFromStr(
const std::string &str,
const std::string &ptrn) {
478 return str.substr(str.find_first_of(ptrn) + 1, std::string::npos);
488 timeInfo = localtime(&rawTime);
490 strftime(buffer,
sizeof(buffer),
"%d-%m-%Y %I:%M:%S %p", timeInfo);
491 std::string str(buffer);
498 std::string::size_type first = fname.find_last_of(
'/');
499 std::string::size_type last = fname.find_last_of(
'.');
500 if (first != std::string::npos && last != std::string::npos) {
501 return fname.substr(first + 1, last);
503 std::cerr <<
"error finding file extension for " << fname << std::endl;
509 template <
typename T>
511 for (
const auto &i : v) std::cout << std::setprecision(15) << i <<
" ";
512 std::cout << std::endl;
516 template <
typename T>
518 for (
const auto &i : v)
519 if (i == val)
return true;
523 template <
typename A,
typename B>
525 return std::pair<B, A>(p.second, p.first);
528 template <
typename A,
typename B>
529 std::multimap<B, A>
flip_map(
const std::map<A, B> &src) {
530 std::multimap<B, A> dst;
531 std::transform(src.begin(), src.end(), std::inserter(dst, dst.begin()),
536 template <
typename T>
537 bool isInBBox(
const std::vector<T> &crd,
const std::vector<T> &bb) {
538 for (std::size_t i = 0; i < crd.size(); ++i)
539 if (crd[i] < bb[2 * i] || crd[i] > bb[2 * i + 1])
return false;
543 template <
typename A,
typename B>
545 if (!mapObj.empty()) {
546 std::vector<A> sortedKeys(mapObj.size());
547 for (
const auto &i : mapObj) sortedKeys.emplace_back(i.first);
550 std::cerr <<
"Map is empty! No sorted keys to return." << std::endl;
557 const char *alp =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
558 std::random_device rd;
559 std::default_random_engine dre(rd());
560 std::uniform_int_distribution<int> uid(0, strlen(alp) - 1);
563 for (
int i = 0; i < length; ++i) out += alp[uid(dre)];
568 template <
typename A,
typename B>
571 auto it = map.begin();
573 auto ub = map.upper_bound(min);
578 for (; it != map.end(); ++it) {
579 if (it->first == i) {
591 #endif // NEMOSYS_AUXILIARYFUNCTIONS_H_ void scale_vec_to_range(std::vector< T > &x, const std::vector< T > &xminmax, const std::vector< T > &yminmax)
std::multimap< B, A > flip_map(const std::map< A, B > &src)
std::vector< T > getMinMax(const std::vector< T > &x)
A leastUnusedKey(const std::map< A, B > &map, A min=1)
std::vector< T > getMeanStdev(const std::vector< T > &x)
std::vector< bool > cellsToRefineMaxdev(const std::vector< T > &values, T dev)
void reciprocal_vec(std::vector< T > &x)
std::vector< std::string > Tokenize(const std::string &lineIn, const char &delim)
std::string find_ext(const std::string &fname)
std::shared_ptr< char > strToChar(const std::string &strng)
std::chrono::time_point< std::chrono::system_clock > time_t
std::vector< T > operator+(const std::vector< T > &x, const std::vector< T > &y)
std::string findToStr(const std::string &str, const std::string &ptrn)
std::pair< B, A > flip_pair(const std::pair< A, B > &p)
void toUpper(std::string &str)
std::string findFromStr(const std::string &str, const std::string &ptrn)
std::string trim_fname(const std::string &name, const std::string &ext)
std::string getRandomString(int length)
void toLower(std::string &str)
T scale_to_range(T x, const std::vector< T > &xminmax, const std::vector< T > &yminmax)
std::string find_name(const std::string &fname)
std::vector< T > hadamard(const std::vector< T > &x, const std::vector< T > &y)
bool valInVec(const std::vector< T > &v, T val)
std::vector< T > operator*(T a, const std::vector< T > &x)
void printVec(const std::vector< T > &v)
std::vector< bool > cellsToRefineStdev(const std::vector< T > &values, T mean, T dev)
bool isInBBox(const std::vector< T > &crd, const std::vector< T > &bb)
std::vector< T > operator-(const std::vector< T > &x, const std::vector< T > &y)
bool hasZero(const std::vector< T > &x)
std::vector< std::vector< T > > fold(const std::vector< T > &v, int dim)
std::vector< A > getSortedKeys(const std::map< A, B > &mapObj)
std::vector< bool > cellsToRefine(const std::vector< T > &values, T tol)
std::vector< T > flatten(const std::vector< std::vector< T >> &v)
T l2_Norm(const std::vector< T > &x)