separate DiscreteMarginals into .h and .cpp

release/4.3a0
Varun Agrawal 2025-05-13 22:45:17 -04:00
parent 6d30fe0e9e
commit 9f4ab83cee
2 changed files with 63 additions and 23 deletions

View File

@ -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 <gtsam/base/Vector.h>
#include <gtsam/discrete/DiscreteBayesTree.h>
#include <gtsam/discrete/DiscreteFactorGraph.h>
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 */

View File

@ -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 */