10 #ifndef MRPT_EIGEN_PLUGINS_IMPL_H
11 #define MRPT_EIGEN_PLUGINS_IMPL_H
21 template<
int R,
int C>
24 template <
typename S,
int Opt,
int MaxR,
int MaxC>
25 static inline void doit(Eigen::Matrix<S,R,C,Opt,MaxR,MaxC> &mat,
size_t new_rows,
size_t new_cols)
27 ::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);
34 template <
typename S,
int Opt,
int MaxR,
int MaxC>
35 static inline void doit(Eigen::Matrix<S,R,1,Opt,MaxR,MaxC> &mat,
size_t new_rows,
size_t )
37 ::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);
44 template <
typename S,
int Opt,
int MaxR,
int MaxC>
45 static inline void doit(Eigen::Matrix<S,1,C,Opt,MaxR,MaxC> &mat,
size_t ,
size_t new_cols)
47 ::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);
53 template <
typename S,
int Opt,
int MaxR,
int MaxC>
54 static inline void doit(Eigen::Matrix<S,1,1,Opt,MaxR,MaxC> &mat,
size_t ,
size_t new_cols)
56 ::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);
63 template <
class Derived>
64 template <
class MATRIX1,
class MATRIX2>
67 Matrix<Scalar,Dynamic,1> evals;
69 eVals.resize(evals.size(),evals.size());
71 eVals.diagonal()=evals;
76 template <
class Derived>
77 template <
class MATRIX1,
class VECTOR1>
80 Eigen::EigenSolver< Derived > es(*
this,
true);
81 eVecs = es.eigenvectors().real();
82 eVals = es.eigenvalues().real();
85 std::vector<std::pair<Scalar,Index> > D;
86 D.reserve(eVals.size());
87 for (Index i=0;i<eVals.size();i++)
88 D.push_back(std::pair<Scalar,Index>(eVals.coeff(i,0),i));
89 std::sort(D.begin(),D.end());
91 sortedEigs.resizeLike(eVecs);
92 for (
int i=0;i<eVals.size();i++)
94 eVals.coeffRef(i,0)=D[i].first;
95 sortedEigs.col(i)=eVecs.col(D[i].second);
102 template <
class Derived>
103 template <
class MATRIX1,
class MATRIX2>
106 Matrix<Scalar,Dynamic,1> evals;
108 eVals.resize(evals.size(),evals.size());
110 eVals.diagonal()=evals;
115 template <
class Derived>
116 template <
class MATRIX1,
class VECTOR1>
120 Eigen::SelfAdjointEigenSolver<Derived> eigensolver(*
this);
121 eVecs = eigensolver.eigenvectors();
122 eVals = eigensolver.eigenvalues();
126 template <
class Derived>
130 if ( Derived::RowsAtCompileTime==Eigen::Dynamic)
134 size_t ini = s.find_first_not_of(
" \t\r\n");
135 if (ini==std::string::npos || s[ini]!=
'[') {
return false; }
137 size_t end = s.find_last_not_of(
" \t\r\n");
138 if (
end==std::string::npos || s[
end]!=
']')
return false;
140 if (ini>
end)
return false;
142 std::vector<Scalar> lstElements;
150 size_t end_row = s.find_first_of(
";]",i);
151 if (end_row==std::string::npos) {
return false; }
154 std::stringstream ss(s.substr(i, end_row-i ));
162 if (ss.bad() || ss.fail())
break;
163 lstElements.push_back(val);
168 if (lstElements.empty())
175 if ( Derived::RowsAtCompileTime==Eigen::Dynamic )
181 const size_t N = lstElements.size();
184 if ((nRow>0 &&
size_t(cols())!=N) ||
185 (nRow==0 && Derived::ColsAtCompileTime!=Eigen::Dynamic && Derived::ColsAtCompileTime!=
int(N)) )
187 if (dump_errors_here)
188 (*dump_errors_here) <<
"[fromMatlabStringFormat] Row " << nRow+1 <<
" has invalid number of columns.\n";
193 if ( Derived::RowsAtCompileTime==Eigen::Dynamic || Derived::ColsAtCompileTime==Eigen::Dynamic )
195 else if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRow)>=Derived::RowsAtCompileTime)
197 if (dump_errors_here)
198 (*dump_errors_here) <<
"[fromMatlabStringFormat] Read more rows than the capacity of the fixed sized matrix.\n";
202 for (
size_t q=0;q<N;q++)
203 coeffRef(nRow,q) = lstElements[q];
212 if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRow)!=Derived::RowsAtCompileTime)
214 if (dump_errors_here)
215 (*dump_errors_here) <<
"[fromMatlabStringFormat] Read less rows than the capacity of the fixed sized matrix.\n";
221 template <
class Derived>
225 s <<
"[" << std::scientific;
226 s.precision(decimal_digits);
227 for (Index i=0;i<rows();i++)
229 for (Index j=0;j<cols();j++)
230 s << coeff(i,j) <<
" ";
231 if (i<rows()-1) s <<
";";
237 template <
class Derived>
239 const std::string &file,
241 bool appendMRPTHeader,
242 const std::string &userHeader
245 #if defined(_MSC_VER) && (_MSC_VER>=1400)
247 if (0!=::fopen_s(&f,file.c_str(),
"wt")) f= NULL;
249 FILE *f=
::fopen(file.c_str(),
"wt");
252 throw std::runtime_error(std::string(
"saveToTextFile: Error opening file ")+file+std::string(
"' for writing a matrix as text."));
254 if (!userHeader.empty())
255 fprintf(f,
"%s",userHeader.c_str() );
257 if (appendMRPTHeader)
261 #if defined(_MSC_VER) && (_MSC_VER>=1400)
262 struct tm timeinfo_data;
263 struct tm * timeinfo;
264 if (0!=::localtime_s(&timeinfo_data,&rawtime)) timeinfo=NULL;
265 else timeinfo = &timeinfo_data;
267 struct tm * timeinfo = ::localtime(&rawtime);
270 #if defined(_MSC_VER) && (_MSC_VER>=1400)
271 char strTimeBuf[100];
272 if (0!=asctime_s(strTimeBuf,
sizeof(strTimeBuf),timeinfo)) strTimeBuf[0]=
'\0';
273 char *strTime = &strTimeBuf[0];
275 char *strTime = asctime(timeinfo);
277 fprintf(f,
"%% File generated with %s at %s\n%%-----------------------------------------------------------------\n",
282 for (Index i=0; i < rows(); i++)
284 for (Index j=0; j < cols(); j++)
292 throw std::runtime_error(
"Unsupported value for the parameter 'fileFormat'!");
303 template <
class Derived>
306 std::ifstream f(file.c_str());
307 if (f.fail())
throw std::runtime_error(std::string(
"loadFromTextFile: can't open file:") + file);
311 template <
class Derived>
316 std::vector<double> fil(512);
318 while ( !f.eof() && !f.fail() )
321 if (str.size() && str[0]!=
'#' && str[0]!=
'%')
324 const char *ptr = str.c_str();
328 while ( ptr[0] && ptr!=ptrEnd )
331 while (ptr[0] && (ptr[0]==
' ' || ptr[0]==
',' || ptr[0]==
'\t' || ptr[0]==
'\r' || ptr[0]==
'\n'))
333 if (fil.size()<=i) fil.resize(fil.size()+ (fil.size()>>1));
335 fil[i] = strtod(ptr,&ptrEnd);
346 if ((Derived::ColsAtCompileTime!=Eigen::Dynamic && Index(i)!=Derived::ColsAtCompileTime) )
347 throw std::runtime_error(
"loadFromTextFile: The matrix in the text file does not match fixed matrix size");
348 if (Derived::ColsAtCompileTime==Eigen::Dynamic && nRows>0 && Index(i)!=cols() )
349 throw std::runtime_error(
"loadFromTextFile: The matrix in the text file does not have the same number of columns in all rows");
352 if ( Derived::RowsAtCompileTime==Eigen::Dynamic || Derived::ColsAtCompileTime==Eigen::Dynamic )
354 if (rows()<
static_cast<int>(nRows+1) || cols()<
static_cast<int>(i))
356 const size_t extra_rows = std::max(
static_cast<size_t>(1), nRows >> 1 );
360 else if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRows)>=Derived::RowsAtCompileTime)
361 throw std::runtime_error(
"loadFromTextFile: Read more rows than the capacity of the fixed sized matrix.");
363 for (
size_t q=0;q<i;q++)
364 coeffRef(nRows,q) = Scalar(fil[q]);
371 if ( Derived::RowsAtCompileTime==Eigen::Dynamic || Derived::ColsAtCompileTime==Eigen::Dynamic )
375 if (!nRows)
throw std::runtime_error(
"loadFromTextFile: Error loading from text file");
EIGEN_STRONG_INLINE void eigenVectors(MATRIX1 &eVecs, MATRIX2 &eVals) const
[For square matrices only] Compute the eigenvectors and eigenvalues (sorted), both returned as matric...
void saveToTextFile(const std::string &file, mrpt::math::TMatrixTextFileFormat fileFormat=mrpt::math::MATRIX_FORMAT_ENG, bool appendMRPTHeader=false, const std::string &userHeader=std::string()) const
Save matrix to a text file, compatible with MATLAB text format (see also the methods of matrix classe...
bool fromMatlabStringFormat(const std::string &s, std::ostream *dump_errors_here=NULL)
Read a matrix from a string in Matlab-like format, for example "[1 0 2; 0 4 -1]" The string must star...
EIGEN_STRONG_INLINE void eigenVectorsSymmetric(MATRIX1 &eVecs, MATRIX2 &eVals) const
[For symmetric matrices only] Compute the eigenvectors and eigenvalues (in no particular order),...
EIGEN_STRONG_INLINE void eigenVectorsSymmetricVec(MATRIX1 &eVecs, VECTOR1 &eVals) const
[For symmetric matrices only] Compute the eigenvectors and eigenvalues (in no particular order),...
std::string inMatlabFormat(const size_t decimal_digits=6) const
Dump matrix in matlab format.
void loadFromTextFile(const std::string &file)
Load matrix from a text file, compatible with MATLAB text format.
EIGEN_STRONG_INLINE iterator end()
EIGEN_STRONG_INLINE void eigenVectorsVec(MATRIX1 &eVecs, VECTOR1 &eVals) const
[For square matrices only] Compute the eigenvectors and eigenvalues (sorted), eigenvectors are the co...
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
std::string BASE_IMPEXP MRPT_getVersion()
Returns a string describing the MRPT version.
FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
int BASE_IMPEXP void BASE_IMPEXP fclose(FILE *f)
An OS-independent version of fscanf.
@ MATRIX_FORMAT_ENG
engineering format 'e'
@ MATRIX_FORMAT_FIXED
fixed floating point 'f'
@ MATRIX_FORMAT_INT
intergers 'i'
static void doit(Eigen::Matrix< S, 1, 1, Opt, MaxR, MaxC > &mat, size_t, size_t new_cols)
static void doit(Eigen::Matrix< S, 1, C, Opt, MaxR, MaxC > &mat, size_t, size_t new_cols)
static void doit(Eigen::Matrix< S, R, 1, Opt, MaxR, MaxC > &mat, size_t new_rows, size_t)
static void doit(Eigen::Matrix< S, R, C, Opt, MaxR, MaxC > &mat, size_t new_rows, size_t new_cols)
Internal resize which compiles to nothing on fixed-size matrices.