This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Questions about returning Eigen types from functions

Tags: None
(comma "," separated)
kp0987
Registered Member
Posts
21
Karma
0
I have read http://eigen.tuxfamily.org/dox-devel/To ... Types.html and know that the question of ways to return Eigen types from functions is asked frequently, but I still have a few questions.

1) Using the cov example, is it OK/efficient to use:
Code: Select all
void cov(const MatrixXf& x, const MatrixXf& y, MatrixXf& C)

If C in the calling code is already of the correct size, not part of a block, and not an expression? ie a simple method. If so then this can still be a useful approach without using the DenseBase versions.

2) If I have a function defined using:
Code: Select all
template <typename Derived, typename OtherDerived>
void cov(const DenseBase<Derived>& x, const DenseBase<Derived>& y, DenseBase<OtherDerived> EIGEN_REF_TO_TEMPORARY C)

where x & y are say Array<complex<double>,Dynamic,2> and within that function I want to call another similarly defined function that returns Real eg: Something like
Code: Select all
DenseBase<Derived> D;
MyOtherFunction(x,y,D);
C = C * D;


What is the correct syntax to do this? Is casting still available? I realize that I do not know how to do this even in a simple case of the same type. Sorry if this is not completely clear.

Thanks for your help.
Hauke
Registered Member
Posts
109
Karma
3
OS
kp0987 wrote:1) Using the cov example, is it OK/efficient to use:
Code: Select all
void cov(const MatrixXf& x, const MatrixXf& y, MatrixXf& C)

If C in the calling code is already of the correct size, not part of a block, and not an expression? ie a simple method. If so then this can still be a useful approach without using the DenseBase versions.


The method is not as efficient as the pure template version when you want to pass expressions as opposed to MatrixXf. The biggest misconception is to assume that any operation in Eigen is returning a plain matrix object. So even
Code: Select all
cov(x.transpose(),y,C);

would already be inefficient because x were creating an unnecessary temporary.

kp0987 wrote:2) If I have a function defined using:
Code: Select all
template <typename Derived, typename OtherDerived>
void cov(const DenseBase<Derived>& x, const DenseBase<Derived>& y, DenseBase<OtherDerived> EIGEN_REF_TO_TEMPORARY C)

where x & y are say Array<complex<double>,Dynamic,2> and within that function I want to call another similarly defined function that returns Real eg: Something like
Code: Select all
DenseBase<Derived> D;
MyOtherFunction(x,y,D);
C = C * D;


What is the correct syntax to do this? Is casting still available? I realize that I do not know how to do this even in a simple case of the same type.


IIRC, the correct thing to do is
Code: Select all
typename Derived::PlainObject D;


PlainObject is here used to get access to the underlying Array type since Derived could itself be an expression. Now, you are back to normal. In your example this would be equivalent to
Code: Select all
Array<complex<double>,Dynamic,2> D;


HTH,
Hauke


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient