From bce84ca4db4cc22293ff64350ba9cbc669e7a5b5 Mon Sep 17 00:00:00 2001 From: dellaert Date: Mon, 20 Oct 2014 15:38:27 +0200 Subject: [PATCH] Successfully created Expression from AutoDiff function! --- .../nonlinear/tests/testExpression.cpp | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/gtsam_unstable/nonlinear/tests/testExpression.cpp b/gtsam_unstable/nonlinear/tests/testExpression.cpp index 11fe49dc6..8a788b7b7 100644 --- a/gtsam_unstable/nonlinear/tests/testExpression.cpp +++ b/gtsam_unstable/nonlinear/tests/testExpression.cpp @@ -519,6 +519,9 @@ class AdaptAutoDiff { static const int M1 = dimension::value; static const int M2 = dimension::value; + typedef Eigen::Matrix RowMajor1; + typedef Eigen::Matrix RowMajor2; + typedef Canonical CanonicalT; typedef Canonical Canonical1; typedef Canonical Canonical2; @@ -535,8 +538,8 @@ class AdaptAutoDiff { public: - typedef Eigen::Matrix JacobianTA1; - typedef Eigen::Matrix JacobianTA2; + typedef Eigen::Matrix JacobianTA1; + typedef Eigen::Matrix JacobianTA2; T operator()(const A1& a1, const A2& a2, boost::optional H1 = boost::none, boost::optional H2 = boost::none) { @@ -551,15 +554,26 @@ public: VectorT result; if (H1 || H2) { + // Get derivatives with AutoDiff double *parameters[] = { v1.data(), v2.data() }; - double *jacobians[] = { H1->data(), H2->data() }; + double rowMajor1[N * M1], rowMajor2[N * M2]; // om the stack + double *jacobians[] = { rowMajor1, rowMajor2 }; success = AutoDiff::Differentiate(f, parameters, 2, result.data(), jacobians); + + // Convert from row-major to columnn-major + // TODO: if this is a bottleneck (probably not!) fix Autodiff to be Column-Major + *H1 = Eigen::Map(rowMajor1); + *H2 = Eigen::Map(rowMajor2); + } else { // Apply the mapping, to get result success = f(v1.data(), v2.data(), result.data()); } + if (!success) + throw std::runtime_error( + "AdaptAutoDiff: function call resulted in failure"); return chartT.hat(result); } @@ -607,8 +621,8 @@ TEST(Expression, AutoDiff3) { Matrix E2 = numericalDerivative22(Adaptor(), P, X); // Get derivatives with AutoDiff, not gives RowMajor results! - Eigen::Matrix H1; - Eigen::Matrix H2; + Matrix29 H1; + Matrix23 H2; Point2 actual2 = snavely(P, X, H1, H2); EXPECT(assert_equal(expected,actual,1e-9)); EXPECT(assert_equal(E1,H1,1e-8)); @@ -616,13 +630,12 @@ TEST(Expression, AutoDiff3) { } TEST(Expression, Snavely) { -// Expression P(1); -// Expression X(2); -//// AutoDiff f; -// Expression expression( -// AdaptAutoDiff(), P, X); -// set expected = list_of(1)(2); -// EXPECT(expected == expression.keys()); + Expression P(1); + Expression X(2); + typedef AdaptAutoDiff Adaptor; + Expression expression(Adaptor(), P, X); + set expected = list_of(1)(2); + EXPECT(expected == expression.keys()); } /* ************************************************************************* */