gtsam
Loading...
Searching...
No Matches
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
17
18#pragma once
19
21#include <gtsam/base/Vector.h>
22#include <gtsam/base/ConcurrentMap.h>
25
26#include <memory>
27
28
29#include <map>
30#include <string>
31#include <iosfwd>
32
33namespace gtsam {
34
73 class GTSAM_EXPORT VectorValues {
74 protected:
75 typedef VectorValues This;
78
79 public:
80 typedef Values::iterator iterator;
81 typedef Values::const_iterator const_iterator;
82 typedef std::shared_ptr<This> shared_ptr;
83 typedef Values::value_type value_type;
85 typedef std::map<Key, size_t> Dims;
86
89
92
94 VectorValues(std::initializer_list<std::pair<Key, Vector>> init)
95 : values_(init.begin(), init.end()) {}
96
99 VectorValues(const VectorValues& first, const VectorValues& second);
100
102 template<class CONTAINER>
103 explicit VectorValues(const CONTAINER& c) : values_(c.begin(), c.end()) {}
104
107
109 template<typename ITERATOR>
110 VectorValues(ITERATOR first, ITERATOR last) : values_(first, last) {}
111
113 VectorValues(const Vector& c, const Dims& dims);
114
116 VectorValues(const Vector& c, const Scatter& scatter);
117
118 // We override the copy constructor; expicitly declare operator=
119 VectorValues& operator=(const VectorValues& other) = default;
120
122 static VectorValues Zero(const VectorValues& other);
123
127
129 size_t size() const { return values_.size(); }
130
132 size_t dim(Key j) const { return at(j).rows(); }
133
135 bool exists(Key j) const { return find(j) != end(); }
136
141 Vector& at(Key j) {
142 iterator item = find(j);
143 if (item == end())
144 throw std::out_of_range(
145 "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
146 else
147 return item->second;
148 }
149
154 const Vector& at(Key j) const {
155 const_iterator item = find(j);
156 if (item == end())
157 throw std::out_of_range(
158 "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
159 else
160 return item->second;
161 }
162
165 Vector& operator[](Key j) { return at(j); }
166
169 const Vector& operator[](Key j) const { return at(j); }
170
174 VectorValues& update(const VectorValues& values);
175
180 iterator insert(const std::pair<Key, Vector>& key_value);
181
186 template<class... Args>
187 inline std::pair<VectorValues::iterator, bool> emplace(Key j, Args&&... args) {
188#if ! defined(GTSAM_USE_TBB) || defined (TBB_GREATER_EQUAL_2020)
189 return values_.emplace(std::piecewise_construct, std::forward_as_tuple(j), std::forward_as_tuple(args...));
190#else
191 return values_.insert({j, Vector(std::forward<Args>(args)...)});
192#endif
193 }
194
199 iterator insert(Key j, const Vector& value) {
200 return insert({j, value});
201 }
202
205 VectorValues& insert(const VectorValues& values);
206
215 VectorValues& insert(const Vector& values, const KeyVector& keys,
216 const Dims& dims);
217
222 inline std::pair<iterator, bool> tryInsert(Key j, const Vector& value) {
223#ifdef TBB_GREATER_EQUAL_2020
224 return values_.emplace(j, value);
225#else
226 return values_.insert({j, value});
227#endif
228 }
229
232 void insert_or_assign(Key j, const Vector& value) {
233 if (!tryInsert(j, value).second) {
234 (*this)[j] = value;
235 }
236 }
237
239 void erase(Key var) {
240 if (values_.unsafe_erase(var) == 0)
241 throw std::invalid_argument("Requested variable '" +
243 "', is not in this VectorValues.");
244 }
245
247 void setZero();
248
249 iterator begin() { return values_.begin(); }
250 const_iterator begin() const { return values_.begin(); }
251 iterator end() { return values_.end(); }
252 const_iterator end() const { return values_.end(); }
253
258 iterator find(Key j) { return values_.find(j); }
259
264 const_iterator find(Key j) const { return values_.find(j); }
265
267 GTSAM_EXPORT friend std::ostream& operator<<(std::ostream&, const VectorValues&);
268
270 void print(const std::string& str = "VectorValues",
271 const KeyFormatter& formatter = DefaultKeyFormatter) const;
272
274 bool equals(const VectorValues& x, double tol = 1e-9) const;
275
279
281 Vector vector() const;
282
284 template <typename CONTAINER>
285 DenseIndex totalDim(const CONTAINER& keys) const {
287 for (Key key : keys) {
288 totalDim += static_cast<DenseIndex>(at(key).size());
289 }
290 return totalDim;
291 }
292
294 template <typename CONTAINER, typename Derived>
295 void fillVector(const CONTAINER& keys,
296 const Eigen::MatrixBase<Derived>& result) const {
297 auto& writable = const_cast<Eigen::MatrixBase<Derived>&>(result);
298 DenseIndex pos = 0;
299 for (Key key : keys) {
300 const Vector& v = at(key);
301 writable.segment(pos, v.size()) = v;
302 pos += v.size();
303 }
304 }
305
307 template <typename CONTAINER>
308 Vector vector(const CONTAINER& keys) const {
311 items.reserve(keys.size());
312 for (Key key : keys) {
313 const Vector* v = &at(key);
314 totalDim += v->size();
315 items.emplace_back(v);
316 }
317
318 Vector result(totalDim);
319 DenseIndex pos = 0;
320 for (const Vector* v : items) {
321 result.segment(pos, v->size()) = *v;
322 pos += v->size();
323 }
324
325 return result;
326 }
327
329 Vector vector(const Dims& dims) const;
330
332 void swap(VectorValues& other);
333
335 bool hasSameStructure(const VectorValues other) const;
336
340
344 double dot(const VectorValues& v) const;
345
347 double norm() const;
348
350 double squaredNorm() const;
351
354 VectorValues operator+(const VectorValues& c) const;
355
358 VectorValues add(const VectorValues& c) const;
359
362 VectorValues& operator+=(const VectorValues& c);
363
366 VectorValues& addInPlace(const VectorValues& c);
367
369 VectorValues& addInPlace_(const VectorValues& c);
370
373 VectorValues operator-(const VectorValues& c) const;
374
377 VectorValues subtract(const VectorValues& c) const;
378
380 friend GTSAM_EXPORT VectorValues operator*(const double a, const VectorValues &v);
381
383 VectorValues scale(const double a) const;
384
386 VectorValues& operator*=(double alpha);
387
389 VectorValues& scaleInPlace(double alpha);
390
392 std::map<Key, const Vector&> sorted() const;
393
395
398
404 std::string html(
405 const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
406
408
409 private:
410#if GTSAM_ENABLE_BOOST_SERIALIZATION
412 friend class boost::serialization::access;
413 template<class ARCHIVE>
414 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
415 ar & BOOST_SERIALIZATION_NVP(values_);
416 }
417#endif
418 }; // VectorValues definition
419
421 template<>
422 struct traits<VectorValues> : public Testable<VectorValues> {
423 };
424
425} // \namespace gtsam
A thin wrapper around std::vector that uses a custom allocator.
typedef and functions to augment Eigen's VectorXd
Maps global variable indices to slot indices.
Included from all GTSAM files.
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition DiscreteValues.cpp:160
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition types.h:49
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition Point2.h:52
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
double dot(const V1 &a, const V2 &b)
Dot product.
Definition Vector.h:191
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
Errors operator-(const Errors &a, const Errors &b)
Subtraction.
Definition Errors.cpp:74
FastMap is a thin wrapper around std::map that uses the boost fast_pool_allocator instead of the defa...
Definition ConcurrentMap.h:69
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Template to create a binary predicate.
Definition Testable.h:112
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
Scatter is an intermediate data structure used when building a HessianFactor incrementally,...
Definition Scatter.h:49
VectorValues represents a collection of vector-valued variables associated each with a unique integer...
Definition VectorValues.h:73
iterator insert(const std::pair< Key, Vector > &key_value)
Insert a vector value with key j.
Definition VectorValues.cpp:90
value_type KeyValuePair
Typedef to pair<Key, Vector>.
Definition VectorValues.h:84
void fillVector(const CONTAINER &keys, const Eigen::MatrixBase< Derived > &result) const
Fill a preallocated Eigen vector expression with a subset of relevant keys.
Definition VectorValues.h:295
const_iterator end() const
Iterator over variables.
Definition VectorValues.h:252
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
iterator end()
Iterator over variables.
Definition VectorValues.h:251
DenseIndex totalDim(const CONTAINER &keys) const
Compute the total dimension of a subset of relevant keys.
Definition VectorValues.h:285
Values::iterator iterator
Iterator over vector values.
Definition VectorValues.h:80
iterator insert(Key j, const Vector &value)
Insert a vector value with key j.
Definition VectorValues.h:199
VectorValues(const CONTAINER &c)
Create from another container holding pair<Key,Vector>.
Definition VectorValues.h:103
Values values_
Vectors making up this VectorValues.
Definition VectorValues.h:77
std::map< Key, size_t > Dims
Keyed vector dimensions.
Definition VectorValues.h:85
ConcurrentMap< Key, Vector > Values
Collection of Vectors making up a VectorValues.
Definition VectorValues.h:76
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:165
std::pair< VectorValues::iterator, bool > emplace(Key j, Args &&... args)
Emplace a vector value with key j.
Definition VectorValues.h:187
iterator find(Key j)
Return the iterator corresponding to the requested key, or end() if no variable is present with this ...
Definition VectorValues.h:258
size_t dim(Key j) const
Return the dimension of variable j.
Definition VectorValues.h:132
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition VectorValues.h:82
size_t size() const
Number of variables stored.
Definition VectorValues.h:129
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:169
bool exists(Key j) const
Check whether a variable with key j exists.
Definition VectorValues.h:135
VectorValues()
Default constructor creates an empty VectorValues.
Definition VectorValues.h:91
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:264
VectorValues(ITERATOR first, ITERATOR last)
Create from a pair of iterators over pair<Key,Vector>.
Definition VectorValues.h:110
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:154
Vector vector(const CONTAINER &keys) const
Access a vector that is a subset of relevant keys.
Definition VectorValues.h:308
void insert_or_assign(Key j, const Vector &value)
insert_or_assign that mimics the STL map insert_or_assign - if the value already exists,...
Definition VectorValues.h:232
const_iterator begin() const
Iterator over variables.
Definition VectorValues.h:250
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:141
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:239
VectorValues(std::initializer_list< std::pair< Key, Vector > > init)
Construct from initializer list.
Definition VectorValues.h:94
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:222
VectorValues(const VectorValues &c)
Copy constructor to specialize the explicit constructor from any container.
Definition VectorValues.h:106
iterator begin()
Iterator over variables.
Definition VectorValues.h:249