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

C++ 11 auto and matrix expressions

Tags: None
(comma "," separated)
kde-renorm
Registered Member
Posts
9
Karma
0

C++ 11 auto and matrix expressions

Tue Jan 06, 2015 9:49 am
Is it OK to assign matrix expression to auto variable, e.g.

Code: Select all
auto x = A.middleRows(...);


Also what is the difference between Ref and Block types. They seem to serve the same purpose?

Thanks.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Be very careful with Eigen and the auto keyword. In your case, x will be a Block<> object referencing the matrix A, not a copy of of a sub part of A. In this case, this might be ok, but sometimes this can be very dangerous, e.g.:

auto x = (A * y) + z;

in which case x will reference a dead object representing the result of A*y.

Block and Ref are sometimes indeed similar, which is why they share the same MapBase base class, but there are fundamental differences though:
- A Ref<> object always reference a block of an exiting dense matrix.
- A Block<> object is an abstract representation of a sub expression, e.g., it is the return type of (A+B).block(...) whereas assigning '(A+B).block(...)' to a Ref<const MatrixXd> object will allocate memory and evaluate the sub-expression.
kde-renorm
Registered Member
Posts
9
Karma
0
Thanks for explaining.
I have another question.

When writing non-template functions that use Ref as it arguments the following is not OK:

Code: Select all
foo(Ref<...>);
foo(A+B);
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
It's OK only if the Ref is const, for instance: Ref<const MatrixXd>


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]