/** * @file GaussianBayesNet.cpp * @brief Chordal Bayes Net, the result of eliminating a factor graph * @author Frank Dellaert */ #include #include #include #include "GaussianBayesNet.h" #include "VectorConfig.h" using namespace std; using namespace gtsam; // Explicitly instantiate so we don't have to include everywhere #include "BayesNet-inl.h" template class BayesNet; // trick from some reading group #define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL) #define REVERSE_FOREACH_PAIR( KEY, VAL, COL) BOOST_REVERSE_FOREACH (boost::tie(KEY,VAL),COL) /* ************************************************************************* */ GaussianBayesNet::GaussianBayesNet(const string& key, double mu, double sigma) { ConditionalGaussian::shared_ptr conditional(new ConditionalGaussian(key, Vector_(1,mu), eye(1), Vector_(1,sigma))); push_back(conditional); } /* ************************************************************************* */ GaussianBayesNet::GaussianBayesNet(const string& key, const Vector& mu, double sigma) { size_t n = mu.size(); ConditionalGaussian::shared_ptr conditional(new ConditionalGaussian(key, mu, eye(n), repeat(n,sigma))); push_back(conditional); } /* ************************************************************************* */ boost::shared_ptr GaussianBayesNet::optimize() const { boost::shared_ptr result(new VectorConfig); /** solve each node in turn in topological sort order (parents first)*/ BOOST_REVERSE_FOREACH(ConditionalGaussian::shared_ptr cg,conditionals_) { Vector x = cg->solve(*result); // Solve for that variable result->insert(cg->key(),x); // store result in partial solution } return result; } /* ************************************************************************* */ pair GaussianBayesNet::matrix() const { // add the dimensions of all variables to get matrix dimension // and at the same time create a mapping from keys to indices size_t N=0; map mapping; BOOST_FOREACH(ConditionalGaussian::shared_ptr cg,conditionals_) { mapping.insert(make_pair(cg->key(),N)); N += cg->dim(); } // create matrix and copy in values Matrix R = zeros(N,N); Vector d(N); string key; size_t I; FOREACH_PAIR(key,I,mapping) { // find corresponding conditional ConditionalGaussian::shared_ptr cg = (*this)[key]; // get RHS and copy to d const Vector& d_ = cg->get_d(); const size_t n = d_.size(); for (size_t i=0;iget_R(); for (size_t i=0;iparentsBegin(); for (; keyS!=cg->parentsEnd(); keyS++) { Matrix S = keyS->second; // get S matrix const size_t m = S.size1(), n = S.size2(); // find S size const size_t J = mapping[keyS->first]; // find column index for (size_t i=0;i