TutorialReshape.dox 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. namespace Eigen {
  2. /** \eigenManualPage TutorialReshape Reshape
  3. Since the version 3.4, %Eigen exposes convenient methods to reshape a matrix to another matrix of different sizes or vector.
  4. All cases are handled via the DenseBase::reshaped(NRowsType,NColsType) and DenseBase::reshaped() functions.
  5. Those functions do not perform in-place reshaping, but instead return a <i> view </i> on the input expression.
  6. \eigenAutoToc
  7. \section TutorialReshapeMat2Mat Reshaped 2D views
  8. The more general reshaping transformation is handled via: `reshaped(nrows,ncols)`.
  9. Here is an example reshaping a 4x4 matrix to a 2x8 one:
  10. <table class="example">
  11. <tr><th>Example:</th><th>Output:</th></tr>
  12. <tr><td>
  13. \include MatrixBase_reshaped_int_int.cpp
  14. </td>
  15. <td>
  16. \verbinclude MatrixBase_reshaped_int_int.out
  17. </td></tr></table>
  18. By default, the input coefficients are always interpreted in column-major order regardless of the storage order of the input expression.
  19. For 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.
  20. \section TutorialReshapeMat2Vec 1D linear views
  21. A very common usage of reshaping is to create a 1D linear view over a given 2D matrix or expression.
  22. In this case, sizes can be deduced and thus omitted as in the following example:
  23. <table class="example">
  24. <tr><th>Example:</th></tr>
  25. <tr><td>
  26. \include MatrixBase_reshaped_to_vector.cpp
  27. </td></tr>
  28. <tr><th>Output:</th></tr>
  29. <tr><td>
  30. \verbinclude MatrixBase_reshaped_to_vector.out
  31. </td></tr></table>
  32. This shortcut always returns a column vector and by default input coefficients are always interpreted in column-major order.
  33. Again, see the documentation of DenseBase::reshaped() for more control on the ordering.
  34. \section TutorialReshapeInPlace
  35. The above examples create reshaped views, but what about reshaping inplace a given matrix?
  36. Of course this task in only conceivable for matrix and arrays having runtime dimensions.
  37. In many cases, this can be accomplished via PlainObjectBase::resize(Index,Index):
  38. <table class="example">
  39. <tr><th>Example:</th></tr>
  40. <tr><td>
  41. \include Tutorial_reshaped_vs_resize_1.cpp
  42. </td></tr>
  43. <tr><th>Output:</th></tr>
  44. <tr><td>
  45. \verbinclude Tutorial_reshaped_vs_resize_1.out
  46. </td></tr></table>
  47. However beware that unlike \c reshaped, the result of \c resize depends on the input storage order.
  48. It thus behaves similarly to `reshaped<AutoOrder>`:
  49. <table class="example">
  50. <tr><th>Example:</th></tr>
  51. <tr><td>
  52. \include Tutorial_reshaped_vs_resize_2.cpp
  53. </td></tr>
  54. <tr><th>Output:</th></tr>
  55. <tr><td>
  56. \verbinclude Tutorial_reshaped_vs_resize_2.out
  57. </td></tr></table>
  58. Finally, assigning a reshaped matrix to itself is currently not supported and will result to undefined-behavior because of \link TopicAliasing aliasing \endlink.
  59. The following is forbidden: \code A = A.reshaped(2,8); \endcode
  60. This is OK: \code A = A.reshaped(2,8).eval(); \endcode
  61. */
  62. }