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<
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,
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&,
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>
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 
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> ;
209 
210  // and add tests
211  friend class ::ExpressionFactorShallowTest;
212 };
213 
218 template <typename T>
220  // Check that T is a vector space
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
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,
250  // Use lambda to endow f with a linear Jacobian
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>
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 
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
const gtsam::Symbol key('X', 0)
std::pair< KeyVector, FastVector< int > > KeysAndDims
Keys and dimensions in same order.
Definition: Expression.h:191
static Matrix A1
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16 & operator+=(bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:184
#define GTSAM_CONCEPT_ASSERT(concept)
Definition: base/concepts.h:22
std::shared_ptr< internal::ExpressionNode< T > > root_
Definition: Expression.h:57
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
std::function< T(const A1 &, const A2 &, const A3 &, typename MakeOptionalJacobian< T, A1 >::type, typename MakeOptionalJacobian< T, A2 >::type, typename MakeOptionalJacobian< T, A3 >::type)> type
Definition: Expression.h:87
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
int n
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
leaf::MyValues values
JacobianMap for returning derivatives from expressions.
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 set mxtics default set mytics default set mx2tics default set my2tics default set xtics border mirror norotate autofreq set ytics border mirror norotate autofreq set ztics border nomirror norotate autofreq set nox2tics set noy2tics set timestamp bottom norotate set rrange [*:*] noreverse nowriteback set trange [*:*] noreverse nowriteback set urange [*:*] noreverse nowriteback set vrange [*:*] noreverse nowriteback set xlabel matrix size set x2label set timefmt d m y n H
Vector Space concept.
Definition: VectorSpace.h:470
std::function< T(const A1 &, const A2 &, typename MakeOptionalJacobian< T, A1 >::type, typename MakeOptionalJacobian< T, A2 >::type)> type
Definition: Expression.h:78
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
Expression< T > type
Define type so we can apply it as a meta-function.
Definition: Expression.h:52
void g(const string &key, int i)
Definition: testBTree.cpp:41
std::vector< Expression< T > > createUnknowns(size_t n, char c, size_t start)
Construct an array of leaves.
virtual ~Expression()
Destructor.
Definition: Expression.h:140
Expression(const std::shared_ptr< internal::ExpressionNode< T > > &root)
Construct with a custom root.
Definition: Expression.h:60
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
Eigen::Triplet< double > T
Errors operator+(const Errors &a, const Errors &b)
Addition.
Definition: Errors.cpp:59
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Errors operator-(const Errors &a, const Errors &b)
Subtraction.
Definition: Errors.cpp:74
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
virtual std::shared_ptr< Expression > clone() const
Definition: Expression.h:172
Key symbol(unsigned char c, std::uint64_t j)
traits
Definition: chartTesting.h:28
Internals for Expression.h, not for general consumption.
Expression()
Default constructor, for serialization.
Definition: Expression.h:188
Special class for optional Jacobian arguments.
const KeyVector keys
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
The matrix class, also used for vectors and row-vectors.
T value(const Values &values, std::vector< Matrix > &H) const
Definition: Expression.h:163
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::function< T(const A1 &, typename MakeOptionalJacobian< T, A1 >::type)> type
Definition: Expression.h:71
std::ptrdiff_t j


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:13