Registered Member
|
Is it OK to assign matrix expression to auto variable, e.g.
Also what is the difference between Ref and Block types. They seem to serve the same purpose? Thanks. |
Moderator
|
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. |
Registered Member
|
Thanks for explaining.
I have another question. When writing non-template functions that use Ref as it arguments the following is not OK:
|
Moderator
|
It's OK only if the Ref is const, for instance: Ref<const MatrixXd>
|
Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]