/* ---------------------------------------------------------------------------- * 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.h * @date September 18, 2014 * @author Frank Dellaert * @author Paul Furgale * @brief Expressions for Block Automatic Differentiation */ #include "Expression-inl.h" #include #include #include #include namespace gtsam { /** * Expression class that supports automatic differentiation */ template class Expression { public: // Construct a constant expression Expression(const T& value) : root_(new ConstantExpression(value)) { } // Construct a leaf expression Expression(const Key& key) : root_(new LeafExpression(key)) { } /// Construct a unary expression template Expression(typename UnaryExpression::function f, const Expression& expression) { // TODO Assert that root of expression is not null. root_.reset(new UnaryExpression(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)); } /// Return keys that play in this expression std::set keys() const { return root_->keys(); } /// Return value and optional derivatives T value(const Values& values, boost::optional&> jacobians = boost::none) const { return root_->value(values, jacobians); } const boost::shared_ptr >& root() const { return root_; } private: boost::shared_ptr > root_; }; // http://stackoverflow.com/questions/16260445/boost-bind-to-operator template struct apply_compose { typedef T result_type; T operator()(const T& x, const T& y, boost::optional H1, boost::optional H2) const { return x.compose(y, H1, H2); } }; /// 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); } /** * BAD Factor that supports arbitrary expressions via AD */ template class BADFactor: NonlinearFactor { const T measurement_; const Expression expression_; /// get value from expression and calculate error with respect to measurement Vector unwhitenedError(const Values& values) const { const T& value = expression_.value(values); return value.localCoordinates(measurement_); } public: /// Constructor BADFactor(const T& measurement, const Expression& expression) : measurement_(measurement), expression_(expression) { } /// Constructor BADFactor(const T& measurement, const ExpressionNode& expression) : measurement_(measurement), expression_(expression) { } /** * Calculate the error of the factor. * This is the log-likelihood, e.g. \f$ 0.5(h(x)-z)^2/\sigma^2 \f$ in case of Gaussian. * In this class, we take the raw prediction error \f$ h(x)-z \f$, ask the noise model * to transform it to \f$ (h(x)-z)^2/\sigma^2 \f$, and then multiply by 0.5. */ virtual double error(const Values& values) const { if (this->active(values)) { const Vector e = unwhitenedError(values); return 0.5 * e.squaredNorm(); } else { return 0.0; } } /// get the dimension of the factor (number of rows on linearization) size_t dim() const { return 0; } /// linearize to a GaussianFactor boost::shared_ptr linearize(const Values& values) const { // We will construct an n-ary factor below, where terms is a container whose // value type is std::pair, specifying the // collection of keys and matrices making up the factor. std::map terms; expression_.value(values, terms); Vector b = unwhitenedError(values); SharedDiagonal model = SharedDiagonal(); return boost::shared_ptr( new JacobianFactor(terms, b, model)); } }; // BADFactor }