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;
70 eVals.resize(evals.size(),evals.size());
72 eVals.diagonal()=evals;
78 template <
class Derived>
79 template <
class MATRIX1,
class VECTOR1>
82 Eigen::EigenSolver< Derived > es(*
this,
true);
83 if (es.info()!=Eigen::Success)
85 eVecs = es.eigenvectors().real();
86 eVals = es.eigenvalues().real();
89 std::vector<std::pair<Scalar,Index> > D;
90 D.reserve(eVals.size());
91 for (Index i=0;i<eVals.size();i++)
92 D.push_back(std::pair<Scalar,Index>(eVals.coeff(i,0),i));
93 std::sort(D.begin(),D.end());
95 sortedEigs.resizeLike(eVecs);
96 for (
int i=0;i<eVals.size();i++)
98 eVals.coeffRef(i,0)=D[i].first;
99 sortedEigs.col(i)=eVecs.col(D[i].second);
107 template <
class Derived>
108 template <
class MATRIX1,
class MATRIX2>
111 Matrix<Scalar,Dynamic,1> evals;
113 eVals.resize(evals.size(),evals.size());
115 eVals.diagonal()=evals;
120 template <
class Derived>
121 template <
class MATRIX1,
class VECTOR1>
125 Eigen::SelfAdjointEigenSolver<Derived> eigensolver(*
this);
126 eVecs = eigensolver.eigenvectors();
127 eVals = eigensolver.eigenvalues();
131 template <
class Derived>
135 if ( Derived::RowsAtCompileTime==Eigen::Dynamic)
139 size_t ini =
s.find_first_not_of(
" \t\r\n");
140 if (ini==std::string::npos ||
s[ini]!=
'[') {
return false; }
142 size_t end =
s.find_last_not_of(
" \t\r\n");
143 if (
end==std::string::npos ||
s[
end]!=
']')
return false;
145 if (ini>
end)
return false;
147 std::vector<Scalar> lstElements;
155 size_t end_row =
s.find_first_of(
";]",i);
156 if (end_row==std::string::npos) {
return false; }
159 std::stringstream ss(
s.substr(i, end_row-i ));
167 if (ss.bad() || ss.fail())
break;
168 lstElements.push_back(
val);
173 if (lstElements.empty())
180 if ( Derived::RowsAtCompileTime==Eigen::Dynamic )
186 const size_t N = lstElements.size();
189 if ((nRow>0 &&
size_t(cols())!=N) ||
190 (nRow==0 && Derived::ColsAtCompileTime!=Eigen::Dynamic && Derived::ColsAtCompileTime!=
int(N)) )
192 if (dump_errors_here)
193 (*dump_errors_here) <<
"[fromMatlabStringFormat] Row " << nRow+1 <<
" has invalid number of columns.\n";
198 if ( Derived::RowsAtCompileTime==Eigen::Dynamic || Derived::ColsAtCompileTime==Eigen::Dynamic )
200 else if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRow)>=Derived::RowsAtCompileTime)
202 if (dump_errors_here)
203 (*dump_errors_here) <<
"[fromMatlabStringFormat] Read more rows than the capacity of the fixed sized matrix.\n";
207 for (
size_t q=0;
q<N;
q++)
208 coeffRef(nRow,
q) = lstElements[
q];
217 if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRow)!=Derived::RowsAtCompileTime)
219 if (dump_errors_here)
220 (*dump_errors_here) <<
"[fromMatlabStringFormat] Read less rows than the capacity of the fixed sized matrix.\n";
226 template <
class Derived>
230 s <<
"[" << std::scientific;
231 s.precision(decimal_digits);
232 for (Index i=0;i<rows();i++)
234 for (Index j=0;j<cols();j++)
235 s << coeff(i,j) <<
" ";
236 if (i<rows()-1)
s <<
";";
242 template <
class Derived>
246 bool appendMRPTHeader,
250 #if defined(_MSC_VER) && (_MSC_VER>=1400) // Use a secure version in Visual Studio 2005+ 252 if (0!=::fopen_s(&f,file.c_str(),
"wt")) f= NULL;
254 FILE *f=
::fopen(file.c_str(),
"wt");
257 throw std::runtime_error(
std::string(
"saveToTextFile: Error opening file ")+file+
std::string(
"' for writing a matrix as text."));
259 if (!userHeader.empty())
260 fprintf(f,
"%s",userHeader.c_str() );
262 if (appendMRPTHeader)
266 #if defined(_MSC_VER) && (_MSC_VER>=1400) // Use a secure version in Visual Studio 2005+ 267 struct tm timeinfo_data;
268 struct tm * timeinfo;
269 if (0!=::localtime_s(&timeinfo_data,&rawtime)) timeinfo=NULL;
270 else timeinfo = &timeinfo_data;
272 struct tm * timeinfo = ::localtime(&rawtime);
275 #if defined(_MSC_VER) && (_MSC_VER>=1400) // Use a secure version in Visual Studio 2005+ 276 char strTimeBuf[100];
277 if (0!=asctime_s(strTimeBuf,
sizeof(strTimeBuf),timeinfo)) strTimeBuf[0]=
'\0';
278 char *strTime = &strTimeBuf[0];
280 char *strTime = asctime(timeinfo);
282 fprintf(f,
"%% File generated with %s at %s\n%%-----------------------------------------------------------------\n",
287 for (Index i=0; i < rows(); i++)
289 for (Index j=0; j < cols(); j++)
297 throw std::runtime_error(
"Unsupported value for the parameter 'fileFormat'!");
308 template <
class Derived>
311 std::ifstream f(file.c_str());
312 if (f.fail())
throw std::runtime_error(
std::string(
"loadFromTextFile: can't open file:") + file);
316 template <
class Derived>
321 std::vector<double> fil(512);
323 while ( !f.eof() && !f.fail() )
326 if (str.size() && str[0]!=
'#' && str[0]!=
'%')
329 const char *ptr = str.c_str();
333 while ( ptr[0] && ptr!=ptrEnd )
336 while (ptr[0] && (ptr[0]==
' ' || ptr[0]==
',' || ptr[0]==
'\t' || ptr[0]==
'\r' || ptr[0]==
'\n'))
338 if (fil.size()<=i) fil.resize(fil.size()+ (fil.size()>>1));
340 fil[i] = strtod(ptr,&ptrEnd);
351 if ((Derived::ColsAtCompileTime!=Eigen::Dynamic && Index(i)!=Derived::ColsAtCompileTime) )
352 throw std::runtime_error(
"loadFromTextFile: The matrix in the text file does not match fixed matrix size");
353 if (Derived::ColsAtCompileTime==Eigen::Dynamic && nRows>0 && Index(i)!=cols() )
354 throw std::runtime_error(
"loadFromTextFile: The matrix in the text file does not have the same number of columns in all rows");
357 if ( Derived::RowsAtCompileTime==Eigen::Dynamic || Derived::ColsAtCompileTime==Eigen::Dynamic )
359 if (rows()<static_cast<int>(nRows+1) || cols()<static_cast<int>(i))
361 const size_t extra_rows = std::max(static_cast<size_t>(1), nRows >> 1 );
365 else if (Derived::RowsAtCompileTime!=Eigen::Dynamic &&
int(nRows)>=Derived::RowsAtCompileTime)
366 throw std::runtime_error(
"loadFromTextFile: Read more rows than the capacity of the fixed sized matrix.");
368 for (
size_t q=0;
q<i;
q++)
376 if ( Derived::RowsAtCompileTime==Eigen::Dynamic || Derived::ColsAtCompileTime==Eigen::Dynamic )
380 if (!nRows)
throw std::runtime_error(
"loadFromTextFile: Error loading from text file");
384 #endif // guard define FILE BASE_IMPEXP * fopen(const char *fileName, const char *mode) MRPT_NO_THROWS
An OS-independent version of fopen.
std::string inMatlabFormat(const size_t decimal_digits=6) const
Dump matrix in matlab format.
engineering format 'e'
EIGEN_STRONG_INLINE bool eigenVectors(MATRIX1 &eVecs, MATRIX2 &eVals) const
[For square matrices only] Compute the eigenvectors and eigenvalues (sorted), both returned as matric...
int BASE_IMPEXP void BASE_IMPEXP fclose(FILE *f)
An OS-independent version of fclose.
GLdouble GLdouble GLdouble GLdouble q
int BASE_IMPEXP fprintf(FILE *fil, const char *format,...) MRPT_NO_THROWS MRPT_printf_format_check(2
An OS-independent version of fprintf.
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...
EIGEN_STRONG_INLINE void eigenVectorsSymmetricVec(MATRIX1 &eVecs, VECTOR1 &eVals) const
[For symmetric matrices only] Compute the eigenvectors and eigenvalues (in no particular order)...
EIGEN_STRONG_INLINE bool eigenVectorsVec(MATRIX1 &eVecs, VECTOR1 &eVals) const
[For square matrices only] Compute the eigenvectors and eigenvalues (sorted), eigenvectors are the co...
static void doit(Eigen::Matrix< S, 1, 1, Opt, MaxR, MaxC > &mat, size_t, size_t new_cols)
EIGEN_STRONG_INLINE void eigenVectorsSymmetric(MATRIX1 &eVecs, MATRIX2 &eVals) const
[For symmetric matrices only] Compute the eigenvectors and eigenvalues (in no particular order)...
static void doit(Eigen::Matrix< S, R, 1, Opt, MaxR, MaxC > &mat, size_t new_rows, size_t)
GLsizei const GLchar ** string
void loadFromTextFile(const std::string &file)
Load matrix from a text file, compatible with MATLAB text format.
Internal resize which compiles to nothing on fixed-size matrices.
static void doit(Eigen::Matrix< S, R, C, Opt, MaxR, MaxC > &mat, size_t new_rows, size_t new_cols)
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...
static void doit(Eigen::Matrix< S, 1, C, Opt, MaxR, MaxC > &mat, size_t, size_t new_cols)
std::string BASE_IMPEXP MRPT_getVersion()
Returns a string describing the MRPT version.
fixed floating point 'f'