Registered Member
|
Hi,
when considering the function
I sometimes want to apply this function onto data arising from third party packages. Eigen2 provides the Map classes
Is it somehow possible to do
? 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.
|
Registered Member
|
The function 'funct' needs to be rewritten like this, as a function template:
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! |
Registered Member
|
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? |
Registered Member
|
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.
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:
Or even, if you don't need to name (for reuse) the map object:
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:
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! |
Registered Member
|
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? |
Registered Member
|
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:
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! |
Registered users: Bing [Bot], Evergrowing, Google [Bot], rblackwell