/* ---------------------------------------------------------------------------- * 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 Factor-inl.h * @brief * @author Richard Roberts * @created Sep 1, 2010 */ #pragma once #include #include #include #include #include #include #include namespace gtsam { /* ************************************************************************* */ template FactorBase::FactorBase(const FactorBase& f) : keys_(f.keys_) {} /* ************************************************************************* */ template FactorBase::FactorBase(const Conditional& c) : keys_(c.keys()) {} /* ************************************************************************* */ template void FactorBase::assertInvariants() const { #ifndef NDEBUG std::set uniqueSorted(keys_.begin(), keys_.end()); assert(uniqueSorted.size() == keys_.size()); assert(std::equal(uniqueSorted.begin(), uniqueSorted.end(), keys_.begin())); #endif } /* ************************************************************************* */ template void FactorBase::print(const std::string& s) const { std::cout << s << " "; BOOST_FOREACH(KEY key, keys_) std::cout << " " << key; std::cout << std::endl; } /* ************************************************************************* */ template //template bool FactorBase::equals(const This& other, double tol) const { return keys_ == other.keys_; } /* ************************************************************************* */ template template typename DERIVED::shared_ptr FactorBase::Combine(const FactorGraph& factors, const FastMap >& variableSlots) { typedef const FastMap > VariableSlots; typedef typeof(boost::lambda::bind(&VariableSlots::value_type::first, boost::lambda::_1)) FirstGetter; typedef boost::transform_iterator< FirstGetter, typename VariableSlots::const_iterator, KEY, KEY> IndexIterator; FirstGetter firstGetter(boost::lambda::bind(&VariableSlots::value_type::first, boost::lambda::_1)); IndexIterator keysBegin(variableSlots.begin(), firstGetter); IndexIterator keysEnd(variableSlots.end(), firstGetter); return typename DERIVED::shared_ptr(new DERIVED(keysBegin, keysEnd)); } /* ************************************************************************* */ template template typename CONDITIONAL::shared_ptr FactorBase::eliminateFirst() { assert(!keys_.empty()); assertInvariants(); KEY eliminated = keys_.front(); keys_.erase(keys_.begin()); return typename CONDITIONAL::shared_ptr(new CONDITIONAL(eliminated, keys_)); } /* ************************************************************************* */ template template typename BayesNet::shared_ptr FactorBase::eliminate(size_t nrFrontals) { assert(keys_.size() >= nrFrontals); assertInvariants(); typename BayesNet::shared_ptr fragment(new BayesNet()); const_iterator nextFrontal = this->begin(); for(KEY n = 0; n < nrFrontals; ++n, ++nextFrontal) fragment->push_back(CONDITIONAL::FromRange( nextFrontal, const_iterator(this->end()), 1)); if(nrFrontals > 0) keys_.assign(fragment->back()->beginParents(), fragment->back()->endParents()); return fragment; } /* ************************************************************************* */ template void FactorBase::permuteWithInverse(const Permutation& inversePermutation) { BOOST_FOREACH(KEY& key, keys_) { key = inversePermutation[key]; } } }