Registered Member
|
G'day all. New user of Eigen here, still learning the ropes.
I am trying to pass a segment of a row of a matrix to a function which will change the underlying data values in the actual matrix. i.e. the segment I pass around will not use a copy of the data. A simple function for testing:
Now in the main() routine I create some matrices. One a "normal" Matrix, the other formed by Mapping an existing buffer:
I can create objects which give me writable access to the underlying rows/columns as follows:
And objects giving me write access to underlying segments of rows/cols:
I can pass any of the above declared variables to my test function quite happily, and the underlying matrix values (in M1 or map2) are changed accordingly.
It is however extremely unwieldy to create individual variables only to provide temporary access to underlying data in a matrix. This is particularly so for the case of the row segments - look at the type declarations! It would be nice to be able to pass row-segments into functions anonymously, e.g.
but of course C++ insists that such temporary reference arguments be const, and I want to use non-const operations on the object within my function. So - is there a nice, easy way of selecting (not copying) regions of a matrix and passing them into a function which will change the underlying values in the original matrix? Thanks in advance for all help. |
Moderator
|
This is a common flaw of C++ and unfortunately there is no easy solution to that problem. In Eigen itself we have the same issue for the swap function:
mat.col(i).swap(mat.col(k)); for which we declared the argument using a const reference and then we const-cast it inside the function. Note that C++1x solves this issue via rvalue references : http://en.wikipedia.org/wiki/C%2B%2B0x# ... _semantics. |
Registered users: Bing [Bot], Google [Bot], Sogou [Bot]