This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Vector Matrix Vector computation

Tags: None
(comma "," separated)
tmensink
Registered Member
Posts
3
Karma
0

Vector Matrix Vector computation

Fri May 06, 2011 3:53 am
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
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
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
tmensink
Registered Member
Posts
3
Karma
0
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


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], q.ignora