Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do colwise operations in Eigen

Tags:

c++

eigen

I am doing this a lot:

auto f_conj = f.conjugate(); //f is a MatrixXcf, so is C;
for(n=0;n<X.cols();++n)
    C.col(n) = X.col(n).cwiseProduct(f_conj);

Am I not supposed to be able to do something like

C.colwise() = X.colwise().cwiseProduct(f_conj)

instead?

like image 805
Poul K. Sørensen Avatar asked Mar 01 '13 18:03

Poul K. Sørensen


1 Answers

What you are really doing is a diagonal product, so I'd recommend you the following expression:

C = f.conjugate().asDiagonal() * X;

If you want to use a colwise() expression, then do not put it on the left hand side:

C = X.colwise().cwiseProduct(f.conjugate());

Moreover, let me warn you about the use of the auto keyword. Here, let me emphasize that f_conj is not a VectorXcf, but an expression of the conjugate of a VectorXcf. So using f_conj or f.conjugate() is exactly the same. Since multiplying two complexes or one complex and one conjugate complex amount to the same cost, in this precise case it's ok to use the auto keyword. However, if f_conj would be for instance: auto f_conj = (f+g).conjugate(), then f+g would be recomputed many times in your for loop. Doing (f+g).conjugate().asDiagonal() * X is perfectly fine though, because Eigen knows what to do.

like image 89
ggael Avatar answered Oct 19 '22 03:10

ggael