c++ - 特征张量模块 : how to swap cols or rows?

标签 c++ module swap eigen

我想使用特征张量不支持的模块交换张量中的列或行。

我想实现的循环如下:

    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/

相关文章:

android - 如何在monkeyrunner测试文件中导入一个py文件函数

使用自定义交换的 C++ 排序实现?

c++ - 将参数转换为包装函数所需的类型

c++ - 最初宣布为持续困惑

javascript - 当命名导入不存在时,Webpack 静默失败

python 包导入模块使用 __init__.py

audio - 如何在 Gstreamer 中动态交换左/右音频输出

java - 通过多态交换对象的位置

c++ - 为什么 boost asio 函数 expires_from_now() 取消 deadline_timer?

c++ - 使用 boost iostream 每个连接发送多个文件