| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |  * @file    VectorConfig.h | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |  * @brief   Factor Graph Configuration | 
					
						
							|  |  |  |  * @author  Carlos Nieto | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							|  |  |  | #include <boost/serialization/map.hpp>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-09 12:43:04 +08:00
										 |  |  | #include "Testable.h"
 | 
					
						
							|  |  |  | #include "Vector.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | #include "Key.h"
 | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  | #include "SymbolMap.h"
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  |   /** Factor Graph Configuration */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |   class VectorConfig : public Testable<VectorConfig> { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   protected: | 
					
						
							|  |  |  |     /** Map from string indices to values */ | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  |     SymbolMap<Vector> values; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   public: | 
					
						
							| 
									
										
										
										
											2010-01-22 12:41:40 +08:00
										 |  |  |     typedef SymbolMap<Vector>::iterator iterator; | 
					
						
							|  |  |  |     typedef SymbolMap<Vector>::const_iterator const_iterator; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-22 22:42:19 +08:00
										 |  |  |     VectorConfig() {} | 
					
						
							|  |  |  |     VectorConfig(const VectorConfig& cfg_in): values(cfg_in.values) {} | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     VectorConfig(const Symbol& j, const Vector& a) { add(j,a); } | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     virtual ~VectorConfig() {} | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** return all the nodes in the graph **/ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     std::vector<Symbol> get_names() const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** Insert a value into the configuration with a given index */ | 
					
						
							| 
									
										
										
										
											2010-02-01 00:05:16 +08:00
										 |  |  |     inline VectorConfig& insert(const Symbol& name, const Vector& val) { | 
					
						
							|  |  |  |       values.insert(std::make_pair(name,val)); | 
					
						
							|  |  |  |       return *this; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-19 04:17:31 +08:00
										 |  |  |     /** Insert a config into another config */ | 
					
						
							|  |  |  |     void insert(const VectorConfig& config); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-11 04:17:11 +08:00
										 |  |  |     /** Add to vector at position j */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     void add(const Symbol& j, const Vector& a); | 
					
						
							| 
									
										
										
										
											2009-12-11 04:17:11 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-29 04:06:29 +08:00
										 |  |  |     const_iterator begin() const {return values.begin();} | 
					
						
							|  |  |  |     const_iterator end()   const {return values.end();} | 
					
						
							| 
									
										
										
										
											2010-01-30 10:04:37 +08:00
										 |  |  |     iterator begin() {return values.begin();} | 
					
						
							|  |  |  |     iterator end()   {return values.end();} | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** get a vector in the configuration by name */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     const Vector& get(const Symbol& name) const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-30 22:53:40 +08:00
										 |  |  |     /** get a vector reference by name */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     Vector& getReference(const Symbol& name); | 
					
						
							| 
									
										
										
										
											2009-12-30 22:53:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     /** operator[] syntax for get */ | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  | 		inline const Vector& operator[](const Symbol& name) const { | 
					
						
							| 
									
										
										
										
											2009-12-28 16:15:09 +08:00
										 |  |  | 			return get(name); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-18 03:34:57 +08:00
										 |  |  |     bool contains(const Symbol& name) const { | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |       const_iterator it = values.find(name); | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  |       return (it!=values.end()); | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  |     /** Nr of vectors */ | 
					
						
							|  |  |  |     size_t size() const { return values.size();} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Total dimensionality */ | 
					
						
							|  |  |  |     size_t dim() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 13:08:29 +08:00
										 |  |  |     /** max of the vectors */ | 
					
						
							|  |  |  |     inline double max() const { | 
					
						
							|  |  |  |     	double m = -std::numeric_limits<double>::infinity(); | 
					
						
							|  |  |  |     	for(const_iterator it=begin(); it!=end(); it++) | 
					
						
							|  |  |  |     		m = std::max(m, gtsam::max(it->second)); | 
					
						
							|  |  |  |     	return m; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  |     /** Scale */ | 
					
						
							|  |  |  |     VectorConfig scale(double s) const; | 
					
						
							|  |  |  |     VectorConfig operator*(double s) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 16:15:09 +08:00
										 |  |  |     /** Negation */ | 
					
						
							|  |  |  |     VectorConfig operator-() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-27 20:13:31 +08:00
										 |  |  |     /** Add in place */ | 
					
						
							|  |  |  |     void operator+=(const VectorConfig &b); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  |     /** Add */ | 
					
						
							|  |  |  |     VectorConfig operator+(const VectorConfig &b) const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** Subtract */ | 
					
						
							|  |  |  |     VectorConfig operator-(const VectorConfig &b) const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** print */ | 
					
						
							|  |  |  |     void print(const std::string& name = "") const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** equals, for unit testing */ | 
					
						
							| 
									
										
										
										
											2009-10-15 04:39:59 +08:00
										 |  |  |     bool equals(const VectorConfig& expected, double tol=1e-9) const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     void clear() {values.clear();} | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  |     /** Dot product */ | 
					
						
							|  |  |  |     double dot(const VectorConfig& b) const; | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2010-01-08 08:40:17 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Add a delta config, needed for use in NonlinearOptimizer | 
					
						
							|  |  |  |      * For VectorConfig, this is just addition. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     friend VectorConfig expmap(const VectorConfig& original, const VectorConfig& delta); | 
					
						
							|  |  |  |     /**
 | 
					
						
							|  |  |  |      * Add a delta vector (not a config) | 
					
						
							|  |  |  |      * Will use the ordering that map uses to loop over vectors | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     friend VectorConfig expmap(const VectorConfig& original, const Vector& delta); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-22 06:23:24 +08:00
										 |  |  |   private: | 
					
						
							|  |  |  |     /** Serialization function */ | 
					
						
							|  |  |  |     friend class boost::serialization::access; | 
					
						
							|  |  |  |     template<class Archive> | 
					
						
							|  |  |  |       void serialize(Archive & ar, const unsigned int version) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       ar & BOOST_SERIALIZATION_NVP(values); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  |   }; // VectorConfig
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 16:15:09 +08:00
										 |  |  |   /** scalar product */ | 
					
						
							|  |  |  |   inline VectorConfig operator*(double s, const VectorConfig& x) {return x*s;} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  |   /** Dot product */ | 
					
						
							|  |  |  |   double dot(const VectorConfig&, const VectorConfig&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 12:01:49 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * BLAS Level 1 scal: x <- alpha*x | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   void scal(double alpha, VectorConfig& x); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-30 10:04:37 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * BLAS Level 1 axpy: y <- alpha*x + y | 
					
						
							|  |  |  |    * UNSAFE !!!! Only works if x and y laid out in exactly same shape | 
					
						
							|  |  |  |    * Used in internal loop in iterative for fast conjugate gradients | 
					
						
							|  |  |  |    * Consider using other functions if this is not in hotspot | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   void axpy(double alpha, const VectorConfig& x, VectorConfig& y); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-28 20:37:34 +08:00
										 |  |  |   /** dim function (for iterative::CGD) */ | 
					
						
							|  |  |  |   inline double dim(const VectorConfig& x) { return x.dim();} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-16 13:08:29 +08:00
										 |  |  |   /** max of the vectors */ | 
					
						
							|  |  |  |   inline double max(const VectorConfig& x) { return x.max();} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-11 16:32:59 +08:00
										 |  |  |   /** print with optional string */ | 
					
						
							|  |  |  |   void print(const VectorConfig& v, const std::string& s = ""); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-12 05:38:08 +08:00
										 |  |  | } // gtsam
 |