diff --git a/gtsam/discrete/DiscreteConditional.cpp b/gtsam/discrete/DiscreteConditional.cpp index 7ed962188..b02d09575 100644 --- a/gtsam/discrete/DiscreteConditional.cpp +++ b/gtsam/discrete/DiscreteConditional.cpp @@ -229,11 +229,22 @@ std::string DiscreteConditional::_repr_markdown_( // Print out signature. ss << " $P("; - for(Key key: frontals()) - ss << keyFormatter(key); - if (nrParents() > 0) - ss << "|"; bool first = true; + for (Key key : frontals()) { + if (!first) ss << ","; + ss << keyFormatter(key); + first = false; + } + if (nrParents() == 0) { + // We have no parents, call factor method. + ss << ")$:" << std::endl; + ss << DecisionTreeFactor::_repr_markdown_(); + return ss.str(); + } + + // We have parents, continue signature and do custom print. + ss << "|"; + first = true; for (Key parent : parents()) { if (!first) ss << ","; ss << keyFormatter(parent); @@ -256,9 +267,8 @@ std::string DiscreteConditional::_repr_markdown_( pairs.emplace_back(key, k); n *= k; } - size_t nrParents = size() - nrFrontals_; std::vector> slatnorf(pairs.rbegin(), - pairs.rend() - nrParents); + pairs.rend() - nrParents()); const auto frontal_assignments = cartesianProduct(slatnorf); for (const auto& a : frontal_assignments) { for (it = beginFrontals(); it != endFrontals(); ++it) ss << a.at(*it); @@ -268,7 +278,7 @@ std::string DiscreteConditional::_repr_markdown_( // Print out separator with alignment hints. ss << "|"; - for (size_t j = 0; j < nrParents + n; j++) ss << ":-:|"; + for (size_t j = 0; j < nrParents() + n; j++) ss << ":-:|"; ss << "\n"; // Print out all rows. diff --git a/gtsam/discrete/tests/testDiscreteBayesNet.cpp b/gtsam/discrete/tests/testDiscreteBayesNet.cpp index 0a3c5d6e1..827b7d248 100644 --- a/gtsam/discrete/tests/testDiscreteBayesNet.cpp +++ b/gtsam/discrete/tests/testDiscreteBayesNet.cpp @@ -181,9 +181,10 @@ TEST(DiscreteBayesNet, markdown) { "`DiscreteBayesNet` of size 2\n" "\n" " $P(Asia)$:\n" - "|0|1|\n" + "|0|value|\n" "|:-:|:-:|\n" - "|0.99|0.01|\n" + "|0|0.99|\n" + "|1|0.01|\n" "\n" " $P(Smoking|Asia)$:\n" "|Asia|0|1|\n" diff --git a/gtsam/discrete/tests/testDiscreteConditional.cpp b/gtsam/discrete/tests/testDiscreteConditional.cpp index 698268e84..964a33926 100644 --- a/gtsam/discrete/tests/testDiscreteConditional.cpp +++ b/gtsam/discrete/tests/testDiscreteConditional.cpp @@ -110,13 +110,15 @@ TEST(DiscreteConditional, Combine) { /* ************************************************************************* */ // Check markdown representation looks as expected, no parents. TEST(DiscreteConditional, markdown_prior) { - DiscreteKey A(Symbol('x', 1), 2); - DiscreteConditional conditional(A % "1/3"); + DiscreteKey A(Symbol('x', 1), 3); + DiscreteConditional conditional(A % "1/2/2"); string expected = " $P(x1)$:\n" - "|0|1|\n" + "|x1|value|\n" "|:-:|:-:|\n" - "|0.25|0.75|\n"; + "|0|0.2|\n" + "|1|0.4|\n" + "|2|0.4|\n"; string actual = conditional._repr_markdown_(); EXPECT(actual == expected); }