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

Pointer to a sub matrix

Tags: None
(comma "," separated)
Mawrio
Registered Member
Posts
6
Karma
0
OS

Pointer to a sub matrix

Mon Sep 21, 2009 7:48 pm
Here is a simplified version of what im trying to do :

Matrix<double,100000,3> mxA;

Block<double,100000,1> * pmxCol1 = NULL;
Block<double,100000,1> * pmxCol2 = NULL;
Block<double,100000,1> * pmxCol3 = NULL;

// Matrix<double,100000,1> * pmxCol1 = NULL;
// Matrix<double,100000,1> * pmxCol2 = NULL;
// Matrix<double,100000,1> * pmxCol3 = NULL;


pmxCol1 = mxr.block<100000,1>(0,0);
pmxCol2 = mxr.block<100000,1>(0,1);
pmxCol3 = mxr.block<100000,1>(0,2);

...
DoSomeLongOperation(pCol1,pCol2,pCol3);
...


I can't find a way to have reference (pointer) to a sub block or a sub matrix. My program need to be fast... I dont want to copy the original data.

Hopes you get my point.

thx
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Pointer to a sub matrix

Mon Sep 21, 2009 7:57 pm
If your matrix has column-major storage (which is the default with Eigen) then doing:
Code: Select all
    matrix(0,i)

returns a reference to the start of the i-th column, and therefore,
Code: Select all
    & matrix(0,i)

returns a pointer to the start of the i-th column.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Pointer to a sub matrix

Mon Sep 21, 2009 8:00 pm
ah, this is only reliable with the devel branch, sorry, as eigen 2.0 may return the coeff by value in which case you'd be taking the address of a temporary.

The more reliable approach with eigen 2.0 is:

Code: Select all
    matrix.data() + i * matrix.rows()


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Mawrio
Registered Member
Posts
6
Karma
0
OS

Re: Pointer to a sub matrix

Mon Sep 21, 2009 8:01 pm
thx but it dont work for a block :/
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Pointer to a sub matrix

Mon Sep 21, 2009 8:08 pm
ok, then do the 1st approach I said, and, if you use Eigen 2.0, cast the resulting pointer as a non-constant pointer. That will force the compiler to choose the non-constant operator(), or at least in the other case you'll have a compilation error.
Code: Select all
  static_cast<double *>(& block(0,0))


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Mawrio
Registered Member
Posts
6
Karma
0
OS

Re: Pointer to a sub matrix

Mon Sep 21, 2009 8:11 pm
The problem is if i want to do something like a SVD i would have to copy the data back in another Matrix container or use another SVD function
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Pointer to a sub matrix

Mon Sep 21, 2009 8:11 pm
oh, i'm stupid.

This is what i should have told you to do all along:

Code: Select all
 & block.coeffRef(0,0);


however, careful, it doesn't check that the indices are in range.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Mawrio
Registered Member
Posts
6
Karma
0
OS

Re: Pointer to a sub matrix

Mon Sep 21, 2009 8:29 pm
Thx for the support, thats what I though, I'll have to copy the data because I need the matrix container not only the data pointer
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Pointer to a sub matrix

Mon Sep 21, 2009 8:40 pm
I still don't really understand what you mean, but do you know that thanks to expression templates, block() does not actually return a copy the block, instead it just returns an expression?

so you can do

matrix.block(.....) = othermatrix.block(....)

and there's no redundant copy here.

hope that helps...


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Pointer to a sub matrix

Mon Sep 21, 2009 8:50 pm
... and you can also name the reference to use it multiple times, e.g:

Block<MatrixType, Dynamic, 1> col1 = mat.col(1);
col1 *= 2;
Mawrio
Registered Member
Posts
6
Karma
0
OS

Re: Pointer to a sub matrix

Mon Sep 21, 2009 9:11 pm
Just got back from work...

Yeah I thought it would return a copy thx 8)
Mawrio
Registered Member
Posts
6
Karma
0
OS

Re: Pointer to a sub matrix

Tue Sep 22, 2009 12:29 pm
Is there a way to have 2 seperate Matrix object that have the same buffer adress?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Pointer to a sub matrix

Tue Sep 22, 2009 12:52 pm
yes, you can use the Map object for that purpose, e.g.:

Map<MatrixType> m1(buffer_address, rows, cols);

then you can use m1 like any Eigen matrix object.


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]