| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    Factor.cpp | 
					
						
							|  |  |  |  * @brief    | 
					
						
							|  |  |  |  * @author  Richard Roberts | 
					
						
							|  |  |  |  * @created Sep 1, 2010 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <gtsam/inference/Factor-inl.h>
 | 
					
						
							|  |  |  | #include <gtsam/inference/Conditional.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/iterator/transform_iterator.hpp>
 | 
					
						
							|  |  |  | #include <boost/lambda/bind.hpp>
 | 
					
						
							|  |  |  | #include <boost/lambda/lambda.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace boost::lambda; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  | Factor::Factor(const Factor& f) : keys_(f.keys_) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Factor::Factor(const Conditional& c) : keys_(c.keys()) {} | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void Factor::print(const std::string& s) const { | 
					
						
							|  |  |  |   cout << s << " "; | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   BOOST_FOREACH(Index key, keys_) cout << " " << key; | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   cout << endl; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | bool Factor::equals(const Factor& other, double tol) const { | 
					
						
							|  |  |  |   return keys_ == other.keys_; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | Conditional::shared_ptr Factor::eliminateFirst() { | 
					
						
							|  |  |  |   assert(!keys_.empty()); | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  |   assertInvariants(); | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   Index eliminated = keys_.front(); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   keys_.erase(keys_.begin()); | 
					
						
							|  |  |  |   return Conditional::shared_ptr(new Conditional(eliminated, keys_)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-10-12 08:14:50 +08:00
										 |  |  | boost::shared_ptr<BayesNet<Conditional> > Factor::eliminate(size_t nrFrontals) { | 
					
						
							|  |  |  |   assert(keys_.size() >= nrFrontals); | 
					
						
							| 
									
										
										
										
											2010-10-14 04:43:58 +08:00
										 |  |  |   assertInvariants(); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:21:48 +08:00
										 |  |  |   BayesNet<Conditional>::shared_ptr fragment(new BayesNet<Conditional>()); | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   const_iterator nextFrontal = this->begin(); | 
					
						
							| 
									
										
										
										
											2010-10-12 08:14:50 +08:00
										 |  |  |   for(Index n = 0; n < nrFrontals; ++n, ++nextFrontal) | 
					
						
							|  |  |  |     fragment->push_back(Conditional::fromRange(nextFrontal, const_iterator(this->end()), 1)); | 
					
						
							|  |  |  |   if(nrFrontals > 0) | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     keys_.assign(fragment->back()->beginParents(), fragment->back()->endParents()); | 
					
						
							|  |  |  |   return fragment; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void Factor::permuteWithInverse(const Permutation& inversePermutation) { | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |   BOOST_FOREACH(Index& key, keys_) { key = inversePermutation[key]; } | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |