| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  |  * @file   GaussianConditional.cpp | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief  Conditional Gaussian Base class | 
					
						
							|  |  |  |  * @author Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <boost/numeric/ublas/vector.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | #include "Ordering.h"
 | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | #include "GaussianConditional.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | using namespace std; | 
					
						
							|  |  |  | using namespace gtsam; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | GaussianConditional::GaussianConditional(const Symbol& key,Vector d, Matrix R, Vector sigmas) : | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	Conditional (key), R_(R),sigmas_(sigmas),d_(d) | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | GaussianConditional::GaussianConditional(const Symbol& key, Vector d, Matrix R, | 
					
						
							|  |  |  | 		const Symbol& name1, Matrix S, Vector sigmas) : | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	Conditional (key), R_(R), sigmas_(sigmas), d_(d) { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	parents_.insert(make_pair(name1, S)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | GaussianConditional::GaussianConditional(const Symbol& key, Vector d, Matrix R, | 
					
						
							|  |  |  | 		const Symbol& name1, Matrix S, const Symbol& name2, Matrix T, Vector sigmas) : | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	Conditional (key), R_(R),sigmas_(sigmas), d_(d) { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	parents_.insert(make_pair(name1, S)); | 
					
						
							|  |  |  | 	parents_.insert(make_pair(name2, T)); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | GaussianConditional::GaussianConditional(const Symbol& key, | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | 		const Vector& d, const Matrix& R, const SymbolMap<Matrix>& parents, Vector sigmas) : | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	Conditional (key), R_(R),sigmas_(sigmas), d_(d), parents_(parents) { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-08 21:57:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | void GaussianConditional::print(const string &s) const | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   cout << s << ": density on " << (string)key_ << endl; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   gtsam::print(R_,"R"); | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  |   for(Parents::const_iterator it = parents_.begin() ; it != parents_.end() ; it++ ) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     const Symbol& j = it->first; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     const Matrix& Aj = it->second; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     gtsam::print(Aj, "A["+(string)j+"]"); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   gtsam::print(d_,"d"); | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  |   gtsam::print(sigmas_,"sigmas"); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | }     | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | bool GaussianConditional::equals(const Conditional &c, double tol) const { | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	if (!Conditional::equals(c)) return false; | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | 	const GaussianConditional* p = dynamic_cast<const GaussianConditional*> (&c); | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	if (p == NULL) return false; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 	Parents::const_iterator it = parents_.begin(); | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// check if the size of the parents_ map is the same
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	if (parents_.size() != p->parents_.size()) return false; | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// check if R_ is equal
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	if (!(equal_with_abs_tol(R_, p->R_, tol))) return false; | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// check if d_ is equal
 | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 	if (!(::equal_with_abs_tol(d_, p->d_, tol))) return false; | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 14:59:59 +08:00
										 |  |  | 	// check if sigmas are equal
 | 
					
						
							|  |  |  | 	if (!(::equal_with_abs_tol(sigmas_, p->sigmas_, tol))) return false; | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | 	// check if the matrices are the same
 | 
					
						
							|  |  |  | 	// iterate over the parents_ map
 | 
					
						
							|  |  |  | 	for (it = parents_.begin(); it != parents_.end(); it++) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		Parents::const_iterator it2 = p->parents_.find(it->first); | 
					
						
							| 
									
										
										
										
											2009-11-02 11:50:30 +08:00
										 |  |  | 		if (it2 != p->parents_.end()) { | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | 			if (!(equal_with_abs_tol(it->second, it2->second, tol))) return false; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 		} else | 
					
						
							| 
									
										
										
										
											2009-10-25 07:14:14 +08:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | list<Symbol> GaussianConditional::parents() const { | 
					
						
							|  |  |  | 	list<Symbol> result; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | 	for (Parents::const_iterator it = parents_.begin(); it != parents_.end(); it++) | 
					
						
							|  |  |  | 		result.push_back(it->first); | 
					
						
							| 
									
										
										
										
											2009-11-01 00:55:17 +08:00
										 |  |  | 	return result; | 
					
						
							| 
									
										
										
										
											2009-10-31 13:12:39 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-11-13 00:41:18 +08:00
										 |  |  | Vector GaussianConditional::solve(const VectorConfig& x) const { | 
					
						
							| 
									
										
										
										
											2009-08-27 09:23:05 +08:00
										 |  |  | 	Vector rhs = d_; | 
					
						
							| 
									
										
										
										
											2010-01-31 12:39:41 +08:00
										 |  |  | 	for (Parents::const_iterator it = parents_.begin(); it!= parents_.end(); it++) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		const Symbol& j = it->first; | 
					
						
							| 
									
										
										
										
											2009-08-27 09:23:05 +08:00
										 |  |  | 		const Matrix& Aj = it->second; | 
					
						
							| 
									
										
										
										
											2010-02-01 00:04:24 +08:00
										 |  |  | 		multiplyAdd(-1.0,Aj,x[j],rhs); | 
					
						
							| 
									
										
										
										
											2009-08-27 09:23:05 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-01-31 12:39:41 +08:00
										 |  |  | 	return backSubstituteUpper(R_, rhs, false); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ |