From 9f4ab83cee14db7f5784a0f18930310e491ee715 Mon Sep 17 00:00:00 2001 From: Varun Agrawal Date: Tue, 13 May 2025 22:45:17 -0400 Subject: [PATCH] separate DiscreteMarginals into .h and .cpp --- gtsam/discrete/DiscreteMarginals.cpp | 58 ++++++++++++++++++++++++++++ gtsam/discrete/DiscreteMarginals.h | 28 +++----------- 2 files changed, 63 insertions(+), 23 deletions(-) create mode 100644 gtsam/discrete/DiscreteMarginals.cpp diff --git a/gtsam/discrete/DiscreteMarginals.cpp b/gtsam/discrete/DiscreteMarginals.cpp new file mode 100644 index 000000000..d7cc79a5c --- /dev/null +++ b/gtsam/discrete/DiscreteMarginals.cpp @@ -0,0 +1,58 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file DiscreteMarginals.cpp + * @brief A class for computing marginals in a DiscreteFactorGraph + * @author Abhijit Kundu + * @author Richard Roberts + * @author Varun Agrawal + * @author Frank Dellaert + * @date June 4, 2012 + */ + +#pragma once + +#include +#include +#include + +namespace gtsam { + +/* ************************************************************************* */ +DiscreteMarginals::DiscreteMarginals(const DiscreteFactorGraph& graph) { + bayesTree_ = graph.eliminateMultifrontal(); +} + +/* ************************************************************************* */ +DiscreteFactor::shared_ptr DiscreteMarginals::operator()(Key variable) const { + // Compute marginal + DiscreteFactor::shared_ptr marginalFactor = + bayesTree_->marginalFactor(variable, &EliminateDiscrete); + return marginalFactor; +} + +/* ************************************************************************* */ +Vector DiscreteMarginals::marginalProbabilities(const DiscreteKey& key) const { + // Compute marginal + DiscreteFactor::shared_ptr marginalFactor = this->operator()(key.first); + + // Create result + Vector vResult(key.second); + for (size_t state = 0; state < key.second; ++state) { + DiscreteValues values; + values[key.first] = state; + vResult(state) = (*marginalFactor)(values); + } + return vResult; +} + +} /* namespace gtsam */ diff --git a/gtsam/discrete/DiscreteMarginals.h b/gtsam/discrete/DiscreteMarginals.h index 62c80e657..fd6c93fe4 100644 --- a/gtsam/discrete/DiscreteMarginals.h +++ b/gtsam/discrete/DiscreteMarginals.h @@ -14,6 +14,7 @@ * @brief A class for computing marginals in a DiscreteFactorGraph * @author Abhijit Kundu * @author Richard Roberts + * @author Varun Agrawal * @author Frank Dellaert * @date June 4, 2012 */ @@ -38,38 +39,19 @@ class DiscreteMarginals { DiscreteMarginals() {} /** Construct a marginals class. - * @param graph The factor graph defining the full joint + * @param graph The factor graph defining the full joint * distribution on all variables. */ - DiscreteMarginals(const DiscreteFactorGraph& graph) { - bayesTree_ = graph.eliminateMultifrontal(); - } + DiscreteMarginals(const DiscreteFactorGraph& graph); /** Compute the marginal of a single variable */ - DiscreteFactor::shared_ptr operator()(Key variable) const { - // Compute marginal - DiscreteFactor::shared_ptr marginalFactor = - bayesTree_->marginalFactor(variable, &EliminateDiscrete); - return marginalFactor; - } + DiscreteFactor::shared_ptr operator()(Key variable) const; /** Compute the marginal of a single variable * @param key DiscreteKey of the Variable * @return Vector of marginal probabilities */ - Vector marginalProbabilities(const DiscreteKey& key) const { - // Compute marginal - DiscreteFactor::shared_ptr marginalFactor = this->operator()(key.first); - - // Create result - Vector vResult(key.second); - for (size_t state = 0; state < key.second; ++state) { - DiscreteValues values; - values[key.first] = state; - vResult(state) = (*marginalFactor)(values); - } - return vResult; - } + Vector marginalProbabilities(const DiscreteKey& key) const; }; } /* namespace gtsam */