Registered Member
|
Hi there,
I'm having some trouble using Eigen::Block as data member in a class, I think this is not a standard way of using it but I'd like to know if there is a way round using Block. Any help would be appreciated. The context is: I have a class with a data member m_block of type Eigen::Block. I want this member to map to different parts of a larger internal Eigen matrix. When I want m_block to map a different part of the internal matrix I use placement new to have m_block referring to the new block. I have used this same pattern with Map instead of Block and it works well. However with Block I can get it to build but it doesn't seem to work. I also would need this m_block to be able to map external memory, I tried to do that by using a Map of the external memory and passing this to the Block constructor instead of a matrix, this doesn't build. I use Block as data member instead of Map because I think it will calculate automatically the necessary strides when I map a block within the internal matrix. I add some example code illustrating the problem: Block<MatrixXd> m_block(MatrixXd(),0,0,0,0); MatrixXd m_internalData = MatrixXd::Random(300, 2); //This builds but when I debug it m_block has a size of 300x2 not 30x2 as I would expect. new (&m_block) Block<MatrixXd>(m_internalData, 10, 0, 30, 2); //This one doesn't even build Map<MatrixXd> k = Map<MatrixXd>(externalData, 30, 2); new (&m_block) Block<MatrixXd>(k, 0, 0, 30, 2); Thanks Martin |
Moderator
|
you should use the last version and declare a block of a Map: Block<Map<MatrixXd> >
|
Registered Member
|
Thanks ggael, that works in the example code I sent.
However I have one question and still one problem. The question is: Why when I don't use a block of a Map I get wrong results when I debug it??? That is: Block<MatrixXd> m_block(MatrixXd(),0,0,0,0); MatrixXd m_internalData = MatrixXd::Random(300, 2); //This builds but when I debug it m_block has a size of 300x2 not 30x2 as I would expect. new (&m_block) Block<MatrixXd>(m_internalData, 10, 0, 30, 2); And then if I use Block<Map<MatrixXd>> m_block(Map<MatrixXd>(0,0,0),0,0,0,0); MatrixXd m_internalData = MatrixXd::Random(300, 2); new (&m_block) Block<Map<MatrixXd>>(Map<MatrixXd>(m_internalData.data(), 300, 2), 10, 0, 30, 2); it works well, that is m_block has a size of 30x2 and is the right section of m_internalData is this how is should be done? I would have never thought of using a Block of a Map unless I had to map to external memory. The problem is: that when I declare my member variable m_block as Block<Map<MatrixXd>> m_block; and initialize it in the constructors initialization list as :m_block(Map<MatrixXd>(0,0,0),0,0,0,0) I get a compile error MapBase.h(222): error C2338: YOU_ARE_TRYING_TO_USE_AN_INDEX_BASED_ACCESSOR_ON_AN_EXPRESSION_THAT_DOES_NOT_SUPPORT_THAT MapBase.h(221) : while compiling class template member function 'double &Eigen::MapBase<Derived>::coeffRef(__w64 int)' with [ Derived=Eigen::Block<Eigen::Map<Eigen::MatrixXd>> ] Block.h(258) : see reference to class template instantiation 'Eigen::MapBase<Derived>' being compiled with [ Derived=Eigen::Block<Eigen::Map<Eigen::MatrixXd>> ] MyClass.h(85) : see reference to class template instantiation 'Eigen::Block<XprType>' being compiled with [ XprType=Eigen::Map<Eigen::MatrixXd> ] I don't know how to solve this error. thanks Martin. |
Moderator
|
this all looks very strange! Do you have a self contained example that I could try myself?
|
Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]