diff --git a/gtsam/discrete/DecisionTreeFactor.cpp b/gtsam/discrete/DecisionTreeFactor.cpp index 2f2c039a4..6e25c6452 100644 --- a/gtsam/discrete/DecisionTreeFactor.cpp +++ b/gtsam/discrete/DecisionTreeFactor.cpp @@ -81,12 +81,18 @@ namespace gtsam { DiscreteFactor::shared_ptr DecisionTreeFactor::operator/( const DiscreteFactor::shared_ptr& f) const { if (auto tf = std::dynamic_pointer_cast(f)) { + // Check if `f` is a TableFactor. If yes, then + // convert `this` to a TableFactor which is cheaper. return std::make_shared(tf->operator/(TableFactor(*this))); + } else if (auto dtf = std::dynamic_pointer_cast(f)) { + // If `f` is a DecisionTreeFactor, divide normally. return std::make_shared(this->operator/(*dtf)); + } else { + // Else, convert `f` to a DecisionTreeFactor so we can divide return std::make_shared( - this->operator/(this->toDecisionTreeFactor())); + this->operator/(f->toDecisionTreeFactor())); } }