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 
30 #include <map>
31 #include <string>
32 
33 namespace gtsam {
34 
73  class GTSAM_EXPORT VectorValues {
74  protected:
75  typedef VectorValues This;
78 
79  public:
82  typedef boost::shared_ptr<This> shared_ptr;
85  typedef std::map<Key, size_t> Dims;
86 
89 
94 
97  VectorValues(const VectorValues& first, const VectorValues& second);
98 
100  template<class CONTAINER>
101  explicit VectorValues(const CONTAINER& c) : values_(c.begin(), c.end()) {}
102 
104  VectorValues(const VectorValues& c) : values_(c.values_) {}
105 
107  template<typename ITERATOR>
108  VectorValues(ITERATOR first, ITERATOR last) : values_(first, last) {}
109 
111  VectorValues(const Vector& c, const Dims& dims);
112 
114  VectorValues(const Vector& c, const Scatter& scatter);
115 
117  static VectorValues Zero(const VectorValues& other);
118 
122 
124  size_t size() const { return values_.size(); }
125 
127  size_t dim(Key j) const { return at(j).rows(); }
128 
130  bool exists(Key j) const { return find(j) != end(); }
131 
136  Vector& at(Key j) {
137  iterator item = find(j);
138  if (item == end())
139  throw std::out_of_range(
140  "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
141  else
142  return item->second;
143  }
144 
149  const Vector& at(Key j) const {
150  const_iterator item = find(j);
151  if (item == end())
152  throw std::out_of_range(
153  "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
154  else
155  return item->second;
156  }
157 
160  Vector& operator[](Key j) { return at(j); }
161 
164  const Vector& operator[](Key j) const { return at(j); }
165 
169  void update(const VectorValues& values);
170 
175  iterator insert(const std::pair<Key, Vector>& key_value);
176 
181  iterator emplace(Key j, const Vector& value);
182 
187  iterator insert(Key j, const Vector& value) {
188  return insert(std::make_pair(j, value));
189  }
190 
193  void insert(const VectorValues& values);
194 
199  std::pair<iterator, bool> tryInsert(Key j, const Vector& value) {
200  return values_.emplace(j, value);
201  }
202 
204  void erase(Key var) {
205  if (values_.unsafe_erase(var) == 0)
206  throw std::invalid_argument("Requested variable '" +
207  DefaultKeyFormatter(var) +
208  "', is not in this VectorValues.");
209  }
210 
212  void setZero();
213 
214  iterator begin() { return values_.begin(); }
215  const_iterator begin() const { return values_.begin(); }
216  iterator end() { return values_.end(); }
217  const_iterator end() const { return values_.end(); }
218 
223  iterator find(Key j) { return values_.find(j); }
224 
229  const_iterator find(Key j) const { return values_.find(j); }
230 
232  void print(const std::string& str = "VectorValues: ",
233  const KeyFormatter& formatter = DefaultKeyFormatter) const;
234 
236  bool equals(const VectorValues& x, double tol = 1e-9) const;
237 
241 
243  Vector vector() const;
244 
246  template <typename CONTAINER>
247  Vector vector(const CONTAINER& keys) const {
248  DenseIndex totalDim = 0;
249  FastVector<const Vector*> items;
250  items.reserve(keys.end() - keys.begin());
251  for (Key key : keys) {
252  const Vector* v = &at(key);
253  totalDim += v->size();
254  items.push_back(v);
255  }
256 
257  Vector result(totalDim);
258  DenseIndex pos = 0;
259  for (const Vector* v : items) {
260  result.segment(pos, v->size()) = *v;
261  pos += v->size();
262  }
263 
264  return result;
265  }
266 
268  Vector vector(const Dims& dims) const;
269 
271  void swap(VectorValues& other);
272 
274  bool hasSameStructure(const VectorValues other) const;
275 
279 
283  double dot(const VectorValues& v) const;
284 
286  double norm() const;
287 
289  double squaredNorm() const;
290 
293  VectorValues operator+(const VectorValues& c) const;
294 
297  VectorValues add(const VectorValues& c) const;
298 
301  VectorValues& operator+=(const VectorValues& c);
302 
305  VectorValues& addInPlace(const VectorValues& c);
306 
308  VectorValues& addInPlace_(const VectorValues& c);
309 
312  VectorValues operator-(const VectorValues& c) const;
313 
316  VectorValues subtract(const VectorValues& c) const;
317 
319  friend GTSAM_EXPORT VectorValues operator*(const double a, const VectorValues &v);
320 
322  VectorValues scale(const double a) const;
323 
325  VectorValues& operator*=(double alpha);
326 
328  VectorValues& scaleInPlace(double alpha);
329 
331 
335 
336  //inline VectorValues scale(const double a, const VectorValues& c) const { return a * (*this); }
337 
339 
340  private:
342  friend class boost::serialization::access;
343  template<class ARCHIVE>
344  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
345  ar & BOOST_SERIALIZATION_NVP(values_);
346  }
347  }; // VectorValues definition
348 
350  template<>
351  struct traits<VectorValues> : public Testable<VectorValues> {
352  };
353 
354 } // \namespace gtsam
Values::value_type value_type
Typedef to pair<Key, Vector>
Definition: VectorValues.h:83
Values::const_iterator const_iterator
Const iterator over vector values.
Definition: VectorValues.h:81
VectorValues(const CONTAINER &c)
Create from another container holding pair<Key,Vector>.
Definition: VectorValues.h:101
double dot(const V1 &a, const V2 &b)
Dot product.
Definition: Vector.h:162
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:63
VectorValues()
Default constructor creates an empty VectorValues.
Definition: VectorValues.h:93
boost::transform_iterator< boost::function1< ConstKeyValuePair, const ConstKeyValuePtrPair & >, KeyValueMap::const_iterator > const_iterator
Const forward iterator, with value type ConstKeyValuePair.
Definition: Values.h:123
Maps global variable indices to slot indices.
A thin wrapper around std::vector that uses a custom allocator.
size_t dim(Key j) const
Return the dimension of variable j.
Definition: VectorValues.h:127
const_iterator end() const
Iterator over variables.
Definition: VectorValues.h:217
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
Values values_
Vectors making up this VectorValues.
Definition: VectorValues.h:77
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
ConcurrentMap< Key, Vector > Values
Collection of Vectors making up a VectorValues.
Definition: VectorValues.h:76
Values::iterator iterator
Iterator over vector values.
Definition: VectorValues.h:80
Template to create a binary predicate.
Definition: Testable.h:110
bool exists(Key j) const
Check whether a variable with key j exists.
Definition: VectorValues.h:130
Variable ordering for the elimination algorithm.
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:229
BinarySumExpression< T > operator-(const Expression< T > &e1, const Expression< T > &e2)
Construct an expression that subtracts one expression from another.
Definition: Expression.h:279
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:149
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:150
Included from all GTSAM files.
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:164
Vector vector(const CONTAINER &keys) const
Access a vector that is a subset of relevant keys.
Definition: VectorValues.h:247
iterator end()
Iterator over variables.
Definition: VectorValues.h:216
const_iterator begin() const
Iterator over variables.
Definition: VectorValues.h:215
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
boost::transform_iterator< boost::function1< KeyValuePair, const KeyValuePtrPair & >, KeyValueMap::iterator > iterator
Mutable forward iterator, with value type KeyValuePair.
Definition: Values.h:119
std::map< Key, size_t > Dims
Keyed vector dimensions.
Definition: VectorValues.h:85
This class represents a collection of vector-valued variables associated each with a unique integer i...
Definition: VectorValues.h:73
A key-value pair, which you get by dereferencing iterators.
Definition: Values.h:101
Scatter is an intermediate data structure used when building a HessianFactor incrementally,...
Definition: Scatter.h:51
value_type KeyValuePair
Typedef to pair<Key, Vector>
Definition: VectorValues.h:84
iterator insert(Key j, const Vector &value)
Insert a vector value with key j.
Definition: VectorValues.h:187
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
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:204
size_t size() const
Number of variables stored.
Definition: VectorValues.h:124
VectorValues(ITERATOR first, ITERATOR last)
Create from a pair of iterators over pair<Key,Vector>.
Definition: VectorValues.h:108
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
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:160
iterator begin()
Iterator over variables.
Definition: VectorValues.h:214
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
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: VectorValues.h:82
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:199
VectorValues(const VectorValues &c)
Implicit copy constructor to specialize the explicit constructor from any container.
Definition: VectorValues.h:104
typedef and functions to augment Eigen's VectorXd
iterator find(Key j)
Return the iterator corresponding to the requested key, or end() if no variable is present with this ...
Definition: VectorValues.h:223
Point2 operator *(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:170
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:136