1*bf2c3715SXin Linamespace Eigen { 2*bf2c3715SXin Li 3*bf2c3715SXin Li/** \eigenManualPage TutorialReshape Reshape 4*bf2c3715SXin Li 5*bf2c3715SXin LiSince the version 3.4, %Eigen exposes convenient methods to reshape a matrix to another matrix of different sizes or vector. 6*bf2c3715SXin LiAll cases are handled via the DenseBase::reshaped(NRowsType,NColsType) and DenseBase::reshaped() functions. 7*bf2c3715SXin LiThose functions do not perform in-place reshaping, but instead return a <i> view </i> on the input expression. 8*bf2c3715SXin Li 9*bf2c3715SXin Li\eigenAutoToc 10*bf2c3715SXin Li 11*bf2c3715SXin Li\section TutorialReshapeMat2Mat Reshaped 2D views 12*bf2c3715SXin Li 13*bf2c3715SXin LiThe more general reshaping transformation is handled via: `reshaped(nrows,ncols)`. 14*bf2c3715SXin LiHere is an example reshaping a 4x4 matrix to a 2x8 one: 15*bf2c3715SXin Li 16*bf2c3715SXin Li<table class="example"> 17*bf2c3715SXin Li<tr><th>Example:</th><th>Output:</th></tr> 18*bf2c3715SXin Li<tr><td> 19*bf2c3715SXin Li\include MatrixBase_reshaped_int_int.cpp 20*bf2c3715SXin Li</td> 21*bf2c3715SXin Li<td> 22*bf2c3715SXin Li\verbinclude MatrixBase_reshaped_int_int.out 23*bf2c3715SXin Li</td></tr></table> 24*bf2c3715SXin Li 25*bf2c3715SXin LiBy default, the input coefficients are always interpreted in column-major order regardless of the storage order of the input expression. 26*bf2c3715SXin LiFor more control on ordering, compile-time sizes, and automatic size deduction, please see de documentation of DenseBase::reshaped(NRowsType,NColsType) that contains all the details with many examples. 27*bf2c3715SXin Li 28*bf2c3715SXin Li 29*bf2c3715SXin Li\section TutorialReshapeMat2Vec 1D linear views 30*bf2c3715SXin Li 31*bf2c3715SXin LiA very common usage of reshaping is to create a 1D linear view over a given 2D matrix or expression. 32*bf2c3715SXin LiIn this case, sizes can be deduced and thus omitted as in the following example: 33*bf2c3715SXin Li 34*bf2c3715SXin Li<table class="example"> 35*bf2c3715SXin Li<tr><th>Example:</th></tr> 36*bf2c3715SXin Li<tr><td> 37*bf2c3715SXin Li\include MatrixBase_reshaped_to_vector.cpp 38*bf2c3715SXin Li</td></tr> 39*bf2c3715SXin Li<tr><th>Output:</th></tr> 40*bf2c3715SXin Li<tr><td> 41*bf2c3715SXin Li\verbinclude MatrixBase_reshaped_to_vector.out 42*bf2c3715SXin Li</td></tr></table> 43*bf2c3715SXin Li 44*bf2c3715SXin LiThis shortcut always returns a column vector and by default input coefficients are always interpreted in column-major order. 45*bf2c3715SXin LiAgain, see the documentation of DenseBase::reshaped() for more control on the ordering. 46*bf2c3715SXin Li 47*bf2c3715SXin Li\section TutorialReshapeInPlace 48*bf2c3715SXin Li 49*bf2c3715SXin LiThe above examples create reshaped views, but what about reshaping inplace a given matrix? 50*bf2c3715SXin LiOf course this task in only conceivable for matrix and arrays having runtime dimensions. 51*bf2c3715SXin LiIn many cases, this can be accomplished via PlainObjectBase::resize(Index,Index): 52*bf2c3715SXin Li 53*bf2c3715SXin Li<table class="example"> 54*bf2c3715SXin Li<tr><th>Example:</th></tr> 55*bf2c3715SXin Li<tr><td> 56*bf2c3715SXin Li\include Tutorial_reshaped_vs_resize_1.cpp 57*bf2c3715SXin Li</td></tr> 58*bf2c3715SXin Li<tr><th>Output:</th></tr> 59*bf2c3715SXin Li<tr><td> 60*bf2c3715SXin Li\verbinclude Tutorial_reshaped_vs_resize_1.out 61*bf2c3715SXin Li</td></tr></table> 62*bf2c3715SXin Li 63*bf2c3715SXin LiHowever beware that unlike \c reshaped, the result of \c resize depends on the input storage order. 64*bf2c3715SXin LiIt thus behaves similarly to `reshaped<AutoOrder>`: 65*bf2c3715SXin Li 66*bf2c3715SXin Li<table class="example"> 67*bf2c3715SXin Li<tr><th>Example:</th></tr> 68*bf2c3715SXin Li<tr><td> 69*bf2c3715SXin Li\include Tutorial_reshaped_vs_resize_2.cpp 70*bf2c3715SXin Li</td></tr> 71*bf2c3715SXin Li<tr><th>Output:</th></tr> 72*bf2c3715SXin Li<tr><td> 73*bf2c3715SXin Li\verbinclude Tutorial_reshaped_vs_resize_2.out 74*bf2c3715SXin Li</td></tr></table> 75*bf2c3715SXin Li 76*bf2c3715SXin LiFinally, assigning a reshaped matrix to itself is currently not supported and will result to undefined-behavior because of \link TopicAliasing aliasing \endlink. 77*bf2c3715SXin LiThe following is forbidden: \code A = A.reshaped(2,8); \endcode 78*bf2c3715SXin LiThis is OK: \code A = A.reshaped(2,8).eval(); \endcode 79*bf2c3715SXin Li 80*bf2c3715SXin Li*/ 81*bf2c3715SXin Li 82*bf2c3715SXin Li} 83