-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.hpp
68 lines (55 loc) · 1.34 KB
/
utils.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
#ifndef UTILS_HPP
#define UTILS_HPP
#include <stdexcept>
#include <vector>
#include <valarray>
#include <random>
#include <iostream>
/*
Utility file containing both typedefs and random number generation
*/
template <typename T>
using Grid3D = std::valarray<T>;
using Vec = std::valarray<double>;
using Spin = std::valarray<double>;
Vec uniform_on_sphere(size_t dim = 3);
std::valarray<size_t> random_site(size_t N, size_t dim = 3);
double uniform_unit();
template<typename T>
std::string printRaw(std::valarray<T> const& v)
{
std::string s = "";
for(size_t i = 0 ; i < v.size() - 1 ; ++i)
{
s += std::to_string(v[i]) + " ";
}
return s + std::to_string(v[v.size() - 1]);
}
// Print operator for valarray v -> "[v1 v2 v3 ...]"
template <typename T>
std::ostream& operator<<(std::ostream& out, std::valarray<T> const& v)
{
return out << "(" << printRaw(v) << ")";
}
template <typename T>
std::istream& operator>>(std::istream& in, std::valarray<T> & v)
{
char c;
in >> std::noskipws >> c;
if(c != '(')
throw std::runtime_error{"No parenthesis"};
std::vector<T> vec;
while(in.good())
{
T val;
in >> std::noskipws >> val;
in >> std::noskipws >> c;
vec.push_back(val);
if (c == ')')
break;
}
v.resize(vec.size());
std::copy(std::begin(vec), std::end(vec), std::begin(v));
return in;
}
#endif