我想使用特征张量不支持的模块交换张量中的列或行。
我想实现的循环如下:
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] );
}
}
}
}
注意第二个 T 张量 j <-> l 中第二个和第四个索引之间的索引交换。
最佳答案
您可以使用收缩和洗牌操作来实现循环:
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);
关于c++ - 特征张量模块 : how to swap cols or rows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29891183/