29 #ifndef MRPT_EIGEN_PLUGINS_IMPL_H
30 #define MRPT_EIGEN_PLUGINS_IMPL_H
37 namespace internal_mrpt
40 template<
int R,
int C>
43 template <
typename S,
int Opt,
int MaxR,
int MaxC>
44 static inline void doit(Eigen::Matrix<S,R,C,Opt,MaxR,MaxC> &mat,
size_t new_rows,
size_t new_cols)
46 ::mrpt::math::detail::TAuxResizer<Eigen::Matrix<S,R,C,Opt,MaxR,MaxC>,Eigen::Matrix<S,R,C,Opt,MaxR,MaxC>::SizeAtCompileTime>::internal_resize(mat,new_rows,new_cols);
54 template <
typename S,
int Opt,
int MaxR,
int MaxC>
55 static inline void doit(Eigen::Matrix<S,R,1,Opt,MaxR,MaxC> &mat,
size_t new_rows,
size_t new_cols)
57 ::mrpt::math::detail::TAuxResizer<Eigen::Matrix<S,R,1,Opt,MaxR,MaxC>,Eigen::Matrix<S,R,1,Opt,MaxR,MaxC>::SizeAtCompileTime>::internal_resize(mat,new_rows);
65 template <
typename S,
int Opt,
int MaxR,
int MaxC>
66 static inline void doit(Eigen::Matrix<S,1,C,Opt,MaxR,MaxC> &mat,
size_t new_rows,
size_t new_cols)
68 ::mrpt::math::detail::TAuxResizer<Eigen::Matrix<S,1,C,Opt,MaxR,MaxC>,Eigen::Matrix<S,1,C,Opt,MaxR,MaxC>::SizeAtCompileTime>::internal_resize(mat,new_cols);
75 template <
typename S,
int Opt,
int MaxR,
int MaxC>
76 static inline void doit(Eigen::Matrix<S,1,1,Opt,MaxR,MaxC> &mat,
size_t new_rows,
size_t new_cols)
78 ::mrpt::math::detail::TAuxResizer<Eigen::Matrix<S,1,1,Opt,MaxR,MaxC>,Eigen::Matrix<S,1,1,Opt,MaxR,MaxC>::SizeAtCompileTime>::internal_resize(mat,new_cols);
86 template <
class Derived>
87 template <
class MATRIX1,
class MATRIX2>
90 Matrix<Scalar,Dynamic,1> evals;
92 eVals.resize(evals.size(),evals.size());
94 eVals.diagonal()=evals;
99 template <
class Derived>
100 template <
class MATRIX1,
class VECTOR1>
103 Eigen::EigenSolver< Derived > es(*
this,
true);
104 eVecs = es.eigenvectors().real();
105 eVals = es.eigenvalues().real();
108 std::vector<std::pair<Scalar,Index> > D;
109 D.reserve(eVals.size());
110 for (Index i=0;i<eVals.size();i++)
111 D.push_back(std::make_pair<Scalar,Index>(eVals.coeff(i,0),i));
112 std::sort(D.begin(),D.end());
114 sortedEigs.resizeLike(eVecs);
115 for (
int i=0;i<eVals.size();i++)
117 eVals.coeffRef(i,0)=D[i].first;
118 sortedEigs.col(i)=eVecs.col(D[i].second);
125 template <
class Derived>
126 template <
class MATRIX1,
class MATRIX2>
129 Matrix<Scalar,Dynamic,1> evals;
131 eVals.resize(evals.size(),evals.size());
133 eVals.diagonal()=evals;
138 template <
class Derived>
139 template <
class MATRIX1,
class VECTOR1>
143 Eigen::SelfAdjointEigenSolver<Derived> eigensolver(*
this);
144 eVecs = eigensolver.eigenvectors();
145 eVals = eigensolver.eigenvalues();
149 template <
class Derived>
153 if ( Derived::RowsAtCompileTime==Eigen::Dynamic)
157 size_t ini = s.find_first_not_of(
" \t\r\n");
158 if (ini==std::string::npos || s[ini]!=
'[') {
return false; }
160 size_t end = s.find_last_not_of(
" \t\r\n");
161 if (end==std::string::npos || s[end]!=
']')
return false;
163 if (ini>end)
return false;
165 std::vector<Scalar> lstElements;
173 size_t end_row = s.find_first_of(
";]",i);
174 if (end_row==std::string::npos) {
return false; }
177 std::stringstream ss(s.substr(i, end_row-i ));
185 if (ss.bad() || ss.fail())
break;
186 lstElements.push_back(val);
191 if (lstElements.empty())
198 if ( Derived::RowsAtCompileTime==Eigen::Dynamic )
204 const size_t N = lstElements.size();
207 if ((nRow>0 &&
size_t(cols())!=N) ||
208 (nRow==0 && Derived::ColsAtCompileTime!=Eigen::Dynamic && Derived::ColsAtCompileTime!=
int(N)) )
210 if (dumpErrorMsgToStdErr)
211 std::cerr <<
"[fromMatlabStringFormat] Row " << nRow+1 <<
" has invalid number of columns.\n";
216 if ( Derived::RowsAtCompileTime==Eigen::Dynamic || Derived::ColsAtCompileTime==Eigen::Dynamic )
218 else if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRow)>=Derived::RowsAtCompileTime)
220 if (dumpErrorMsgToStdErr)
221 std::cerr <<
"[fromMatlabStringFormat] Read more rows than the capacity of the fixed sized matrix.\n";
225 for (
size_t q=0;q<N;q++)
226 coeffRef(nRow,q) = lstElements[q];
235 if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRow)!=Derived::RowsAtCompileTime)
237 if (dumpErrorMsgToStdErr)
238 std::cerr <<
"[fromMatlabStringFormat] Read less rows than the capacity of the fixed sized matrix.\n";
244 template <
class Derived>
248 s <<
"[" << std::scientific;
249 s.precision(decimal_digits);
250 for (Index i=0;i<rows();i++)
252 for (Index j=0;j<cols();j++)
253 s << coeff(i,j) <<
" ";
254 if (i<rows()-1) s <<
";";
260 template <
class Derived>
262 const std::string &file,
264 bool appendMRPTHeader,
265 const std::string &userHeader
268 FILE *f=
::fopen(file.c_str(),
"wt");
270 throw std::runtime_error(std::string(
"saveToTextFile: Error opening file ")+file+std::string(
"' for writing a matrix as text."));
272 if (!userHeader.empty())
273 fprintf(f,
"%s",userHeader.c_str() );
275 if (appendMRPTHeader)
279 struct tm * timeinfo = ::localtime(&rawtime);
281 fprintf(f,
"%% File generated with MRPT %s at %s\n%%-----------------------------------------------------------------\n",
286 for (Index i=0; i < rows(); i++)
288 for (Index j=0; j < cols(); j++)
296 throw std::runtime_error(
"Unsupported value for the parameter 'fileFormat'!");
307 template <
class Derived>
310 std::ifstream f(file.c_str());
311 if (f.fail())
throw std::runtime_error(std::string(
"loadFromTextFile: can't open file:") + file);
315 template <
class Derived>
320 std::vector<double> fil(512);
325 if (str.size() && str[0]!=
'#' && str[0]!=
'%')
328 const char *ptr = str.c_str();
332 while ( ptr[0] && ptr!=ptrEnd )
335 while (ptr[0] && (ptr[0]==
' ' || ptr[0]==
'\t' || ptr[0]==
'\r' || ptr[0]==
'\n'))
337 if (fil.size()<=i) fil.resize(fil.size()+512);
339 fil[i] = strtod(ptr,&ptrEnd);
350 if ((Derived::ColsAtCompileTime!=Eigen::Dynamic && Index(i)!=Derived::ColsAtCompileTime) )
351 throw std::runtime_error(
"loadFromTextFile: The matrix in the text file does not match fixed matrix size");
352 if (Derived::ColsAtCompileTime==Eigen::Dynamic && nRows>0 && Index(i)!=cols() )
353 throw std::runtime_error(
"loadFromTextFile: The matrix in the text file does not have the same number of columns in all rows");
356 if ( Derived::RowsAtCompileTime==Eigen::Dynamic || Derived::ColsAtCompileTime==Eigen::Dynamic )
358 else if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRows)>=Derived::RowsAtCompileTime)
359 throw std::runtime_error(
"loadFromTextFile: Read more rows than the capacity of the fixed sized matrix.");
361 for (
size_t q=0;q<i;q++)
362 coeffRef(nRows,q) = Scalar(fil[q]);
370 throw std::runtime_error(
"loadFromTextFile: Error loading from text file");
374 #endif // guard define