Re: [eigen] Convert Eigen::Tensor to Eigen::Matrix
• To: eigen@xxxxxxxxxxxxxxxxxxx
• Subject: Re: [eigen] Convert Eigen::Tensor<double, 2> to Eigen::Matrix<double, -1, -1>
• From: Benoit Steiner <bsteiner@xxxxxxxxxx>
• Date: Tue, 17 May 2016 19:18:07 -0700
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=xJAbVVr59u3C3S9+udv/wQb+PG2h+PQ24vnq0JK1QfY=; b=gKWDo6r6QBCPGrjmOLb5SA1r13+e4i4GQqt2+koaD/3woJJ6qlsfosG/zdRVjHY0J+ t6AGOeeOBH/JQwuFbF+XorVBvQcCeNT5V+JGw9bdKlbVxECLR5e2lpEiBo9WohCmADh1 QDOIZk+oGDk8lu81nv/tcwzLzr0HT7Va6Hv+iDjkSYIQNKBY7s2cTNYb9w8CnzQcvi+X dGs0mL5YPy9GoGd+WKn/V8vp9BbyNwDNO4HdQkL0ZQtGkpjeD4EYyu/izLf2lVyzYgt7 BGXBlIttHyJ43C7tI5Ji3KJBfItWHlWzJkUteDTnx9IQfvHG2rPyxObQrksCEAMnPoBd 59eA==

You can use a matrix map to wrap the coefficients of the output tensor and use them in the solver:

f = g.contract(D, ctr);
Eigen::Map<Eigen::MatrixXd> F(f.data(), 5, 5);
Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double, -1, -1> > eigensolver(F);
...

On Tue, May 17, 2016 at 7:11 PM, Andreas Copan wrote:
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);

Eigen::SelfAdjointEigenSolver<Eigen::Matrix<double, -1, -1> > eigensolver(F);
Eigen::Matrix<double, -1, -1> eigenvectors = eigensolver.eigenvectors();
Eigen::Matrix<double, -1,  1> eigenvalues  = eigensolver.eigenvalues();
std::cout << eigenvectors << std::endl;
std::cout << eigenvalues  << std::endl;
}

 Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/