From 8e264f4289f70440c7382e90453a4c0c1e89c831 Mon Sep 17 00:00:00 2001 From: dellaert Date: Thu, 9 Oct 2014 14:38:16 +0200 Subject: [PATCH] Attempt at defining Trace recursively --- gtsam_unstable/nonlinear/Expression-inl.h | 50 ++++++++++++++--------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/gtsam_unstable/nonlinear/Expression-inl.h b/gtsam_unstable/nonlinear/Expression-inl.h index c97a903eb..b7038fc4c 100644 --- a/gtsam_unstable/nonlinear/Expression-inl.h +++ b/gtsam_unstable/nonlinear/Expression-inl.h @@ -378,6 +378,36 @@ public: }; +//----------------------------------------------------------------------------- + +#include + +/// Recursive Trace Class +template +struct MakeTrace: public JacobianTrace { + typedef boost::mpl::front A1; + static const size_t dimA = A1::dimension; + typedef Eigen::Matrix JacobianTA; + typedef Eigen::Matrix Jacobian2T; + + typename JacobianTrace::Pointer trace1; + JacobianTA dTdA1; + + /// Start the reverse AD process + virtual void startReverseAD(JacobianMap& jacobians) const { + Select::reverseAD(trace1, dTdA1, jacobians); + } + /// Given df/dT, multiply in dT/dA and continue reverse AD process + virtual void reverseAD(const Matrix& dFdT, JacobianMap& jacobians) const { + trace1.reverseAD(dFdT * dTdA1, jacobians); + } + /// Version specialized to 2-dimensional output + virtual void reverseAD2(const Jacobian2T& dFdT, + JacobianMap& jacobians) const { + trace1.reverseAD2(dFdT * dTdA1, jacobians); + } +}; + //----------------------------------------------------------------------------- /// Unary Function Expression template @@ -423,25 +453,7 @@ public: } /// Trace structure for reverse AD - struct Trace: public JacobianTrace { - typename JacobianTrace::Pointer trace1; - JacobianTA dTdA1; - - /// Start the reverse AD process - virtual void startReverseAD(JacobianMap& jacobians) const { - Select::reverseAD(trace1, dTdA1, jacobians); - } - /// Given df/dT, multiply in dT/dA and continue reverse AD process - virtual void reverseAD(const Matrix& dFdT, JacobianMap& jacobians) const { - trace1.reverseAD(dFdT * dTdA1, jacobians); - } - /// Version specialized to 2-dimensional output - typedef Eigen::Matrix Jacobian2T; - virtual void reverseAD2(const Jacobian2T& dFdT, - JacobianMap& jacobians) const { - trace1.reverseAD2(dFdT * dTdA1, jacobians); - } - }; + typedef MakeTrace > Trace; /// Construct an execution trace for reverse AD virtual T traceExecution(const Values& values,