I am trying to use the CppAD scalar type with Eigen.
The following fails to compile:
#include <Eigen/Dense>
#include <cppad/cppad.hpp>
#include <cppad/example/cppad_eigen.hpp>
int main()
{
using Scalar = CppAD::AD<double>;
//using Scalar = double;
Eigen::Matrix<Scalar, 1,4> row;
Eigen::Matrix<Scalar, 4,1> col;
Scalar scalar = 5;
Scalar res2 = row * col + scalar; //fails
return 0;
}
The error is basically that it cannot add a scalar to the result of the multiplication. However, the result of the multiplication is a scalar itself, so it should not be a problem. Indeed, when using double as the Scalar type, there are no issues.
Here is the compiler error:
cppad-eigen-problem.cpp:14:29: error: no match for ‘operator+’ (operand types are ‘const Eigen::Product<Eigen::Matrix<CppAD::AD<double>, 1, 4>, Eigen::Matrix<CppAD::AD<double>, 4, 1, 0, 4, 1>, 0>’ and ‘Scalar’ {aka ‘CppAD::AD<double>’})
14 | Scalar res2 = row * col + scalar; //fails
| ~~~~~~~~~ ^ ~~~~~~
| | |
| | Scalar {aka CppAD::AD<double>}
| const Eigen::Product<Eigen::Matrix<CppAD::AD<double>, 1, 4>, Eigen::Matrix<CppAD::AD<double>, 4, 1, 0, 4, 1>, 0>
There is an issue on the CppAD project, but I am not sure where the problem is:
If I use another trivial custom scalar, I cannot reproduce the error...
On the other hand, the CppAD Eigen traits for their scalar types looks ok to me.
Versions: Eigen 3.3.7, latest master of CppAD, g++ 9.3.0
Any clue?
Thanks