22#include <gtsam/nonlinear/internal/ExpressionNode.h>
24#include <boost/bind/bind.hpp>
25#include <boost/tuple/tuple.hpp>
26#include <boost/range/adaptor/map.hpp>
27#include <boost/range/algorithm.hpp>
33 root_(new internal::ConstantExpression<T>(value)) {
38 root_(new internal::LeafExpression<T>(key)) {
43 root_(new internal::LeafExpression<T>(
symbol)) {
48 root_(new internal::LeafExpression<T>(
Symbol(c, j))) {
56 root_(new internal::UnaryExpression<T, A>(function, expression)) {
61template<
typename A1,
typename A2>
65 new internal::BinaryExpression<T, A1, A2>(function, expression1,
71template<
typename A1,
typename A2,
typename A3>
76 new internal::TernaryExpression<T, A1, A2, A3>(function, expression1,
77 expression2, expression3)) {
86 new internal::UnaryExpression<T, A>(std::bind(method,
87 std::placeholders::_1, std::placeholders::_2),
93template<
typename A1,
typename A2>
99 new internal::BinaryExpression<T, A1, A2>(
100 std::bind(method, std::placeholders::_1,
101 std::placeholders::_2, std::placeholders::_3,
102 std::placeholders::_4),
103 expression1, expression2)) {
108template<
typename A1,
typename A2,
typename A3>
110 T (A1::*method)(
const A2&,
const A3&,
116 new internal::TernaryExpression<T, A1, A2, A3>(
117 std::bind(method, std::placeholders::_1,
118 std::placeholders::_2, std::placeholders::_3,
119 std::placeholders::_4, std::placeholders::_5,
120 std::placeholders::_6),
121 expression1, expression2, expression3)) {
126 return root_->keys();
141 boost::optional<std::vector<Matrix>&> H)
const {
146 FastVector<int> dims;
147 boost::tie(keys, dims) = keysAndDims();
148 return valueAndDerivatives(values, keys, dims, *H);
151 return root_->value(values);
161 return root_->traceSize();
168 const KeyVector& keys,
const FastVector<int>& dims,
169 std::vector<Matrix>& H)
const {
172 assert(H.size()==keys.size());
177 Ab.matrix().setZero();
178 internal::JacobianMap jacobianMap(keys, Ab);
181 T result = valueAndJacobianMap(values, jacobianMap);
184 for (
DenseIndex i = 0; i < static_cast<DenseIndex>(keys.size()); i++)
193 return root_->traceExecution(values, trace,
194 static_cast<internal::ExecutionTraceStorage*
>(traceStorage));
199 internal::JacobianMap& jacobians)
const {
205 size_t size = traceSize();
211 auto traceStorage =
static_cast<internal::ExecutionTraceStorage*
>(_aligned_malloc(size, internal::TraceAlignment));
213 internal::ExecutionTraceStorage traceStorage[size];
217 T value(this->traceExecution(values, trace, traceStorage));
218 trace.startReverseAD1(jacobians);
221 _aligned_free(traceStorage);
229 std::map<Key, int> map;
231 size_t n = map.size();
232 KeysAndDims pair = std::make_pair(
KeyVector(n), FastVector<int>(n));
233 boost::copy(map | boost::adaptors::map_keys, pair.first.begin());
234 boost::copy(map | boost::adaptors::map_values, pair.second.begin());
242 typedef T result_type;
246 return x.compose(y, H1, H2);
252 double operator()(
const double& x,
const double& y,
255 if (H1) H1->setConstant(y);
256 if (H2) H2->setConstant(x);
271 std::placeholders::_2, std::placeholders::_3,
272 std::placeholders::_4),
273 expression1, expression2);
279 std::vector<Expression<T> > unknowns;
281 for (
size_t i = start; i < start + n; i++)
287ScalarMultiplyExpression<T>::ScalarMultiplyExpression(
double s,
const Expression<T>& e)
288 : Expression<T>(boost::
make_shared<internal::ScalarMultiplyNode<T>>(s, e)) {}
292BinarySumExpression<T>::BinarySumExpression(
const Expression<T>& e1,
const Expression<T>& e2)
293 : Expression<T>(boost::
make_shared<internal::BinarySumNode<T>>(e1, e2)) {}
297 root_ = boost::make_shared<internal::BinarySumNode<T>>(*
this, e);
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
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:75
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
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:47
gtsam::enable_if_t< needs_eigen_aligned_allocator< T >::value, boost::shared_ptr< T > > make_shared(Args &&... args)
Add our own make_shared as a layer of wrapping on boost::make_shared This solves the problem with the...
Definition: make_shared.h:57
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
Definition: VerticalBlockMatrix.h:42
Character and index key used to refer to variables.
Definition: Symbol.h:35
Definition: Expression-inl.h:241
Definition: Expression.h:40
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()
Default constructor, for serialization.
Definition: Expression.h:182
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
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
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:63