I'm having trouble multiplying two matrices using the Eigen library. I have the following function. Here is a small example of what I would like to do :
Program name: testMatOp.cpp
#include <iostream>
#include <Eigen/Dense>
using namespace std;
template <typename DerivedA, typename DerivedB>
void multiply(const Eigen::MatrixBase<DerivedA> &A,
const Eigen::ArrayBase<DerivedB> &B){
Eigen::MatrixXf C(2,4);
C.array() = A.array().rowwise() * B.cast<float>();
}
int main()
{
Eigen::MatrixXf A(2,4);
Eigen::MatrixXf C(2,4);
//igen::VectorXf v(4);
Eigen::Array<int,1,Eigen::Dynamic>B;
B.resize(4);
A << 1, 2, 6, 9,
3, 1, 7, 2;
B << 0,
1,
0,
0;
multiply(A,B);
}
I would like to pass Matrix A and Vector B to multiply. I understand that Eigen does not do automatic promotion and that B needs to be casted as a float vector in order for the multiplication to happen. When I compile, I get the following compilation error among others
testMatOp.cpp:34:44: error: expected primary-expression before 'float'
testMatOp.cpp:34:44: error: expected ';' before 'float'
testMatOp.cpp: In instantiation of 'void multiply(const Eigen::MatrixBase<Derived>&, const Eigen::ArrayBase<DerivedB>&) [with DerivedA = Eigen::Matrix<float, -1, -1>; DerivedB = Eigen::Array<int, 1, -1>]':
testMatOp.cpp:54:15: required from here
testMatOp.cpp:34:3: error: no match for 'operator*' in '((const Eigen::DenseBase<Eigen::ArrayWrapper<const Eigen::Matrix<float, -1, -1> > >*)(&(& A)->Eigen::MatrixBase<Derived>::array<Eigen::Matrix<float, -1, -1> >()))->Eigen::DenseBase<Derived>::rowwise<Eigen::ArrayWrapper<const Eigen::Matrix<float, -1, -1> > >() * B.Eigen::ArrayBase<Derived>::cast<NewType>'
testMatOp.cpp:34:3: note: candidates are:
In file included from ../3rdparty/Eigen/Core:336:0,
from ../3rdparty/Eigen/Dense:1,
from testMatOp.cpp:26:
What might I be doing wrong. I did take a look at this post : Cast Eigen::MatrixXd to Eigen::MatrixXf which correctly describes how to cast, but I'm unable to make it work for this example.
Any help would be appreciated. Thanks!
-a