From 713c49c9153f9f023621a6cd9a06997594ea52ab Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Sun, 5 Jan 2025 15:20:09 -0500 Subject: [PATCH] more robust product --- gtsam/discrete/DiscreteFactorGraph.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index 0444f47ae..a2b896286 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -65,9 +65,16 @@ namespace gtsam { /* ************************************************************************ */ DecisionTreeFactor DiscreteFactorGraph::product() const { - DiscreteFactor::shared_ptr result = *this->begin(); - for (auto it = this->begin() + 1; it != this->end(); ++it) { - if (*it) result = result->multiply(*it); + DiscreteFactor::shared_ptr result; + for (auto it = this->begin(); it != this->end(); ++it) { + if (*it) { + if (result) { + result = result->multiply(*it); + } else { + // Assign to the first non-null factor + result = *it; + } + } } return result->toDecisionTreeFactor(); }