gtsam 4.1.1
gtsam
Expression-inl.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
20#pragma once
21
22#include <gtsam/nonlinear/internal/ExpressionNode.h>
23
24#include <boost/bind/bind.hpp>
25#include <boost/tuple/tuple.hpp>
26#include <boost/range/adaptor/map.hpp>
27#include <boost/range/algorithm.hpp>
28
29namespace gtsam {
30
31template<typename T>
33 root_(new internal::ConstantExpression<T>(value)) {
34}
35
36template<typename T>
38 root_(new internal::LeafExpression<T>(key)) {
39}
40
41template<typename T>
43 root_(new internal::LeafExpression<T>(symbol)) {
44}
45
46template<typename T>
47Expression<T>::Expression(unsigned char c, std::uint64_t j) :
48 root_(new internal::LeafExpression<T>(Symbol(c, j))) {
49}
50
52template<typename T>
53template<typename A>
54Expression<T>::Expression(typename UnaryFunction<A>::type function,
55 const Expression<A>& expression) :
56 root_(new internal::UnaryExpression<T, A>(function, expression)) {
57}
58
60template<typename T>
61template<typename A1, typename A2>
62Expression<T>::Expression(typename BinaryFunction<A1, A2>::type function,
63 const Expression<A1>& expression1, const Expression<A2>& expression2) :
64 root_(
65 new internal::BinaryExpression<T, A1, A2>(function, expression1,
66 expression2)) {
67}
68
70template<typename T>
71template<typename A1, typename A2, typename A3>
72Expression<T>::Expression(typename TernaryFunction<A1, A2, A3>::type function,
73 const Expression<A1>& expression1, const Expression<A2>& expression2,
74 const Expression<A3>& expression3) :
75 root_(
76 new internal::TernaryExpression<T, A1, A2, A3>(function, expression1,
77 expression2, expression3)) {
78}
79
81template<typename T>
82template<typename A>
84 T (A::*method)(typename MakeOptionalJacobian<T, A>::type) const) :
85 root_(
86 new internal::UnaryExpression<T, A>(std::bind(method,
87 std::placeholders::_1, std::placeholders::_2),
88 expression)) {
89}
90
92template<typename T>
93template<typename A1, typename A2>
95 T (A1::*method)(const A2&, typename MakeOptionalJacobian<T, A1>::type,
97 const Expression<A2>& expression2) :
98 root_(
99 new internal::BinaryExpression<T, A1, A2>(
100 std::bind(method, std::placeholders::_1,
101 std::placeholders::_2, std::placeholders::_3,
102 std::placeholders::_4),
103 expression1, expression2)) {
104}
107template<typename T>
108template<typename A1, typename A2, typename A3>
110 T (A1::*method)(const A2&, const A3&,
113 typename MakeOptionalJacobian<T, A3>::type) const,
114 const Expression<A2>& expression2, const Expression<A3>& expression3) :
115 root_(
116 new internal::TernaryExpression<T, A1, A2, A3>(
117 std::bind(method, std::placeholders::_1,
118 std::placeholders::_2, std::placeholders::_3,
119 std::placeholders::_4, std::placeholders::_5,
120 std::placeholders::_6),
121 expression1, expression2, expression3)) {
122}
123
124template<typename T>
125std::set<Key> Expression<T>::keys() const {
126 return root_->keys();
127}
128
129template<typename T>
130void Expression<T>::dims(std::map<Key, int>& map) const {
131 root_->dims(map);
132}
134template<typename T>
135void Expression<T>::print(const std::string& s) const {
136 root_->print(s);
137}
138
139template<typename T>
141 boost::optional<std::vector<Matrix>&> H) const {
142
143 if (H) {
144 // Call private version that returns derivatives in H
146 FastVector<int> dims;
147 boost::tie(keys, dims) = keysAndDims();
148 return valueAndDerivatives(values, keys, dims, *H);
149 } else
150 // no derivatives needed, just return value
151 return root_->value(values);
152}
153
154template<typename T>
155const boost::shared_ptr<internal::ExpressionNode<T> >& Expression<T>::root() const {
156 return root_;
157}
159template<typename T>
161 return root_->traceSize();
162}
163
164// Private methods:
165
166template<typename T>
168 const KeyVector& keys, const FastVector<int>& dims,
169 std::vector<Matrix>& H) const {
170
171 // H should be pre-allocated
172 assert(H.size()==keys.size());
173
174 // Pre-allocate and zero VerticalBlockMatrix
175 static const int Dim = traits<T>::dimension;
176 VerticalBlockMatrix Ab(dims, Dim);
177 Ab.matrix().setZero();
178 internal::JacobianMap jacobianMap(keys, Ab);
179
180 // Call unsafe version
181 T result = valueAndJacobianMap(values, jacobianMap);
182
183 // Copy blocks into the vector of jacobians passed in
184 for (DenseIndex i = 0; i < static_cast<DenseIndex>(keys.size()); i++)
185 H[i] = Ab(i);
186
187 return result;
188}
190template<typename T>
192 internal::ExecutionTrace<T>& trace, void* traceStorage) const {
193 return root_->traceExecution(values, trace,
194 static_cast<internal::ExecutionTraceStorage*>(traceStorage));
195}
196
197template<typename T>
199 internal::JacobianMap& jacobians) const {
200 // The following piece of code is absolutely crucial for performance.
201 // We allocate a block of memory on the stack, which can be done at runtime
202 // with modern C++ compilers. The traceExecution then fills this memory
203 // with an execution trace, made up entirely of "Record" structs, see
204 // the FunctionalNode class in expression-inl.h
205 size_t size = traceSize();
206
207 // Windows does not support variable length arrays, so memory must be dynamically
208 // allocated on Visual Studio. For more information see the issue below
209 // https://bitbucket.org/gtborg/gtsam/issue/178/vlas-unsupported-in-visual-studio
210#ifdef _MSC_VER
211 auto traceStorage = static_cast<internal::ExecutionTraceStorage*>(_aligned_malloc(size, internal::TraceAlignment));
212#else
213 internal::ExecutionTraceStorage traceStorage[size];
214#endif
215
217 T value(this->traceExecution(values, trace, traceStorage));
218 trace.startReverseAD1(jacobians);
219
220#ifdef _MSC_VER
221 _aligned_free(traceStorage);
222#endif
223
224 return value;
225}
226
227template<typename T>
229 std::map<Key, int> map;
230 dims(map);
231 size_t n = map.size();
232 KeysAndDims pair = std::make_pair(KeyVector(n), FastVector<int>(n));
233 boost::copy(map | boost::adaptors::map_keys, pair.first.begin());
234 boost::copy(map | boost::adaptors::map_values, pair.second.begin());
235 return pair;
236}
237
238namespace internal {
239// http://stackoverflow.com/questions/16260445/boost-bind-to-operator
240template<class T>
242 typedef T result_type;
243 static const int Dim = traits<T>::dimension;
244 T operator()(const T& x, const T& y, OptionalJacobian<Dim, Dim> H1 =
245 boost::none, OptionalJacobian<Dim, Dim> H2 = boost::none) const {
246 return x.compose(y, H1, H2);
247 }
248};
249
250template <>
251struct apply_compose<double> {
252 double operator()(const double& x, const double& y,
253 OptionalJacobian<1, 1> H1 = boost::none,
254 OptionalJacobian<1, 1> H2 = boost::none) const {
255 if (H1) H1->setConstant(y);
256 if (H2) H2->setConstant(x);
257 return x * y;
258 }
259};
260
261}
262
263// Global methods:
264
266template<typename T>
268 const Expression<T>& expression2) {
269 return Expression<T>(
270 std::bind(internal::apply_compose<T>(), std::placeholders::_1,
271 std::placeholders::_2, std::placeholders::_3,
272 std::placeholders::_4),
273 expression1, expression2);
274}
275
277template<typename T>
278std::vector<Expression<T> > createUnknowns(size_t n, char c, size_t start) {
279 std::vector<Expression<T> > unknowns;
280 unknowns.reserve(n);
281 for (size_t i = start; i < start + n; i++)
282 unknowns.push_back(Expression<T>(c, i));
283 return unknowns;
284}
285
286template <typename T>
287ScalarMultiplyExpression<T>::ScalarMultiplyExpression(double s, const Expression<T>& e)
288 : Expression<T>(boost::make_shared<internal::ScalarMultiplyNode<T>>(s, e)) {}
289
290
291template <typename T>
292BinarySumExpression<T>::BinarySumExpression(const Expression<T>& e1, const Expression<T>& e2)
293 : Expression<T>(boost::make_shared<internal::BinarySumNode<T>>(e1, e2)) {}
294
295template <typename T>
297 root_ = boost::make_shared<internal::BinarySumNode<T>>(*this, e);
298 return *this;
299}
300
301} // namespace gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:75
Key symbol(unsigned char c, std::uint64_t j)
Create a symbol key from a character and index, i.e.
Definition: Symbol.h:135
std::vector< Expression< T > > createUnknowns(size_t n, char c, size_t start)
Construct an array of leaves.
Definition: Expression-inl.h:278
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:47
gtsam::enable_if_t< needs_eigen_aligned_allocator< T >::value, boost::shared_ptr< T > > make_shared(Args &&... args)
Add our own make_shared as a layer of wrapping on boost::make_shared This solves the problem with the...
Definition: make_shared.h:57
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:69
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition: OptionalJacobian.h:39
Definition: VerticalBlockMatrix.h:42
Character and index key used to refer to variables.
Definition: Symbol.h:35
Definition: Expression-inl.h:241
Definition: Expression.h:40
Expression class that supports automatic differentiation.
Definition: Expression.h:48
const boost::shared_ptr< internal::ExpressionNode< T > > & root() const
Return root.
Definition: Expression-inl.h:155
Expression()
Default constructor, for serialization.
Definition: Expression.h:182
std::set< Key > keys() const
Return keys that play in this expression.
Definition: Expression-inl.h:125
std::pair< KeyVector, FastVector< int > > KeysAndDims
Keys and dimensions in same order.
Definition: Expression.h:185
void dims(std::map< Key, int > &map) const
Return dimensions for each argument, as a map.
Definition: Expression-inl.h:130
void print(const std::string &s) const
Print.
Definition: Expression-inl.h:135
T valueAndJacobianMap(const Values &values, internal::JacobianMap &jacobians) const
brief Return value and derivatives, reverse AD version
Definition: Expression-inl.h:198
T valueAndDerivatives(const Values &values, const KeyVector &keys, const FastVector< int > &dims, std::vector< Matrix > &H) const
private version that takes keys and dimensions, returns derivatives
Definition: Expression-inl.h:167
T value(const Values &values, boost::optional< std::vector< Matrix > & > H=boost::none) const
Return value and optional derivatives, reverse AD version Notes: this is not terribly efficient,...
Definition: Expression-inl.h:140
size_t traceSize() const
Return size needed for memory buffer in traceExecution.
Definition: Expression-inl.h:160
T traceExecution(const Values &values, internal::ExecutionTrace< T > &trace, void *traceStorage) const
trace execution, very unsafe
Definition: Expression-inl.h:191
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63