From 445b5834dc9a90b522e443e639a85ecf8dea02e7 Mon Sep 17 00:00:00 2001 From: dellaert Date: Tue, 17 Feb 2015 00:39:03 +0100 Subject: [PATCH 1/2] Use Eigen::format, now compatible with matlab, and stream precision affects printing. --- gtsam/base/Matrix.cpp | 26 +++++++++++--------------- gtsam/linear/JacobianFactor.cpp | 16 ++++++++++++---- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/gtsam/base/Matrix.cpp b/gtsam/base/Matrix.cpp index 7bcd32b9f..e6eef04d5 100644 --- a/gtsam/base/Matrix.cpp +++ b/gtsam/base/Matrix.cpp @@ -181,21 +181,17 @@ void transposeMultiplyAdd(double alpha, const Matrix& A, const Vector& e, SubVec /* ************************************************************************* */ void print(const Matrix& A, const string &s, ostream& stream) { - size_t m = A.rows(), n = A.cols(); - - // print out all elements - stream << s << "[\n"; - for( size_t i = 0 ; i < m ; i++) { - for( size_t j = 0 ; j < n ; j++) { - double aij = A(i,j); - if(aij != 0.0) - stream << setw(12) << setprecision(9) << aij << ",\t"; - else - stream << " 0.0,\t"; - } - stream << endl; - } - stream << "];" << endl; + static const Eigen::IOFormat matlab( + Eigen::StreamPrecision, // precision + 0, // flags + " ", // coeffSeparator + ";\n", // rowSeparator + " ", // rowPrefix + "", // rowSuffix + "[\n", // matPrefix + "\n ]" // matSuffix + ); + cout << s << A.format(matlab) << endl; } /* ************************************************************************* */ diff --git a/gtsam/linear/JacobianFactor.cpp b/gtsam/linear/JacobianFactor.cpp index eba06f99a..935ed40ae 100644 --- a/gtsam/linear/JacobianFactor.cpp +++ b/gtsam/linear/JacobianFactor.cpp @@ -347,13 +347,21 @@ JacobianFactor::JacobianFactor(const GaussianFactorGraph& graph, /* ************************************************************************* */ void JacobianFactor::print(const string& s, const KeyFormatter& formatter) const { + static const Eigen::IOFormat matlab( + Eigen::StreamPrecision, // precision + 0, // flags + " ", // coeffSeparator + ";\n", // rowSeparator + " ", // rowPrefix + "", // rowSuffix + "[\n", // matPrefix + "\n ]" // matSuffix + ); if (!s.empty()) cout << s << "\n"; for (const_iterator key = begin(); key != end(); ++key) { - cout - << formatMatrixIndented( - (boost::format(" A[%1%] = ") % formatter(*key)).str(), getA(key)) - << endl; + cout << boost::format(" A[%1%] = ") % formatter(*key); + cout << getA(key).format(matlab) << endl; } cout << formatMatrixIndented(" b = ", getb(), true) << "\n"; if (model_) From 0b2d4f11829433f6488b8b0886631a68d69f402a Mon Sep 17 00:00:00 2001 From: lvzhaoyang Date: Thu, 22 Oct 2015 15:04:53 -0400 Subject: [PATCH 2/2] feature: change rowPrefix to an indent. Now all columns are aligned --- gtsam/base/Matrix.cpp | 2 +- gtsam/linear/JacobianFactor.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/gtsam/base/Matrix.cpp b/gtsam/base/Matrix.cpp index 1de27c0a2..3cafdd0ba 100644 --- a/gtsam/base/Matrix.cpp +++ b/gtsam/base/Matrix.cpp @@ -189,7 +189,7 @@ void print(const Matrix& A, const string &s, ostream& stream) { 0, // flags " ", // coeffSeparator ";\n", // rowSeparator - " ", // rowPrefix + " \t", // rowPrefix "", // rowSuffix "[\n", // matPrefix "\n ]" // matSuffix diff --git a/gtsam/linear/JacobianFactor.cpp b/gtsam/linear/JacobianFactor.cpp index f2678d56c..d4df57298 100644 --- a/gtsam/linear/JacobianFactor.cpp +++ b/gtsam/linear/JacobianFactor.cpp @@ -349,11 +349,11 @@ void JacobianFactor::print(const string& s, const KeyFormatter& formatter) const { static const Eigen::IOFormat matlab( Eigen::StreamPrecision, // precision - 0, // flags + 0, // flags " ", // coeffSeparator ";\n", // rowSeparator - " ", // rowPrefix - "", // rowSuffix + "\t", // rowPrefix + "", // rowSuffix "[\n", // matPrefix "\n ]" // matSuffix );