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)
17 eig->resize(std.size());
18 for(
size_t i = 0; i < std.size(); ++i)
19 eig->coeffRef(i) = std[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>
33 void serialize(
const Eigen::Matrix<S, T, U>& mat, std::ostream& strm);
35 template<
class S,
int T,
int U>
36 void deserialize(std::istream& strm, Eigen::Matrix<S, T, U>* mat);
38 template<
class S,
int T,
int U>
39 void serializeASCII(
const Eigen::Matrix<S, T, U>& mat, std::ostream& strm);
41 template<
class S,
int T,
int U>
65 template<
class S,
int T,
int U>
66 void serialize(
const Eigen::Matrix<S, T, U>& mat, std::ostream& strm)
68 int bytes =
sizeof(S);
69 int rows = mat.rows();
70 int cols = mat.cols();
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>
78 void deserialize(std::istream& strm, Eigen::Matrix<S, T, U>* mat)
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));
88 S *buf = (S*) malloc(
sizeof(S) * rows * cols);
89 strm.read((
char*)buf,
sizeof(S) * rows * cols);
90 *mat = Eigen::Map< Eigen::Matrix<S, T, U> >(buf, 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));
117 *mat = Eigen::Matrix<S, T, U>(rows, cols);
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 void stdToEig(const std::vector< double > &std, Eigen::VectorXd *eig)
void serializeScalarASCII(T val, std::ostream &strm)
void deserializeASCII(std::istream &strm, Eigen::Matrix< S, T, U > *mat)
void serialize(const Eigen::Matrix< S, T, U > &mat, std::ostream &strm)
GLM_FUNC_DECL vecType< T, P > sqrt(vecType< T, P > const &x)
double stdev(const Eigen::VectorXd &vec)
void serializeScalar(T val, std::ostream &strm)
void deserializeScalarASCII(std::istream &strm, T *val)
void serializeASCII(const Eigen::Matrix< S, T, U > &mat, std::ostream &strm)
void deserializeScalar(std::istream &strm, T *val)
void deserialize(std::istream &strm, Eigen::Matrix< S, T, U > *mat)