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

Difference between broadcasting and the following?

Tags: None
(comma "," separated)
duetosymmetry
Registered Member
Posts
18
Karma
0
Let's say you have an (m x n) matrix M and a vector V of length n, and you want to multiply each row of M by the values in V, elementwise. With broadcasting, you would write:
Code: Select all
MatrixXf M(m, n);
VectorXf V(n);

...

M.rowwise() *= V;

Mathematically, you could also treat V as a square, diagonal matrix and right-multiply it, and this should achieve the same thing:
Code: Select all
M *= V.asDiagonal();


Is there a functional difference between these two? Do they generate the same code?

Cheers
Leo
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Code: Select all
M *= V.asDiagonal();

means
Code: Select all
M = M * V.asDiagonal();

which is functionally equivalent to
Code: Select all
M.colwise() *= V;

If you want rowwise, you have to put the diagonal on the left:
Code: Select all
M = V.asDiagonal() * M;


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]