![]() Registered Member ![]()
|
Hello,
I'm working on converting some Matlab code to Eigen. In several places in the code I have a vector of indices, and I need to get the corresponding elements from another vector. For example, if v = [10; 20; 30; 40; 50] and i = [0; 1; 4] then I need to be able to get the value of v(i) (in this case [10; 20; 50]). There doesn't seem to be any built-in functions in Eigen for doing this, so I wrote the following function:
So now I can call v_idx(x, v, i) and x will be set to [10; 20; 50]. This works fine, but it's a little cumbersome to use, because I have to pass in a result vector as one of the arguments every time. When I convert more complicated expressions from the Matlab code, I end up having to add several temporary variables, which I don't want to do if I don't have to. Is there any way to rewrite this function so that it returns a vector without introducing unnecessary temporaries? I guess it would involve using expression templates somehow, but I'm having trouble understanding that part of the documentation... Also, how would I write a function to replace the elements of v(i) with another vector? For example, if I write something like v.idx(i) = w (where w = [100; 200; 300]) then that should make v = [100; 200; 30; 40; 300]. Thanks! |
![]() Registered Member ![]()
|
I can point you to the ReturnByValue class in Eigen 3. There are quite a few examples in the code. Here is a rough sketch of what you have to do... The idea is, that you write a struct inheriting ReturnByValue. That struct implements a function evalTo(..) which is used during the assignment of that struct to another Matrix or Array. The actual implementation of your function will reside within evalTo(..). The other important thing is that you need to provide a typedef that tells the compiler in which type the object should be evaluated when it is used as a temporary - e.g. when passing it to something like std::cout. So where does the struct come from? You initial function will create and return it as a proxy object. In the end it will look similar to this
As said before, for the rest I am kindly asking you to take a look at the source code. Just come back if you have problems. Regards, Hauke |
![]() Registered Member ![]()
|
When I asked for an explanation of ReturnByValue, Benoit kindly wrote me the following example which I found very helpful: http://listengine.tuxfamily.org/lists.t ... 00216.html .
Incidentally, I remember that we had a couple of people already asking about the functionality that ccrawford mentions, so it would be very useful to have an implementation. |
![]() Registered Member ![]()
|
At the same time, in this particular example, I think if this is best implemented as a new expression class, enabling it to be completely lazy. (The downside of that, though, would be the indirect memory access and potentially poor memory locality.)
There already have been some discussions on the list about adding such an expression, a few months ago.
Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list! |
![]() Registered Member ![]()
|
Thanks Hauke and jitseniesen for your help. After a couple of days wrangling with compiler errors and segfaults, I came up with the following implementation, which seems to be working:
|
Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]