0

I have created a function to display the shape of a Tensor on eigen.

template<typename Scalar_, int rank>
void shape(const Eigen::Tensor<Scalar_, rank>& x)
{
  cout << "( ";  
  for (int i(0); i<x.NumDimensions; i++){
      cout << x.dimensions()[i];
      cout << ",";

  }
  cout << ")";  
}

To use it I have to do :

Tensor <double,2> t (2,2)
shape(t)

I would like to integrate it directly into the tensor class so that we can call it like this:

Tensor <double,2> t (2,2)
t.shape

How to extend Eigen's Tensor class to add functions?

  • why? non-member non-friend function are better choice anyway https://www.drdobbs.com/cpp/how-non-member-functions-improve-encapsu/184401197 – Alessandro Teruzzi May 14 '21 at 10:32
  • Because I thought it was better to structure the code, if I don't make member functions, I will have a Tensor.h file that contains all the functions to integrate with Tensor. In terms of code structure it's not disturbing ? – Nicolas Brugie May 14 '21 at 10:56
  • nope, the article is going to the details on way non-member non-friend functions (like your shape one) are better. They increase encapsulation, it is a good reading. If you have a copy of "effective c++" it is expanded. – Alessandro Teruzzi May 14 '21 at 11:52

0 Answers0