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

pointer to Nth Row needed

Tags: None
(comma "," separated)
xaffeine
Registered Member
Posts
24
Karma
0

pointer to Nth Row needed

Tue May 06, 2014 7:58 pm
I very frequently need to get a pointer to a specific row of a matrix, to pass to a low-level function. For speed reasons, I do not want to copy the row data. I have defined my matrices to be row-major so that copying should not be necessary. I expected to be able to call .row().data() without incurring any copying, but I can see from profiling that indeed a copy is made. Do I need to do my own pointer/size arithmetic? Of not, what is the nice way to do it?

Here are the relevant code fragments.

typedef Matrix< complex<float>, Dynamic, Dynamic, RowMajor > RowMajorComplexMat;

RowMajorComplexMat bigMat;

for( int band =0; band < mnBands; ++ band )
{
const RowMajorComplexMat & thisRow = bigMat.row(band); // takes time, but shouldn't

doSomethingWith( thisRow.data() );
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: pointer to Nth Row needed

Tue May 06, 2014 8:38 pm
bigMat.row(band).data() does not perform any copy. However, the returned type of .row(.) is not a Matrix<> object. It is a Block<....> expression whose precise type is given by RowMajorComplexMat::RowXpr.
xaffeine
Registered Member
Posts
24
Karma
0

Re: pointer to Nth Row needed

Tue May 06, 2014 10:54 pm
ggael wrote:bigMat.row(band).data() does not perform any copy. However, the returned type of .row(.) is not a Matrix<> object. It is a Block<....> expression whose precise type is given by RowMajorComplexMat::RowXpr.


Thank you, that is interesting to learn. So, if I write the following, should the problem go away?

for( int band =0; band < nBands; ++ band )
{
const RowMajorComplexMat::RowXpr & thisRow = bigMat.row(band);

doSomethingWith( thisRow.data() );
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: pointer to Nth Row needed  Topic is solved

Wed May 07, 2014 6:50 am
Do not use a reference:

RowMajorComplexMat::ConstRowXpr thisRow = bigMat.row(band);
xaffeine
Registered Member
Posts
24
Karma
0

Re: pointer to Nth Row needed

Wed May 07, 2014 6:15 pm
Do you ever feel like someone should write a book about Eigen?


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot], rblackwell