From 453059bd61f4b08acdc43c37b9b098a5579d36a3 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Sun, 5 Jan 2025 14:03:08 -0500 Subject: [PATCH] simplify to remove DiscreteProduct static function --- gtsam/discrete/DiscreteFactorGraph.cpp | 44 ++++++++++---------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index 0444f47ae..b3029111a 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -65,11 +65,23 @@ namespace gtsam { /* ************************************************************************ */ DecisionTreeFactor DiscreteFactorGraph::product() const { - DiscreteFactor::shared_ptr result = *this->begin(); + // PRODUCT: multiply all factors + gttic(product); + DiscreteFactor::shared_ptr product = *this->begin(); for (auto it = this->begin() + 1; it != this->end(); ++it) { - if (*it) result = result->multiply(*it); + if (*it) product = product->multiply(*it); } - return result->toDecisionTreeFactor(); + gttoc(product); + + DecisionTreeFactor = result->toDecisionTreeFactor(); + + // Max over all the potentials by pretending all keys are frontal: + auto denominator = product.max(product.size()); + + // Normalize the product factor to prevent underflow. + product = product / (*denominator); + + return product; } /* ************************************************************************ */ @@ -111,34 +123,12 @@ namespace gtsam { // } // } - /** - * @brief Multiply all the `factors`. - * - * @param factors The factors to multiply as a DiscreteFactorGraph. - * @return DecisionTreeFactor - */ - static DecisionTreeFactor DiscreteProduct( - const DiscreteFactorGraph& factors) { - // PRODUCT: multiply all factors - gttic(product); - DecisionTreeFactor product = factors.product(); - gttoc(product); - - // Max over all the potentials by pretending all keys are frontal: - auto denominator = product.max(product.size()); - - // Normalize the product factor to prevent underflow. - product = product / (*denominator); - - return product; - } - /* ************************************************************************ */ // Alternate eliminate function for MPE std::pair // EliminateForMPE(const DiscreteFactorGraph& factors, const Ordering& frontalKeys) { - DecisionTreeFactor product = DiscreteProduct(factors); + DecisionTreeFactor product = factors.product(); // max out frontals, this is the factor on the separator gttic(max); @@ -216,7 +206,7 @@ namespace gtsam { std::pair // EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& frontalKeys) { - DecisionTreeFactor product = DiscreteProduct(factors); + DecisionTreeFactor product = factors.product(); // sum out frontals, this is the factor on the separator gttic(sum);