Registered Member
|
Hi there,
I am just on the course of writing a matrix-base plugin to replace our hand-crafted library and stumbled over the following issue. It is a condensed example illustrating my problem. Basically the last call does for some reason not work under VS.net (Version 9.0.30729.1 SP, Eigen SVN Rev. 947494). Actually, it gets stuck in an endless loop. Maybe the return-type of getRow(...) is wrong? I tried a few but was not able to find the correct one.... Any help would be appreciated. Regards, Hauke
|
Moderator
|
hi,
the problem is not an infinite loop but the fact you return a temporary object by reference (this is because Transpose store its argument by reference). This could be solved by using a NestByValue wrapper, but then you would have two copy of the data. So, first of all, do you really need such a getRow function ? You can achieve the same, and without any temporary, via:
Now if you really want to copy your row into a temporary (to enforce sequential and aligned storage) you can add .eval():
and finally, if you really need an extra getRow function, then, for efficiency purpose, I suggest to have the following 3 overloads:
|
Registered Member
|
Hi again,
first, thanks for your quick answer.
Just to get it right - i.e. in my plugin-code
the first part before the transpose would create a temporary and transpose would return an object containing this temporary by reference? I assumed the operations (block extraction and transposition) would be nested - containing a reference to *this - my fault.
Since I am trying to replace our hand-crafted matrix and vector libraries, I need to stick to the interface and thus I am extremely happy about your EIGEN_MATRIXBASE_PLUGIN define....
Right, but I cannot assign [font=Courier]m.row(_r_)[/font] to a mx1 vector - unfortunately all our vectors used to be column-vectors and we need to stick to it and thus I need the transposition. Well, alternatively I could write a constructor that allows to assign column-vectors and row-vectors. Finally for the corresponding getCol, do I need return type specializations as well, or is the following fine? Btw, this is the actual code injected into my MatrixBase...
- Hauke |
Moderator
|
no no, Eigen always returns expressions. So when you write:
no temporary is created. You can even do:
The problem of your initial getRow() function is that you explicitly created temporary objects via the Matrix class. Also, note that you can assign a row vector to column vector without explicit transposition. What is forbidden, however, is, for instance, to add a column vector to a row vector. So, if you want getRow returns a column vector without creating a temporary, you should return something like:
and the body would look like:
The nest-by-value thing is needed because the Block object have to be stored by value by the Transpose object. |
Registered Member
|
Thank you ggael, the nestByValue() did the trick.
I introduced the bug with the temporary when I created the stripped code for the initial post - in the end I was actually looking for nestByValue(). Great - thanks again. - Hauke |
Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell