/
boostutil.h
70 lines (56 loc) · 1.34 KB
/
boostutil.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
// utility functions for boost matrices/vectors
#include <fstream>
namespace ublas = boost::numeric::ublas;
using namespace std;
double vsum(ublas::vector<double> v)
{
double s = 0.0;
for(unsigned int i = 0; i < v.size(); i++)
s += v(i);
return s;
}
double vmax(ublas::vector<double> v)
{
return *max_element(v.begin(), v.end());
}
double vmin(ublas::vector<double> v)
{
return *min_element(v.begin(), v.end());
}
void zero(ublas::vector<double> & v)
{
for(unsigned int i = 0; i < v.size(); i++)
v(i) = 0.0;
}
void loadtxt(string fname, ublas::matrix<double> & M)
{
ifstream f (fname.c_str());
if(!f.is_open())
{
cerr << "Error opening file " << fname << ". Exiting..." << endl;
exit(EXIT_FAILURE);
}
for (unsigned int i = 0; i < M.size1(); i++)
for (unsigned int j = 0; j < M.size2(); j++)
f >> M(i,j);
f.close();
}
void savetxt(string fname, ublas::matrix<double> & M)
{
ofstream f (fname.c_str());
if(!f.is_open())
{
cerr << "Error opening file " << fname << ". Exiting..." << endl;
exit(EXIT_FAILURE);
}
for (unsigned int i = 0; i < M.size1(); i++)
{
for (unsigned int j = 0; j < M.size2(); j++)
{
f << M(i,j);
if(j < M.size2()-1) f << " ";
else f << endl;
}
}
f.close();
}