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

Tensor product of two vectors

Tags: None
(comma "," separated)
davidhigh
Registered Member
Posts
18
Karma
0

Tensor product of two vectors

Thu Mar 29, 2012 10:52 pm
Hello,

is there an efficient way to calculate the tensor product of two vectors (with the same dimension)?

thanks in advance,
David

Code: Select all
const int N=5;
Eigen::VectorXd a=Eigen::MatrixXd::Random(N);
Eigen::VectorXd b=Eigen::MatrixXd::Random(N);
Eigen::MatrixXd c(N,N);

for(int i=0;i<N;++i)
   for(int j=0;j<N;++j)
       c(i,j)=a(i)*b(j);


did I mention that Eigen is great?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Tensor product of two vectors

Thu Mar 29, 2012 10:57 pm
that's a standard matrix product:

c = a * b;

and to avoid a temporary you can tell Eigen there is no aliasing:

c.noalias() = a * b;
davidhigh
Registered Member
Posts
18
Karma
0

Re: Tensor product of two vectors

Thu Mar 29, 2012 11:31 pm
Now that you mention it -- of course. Thank you.

What does aliasing mean? should I do it in any Mat-Mat product? I thought this lazy-evaluation stuff does it...?

best,
David
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Tensor product of two vectors

Fri Mar 30, 2012 7:39 am
matrix products is the exception. This is to make sure that the common: X = X*X.adjoint; works fine.Using noalias makes sense only if you have the matrix product on the righ and side.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]