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

Getting something like a row-iterator

Tags: None
(comma "," separated)
deng
Registered Member
Posts
10
Karma
0
I've just started using Eigen and stumbled upon another little problem, which I guess must have an easy solution but I'm probably not yet thinking in the "Eigen way-of-things". :-)

Say I have to matrices A and B, and I'm looping through both like this:

Code: Select all
for(i=0;i<A.rows();++i) {
   for(j=0;j<B.rows();++j) {
       // Doing some calculations dependent on (A.row(i) - B.row(j)), but only if 'j' wasn't used in a previous iteration.
   }
}


It's obvious that calling A.row(i) in every inner iteration is unnecessary, so you could either use a temporary which is created in the outer loop, or you'd use a pointer or something like a row-iterator. I've read through the forum and already saw that you don't have iterators, so I'm wondering how I would solve this.

I know there is rowwise(), and I can probably extend that somehow with a functor, but I guess there must be something easier? The only solution I could come up with was using 'Map' like this:

Code: Select all
 Map<RowVectorXd,0,Stride<0,Dynamic> >(const_cast<double*>(A.row(i).data()),A.cols(),Stride<0,Dynamic>(0,A.rows()))


but this looks like overkill to me.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Note that .row()/.col() return a lightweight view on a row or column, and so it has zero overhead. You can also name a row:

YourMatrixType::RowXpr row_of_A(A.row(i));

that is equivalent but more elegant than your Map<> trick.

If you really want/need an iterator, there is a start here:

http://eigen.tuxfamily.org/bz/show_bug.cgi?id=231
deng
Registered Member
Posts
10
Karma
0
Thanks Gael. Row- and ColXpr were exactly what I was looking for. You're also right of course that .col(i) does not induce any overhead. In fact, I saw some improvements when introducing a temporary, but it seems this was only because of some serious slowdown(!) when using SSE2. I will open a new thread for this soon with some testcode to demonstrate this.


Bookmarks



Who is online

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