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 template<typename T> class ExpressionEqualityConstraint;
39 
40 namespace internal {
41 template<typename T> class ExecutionTrace;
42 template<typename T> class ExpressionNode;
43 }
44 
48 template<typename T>
49 class Expression {
50 
51 public:
52 
55 
56 protected:
57 
58  // Paul's trick shared pointer, polymorphic root of entire expression tree
59  std::shared_ptr<internal::ExpressionNode<T> > root_;
60 
62  Expression(const std::shared_ptr<internal::ExpressionNode<T> >& root) : root_(root) {}
63 
64 public:
65 
66  // Expressions wrap trees of functions that can evaluate their own derivatives.
67  // The meta-functions below are useful to specify the type of those functions.
68  // Example, a function taking a camera and a 3D point and yielding a 2D point:
69  // Expression<Point2>::BinaryFunction<PinholeCamera<Cal3_S2>,Point3>::type
70  template<class A1>
71  struct UnaryFunction {
72  typedef std::function<
74  };
75 
76  template<class A1, class A2>
77  struct BinaryFunction {
78  typedef std::function<
79  T(const A1&, const A2&, typename MakeOptionalJacobian<T, A1>::type,
81  };
82 
83  template<class A1, class A2, class A3>
84  struct TernaryFunction {
85  typedef std::function<
86  T(const A1&, const A2&, const A3&,
90  };
91 
93  Expression(const T& value);
94 
96  Expression(const Key& key);
97 
99  Expression(const Symbol& symbol);
100 
102  Expression(unsigned char c, std::uint64_t j);
103 
105  template<typename A>
106  Expression(typename UnaryFunction<A>::type function,
107  const Expression<A>& expression);
108 
110  template<typename A1, typename A2>
111  Expression(typename BinaryFunction<A1, A2>::type function,
112  const Expression<A1>& expression1, const Expression<A2>& expression2);
113 
115  template<typename A1, typename A2, typename A3>
117  const Expression<A1>& expression1, const Expression<A2>& expression2,
118  const Expression<A3>& expression3);
119 
121  template<typename A>
122  Expression(const Expression<A>& expression,
123  T (A::*method)(typename MakeOptionalJacobian<T, A>::type) const);
124 
126  template<typename A1, typename A2>
127  Expression(const Expression<A1>& expression1,
128  T (A1::*method)(const A2&, typename MakeOptionalJacobian<T, A1>::type,
129  typename MakeOptionalJacobian<T, A2>::type) const,
130  const Expression<A2>& expression2);
131 
133  template<typename A1, typename A2, typename A3>
134  Expression(const Expression<A1>& expression1,
135  T (A1::*method)(const A2&, const A3&,
138  typename MakeOptionalJacobian<T, A3>::type) const,
139  const Expression<A2>& expression2, const Expression<A3>& expression3);
140 
142  virtual ~Expression() {
143  }
144 
146  std::set<Key> keys() const;
147 
149  void dims(std::map<Key, int>& map) const;
150 
152  void print(const std::string& s) const;
153 
159  T value(const Values& values, std::vector<Matrix>* H = nullptr) const;
160 
165  T value(const Values& values, std::vector<Matrix>& H) const {
166  return value(values, &H);
167  }
168 
174  virtual std::shared_ptr<Expression> clone() const {
175  return std::make_shared<Expression>(*this);
176  }
177 
179  const std::shared_ptr<internal::ExpressionNode<T> >& root() const;
180 
182  size_t traceSize() const;
183 
186 
187 protected:
188 
191 
193  typedef std::pair<KeyVector, FastVector<int> > KeysAndDims;
194  KeysAndDims keysAndDims() const;
195 
198  const FastVector<int>& dims, std::vector<Matrix>& H) const;
199 
202  char* traceStorage) const;
203 
206  internal::JacobianMap& jacobians) const;
207 
208  // be very selective on who can access these private methods:
209  friend class ExpressionFactor<T> ;
213 
214  // and add tests
215  friend class ::ExpressionFactorShallowTest;
216 };
217 
222 template <typename T>
224  // Check that T is a vector space
226 
227  public:
228  explicit ScalarMultiplyExpression(double s, const Expression<T>& e);
229 };
230 
235 template <typename T>
236 class BinarySumExpression : public Expression<T> {
237  // Check that T is a vector space
239 
240  public:
241  explicit BinarySumExpression(const Expression<T>& e1, const Expression<T>& e2);
242 };
243 
244 
250 template <typename T, typename A>
252  const std::function<T(A)>& f, const Expression<A>& expression,
254  // Use lambda to endow f with a linear Jacobian
256  [=](const A& value, typename MakeOptionalJacobian<T, A>::type H) {
257  if (H)
258  *H << dTdA;
259  return f(value);
260  };
261  return Expression<T>(g, expression);
262 }
263 
270 template <typename T>
273 }
274 
281 template <typename T>
283  return BinarySumExpression<T>(e1, e2);
284 }
285 
287 template <typename T>
289  // TODO(frank, abe): Implement an actual negate operator instead of multiplying by -1
290  return e1 + (-1.0) * e2;
291 }
292 
298 template<typename T>
299 Expression<T> operator*(const Expression<T>& e1, const Expression<T>& e2);
300 
306 template<typename T>
307 std::vector<Expression<T> > createUnknowns(size_t n, char c, size_t start = 0);
308 
309 } // namespace gtsam
310 
312 
gtsam::Expression::keysAndDims
KeysAndDims keysAndDims() const
Definition: Expression-inl.h:229
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:41
gtsam::Expression::UnaryFunction::type
std::function< T(const A1 &, typename MakeOptionalJacobian< T, A1 >::type)> type
Definition: Expression.h:73
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:143
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:293
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:71
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:89
gtsam::BinarySumExpression::BinarySumExpression
BinarySumExpression(const Expression< T > &e1, const Expression< T > &e2)
Definition: Expression-inl.h:298
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:42
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:247
gtsam::Expression::root_
std::shared_ptr< internal::ExpressionNode< T > > root_
Definition: Expression.h:59
gtsam::Expression::clone
virtual std::shared_ptr< Expression > clone() const
Definition: Expression.h:174
gtsam::BinarySumExpression
Definition: Expression.h:236
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:168
JacobianMap.h
JacobianMap for returning derivatives from expressions.
gtsam::Expression::value
T value(const Values &values, std::vector< Matrix > &H) const
Definition: Expression.h:165
gtsam::ScalarExpressionInequalityConstraint
Definition: NonlinearInequalityConstraint.h:78
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:49
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:156
gtsam::Expression::keys
std::set< Key > keys() const
Return keys that play in this expression.
Definition: Expression-inl.h:128
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:193
gtsam::ScalarMultiplyExpression
Definition: Expression.h:223
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:284
OptionalJacobian.h
Special class for optional Jacobian arguments.
gtsam::Expression::Expression
Expression()
Default constructor, for serialization.
Definition: Expression.h:190
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:138
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:133
gtsam::operator+
HybridGaussianProductFactor operator+(const HybridGaussianProductFactor &a, const HybridGaussianProductFactor &b)
Definition: HybridGaussianProductFactor.cpp:39
key
const gtsam::Symbol key('X', 0)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
gtsam
traits
Definition: SFMdata.h:40
gtsam::ExpressionEqualityConstraint
Definition: NonlinearEqualityConstraint.h:55
gtsam::traits
Definition: Group.h:36
gtsam::OptionalJacobian
Definition: OptionalJacobian.h:38
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:161
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:192
gtsam::Expression::~Expression
virtual ~Expression()
Destructor.
Definition: Expression.h:142
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:205
internal
Definition: BandTriangularSolver.h:13
gtsam::Expression::BinaryFunction
Definition: Expression.h:77
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:80
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:54
gtsam::Expression::operator+=
Expression< T > & operator+=(const Expression< T > &e)
Add another expression to this expression.
Definition: Expression-inl.h:302
VectorSpace.h
gtsam::Expression::Expression
Expression(const std::shared_ptr< internal::ExpressionNode< T > > &root)
Construct with a custom root.
Definition: Expression.h:62
test_callbacks.value
value
Definition: test_callbacks.py:160
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:251
gtsam::ExpressionFactor
Definition: Expression.h:36
gtsam::Symbol
Definition: inference/Symbol.h:37
gtsam::Expression::TernaryFunction
Definition: Expression.h:84


gtsam
Author(s):
autogenerated on Sun Feb 16 2025 04:01:21