![]() Registered Member ![]()
|
Hi,
I have the feeling that this is a very easy question, but I couldn't find it in the Tutorial, nor on the board. I want to compute the scalar = Vec' * Matrix * Vec; What I currently do is: tmp = Vec' * Matrix * Vec; scalar = tmp(0,0); But this seems not optimal, and it seems slow. Thanks, Thomas |
![]() Moderator ![]()
|
Are you using very small fixed size matrix-vector (e.g. Vector4d) or dynamic ones (e.g., VectorXd)? If the former, then you could try to use a lazy product, e.g.:
scalar = (Matrix.lazyProduct(Vec).array() * Vec.array()).sum(); In the former case, the only minor optimization I see is in case you have at hand a vector that you can reuse as a temporary, let's call it aux: aux.noalias() = Matrix * Vec; scalar = (Vec.adjoint() * aux); In both cases, the perf gain will be very small if any. This is nearly optimal. There is no |
![]() Registered Member ![]()
|
Hi ggael,
Thanks for your quick respond. Good to know it is (near) optimal, it means that another part of my code is (too) slow. Cheers, Thomas |
Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], q.ignora