blob: eac0eaa5915c34d3ad8e8a07b8e70364b94b35f6 [file] [log] [blame]
Carlos Becker97889a72010-06-28 18:42:59 +01001namespace Eigen {
2
Benoit Jacob4d4a23c2010-06-30 10:11:55 -04003/** \page TutorialBlockOperations Tutorial page 4 - %Block operations
Carlos Becker97889a72010-06-28 18:42:59 +01004 \ingroup Tutorial
5
6\li \b Previous: \ref TutorialArrayClass
Benoit Jacob4d4a23c2010-06-30 10:11:55 -04007\li \b Next: \ref TutorialAdvancedInitialization
Carlos Becker97889a72010-06-28 18:42:59 +01008
Benoit Jacob5a52f282010-07-01 20:52:40 -04009This tutorial page explains the essentials of block operations.
10A block is a rectangular part of a matrix or array. Blocks expressions can be used both
11as rvalues and as lvalues. As usual with Eigen expressions, this abstraction has zero runtime cost
12provided that you let your compiler optimize.
Carlos Becker97889a72010-06-28 18:42:59 +010013
14\b Table \b of \b contents
Benoit Jacob5a52f282010-07-01 20:52:40 -040015 - \ref TutorialBlockOperationsUsing
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010016 - \ref TutorialBlockOperationsSyntaxColumnRows
17 - \ref TutorialBlockOperationsSyntaxCorners
18 - \ref TutorialBlockOperationsSyntaxVectors
19
Carlos Becker97889a72010-06-28 18:42:59 +010020
Carlos Becker97889a72010-06-28 18:42:59 +010021\section TutorialBlockOperationsUsing Using block operations
Carlos Becker97889a72010-06-28 18:42:59 +010022
23The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010024There are two versions, whose syntax is as follows:
Carlos Becker97889a72010-06-28 18:42:59 +010025
Gael Guennebaudf66fe262010-10-19 11:40:49 +020026<table class="manual">
27<tr><th>\b %Block \b operation</td>
28<th>Version constructing a \n dynamic-size block expression</th>
29<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010030<tr><td>%Block of size <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td>
Carlos Becker97889a72010-06-28 18:42:59 +010031 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040032matrix.block(i,j,p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010033 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040034matrix.block<p,q>(i,j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010035</tr>
36</table>
37
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040038As always in Eigen, indices start at 0.
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010039
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040040Both versions can be used on fixed-size and dynamic-size matrices and arrays.
41These two expressions are semantically equivalent.
42The only difference is that the fixed-size version will typically give you faster code if the block size is small,
43but requires this size to be known at compile time.
44
45The following program uses the dynamic-size and fixed-size versions to print the values of several blocks inside a
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010046matrix.
47
Gael Guennebaudf66fe262010-10-19 11:40:49 +020048<table class="example">
49<tr><th>Example:</th><th>Output:</th></tr>
50<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010051\include Tutorial_BlockOperations_print_block.cpp
52</td>
53<td>
Carlos Becker97889a72010-06-28 18:42:59 +010054\verbinclude Tutorial_BlockOperations_print_block.out
55</td></tr></table>
56
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040057In the above example the \link DenseBase::block() .block() \endlink function was employed as a \em rvalue, i.e.
58it was only read from. However, blocks can also be used as \em lvalues, meaning that you can assign to a block.
Carlos Becker97889a72010-06-28 18:42:59 +010059
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040060This is illustrated in the following example. This example also demonstrates blocks in arrays, which works exactly like the above-demonstrated blocks in matrices.
Carlos Becker97889a72010-06-28 18:42:59 +010061
Gael Guennebaudf66fe262010-10-19 11:40:49 +020062<table class="example">
63<tr><th>Example:</th><th>Output:</th></tr>
64<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010065\include Tutorial_BlockOperations_block_assignment.cpp
66</td>
67<td>
Carlos Becker97889a72010-06-28 18:42:59 +010068\verbinclude Tutorial_BlockOperations_block_assignment.out
69</td></tr></table>
70
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040071While the \link DenseBase::block() .block() \endlink method can be used for any block operation, there are
72other methods for special cases, providing more specialized API and/or better performance. On the topic of performance, all what
73matters 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,
74using the specialized \link DenseBase::col() .col() \endlink function described below lets Eigen know that, which can give it optimization opportunities.
Carlos Becker97889a72010-06-28 18:42:59 +010075
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040076The rest of this page describes these specialized methods.
Carlos Becker97889a72010-06-28 18:42:59 +010077
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010078\section TutorialBlockOperationsSyntaxColumnRows Columns and rows
Carlos Becker97889a72010-06-28 18:42:59 +010079
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040080Individual columns and rows are special cases of blocks. Eigen provides methods to easily address them:
81\link DenseBase::col() .col() \endlink and \link DenseBase::row() .row()\endlink.
Carlos Becker97889a72010-06-28 18:42:59 +010082
Gael Guennebaudf66fe262010-10-19 11:40:49 +020083<table class="manual">
84<tr><th>%Block operation</th>
85<th>Method</th>
Carlos Becker97889a72010-06-28 18:42:59 +010086<tr><td>i<sup>th</sup> row
87 \link DenseBase::row() * \endlink</td>
88 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010089matrix.row(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010090</tr>
91<tr><td>j<sup>th</sup> column
92 \link DenseBase::col() * \endlink</td>
93 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010094matrix.col(j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010095</tr>
96</table>
97
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040098The 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.
Carlos Becker97889a72010-06-28 18:42:59 +010099
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200100<table class="example">
101<tr><th>Example:</th><th>Output:</th></tr>
102<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100103\include Tutorial_BlockOperations_colrow.cpp
104</td>
105<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100106\verbinclude Tutorial_BlockOperations_colrow.out
Carlos Becker97889a72010-06-28 18:42:59 +0100107</td></tr></table>
108
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400109That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression.
110
Carlos Becker97889a72010-06-28 18:42:59 +0100111
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100112\section TutorialBlockOperationsSyntaxCorners Corner-related operations
Carlos Becker97889a72010-06-28 18:42:59 +0100113
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100114Eigen also provides special methods for blocks that are flushed against one of the corners or sides of a
115matrix or array. For instance, \link DenseBase::topLeftCorner() .topLeftCorner() \endlink can be used to refer
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400116to a block in the top-left corner of a matrix.
Carlos Becker97889a72010-06-28 18:42:59 +0100117
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100118The different possibilities are summarized in the following table:
119
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200120<table class="manual">
121<tr><th>%Block \b operation</td>
122<th>Version constructing a \n dynamic-size block expression</th>
123<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100124<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100125 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100126matrix.topLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100127 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100128matrix.topLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100129</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100130<tr><td>Bottom-left p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100131 \link DenseBase::bottomLeftCorner() * \endlink</td>
132 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100133matrix.bottomLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100134 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100135matrix.bottomLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100136</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100137<tr><td>Top-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100138 \link DenseBase::topRightCorner() * \endlink</td>
139 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100140matrix.topRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100141 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100142matrix.topRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100143</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100144<tr><td>Bottom-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100145 \link DenseBase::bottomRightCorner() * \endlink</td>
146 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100147matrix.bottomRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100148 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100149matrix.bottomRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100150</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100151<tr><td>%Block containing the first q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100152 \link DenseBase::topRows() * \endlink</td>
153 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100154matrix.topRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100155 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100156matrix.topRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100157</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100158<tr><td>%Block containing the last q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100159 \link DenseBase::bottomRows() * \endlink</td>
160 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100161matrix.bottomRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100162 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100163matrix.bottomRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100164</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100165<tr><td>%Block containing the first p columns
Carlos Becker97889a72010-06-28 18:42:59 +0100166 \link DenseBase::leftCols() * \endlink</td>
167 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100168matrix.leftCols(p);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100169 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100170matrix.leftCols<p>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100171</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100172<tr><td>%Block containing the last q columns
Carlos Becker97889a72010-06-28 18:42:59 +0100173 \link DenseBase::rightCols() * \endlink</td>
174 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100175matrix.rightCols(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100176 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100177matrix.rightCols<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100178</tr>
179</table>
180
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100181Here is a simple example illustrating the use of the operations presented above:
Carlos Becker97889a72010-06-28 18:42:59 +0100182
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200183<table class="example">
184<tr><th>Example:</th><th>Output:</th></tr>
185<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100186\include Tutorial_BlockOperations_corner.cpp
187</td>
188<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100189\verbinclude Tutorial_BlockOperations_corner.out
Carlos Becker97889a72010-06-28 18:42:59 +0100190</td></tr></table>
191
192
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100193\section TutorialBlockOperationsSyntaxVectors Block operations for vectors
Carlos Becker97889a72010-06-28 18:42:59 +0100194
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400195Eigen also provides a set of block operations designed specifically for the special case of vectors and one-dimensional arrays:
Carlos Becker97889a72010-06-28 18:42:59 +0100196
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200197<table class="manual">
198<tr><th> %Block operation</th>
199<th>Version constructing a \n dynamic-size block expression</th>
200<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100201<tr><td>%Block containing the first \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100202 \link DenseBase::head() * \endlink</td>
203 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100204vector.head(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100205 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100206vector.head<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100207</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100208<tr><td>%Block containing the last \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100209 \link DenseBase::tail() * \endlink</td>
210 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100211vector.tail(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100212 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100213vector.tail<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100214</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100215<tr><td>%Block containing \p n elements, starting at position \p i
Carlos Becker97889a72010-06-28 18:42:59 +0100216 \link DenseBase::segment() * \endlink</td>
217 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100218vector.segment(i,n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100219 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100220vector.segment<n>(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100221</tr>
222</table>
223
224
225An example is presented below:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200226<table class="example">
227<tr><th>Example:</th><th>Output:</th></tr>
228<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100229\include Tutorial_BlockOperations_vector.cpp
230</td>
231<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100232\verbinclude Tutorial_BlockOperations_vector.out
Carlos Becker97889a72010-06-28 18:42:59 +0100233</td></tr></table>
234
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400235\li \b Next: \ref TutorialAdvancedInitialization
Carlos Becker97889a72010-06-28 18:42:59 +0100236
237*/
238
239}