/** * @file VectorConfig.h * @brief Factor Graph Configuration * @author Carlos Nieto * @author Christian Potthast */ // \callgraph #pragma once #include #include #include "Testable.h" #include "Vector.h" #include "Key.h" #include "SymbolMap.h" namespace gtsam { /** Factor Graph Configuration */ class VectorConfig : public Testable { protected: /** Map from string indices to values */ SymbolMap values; public: typedef SymbolMap::iterator iterator; typedef SymbolMap::const_iterator const_iterator; VectorConfig() {} VectorConfig(const VectorConfig& cfg_in): values(cfg_in.values) {} VectorConfig(const Symbol& j, const Vector& a) { add(j,a); } virtual ~VectorConfig() {} /** return all the nodes in the graph **/ std::vector get_names() const; /** Insert a value into the configuration with a given index */ inline VectorConfig& insert(const Symbol& name, const Vector& val) { values.insert(std::make_pair(name,val)); return *this; } /** Insert a config into another config */ void insert(const VectorConfig& config); /** Add to vector at position j */ void add(const Symbol& j, const Vector& a); const_iterator begin() const {return values.begin();} const_iterator end() const {return values.end();} iterator begin() {return values.begin();} iterator end() {return values.end();} /** get a vector in the configuration by name */ const Vector& get(const Symbol& name) const; /** get a vector reference by name */ Vector& getReference(const Symbol& name); /** operator[] syntax for get */ inline const Vector& operator[](const Symbol& name) const { return get(name); } bool contains(const Symbol& name) const { const_iterator it = values.find(name); return (it!=values.end()); } /** Nr of vectors */ size_t size() const { return values.size();} /** Total dimensionality */ size_t dim() const; /** max of the vectors */ inline double max() const { double m = -std::numeric_limits::infinity(); for(const_iterator it=begin(); it!=end(); it++) m = std::max(m, gtsam::max(it->second)); return m; } /** Scale */ VectorConfig scale(double s) const; VectorConfig operator*(double s) const; /** Negation */ VectorConfig operator-() const; /** Add in place */ void operator+=(const VectorConfig &b); /** Add */ VectorConfig operator+(const VectorConfig &b) const; /** Subtract */ VectorConfig operator-(const VectorConfig &b) const; /** print */ void print(const std::string& name = "") const; /** equals, for unit testing */ bool equals(const VectorConfig& expected, double tol=1e-9) const; void clear() {values.clear();} /** Dot product */ double dot(const VectorConfig& b) const; /** * 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); private: /** Serialization function */ friend class boost::serialization::access; template void serialize(Archive & ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(values); } }; // VectorConfig /** scalar product */ inline VectorConfig operator*(double s, const VectorConfig& x) {return x*s;} /** Dot product */ double dot(const VectorConfig&, const VectorConfig&); /** * BLAS Level 1 scal: x <- alpha*x */ void scal(double alpha, VectorConfig& 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 VectorConfig& x, VectorConfig& y); /** dim function (for iterative::CGD) */ inline double dim(const VectorConfig& x) { return x.dim();} /** max of the vectors */ inline double max(const VectorConfig& x) { return x.max();} /** print with optional string */ void print(const VectorConfig& v, const std::string& s = ""); } // gtsam