From 2e4ae8ca0a5c520c9072bf0a6bbc9563e349d82c Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Tue, 28 Jan 2025 22:08:17 -0500 Subject: [PATCH] add explanatory comments --- gtsam/discrete/DiscreteFactorGraph.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index 321ec7147c..f2bae4b9bf 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -215,6 +215,7 @@ namespace gtsam { EliminateDiscrete(const DiscreteFactorGraph& factors, const Ordering& frontalKeys) { gttic(product); + // `product` is scaled later to prevent underflow. DiscreteFactor::shared_ptr product = factors.product(); gttoc(product); @@ -222,8 +223,11 @@ namespace gtsam { gttic(sum); DiscreteFactor::shared_ptr sum = product->sum(frontalKeys); gttoc(sum); - + // Normalize/scale to prevent underflow. + // We divide both `product` and `sum` by `max(sum)` + // since it is faster to compute and when the conditional + // is formed by `product/sum`, the scaling term cancels out. gttic(scale); DiscreteFactor::shared_ptr denominator = sum->max(sum->size()); product = product->operator/(denominator);