diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index b3029111a..0444f47ae 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -65,23 +65,11 @@ namespace gtsam { /* ************************************************************************ */ DecisionTreeFactor DiscreteFactorGraph::product() const { - // PRODUCT: multiply all factors - gttic(product); - DiscreteFactor::shared_ptr product = *this->begin(); + DiscreteFactor::shared_ptr result = *this->begin(); for (auto it = this->begin() + 1; it != this->end(); ++it) { - if (*it) product = product->multiply(*it); + if (*it) result = result->multiply(*it); } - 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; + return result->toDecisionTreeFactor(); } /* ************************************************************************ */ @@ -123,12 +111,34 @@ 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 = factors.product(); + DecisionTreeFactor product = DiscreteProduct(factors); // max out frontals, this is the factor on the separator gttic(max); @@ -206,7 +216,7 @@ namespace gtsam { std::pair // EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& frontalKeys) { - DecisionTreeFactor product = factors.product(); + DecisionTreeFactor product = DiscreteProduct(factors); // sum out frontals, this is the factor on the separator gttic(sum);