You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.6 KiB

  1. namespace StormEigen {
  2. /** \eigenManualPage TutorialBlockOperations Block operations
  3. This page explains the essentials of block operations.
  4. A block is a rectangular part of a matrix or array. Blocks expressions can be used both
  5. as rvalues and as lvalues. As usual with Eigen expressions, this abstraction has zero runtime cost
  6. provided that you let your compiler optimize.
  7. \eigenAutoToc
  8. \section TutorialBlockOperationsUsing Using block operations
  9. The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
  10. There are two versions, whose syntax is as follows:
  11. <table class="manual">
  12. <tr><th>\b %Block \b operation</td>
  13. <th>Version constructing a \n dynamic-size block expression</th>
  14. <th>Version constructing a \n fixed-size block expression</th></tr>
  15. <tr><td>%Block of size <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td>
  16. <td>\code
  17. matrix.block(i,j,p,q);\endcode </td>
  18. <td>\code
  19. matrix.block<p,q>(i,j);\endcode </td>
  20. </tr>
  21. </table>
  22. As always in Eigen, indices start at 0.
  23. Both versions can be used on fixed-size and dynamic-size matrices and arrays.
  24. These two expressions are semantically equivalent.
  25. The only difference is that the fixed-size version will typically give you faster code if the block size is small,
  26. but requires this size to be known at compile time.
  27. The following program uses the dynamic-size and fixed-size versions to print the values of several blocks inside a
  28. matrix.
  29. <table class="example">
  30. <tr><th>Example:</th><th>Output:</th></tr>
  31. <tr><td>
  32. \include Tutorial_BlockOperations_print_block.cpp
  33. </td>
  34. <td>
  35. \verbinclude Tutorial_BlockOperations_print_block.out
  36. </td></tr></table>
  37. In the above example the \link DenseBase::block() .block() \endlink function was employed as a \em rvalue, i.e.
  38. it was only read from. However, blocks can also be used as \em lvalues, meaning that you can assign to a block.
  39. This is illustrated in the following example. This example also demonstrates blocks in arrays, which works exactly like the above-demonstrated blocks in matrices.
  40. <table class="example">
  41. <tr><th>Example:</th><th>Output:</th></tr>
  42. <tr><td>
  43. \include Tutorial_BlockOperations_block_assignment.cpp
  44. </td>
  45. <td>
  46. \verbinclude Tutorial_BlockOperations_block_assignment.out
  47. </td></tr></table>
  48. While the \link DenseBase::block() .block() \endlink method can be used for any block operation, there are
  49. other methods for special cases, providing more specialized API and/or better performance. On the topic of performance, all what
  50. matters is that you give Eigen as much information as possible at compile time. For example, if your block is a single whole column in a matrix,
  51. using the specialized \link DenseBase::col() .col() \endlink function described below lets Eigen know that, which can give it optimization opportunities.
  52. The rest of this page describes these specialized methods.
  53. \section TutorialBlockOperationsSyntaxColumnRows Columns and rows
  54. Individual columns and rows are special cases of blocks. Eigen provides methods to easily address them:
  55. \link DenseBase::col() .col() \endlink and \link DenseBase::row() .row()\endlink.
  56. <table class="manual">
  57. <tr><th>%Block operation</th>
  58. <th>Method</th>
  59. <tr><td>i<sup>th</sup> row
  60. \link DenseBase::row() * \endlink</td>
  61. <td>\code
  62. matrix.row(i);\endcode </td>
  63. </tr>
  64. <tr><td>j<sup>th</sup> column
  65. \link DenseBase::col() * \endlink</td>
  66. <td>\code
  67. matrix.col(j);\endcode </td>
  68. </tr>
  69. </table>
  70. The argument for \p col() and \p row() is the index of the column or row to be accessed. As always in Eigen, indices start at 0.
  71. <table class="example">
  72. <tr><th>Example:</th><th>Output:</th></tr>
  73. <tr><td>
  74. \include Tutorial_BlockOperations_colrow.cpp
  75. </td>
  76. <td>
  77. \verbinclude Tutorial_BlockOperations_colrow.out
  78. </td></tr></table>
  79. That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression.
  80. \section TutorialBlockOperationsSyntaxCorners Corner-related operations
  81. Eigen also provides special methods for blocks that are flushed against one of the corners or sides of a
  82. matrix or array. For instance, \link DenseBase::topLeftCorner() .topLeftCorner() \endlink can be used to refer
  83. to a block in the top-left corner of a matrix.
  84. The different possibilities are summarized in the following table:
  85. <table class="manual">
  86. <tr><th>%Block \b operation</td>
  87. <th>Version constructing a \n dynamic-size block expression</th>
  88. <th>Version constructing a \n fixed-size block expression</th></tr>
  89. <tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
  90. <td>\code
  91. matrix.topLeftCorner(p,q);\endcode </td>
  92. <td>\code
  93. matrix.topLeftCorner<p,q>();\endcode </td>
  94. </tr>
  95. <tr><td>Bottom-left p by q block
  96. \link DenseBase::bottomLeftCorner() * \endlink</td>
  97. <td>\code
  98. matrix.bottomLeftCorner(p,q);\endcode </td>
  99. <td>\code
  100. matrix.bottomLeftCorner<p,q>();\endcode </td>
  101. </tr>
  102. <tr><td>Top-right p by q block
  103. \link DenseBase::topRightCorner() * \endlink</td>
  104. <td>\code
  105. matrix.topRightCorner(p,q);\endcode </td>
  106. <td>\code
  107. matrix.topRightCorner<p,q>();\endcode </td>
  108. </tr>
  109. <tr><td>Bottom-right p by q block
  110. \link DenseBase::bottomRightCorner() * \endlink</td>
  111. <td>\code
  112. matrix.bottomRightCorner(p,q);\endcode </td>
  113. <td>\code
  114. matrix.bottomRightCorner<p,q>();\endcode </td>
  115. </tr>
  116. <tr><td>%Block containing the first q rows
  117. \link DenseBase::topRows() * \endlink</td>
  118. <td>\code
  119. matrix.topRows(q);\endcode </td>
  120. <td>\code
  121. matrix.topRows<q>();\endcode </td>
  122. </tr>
  123. <tr><td>%Block containing the last q rows
  124. \link DenseBase::bottomRows() * \endlink</td>
  125. <td>\code
  126. matrix.bottomRows(q);\endcode </td>
  127. <td>\code
  128. matrix.bottomRows<q>();\endcode </td>
  129. </tr>
  130. <tr><td>%Block containing the first p columns
  131. \link DenseBase::leftCols() * \endlink</td>
  132. <td>\code
  133. matrix.leftCols(p);\endcode </td>
  134. <td>\code
  135. matrix.leftCols<p>();\endcode </td>
  136. </tr>
  137. <tr><td>%Block containing the last q columns
  138. \link DenseBase::rightCols() * \endlink</td>
  139. <td>\code
  140. matrix.rightCols(q);\endcode </td>
  141. <td>\code
  142. matrix.rightCols<q>();\endcode </td>
  143. </tr>
  144. </table>
  145. Here is a simple example illustrating the use of the operations presented above:
  146. <table class="example">
  147. <tr><th>Example:</th><th>Output:</th></tr>
  148. <tr><td>
  149. \include Tutorial_BlockOperations_corner.cpp
  150. </td>
  151. <td>
  152. \verbinclude Tutorial_BlockOperations_corner.out
  153. </td></tr></table>
  154. \section TutorialBlockOperationsSyntaxVectors Block operations for vectors
  155. Eigen also provides a set of block operations designed specifically for the special case of vectors and one-dimensional arrays:
  156. <table class="manual">
  157. <tr><th> %Block operation</th>
  158. <th>Version constructing a \n dynamic-size block expression</th>
  159. <th>Version constructing a \n fixed-size block expression</th></tr>
  160. <tr><td>%Block containing the first \p n elements
  161. \link DenseBase::head() * \endlink</td>
  162. <td>\code
  163. vector.head(n);\endcode </td>
  164. <td>\code
  165. vector.head<n>();\endcode </td>
  166. </tr>
  167. <tr><td>%Block containing the last \p n elements
  168. \link DenseBase::tail() * \endlink</td>
  169. <td>\code
  170. vector.tail(n);\endcode </td>
  171. <td>\code
  172. vector.tail<n>();\endcode </td>
  173. </tr>
  174. <tr><td>%Block containing \p n elements, starting at position \p i
  175. \link DenseBase::segment() * \endlink</td>
  176. <td>\code
  177. vector.segment(i,n);\endcode </td>
  178. <td>\code
  179. vector.segment<n>(i);\endcode </td>
  180. </tr>
  181. </table>
  182. An example is presented below:
  183. <table class="example">
  184. <tr><th>Example:</th><th>Output:</th></tr>
  185. <tr><td>
  186. \include Tutorial_BlockOperations_vector.cpp
  187. </td>
  188. <td>
  189. \verbinclude Tutorial_BlockOperations_vector.out
  190. </td></tr></table>
  191. */
  192. }