gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VectorValues.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
18 #pragma once
19 
20 #include <gtsam/base/Vector.h>
21 #include <gtsam/base/ConcurrentMap.h>
22 #include <gtsam/base/FastVector.h>
23 #include <gtsam/global_includes.h>
25 
26 #include <boost/shared_ptr.hpp>
27 
28 namespace gtsam {
29 
89  class GTSAM_EXPORT VectorValues {
90  protected:
91  typedef VectorValues This;
94 
95  public:
98  //typedef Values::reverse_iterator reverse_iterator; ///< Reverse iterator over vector values
99  //typedef Values::const_reverse_iterator const_reverse_iterator; ///< Const reverse iterator over vector values
100  typedef boost::shared_ptr<This> shared_ptr;
103  typedef std::map<Key,size_t> Dims;
104 
107 
112 
114  VectorValues(const VectorValues& first, const VectorValues& second);
115 
117  template<class CONTAINER>
118  explicit VectorValues(const CONTAINER& c) : values_(c.begin(), c.end()) {}
119 
121  VectorValues(const VectorValues& c) : values_(c.values_) {}
122 
124  template<typename ITERATOR>
125  VectorValues(ITERATOR first, ITERATOR last) : values_(first, last) {}
126 
128  VectorValues(const Vector& c, const Dims& dims);
129 
131  static VectorValues Zero(const VectorValues& other);
132 
136 
138  Key size() const { return values_.size(); }
139 
141  size_t dim(Key j) const { return at(j).rows(); }
142 
144  bool exists(Key j) const { return find(j) != end(); }
145 
147  Vector& at(Key j) {
148  iterator item = find(j);
149  if(item == end())
150  throw std::out_of_range(
151  "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
152  else
153  return item->second;
154  }
155 
157  const Vector& at(Key j) const {
158  const_iterator item = find(j);
159  if(item == end())
160  throw std::out_of_range(
161  "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
162  else
163  return item->second;
164  }
165 
168  Vector& operator[](Key j) { return at(j); }
169 
172  const Vector& operator[](Key j) const { return at(j); }
173 
177  void update(const VectorValues& values);
178 
183  iterator insert(Key j, const Vector& value) {
184  return insert(std::make_pair(j, value)); // Note only passing a reference to the Vector
185  }
186 
191  iterator insert(const std::pair<Key, Vector>& key_value) {
192  // Note that here we accept a pair with a reference to the Vector, but the Vector is copied as
193  // it is inserted into the values_ map.
194  std::pair<iterator, bool> result = values_.insert(key_value);
195  if(!result.second)
196  throw std::invalid_argument(
197  "Requested to insert variable '" + DefaultKeyFormatter(key_value.first)
198  + "' already in this VectorValues.");
199  return result.first;
200  }
201 
204  void insert(const VectorValues& values);
205 
210  std::pair<iterator, bool> tryInsert(Key j, const Vector& value) {
211  return values_.insert(std::make_pair(j, value)); }
212 
214  void erase(Key var) {
215  if(values_.unsafe_erase(var) == 0)
216  throw std::invalid_argument("Requested variable '" + DefaultKeyFormatter(var) + "', is not in this VectorValues.");
217  }
218 
220  void setZero();
221 
222  iterator begin() { return values_.begin(); }
223  const_iterator begin() const { return values_.begin(); }
224  iterator end() { return values_.end(); }
225  const_iterator end() const { return values_.end(); }
226  //reverse_iterator rbegin() { return values_.rbegin(); } ///< Reverse iterator over variables
227  //const_reverse_iterator rbegin() const { return values_.rbegin(); } ///< Reverse iterator over variables
228  //reverse_iterator rend() { return values_.rend(); } ///< Reverse iterator over variables
229  //const_reverse_iterator rend() const { return values_.rend(); } ///< Reverse iterator over variables
230 
232  iterator find(Key j) { return values_.find(j); }
233 
235  const_iterator find(Key j) const { return values_.find(j); }
236 
238  void print(const std::string& str = "VectorValues: ",
239  const KeyFormatter& formatter = DefaultKeyFormatter) const;
240 
242  bool equals(const VectorValues& x, double tol = 1e-9) const;
243 
247 
249  Vector vector() const;
250 
252  Vector vector(const FastVector<Key>& keys) const;
253 
255  Vector vector(const Dims& dims) const;
256 
258  void swap(VectorValues& other);
259 
261  bool hasSameStructure(const VectorValues other) const;
262 
266 
270  double dot(const VectorValues& v) const;
271 
273  double norm() const;
274 
276  double squaredNorm() const;
277 
280  VectorValues operator+(const VectorValues& c) const;
281 
284  VectorValues add(const VectorValues& c) const;
285 
288  VectorValues& operator+=(const VectorValues& c);
289 
292  VectorValues& addInPlace(const VectorValues& c);
293 
295  VectorValues& addInPlace_(const VectorValues& c);
296 
299  VectorValues operator-(const VectorValues& c) const;
300 
303  VectorValues subtract(const VectorValues& c) const;
304 
306  friend GTSAM_EXPORT VectorValues operator*(const double a, const VectorValues &v);
307 
309  VectorValues scale(const double a) const;
310 
312  VectorValues& operator*=(double alpha);
313 
315  VectorValues& scaleInPlace(double alpha);
316 
318 
322 
323  //inline VectorValues scale(const double a, const VectorValues& c) const { return a * (*this); }
324 
326 
330  //friend VectorValues operator*(const double a, const VectorValues &v) {
331  // VectorValues result = VectorValues::SameStructure(v);
332  // for(Key j = 0; j < v.size(); ++j)
333  // result.values_[j] = a * v.values_[j];
334  // return result;
335  //}
336 
338  //friend void axpy(double alpha, const VectorValues& x, VectorValues& y) {
339  // if(x.size() != y.size())
340  // throw std::invalid_argument("axpy(VectorValues) called with different vector sizes");
341  // for(Key j = 0; j < x.size(); ++j)
342  // if(x.values_[j].size() == y.values_[j].size())
343  // y.values_[j] += alpha * x.values_[j];
344  // else
345  // throw std::invalid_argument("axpy(VectorValues) called with different vector sizes");
346  //}
348  //friend void sqrt(VectorValues &x) {
349  // for(Key j = 0; j < x.size(); ++j)
350  // x.values_[j] = x.values_[j].cwiseSqrt();
351  //}
352 
354  //friend void ediv(const VectorValues& numerator, const VectorValues& denominator, VectorValues &result) {
355  // if(numerator.size() != denominator.size() || numerator.size() != result.size())
356  // throw std::invalid_argument("ediv(VectorValues) called with different vector sizes");
357  // for(Key j = 0; j < numerator.size(); ++j)
358  // if(numerator.values_[j].size() == denominator.values_[j].size() && numerator.values_[j].size() == result.values_[j].size())
359  // result.values_[j] = numerator.values_[j].cwiseQuotient(denominator.values_[j]);
360  // else
361  // throw std::invalid_argument("ediv(VectorValues) called with different vector sizes");
362  //}
363 
365  //friend void edivInPlace(VectorValues& x, const VectorValues& y) {
366  // if(x.size() != y.size())
367  // throw std::invalid_argument("edivInPlace(VectorValues) called with different vector sizes");
368  // for(Key j = 0; j < x.size(); ++j)
369  // if(x.values_[j].size() == y.values_[j].size())
370  // x.values_[j].array() /= y.values_[j].array();
371  // else
372  // throw std::invalid_argument("edivInPlace(VectorValues) called with different vector sizes");
373  //}
374 
375  private:
377  friend class boost::serialization::access;
378  template<class ARCHIVE>
379  void serialize(ARCHIVE & ar, const unsigned int version) {
380  ar & BOOST_SERIALIZATION_NVP(values_);
381  }
382  }; // VectorValues definition
383 
384 } // \namespace gtsam
void erase(Key var)
Erase the vector with the given key, or throw std::out_of_range if it does not exist.
Definition: VectorValues.h:214
Values::iterator iterator
Iterator over vector values.
Definition: VectorValues.h:96
VectorValues(ITERATOR first, ITERATOR last)
Create from a pair of iterators over pair<Key,Vector>.
Definition: VectorValues.h:125
Vector & at(Key j)
Read/write access to the vector value with key j, throws std::out_of_range if j does not exist...
Definition: VectorValues.h:147
typedef and functions to augment Eigen's VectorXd
double dot(const V1 &a, const V2 &b)
Dot product.
Definition: Vector.h:259
const_iterator begin() const
Iterator over variables.
Definition: VectorValues.h:223
iterator find(Key j)
Return the iterator corresponding to the requested key, or end() if no variable is present with this ...
Definition: VectorValues.h:232
iterator insert(const std::pair< Key, Vector > &key_value)
Insert a vector value with key j.
Definition: VectorValues.h:191
Values values_
Collection of Vectors making up this VectorValues.
Definition: VectorValues.h:93
Values::const_iterator const_iterator
Const iterator over vector values.
Definition: VectorValues.h:97
ConcurrentMap< Key, Vector > Values
Typedef for the collection of Vectors making up a VectorValues.
Definition: VectorValues.h:92
Included from all GTSAM files.
Values::value_type value_type
Typedef to pair<Key, Vector>, a key-value pair.
Definition: VectorValues.h:101
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:249
const_iterator end() const
Iterator over variables.
Definition: VectorValues.h:225
Key size() const
Number of variables stored.
Definition: VectorValues.h:138
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: VectorValues.h:100
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
const Vector & at(Key j) const
Access the vector value with key j (const version), throws std::out_of_range if j does not exist...
Definition: VectorValues.h:157
iterator begin()
Iterator over variables.
Definition: VectorValues.h:222
VectorValues(const VectorValues &c)
Implicit copy constructor to specialize the explicit constructor from any container.
Definition: VectorValues.h:121
Template to create a binary predicate.
Definition: Testable.h:102
size_t Key
Integer nonlinear key type.
Definition: types.h:59
VectorValues(const CONTAINER &c)
Create from another container holding pair<Key,Vector>.
Definition: VectorValues.h:118
const Vector & operator[](Key j) const
Access the vector value with key j (const version), throws std::out_of_range if j does not exist...
Definition: VectorValues.h:172
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:89
const_iterator find(Key j) const
Return the iterator corresponding to the requested key, or end() if no variable is present with this ...
Definition: VectorValues.h:235
size_t dim(Key j) const
Return the dimension of variable j.
Definition: VectorValues.h:141
Vector & operator[](Key j)
Read/write access to the vector value with key j, throws std::out_of_range if j does not exist...
Definition: VectorValues.h:168
bool exists(Key j) const
Check whether a variable with key j exists.
Definition: VectorValues.h:144
iterator insert(Key j, const Vector &value)
Insert a vector value with key j.
Definition: VectorValues.h:183
iterator end()
Iterator over variables.
Definition: VectorValues.h:224
VectorValues()
Default constructor creates an empty VectorValues.
Definition: VectorValues.h:111
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:106
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:128
value_type KeyValuePair
Typedef to pair<Key, Vector>, a key-value pair.
Definition: VectorValues.h:102
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:124
std::pair< iterator, bool > tryInsert(Key j, const Vector &value)
insert that mimics the STL map insert - if the value already exists, the map is not modified and an i...
Definition: VectorValues.h:210
A thin wrapper around std::vector that uses boost's pool_allocator.
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62