Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unclear return type for function taking eigen types as parameters

I'm trying to use the Eigen3 library in a scientific program but I'm struggling to make some simple functions and member functions. For example, I'm not certain what kind of return type should I choose for something like the following:

template <typename DerivedA,typename DerivedB>
inline **something** mult(const MatrixBase<DerivedA>& p1,
                          const MatrixBase<DerivedB>& p2)
  {
  return p1*p2;
  }

I assume that there is some class, say productOp, that I could return without a problem. I still could not imagine what would happen in a function involving a large number of operations, or even worse, an iteration that depends on the input:

template <typename Derived>
**something** foo(const MatrixBase<Derived>& p1))
  {
  **something** p2;
  p2.setZero();
  while(p2(0,0) < 1)
    p2 += p1; 
  return p2;
  }

My questions are:

  1. Is the second example even possible?
  2. How can I figure the type of an operation like p1*p2?
  3. How can I figure the return type in the case of elaborate functions?
like image 714
German Capuano Avatar asked Jul 28 '17 11:07

German Capuano


1 Answers

In the first example, returning auto will works well because it is a single expression that does not reference any local temporary.

In the second, you need to create and return an actual matrix/vector with its own storage, for instance:

typename Derived::PlainObject p2;
p2.resizeLike(p1);

and the return type will be typename Derived::PlainObject.

like image 79
ggael Avatar answered Nov 15 '22 05:11

ggael