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.

239 lines
8.0 KiB

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