| 
									
										
										
										
											2010-10-14 12:54:38 +08:00
										 |  |  | /* ----------------------------------------------------------------------------
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * 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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  * -------------------------------------------------------------------------- */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * @file    VectorMap.h | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |  * @brief   Factor Graph Valuesuration | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  |  * @author  Carlos Nieto | 
					
						
							|  |  |  |  * @author  Christian Potthast | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // \callgraph
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <map>
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <boost/serialization/map.hpp>
 | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  | #include <gtsam/base/types.h>
 | 
					
						
							| 
									
										
										
										
											2010-08-20 01:23:19 +08:00
										 |  |  | #include <gtsam/base/Testable.h>
 | 
					
						
							|  |  |  | #include <gtsam/base/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace gtsam { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |   /** Factor Graph Valuesuration */ | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   class VectorMap : public Testable<VectorMap> { | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   protected: | 
					
						
							|  |  |  |     /** Map from string indices to values */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     std::map<Index,Vector> values; | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   public: | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     typedef std::map<Index,Vector>::iterator iterator; | 
					
						
							|  |  |  |     typedef std::map<Index,Vector>::const_iterator const_iterator; | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     VectorMap() {} | 
					
						
							|  |  |  |     VectorMap(const VectorMap& cfg_in): values(cfg_in.values) {} | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     VectorMap(Index j, const Vector& a) { insert(j,a); } | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |      | 
					
						
							|  |  |  |     virtual ~VectorMap() {} | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** return all the nodes in the graph **/ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     std::vector<Index> get_names() const; | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** convert into a single large vector */ | 
					
						
							|  |  |  |     Vector vector() const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 11:09:58 +08:00
										 |  |  |     /** Insert a value into the values structure with a given index */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     VectorMap& insert(Index name, const Vector& v); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Insert or add a value with given index */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     VectorMap& insertAdd(Index j, const Vector& v); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Insert a config into another config */ | 
					
						
							|  |  |  |     void insert(const VectorMap& config); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Insert a config into another config, add if key already exists */ | 
					
						
							|  |  |  |     void insertAdd(const VectorMap& config); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     const_iterator begin() const {return values.begin();} | 
					
						
							|  |  |  |     const_iterator end()   const {return values.end();} | 
					
						
							|  |  |  |     iterator begin() {return values.begin();} | 
					
						
							|  |  |  |     iterator end()   {return values.end();} | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Vector access in VectorMap is via a Vector reference */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     Vector& operator[](Index j); | 
					
						
							|  |  |  |     const Vector& operator[](Index j) const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** [set] and [get] provided for access via MATLAB */ | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     inline Vector& get(Index j) { return (*this)[j];} | 
					
						
							|  |  |  |     void set(Index j, const Vector& v) { (*this)[j] = v; } | 
					
						
							|  |  |  |     inline const Vector& get(Index j) const { return (*this)[j];} | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-12 05:14:35 +08:00
										 |  |  |     bool contains(Index name) const { | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |       const_iterator it = values.find(name); | 
					
						
							|  |  |  |       return (it!=values.end()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Nr of vectors */ | 
					
						
							|  |  |  |     size_t size() const { return values.size();} | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Total dimensionality */ | 
					
						
							|  |  |  |     size_t dim() const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +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; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Scale */ | 
					
						
							|  |  |  |     VectorMap scale(double s) const; | 
					
						
							|  |  |  |     VectorMap operator*(double s) const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Negation */ | 
					
						
							|  |  |  |     VectorMap operator-() const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Add in place */ | 
					
						
							|  |  |  |     void operator+=(const VectorMap &b); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Add */ | 
					
						
							|  |  |  |     VectorMap operator+(const VectorMap &b) const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Subtract */ | 
					
						
							|  |  |  |     VectorMap operator-(const VectorMap &b) const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** print */ | 
					
						
							|  |  |  |     void print(const std::string& name = "") const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** equals, for unit testing */ | 
					
						
							|  |  |  |     bool equals(const VectorMap& expected, double tol=1e-9) const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     void clear() {values.clear();} | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Dot product */ | 
					
						
							|  |  |  |     double dot(const VectorMap& b) const; | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Set all vectors to zero */ | 
					
						
							|  |  |  |     VectorMap& zero(); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /** Create a clone of x with exactly same structure, except with zero values */ | 
					
						
							|  |  |  |     static VectorMap zero(const VectorMap& x); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Add a delta config, needed for use in NonlinearOptimizer | 
					
						
							|  |  |  |      * For VectorMap, this is just addition. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     friend VectorMap expmap(const VectorMap& original, const VectorMap& delta); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |     /**
 | 
					
						
							|  |  |  |      * Add a delta vector (not a config) | 
					
						
							|  |  |  |      * Will use the ordering that map uses to loop over vectors | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     friend VectorMap expmap(const VectorMap& original, const Vector& delta); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; // VectorMap
 | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /** scalar product */ | 
					
						
							|  |  |  |   inline VectorMap operator*(double s, const VectorMap& x) {return x*s;} | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /** Dot product */ | 
					
						
							|  |  |  |   double dot(const VectorMap&, const VectorMap&); | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * BLAS Level 1 scal: x <- alpha*x | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   void scal(double alpha, VectorMap& x); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /**
 | 
					
						
							|  |  |  |    * 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 VectorMap& x, VectorMap& y); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** dim function (for iterative::CGD) */ | 
					
						
							|  |  |  |   inline double dim(const VectorMap& x) { return x.dim();} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** max of the vectors */ | 
					
						
							|  |  |  |   inline double max(const VectorMap& x) { return x.max();} | 
					
						
							| 
									
										
										
										
											2010-08-27 03:55:40 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-10-09 06:04:47 +08:00
										 |  |  |   /** print with optional string */ | 
					
						
							|  |  |  |   void print(const VectorMap& v, const std::string& s = ""); | 
					
						
							| 
									
										
										
										
											2010-02-17 11:29:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | } // gtsam
 |