Loading...
Searching...
No Matches
gtsam::VectorValues Class Reference
Linear

## Detailed Description

VectorValues represents a collection of vector-valued variables associated each with a unique integer index.

It is typically used to store the variables of a GaussianFactorGraph. Optimizing a GaussianFactorGraph or GaussianBayesNet returns this class.

For basic usage, such as receiving a linear solution from gtsam solving functions, or creating this class in unit tests and examples where speed is not important, you can use a simple interface:

- The default constructor VectorValues() to create this class
- insert(Key, const Vector&) to add vector variables
- operator[](Key) for read and write access to stored variables
- exists (Key) to check if a variable is present
- Other facilities like iterators, size(), dim(), etc.

Example:

VectorValues values;
values.emplace(3, Vector3(1.0, 2.0, 3.0));
values.emplace(4, Vector2(4.0, 5.0));
values.emplace(0, (Vector(4) << 6.0, 7.0, 8.0, 9.0).finished());
// Prints [ 3.0 4.0 ]
gtsam::print(values[1]);
// Prints [ 8.0 9.0 ]
values[1] = Vector2(8.0, 9.0);
gtsam::print(values[1]);
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:156
gtsam::VectorValues::emplace
std::pair< VectorValues::iterator, bool > emplace(Key j, Args &&... args)
Emplace a vector value with key j.
Definition VectorValues.h:185
gtsam::VectorValues::VectorValues
VectorValues()
Default constructor creates an empty VectorValues.
Definition VectorValues.h:92

## Advanced Interface and Performance Information

Access is through the variable Key j, and returns a SubVector, which is a view on the underlying data structure.

This class is additionally used in gradient descent and dog leg to store the gradient.

## Standard Interface

GTSAM_EXPORT friend std::ostream & | operator<< (std::ostream &os, const VectorValues &v)
| overload operator << to print to stringstream
size_t | size () const
| Number of variables stored.
size_t | dim (Key j) const
| Return the dimension of variable `j`.
bool | exists (Key j) const
| Check whether a variable with key `j` exists.
Vector & | at (Key j)
| Read/write access to the vector value with key `j`, throws std::out_of_range if `j` does not exist, identical to operator[](Key).
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, identical to operator[](Key).
Vector & | operator[] (Key j)
| Read/write access to the vector value with key `j`, throws std::out_of_range if `j` does not exist, identical to at(Key).
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, identical to at(Key).
VectorValues & | update (const VectorValues &values)
| For all key/value pairs in `values`, replace values with corresponding keys in this class with those in `values`.
iterator | insert (const std::pair< Key, Vector > &key_value)
| Insert a vector `value` with key `j`.
template<class... Args>
std::pair< VectorValues::iterator, bool > | emplace (Key j, Args &&... args)
| Emplace a vector `value` with key `j`.
iterator | insert (Key j, const Vector &value)
| Insert a vector `value` with key `j`.
VectorValues & | insert (const VectorValues &values)
| Insert all values from `values`.
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 iterator to the existing value is returned, along with 'false'.
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, the map is updated, otherwise a new value is inserted at j.
void | erase (Key var)
| Erase the vector with the given key, or throw std::out_of_range if it does not exist.
void | setZero ()
| Set all values to zero vectors.
iterator | begin ()
| Iterator over variables.
const_iterator | begin () const
| Iterator over variables.
iterator | end ()
| Iterator over variables.
const_iterator | end () const
| Iterator over variables.
iterator | find (Key j)
| Return the iterator corresponding to the requested key, or end() if no variable is present with this key.
const_iterator | find (Key j) const
| Return the iterator corresponding to the requested key, or end() if no variable is present with this key.
void | print (const std::string &str="VectorValues", const KeyFormatter &formatter=DefaultKeyFormatter) const
| print required by Testable for unit testing
bool | equals (const VectorValues &x, double tol=1e-9) const
| equals required by Testable for unit testing

## Linear algebra operations

GTSAM_EXPORT VectorValues | operator* (const double a, const VectorValues &v)
| Element-wise scaling by a constant.
double | dot (const VectorValues &v) const
| Dot product with another VectorValues, interpreting both as vectors of their concatenated values.
double | norm () const
| Vector L2 norm.
double | squaredNorm () const
| Squared vector L2 norm.
VectorValues | operator+ (const VectorValues &c) const
| Element-wise addition, synonym for add().
VectorValues | add (const VectorValues &c) const
| Element-wise addition, synonym for operator+().
VectorValues & | operator+= (const VectorValues &c)
| Element-wise addition in-place, synonym for operator+=().
VectorValues & | addInPlace (const VectorValues &c)
| Element-wise addition in-place, synonym for operator+=().
VectorValues & | addInPlace_ (const VectorValues &c)
| Element-wise addition in-place, but allows for empty slots in *this.
VectorValues | operator- (const VectorValues &c) const
| Element-wise subtraction, synonym for subtract().
VectorValues | subtract (const VectorValues &c) const
| Element-wise subtraction, synonym for operator-().
VectorValues | scale (const double a) const
| Element-wise scaling by a constant.
VectorValues & | operator*= (double alpha)
| Element-wise scaling by a constant in-place.
VectorValues & | scaleInPlace (double alpha)
| Element-wise scaling by a constant in-place.

## Standard Constructors

| VectorValues ()
| Default constructor creates an empty VectorValues.
| VectorValues (std::initializer_list< std::pair< Key, Vector > > init)
| Construct from initializer list.
| VectorValues (const VectorValues &first, const VectorValues &second)
| Merge two VectorValues into one, this is more efficient than inserting elements one by one.
template<class CONTAINER>
| VectorValues (const CONTAINER &c)
| Create from another container holding pair<Key,Vector>.
| VectorValues (const VectorValues &c)
| Implicit copy constructor to specialize the explicit constructor from any container.
template<typename ITERATOR>
| VectorValues (ITERATOR first, ITERATOR last)
| Create from a pair of iterators over pair<Key,Vector>.
| VectorValues (const Vector &c, const Dims &dims)
| Constructor from Vector, with Dims.
| VectorValues (const Vector &c, const Scatter &scatter)
| Constructor from Vector, with Scatter.
static VectorValues | Zero (const VectorValues &other)
| Create a VectorValues with the same structure as `other`, but filled with zeros.

## Public Member Functions

Advanced Interface
Vector | vector () const
| Retrieve the entire solution as a single vector.
template<typename CONTAINER>
Vector | vector (const CONTAINER &keys) const
| Access a vector that is a subset of relevant keys.
Vector | vector (const Dims &dims) const
| Access a vector that is a subset of relevant keys, dims version.
void | swap (VectorValues &other)
| Swap the data in this VectorValues with another.
bool | hasSameStructure (const VectorValues other) const
| Check if this VectorValues has the same structure (keys and dimensions) as another.
Wrapper support
std::string | html (const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
| Output as a html table.

## Public Types

typedef Values::iterator | iterator
| Iterator over vector values.
typedef Values::const_iterator | const_iterator
| Const iterator over vector values.
typedef boost::shared_ptr< This > | shared_ptr
| shared_ptr to this class
typedef Values::value_type | value_type
| Typedef to pair<Key, Vector>.
typedef value_type | KeyValuePair
| Typedef to pair<Key, Vector>.
typedef std::map< Key, size_t > | Dims
| Keyed vector dimensions.

## Protected Types

typedef VectorValues | This
typedef ConcurrentMap< Key, Vector > | Values
| Collection of Vectors making up a VectorValues.

## Protected Attributes

Values Values | values_
| Vectors making up this VectorValues.

## Friends

class | boost::serialization::access
| Serialization function.

## Member Function Documentation

## add()

VectorValues VectorValues::add | ( | const VectorValues & | c | ) | const

Element-wise addition, synonym for operator+().

Both VectorValues must have the same structure (checked when NDEBUG is not defined).

## addInPlace()

VectorValues & VectorValues::addInPlace | ( | const VectorValues & | c | ) |

Element-wise addition in-place, synonym for operator+=().

Both VectorValues must have the same structure (checked when NDEBUG is not defined).

## addInPlace_()

VectorValues & VectorValues::addInPlace_ | ( | const VectorValues & | c | ) |

Element-wise addition in-place, but allows for empty slots in *this.

Slower

## dot()

double VectorValues::dot | ( | const VectorValues & | v | ) | const

Dot product with another VectorValues, interpreting both as vectors of their concatenated values.

Both VectorValues must have the same structure (checked when NDEBUG is not defined).

## emplace()

template<class... Args>

std::pair< VectorValues::iterator, bool > gtsam::VectorValues::emplace | ( | Key | j,
|  | Args &&... | args )

inline

Emplace a vector `value` with key `j`.

Throws an invalid_argument exception if the key `j` is already used.

Parameters

  value | The vector to be inserted.
  j | The index with which the value will be associated.

## html()

string VectorValues::html | ( | const KeyFormatter & | keyFormatter = DefaultKeyFormatter | ) | const

Output as a html table.

Parameters

  keyFormatter | function that formats keys.

## insert() [1/3]

VectorValues::iterator VectorValues::insert | ( | const std::pair< Key, Vector > & | key_value | ) |

Insert a vector `value` with key `j`.

Throws an invalid_argument exception if the key `j` is already used.

Parameters

  value | The vector to be inserted.
  j | The index with which the value will be associated.

## insert() [2/3]

VectorValues & VectorValues::insert | ( | const VectorValues & | values | ) |

Insert all values from `values`.

Throws an invalid_argument exception if any keys to be inserted are already used.

## insert() [3/3]

iterator gtsam::VectorValues::insert | ( | Key | j,
|  | const Vector & | value )

inline

Insert a vector `value` with key `j`.

Throws an invalid_argument exception if the key `j` is already used.

Parameters

  value | The vector to be inserted.
  j | The index with which the value will be associated.

## operator+()

VectorValues VectorValues::operator+ | ( | const VectorValues & | c | ) | const

Element-wise addition, synonym for add().

Both VectorValues must have the same structure (checked when NDEBUG is not defined).

## operator+=()

VectorValues & VectorValues::operator+= | ( | const VectorValues & | c | ) |

Element-wise addition in-place, synonym for operator+=().

Both VectorValues must have the same structure (checked when NDEBUG is not defined).

## operator-()

VectorValues VectorValues::operator- | ( | const VectorValues & | c | ) | const

Element-wise subtraction, synonym for subtract().

Both VectorValues must have the same structure (checked when NDEBUG is not defined).

## subtract()

VectorValues VectorValues::subtract | ( | const VectorValues & | c | ) | const

Element-wise subtraction, synonym for operator-().

Both VectorValues must have the same structure (checked when NDEBUG is not defined).

## tryInsert()

std::pair< iterator, bool > gtsam::VectorValues::tryInsert | ( | Key | j,
|  | const Vector & | value )

inline

insert that mimics the STL map insert - if the value already exists, the map is not modified and an iterator to the existing value is returned, along with 'false'.

If the value did not exist, it is inserted and an iterator pointing to the new element, along with 'true', is returned.

## update()

VectorValues & VectorValues::update | ( | const VectorValues & | values | ) |

For all key/value pairs in `values`, replace values with corresponding keys in this class with those in `values`.

Throws std::out_of_range if any keys in `values` are not present in this class.

The documentation for this class was generated from the following files:

- /build/gtsam-gchP9J/gtsam-4.2.2+dfsg/gtsam/linear/VectorValues.h
- /build/gtsam-gchP9J/gtsam-4.2.2+dfsg/gtsam/linear/VectorValues.cpp