Eigen Tensor C++, multiply each row by its transpose

PHOTO EMBED

Fri Feb 18 2022 12:26:38 GMT+0000 (Coordinated Universal Time)

Saved by @Ruben_Guerrero #cpp

#include <unsupported/Eigen/CXX11/Tensor>
int main(){
    // Define a tensor
    Eigen::Tensor<double,3> A(55,50,2);
    A.setRandom();

    // Define the pairs of indices to multiply (i.e. index 1 on A twice)
    std::array<Eigen::IndexPair<long>, 1> idx = { Eigen::IndexPair<long> {1, 1} };
    
    // Define a parallel device 
    int num_threads = 4;
    Eigen::ThreadPool       tpl (num_threads);
    Eigen::ThreadPoolDevice dev (&tpl, num_threads);

    // Initialize result buffer B. B will have dimensions [55,2,55,2]
    Eigen::Tensor<double,4> B(55,2,55,2);

    // Contract using the parallel device
    B.device(dev) = A.contract(A, idx);
}

content_copyCOPY

Tensor contraction using Eigen.

https://reddit.fun/160223/numpy-3d-array-eigen-tensor-multiply-each-row-its-transpose