From b213e6419a2137a90d7207aa9aabcb6091c73ed9 Mon Sep 17 00:00:00 2001 From: dellaert Date: Mon, 11 May 2015 22:12:00 -0700 Subject: [PATCH] Re-ordered for clarity --- gtsam/nonlinear/Expression-inl.h | 104 ++++++++++++++----------------- gtsam/nonlinear/Expression.h | 58 ++++++++--------- 2 files changed, 77 insertions(+), 85 deletions(-) diff --git a/gtsam/nonlinear/Expression-inl.h b/gtsam/nonlinear/Expression-inl.h index f9f81c598..b3ead11c4 100644 --- a/gtsam/nonlinear/Expression-inl.h +++ b/gtsam/nonlinear/Expression-inl.h @@ -27,36 +27,60 @@ namespace gtsam { -/// Print template void Expression::print(const std::string& s) const { std::cout << s << *root_ << std::endl; } -// Construct a constant expression template Expression::Expression(const T& value) : root_(new internal::ConstantExpression(value)) { } -// Construct a leaf expression, with Key template Expression::Expression(const Key& key) : root_(new internal::LeafExpression(key)) { } -// Construct a leaf expression, with Symbol template Expression::Expression(const Symbol& symbol) : root_(new internal::LeafExpression(symbol)) { } -// Construct a leaf expression, creating Symbol template Expression::Expression(unsigned char c, size_t j) : root_(new internal::LeafExpression(Symbol(c, j))) { } +/// Construct a unary function expression +template +template +Expression::Expression(typename UnaryFunction::type function, + const Expression& expression) : + root_(new internal::UnaryExpression(function, expression)) { +} + +/// Construct a binary function expression +template +template +Expression::Expression(typename BinaryFunction::type function, + const Expression& expression1, const Expression& expression2) : + root_( + new internal::BinaryExpression(function, expression1, + expression2)) { +} + +/// Construct a ternary function expression +template +template +Expression::Expression(typename TernaryFunction::type function, + const Expression& expression1, const Expression& expression2, + const Expression& expression3) : + root_( + new internal::TernaryExpression(function, expression1, + expression2, expression3)) { +} + /// Construct a nullary method expression template template @@ -67,14 +91,6 @@ Expression::Expression(const Expression& expression, expression)) { } -/// Construct a unary function expression -template -template -Expression::Expression(typename UnaryFunction::type function, - const Expression& expression) : - root_(new internal::UnaryExpression(function, expression)) { -} - /// Construct a unary method expression template template @@ -87,16 +103,6 @@ Expression::Expression(const Expression& expression1, boost::bind(method, _1, _2, _3, _4), expression1, expression2)) { } -/// Construct a binary function expression -template -template -Expression::Expression(typename BinaryFunction::type function, - const Expression& expression1, const Expression& expression2) : - root_( - new internal::BinaryExpression(function, expression1, - expression2)) { -} - /// Construct a binary method expression template template @@ -112,46 +118,16 @@ Expression::Expression(const Expression& expression1, expression2, expression3)) { } -/// Construct a ternary function expression -template -template -Expression::Expression(typename TernaryFunction::type function, - const Expression& expression1, const Expression& expression2, - const Expression& expression3) : - root_( - new internal::TernaryExpression(function, expression1, - expression2, expression3)) { -} - -/// Return root -template -const boost::shared_ptr >& Expression::root() const { - return root_; -} - -// Return size needed for memory buffer in traceExecution -template -size_t Expression::traceSize() const { - return root_->traceSize(); -} - -/// Return keys that play in this expression template std::set Expression::keys() const { return root_->keys(); } -/// Return dimensions for each argument, as a map template void Expression::dims(std::map& map) const { root_->dims(map); } -/** - * @brief Return value and optional derivatives, reverse AD version - * Notes: this is not terribly efficient, and H should have correct size. - * The order of the Jacobians is same as keys in either keys() or dims() - */ template T Expression::value(const Values& values, boost::optional&> H) const { @@ -165,7 +141,18 @@ T Expression::value(const Values& values, return root_->value(values); } -/// private version that takes keys and dimensions, returns derivatives +template +const boost::shared_ptr >& Expression::root() const { + return root_; +} + +template +size_t Expression::traceSize() const { + return root_->traceSize(); +} + +// Private methods: + template T Expression::value(const Values& values, const FastVector& keys, const FastVector& dims, std::vector& H) const { @@ -236,6 +223,7 @@ typename Expression::KeysAndDims Expression::keysAndDims() const { return pair; } +namespace internal { // http://stackoverflow.com/questions/16260445/boost-bind-to-operator template struct apply_compose { @@ -246,13 +234,17 @@ struct apply_compose { return x.compose(y, H1, H2); } }; +} + +// Global methods: /// Construct a product expression, assumes T::compose(T) -> T template Expression operator*(const Expression& expression1, const Expression& expression2) { - return Expression(boost::bind(apply_compose(), _1, _2, _3, _4), - expression1, expression2); + return Expression( + boost::bind(internal::apply_compose(), _1, _2, _3, _4), expression1, + expression2); } /// Construct an array of leaves diff --git a/gtsam/nonlinear/Expression.h b/gtsam/nonlinear/Expression.h index a7a3326c3..2c503dce9 100644 --- a/gtsam/nonlinear/Expression.h +++ b/gtsam/nonlinear/Expression.h @@ -26,6 +26,7 @@ #include #include +// Forward declare tests class ExpressionFactorShallowTest; namespace gtsam { @@ -96,16 +97,27 @@ public: /// Construct a leaf expression, creating Symbol Expression(unsigned char c, size_t j); - /// Construct a nullary method expression - template - Expression(const Expression& expression, - T (A::*method)(typename MakeOptionalJacobian::type) const); - /// Construct a unary function expression template Expression(typename UnaryFunction::type function, const Expression& expression); + /// Construct a binary function expression + template + Expression(typename BinaryFunction::type function, + const Expression& expression1, const Expression& expression2); + + /// Construct a ternary function expression + template + Expression(typename TernaryFunction::type function, + const Expression& expression1, const Expression& expression2, + const Expression& expression3); + + /// Construct a nullary method expression + template + Expression(const Expression& expression, + T (A::*method)(typename MakeOptionalJacobian::type) const); + /// Construct a unary method expression template Expression(const Expression& expression1, @@ -113,11 +125,6 @@ public: typename MakeOptionalJacobian::type) const, const Expression& expression2); - /// Construct a binary function expression - template - Expression(typename BinaryFunction::type function, - const Expression& expression1, const Expression& expression2); - /// Construct a binary method expression template Expression(const Expression& expression1, @@ -127,18 +134,6 @@ public: typename MakeOptionalJacobian::type) const, const Expression& expression2, const Expression& expression3); - /// Construct a ternary function expression - template - Expression(typename TernaryFunction::type function, - const Expression& expression1, const Expression& expression2, - const Expression& expression3); - - /// Return root - const boost::shared_ptr >& root() const; - - // Return size needed for memory buffer in traceExecution - size_t traceSize() const; - /// Return keys that play in this expression std::set keys() const; @@ -162,9 +157,15 @@ public: return boost::make_shared(*this); } + /// Return root + const boost::shared_ptr >& root() const; + + /// Return size needed for memory buffer in traceExecution + size_t traceSize() const; + private: - /// Vaguely unsafe keys and dimensions in same order + /// Keys and dimensions in same order typedef std::pair, FastVector > KeysAndDims; KeysAndDims keysAndDims() const; @@ -176,15 +177,14 @@ private: T traceExecution(const Values& values, internal::ExecutionTrace& trace, void* traceStorage) const; - /** - * @brief Return value and derivatives, reverse AD version - * This very unsafe method needs a JacobianMap with correctly allocated - * and initialized VerticalBlockMatrix, hence is declared private. - */ + /// brief Return value and derivatives, reverse AD version T value(const Values& values, JacobianMap& jacobians) const; // be very selective on who can access these private methods: - friend class ExpressionFactor ; + friend class ExpressionFactor; + friend class internal::ExpressionNode; + + // and add tests friend class ::ExpressionFactorShallowTest; };