AutoDiff - Eigen Matrix Support and with operators like Dot / Cross Product

349 views
Skip to first unread message

Adam Hartshorne

unread,
Mar 20, 2017, 9:27:15 PM3/20/17
to Ceres Solver
I wonder if somebody could confirm if one has a templated cost function that supports the built-in autodiff, does Ceres supports the use of Eigen Matrices and also operators like dot product and cross product e.g something like this

 template <typename T>
 
bool operator()(const T* input_pointA, const T* input_pointB, const T* input_pointC, T* residual) const {

 
Eigen::Map<const Eigen::Matrix<T, 3, 1> > pointA(input_pointA);
 
Eigen::Map<const Eigen::Matrix<T, 3, 1> > pointB(input_pointB);
 
Eigen::Map<const Eigen::Matrix<T, 3, 1> > pointC(input_pointC);

 
Eigen::Matrix<T, 3, 1> N = (pointA.cross(pointB)).normalize() ;

 residual
[0] = pointA.dot(pointB);
 residual
[1] = N.dot(pointC);

 
return true ;

}


Furthermore can you abstract it further such as like this?



template <typename T>
void testFunction(const Eigen::Matrix<T, 3, 1> &A,
 
const Eigen::Matrix<T, 3, 1> &B,
 
const Eigen::Matrix<T, 3, 1> &C
Eigen::Matrix<T, 2, 1>& resids)
{

 
Eigen::Matrix<T, 3, 1> N = (A.cross(B)).normalize() ;
  resids
(0) = A.dot(B);
  resids
(1) = N.dot(C);

}


 
template <typename T>
 
bool operator()(const T* input_pointA, const T* input_pointB, const T* input_pointC, T* residual) const {

 
Eigen::Map<const Eigen::Matrix<T, 3, 1> > pointA(input_pointA);
 
Eigen::Map<const Eigen::Matrix<T, 3, 1> > pointB(input_pointB);
 
Eigen::Map<const Eigen::Matrix<T, 3, 1> > pointC(input_pointC);

 
Eigen::Matrix<T, 2, 1> resids ;

 testFunction
(pointA, pointB, pointC, resids)

 residual
[0] = resids(0) ;
 residual
[1] = resids(1);

 
return true ;
}

Sameer Agarwal

unread,
Mar 20, 2017, 9:31:39 PM3/20/17
to Ceres Solver, sweeney...@gmail.com
Adam,
Try Ceres Solver at HEAD. A bunch of changes were recently submitted to make autodiff work better with eigen matrices, e.g.
I am sure +Chris Sweeney can say more.
Sameer


--
You received this message because you are subscribed to the Google Groups "Ceres Solver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ceres-solver...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ceres-solver/7283826c-4112-43e5-8a6e-0121d8221729%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Sweeney

unread,
Mar 20, 2017, 9:38:33 PM3/20/17
to Ceres Solver
Adam,

With the HEAD of the master branch, I use functions like that and much more linear algebra with Eigen within my templated cost functions. You should also have Eigen 3.3 or later in order for this to work though.

You "test" function will not work because your function arguments are Eigen::Matrix types but you are passing Eigen::Map types. See Eigen's documentation for how to write functions for Eigen objects: http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html

Chris

Adam Hartshorne

unread,
Mar 20, 2017, 9:42:46 PM3/20/17
to Ceres Solver, sweeney...@gmail.com
Thanks for the info.

Yes I saw the mistake with my second example just after I posted it. Sorry late here.

But I am correct in thinking that it is possible to have such an secondary templated function that is called from within the cost function () ?
Reply all
Reply to author
Forward
0 new messages