gtsam/gtsam_unstable/nonlinear/Expression-inl.h

572 lines
16 KiB
C
Raw Normal View History

2014-09-30 18:20:02 +08:00
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
/**
* @file Expression-inl.h
* @date September 18, 2014
* @author Frank Dellaert
* @author Paul Furgale
* @brief Internals for Expression.h, not for general consumption
*/
#pragma once
#include <gtsam/nonlinear/Values.h>
2014-10-01 17:25:49 +08:00
#include <gtsam/base/Matrix.h>
2014-09-30 18:20:02 +08:00
#include <boost/foreach.hpp>
namespace gtsam {
template<typename T>
class Expression;
typedef std::map<Key, Matrix> JacobianMap;
//-----------------------------------------------------------------------------
/**
* Value and Jacobians
*/
template<class T>
class Augmented {
private:
T value_;
JacobianMap jacobians_;
typedef std::pair<Key, Matrix> Pair;
/// Insert terms into jacobians_, premultiplying by H, adding if already exists
void add(const JacobianMap& terms) {
2014-10-06 03:53:40 +08:00
BOOST_FOREACH(const Pair& term, terms) {
JacobianMap::iterator it = jacobians_.find(term.first);
if (it != jacobians_.end())
it->second += term.second;
else
jacobians_[term.first] = term.second;
}
}
/// Insert terms into jacobians_, premultiplying by H, adding if already exists
void add(const Matrix& H, const JacobianMap& terms) {
BOOST_FOREACH(const Pair& term, terms) {
JacobianMap::iterator it = jacobians_.find(term.first);
if (it != jacobians_.end())
it->second += H * term.second;
else
jacobians_[term.first] = H * term.second;
}
}
public:
/// Construct value that does not depend on anything
Augmented(const T& t) :
value_(t) {
}
/// Construct value dependent on a single key
Augmented(const T& t, Key key) :
value_(t) {
size_t n = t.dim();
jacobians_[key] = Eigen::MatrixXd::Identity(n, n);
}
2014-10-05 19:37:51 +08:00
/// Construct value dependent on a single key, with Jacobain H
Augmented(const T& t, Key key, const Matrix& H) :
value_(t) {
jacobians_[key] = H;
}
/// Construct value, pre-multiply jacobians by H
Augmented(const T& t, const Matrix& H, const JacobianMap& jacobians) :
value_(t) {
add(H, jacobians);
}
/// Construct value, pre-multiply jacobians by H
Augmented(const T& t, const Matrix& H1, const JacobianMap& jacobians1,
const Matrix& H2, const JacobianMap& jacobians2) :
value_(t) {
add(H1, jacobians1);
add(H2, jacobians2);
}
2014-10-06 04:09:24 +08:00
/// Construct value, pre-multiply jacobians by H
Augmented(const T& t, const Matrix& H1, const JacobianMap& jacobians1,
const Matrix& H2, const JacobianMap& jacobians2,
const Matrix& H3, const JacobianMap& jacobians3) :
value_(t) {
add(H2, jacobians2);
add(H3, jacobians3);
add(H1, jacobians1);
}
/// Return value
const T& value() const {
return value_;
}
/// Return jacobians
const JacobianMap& jacobians() const {
return jacobians_;
}
/// Return jacobians
JacobianMap& jacobians() {
return jacobians_;
}
/// Not dependent on any key
bool constant() const {
return jacobians_.empty();
}
/// debugging
void print(const KeyFormatter& keyFormatter = DefaultKeyFormatter) {
BOOST_FOREACH(const Pair& term, jacobians_)
std::cout << "(" << keyFormatter(term.first) << ", " << term.second.rows()
<< "x" << term.second.cols() << ") ";
std::cout << std::endl;
}
};
//-----------------------------------------------------------------------------
template<class T>
struct JacobianTrace {
T t;
T value() const {
return t;
}
virtual void reverseAD(JacobianMap& jacobians) const = 0;
2014-10-05 23:20:55 +08:00
virtual void reverseAD(const Matrix& H, JacobianMap& jacobians) const = 0;
};
//-----------------------------------------------------------------------------
2014-09-30 18:20:02 +08:00
/**
* Expression node. The superclass for objects that do the heavy lifting
* An Expression<T> has a pointer to an ExpressionNode<T> underneath
* allowing Expressions to have polymorphic behaviour even though they
* are passed by value. This is the same way boost::function works.
* http://loki-lib.sourceforge.net/html/a00652.html
*/
template<class T>
class ExpressionNode {
2014-09-30 18:20:02 +08:00
protected:
ExpressionNode() {
}
2014-09-30 18:20:02 +08:00
public:
/// Destructor
2014-09-30 18:20:02 +08:00
virtual ~ExpressionNode() {
}
/// Return keys that play in this expression as a set
virtual std::set<Key> keys() const = 0;
/// Return value
virtual T value(const Values& values) const = 0;
/// Return value and derivatives
2014-10-04 03:13:34 +08:00
virtual Augmented<T> forward(const Values& values) const = 0;
2014-10-05 17:22:14 +08:00
/// Construct an execution trace for reverse AD
2014-10-05 23:20:55 +08:00
virtual boost::shared_ptr<JacobianTrace<T> > traceExecution(
const Values& values) const = 0;
2014-09-30 18:20:02 +08:00
};
//-----------------------------------------------------------------------------
2014-09-30 18:20:02 +08:00
/// Constant Expression
template<class T>
class ConstantExpression: public ExpressionNode<T> {
/// The constant value
T constant_;
2014-09-30 18:20:02 +08:00
/// Constructor with a value, yielding a constant
ConstantExpression(const T& value) :
constant_(value) {
2014-09-30 18:20:02 +08:00
}
friend class Expression<T> ;
public:
/// Destructor
2014-09-30 18:20:02 +08:00
virtual ~ConstantExpression() {
}
/// Return keys that play in this expression, i.e., the empty set
virtual std::set<Key> keys() const {
std::set<Key> keys;
return keys;
}
/// Return value
virtual T value(const Values& values) const {
return constant_;
2014-09-30 18:20:02 +08:00
}
/// Return value and derivatives
2014-10-04 03:13:34 +08:00
virtual Augmented<T> forward(const Values& values) const {
2014-10-05 17:22:14 +08:00
return Augmented<T>(constant_);
}
/// Trace structure for reverse AD
struct Trace: public JacobianTrace<T> {
/// If the expression is just a constant, we do nothing
virtual void reverseAD(JacobianMap& jacobians) const {
}
/// Base case: we simply ignore the given df/dT
2014-10-05 23:20:55 +08:00
virtual void reverseAD(const Matrix& H, JacobianMap& jacobians) const {
}
};
2014-10-05 17:22:14 +08:00
/// Construct an execution trace for reverse AD
2014-10-05 23:20:55 +08:00
virtual boost::shared_ptr<JacobianTrace<T> > traceExecution(
const Values& values) const {
boost::shared_ptr<Trace> trace = boost::make_shared<Trace>();
trace->t = constant_;
return trace;
2014-10-05 17:22:14 +08:00
}
2014-09-30 18:20:02 +08:00
};
//-----------------------------------------------------------------------------
/// Leaf Expression
template<class T>
class LeafExpression: public ExpressionNode<T> {
/// The key into values
2014-09-30 18:20:02 +08:00
Key key_;
/// Constructor with a single key
LeafExpression(Key key) :
key_(key) {
}
friend class Expression<T> ;
public:
/// Destructor
2014-09-30 18:20:02 +08:00
virtual ~LeafExpression() {
}
/// Return keys that play in this expression
virtual std::set<Key> keys() const {
std::set<Key> keys;
keys.insert(key_);
return keys;
}
/// Return value
virtual T value(const Values& values) const {
return values.at<T>(key_);
}
/// Return value and derivatives
2014-10-04 03:13:34 +08:00
virtual Augmented<T> forward(const Values& values) const {
T t = value(values);
return Augmented<T>(t, key_);
2014-09-30 18:20:02 +08:00
}
2014-10-05 19:37:51 +08:00
/// Trace structure for reverse AD
struct Trace: public JacobianTrace<T> {
2014-10-05 19:37:51 +08:00
Key key;
/// If the expression is just a leaf, we just insert an identity matrix
virtual void reverseAD(JacobianMap& jacobians) const {
size_t n = T::Dim();
jacobians[key] = Eigen::MatrixXd::Identity(n, n);
}
/// Base case: given df/dT, add it jacobians with correct key and we are done
2014-10-05 23:20:55 +08:00
virtual void reverseAD(const Matrix& H, JacobianMap& jacobians) const {
JacobianMap::iterator it = jacobians.find(key);
if (it != jacobians.end())
it->second += H;
else
jacobians[key] = H;
2014-10-05 19:37:51 +08:00
}
};
/// Construct an execution trace for reverse AD
2014-10-05 23:20:55 +08:00
virtual boost::shared_ptr<JacobianTrace<T> > traceExecution(
const Values& values) const {
2014-10-05 19:37:51 +08:00
boost::shared_ptr<Trace> trace = boost::make_shared<Trace>();
trace->t = value(values);
trace->key = key_;
return trace;
}
2014-09-30 18:20:02 +08:00
};
//-----------------------------------------------------------------------------
/// Unary Function Expression
template<class T, class A>
class UnaryExpression: public ExpressionNode<T> {
public:
2014-10-03 18:52:35 +08:00
typedef boost::function<T(const A&, boost::optional<Matrix&>)> Function;
private:
2014-10-03 18:52:35 +08:00
Function function_;
boost::shared_ptr<ExpressionNode<A> > expressionA_;
/// Constructor with a unary function f, and input argument e
UnaryExpression(Function f, const Expression<A>& e) :
function_(f), expressionA_(e.root()) {
}
friend class Expression<T> ;
public:
/// Destructor
virtual ~UnaryExpression() {
}
/// Return keys that play in this expression
virtual std::set<Key> keys() const {
return expressionA_->keys();
}
/// Return value
virtual T value(const Values& values) const {
return function_(this->expressionA_->value(values), boost::none);
}
/// Return value and derivatives
2014-10-04 03:13:34 +08:00
virtual Augmented<T> forward(const Values& values) const {
using boost::none;
2014-10-04 03:13:34 +08:00
Augmented<A> argument = this->expressionA_->forward(values);
Matrix H;
T t = function_(argument.value(),
argument.constant() ? none : boost::optional<Matrix&>(H));
return Augmented<T>(t, H, argument.jacobians());
2014-09-30 18:20:02 +08:00
}
2014-10-05 19:33:23 +08:00
/// Trace structure for reverse AD
struct Trace: public JacobianTrace<T> {
boost::shared_ptr<JacobianTrace<A> > trace1;
2014-10-05 19:33:23 +08:00
Matrix H1;
/// Start the reverse AD process
virtual void reverseAD(JacobianMap& jacobians) const {
trace1->reverseAD(H1, jacobians);
}
/// Given df/dT, multiply in dT/dA and continue reverse AD process
2014-10-05 23:20:55 +08:00
virtual void reverseAD(const Matrix& H, JacobianMap& jacobians) const {
trace1->reverseAD(H * H1, jacobians);
2014-10-05 19:33:23 +08:00
}
};
/// Construct an execution trace for reverse AD
2014-10-05 23:20:55 +08:00
virtual boost::shared_ptr<JacobianTrace<T> > traceExecution(
const Values& values) const {
2014-10-05 19:33:23 +08:00
boost::shared_ptr<Trace> trace = boost::make_shared<Trace>();
2014-10-05 23:20:55 +08:00
trace->trace1 = this->expressionA_->traceExecution(values);
2014-10-05 19:33:23 +08:00
trace->t = function_(trace->trace1->value(), trace->H1);
return trace;
}
2014-09-30 18:20:02 +08:00
};
//-----------------------------------------------------------------------------
2014-10-03 18:48:28 +08:00
/// Binary Expression
2014-09-30 18:20:02 +08:00
template<class T, class A1, class A2>
2014-09-30 18:20:02 +08:00
class BinaryExpression: public ExpressionNode<T> {
public:
2014-10-03 18:48:28 +08:00
typedef boost::function<
T(const A1&, const A2&, boost::optional<Matrix&>,
2014-10-03 18:52:35 +08:00
boost::optional<Matrix&>)> Function;
private:
2014-10-03 18:52:35 +08:00
Function function_;
boost::shared_ptr<ExpressionNode<A1> > expressionA1_;
boost::shared_ptr<ExpressionNode<A2> > expressionA2_;
/// Constructor with a binary function f, and two input arguments
BinaryExpression(Function f, //
2014-10-03 18:48:28 +08:00
const Expression<A1>& e1, const Expression<A2>& e2) :
function_(f), expressionA1_(e1.root()), expressionA2_(e2.root()) {
}
friend class Expression<T> ;
public:
/// Destructor
virtual ~BinaryExpression() {
}
/// Return keys that play in this expression
virtual std::set<Key> keys() const {
std::set<Key> keys1 = expressionA1_->keys();
std::set<Key> keys2 = expressionA2_->keys();
keys1.insert(keys2.begin(), keys2.end());
return keys1;
}
/// Return value
virtual T value(const Values& values) const {
using boost::none;
2014-10-03 18:48:28 +08:00
return function_(this->expressionA1_->value(values),
this->expressionA2_->value(values), none, none);
}
/// Return value and derivatives
2014-10-04 03:13:34 +08:00
virtual Augmented<T> forward(const Values& values) const {
using boost::none;
2014-10-04 03:13:34 +08:00
Augmented<A1> argument1 = this->expressionA1_->forward(values);
Augmented<A2> argument2 = this->expressionA2_->forward(values);
Matrix H1, H2;
2014-10-03 18:48:28 +08:00
T t = function_(argument1.value(), argument2.value(),
argument1.constant() ? none : boost::optional<Matrix&>(H1),
argument2.constant() ? none : boost::optional<Matrix&>(H2));
return Augmented<T>(t, H1, argument1.jacobians(), H2, argument2.jacobians());
}
2014-10-05 19:27:41 +08:00
/// Trace structure for reverse AD
struct Trace: public JacobianTrace<T> {
boost::shared_ptr<JacobianTrace<A1> > trace1;
boost::shared_ptr<JacobianTrace<A2> > trace2;
2014-10-05 19:27:41 +08:00
Matrix H1, H2;
/// Start the reverse AD process
virtual void reverseAD(JacobianMap& jacobians) const {
trace1->reverseAD(H1, jacobians);
trace2->reverseAD(H2, jacobians);
}
/// Given df/dT, multiply in dT/dA and continue reverse AD process
2014-10-05 23:20:55 +08:00
virtual void reverseAD(const Matrix& H, JacobianMap& jacobians) const {
trace1->reverseAD(H * H1, jacobians);
trace2->reverseAD(H * H2, jacobians);
2014-10-05 19:27:41 +08:00
}
};
/// Construct an execution trace for reverse AD
2014-10-05 23:20:55 +08:00
virtual boost::shared_ptr<JacobianTrace<T> > traceExecution(
const Values& values) const {
2014-10-05 19:27:41 +08:00
boost::shared_ptr<Trace> trace = boost::make_shared<Trace>();
2014-10-05 23:20:55 +08:00
trace->trace1 = this->expressionA1_->traceExecution(values);
trace->trace2 = this->expressionA2_->traceExecution(values);
2014-10-05 19:27:41 +08:00
trace->t = function_(trace->trace1->value(), trace->trace2->value(),
trace->H1, trace->H2);
return trace;
}
};
2014-10-06 01:09:16 +08:00
2014-10-03 18:48:28 +08:00
//-----------------------------------------------------------------------------
2014-10-06 04:09:24 +08:00
/// Ternary Expression
template<class T, class A1, class A2, class A3>
class TernaryExpression: public ExpressionNode<T> {
public:
typedef boost::function<
T(const A1&, const A2&, const A3&, boost::optional<Matrix&>,
boost::optional<Matrix&>, boost::optional<Matrix&>)> Function;
private:
Function function_;
boost::shared_ptr<ExpressionNode<A1> > expressionA1_;
boost::shared_ptr<ExpressionNode<A2> > expressionA2_;
boost::shared_ptr<ExpressionNode<A3> > expressionA3_;
/// Constructor with a ternary function f, and three input arguments
TernaryExpression(Function f, //
const Expression<A1>& e1, const Expression<A2>& e2, const Expression<A3>& e3) :
function_(f), expressionA1_(e1.root()), expressionA2_(e2.root()), expressionA3_(e3.root()) {
}
friend class Expression<T> ;
public:
/// Destructor
virtual ~TernaryExpression() {
}
/// Return keys that play in this expression
virtual std::set<Key> keys() const {
std::set<Key> keys1 = expressionA1_->keys();
std::set<Key> keys2 = expressionA2_->keys();
std::set<Key> keys3 = expressionA3_->keys();
keys2.insert(keys3.begin(), keys3.end());
keys1.insert(keys2.begin(), keys2.end());
return keys1;
}
/// Return value
virtual T value(const Values& values) const {
using boost::none;
return function_(this->expressionA1_->value(values),
this->expressionA2_->value(values), this->expressionA3_->value(values), none, none, none);
}
2014-10-06 04:09:24 +08:00
/// Return value and derivatives
2014-10-06 05:36:53 +08:00
virtual Augmented<T> forward(const Values& values) const {
2014-10-06 04:09:24 +08:00
using boost::none;
2014-10-06 05:36:53 +08:00
Augmented<A1> argument1 = this->expressionA1_->forward(values);
Augmented<A2> argument2 = this->expressionA2_->forward(values);
Augmented<A3> argument3 = this->expressionA3_->forward(values);
2014-10-06 04:09:24 +08:00
Matrix H1, H2, H3;
T t = function_(argument1.value(), argument2.value(), argument3.value(),
argument1.constant() ? none : boost::optional<Matrix&>(H1),
argument2.constant() ? none : boost::optional<Matrix&>(H2),
argument3.constant() ? none : boost::optional<Matrix&>(H3));
2014-10-06 05:36:53 +08:00
return Augmented<T>(t, H1, argument1.jacobians(), H2, argument2.jacobians(), H3, argument3.jacobians());
2014-10-06 04:09:24 +08:00
}
/// Trace structure for reverse AD
struct Trace: public JacobianTrace<T> {
boost::shared_ptr<JacobianTrace<A1> > trace1;
boost::shared_ptr<JacobianTrace<A2> > trace2;
boost::shared_ptr<JacobianTrace<A3> > trace3;
Matrix H1, H2, H3;
/// Start the reverse AD process
virtual void reverseAD(JacobianMap& jacobians) const {
trace1->reverseAD(H1, jacobians);
trace2->reverseAD(H2, jacobians);
trace3->reverseAD(H3, jacobians);
}
/// Given df/dT, multiply in dT/dA and continue reverse AD process
virtual void reverseAD(const Matrix& H, JacobianMap& jacobians) const {
trace1->reverseAD(H * H1, jacobians);
trace2->reverseAD(H * H2, jacobians);
trace3->reverseAD(H * H3, jacobians);
}
};
/// Construct an execution trace for reverse AD
virtual boost::shared_ptr<JacobianTrace<T> > traceExecution(
const Values& values) const {
boost::shared_ptr<Trace> trace = boost::make_shared<Trace>();
trace->trace1 = this->expressionA1_->traceExecution(values);
trace->trace2 = this->expressionA2_->traceExecution(values);
trace->trace3 = this->expressionA3_->traceExecution(values);
trace->t = function_(trace->trace1->value(), trace->trace2->value(), trace->trace3->value(),
trace->H1, trace->H2, trace->H3);
return trace;
}
2014-10-06 04:09:24 +08:00
};
//-----------------------------------------------------------------------------
2014-09-30 18:20:02 +08:00
}