21 #include <boost/bind.hpp> 22 #include <boost/range/combine.hpp> 23 #include <boost/range/numeric.hpp> 24 #include <boost/range/adaptor/transformed.hpp> 25 #include <boost/range/adaptor/map.hpp> 32 using boost::adaptors::transformed;
33 using boost::adaptors::map_values;
34 using boost::accumulate;
40 merge(first.
begin(), first.
end(), second.
begin(), second.
end(), inserter(values_, values_.end()),
43 throw invalid_argument(
"Requested to merge two VectorValues that have one or more variables in common.");
47 VectorValues::VectorValues(
const Vector&
x,
const Dims& dims) {
48 using Pair = pair<const Key, size_t>;
50 for (
const Pair&
v : dims) {
53 boost::tie(key, n) =
v;
54 #ifdef TBB_GREATER_EQUAL_2020 55 values_.emplace(key, x.segment(j, n));
57 values_.insert(std::make_pair(key, x.segment(j, n)));
67 #ifdef TBB_GREATER_EQUAL_2020 68 values_.emplace(
v.key, x.segment(j,
v.dimension));
70 values_.insert(std::make_pair(
v.key, x.segment(j,
v.dimension)));
81 #ifdef TBB_GREATER_EQUAL_2020 82 result.
values_.emplace(
v.first, Vector::Zero(
v.second.size()));
84 result.
values_.insert(std::make_pair(
v.first, Vector::Zero(
v.second.size())));
91 std::pair<iterator, bool>
result = values_.insert(key_value);
93 throw std::invalid_argument(
95 +
"' already in this VectorValues.");
106 size_t oldSize = values_.size();
107 hint = values_.insert(hint, key_value);
108 if(values_.size() > oldSize) {
109 values_.unsafe_erase(hint);
110 throw out_of_range(
"Requested to update a VectorValues with another VectorValues that contains keys not present in the first.");
112 hint->second = key_value.second;
120 size_t originalSize =
size();
121 values_.insert(values.
begin(), values.
end());
122 if(
size() != originalSize + values.
size())
123 throw invalid_argument(
"Requested to insert a VectorValues into another VectorValues that already contains one or more of its keys.");
129 for(
Vector&
v: values_ | map_values)
137 map<Key, Vector> sorted;
138 for (
const auto& key_value : v) {
139 sorted.emplace(key_value.first, key_value.second);
141 for (
const auto& key_value : sorted)
143 for (
const auto& key_value : v)
146 os <<
" " <<
StreamedKey(key_value.first) <<
": " << key_value.second.transpose()
155 cout << str <<
": " <<
size() <<
" elements\n";
164 for(
const auto values: boost::combine(*
this, x)) {
176 for (
const Vector&
v : *
this | map_values) totalDim +=
v.size();
181 for (
const Vector&
v : *
this | map_values) {
182 result.segment(pos,
v.size()) =
v;
194 for(
size_t dim: keys | map_values)
198 for(
const Dims::value_type& it: keys) {
199 result.segment(j,it.second) = at(it.first);
207 this->values_.swap(other.
values_);
216 return vv.get<0>().
first == vv.get<1>().first
217 && vv.get<0>().second.size() == vv.get<1>().second.size();
224 return accumulate(combine(*
this, other)
232 throw invalid_argument(
"VectorValues::dot called with a VectorValues of different structure");
234 typedef boost::tuple<value_type, value_type> ValuePair;
235 using boost::adaptors::map_values;
236 for(
const ValuePair
values: boost::combine(*
this, v)) {
238 invalid_argument(
"VectorValues::dot called with a VectorValues of different structure"));
240 invalid_argument(
"VectorValues::dot called with a VectorValues of different structure"));
241 result +=
values.get<0>().second.dot(
values.get<1>().second);
247 double VectorValues::norm()
const {
252 double VectorValues::squaredNorm()
const {
253 double sumSquares = 0.0;
254 using boost::adaptors::map_values;
255 for(
const Vector&
v: *
this | map_values)
256 sumSquares +=
v.squaredNorm();
264 throw invalid_argument(
"VectorValues::operator+ called with different vector sizes");
266 invalid_argument(
"VectorValues::operator+ called with different vector sizes"));
271 #ifdef TBB_GREATER_EQUAL_2020
272 result.
values_.emplace(j1->first, j1->second + j2->second);
274 result.
values_.insert(std::make_pair(j1->first, j1->second + j2->second));
290 throw invalid_argument(
"VectorValues::operator+= called with different vector sizes");
292 invalid_argument(
"VectorValues::operator+= called with different vector sizes"));
297 for(; j1 !=
end(); ++j1, ++j2)
298 j1->second += j2->second;
313 pair<VectorValues::iterator, bool>
xi = tryInsert(j2->first,
Vector());
315 xi.first->second = j2->second;
317 xi.first->second += j2->second;
326 throw invalid_argument(
"VectorValues::operator- called with different vector sizes");
328 invalid_argument(
"VectorValues::operator- called with different vector sizes"));
333 #ifdef TBB_GREATER_EQUAL_2020
334 result.
values_.emplace(j1->first, j1->second - j2->second);
336 result.
values_.insert(std::make_pair(j1->first, j1->second - j2->second));
353 #ifdef TBB_GREATER_EQUAL_2020 354 result.
values_.emplace(key_v.first, a * key_v.second);
356 result.
values_.insert(std::make_pair(key_v.first, a * key_v.second));
370 for(
Vector&
v: *
this | map_values)
378 return *
this *=
alpha;
void print(const Matrix &A, const string &s, ostream &stream)
Vector3_ operator*(const Double_ &s, const Vector3_ &v)
One SlotEntry stores the slot index for a variable, as well its dim.
Values::iterator iterator
Iterator over vector values.
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half & operator*=(half &a, const half &b)
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
Values::value_type value_type
Typedef to pair<Key, Vector>
iterator end()
Iterator over variables.
std::map< Key, size_t > Dims
Keyed vector dimensions.
GTSAM_EXPORT ostream & operator<<(ostream &os, const VectorValues &v)
static const KeyFormatter DefaultKeyFormatter
ptrdiff_t DenseIndex
The index type for Eigen objects.
const KeyFormatter & formatter
graph add(boost::make_shared< UnaryFactor >(1, 0.0, 0.0, unaryNoise))
int EIGEN_BLAS_FUNC() swap(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Scalar EIGEN_BLAS_FUNC() dot(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
constexpr int first(int i)
Implementation details for constexpr functions.
Values values_
Vectors making up this VectorValues.
To use the key_formatter on Keys, they must be wrapped in a StreamedKey.
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
value_type KeyValuePair
Typedef to pair<Key, Vector>
Values::const_iterator const_iterator
Const iterator over vector values.
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half & operator+=(half &a, const half &b)
bool structureCompareOp(const boost::tuple< VectorValues::value_type, VectorValues::value_type > &vv)
bool equal_with_abs_tol(const Eigen::DenseBase< MATRIX > &A, const Eigen::DenseBase< MATRIX > &B, double tol=1e-9)
const mpreal dim(const mpreal &a, const mpreal &b, mp_rnd_t r=mpreal::get_default_rnd())
ofstream os("timeSchurFactors.csv")
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 y set format x g set format y g set format x2 g set format y2 g set format z g set angles radians set nogrid set key title set key left top Right noreverse box linetype linewidth samplen spacing width set nolabel set noarrow set nologscale set logscale x set set pointsize set encoding default set nopolar set noparametric set set set set surface set nocontour set clabel set mapping cartesian set nohidden3d set cntrparam order set cntrparam linear set cntrparam levels auto set cntrparam points set size set set xzeroaxis lt lw set x2zeroaxis lt lw set yzeroaxis lt lw set y2zeroaxis lt lw set tics in set ticslevel set tics scale
Jet< T, N > operator-(const Jet< T, N > &f)
#define assert_throw(CONDITION, EXCEPTION)
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
iterator begin()
Iterator over variables.
std::uint64_t Key
Integer nonlinear key type.
Jet< T, N > const & operator+(const Jet< T, N > &f)