From a7e7977c5ff12e0814ca6ce140606788e26224cf Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Fri, 25 Mar 2022 16:47:28 -0400 Subject: [PATCH 1/4] Improve printing --- gtsam/linear/GaussianConditional.cpp | 15 +++++++++----- gtsam/linear/GaussianConditional.h | 4 ++-- .../linear/tests/testGaussianConditional.cpp | 20 ++++++++++++++----- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/gtsam/linear/GaussianConditional.cpp b/gtsam/linear/GaussianConditional.cpp index c44ab246b1..020a24be5a 100644 --- a/gtsam/linear/GaussianConditional.cpp +++ b/gtsam/linear/GaussianConditional.cpp @@ -89,15 +89,20 @@ namespace gtsam { /* ************************************************************************ */ void GaussianConditional::print(const string &s, const KeyFormatter& formatter) const { - cout << s << " p("; + cout << s << "GaussianConditional p("; for (const_iterator it = beginFrontals(); it != endFrontals(); ++it) { - cout << (boost::format("%1%")%(formatter(*it))).str() << " "; + cout << (boost::format("%1%") % (formatter(*it))).str() + << (nrFrontals() > 1 ? " " : ""); } - cout << "|"; - for (const_iterator it = beginParents(); it != endParents(); ++it) { - cout << " " << (boost::format("%1%")%(formatter(*it))).str(); + + if (nrParents()) { + cout << " |"; + for (const_iterator it = beginParents(); it != endParents(); ++it) { + cout << " " << (boost::format("%1%") % (formatter(*it))).str(); + } } cout << ")" << endl; + cout << formatMatrixIndented(" R = ", R()) << endl; for (const_iterator it = beginParents() ; it != endParents() ; ++it) { cout << formatMatrixIndented((boost::format(" S[%1%] = ")%(formatter(*it))).str(), getA(it)) diff --git a/gtsam/linear/GaussianConditional.h b/gtsam/linear/GaussianConditional.h index 6dd278536a..e8aae4c31a 100644 --- a/gtsam/linear/GaussianConditional.h +++ b/gtsam/linear/GaussianConditional.h @@ -109,8 +109,8 @@ namespace gtsam { /// @{ /** print */ - void print(const std::string& = "GaussianConditional", - const KeyFormatter& formatter = DefaultKeyFormatter) const override; + void print(const std::string& = "", const KeyFormatter& formatter = + DefaultKeyFormatter) const override; /** equals function */ bool equals(const GaussianFactor&cg, double tol = 1e-9) const override; diff --git a/gtsam/linear/tests/testGaussianConditional.cpp b/gtsam/linear/tests/testGaussianConditional.cpp index 4a95152079..6d4a74abcc 100644 --- a/gtsam/linear/tests/testGaussianConditional.cpp +++ b/gtsam/linear/tests/testGaussianConditional.cpp @@ -404,13 +404,23 @@ TEST(GaussianConditional, Print) { const Vector2 b(20, 40); const double sigma = 3; - std::string s = "GaussianConditional"; + GaussianConditional conditional(X(0), b, Matrix2::Identity(), + noiseModel::Isotropic::Sigma(2, sigma)); - auto conditional = + // Test printing for single parent. + std::string expected = + "GaussianConditional p(x0)\n" + " R = [ 1 0 ]\n" + " [ 0 1 ]\n" + " d = [ 20 40 ]\n" + "isotropic dim=2 sigma=3\n"; + EXPECT(assert_print_equal(expected, conditional)); + + auto conditional1 = GaussianConditional::FromMeanAndStddev(X(0), A1, X(1), b, sigma); // Test printing for single parent. - std::string expected = + std::string expected1 = "GaussianConditional p(x0 | x1)\n" " R = [ 1 0 ]\n" " [ 0 1 ]\n" @@ -418,7 +428,7 @@ TEST(GaussianConditional, Print) { " [ -3 -4 ]\n" " d = [ 20 40 ]\n" "isotropic dim=2 sigma=3\n"; - EXPECT(assert_print_equal(expected, conditional, s)); + EXPECT(assert_print_equal(expected1, conditional1)); // Test printing for multiple parents. auto conditional2 = GaussianConditional::FromMeanAndStddev(X(0), A1, Y(0), A2, @@ -433,7 +443,7 @@ TEST(GaussianConditional, Print) { " [ -7 -8 ]\n" " d = [ 20 40 ]\n" "isotropic dim=2 sigma=3\n"; - EXPECT(assert_print_equal(expected2, conditional2, s)); + EXPECT(assert_print_equal(expected2, conditional2)); } /* ************************************************************************* */ From 0dfd69fa4af4028672636a77dafdf7c4ab1faf13 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Fri, 25 Mar 2022 16:50:21 -0400 Subject: [PATCH 2/4] update comment in test --- gtsam/linear/tests/testGaussianConditional.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtsam/linear/tests/testGaussianConditional.cpp b/gtsam/linear/tests/testGaussianConditional.cpp index 6d4a74abcc..5fbde44e0d 100644 --- a/gtsam/linear/tests/testGaussianConditional.cpp +++ b/gtsam/linear/tests/testGaussianConditional.cpp @@ -407,7 +407,7 @@ TEST(GaussianConditional, Print) { GaussianConditional conditional(X(0), b, Matrix2::Identity(), noiseModel::Isotropic::Sigma(2, sigma)); - // Test printing for single parent. + // Test printing for no parents. std::string expected = "GaussianConditional p(x0)\n" " R = [ 1 0 ]\n" From 77adcdbab2c0d52f867fa8bae7c09c2380e061a3 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Wed, 30 Mar 2022 16:28:36 -0400 Subject: [PATCH 3/4] undo default print change --- gtsam/linear/GaussianConditional.cpp | 2 +- gtsam/linear/GaussianConditional.h | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/gtsam/linear/GaussianConditional.cpp b/gtsam/linear/GaussianConditional.cpp index 020a24be5a..6199f91a75 100644 --- a/gtsam/linear/GaussianConditional.cpp +++ b/gtsam/linear/GaussianConditional.cpp @@ -89,7 +89,7 @@ namespace gtsam { /* ************************************************************************ */ void GaussianConditional::print(const string &s, const KeyFormatter& formatter) const { - cout << s << "GaussianConditional p("; + cout << s << " p("; for (const_iterator it = beginFrontals(); it != endFrontals(); ++it) { cout << (boost::format("%1%") % (formatter(*it))).str() << (nrFrontals() > 1 ? " " : ""); diff --git a/gtsam/linear/GaussianConditional.h b/gtsam/linear/GaussianConditional.h index e8aae4c31a..b2b616dab7 100644 --- a/gtsam/linear/GaussianConditional.h +++ b/gtsam/linear/GaussianConditional.h @@ -109,8 +109,9 @@ namespace gtsam { /// @{ /** print */ - void print(const std::string& = "", const KeyFormatter& formatter = - DefaultKeyFormatter) const override; + void print( + const std::string& = "GaussianConditional", + const KeyFormatter& formatter = DefaultKeyFormatter) const override; /** equals function */ bool equals(const GaussianFactor&cg, double tol = 1e-9) const override; From 00d8e5610c2f66ba78889c465bf58a5722457ff3 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Wed, 30 Mar 2022 17:39:36 -0400 Subject: [PATCH 4/4] fix assertions --- gtsam/linear/tests/testGaussianConditional.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gtsam/linear/tests/testGaussianConditional.cpp b/gtsam/linear/tests/testGaussianConditional.cpp index 5fbde44e0d..6ec06a0ceb 100644 --- a/gtsam/linear/tests/testGaussianConditional.cpp +++ b/gtsam/linear/tests/testGaussianConditional.cpp @@ -414,7 +414,7 @@ TEST(GaussianConditional, Print) { " [ 0 1 ]\n" " d = [ 20 40 ]\n" "isotropic dim=2 sigma=3\n"; - EXPECT(assert_print_equal(expected, conditional)); + EXPECT(assert_print_equal(expected, conditional, "GaussianConditional")); auto conditional1 = GaussianConditional::FromMeanAndStddev(X(0), A1, X(1), b, sigma); @@ -428,7 +428,7 @@ TEST(GaussianConditional, Print) { " [ -3 -4 ]\n" " d = [ 20 40 ]\n" "isotropic dim=2 sigma=3\n"; - EXPECT(assert_print_equal(expected1, conditional1)); + EXPECT(assert_print_equal(expected1, conditional1, "GaussianConditional")); // Test printing for multiple parents. auto conditional2 = GaussianConditional::FromMeanAndStddev(X(0), A1, Y(0), A2, @@ -443,7 +443,7 @@ TEST(GaussianConditional, Print) { " [ -7 -8 ]\n" " d = [ 20 40 ]\n" "isotropic dim=2 sigma=3\n"; - EXPECT(assert_print_equal(expected2, conditional2)); + EXPECT(assert_print_equal(expected2, conditional2, "GaussianConditional")); } /* ************************************************************************* */