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

[SOLVED] Question on Map and Vector compatibility

Tags: None
(comma "," separated)
Seb
Registered Member
Posts
99
Karma
0
Hi,

when considering the function
Code: Select all
void funct(Vector3d & vec);

I sometimes want to apply this function onto data arising from third party packages. Eigen2 provides the Map classes
Code: Select all
Map map_from_buf(data_pointer);

Is it somehow possible to do
Code: Select all
void funct( Map (data_pointer) );

?

When doing so I get a compile error. Off course, I could create an instance of Vector3d and copy the data from the data buffer, but this is to inefficient for me.

Thanks.

Last edited by bjacob on Fri Jan 09, 2009 8:03 pm, edited 1 time in total.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
The function 'funct' needs to be rewritten like this, as a function template:

Code: Select all
template
void funct(MatrixBase& vec);


Now Derived could be any type but you can get a lot of information on it by using these enums:
Derived::RowsAtCompileTime
Derived::ColsAtCompileTime
Derived::SizeAtCompileTime

And this typedef:
typename Derived::Scalar (the type of the coefficients)

For example if you want to restrict your function to vectors of fixed size 3 ( so either 1 col and 3 rows, or 3 cols and 1 row) you can do inside the body this function:
EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Derived,3)
(see the definition of that in Eigen/src/Core/util/StaticAssert.h)

or if you want to disallow row-vectors (1 row, 3 cols) and only allow column vectors like Vector3d,
EIGEN_STATIC_ASSERT_MATRIX_SPECIFIC_SIZE(Derived,3,1)

etc... not that i'm not yet 100% sure that these macros are going to be part of the API stability guarantee but worst case you can look up their definition in the above mentioned file.

Last edited by bjacob on Fri Jan 09, 2009 10:47 am, edited 1 time in total.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Seb
Registered Member
Posts
99
Karma
0
Thanks for this hint. To my understanding I can use the mechanism to refer any Eigen object which is based on MatrixBase to my function.

Still, I do not know how I can transform my c-array into MatrixBase. Is there a way where I create a Matrix object that works as a kind of reference, that uses the given c-array for its storage layout?
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Seb wrote:Thanks for this hint. To my understanding I can use the mechanism to refer any Eigen object which is based on MatrixBase to my function.


Yes, such a templated function will take any Eigen type deriving from MatrixBase. If you look at Eigen's source code you'll see that every ExpressionType inherits MatrixBase.

Still, I do not know how I can transform my c-array into MatrixBase. Is there a way where I create a Matrix object that works as a kind of reference, that uses the given c-array for its storage layout?


What you're asking for is exactly Map. It is an expression type, that is, it inherits MatrixBase, so you can pass it to your function. Something like this:

Code: Select all
double *my_array;
....
Eigen::Map my_map(my_array, rows, cols);
funct(my_map);
[hr]
Or even, if you don't need to name (for reuse) the map object:

Code: Select all
double *my_array;
...
funct( Eigen::MatrixXd::Map(my_array, rows, cols) );


Notice that if my_array is 16-byte-aligned then you can tell that to Eigen and that'll help it greatly achieving better vectorization:

Code: Select all
double *my_array;  // must be a multiple of 16
...
funct( Eigen::MatrixXd::MapAligned(my_array, rows, cols) );

Last edited by bjacob on Fri Jan 09, 2009 4:53 pm, edited 1 time in total.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
Seb
Registered Member
Posts
99
Karma
0
Thanks! That seems to work!

Still I have a question on efficiency using this syntax.

In the original implementation, I made use of known object size such that the compiler could optimize all operations. Do you know if I lose some efficiency when using MatrixBase?
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Seb wrote:In the original implementation, I made use of known object size such that the compiler could optimize all operations. Do you know if I lose some efficiency when using MatrixBase?


Ah, very good point! If your vectors have fixed size (like Vector3d) then indeed you want to tell that to Eigen::Map so Eigen can take advantage of that! Do like this:

Code: Select all
funct (Eigen::Vector3d::Map(my_array));


Since Vector3d has fixed size 3, you can call on it the Map(ptr) method that doesn't take extra size/rows/cols parameters!


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell