Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eigen Matrices multiplication

Tags:

c++

eigen

Why is it necessary to use the noallias() expression when doing matrix product when using c++ eigen library?

m1.noalias() += (s1*s2*conj(s3)*s4) * m2.adjoint() * m3.conjugate()

I have been reading some notes about it but still find it difficult to understand.

like image 259
NdjikeFils Avatar asked May 17 '26 05:05

NdjikeFils


1 Answers

when you are doing a sum like:

A=A+B

eigen can directly use the variable A to perform the operation since, each cell of the matrix can be calculated without impacting the calculation of the other cells Ai,j=Ai,j+Bi,j

when you are doing a product like:

A=A*B

you can not do the same since if you start calculating and replace A0,0 - then you can not calculate the other A0,j

so by default - when performing assignment of a product operation, a temporary structure is created and the assignment is done afterward (see noalias).

When you use noalias on the source term of an assignment, you "guarantee" that the assigned variable is not part of the terms of the product and that it is safe to not use a temporary structure.

This is coming from the fact that Eigen is "lazy" about when performing the operations (meaning that it does it only when necessary and not instantly as we are used to in standard C++) - noalias is the way to tell Eigen that this is also safe to do when doing a product operation and assigning it to a variable.

like image 134
Jean Senellart Avatar answered May 18 '26 20:05

Jean Senellart