1 #ifndef EIGEN_EXTENSIONS_H
2 #define EIGEN_EXTENSIONS_H
6 #include <Eigen/Sparse>
15 inline void stdToEig(
const std::vector<double>&
std, Eigen::VectorXd* eig)
18 for(
size_t i = 0;
i <
std.size(); ++
i)
22 inline double stdev(
const Eigen::VectorXd& vec)
24 double mean = vec.sum() / (double)vec.rows();
26 for(
int i = 0;
i < vec.rows(); ++
i)
27 total += (vec.coeffRef(
i) -
mean) * (vec.coeffRef(
i) -
mean);
28 double var = total / (double)vec.rows();
32 template<
class S,
int T,
int U>
35 template<
class S,
int T,
int U>
38 template<
class S,
int T,
int U>
41 template<
class S,
int T,
int U>
65 template<
class S,
int T,
int U>
71 strm.write((
char*)&
bytes,
sizeof(
int));
72 strm.write((
char*)&
rows,
sizeof(
int));
73 strm.write((
char*)&
cols,
sizeof(
int));
74 strm.write((
const char*)
mat.data(),
sizeof(
S) *
rows *
cols);
77 template<
class S,
int T,
int U>
83 strm.read((
char*)&
bytes,
sizeof(
int));
84 strm.read((
char*)&
rows,
sizeof(
int));
85 strm.read((
char*)&
cols,
sizeof(
int));
86 assert(
bytes ==
sizeof(
S));
89 strm.read((
char*)buf,
sizeof(
S) *
rows *
cols);
94 template<
class S,
int T,
int U>
97 int old_precision = strm.precision();
99 strm <<
"% " <<
mat.rows() <<
" " <<
mat.cols() << std::endl;
100 strm <<
mat << std::endl;
101 strm.precision(old_precision);
104 template<
class S,
int T,
int U>
109 while(line.length() == 0) getline(strm, line);
110 assert(line[0] ==
'%');
111 std::istringstream iss(line.substr(1));
118 for(
int y = 0;
y <
rows; ++
y) {
120 std::istringstream iss(line);
121 for(
int x = 0;
x <
cols; ++
x) {
122 iss >>
mat->coeffRef(
y,
x);
130 strm.write((
char*)&val,
sizeof(
T));
136 strm.read((
char*)val,
sizeof(
T));
142 int old_precision = strm.precision();
144 strm <<
"% " << val << std::endl;
145 strm.precision(old_precision);
152 while(line.length() == 0) getline(strm, line);
153 assert(line[0] ==
'%');
154 std::istringstream iss(line.substr(1));
160 #endif // EIGEN_EXTENSIONS_H