Convert an Eigen::TensorMap to Eigen::Tensor
Asked Answered
W

1

5

It's possible to convert an Eigen::Map to a Matrix by assignment:

vector<float> v = { 1, 2, 3, 4 };
auto m_map = Eigen::Map<Eigen::Matrix<float, 2, 2, Eigen::RowMajor>>(&v[0]);
Eigen::MatrixXf m = m_map;
cout << m << endl;

This produces:

 1 2
 3 4

If I try to do something similar with a Tensor:

vector<float> v = { 1, 2, 3, 4 };
auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
Eigen::Tensor<float, 2> t = mapped_t;

I simply get the compiler error YOU_MADE_A_PROGRAMMING_MISTAKE. Is there any way to convert a TensorMap to a Tensor?

Whimsicality answered 20/6, 2017 at 22:54 Comment(0)
B
8

Well, Eigen::RowMajor is not the default for Eigen::Tensor which means you are not assigning to the same type which means YOU_MADE_A_PROGRAMMING_MISTAKE. You have to explicitly request swapping the layout.

#include <vector>
#include <unsupported/Eigen/CXX11/Tensor>

int main()
{
  std::vector<float> v = { 1, 2, 3, 4 };
  auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
  Eigen::Tensor<float, 2> t = Eigen::TensorLayoutSwapOp<Eigen::Tensor<float, 2, Eigen::RowMajor>>(mapped_t);
}

Using C++14 you could write a nice instantiator function for that.

#include <type_traits>
#include <vector>
#include <unsupported/Eigen/CXX11/Tensor>

namespace Eigen {
  template < typename T >
  decltype(auto) TensorLayoutSwap(T&& t)
  {
    return Eigen::TensorLayoutSwapOp<typename std::remove_reference<T>::type>(t);
  }
}

int main()
{
  std::vector<float> v = { 1, 2, 3, 4 };
  auto mapped_t = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&v[0], 2, 2);
  Eigen::Tensor<float, 2> t = Eigen::TensorLayoutSwap(mapped_t);
}
Brewis answered 21/6, 2017 at 0:0 Comment(1)
I am getting some error like this: static Eigen::PlainObjectBase<Derived>::MapType Eigen::PlainObjectBase<Derived>::Map(Eigen::PlainObjectBase<Derived>::Scalar*) [with Derived = Eigen::Matrix<const float, -1, -1, 0, -1, -1>; Eigen::PlainObjectBase<Derived>::MapType = Eigen::Map<Eigen::Matrix<const float, -1, -1, 0, -1, -1>, 0, Eigen::Stride<0, 0> >; Eigen::PlainObjectBase<Derived>::Scalar = const float]' cannot be overloadedCicerone

© 2022 - 2024 — McMap. All rights reserved.