/* ---------------------------------------------------------------------------- * 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 testBAD.cpp * @date September 18, 2014 * @author Frank Dellaert * @brief unit tests for Block Automatic Differentiation */ #include #include #include #include #include #include #include using namespace std; using namespace gtsam; /// This class might have to become a class hierarchy ? template class Expression { public: /// Constructor with a single key Expression(Key key) { } /// Constructor with a value, yielding a constant Expression(const T& t) { } }; /// Expression version of transform Expression transformTo(const Expression& x, const Expression& p) { return Expression(0); } /// Expression version of project Expression project(const Expression& p) { return Expression(0); } /// Expression version of uncalibrate Expression uncalibrate(const Expression& K, const Expression& p) { return Expression(0); } /// Expression version of Point2.sub Expression operator -(const Expression& p, const Expression& q) { return Expression(0); } /// AD Factor template class BADFactor: NonlinearFactor { public: /// Constructor BADFactor(const Expression& t) { } /** * Calculate the error of the factor * This is typically equal to log-likelihood, e.g. \f$ 0.5(h(x)-z)^2/sigma^2 \f$ */ double error(const Values& c) const { return 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& c) const { return boost::shared_ptr(new JacobianFactor()); } }; /* ************************************************************************* */ TEST(BAD, test) { // Create leaves Expression x(1); Expression p(2); Expression K(3); Expression uv(Point2(300, 62)); // Create expression tree Expression p_cam = transformTo(x, p); Expression projection = project(p_cam); Expression uv_hat = uncalibrate(K, projection); Expression e = uv - uv_hat; // Create factor BADFactor f(e); // Create some values Values values; // Check value EXPECT_DOUBLES_EQUAL(0, f.error(values), 1e-9); // Check dimension EXPECT_LONGS_EQUAL(0, f.dim()); // Check linearization JacobianFactor expected( // 1, (Matrix(2, 6) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), // 2, (Matrix(2, 3) << 1, 2, 3, 4, 5, 6), // 3, (Matrix(2, 5) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), // (Vector(2) << 1, 2)); boost::shared_ptr gf = f.linearize(values); boost::shared_ptr jf = // boost::dynamic_pointer_cast(gf); EXPECT( assert_equal(expected, *jf,1e-9)); } /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr); } /* ************************************************************************* */