Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 1 | namespace Eigen { |
| 2 | |
| 3 | /** \page TutorialBlockOperations Tutorial page 4 - Block operations |
| 4 | \ingroup Tutorial |
| 5 | |
| 6 | \li \b Previous: \ref TutorialArrayClass |
| 7 | \li \b Next: (not yet written) |
| 8 | |
| 9 | This tutorial explains the essentials of Block operations together with many examples. |
| 10 | |
| 11 | \b Table \b of \b contents |
| 12 | - \ref TutorialBlockOperationsWhatIs |
| 13 | - \ref TutorialBlockOperationsFixedAndDynamicSize |
| 14 | - \ref TutorialBlockOperationsSyntax |
| 15 | - \ref TutorialBlockOperationsSyntaxColumnRows |
| 16 | - \ref TutorialBlockOperationsSyntaxCorners |
| 17 | |
| 18 | |
| 19 | \section TutorialBlockOperationsWhatIs What are Block operations? |
| 20 | Block operations are a set of functions that provide an easy way to access a set of coefficients |
| 21 | inside a \b Matrix or \link ArrayBase Array \endlink. A typical example is accessing a single row or |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 22 | column within a given matrix, as well as extracting a sub-matrix from the latter. |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 23 | |
| 24 | Blocks are highly flexible and can be used both as \b rvalues and \b lvalues in expressions, simplifying |
| 25 | the task of writing combined expressions with Eigen. |
| 26 | |
| 27 | \subsection TutorialBlockOperationsFixedAndDynamicSize Block operations and compile-time optimizations |
| 28 | As said earlier, a block operation is a way of accessing a group of coefficients inside a Matrix or |
| 29 | Array object. Eigen considers two different cases in order to provide compile-time optimization for |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 30 | block operations, depending on whether the the size of the block to be accessed is known at compile time or not. |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 31 | |
| 32 | To deal with these two situations, for each type of block operation Eigen provides a default version that |
| 33 | is able to work with run-time dependant block sizes and another one for block operations whose block size is |
| 34 | known at compile-time. |
| 35 | |
| 36 | Even though both functions can be applied to fixed-size objects, it is advisable to use special block operations |
| 37 | in this case, allowing Eigen to perform more optimizations at compile-time. |
| 38 | |
| 39 | \section TutorialBlockOperationsUsing Using block operations |
| 40 | Block operations are implemented such that they are easy to use and combine with operators and other |
| 41 | matrices or arrays. |
| 42 | |
| 43 | The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink. |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 44 | This function returns a block of size <tt>(p,q)</tt> whose origin is at <tt>(i,j)</tt> by using |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 45 | the following syntax: |
| 46 | |
| 47 | <table class="tutorial_code" align="center"> |
| 48 | <tr><td align="center">\b Block \b operation</td> |
| 49 | <td align="center">Default \b version</td> |
| 50 | <td align="center">Optimized version when the<br>size is known at compile time</td></tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 51 | <tr><td>Block of length <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 52 | <td>\code |
| 53 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 54 | std::cout << m.block(i,j,p,q);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 55 | <td>\code |
| 56 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 57 | std::cout << m.block<p,q>(i,j);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 58 | </tr> |
| 59 | </table> |
| 60 | |
| 61 | Therefore, if we want to print the values of a block inside a matrix we can simply write: |
| 62 | <table class="tutorial_code"><tr><td> |
| 63 | \include Tutorial_BlockOperations_print_block.cpp |
| 64 | </td> |
| 65 | <td> |
| 66 | Output: |
| 67 | \verbinclude Tutorial_BlockOperations_print_block.out |
| 68 | </td></tr></table> |
| 69 | |
| 70 | |
| 71 | In the previous example the \link DenseBase::block() .block() \endlink function was employed |
| 72 | to read the values inside matrix \p m . Blocks can also be used to perform operations and |
| 73 | assignments within matrices or arrays of different size: |
| 74 | |
| 75 | <table class="tutorial_code"><tr><td> |
| 76 | \include Tutorial_BlockOperations_block_assignment.cpp |
| 77 | </td> |
| 78 | <td> |
| 79 | Output: |
| 80 | \verbinclude Tutorial_BlockOperations_block_assignment.out |
| 81 | </td></tr></table> |
| 82 | |
| 83 | |
| 84 | Blocks can also be combined with matrices and arrays to create more complex expressions: |
| 85 | |
| 86 | \code |
| 87 | MatrixXf m(3,3), n(2,2); |
| 88 | MatrixXf p(3,3); |
| 89 | |
| 90 | m.block(0,0,2,2) = m.block(0,0,2,2) * n + p.block(1,1,2,2); |
| 91 | \endcode |
| 92 | |
| 93 | It is important to point out that \link DenseBase::block() .block() \endlink is the |
| 94 | general case for a block operation but there are many other useful block operations, |
| 95 | as described in the next section. |
| 96 | |
| 97 | \section TutorialBlockOperationsSyntax Block operation syntax |
| 98 | The following tables show a summary of Eigen's block operations and how they are applied to |
| 99 | fixed- and dynamic-sized Eigen objects. |
| 100 | |
| 101 | \subsection TutorialBlockOperationsSyntaxColumnRows Columns and rows |
| 102 | Other extremely useful block operations are \link DenseBase::col() .col() \endlink and |
| 103 | \link DenseBase::row() .row() \endlink which provide access to a |
| 104 | specific row or column. This is a special case in the sense that the syntax for fixed- and |
| 105 | dynamic-sized objects is exactly the same: |
| 106 | |
| 107 | <table class="tutorial_code" align="center"> |
| 108 | <tr><td align="center">\b Block \b operation</td> |
| 109 | <td align="center">Default version</td> |
| 110 | <td align="center">Optimized version when the<br>size is known at compile time</td></tr> |
| 111 | <tr><td>i<sup>th</sup> row |
| 112 | \link DenseBase::row() * \endlink</td> |
| 113 | <td>\code |
| 114 | MatrixXf m; |
| 115 | std::cout << m.row(i);\endcode </td> |
| 116 | <td>\code |
| 117 | Matrix3f m; |
| 118 | std::cout << m.row(i);\endcode </td> |
| 119 | </tr> |
| 120 | <tr><td>j<sup>th</sup> column |
| 121 | \link DenseBase::col() * \endlink</td> |
| 122 | <td>\code |
| 123 | MatrixXf m; |
| 124 | std::cout << m.col(j);\endcode </td> |
| 125 | <td>\code |
| 126 | Matrix3f m; |
| 127 | std::cout << m.col(j);\endcode </td> |
| 128 | </tr> |
| 129 | </table> |
| 130 | |
| 131 | A simple example demonstrating these feature follows: |
| 132 | |
| 133 | <table class="tutorial_code"><tr><td> |
| 134 | C++ code: |
| 135 | \include Tutorial_BlockOperations_colrow.cpp |
| 136 | </td> |
| 137 | <td> |
| 138 | Output: |
| 139 | \include Tutorial_BlockOperations_colrow.out |
| 140 | </td></tr></table> |
| 141 | |
| 142 | |
| 143 | \b NOTE: the argument for \p col() and \p row() is the index of the column or row to be accessed, |
| 144 | starting at 0. Therefore, \p col(0) will access the first column and \p col(1) the second one. |
| 145 | |
| 146 | |
| 147 | \subsection TutorialBlockOperationsSyntaxCorners Corner-related operations |
| 148 | <table class="tutorial_code" align="center"> |
| 149 | <tr><td align="center">\b Block \b operation</td> |
| 150 | <td align="center">Default version</td> |
| 151 | <td align="center">Optimized version when the<br>size is known at compile time</td></tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 152 | <tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 153 | <td>\code |
| 154 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 155 | std::cout << m.topLeftCorner(p,q);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 156 | <td>\code |
| 157 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 158 | std::cout << m.topLeftCorner<p,q>();\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 159 | </tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 160 | <tr><td>Bottom-left p by q block |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 161 | \link DenseBase::bottomLeftCorner() * \endlink</td> |
| 162 | <td>\code |
| 163 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 164 | std::cout << m.bottomLeftCorner(p,q);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 165 | <td>\code |
| 166 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 167 | std::cout << m.bottomLeftCorner<p,q>();\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 168 | </tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 169 | <tr><td>Top-right p by q block |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 170 | \link DenseBase::topRightCorner() * \endlink</td> |
| 171 | <td>\code |
| 172 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 173 | std::cout << m.topRightCorner(p,q);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 174 | <td>\code |
| 175 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 176 | std::cout << m.topRightCorner<p,q>();\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 177 | </tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 178 | <tr><td>Bottom-right p by q block |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 179 | \link DenseBase::bottomRightCorner() * \endlink</td> |
| 180 | <td>\code |
| 181 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 182 | std::cout << m.bottomRightCorner(p,q);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 183 | <td>\code |
| 184 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 185 | std::cout << m.bottomRightCorner<p,q>();\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 186 | </tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 187 | <tr><td>Block containing the first q rows |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 188 | \link DenseBase::topRows() * \endlink</td> |
| 189 | <td>\code |
| 190 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 191 | std::cout << m.topRows(q);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 192 | <td>\code |
| 193 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 194 | std::cout << m.topRows<q>();\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 195 | </tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 196 | <tr><td>Block containing the last q rows |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 197 | \link DenseBase::bottomRows() * \endlink</td> |
| 198 | <td>\code |
| 199 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 200 | std::cout << m.bottomRows(q);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 201 | <td>\code |
| 202 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 203 | std::cout << m.bottomRows<q>();\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 204 | </tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 205 | <tr><td>Block containing the first p columns |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 206 | \link DenseBase::leftCols() * \endlink</td> |
| 207 | <td>\code |
| 208 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 209 | std::cout << m.leftCols(p);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 210 | <td>\code |
| 211 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 212 | std::cout << m.leftCols<p>();\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 213 | </tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 214 | <tr><td>Block containing the last q columns |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 215 | \link DenseBase::rightCols() * \endlink</td> |
| 216 | <td>\code |
| 217 | MatrixXf m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 218 | std::cout << m.rightCols(q);\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 219 | <td>\code |
| 220 | Matrix3f m; |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 221 | std::cout << m.rightCols<q>();\endcode </td> |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 222 | </tr> |
| 223 | </table> |
| 224 | |
| 225 | |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 226 | Here is a simple example showing the power of the operations presented above: |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 227 | |
| 228 | <table class="tutorial_code"><tr><td> |
| 229 | C++ code: |
| 230 | \include Tutorial_BlockOperations_corner.cpp |
| 231 | </td> |
| 232 | <td> |
| 233 | Output: |
| 234 | \include Tutorial_BlockOperations_corner.out |
| 235 | </td></tr></table> |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | \subsection TutorialBlockOperationsSyntaxVectors Block operations for vectors |
| 245 | Eigen also provides a set of block operations designed specifically for vectors: |
| 246 | |
| 247 | <table class="tutorial_code" align="center"> |
| 248 | <tr><td align="center">\b Block \b operation</td> |
| 249 | <td align="center">Default version</td> |
| 250 | <td align="center">Optimized version when the<br>size is known at compile time</td></tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 251 | <tr><td>Block containing the first \p n elements |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 252 | \link DenseBase::head() * \endlink</td> |
| 253 | <td>\code |
| 254 | VectorXf v; |
| 255 | std::cout << v.head(n);\endcode </td> |
| 256 | <td>\code |
| 257 | Vector3f v; |
| 258 | std::cout << v.head<n>();\endcode </td> |
| 259 | </tr> |
Jitse Niesen | 3070164 | 2010-06-29 11:42:51 +0100 | [diff] [blame] | 260 | <tr><td>Block containing the last \p n elements |
Carlos Becker | 97889a7 | 2010-06-28 18:42:59 +0100 | [diff] [blame] | 261 | \link DenseBase::tail() * \endlink</td> |
| 262 | <td>\code |
| 263 | VectorXf v; |
| 264 | std::cout << v.tail(n);\endcode </td> |
| 265 | <td>\code |
| 266 | Vector3f m; |
| 267 | std::cout << v.tail<n>();\endcode </td> |
| 268 | </tr> |
| 269 | <tr><td>Block containing \p n elements, starting at position \p i |
| 270 | \link DenseBase::segment() * \endlink</td> |
| 271 | <td>\code |
| 272 | VectorXf v; |
| 273 | std::cout << v.segment(i,n);\endcode </td> |
| 274 | <td>\code |
| 275 | Vector3f m; |
| 276 | std::cout << v.segment<n>(i);\endcode </td> |
| 277 | </tr> |
| 278 | </table> |
| 279 | |
| 280 | |
| 281 | An example is presented below: |
| 282 | <table class="tutorial_code"><tr><td> |
| 283 | C++ code: |
| 284 | \include Tutorial_BlockOperations_vector.cpp |
| 285 | </td> |
| 286 | <td> |
| 287 | Output: |
| 288 | \include Tutorial_BlockOperations_vector.out |
| 289 | </td></tr></table> |
| 290 | |
| 291 | |
| 292 | */ |
| 293 | |
| 294 | } |