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> template<class T, class A>
class UnaryExpression: public ExpressionNode<T> { 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_; boost::shared_ptr<ExpressionNode<A> > expressionA_;
/// Constructor with one input argument expression /// Constructor with a unary function f, and input argument e
UnaryExpression(const Expression<A>& e) : UnaryExpression(Function f, const Expression<A>& e) :
expressionA_(e.root()) { function_(f), expressionA_(e.root()) {
} }
friend class Expression<T> ;
public: public:
/// Destructor /// Destructor
@ -245,78 +252,6 @@ public:
return expressionA_->keys(); 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 /// Return value
virtual T value(const Values& values) const { virtual T value(const Values& values) const {
return function_(this->expressionA_->value(values), boost::none); return function_(this->expressionA_->value(values), boost::none);
@ -340,16 +275,26 @@ public:
template<class T, class A1, class A2> template<class T, class A1, class A2>
class BinaryExpression: public ExpressionNode<T> { 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<A1> > expressionA1_;
boost::shared_ptr<ExpressionNode<A2> > expressionA2_; boost::shared_ptr<ExpressionNode<A2> > expressionA2_;
/// Constructor with a binary function f, and two input arguments /// Constructor with a binary function f, and two input arguments
BinaryExpression(const Expression<A1>& e1, const Expression<A2>& e2) : BinaryExpression(Function f, //
expressionA1_(e1.root()), expressionA2_(e2.root()) { const Expression<A1>& e1, const Expression<A2>& e2) :
function_(f), expressionA1_(e1.root()), expressionA2_(e2.root()) {
} }
friend class Expression<T> ;
public: public:
/// Destructor /// Destructor
@ -364,88 +309,6 @@ public:
return keys1; 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 /// Return value
virtual T value(const Values& values) const { virtual T value(const Values& values) const {
using boost::none; using boost::none;

View File

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