From c8dd361080205e96fdd64bcebfc039df7d5fb182 Mon Sep 17 00:00:00 2001 From: dellaert Date: Fri, 3 Oct 2014 12:48:28 +0200 Subject: [PATCH] Common base class BinaryExpression --- gtsam_unstable/nonlinear/Expression-inl.h | 116 ++++++++++++---------- gtsam_unstable/nonlinear/Expression.h | 22 ++-- 2 files changed, 76 insertions(+), 62 deletions(-) diff --git a/gtsam_unstable/nonlinear/Expression-inl.h b/gtsam_unstable/nonlinear/Expression-inl.h index 93240f80d..500b4493b 100644 --- a/gtsam_unstable/nonlinear/Expression-inl.h +++ b/gtsam_unstable/nonlinear/Expression-inl.h @@ -335,31 +335,21 @@ public: }; //----------------------------------------------------------------------------- -/// Binary function Expression +/// Binary Expression template class BinaryExpression: public ExpressionNode { -public: - - typedef boost::function< - T(const A1&, const A2&, boost::optional, - boost::optional)> function; - -private: +protected: boost::shared_ptr > expressionA1_; boost::shared_ptr > expressionA2_; - function function_; /// Constructor with a binary function f, and two input arguments - BinaryExpression(function f, // - const Expression& e1, const Expression& e2) : - expressionA1_(e1.root()), expressionA2_(e2.root()), function_(f) { + BinaryExpression(const Expression& e1, const Expression& e2) : + expressionA1_(e1.root()), expressionA2_(e2.root()) { } - friend class Expression ; - public: /// Destructor @@ -374,32 +364,13 @@ public: return keys1; } - /// Return value - virtual T value(const Values& values) const { - using boost::none; - return function_(expressionA1_->value(values), expressionA2_->value(values), - none, none); - } - - /// Return value and derivatives - virtual Augmented augmented(const Values& values) const { - using boost::none; - Augmented argument1 = expressionA1_->augmented(values); - Augmented argument2 = expressionA2_->augmented(values); - Matrix H1, H2; - T t = function_(argument1.value(), argument2.value(), - argument1.constant() ? none : boost::optional(H1), - argument2.constant() ? none : boost::optional(H2)); - return Augmented(t, H1, argument1.jacobians(), H2, argument2.jacobians()); - } - }; //----------------------------------------------------------------------------- /// Binary Expression template -class MethodExpression: public ExpressionNode { +class UnaryMethodExpression: public BinaryExpression { public: @@ -408,13 +379,12 @@ public: private: - boost::shared_ptr > expressionA1_; - boost::shared_ptr > expressionA2_; method method_; /// Constructor with a binary function f, and two input arguments - MethodExpression(const Expression& e1, method f, const Expression& e2) : - expressionA1_(e1.root()), expressionA2_(e2.root()), method_(f) { + UnaryMethodExpression(const Expression& e1, method f, + const Expression& e2) : + BinaryExpression(e1, e2), method_(f) { } friend class Expression ; @@ -422,29 +392,21 @@ private: public: /// Destructor - virtual ~MethodExpression() { - } - - /// Return keys that play in this expression - virtual std::set keys() const { - std::set keys1 = expressionA1_->keys(); - std::set keys2 = expressionA2_->keys(); - keys1.insert(keys2.begin(), keys2.end()); - return keys1; + virtual ~UnaryMethodExpression() { } /// Return value virtual T value(const Values& values) const { using boost::none; - return (expressionA1_->value(values).*(method_))(expressionA2_->value(values), - none, none); + return (this->expressionA1_->value(values).*(method_))( + this->expressionA2_->value(values), none, none); } /// Return value and derivatives virtual Augmented augmented(const Values& values) const { using boost::none; - Augmented argument1 = expressionA1_->augmented(values); - Augmented argument2 = expressionA2_->augmented(values); + Augmented argument1 = this->expressionA1_->augmented(values); + Augmented argument2 = this->expressionA2_->augmented(values); Matrix H1, H2; T t = (argument1.value().*(method_))(argument2.value(), argument1.constant() ? none : boost::optional(H1), @@ -454,5 +416,57 @@ public: }; +//----------------------------------------------------------------------------- +/// Binary function Expression + +template +class BinaryFunctionExpression: public BinaryExpression { + +public: + + typedef boost::function< + T(const A1&, const A2&, boost::optional, + boost::optional)> function; + +private: + + function function_; + + /// Constructor with a binary function f, and two input arguments + BinaryFunctionExpression(function f, // + const Expression& e1, const Expression& e2) : + BinaryExpression(e1, e2), function_(f) { + } + + friend class Expression ; + +public: + + /// Destructor + virtual ~BinaryFunctionExpression() { + } + + /// Return value + virtual T value(const Values& values) const { + using boost::none; + return function_(this->expressionA1_->value(values), + this->expressionA2_->value(values), none, none); + } + + /// Return value and derivatives + virtual Augmented augmented(const Values& values) const { + using boost::none; + Augmented argument1 = this->expressionA1_->augmented(values); + Augmented argument2 = this->expressionA2_->augmented(values); + Matrix H1, H2; + T t = function_(argument1.value(), argument2.value(), + argument1.constant() ? none : boost::optional(H1), + argument2.constant() ? none : boost::optional(H2)); + return Augmented(t, H1, argument1.jacobians(), H2, argument2.jacobians()); + } + +}; +//----------------------------------------------------------------------------- + } diff --git a/gtsam_unstable/nonlinear/Expression.h b/gtsam_unstable/nonlinear/Expression.h index 82c0e2119..ab276434c 100644 --- a/gtsam_unstable/nonlinear/Expression.h +++ b/gtsam_unstable/nonlinear/Expression.h @@ -68,21 +68,21 @@ public: root_.reset(new UnaryFunctionExpression(f, expression)); } - /// Construct a binary expression - template - Expression(typename BinaryExpression::function f, - const Expression& expression1, const Expression& expression2) { - // TODO Assert that root of expressions 1 and 2 are not null. - root_.reset(new BinaryExpression(f, expression1, expression2)); - } - - /// Construct a binary expression, where a method is passed + /// Construct a unary method expression template Expression(const Expression& expression1, - typename MethodExpression::method f, + typename UnaryMethodExpression::method f, const Expression& expression2) { // TODO Assert that root of expressions 1 and 2 are not null. - root_.reset(new MethodExpression(expression1, f, expression2)); + root_.reset(new UnaryMethodExpression(expression1, f, expression2)); + } + + /// Construct a binary function expression + template + Expression(typename BinaryFunctionExpression::function f, + const Expression& expression1, const Expression& expression2) { + // TODO Assert that root of expressions 1 and 2 are not null. + root_.reset(new BinaryFunctionExpression(f, expression1, expression2)); } /// Return keys that play in this expression