GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
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 
23 #include <gtsam/inference/Symbol.h>
25 #include <gtsam/base/VectorSpace.h>
26 
27 #include <map>
28 
29 // Forward declare tests
30 class ExpressionFactorShallowTest;
31 
32 namespace gtsam {
33 
34 // Forward declares
35 class Values;
36 template<typename T> class ExpressionFactor;
37 
38 namespace internal {
39 template<typename T> class ExecutionTrace;
40 template<typename T> class ExpressionNode;
41 }
42 
46 template<typename T>
47 class Expression {
48 
49 public:
50 
53 
54 protected:
55 
56  // Paul's trick shared pointer, polymorphic root of entire expression tree
57  std::shared_ptr<internal::ExpressionNode<T> > root_;
58 
60  Expression(const std::shared_ptr<internal::ExpressionNode<T> >& root) : root_(root) {}
61 
62 public:
63 
64  // Expressions wrap trees of functions that can evaluate their own derivatives.
65  // The meta-functions below are useful to specify the type of those functions.
66  // Example, a function taking a camera and a 3D point and yielding a 2D point:
67  // Expression<Point2>::BinaryFunction<PinholeCamera<Cal3_S2>,Point3>::type
68  template<class A1>
69  struct UnaryFunction {
70  typedef std::function<
71  T(const A1&, typename MakeOptionalJacobian<T, A1>::type)> type;
72  };
73 
74  template<class A1, class A2>
75  struct BinaryFunction {
76  typedef std::function<
77  T(const A1&, const A2&, typename MakeOptionalJacobian<T, A1>::type,
78  typename MakeOptionalJacobian<T, A2>::type)> type;
79  };
80 
81  template<class A1, class A2, class A3>
82  struct TernaryFunction {
83  typedef std::function<
84  T(const A1&, const A2&, const A3&,
87  typename MakeOptionalJacobian<T, A3>::type)> type;
88  };
89 
91  Expression(const T& value);
92 
94  Expression(const Key& key);
95 
97  Expression(const Symbol& symbol);
98 
100  Expression(unsigned char c, std::uint64_t j);
101 
103  template<typename A>
104  Expression(typename UnaryFunction<A>::type function,
105  const Expression<A>& expression);
106 
108  template<typename A1, typename A2>
109  Expression(typename BinaryFunction<A1, A2>::type function,
110  const Expression<A1>& expression1, const Expression<A2>& expression2);
111 
113  template<typename A1, typename A2, typename A3>
114  Expression(typename TernaryFunction<A1, A2, A3>::type function,
115  const Expression<A1>& expression1, const Expression<A2>& expression2,
116  const Expression<A3>& expression3);
117 
119  template<typename A>
120  Expression(const Expression<A>& expression,
121  T (A::*method)(typename MakeOptionalJacobian<T, A>::type) const);
122 
124  template<typename A1, typename A2>
125  Expression(const Expression<A1>& expression1,
126  T (A1::*method)(const A2&, typename MakeOptionalJacobian<T, A1>::type,
127  typename MakeOptionalJacobian<T, A2>::type) const,
128  const Expression<A2>& expression2);
129 
131  template<typename A1, typename A2, typename A3>
132  Expression(const Expression<A1>& expression1,
133  T (A1::*method)(const A2&, const A3&,
136  typename MakeOptionalJacobian<T, A3>::type) const,
137  const Expression<A2>& expression2, const Expression<A3>& expression3);
138 
140  virtual ~Expression() {
141  }
142 
144  std::set<Key> keys() const;
145 
147  void dims(std::map<Key, int>& map) const;
148 
150  void print(const std::string& s) const;
151 
157  T value(const Values& values, std::vector<Matrix>* H = nullptr) const;
158 
163  T value(const Values& values, std::vector<Matrix>& H) const {
164  return value(values, &H);
165  }
166 
172  virtual std::shared_ptr<Expression> clone() const {
173  return std::make_shared<Expression>(*this);
174  }
175 
177  const std::shared_ptr<internal::ExpressionNode<T> >& root() const;
178 
180  size_t traceSize() const;
181 
183  Expression<T>& operator+=(const Expression<T>& e);
184 
185 protected:
186 
189 
191  typedef std::pair<KeyVector, FastVector<int> > KeysAndDims;
192  KeysAndDims keysAndDims() const;
193 
195  T valueAndDerivatives(const Values& values, const KeyVector& keys,
196  const FastVector<int>& dims, std::vector<Matrix>& H) const;
197 
199  T traceExecution(const Values& values, internal::ExecutionTrace<T>& trace,
200  char* traceStorage) const;
201 
203  T valueAndJacobianMap(const Values& values,
204  internal::JacobianMap& jacobians) const;
205 
206  // be very selective on who can access these private methods:
207  friend class ExpressionFactor<T> ;
208  friend class internal::ExpressionNode<T>;
209 
210  // and add tests
211  friend class ::ExpressionFactorShallowTest;
212 };
213 
218 template <typename T>
220  // Check that T is a vector space
221  GTSAM_CONCEPT_ASSERT(gtsam::IsVectorSpace<T>);
222 
223  public:
224  explicit ScalarMultiplyExpression(double s, const Expression<T>& e);
225 };
226 
231 template <typename T>
232 class BinarySumExpression : public Expression<T> {
233  // Check that T is a vector space
234  GTSAM_CONCEPT_ASSERT(gtsam::IsVectorSpace<T>);
235 
236  public:
237  explicit BinarySumExpression(const Expression<T>& e1, const Expression<T>& e2);
238 };
239 
240 
246 template <typename T, typename A>
248  const std::function<T(A)>& f, const Expression<A>& expression,
249  const Eigen::Matrix<double, traits<T>::dimension, traits<A>::dimension>& dTdA) {
250  // Use lambda to endow f with a linear Jacobian
251  typename Expression<T>::template UnaryFunction<A>::type g =
252  [=](const A& value, typename MakeOptionalJacobian<T, A>::type H) {
253  if (H)
254  *H << dTdA;
255  return f(value);
256  };
257  return Expression<T>(g, expression);
258 }
259 
266 template <typename T>
268  return ScalarMultiplyExpression<T>(s, e);
269 }
270 
277 template <typename T>
279  return BinarySumExpression<T>(e1, e2);
280 }
281 
283 template <typename T>
285  // TODO(frank, abe): Implement an actual negate operator instead of multiplying by -1
286  return e1 + (-1.0) * e2;
287 }
288 
294 template<typename T>
295 Expression<T> operator*(const Expression<T>& e1, const Expression<T>& e2);
296 
302 template<typename T>
303 std::vector<Expression<T> > createUnknowns(size_t n, char c, size_t start = 0);
304 
305 } // namespace gtsam
306 
308 
std::pair< KeyVector, FastVector< int > > KeysAndDims
Keys and dimensions in same order.
Definition: Expression.h:191
Definition: Expression.h:232
Expression< T > linearExpression(const std::function< T(A)> &f, const Expression< A > &expression, const Eigen::Matrix< double, traits< T >::dimension, traits< A >::dimension > &dTdA)
Definition: Expression.h:247
Definition: Expression.h:82
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Definition: Group.h:43
Definition: Expression.h:47
JacobianMap for returning derivatives from expressions.
std::vector< Expression< T > > createUnknowns(size_t n, char c, size_t start)
Construct an array of leaves.
Definition: Expression-inl.h:283
Vector Space concept.
Definition: VectorSpace.h:470
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
Definition: Expression.h:219
Expression< T > type
Define type so we can apply it as a meta-function.
Definition: Expression.h:52
Definition: JacobianMap.h:32
virtual ~Expression()
Destructor.
Definition: Expression.h:140
GTSAM_EXPORT void print(const Matrix &A, const std::string &s, std::ostream &stream)
Expression(const std::shared_ptr< internal::ExpressionNode< T > > &root)
Construct with a custom root.
Definition: Expression.h:60
Definition: Expression.h:40
virtual std::shared_ptr< Expression > clone() const
Definition: Expression.h:172
Key symbol(unsigned char c, std::uint64_t j)
Definition: Symbol.h:139
Definition: Values.h:65
Definition: chartTesting.h:28
Internals for Expression.h, not for general consumption.
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
Definition: OptionalJacobian.h:38
GTSAM_EXPORT Errors operator-(const Errors &a, const Errors &b)
Subtraction.
Expression()
Default constructor, for serialization.
Definition: Expression.h:188
Definition: Expression.h:69
Definition: Symbol.h:37
Special class for optional Jacobian arguments.
Definition: Expression.h:36
T value(const Values &values, std::vector< Matrix > &H) const
Definition: Expression.h:163
Definition: Expression.h:75
GTSAM_EXPORT Errors operator+(const Errors &a, const Errors &b)
Addition.
Definition: Expression.h:39
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102