This bugzilla service is closed. All entries have been migrated to
Bug 437 - [feature request] Add Reshape Operation
Summary: [feature request] Add Reshape Operation
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - general (show other bugs)
Version: unspecified
Hardware: All All
: Normal enhancement
Assignee: Nobody
Depends on:
Blocks: 3.4 712
  Show dependency treegraph
Reported: 2012-03-19 10:38 UTC by brian
Modified: 2019-12-04 11:33 UTC (History)
5 users (show)


Description brian 2012-03-19 10:38:19 UTC
I often want to have a reshaped view of an existing array or matrix. For example, view a 3xK matrix as a 3K x 1 vector to perform a linear operation on the vertices of a model. This can be achieved with the Eigen::Map object, but is rather verbose.

It would be great, if eigen had a .reshape(h,w) function which would return an Eigen::Map with a new view on the data.

Could this be implemented? Do other people also need this funcitonality? I believe, that it is quite common, when you are coming from a matlab mindset.

kind regards,

Comment 1 brian 2012-03-20 12:22:04 UTC
After working  a bit more on my problem, I see that it would really be best to get as the return a reshape expression, not an Eigen::Map, as the reshape might happen on a non-evaluated expression. How complicated is it to implement this?
Comment 2 Christoph Hertzberg 2012-03-20 13:06:27 UTC
I think basically you can implement it the same way as Block is implemented -- of course with different index calculations.
Comment 3 brian 2012-03-22 12:54:02 UTC
Thank you for the feedback. I currently have no spare time to dig into this, is anyone willing to take it up?
Comment 4 brian 2012-04-02 07:45:36 UTC
Would anyone be willing to walk me through this. The Eigen code is quite complex, but if there is someone holding my hand I think I could make the necessary adaptations without breaking anything. Christoph maybe?
Comment 5 Christoph Hertzberg 2012-04-02 12:48:00 UTC
OK, the quick hack would be to copy Eigen/src/Core/Block.h and literally search and replace all Block by Reshape, then adapt all index calculations and the constructors. Most likely you also need forward-declarations in Eigen/src/Core/util/ForwardDeclarations.h and then include the new file somewhere from Eigen/Core.

Next and final step is to implement some reshape member functions -- also similar to the way the block member functions are implemented (Eigen/src/plugin/BlockMethods.h) -- and add them to Eigen/src/Core/DenseBase.h.

This solution will most likely not break anything, so have no fear *g*.

Overall, this solution will result in a lot of code duplication, so I guess the cleaner solution would be to make the Block-class more general, maybe also fixing bug 58, by making Map essentially be a (trivial) Block.

W.r.t Eigen's code complexity, I guess it is more a problem of the poor internal documentation (see bug 138).
Comment 6 brian 2012-04-09 21:51:36 UTC
Hello Christoph,

so after looking at Block.h I feel a bit intimidated. Can you explain the purpose and way of functioning of the template specialization at the end? And will I need to change anything inside the traits block? How about the packet access, how does that work, what do I have to keep in mind while tinkling with it?

thank you,

Comment 7 Hilton Bristow 2013-12-12 06:38:46 UTC
+1. reshape() with a vec() specialization would be greatly appreciated. Perhaps a more general "View" interface which implements block(), diagonal(), diagonalize(), reshape() and vec() would be the way to go.
Comment 8 Christoph Hertzberg 2013-12-12 11:40:52 UTC
@Brian: Sorry for not responding (for very long). I may have time between the years to look into that.

@Hilton: Yes, Bug 712 will basically fall of this feature, using (size(), 1) as parameters.

Generally, I think a clean solution should return something equivalent to a Map if possible. However, things like
  MatrixXd M;
will have changing inner strides, so they could not be represented by a map -- and of course it only works for DirectAccess types.
Comment 9 yoco 2014-01-17 22:21:08 UTC
Hi all :)

I am new to the eigen project.

I have done some work about this issue. It works basically.

Except the specialization of the "direct access". I currently have no idea what that is.

Please give me any suggestion if I done anything wrong.

thanks in advance.
Comment 10 Gael Guennebaud 2018-10-08 20:33:55 UTC
Merged in PR 301:
Comment 11 Nobody 2019-12-04 11:33:14 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance:

Note You need to log in before you can comment on or make changes to this bug.