Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for autodiff #237

Merged
merged 7 commits into from
Sep 26, 2021
Merged

Add support for autodiff #237

merged 7 commits into from
Sep 26, 2021

Conversation

artivis
Copy link
Owner

@artivis artivis commented Sep 25, 2021

Add support for the autodiff library

autodiff automatic differentiation made easier for C++ (web - github).

This PR only adds support for the autodiff::dual type.

Find a doc page here (mostly copied from the Ceres/Jet doc page).

The Jacobians produced by autodiff are compared against manif analytics ones in unit tests.

Also adds

  • operator[](i) to both groups and tangents to access single elements of the underlying vector
  • size() to both groups and tangents which return the underlying vector size (== RepSize)

Closes #196.
Closes #143.

@artivis artivis self-assigned this Sep 25, 2021
@artivis artivis added the enhancement New feature or request label Sep 25, 2021
@codecov
Copy link

codecov bot commented Sep 25, 2021

Codecov Report

Merging #237 (c85ac91) into devel (317ff22) will increase coverage by 0.00%.
The diff coverage is n/a.

@@           Coverage Diff           @@
##            devel     #237   +/-   ##
=======================================
  Coverage   98.17%   98.17%           
=======================================
  Files          55       55           
  Lines        1753     1754    +1     
=======================================
+ Hits         1721     1722    +1     
  Misses         32       32           

@artivis artivis force-pushed the feature/autodiff branch 2 times, most recently from a01c838 to b430a8c Compare September 25, 2021 21:13
@artivis
Copy link
Owner Author

artivis commented Sep 25, 2021

@joansola, this is ready for review/approval.

Copy link
Collaborator

@joansola joansola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me. I have not tested it, though.

What exactly is the autodiff package?

@artivis
Copy link
Owner Author

artivis commented Sep 26, 2021

What exactly is the autodiff package?

It's a modern (C++17) library for automatic differentiation that provides a scalar type and a few neat functions. Think ceres::Jet and the bits to evaluate the Jacobians.

Here is a striped down example (yet nearly compilable):

using dual = autodiff::dual;

// cost function
auto fun = [](const auto& measurement, const auto& state_i, const auto& state_j){
  return measurement - (state_j - state_i);
};

SE3<dual> xi, xj;
SE3Tangent<dual> meas_ij, e;

// cost function Jacs wrt underlying vector
Eigen::MatrixXd J_e_xi = autodiff::jacobian(fun, autodiff::wrt(xi), autodiff::at(meas_ij, xi, xj), e);
Eigen::MatrixXd J_e_xj = autodiff::jacobian(fun, autodiff::wrt(xj), autodiff::at(meas_ij, xi, xj), e);

// Local param Jacs
Eigen::MatrixXd J_xi_xiw0 = manif::autodiffLocalParameterizationJacobian<dual>(xi);
Eigen::MatrixXd J_xj_xjw0 = manif::autodiffLocalParameterizationJacobian<dual>(xj);

// cost function Jacs wrt local perturbation on the tangent space
Eigen::MatrixXd J_e_xiw0 = J_e_xi * J_xi_xiw0;
Eigen::MatrixXd J_e_xjw0 = J_e_xj * J_xj_xjw0;

Find more examples in the unit tests.

Copy link
Collaborator

@joansola joansola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@artivis artivis merged commit ab560a3 into devel Sep 26, 2021
@artivis artivis deleted the feature/autodiff branch September 26, 2021 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

autodiff (library) support Include operator()(int idx) for manif tangent-types?
2 participants