| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |  * @file    VectorConfig.cpp | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief   Factor Graph Configuration | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |  * @brief   VectorConfig | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @author  Carlos Nieto | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/foreach.hpp>
 | 
					
						
							|  |  |  | #include <boost/tuple/tuple.hpp>
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | #include "VectorConfig.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // trick from some reading group
 | 
					
						
							|  |  |  | #define FOREACH_PAIR( KEY, VAL, COL) BOOST_FOREACH (boost::tie(KEY,VAL),COL) 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | using namespace std; | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | void check_size(const Symbol& key, const Vector & vj, const Vector & dj) { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   if (dj.size()!=vj.size()) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     cout << "For key \"" << (string)key << "\"" << endl; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     cout << "vj.size = " << vj.size() << endl; | 
					
						
							|  |  |  |     cout << "dj.size = " << dj.size() << endl; | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     throw(std::invalid_argument("VectorConfig::+ mismatched dimensions")); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-20 13:10:55 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | std::vector<Symbol> VectorConfig::get_names() const { | 
					
						
							|  |  |  |   std::vector<Symbol> names; | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  |   for(const_iterator it=values.begin(); it!=values.end(); it++) | 
					
						
							|  |  |  |     names.push_back(it->first); | 
					
						
							|  |  |  |   return names; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 04:17:31 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void VectorConfig::insert(const VectorConfig& config) { | 
					
						
							|  |  |  | 	for (const_iterator it = config.begin(); it!=config.end(); it++) { | 
					
						
							|  |  |  | 		insert(it->first, it->second); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | void VectorConfig::add(const Symbol& j, const Vector& a) { | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | 	Vector& vj = values[j]; | 
					
						
							|  |  |  | 	if (vj.size()==0) vj = a; else vj += a; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | size_t VectorConfig::dim() const { | 
					
						
							|  |  |  | 	size_t result=0; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol key; Vector v; // rtodo: copying vector?
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | 	FOREACH_PAIR(key, v, values) result += v.size(); | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | VectorConfig VectorConfig::scale(double s) const { | 
					
						
							| 
									
										
										
										
											2009-11-20 13:10:55 +08:00
										 |  |  | 	VectorConfig scaled; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol key; Vector val; // rtodo: copying vector?
 | 
					
						
							| 
									
										
										
										
											2009-12-11 12:57:31 +08:00
										 |  |  | 	FOREACH_PAIR(key, val, values) | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | 		scaled.insert(key, s*val); | 
					
						
							| 
									
										
										
										
											2009-11-20 13:10:55 +08:00
										 |  |  | 	return scaled; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | VectorConfig VectorConfig::operator*(double s) const { | 
					
						
							|  |  |  | 	return scale(s); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 16:15:09 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | VectorConfig VectorConfig::operator-() const { | 
					
						
							|  |  |  | 	VectorConfig result; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol j; Vector v; // rtodo: copying vector?
 | 
					
						
							| 
									
										
										
										
											2009-12-28 16:15:09 +08:00
										 |  |  | 	FOREACH_PAIR(j, v, values) | 
					
						
							|  |  |  | 		result.insert(j, -v); | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void VectorConfig::operator+=(const VectorConfig& b) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   Symbol j; Vector b_j; // rtodo: copying vector?
 | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 	FOREACH_PAIR(j, b_j, b.values) { | 
					
						
							|  |  |  | 		iterator it = values.find(j); | 
					
						
							|  |  |  | 		if (it==values.end()) | 
					
						
							|  |  |  | 			insert(j, b_j); | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			it->second += b_j; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | VectorConfig VectorConfig::operator+(const VectorConfig& b) const { | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 	VectorConfig result = *this; | 
					
						
							|  |  |  | 	result += b; | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | VectorConfig VectorConfig::operator-(const VectorConfig& b) const { | 
					
						
							|  |  |  | 	VectorConfig result; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol j; Vector v; // rtodo: copying vector?
 | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  | 	FOREACH_PAIR(j, v, values) | 
					
						
							|  |  |  | 		result.insert(j, v - b.get(j)); | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | VectorConfig expmap(const VectorConfig& original, const VectorConfig& delta) | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | 	VectorConfig newConfig; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol j; Vector vj; // rtodo: copying vector?
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 	FOREACH_PAIR(j, vj, original.values) { | 
					
						
							| 
									
										
										
										
											2009-11-29 05:44:07 +08:00
										 |  |  | 		if (delta.contains(j)) { | 
					
						
							|  |  |  | 			const Vector& dj = delta[j]; | 
					
						
							|  |  |  | 			check_size(j,vj,dj); | 
					
						
							|  |  |  | 			newConfig.insert(j, vj + dj); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			newConfig.insert(j, vj); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return newConfig; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 06:43:34 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | VectorConfig expmap(const VectorConfig& original, const Vector& delta) | 
					
						
							| 
									
										
										
										
											2009-12-12 06:43:34 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 	VectorConfig newConfig; | 
					
						
							|  |  |  | 	size_t i = 0; | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 	Symbol j; Vector vj; // rtodo: copying vector?
 | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  | 	FOREACH_PAIR(j, vj, original.values) { | 
					
						
							| 
									
										
										
										
											2009-12-12 06:43:34 +08:00
										 |  |  | 		size_t mj = vj.size(); | 
					
						
							|  |  |  | 		Vector dj = sub(delta, i, i+mj); | 
					
						
							|  |  |  | 		newConfig.insert(j, vj + dj); | 
					
						
							|  |  |  | 		i += mj; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return newConfig; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | const Vector& VectorConfig::get(const Symbol& name) const { | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  |   return values.at(name); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-30 22:53:40 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | Vector& VectorConfig::getReference(const Symbol& name) { | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  |   return values.at(name); | 
					
						
							| 
									
										
										
										
											2009-12-30 22:53:40 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | void VectorConfig::print(const string& name) const { | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   odprintf("VectorConfig %s\n", name.c_str()); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   odprintf("size: %d\n", values.size()); | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   Symbol j; Vector v; // rtodo: copying vector
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   FOREACH_PAIR(j, v, values) { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     odprintf("%s:", ((string)j).c_str()); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     gtsam::print(v); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  | bool VectorConfig::equals(const VectorConfig& expected, double tol) const { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   Symbol j; Vector vActual; // rtodo: copying vector
 | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |   if( values.size() != expected.size() ) return false; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // iterate over all nodes
 | 
					
						
							|  |  |  |   FOREACH_PAIR(j, vActual, values) { | 
					
						
							|  |  |  |     Vector vExpected = expected[j]; | 
					
						
							|  |  |  |     if(!equal_with_abs_tol(vExpected,vActual,tol))  | 
					
						
							| 
									
										
										
										
											2009-11-05 04:59:16 +08:00
										 |  |  |     	return false; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | double VectorConfig::dot(const VectorConfig& b) const { | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |   Symbol j; Vector v; double result = 0.0; // rtodo: copying vector
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | 	FOREACH_PAIR(j, v, values) result += gtsam::dot(v,b.get(j)); | 
					
						
							|  |  |  | 	return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | double dot(const VectorConfig& a, const VectorConfig& b) { | 
					
						
							|  |  |  | 	return a.dot(b); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2010-01-30 10:04:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 12:01:49 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void scal(double alpha, VectorConfig& x) { | 
					
						
							|  |  |  | 	for (VectorConfig::iterator xj = x.begin(); xj != x.end(); xj++) | 
					
						
							|  |  |  | 		scal(alpha, xj->second); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 10:04:37 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | void axpy(double alpha, const VectorConfig& x, VectorConfig& y) { | 
					
						
							|  |  |  | 	VectorConfig::const_iterator xj = x.begin(); | 
					
						
							|  |  |  | 	for (VectorConfig::iterator yj = y.begin(); yj != y.end(); yj++, xj++) | 
					
						
							|  |  |  | 		axpy(alpha, xj->second, yj->second); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-11 16:32:59 +08:00
										 |  |  | void print(const VectorConfig& v, const std::string& s){ | 
					
						
							|  |  |  | 	v.print(s); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | /* ************************************************************************* */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | } // gtsam
 |