MUCH simpler by just using boost::bind to turn methods into functions

release/4.3a0
dellaert 2014-10-03 13:18:25 +02:00
parent bdf5451565
commit a5b92f0342
2 changed files with 53 additions and 185 deletions

View File

@ -221,19 +221,26 @@ public:
};
//-----------------------------------------------------------------------------
/// Unary Expression
/// Unary Function Expression
template<class T, class A>
class UnaryExpression: public ExpressionNode<T> {
protected:
public:
typedef boost::function<T(const A&, boost::optional<Matrix&>)> Function;
private:
Function function_;
boost::shared_ptr<ExpressionNode<A> > expressionA_;
/// Constructor with one input argument expression
UnaryExpression(const Expression<A>& e) :
expressionA_(e.root()) {
/// 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
@ -245,78 +252,6 @@ public:
return expressionA_->keys();
}
};
//-----------------------------------------------------------------------------
/// Nullary Method Expression
template<class T, class A>
class NullaryMethodExpression: public UnaryExpression<T, A> {
public:
typedef T (A::*Method)(boost::optional<Matrix&>) const;
private:
Method method_;
/// Constructor with a unary function f, and input argument e
NullaryMethodExpression(const Expression<A>& e, Method f) :
UnaryExpression<T, A>(e), method_(f) {
}
friend class Expression<T> ;
public:
/// Destructor
virtual ~NullaryMethodExpression() {
}
/// Return value
virtual T value(const Values& values) const {
using boost::none;
return (this->expressionA_->value(values).*(method_))(none);
}
/// Return value and derivatives
virtual Augmented<T> augmented(const Values& values) const {
using boost::none;
Augmented<A> argument = this->expressionA_->augmented(values);
Matrix H;
T t = (argument.value().*(method_))(
argument.constant() ? none : boost::optional<Matrix&>(H));
return Augmented<T>(t, H, argument.jacobians());
}
};
//-----------------------------------------------------------------------------
/// Unary Function Expression
template<class T, class A>
class UnaryFunctionExpression: public UnaryExpression<T, A> {
public:
typedef boost::function<T(const A&, boost::optional<Matrix&>)> Function;
private:
Function function_;
/// Constructor with a unary function f, and input argument e
UnaryFunctionExpression(Function f, const Expression<A>& e) :
UnaryExpression<T, A>(e), function_(f) {
}
friend class Expression<T> ;
public:
/// Destructor
virtual ~UnaryFunctionExpression() {
}
/// Return value
virtual T value(const Values& values) const {
return function_(this->expressionA_->value(values), boost::none);
@ -340,16 +275,26 @@ public:
template<class T, class A1, class A2>
class BinaryExpression: public ExpressionNode<T> {
protected:
public:
typedef boost::function<
T(const A1&, const A2&, boost::optional<Matrix&>,
boost::optional<Matrix&>)> Function;
private:
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(const Expression<A1>& e1, const Expression<A2>& e2) :
expressionA1_(e1.root()), expressionA2_(e2.root()) {
BinaryExpression(Function f, //
const Expression<A1>& e1, const Expression<A2>& e2) :
function_(f), expressionA1_(e1.root()), expressionA2_(e2.root()) {
}
friend class Expression<T> ;
public:
/// Destructor
@ -364,88 +309,6 @@ public:
return keys1;
}
};
//-----------------------------------------------------------------------------
/// Binary Expression
template<class T, class A1, class A2>
class UnaryMethodExpression: public BinaryExpression<T, A1, A2> {
public:
typedef T (A1::*Method)(const A2&, boost::optional<Matrix&>,
boost::optional<Matrix&>) const;
private:
Method method_;
/// Constructor with a binary function f, and two input arguments
UnaryMethodExpression(const Expression<A1>& e1, Method f,
const Expression<A2>& e2) :
BinaryExpression<T, A1, A2>(e1, e2), method_(f) {
}
friend class Expression<T> ;
public:
/// Destructor
virtual ~UnaryMethodExpression() {
}
/// Return value
virtual T value(const Values& values) const {
using boost::none;
return (this->expressionA1_->value(values).*(method_))(
this->expressionA2_->value(values), none, none);
}
/// Return value and derivatives
virtual Augmented<T> augmented(const Values& values) const {
using boost::none;
Augmented<A1> argument1 = this->expressionA1_->augmented(values);
Augmented<A2> argument2 = this->expressionA2_->augmented(values);
Matrix H1, H2;
T t = (argument1.value().*(method_))(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());
}
};
//-----------------------------------------------------------------------------
/// Binary function Expression
template<class T, class A1, class A2>
class BinaryFunctionExpression: public BinaryExpression<T, A1, A2> {
public:
typedef boost::function<
T(const A1&, const A2&, boost::optional<Matrix&>,
boost::optional<Matrix&>)> Function;
private:
Function function_;
/// Constructor with a binary function f, and two input arguments
BinaryFunctionExpression(Function f, //
const Expression<A1>& e1, const Expression<A2>& e2) :
BinaryExpression<T, A1, A2>(e1, e2), function_(f) {
}
friend class Expression<T> ;
public:
/// Destructor
virtual ~BinaryFunctionExpression() {
}
/// Return value
virtual T value(const Values& values) const {
using boost::none;

View File

@ -30,6 +30,12 @@ namespace gtsam {
*/
template<typename T>
class Expression {
private:
// Paul's trick shared pointer, polymorphic root of entire expression tree
boost::shared_ptr<ExpressionNode<T> > root_;
public:
// Construct a constant expression
@ -53,36 +59,36 @@ public:
}
/// Construct a nullary method expression
template<typename E>
Expression(const Expression<E>& expression,
typename NullaryMethodExpression<T, E>::Method f) {
// TODO Assert that root of expression is not null.
root_.reset(new NullaryMethodExpression<T, E>(expression, f));
template<typename A>
Expression(const Expression<A>& expression,
T (A::*method)(boost::optional<Matrix&>) const) {
root_.reset(
new UnaryExpression<T, A>(boost::bind(method, _1, _2), expression));
}
/// Construct a unary function expression
template<typename E>
Expression(typename UnaryFunctionExpression<T, E>::Function f,
const Expression<E>& expression) {
// TODO Assert that root of expression is not null.
root_.reset(new UnaryFunctionExpression<T, E>(f, expression));
template<typename A>
Expression(typename UnaryExpression<T, A>::Function function,
const Expression<A>& expression) {
root_.reset(new UnaryExpression<T, A>(function, expression));
}
/// Construct a unary method expression
template<typename E1, typename E2>
Expression(const Expression<E1>& expression1,
typename UnaryMethodExpression<T, E1, E2>::Method f,
const Expression<E2>& expression2) {
// TODO Assert that root of expressions 1 and 2 are not null.
root_.reset(new UnaryMethodExpression<T, E1, E2>(expression1, f, expression2));
template<typename A1, typename A2>
Expression(const Expression<A1>& expression1,
T (A1::*method)(const A2&, boost::optional<Matrix&>,
boost::optional<Matrix&>) const, const Expression<A2>& expression2) {
root_.reset(
new BinaryExpression<T, A1, A2>(boost::bind(method, _1, _2, _3, _4),
expression1, expression2));
}
/// Construct a binary function expression
template<typename E1, typename E2>
Expression(typename BinaryFunctionExpression<T, E1, E2>::Function f,
const Expression<E1>& expression1, const Expression<E2>& expression2) {
// TODO Assert that root of expressions 1 and 2 are not null.
root_.reset(new BinaryFunctionExpression<T, E1, E2>(f, expression1, expression2));
template<typename A1, typename A2>
Expression(typename BinaryExpression<T, A1, A2>::Function function,
const Expression<A1>& expression1, const Expression<A2>& expression2) {
root_.reset(
new BinaryExpression<T, A1, A2>(function, expression1, expression2));
}
/// Return keys that play in this expression
@ -103,8 +109,7 @@ public:
const boost::shared_ptr<ExpressionNode<T> >& root() const {
return root_;
}
private:
boost::shared_ptr<ExpressionNode<T> > root_;
};
// http://stackoverflow.com/questions/16260445/boost-bind-to-operator