blob: 64454a65e26e8c3c09687d49368256efe3d6d8cb [file] [log] [blame]
Carlos Becker97889a72010-06-28 18:42:59 +01001namespace Eigen {
2
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01003/** \eigenManualPage TutorialBlockOperations Block operations
Carlos Becker97889a72010-06-28 18:42:59 +01004
5\li \b Previous: \ref TutorialArrayClass
Benoit Jacob4d4a23c2010-06-30 10:11:55 -04006\li \b Next: \ref TutorialAdvancedInitialization
Carlos Becker97889a72010-06-28 18:42:59 +01007
Benoit Jacob5a52f282010-07-01 20:52:40 -04008This tutorial page explains the essentials of block operations.
9A block is a rectangular part of a matrix or array. Blocks expressions can be used both
10as rvalues and as lvalues. As usual with Eigen expressions, this abstraction has zero runtime cost
11provided that you let your compiler optimize.
Carlos Becker97889a72010-06-28 18:42:59 +010012
Gael Guennebaud93ee82b2013-01-05 16:37:11 +010013\eigenAutoToc
Carlos Becker97889a72010-06-28 18:42:59 +010014
Carlos Becker97889a72010-06-28 18:42:59 +010015\section TutorialBlockOperationsUsing Using block operations
Carlos Becker97889a72010-06-28 18:42:59 +010016
17The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010018There are two versions, whose syntax is as follows:
Carlos Becker97889a72010-06-28 18:42:59 +010019
Gael Guennebaudf66fe262010-10-19 11:40:49 +020020<table class="manual">
21<tr><th>\b %Block \b operation</td>
22<th>Version constructing a \n dynamic-size block expression</th>
23<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010024<tr><td>%Block of size <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td>
Carlos Becker97889a72010-06-28 18:42:59 +010025 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040026matrix.block(i,j,p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010027 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040028matrix.block<p,q>(i,j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010029</tr>
30</table>
31
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040032As always in Eigen, indices start at 0.
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010033
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040034Both versions can be used on fixed-size and dynamic-size matrices and arrays.
35These two expressions are semantically equivalent.
36The only difference is that the fixed-size version will typically give you faster code if the block size is small,
37but requires this size to be known at compile time.
38
39The 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 +010040matrix.
41
Gael Guennebaudf66fe262010-10-19 11:40:49 +020042<table class="example">
43<tr><th>Example:</th><th>Output:</th></tr>
44<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010045\include Tutorial_BlockOperations_print_block.cpp
46</td>
47<td>
Carlos Becker97889a72010-06-28 18:42:59 +010048\verbinclude Tutorial_BlockOperations_print_block.out
49</td></tr></table>
50
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040051In the above example the \link DenseBase::block() .block() \endlink function was employed as a \em rvalue, i.e.
52it 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 +010053
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040054This 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 +010055
Gael Guennebaudf66fe262010-10-19 11:40:49 +020056<table class="example">
57<tr><th>Example:</th><th>Output:</th></tr>
58<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010059\include Tutorial_BlockOperations_block_assignment.cpp
60</td>
61<td>
Carlos Becker97889a72010-06-28 18:42:59 +010062\verbinclude Tutorial_BlockOperations_block_assignment.out
63</td></tr></table>
64
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040065While the \link DenseBase::block() .block() \endlink method can be used for any block operation, there are
66other methods for special cases, providing more specialized API and/or better performance. On the topic of performance, all what
67matters 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,
68using 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 +010069
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040070The rest of this page describes these specialized methods.
Carlos Becker97889a72010-06-28 18:42:59 +010071
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010072\section TutorialBlockOperationsSyntaxColumnRows Columns and rows
Carlos Becker97889a72010-06-28 18:42:59 +010073
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040074Individual columns and rows are special cases of blocks. Eigen provides methods to easily address them:
75\link DenseBase::col() .col() \endlink and \link DenseBase::row() .row()\endlink.
Carlos Becker97889a72010-06-28 18:42:59 +010076
Gael Guennebaudf66fe262010-10-19 11:40:49 +020077<table class="manual">
78<tr><th>%Block operation</th>
79<th>Method</th>
Carlos Becker97889a72010-06-28 18:42:59 +010080<tr><td>i<sup>th</sup> row
81 \link DenseBase::row() * \endlink</td>
82 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010083matrix.row(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010084</tr>
85<tr><td>j<sup>th</sup> column
86 \link DenseBase::col() * \endlink</td>
87 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010088matrix.col(j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010089</tr>
90</table>
91
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040092The 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 +010093
Gael Guennebaudf66fe262010-10-19 11:40:49 +020094<table class="example">
95<tr><th>Example:</th><th>Output:</th></tr>
96<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010097\include Tutorial_BlockOperations_colrow.cpp
98</td>
99<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100100\verbinclude Tutorial_BlockOperations_colrow.out
Carlos Becker97889a72010-06-28 18:42:59 +0100101</td></tr></table>
102
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400103That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression.
104
Carlos Becker97889a72010-06-28 18:42:59 +0100105
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100106\section TutorialBlockOperationsSyntaxCorners Corner-related operations
Carlos Becker97889a72010-06-28 18:42:59 +0100107
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100108Eigen also provides special methods for blocks that are flushed against one of the corners or sides of a
109matrix or array. For instance, \link DenseBase::topLeftCorner() .topLeftCorner() \endlink can be used to refer
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400110to a block in the top-left corner of a matrix.
Carlos Becker97889a72010-06-28 18:42:59 +0100111
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100112The different possibilities are summarized in the following table:
113
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200114<table class="manual">
115<tr><th>%Block \b operation</td>
116<th>Version constructing a \n dynamic-size block expression</th>
117<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100118<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100119 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100120matrix.topLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100121 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100122matrix.topLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100123</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100124<tr><td>Bottom-left p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100125 \link DenseBase::bottomLeftCorner() * \endlink</td>
126 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100127matrix.bottomLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100128 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100129matrix.bottomLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100130</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100131<tr><td>Top-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100132 \link DenseBase::topRightCorner() * \endlink</td>
133 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100134matrix.topRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100135 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100136matrix.topRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100137</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100138<tr><td>Bottom-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100139 \link DenseBase::bottomRightCorner() * \endlink</td>
140 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100141matrix.bottomRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100142 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100143matrix.bottomRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100144</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100145<tr><td>%Block containing the first q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100146 \link DenseBase::topRows() * \endlink</td>
147 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100148matrix.topRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100149 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100150matrix.topRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100151</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100152<tr><td>%Block containing the last q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100153 \link DenseBase::bottomRows() * \endlink</td>
154 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100155matrix.bottomRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100156 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100157matrix.bottomRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100158</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100159<tr><td>%Block containing the first p columns
Carlos Becker97889a72010-06-28 18:42:59 +0100160 \link DenseBase::leftCols() * \endlink</td>
161 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100162matrix.leftCols(p);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100163 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100164matrix.leftCols<p>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100165</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100166<tr><td>%Block containing the last q columns
Carlos Becker97889a72010-06-28 18:42:59 +0100167 \link DenseBase::rightCols() * \endlink</td>
168 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100169matrix.rightCols(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100170 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100171matrix.rightCols<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100172</tr>
173</table>
174
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100175Here is a simple example illustrating the use of the operations presented above:
Carlos Becker97889a72010-06-28 18:42:59 +0100176
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200177<table class="example">
178<tr><th>Example:</th><th>Output:</th></tr>
179<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100180\include Tutorial_BlockOperations_corner.cpp
181</td>
182<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100183\verbinclude Tutorial_BlockOperations_corner.out
Carlos Becker97889a72010-06-28 18:42:59 +0100184</td></tr></table>
185
186
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100187\section TutorialBlockOperationsSyntaxVectors Block operations for vectors
Carlos Becker97889a72010-06-28 18:42:59 +0100188
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400189Eigen 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 +0100190
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200191<table class="manual">
192<tr><th> %Block operation</th>
193<th>Version constructing a \n dynamic-size block expression</th>
194<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100195<tr><td>%Block containing the first \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100196 \link DenseBase::head() * \endlink</td>
197 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100198vector.head(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100199 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100200vector.head<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100201</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100202<tr><td>%Block containing the last \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100203 \link DenseBase::tail() * \endlink</td>
204 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100205vector.tail(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100206 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100207vector.tail<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100208</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100209<tr><td>%Block containing \p n elements, starting at position \p i
Carlos Becker97889a72010-06-28 18:42:59 +0100210 \link DenseBase::segment() * \endlink</td>
211 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100212vector.segment(i,n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100213 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100214vector.segment<n>(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100215</tr>
216</table>
217
218
219An example is presented below:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200220<table class="example">
221<tr><th>Example:</th><th>Output:</th></tr>
222<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100223\include Tutorial_BlockOperations_vector.cpp
224</td>
225<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100226\verbinclude Tutorial_BlockOperations_vector.out
Carlos Becker97889a72010-06-28 18:42:59 +0100227</td></tr></table>
228
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400229\li \b Next: \ref TutorialAdvancedInitialization
Carlos Becker97889a72010-06-28 18:42:59 +0100230
231*/
232
233}