From 4ff70141f8208db49f9deab3b51222eb66f63874 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Tue, 31 Dec 2024 00:10:37 -0500 Subject: [PATCH] use a TableFactor as the underlying data representation for DiscreteTableConditional since it provides a clean abstraction --- gtsam/discrete/DiscreteTableConditional.cpp | 17 ++++++----------- gtsam/discrete/DiscreteTableConditional.h | 7 +++++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/gtsam/discrete/DiscreteTableConditional.cpp b/gtsam/discrete/DiscreteTableConditional.cpp index b09e2738f..a4fdcef5d 100644 --- a/gtsam/discrete/DiscreteTableConditional.cpp +++ b/gtsam/discrete/DiscreteTableConditional.cpp @@ -41,23 +41,21 @@ namespace gtsam { DiscreteTableConditional::DiscreteTableConditional(const size_t nrFrontals, const TableFactor& f) : BaseConditional(nrFrontals, DecisionTreeFactor(f.discreteKeys(), ADT())), - sparse_table_((f / (*f.sum(nrFrontals))).sparseTable()) { - // sparse_table_ = sparse_table_.prune(); -} + table_(f / (*f.sum(nrFrontals))) {} /* ************************************************************************** */ DiscreteTableConditional::DiscreteTableConditional( size_t nrFrontals, const DiscreteKeys& keys, const Eigen::SparseVector& potentials) : BaseConditional(nrFrontals, keys, DecisionTreeFactor(keys, ADT())), - sparse_table_(potentials) {} + table_(TableFactor(keys, potentials)) {} /* ************************************************************************** */ DiscreteTableConditional::DiscreteTableConditional(const TableFactor& joint, const TableFactor& marginal) : BaseConditional(joint.size() - marginal.size(), joint.discreteKeys() & marginal.discreteKeys(), ADT()), - sparse_table_((joint / marginal).sparseTable()) {} + table_(joint / marginal) {} /* ************************************************************************** */ DiscreteTableConditional::DiscreteTableConditional(const TableFactor& joint, @@ -71,8 +69,7 @@ DiscreteTableConditional::DiscreteTableConditional(const TableFactor& joint, /* ************************************************************************** */ DiscreteTableConditional::DiscreteTableConditional(const Signature& signature) : BaseConditional(1, DecisionTreeFactor()), - sparse_table_(TableFactor(signature.discreteKeys(), signature.cpt()) - .sparseTable()) {} + table_(TableFactor(signature.discreteKeys(), signature.cpt())) {} /* ************************************************************************** */ DiscreteTableConditional DiscreteTableConditional::operator*( @@ -108,9 +105,7 @@ DiscreteTableConditional DiscreteTableConditional::operator*( // Finally, add parents to keys, in order for (auto&& dk : parents) discreteKeys.push_back(dk); - TableFactor a(this->discreteKeys(), this->sparse_table_), - b(other.discreteKeys(), other.sparse_table_); - TableFactor product = a * other; + TableFactor product = this->table_ * other.table(); return DiscreteTableConditional(newFrontals.size(), product); } @@ -128,7 +123,7 @@ void DiscreteTableConditional::print(const string& s, } } cout << "):\n"; - // BaseFactor::print("", formatter); + table_.print("", formatter); cout << endl; } diff --git a/gtsam/discrete/DiscreteTableConditional.h b/gtsam/discrete/DiscreteTableConditional.h index 28e35277d..fae0c0761 100644 --- a/gtsam/discrete/DiscreteTableConditional.h +++ b/gtsam/discrete/DiscreteTableConditional.h @@ -29,13 +29,16 @@ namespace gtsam { /** - * Discrete Conditional Density which uses a SparseTable as the internal + * Discrete Conditional Density which uses a SparseVector as the internal * representation, similar to the TableFactor. * * @ingroup discrete */ class GTSAM_EXPORT DiscreteTableConditional : public DiscreteConditional { - Eigen::SparseVector sparse_table_; + private: + TableFactor table_; + + typedef Eigen::SparseVector::InnerIterator SparseIt; public: // typedefs needed to play nice with gtsam