diff --git a/gtsam/base/Matrix.cpp b/gtsam/base/Matrix.cpp index 2b72a55e4..644a437fa 100644 --- a/gtsam/base/Matrix.cpp +++ b/gtsam/base/Matrix.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -727,6 +728,44 @@ Matrix Cayley(const Matrix& A) { // inlined to let Eigen do more optimization return (Matrix::Identity(n, n) - A)*(Matrix::Identity(n, n) + A).inverse(); } + /* ************************************************************************* */ +std::string formatMatrixIndented(const std::string& label, const Matrix& matrix, bool makeVectorHorizontal) +{ + stringstream ss; + const string firstline = label; + ss << firstline; + const string padding(firstline.size(), ' '); + const bool transposeMatrix = makeVectorHorizontal && matrix.cols() == 1 && matrix.rows() > 1; + const DenseIndex effectiveRows = transposeMatrix ? matrix.cols() : matrix.rows(); + + if(matrix.rows() > 0 && matrix.cols() > 0) + { + stringstream matrixPrinted; + if(transposeMatrix) + matrixPrinted << matrix.transpose(); + else + matrixPrinted << matrix; + const std::string matrixStr = matrixPrinted.str(); + boost::tokenizer > tok(matrixStr, boost::char_separator("\n")); + + DenseIndex row = 0; + BOOST_FOREACH(const std::string& line, tok) + { + assert(row < effectiveRows); + if(row > 0) + ss << padding; + ss << "[ " << line << " ]"; + if(row < effectiveRows - 1) + ss << "\n"; + ++ row; + } + } else { + ss << "Empty (" << matrix.rows() << "x" << matrix.cols() << ")"; + } + return ss.str(); +} + + } // namespace gtsam diff --git a/gtsam/base/Matrix.h b/gtsam/base/Matrix.h index 6b1beb304..1dc49022a 100644 --- a/gtsam/base/Matrix.h +++ b/gtsam/base/Matrix.h @@ -486,6 +486,8 @@ Eigen::Matrix Cayley(const Eigen::Matrix& A) { return (FMat::Identity() - A)*(FMat::Identity() + A).inverse(); } +std::string formatMatrixIndented(const std::string& label, const Matrix& matrix, bool makeVectorHorizontal = false); + } // namespace gtsam #include