22#include <gtsam/nonlinear/internal/ExpressionNode.h>
36 root_(new internal::ConstantExpression<T>(
value)) {
41 root_(new internal::LeafExpression<T>(key)) {
46 root_(new internal::LeafExpression<T>(
symbol)) {
51 root_(new internal::LeafExpression<T>(
Symbol(c, j))) {
59 root_(new internal::UnaryExpression<T, A>(function, expression)) {
64template<
typename A1,
typename A2>
68 new internal::BinaryExpression<T, A1, A2>(function, expression1,
74template<
typename A1,
typename A2,
typename A3>
79 new internal::TernaryExpression<T, A1, A2, A3>(function, expression1,
80 expression2, expression3)) {
87 T (A::*method)(
typename MakeOptionalJacobian<T, A>::type)
const) :
89 new internal::UnaryExpression<T, A>(
std::bind(method,
90 std::placeholders::_1,
std::placeholders::_2),
96template<
typename A1,
typename A2>
98 T (A1::*method)(
const A2&,
typename MakeOptionalJacobian<T, A1>::type,
99 typename MakeOptionalJacobian<T, A2>::type)
const,
102 new internal::BinaryExpression<T, A1, A2>(
103 std::bind(method,
std::placeholders::_1,
104 std::placeholders::_2,
std::placeholders::_3,
105 std::placeholders::_4),
106 expression1, expression2)) {
111template<
typename A1,
typename A2,
typename A3>
113 T (A1::*method)(
const A2&,
const A3&,
114 typename MakeOptionalJacobian<T, A1>::type,
115 typename MakeOptionalJacobian<T, A2>::type,
116 typename MakeOptionalJacobian<T, A3>::type)
const,
119 new internal::TernaryExpression<T, A1, A2, A3>(
120 std::bind(method,
std::placeholders::_1,
121 std::placeholders::_2,
std::placeholders::_3,
122 std::placeholders::_4,
std::placeholders::_5,
123 std::placeholders::_6),
124 expression1, expression2, expression3)) {
129 return root_->keys();
144 std::vector<Matrix>* H)
const {
147 const auto [
keys,
dims] = keysAndDims();
151 return root_->value(values);
162 return root_->traceSize();
170 std::vector<Matrix>& H)
const {
173 assert(H.size()==
keys.size());
179 internal::JacobianMap jacobianMap(
keys, Ab);
185 for (
DenseIndex i = 0; i < static_cast<DenseIndex>(
keys.size()); i++)
194 return root_->traceExecution(values, trace, traceStorage);
198inline std::unique_ptr<internal::ExecutionTraceStorage[]> allocAligned(
size_t size) {
199 const size_t alignedSize = (size + internal::TraceAlignment - 1) / internal::TraceAlignment;
200 return std::unique_ptr<internal::ExecutionTraceStorage[]>(
201 new internal::ExecutionTraceStorage[alignedSize]);
206 internal::JacobianMap& jacobians)
const {
210 auto traceStorage = allocAligned(size);
216 T
value(this->
traceExecution(values, trace,
reinterpret_cast<char *
>(traceStorage.get())));
219 trace.startReverseAD1(jacobians);
221 }
catch (
const std::bad_alloc &e) {
222 std::cerr <<
"valueAndJacobianMap exception: " << e.what() <<
'\n';
230 std::map<Key, int> map;
232 size_t n = map.size();
235 auto key_it =
pair.first.begin();
236 auto dim_it =
pair.second.begin();
237 for (
const auto& [key, value] :
map) {
248 typedef T result_type;
252 return x.compose(y, H1, H2);
258 double operator()(
const double& x,
const double& y,
261 if (H1) H1->setConstant(y);
262 if (H2) H2->setConstant(x);
277 std::placeholders::_2, std::placeholders::_3,
278 std::placeholders::_4),
279 expression1, expression2);
285 std::vector<Expression<T> > unknowns;
287 for (
size_t i = start; i < start + n; i++)
293ScalarMultiplyExpression<T>::ScalarMultiplyExpression(
double s,
const Expression<T>& e)
294 : Expression<T>(
std::make_shared<internal::ScalarMultiplyNode<T>>(s, e)) {}
298BinarySumExpression<T>::BinarySumExpression(
const Expression<T>& e1,
const Expression<T>& e2)
299 : Expression<T>(
std::make_shared<internal::BinarySumNode<T>>(e1, e2)) {}
303 root_ = std::make_shared<internal::BinarySumNode<T>>(*
this, e);
std::vector< T, typename internal::FastDefaultVectorAllocator< T >::type > FastVector
FastVector is a type alias to a std::vector with a custom memory allocator.
Definition FastVector.h:33
Global functions in a separate testing namespace.
Definition chartTesting.h:28
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition types.h:49
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
Key symbol(unsigned char c, std::uint64_t j)
Create a symbol key from a character and index, i.e.
Definition Symbol.h:139
std::vector< Expression< T > > createUnknowns(size_t n, char c, size_t start)
Construct an array of leaves.
Definition Expression-inl.h:284
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition Point2.h:52
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
This class stores a dense matrix and allows it to be accessed as a collection of vertical blocks.
Definition VerticalBlockMatrix.h:47
const Matrix & matrix() const
Access to full matrix (including any portions excluded by rowStart(), rowEnd(), and firstBlock()).
Definition VerticalBlockMatrix.h:278
Character and index key used to refer to variables.
Definition Symbol.h:37
Definition Expression-inl.h:247
Definition Expression.h:41
Expression class that supports automatic differentiation.
Definition Expression.h:49
Expression(const std::shared_ptr< internal::ExpressionNode< T > > &root)
Construct with a custom root.
Definition Expression.h:62
KeySet keys() const
Return keys that play in this expression.
Definition Expression-inl.h:128
Expression()
Default constructor, for serialization.
Definition Expression.h:190
const std::shared_ptr< internal::ExpressionNode< T > > & root() const
Return root.
Definition Expression-inl.h:156
std::pair< KeyVector, FastVector< int > > KeysAndDims
Keys and dimensions in same order.
Definition Expression.h:193
T traceExecution(const Values &values, internal::ExecutionTrace< T > &trace, char *traceStorage) const
trace execution, very unsafe
Definition Expression-inl.h:192
void dims(std::map< Key, int > &map) const
Return dimensions for each argument, as a map.
Definition Expression-inl.h:133
void print(const std::string &s) const
Print.
Definition Expression-inl.h:138
T valueAndJacobianMap(const Values &values, internal::JacobianMap &jacobians) const
brief Return value and derivatives, reverse AD version
Definition Expression-inl.h:205
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
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
Expression< T > & operator+=(const Expression< T > &e)
Add another expression to this expression.
Definition Expression-inl.h:302
size_t traceSize() const
Return size needed for memory buffer in traceExecution.
Definition Expression-inl.h:161
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65