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:
p1*p2
?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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With