Belzebu
Belzebu

Reputation: 121

Eigen Tensor module: how to swap cols or rows?

I would like to swap columns or rows in a Tensor using Eigen Tensor unsupported module.

The loop that I would like to implement is as follows:

    for( int i = 0; i < N; i++){
        for( int j = 0; j < N; j++){
            G[i][j] = 0.0e0;
            for( int k = 0; k < N; k++){
                for( int l = 0; l < N; l++){

                G[i,j] += P[k,l] * ( T[i][j][k][l] - 0.5e0*T[i][l][k][j] );

                }
            }
        }
    }

Note the swap of indices between the second and fourth indices in the second T tensor, j <-> l.

Upvotes: 2

Views: 1065

Answers (1)

Benoit Steiner
Benoit Steiner

Reputation: 1469

You can use the contraction and shuffling operation to implement your loops:

Eigen::array<int, 4> shuffling;
shuffling[0] = 0;
shuffling[1] = 3;
shuffling[2] = 2;
shuffling[3] = 1;

Eigen::array<,2> contract_dims;
contract_dims[0] = Eigen::IndexPair<DenseIndex>(0, 2);
contract_dims[1] = Eigen::IndexPair<DenseIndex>(1, 3);

G = P.contract(T - T.shuffle(shuffling) * 0.5, contract_dims);

Upvotes: 2

Related Questions