gtsam 4.1.1
gtsam
Expression.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/JacobianMap.h>
25#include <gtsam/base/VectorSpace.h>
26
27#include <boost/make_shared.hpp>
28#include <map>
29
30// Forward declare tests
31class ExpressionFactorShallowTest;
32
33namespace gtsam {
34
35// Forward declares
36class Values;
37template<typename T> class ExpressionFactor;
38
39namespace internal {
40template<typename T> class ExecutionTrace;
41template<typename T> class ExpressionNode;
42}
43
47template<typename T>
49
50public:
51
54
55protected:
56
57 // Paul's trick shared pointer, polymorphic root of entire expression tree
58 boost::shared_ptr<internal::ExpressionNode<T> > root_;
59
61 Expression(const boost::shared_ptr<internal::ExpressionNode<T> >& root) : root_(root) {}
62
63public:
64
65 // Expressions wrap trees of functions that can evaluate their own derivatives.
66 // The meta-functions below are useful to specify the type of those functions.
67 // Example, a function taking a camera and a 3D point and yielding a 2D point:
68 // Expression<Point2>::BinaryFunction<PinholeCamera<Cal3_S2>,Point3>::type
69 template<class A1>
71 typedef std::function<
72 T(const A1&, typename MakeOptionalJacobian<T, A1>::type)> type;
73 };
74
75 template<class A1, class A2>
77 typedef std::function<
78 T(const A1&, const A2&, typename MakeOptionalJacobian<T, A1>::type,
80 };
81
82 template<class A1, class A2, class A3>
84 typedef std::function<
85 T(const A1&, const A2&, const A3&,
89 };
90
92 Expression(const T& value);
93
95 Expression(const Key& key);
96
98 Expression(const Symbol& symbol);
99
101 Expression(unsigned char c, std::uint64_t j);
102
104 template<typename A>
105 Expression(typename UnaryFunction<A>::type function,
106 const Expression<A>& expression);
107
109 template<typename A1, typename A2>
110 Expression(typename BinaryFunction<A1, A2>::type function,
111 const Expression<A1>& expression1, const Expression<A2>& expression2);
112
114 template<typename A1, typename A2, typename A3>
115 Expression(typename TernaryFunction<A1, A2, A3>::type function,
116 const Expression<A1>& expression1, const Expression<A2>& expression2,
117 const Expression<A3>& expression3);
118
120 template<typename A>
121 Expression(const Expression<A>& expression,
122 T (A::*method)(typename MakeOptionalJacobian<T, A>::type) const);
123
125 template<typename A1, typename A2>
126 Expression(const Expression<A1>& expression1,
127 T (A1::*method)(const A2&, typename MakeOptionalJacobian<T, A1>::type,
128 typename MakeOptionalJacobian<T, A2>::type) const,
129 const Expression<A2>& expression2);
130
132 template<typename A1, typename A2, typename A3>
133 Expression(const Expression<A1>& expression1,
134 T (A1::*method)(const A2&, const A3&,
137 typename MakeOptionalJacobian<T, A3>::type) const,
138 const Expression<A2>& expression2, const Expression<A3>& expression3);
139
141 virtual ~Expression() {
142 }
143
145 std::set<Key> keys() const;
146
148 void dims(std::map<Key, int>& map) const;
149
151 void print(const std::string& s) const;
152
158 T value(const Values& values, boost::optional<std::vector<Matrix>&> H =
159 boost::none) const;
160
166 virtual boost::shared_ptr<Expression> clone() const {
167 return boost::make_shared<Expression>(*this);
168 }
169
171 const boost::shared_ptr<internal::ExpressionNode<T> >& root() const;
172
174 size_t traceSize() const;
175
178
179protected:
180
183
185 typedef std::pair<KeyVector, FastVector<int> > KeysAndDims;
186 KeysAndDims keysAndDims() const;
187
189 T valueAndDerivatives(const Values& values, const KeyVector& keys,
190 const FastVector<int>& dims, std::vector<Matrix>& H) const;
191
193 T traceExecution(const Values& values, internal::ExecutionTrace<T>& trace,
194 void* traceStorage) const;
195
197 T valueAndJacobianMap(const Values& values,
198 internal::JacobianMap& jacobians) const;
199
200 // be very selective on who can access these private methods:
201 friend class ExpressionFactor<T> ;
202 friend class internal::ExpressionNode<T>;
203
204 // and add tests
205 friend class ::ExpressionFactorShallowTest;
206};
207
212template <typename T>
214 // Check that T is a vector space
215 BOOST_CONCEPT_ASSERT((gtsam::IsVectorSpace<T>));
216
217 public:
218 explicit ScalarMultiplyExpression(double s, const Expression<T>& e);
219};
220
225template <typename T>
227 // Check that T is a vector space
228 BOOST_CONCEPT_ASSERT((gtsam::IsVectorSpace<T>));
229
230 public:
231 explicit BinarySumExpression(const Expression<T>& e1, const Expression<T>& e2);
232};
233
234
240template <typename T, typename A>
242 const std::function<T(A)>& f, const Expression<A>& expression,
243 const Eigen::Matrix<double, traits<T>::dimension, traits<A>::dimension>& dTdA) {
244 // Use lambda to endow f with a linear Jacobian
245 typename Expression<T>::template UnaryFunction<A>::type g =
246 [=](const A& value, typename MakeOptionalJacobian<T, A>::type H) {
247 if (H)
248 *H << dTdA;
249 return f(value);
250 };
251 return Expression<T>(g, expression);
252}
253
260template <typename T>
262 return ScalarMultiplyExpression<T>(s, e);
263}
264
271template <typename T>
273 return BinarySumExpression<T>(e1, e2);
274}
275
277template <typename T>
279 // TODO(frank, abe): Implement an actual negate operator instead of multiplying by -1
280 return e1 + (-1.0) * e2;
281}
282
288template<typename T>
289Expression<T> operator*(const Expression<T>& e1, const Expression<T>& e2);
290
296template<typename T>
297std::vector<Expression<T> > createUnknowns(size_t n, char c, size_t start = 0);
298
299} // namespace gtsam
300
302
Special class for optional Jacobian arguments.
Internals for Expression.h, not for general consumption.
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
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
BinarySumExpression< T > operator+(const Expression< T > &e1, const Expression< T > &e2)
Construct an expression that sums two input expressions of the same type T The type T must be a vecto...
Definition: Expression.h:272
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:47
Expression< T > linearExpression(const std::function< T(A)> &f, const Expression< A > &expression, const Eigen::Matrix< double, traits< T >::dimension, traits< A >::dimension > &dTdA)
Create an expression out of a linear function f:T->A with (constant) Jacobian dTdA TODO(frank): creat...
Definition: Expression.h:241
BinarySumExpression< T > operator-(const Expression< T > &e1, const Expression< T > &e2)
Construct an expression that subtracts one expression from another.
Definition: Expression.h:278
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
Vector Space concept.
Definition: VectorSpace.h:470
Character and index key used to refer to variables.
Definition: Symbol.h:35
Factor that supports arbitrary expressions via AD.
Definition: ExpressionFactor.h:44
Definition: Expression.h:40
Definition: Expression.h:41
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< T > type
Define type so we can apply it as a meta-function.
Definition: Expression.h:53
Expression()
Default constructor, for serialization.
Definition: Expression.h:182
virtual ~Expression()
Destructor.
Definition: Expression.h:141
virtual boost::shared_ptr< Expression > clone() const
Definition: Expression.h:166
Expression(const boost::shared_ptr< internal::ExpressionNode< T > > &root)
Construct with a custom root.
Definition: Expression.h:61
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
Expression< T > & operator+=(const Expression< T > &e)
Add another expression to this expression.
Definition: Expression-inl.h:296
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
Definition: Expression.h:70
Definition: Expression.h:76
Definition: Expression.h:83
A ScalarMultiplyExpression is a specialization of Expression that multiplies with a scalar It optimiz...
Definition: Expression.h:213
A BinarySumExpression is a specialization of Expression that adds two expressions together It optimiz...
Definition: Expression.h:226
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63