[CppAD] Eigen expressions with CppAD
Braun, Michael
braunm at mail.smu.edu
Wed Dec 18 12:02:36 EST 2013
I'm not sure if this is an Eigen issue or a CppAD issue, but I am hoping someone on this list can help me narrow down this problem.
One of the nice features about Eigen is that it can efficiently compute matrix expressions like MatrixXd C = A * B * A.transpose(). It appears that when using the CppAD::AD<double> type, I get a "use of overloaded operator '*' is ambiguous" error when compiling (with both clang and intel). Eigen does not want to multiply a return-type expression by a matrix. There are no problems when the base scalar type is double.
I would greatly appreciate and suggestions, or other ideas about what is going on.
Thanks.
#include <Eigen/Core>
#include <cppad/cppad.hpp>
#include <cppad/example/cppad_eigen.hpp>
#include <iostream>
using Eigen::MatrixXd;
using Eigen::Matrix;
using Eigen::Dynamic;
using std::cout;
using Eigen::MatrixBase;
typedef CppAD::AD<double> AScalar;
template<typename T>
void print_mat(const MatrixBase<T>& X){
for (int i=0; i<X.rows(); i++) {
for (int j=0; j<X.cols(); j++) {
cout << CppAD::Value(X(i,j)) << "\t";
}
cout << "\n";
}
}
int main() {
Matrix<double, Dynamic, Dynamic> A1(3,3);
Matrix<double, Dynamic, Dynamic> B1(3,3);
Matrix<double, Dynamic, Dynamic> C1(3,3);
Matrix<double, Dynamic, Dynamic> D1(3,3);
Matrix<AScalar, Dynamic, Dynamic> A2(3,3);
Matrix<AScalar, Dynamic, Dynamic> B2(3,3);
Matrix<AScalar, Dynamic, Dynamic> C2(3,3);
Matrix<AScalar, Dynamic, Dynamic> D2(3,3);
A1 << 1,2,3,
0,4,5,
0,0,6;
B1 << 2,4,6,
4,5,7,
6,7,1;
A2 = A1.cast<AScalar>();
B2 = B1.cast<AScalar>();
// ABA' with Base = double. This works.
C1 = A1 * B1 * A1.transpose();
cout << "C1:\n" << C1 << "\n\n";
// (AB)A' with Base = AD<double>. This works
D2 = (A2 * B2).eval() * A2.transpose();
cout << "D2:\n";
print_mat(D2);
// ABA' with Base = AD<double>. Does not compile.
// "use of overloaded operator '*' is ambiguous" error.
C2 = A2 * B2 * A2.transpose();
cout << "C2:\n" << C2 << "\n\n";
}
--------------------------
Michael Braun
Associate Professor of Marketing
Cox School of Business
Southern Methodist University
Dallas, TX 75275
braunm at smu.edu
More information about the CppAD
mailing list