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

Tensor-Contraction and Index-addressing

Tags: None
(comma "," separated)
cruck
Registered Member
Posts
1
Karma
0
I would like to perform simple Tensorcontractions with the Eigen::Tensor module, but so far, i dont understand the way you adress the right dimensions.

Here is my current code:
Code: Select all
Eigen::Tensor<double, 3> B(3,4,3); B.setRandom();
Eigen::Tensor<double, 3> C(3,4,3); C.setRandom();

// Eigen::array<Eigen::IndexList<int,int,int>,1> idx =
//                      {Eigen::IndexList<int,int,int>(1,0,0)};
//                       also does not seem to be the way
Eigen::array<int,3> idx({0,0,1});
Eigen::Tensor<double, 4> D = B.contract(C, idx);

I would just like to contract over the last dimension of B and the first dimension of C. But i dont understand how the system works and the documentation only gives you an example for 2D-Tensors.
twithaar
Registered Member
Posts
23
Karma
0
I would just like to contract over the last dimension of B and the first dimension of C


This one should do it:

Code: Select all
Eigen::array<Eigen::IndexPair<long>, 1> cdim = {Eigen::IndexPair<long>{2, 0}};
Eigen::Tensor<double, 4> D = B.contract(C, idx);


You want to contract 2 dimensions, one from B and one from C, so a single `IndexPair` tells contract which dimensions to run over


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell