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>
24 #include <gtsam/base/FastVector.h>
25 #include <gtsam/global_includes.h>
26 
27 #include <memory>
28 
29 
30 #include <map>
31 #include <string>
32 #include <iosfwd>
33 
34 namespace gtsam {
35 
74  class GTSAM_EXPORT VectorValues {
75  protected:
76  typedef VectorValues This;
79 
81  std::map<Key, Vector> sorted() const;
82 
83  public:
84  typedef Values::iterator iterator;
85  typedef Values::const_iterator const_iterator;
86  typedef std::shared_ptr<This> shared_ptr;
89  typedef std::map<Key, size_t> Dims;
90 
93 
96 
98  VectorValues(std::initializer_list<std::pair<Key, Vector>> init)
99  : values_(init.begin(), init.end()) {}
100 
103  VectorValues(const VectorValues& first, const VectorValues& second);
104 
106  template<class CONTAINER>
107  explicit VectorValues(const CONTAINER& c) : values_(c.begin(), c.end()) {}
108 
110  VectorValues(const VectorValues& c) : values_(c.values_) {}
111 
113  template<typename ITERATOR>
114  VectorValues(ITERATOR first, ITERATOR last) : values_(first, last) {}
115 
117  VectorValues(const Vector& c, const Dims& dims);
118 
120  VectorValues(const Vector& c, const Scatter& scatter);
121 
123  static VectorValues Zero(const VectorValues& other);
124 
128 
130  size_t size() const { return values_.size(); }
131 
133  size_t dim(Key j) const { return at(j).rows(); }
134 
136  bool exists(Key j) const { return find(j) != end(); }
137 
143  iterator item = find(j);
144  if (item == end())
145  throw std::out_of_range(
146  "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
147  else
148  return item->second;
149  }
150 
155  const Vector& at(Key j) const {
156  const_iterator item = find(j);
157  if (item == end())
158  throw std::out_of_range(
159  "Requested variable '" + DefaultKeyFormatter(j) + "' is not in this VectorValues.");
160  else
161  return item->second;
162  }
163 
166  Vector& operator[](Key j) { return at(j); }
167 
170  const Vector& operator[](Key j) const { return at(j); }
171 
176 
181  iterator insert(const std::pair<Key, Vector>& key_value);
182 
187  template<class... Args>
188  inline std::pair<VectorValues::iterator, bool> emplace(Key j, Args&&... args) {
189 #if ! defined(GTSAM_USE_TBB) || defined (TBB_GREATER_EQUAL_2020)
190  return values_.emplace(std::piecewise_construct, std::forward_as_tuple(j), std::forward_as_tuple(args...));
191 #else
192  return values_.insert({j, Vector(std::forward<Args>(args)...)});
193 #endif
194  }
195 
201  return insert({j, value});
202  }
203 
207 
212  inline std::pair<iterator, bool> tryInsert(Key j, const Vector& value) {
213 #ifdef TBB_GREATER_EQUAL_2020
214  return values_.emplace(j, value);
215 #else
216  return values_.insert({j, value});
217 #endif
218  }
219 
223  if (!tryInsert(j, value).second) {
224  (*this)[j] = value;
225  }
226  }
227 
229  void erase(Key var) {
230  if (values_.unsafe_erase(var) == 0)
231  throw std::invalid_argument("Requested variable '" +
232  DefaultKeyFormatter(var) +
233  "', is not in this VectorValues.");
234  }
235 
237  void setZero();
238 
239  iterator begin() { return values_.begin(); }
240  const_iterator begin() const { return values_.begin(); }
241  iterator end() { return values_.end(); }
242  const_iterator end() const { return values_.end(); }
243 
248  iterator find(Key j) { return values_.find(j); }
249 
254  const_iterator find(Key j) const { return values_.find(j); }
255 
257  GTSAM_EXPORT friend std::ostream& operator<<(std::ostream&, const VectorValues&);
258 
260  void print(const std::string& str = "VectorValues",
262 
264  bool equals(const VectorValues& x, double tol = 1e-9) const;
265 
269 
271  Vector vector() const;
272 
274  template <typename CONTAINER>
275  Vector vector(const CONTAINER& keys) const {
276  DenseIndex totalDim = 0;
278  items.reserve(keys.end() - keys.begin());
279  for (Key key : keys) {
280  const Vector* v = &at(key);
281  totalDim += v->size();
282  items.push_back(v);
283  }
284 
285  Vector result(totalDim);
286  DenseIndex pos = 0;
287  for (const Vector* v : items) {
288  result.segment(pos, v->size()) = *v;
289  pos += v->size();
290  }
291 
292  return result;
293  }
294 
296  Vector vector(const Dims& dims) const;
297 
299  void swap(VectorValues& other);
300 
302  bool hasSameStructure(const VectorValues other) const;
303 
307 
311  double dot(const VectorValues& v) const;
312 
314  double norm() const;
315 
317  double squaredNorm() const;
318 
321  VectorValues operator+(const VectorValues& c) const;
322 
325  VectorValues add(const VectorValues& c) const;
326 
330 
333  VectorValues& addInPlace(const VectorValues& c);
334 
336  VectorValues& addInPlace_(const VectorValues& c);
337 
340  VectorValues operator-(const VectorValues& c) const;
341 
344  VectorValues subtract(const VectorValues& c) const;
345 
347  friend GTSAM_EXPORT VectorValues operator*(const double a, const VectorValues &v);
348 
350  VectorValues scale(const double a) const;
351 
353  VectorValues& operator*=(double alpha);
354 
356  VectorValues& scaleInPlace(double alpha);
357 
359 
362 
368  std::string html(
369  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
370 
372 
373  private:
374 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
375 
376  friend class boost::serialization::access;
377  template<class ARCHIVE>
378  void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
379  ar & BOOST_SERIALIZATION_NVP(values_);
380  }
381 #endif
382  }; // VectorValues definition
383 
385  template<>
386  struct traits<VectorValues> : public Testable<VectorValues> {
387  };
388 
389 } // \namespace gtsam
gtsam::VectorValues::Dims
std::map< Key, size_t > Dims
Keyed vector dimensions.
Definition: VectorValues.h:89
gtsam::ConcurrentMap< Key, Vector >
relicense.update
def update(text)
Definition: relicense.py:46
gtsam::add
static Y add(const Y &y1, const Y &y2)
Definition: HybridGaussianProductFactor.cpp:32
Vector.h
typedef and functions to augment Eigen's VectorXd
alpha
RealScalar alpha
Definition: level1_cplx_impl.h:147
FastVector.h
A thin wrapper around std::vector that uses a custom allocator.
global_includes.h
Included from all GTSAM files.
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::operator<<
std::ostream & operator<<(std::ostream &os, const Dih6 &m)
Definition: testGroup.cpp:109
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::operator-
Errors operator-(const Errors &a, const Errors &b)
Subtraction.
Definition: Errors.cpp:74
gtsam::VectorValues::find
iterator find(Key j)
Definition: VectorValues.h:248
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
gtsam::VectorValues::at
Vector & at(Key j)
Definition: VectorValues.h:142
gtsam::VectorValues::iterator
Values::iterator iterator
Iterator over vector values.
Definition: VectorValues.h:84
gtsam::VectorValues::at
const Vector & at(Key j) const
Definition: VectorValues.h:155
ConcurrentMap.h
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::VectorValues::values_
Values values_
Vectors making up this VectorValues.
Definition: VectorValues.h:78
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
Ordering.h
Variable ordering for the elimination algorithm.
Eigen::bfloat16_impl::operator*=
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 & operator*=(bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:188
iterator
Definition: pytypes.h:1460
Eigen::last
static const symbolic::SymbolExpr< internal::symbolic_last_tag > last
Definition: IndexedViewHelper.h:38
gtsam::Vector
Eigen::VectorXd Vector
Definition: Vector.h:38
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::VectorValues::VectorValues
VectorValues()
Default constructor creates an empty VectorValues.
Definition: VectorValues.h:95
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::operator*
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
gtsam::VectorValues::const_iterator
Values::const_iterator const_iterator
Const iterator over vector values.
Definition: VectorValues.h:85
gtsam::Dims
std::vector< Key > Dims
Definition: HessianFactor.cpp:41
gtsam::VectorValues::VectorValues
VectorValues(const CONTAINER &c)
Definition: VectorValues.h:107
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
gtsam::VectorValues::VectorValues
VectorValues(ITERATOR first, ITERATOR last)
Definition: VectorValues.h:114
gtsam::VectorValues
Definition: VectorValues.h:74
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::KeyFormatter
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
gtsam::dot
double dot(const V1 &a, const V2 &b)
Definition: Vector.h:195
gtsam::VectorValues::VectorValues
VectorValues(const VectorValues &c)
Definition: VectorValues.h:110
gtsam::Scatter
Definition: Scatter.h:49
gtsam::VectorValues::tryInsert
std::pair< iterator, bool > tryInsert(Key j, const Vector &value)
Definition: VectorValues.h:212
gtsam::ConcurrentMap::unsafe_erase
void unsafe_erase(typename Base::iterator position)
Definition: ConcurrentMap.h:94
gtsam::VectorValues::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: VectorValues.h:86
gtsam::VectorValues::dim
size_t dim(Key j) const
Definition: VectorValues.h:133
gtsam::equals
Definition: Testable.h:112
gtsam::operator+
HybridGaussianProductFactor operator+(const HybridGaussianProductFactor &a, const HybridGaussianProductFactor &b)
Definition: HybridGaussianProductFactor.cpp:39
str
Definition: pytypes.h:1558
key
const gtsam::Symbol key('X', 0)
gtsam::VectorValues::operator[]
Vector & operator[](Key j)
Definition: VectorValues.h:166
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
gtsam::VectorValues::Values
ConcurrentMap< Key, Vector > Values
Collection of Vectors making up a VectorValues.
Definition: VectorValues.h:77
gtsam
traits
Definition: SFMdata.h:40
gtsam::Testable
Definition: Testable.h:152
gtsam::traits
Definition: Group.h:36
gtsam::VectorValues::insert_or_assign
void insert_or_assign(Key j, const Vector &value)
Definition: VectorValues.h:222
gtsam::DenseIndex
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:103
args
Definition: pytypes.h:2210
gtsam::VectorValues::exists
bool exists(Key j) const
Definition: VectorValues.h:136
gtsam::VectorValues::size
size_t size() const
Definition: VectorValues.h:130
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
gtsam::scale
static double scale(double x, double a, double b, double t1, double t2)
Scale x from [a, b] to [t1, t2].
Definition: Chebyshev.cpp:35
gtsam::Values::value_type
KeyValuePair value_type
Definition: Values.h:106
gtsam::tol
const G double tol
Definition: Group.h:79
setZero
v setZero(3)
gtsam::VectorValues::find
const_iterator find(Key j) const
Definition: VectorValues.h:254
gtsam::html
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition: DiscreteValues.cpp:135
gtsam::VectorValues::erase
void erase(Key var)
Definition: VectorValues.h:229
gtsam::VectorValues::This
VectorValues This
Definition: VectorValues.h:76
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
gtsam::VectorValues::KeyValuePair
value_type KeyValuePair
Typedef to pair<Key, Vector>
Definition: VectorValues.h:88
pos
Definition: example-NearestNeighbor.cpp:32
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::VectorValues::vector
Vector vector(const CONTAINER &keys) const
Definition: VectorValues.h:275
gtsam::VectorValues::emplace
std::pair< VectorValues::iterator, bool > emplace(Key j, Args &&... args)
Definition: VectorValues.h:188
insert
A insert(1, 2)=0
swap
int EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:130
init
Definition: TutorialInplaceLU.cpp:2
Eigen::bfloat16_impl::operator+=
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 & operator+=(bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:184
Scatter.h
Maps global variable indices to slot indices.
test_callbacks.value
value
Definition: test_callbacks.py:160
gtsam::VectorValues::VectorValues
VectorValues(std::initializer_list< std::pair< Key, Vector >> init)
Construct from initializer list.
Definition: VectorValues.h:98
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
gtsam::VectorValues::insert
iterator insert(Key j, const Vector &value)
Definition: VectorValues.h:200
gtsam::VectorValues::value_type
Values::value_type value_type
Typedef to pair<Key, Vector>
Definition: VectorValues.h:87
gtsam::VectorValues::operator[]
const Vector & operator[](Key j) const
Definition: VectorValues.h:170


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:09:41