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 
196  const FastVector<int>& dims, std::vector<Matrix>& H) const;
197 
200  char* traceStorage) const;
201 
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 
gtsam::Expression::keysAndDims
KeysAndDims keysAndDims() const
Definition: Expression-inl.h:228
H
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
Definition: gnuplot_common_settings.hh:74
gtsam::internal::ExecutionTrace
Definition: Expression.h:39
gtsam::Expression::UnaryFunction::type
std::function< T(const A1 &, typename MakeOptionalJacobian< T, A1 >::type)> type
Definition: Expression.h:71
gtsam::Expression::value
T value(const Values &values, std::vector< Matrix > *H=nullptr) const
Return value and optional derivatives, reverse AD version Notes: this is not terribly efficient,...
Definition: Expression-inl.h:142
A3
static const double A3[]
Definition: expn.h:8
gtsam.examples.DogLegOptimizerExample.type
type
Definition: DogLegOptimizerExample.py:111
gtsam::ScalarMultiplyExpression::ScalarMultiplyExpression
ScalarMultiplyExpression(double s, const Expression< T > &e)
Definition: Expression-inl.h:292
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::Expression::UnaryFunction
Definition: Expression.h:69
gtsam::Expression::TernaryFunction::type
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
gtsam::BinarySumExpression::BinarySumExpression
BinarySumExpression(const Expression< T > &e1, const Expression< T > &e2)
Definition: Expression-inl.h:297
gtsam::operator-
Errors operator-(const Errors &a, const Errors &b)
Subtraction.
Definition: Errors.cpp:74
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
gtsam::FastVector
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
Definition: FastVector.h:34
Values
gtsam::BinarySumExpression::GTSAM_CONCEPT_ASSERT
GTSAM_CONCEPT_ASSERT(IsVectorSpace< T >)
gtsam::internal::ExpressionNode
Definition: Expression.h:40
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
gtsam::Expression::root_
std::shared_ptr< internal::ExpressionNode< T > > root_
Definition: Expression.h:57
gtsam::Expression::clone
virtual std::shared_ptr< Expression > clone() const
Definition: Expression.h:172
gtsam::BinarySumExpression
Definition: Expression.h:232
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
gtsam::Expression::valueAndDerivatives
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
JacobianMap.h
JacobianMap for returning derivatives from expressions.
gtsam::Expression::value
T value(const Values &values, std::vector< Matrix > &H) const
Definition: Expression.h:163
gtsam::IsVectorSpace
Vector Space concept.
Definition: VectorSpace.h:470
gtsam::operator*
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
gtsam::Expression
Definition: Expression.h:47
n
int n
Definition: BiCGSTAB_simple.cpp:1
gtsam::Expression::root
const std::shared_ptr< internal::ExpressionNode< T > > & root() const
Return root.
Definition: Expression-inl.h:155
gtsam::Expression::keys
std::set< Key > keys() const
Return keys that play in this expression.
Definition: Expression-inl.h:127
gtsam::operator+
Errors operator+(const Errors &a, const Errors &b)
Addition.
Definition: Errors.cpp:59
A
Definition: test_numpy_dtypes.cpp:298
gtsam::Expression::KeysAndDims
std::pair< KeyVector, FastVector< int > > KeysAndDims
Keys and dimensions in same order.
Definition: Expression.h:191
gtsam::ScalarMultiplyExpression
Definition: Expression.h:219
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::internal::JacobianMap
Definition: JacobianMap.h:32
A2
static const double A2[]
Definition: expn.h:7
gtsam::symbol
Key symbol(unsigned char c, std::uint64_t j)
Definition: inference/Symbol.h:139
Symbol.h
gtsam::createUnknowns
std::vector< Expression< T > > createUnknowns(size_t n, char c, size_t start)
Construct an array of leaves.
Definition: Expression-inl.h:283
OptionalJacobian.h
Special class for optional Jacobian arguments.
gtsam::Expression::Expression
Expression()
Default constructor, for serialization.
Definition: Expression.h:188
Eigen::Triplet< double >
Expression-inl.h
Internals for Expression.h, not for general consumption.
gtsam::Expression::print
void print(const std::string &s) const
Print.
Definition: Expression-inl.h:137
g
void g(const string &key, int i)
Definition: testBTree.cpp:41
gtsam::Expression::dims
void dims(std::map< Key, int > &map) const
Return dimensions for each argument, as a map.
Definition: Expression-inl.h:132
key
const gtsam::Symbol key('X', 0)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam
traits
Definition: chartTesting.h:28
gtsam::traits
Definition: Group.h:36
gtsam::OptionalJacobian
Definition: OptionalJacobian.h:38
leaf::values
leaf::MyValues values
gtsam::Values
Definition: Values.h:65
gtsam::Expression::traceSize
size_t traceSize() const
Return size needed for memory buffer in traceExecution.
Definition: Expression-inl.h:160
A1
static const double A1[]
Definition: expn.h:6
gtsam::Expression::traceExecution
T traceExecution(const Values &values, internal::ExecutionTrace< T > &trace, char *traceStorage) const
trace execution, very unsafe
Definition: Expression-inl.h:191
gtsam::Expression::~Expression
virtual ~Expression()
Destructor.
Definition: Expression.h:140
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
uint64_t
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
gtsam::Expression::valueAndJacobianMap
T valueAndJacobianMap(const Values &values, internal::JacobianMap &jacobians) const
brief Return value and derivatives, reverse AD version
Definition: Expression-inl.h:204
internal
Definition: BandTriangularSolver.h:13
gtsam::Expression::BinaryFunction
Definition: Expression.h:75
gtsam::Expression::BinaryFunction::type
std::function< T(const A1 &, const A2 &, typename MakeOptionalJacobian< T, A1 >::type, typename MakeOptionalJacobian< T, A2 >::type)> type
Definition: Expression.h:78
gtsam::ScalarMultiplyExpression::GTSAM_CONCEPT_ASSERT
GTSAM_CONCEPT_ASSERT(IsVectorSpace< T >)
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::Expression::type
Expression< T > type
Define type so we can apply it as a meta-function.
Definition: Expression.h:52
gtsam::Expression::operator+=
Expression< T > & operator+=(const Expression< T > &e)
Add another expression to this expression.
Definition: Expression-inl.h:301
VectorSpace.h
gtsam::Expression::Expression
Expression(const std::shared_ptr< internal::ExpressionNode< T > > &root)
Construct with a custom root.
Definition: Expression.h:60
test_callbacks.value
value
Definition: test_callbacks.py:158
gtsam::linearExpression
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
gtsam::ExpressionFactor
Definition: Expression.h:36
gtsam::Symbol
Definition: inference/Symbol.h:37
gtsam::Expression::TernaryFunction
Definition: Expression.h:82


gtsam
Author(s):
autogenerated on Thu Jun 13 2024 03:02:20