[eigen] Convert Eigen::Tensor to Eigen::Matrix
• To: eigen@xxxxxxxxxxxxxxxxxxx
• Subject: [eigen] Convert Eigen::Tensor<double, 2> to Eigen::Matrix<double, -1, -1>
• From: Andreas Copan <avcopan@xxxxxxxxx>
• Date: Tue, 17 May 2016 22:11:45 -0400
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to; bh=O7W4R7K1L3Mhbzoocd4ICB+9wM9jvqXBMkvb4auz1IY=; b=FtQMX8VO/AvhZxVsd3FfQe53onGhUxIjal0lWD4M2/3WMDINCGoEgABS4JrW7jdgPQ fbky2bjFh1VSPuLxTabQQCcMyNJ9HuLQ7oYIuWSvnXnbTHHFz1uJXtSA3N6Ec+U2ZV5e JQR2wOTxwERHNfLcKxeeBCHYlx+91DNHiz5wjJoA5lJrfpoPLc6qRJG+AjXKyhmjhMAV xHIQiK5i2LnoGEpiRtBo/QYmOTyMnE0Zz6mXqA4VYL4TqN+pfJGMDKMeMUfni3IeyK+N 23K4WdZk8ieRniCedf4WLMY7O0K6OXj3Gh7TBZ9an1snhViefSeqvWaCGJOdCiQamgMQ TviA==

I am writing a code in which I need to contract a four-dimensional tensor (Eigen::Tensor<double, 4>) with a two-dimensional tensor (Eigen::Tensor<double, 2>) and then diagonalize it.  It looks like there currently isn't a .asMatrix() method for converting an Eigen::Tensor to a matrix for diagonalization, is that right?  If not, what would people recommend as the best way to do this using the functionality currently available?  Even just a simple method of filling an Eigen::Matrix with values from an Eigen::Tensor would help me out.  Here is a minimal example of what I would like to do.

#include <iostream>
#include <array>
#include <Eigen/Dense>                    // Eigen::Matrix
#include <unsupported/Eigen/CXX11/Tensor> // Eigen::Tensor

using IndexPair = Eigen::IndexPair<int>;
template<int naxes>
using Contraction = std::array<IndexPair, naxes>;

int main() {
Eigen::Tensor<double, 4> g(5, 5, 5, 5);
Eigen::Tensor<double, 2> D(5, 5);
Eigen::Tensor<double, 2> f(5, 5);
g.setRandom();
D.setRandom();
Contraction<2> ctr({IndexPair(1, 1), IndexPair(3, 0)});
f = g.contract(D, ctr); // f_ij = sum_kl g_ikjl * D_lk

/* this is what I would like to avoid: */
Eigen::Matrix<double, -1, -1> F(5, 5);
for(int i=0; i<5; ++i)
for(int j=0; j<5; ++j)
F(i,j) = f(i,j);