gtsam  4.0.0
gtsam
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/linear/Scatter.h>
22 #include <gtsam/base/Vector.h>
23 #include <gtsam/base/ConcurrentMap.h>
24 #include <gtsam/base/FastVector.h>
25 #include <gtsam/global_includes.h>
26 
27 #include <boost/shared_ptr.hpp>
28 
29 namespace gtsam {
30 
90  class GTSAM_EXPORT VectorValues {
91  protected:
92  typedef VectorValues This;
94  Values values_;
95 
96  public:
99  //typedef Values::reverse_iterator reverse_iterator; ///< Reverse iterator over vector values
100  //typedef Values::const_reverse_iterator const_reverse_iterator; ///< Const reverse iterator over vector values
101  typedef boost::shared_ptr<This> shared_ptr;
103  typedef value_type KeyValuePair;
104  typedef std::map<Key,size_t> Dims;
105 
108 
113 
115  VectorValues(const VectorValues& first, const VectorValues& second);
116 
118  template<class CONTAINER>
119  explicit VectorValues(const CONTAINER& c) : values_(c.begin(), c.end()) {}
120 
122  VectorValues(const VectorValues& c) : values_(c.values_) {}
123 
125  template<typename ITERATOR>
126  VectorValues(ITERATOR first, ITERATOR last) : values_(first, last) {}
127 
129  VectorValues(const Vector& c, const Dims& dims);
130 
132  VectorValues(const Vector& c, const Scatter& scatter);
133 
135  static VectorValues Zero(const VectorValues& other);
136 
140 
142  size_t size() const { return values_.size(); }
143 
145  size_t dim(Key j) const { return at(j).rows(); }
146 
148  bool exists(Key j) const { return find(j) != end(); }
149 
151  Vector& at(Key j) {
152  iterator item = find(j);
153  if(item == end())
154  throw std::out_of_range(
155  "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
156  else
157  return item->second;
158  }
159 
161  const Vector& at(Key j) const {
162  const_iterator item = find(j);
163  if(item == end())
164  throw std::out_of_range(
165  "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
166  else
167  return item->second;
168  }
169 
172  Vector& operator[](Key j) { return at(j); }
173 
176  const Vector& operator[](Key j) const { return at(j); }
177 
181  void update(const VectorValues& values);
182 
187  iterator insert(Key j, const Vector& value) {
188  return insert(std::make_pair(j, value));
189  }
190 
195  iterator insert(const std::pair<Key, Vector>& key_value);
196 
199  void insert(const VectorValues& values);
200 
205  std::pair<iterator, bool> tryInsert(Key j, const Vector& value) {
206  return values_.insert(std::make_pair(j, value)); }
207 
209  void erase(Key var) {
210  if(values_.unsafe_erase(var) == 0)
211  throw std::invalid_argument("Requested variable '" + DefaultKeyFormatter(var) + "', is not in this VectorValues.");
212  }
213 
215  void setZero();
216 
217  iterator begin() { return values_.begin(); }
218  const_iterator begin() const { return values_.begin(); }
219  iterator end() { return values_.end(); }
220  const_iterator end() const { return values_.end(); }
221  //reverse_iterator rbegin() { return values_.rbegin(); } ///< Reverse iterator over variables
222  //const_reverse_iterator rbegin() const { return values_.rbegin(); } ///< Reverse iterator over variables
223  //reverse_iterator rend() { return values_.rend(); } ///< Reverse iterator over variables
224  //const_reverse_iterator rend() const { return values_.rend(); } ///< Reverse iterator over variables
225 
227  iterator find(Key j) { return values_.find(j); }
228 
230  const_iterator find(Key j) const { return values_.find(j); }
231 
233  void print(const std::string& str = "VectorValues: ",
234  const KeyFormatter& formatter = DefaultKeyFormatter) const;
235 
237  bool equals(const VectorValues& x, double tol = 1e-9) const;
238 
242 
244  Vector vector() const;
245 
247  Vector vector(const FastVector<Key>& keys) const;
248 
250  Vector vector(const Dims& dims) const;
251 
253  void swap(VectorValues& other);
254 
256  bool hasSameStructure(const VectorValues other) const;
257 
261 
265  double dot(const VectorValues& v) const;
266 
268  double norm() const;
269 
271  double squaredNorm() const;
272 
275  VectorValues operator+(const VectorValues& c) const;
276 
279  VectorValues add(const VectorValues& c) const;
280 
283  VectorValues& operator+=(const VectorValues& c);
284 
287  VectorValues& addInPlace(const VectorValues& c);
288 
290  VectorValues& addInPlace_(const VectorValues& c);
291 
294  VectorValues operator-(const VectorValues& c) const;
295 
298  VectorValues subtract(const VectorValues& c) const;
299 
301  friend GTSAM_EXPORT VectorValues operator*(const double a, const VectorValues &v);
302 
304  VectorValues scale(const double a) const;
305 
307  VectorValues& operator*=(double alpha);
308 
310  VectorValues& scaleInPlace(double alpha);
311 
313 
317 
318  //inline VectorValues scale(const double a, const VectorValues& c) const { return a * (*this); }
319 
321 
325  //friend VectorValues operator*(const double a, const VectorValues &v) {
326  // VectorValues result = VectorValues::SameStructure(v);
327  // for(Key j = 0; j < v.size(); ++j)
328  // result.values_[j] = a * v.values_[j];
329  // return result;
330  //}
331 
333  //friend void axpy(double alpha, const VectorValues& x, VectorValues& y) {
334  // if(x.size() != y.size())
335  // throw std::invalid_argument("axpy(VectorValues) called with different vector sizes");
336  // for(Key j = 0; j < x.size(); ++j)
337  // if(x.values_[j].size() == y.values_[j].size())
338  // y.values_[j] += alpha * x.values_[j];
339  // else
340  // throw std::invalid_argument("axpy(VectorValues) called with different vector sizes");
341  //}
343  //friend void sqrt(VectorValues &x) {
344  // for(Key j = 0; j < x.size(); ++j)
345  // x.values_[j] = x.values_[j].cwiseSqrt();
346  //}
347 
349  //friend void ediv(const VectorValues& numerator, const VectorValues& denominator, VectorValues &result) {
350  // if(numerator.size() != denominator.size() || numerator.size() != result.size())
351  // throw std::invalid_argument("ediv(VectorValues) called with different vector sizes");
352  // for(Key j = 0; j < numerator.size(); ++j)
353  // if(numerator.values_[j].size() == denominator.values_[j].size() && numerator.values_[j].size() == result.values_[j].size())
354  // result.values_[j] = numerator.values_[j].cwiseQuotient(denominator.values_[j]);
355  // else
356  // throw std::invalid_argument("ediv(VectorValues) called with different vector sizes");
357  //}
358 
360  //friend void edivInPlace(VectorValues& x, const VectorValues& y) {
361  // if(x.size() != y.size())
362  // throw std::invalid_argument("edivInPlace(VectorValues) called with different vector sizes");
363  // for(Key j = 0; j < x.size(); ++j)
364  // if(x.values_[j].size() == y.values_[j].size())
365  // x.values_[j].array() /= y.values_[j].array();
366  // else
367  // throw std::invalid_argument("edivInPlace(VectorValues) called with different vector sizes");
368  //}
369 
370  private:
372  friend class boost::serialization::access;
373  template<class ARCHIVE>
374  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
375  ar & BOOST_SERIALIZATION_NVP(values_);
376  }
377  }; // VectorValues definition
378 
380  template<>
381  struct traits<VectorValues> : public Testable<VectorValues> {
382  };
383 
384 } // \namespace gtsam
Values::iterator iterator
Iterator over vector values.
Definition: VectorValues.h:97
Values values_
Collection of Vectors making up this VectorValues.
Definition: VectorValues.h:94
Values::const_iterator const_iterator
Const iterator over vector values.
Definition: VectorValues.h:98
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:230
const_iterator end() const
Iterator over variables.
Definition: VectorValues.h:220
VectorValues(const VectorValues &c)
Implicit copy constructor to specialize the explicit constructor from any container.
Definition: VectorValues.h:122
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:176
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:140
ConcurrentMap< Key, Vector > Values
Typedef for the collection of Vectors making up a VectorValues.
Definition: VectorValues.h:93
Included from all GTSAM files.
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:123
BinarySumExpression< T > operator-(const Expression< T > &e1, const Expression< T > &e2)
Construct an expression that subtracts one expression from another.
Definition: Expression.h:279
Values::value_type value_type
Typedef to pair<Key, Vector>, a key-value pair.
Definition: VectorValues.h:102
size_t size() const
Number of variables stored.
Definition: VectorValues.h:142
size_t dim(Key j) const
Return the dimension of variable j.
Definition: VectorValues.h:145
typedef and functions to augment Eigen&#39;s VectorXd
bool exists(Key j) const
Check whether a variable with key j exists.
Definition: VectorValues.h:148
iterator find(Key j)
Return the iterator corresponding to the requested key, or end() if no variable is present with this ...
Definition: VectorValues.h:227
VectorValues(ITERATOR first, ITERATOR last)
Create from a pair of iterators over pair<Key,Vector>.
Definition: VectorValues.h:126
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
Template to create a binary predicate.
Definition: Testable.h:110
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:151
BinarySumExpression< T > operator+(const Expression< T > &e1, const Expression< T > &e2)
Construct an expression that sums two input expressions of the same type T The type T must be a vecto...
Definition: Expression.h:273
VectorValues(const CONTAINER &c)
Create from another container holding pair<Key,Vector>.
Definition: VectorValues.h:119
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:90
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
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:205
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:161
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: VectorValues.h:101
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:101
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:209
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:170
value_type KeyValuePair
Typedef to pair<Key, Vector>, a key-value pair.
Definition: VectorValues.h:103
iterator begin()
Iterator over variables.
Definition: VectorValues.h:217
Scatter is an intermediate data structure used when building a HessianFactor incrementally, to get the keys in the right order.
Definition: Scatter.h:51
Maps global variable indices to slot indices.
VectorValues()
Default constructor creates an empty VectorValues.
Definition: VectorValues.h:112
double dot(const V1 &a, const V2 &b)
Dot product.
Definition: Vector.h:162
const_iterator begin() const
Iterator over variables.
Definition: VectorValues.h:218
A thin wrapper around std::vector that uses boost&#39;s pool_allocator.
iterator insert(Key j, const Vector &value)
Insert a vector value with key j.
Definition: VectorValues.h:187
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:119
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:172
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:33
iterator end()
Iterator over variables.
Definition: VectorValues.h:219