blob: 639df178ef5c516f745d26e59817fa848e6aed0a [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
Gael Guennebaud2ea1e492012-12-28 18:58:07 +010014\tableofcontents
Carlos Becker97889a72010-06-28 18:42:59 +010015
Carlos Becker97889a72010-06-28 18:42:59 +010016\section TutorialBlockOperationsUsing Using block operations
Carlos Becker97889a72010-06-28 18:42:59 +010017
18The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010019There are two versions, whose syntax is as follows:
Carlos Becker97889a72010-06-28 18:42:59 +010020
Gael Guennebaudf66fe262010-10-19 11:40:49 +020021<table class="manual">
22<tr><th>\b %Block \b operation</td>
23<th>Version constructing a \n dynamic-size block expression</th>
24<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010025<tr><td>%Block of size <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td>
Carlos Becker97889a72010-06-28 18:42:59 +010026 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040027matrix.block(i,j,p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010028 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040029matrix.block<p,q>(i,j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010030</tr>
31</table>
32
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040033As always in Eigen, indices start at 0.
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010034
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040035Both versions can be used on fixed-size and dynamic-size matrices and arrays.
36These two expressions are semantically equivalent.
37The only difference is that the fixed-size version will typically give you faster code if the block size is small,
38but requires this size to be known at compile time.
39
40The 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 +010041matrix.
42
Gael Guennebaudf66fe262010-10-19 11:40:49 +020043<table class="example">
44<tr><th>Example:</th><th>Output:</th></tr>
45<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010046\include Tutorial_BlockOperations_print_block.cpp
47</td>
48<td>
Carlos Becker97889a72010-06-28 18:42:59 +010049\verbinclude Tutorial_BlockOperations_print_block.out
50</td></tr></table>
51
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040052In the above example the \link DenseBase::block() .block() \endlink function was employed as a \em rvalue, i.e.
53it 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 +010054
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040055This 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 +010056
Gael Guennebaudf66fe262010-10-19 11:40:49 +020057<table class="example">
58<tr><th>Example:</th><th>Output:</th></tr>
59<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010060\include Tutorial_BlockOperations_block_assignment.cpp
61</td>
62<td>
Carlos Becker97889a72010-06-28 18:42:59 +010063\verbinclude Tutorial_BlockOperations_block_assignment.out
64</td></tr></table>
65
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040066While the \link DenseBase::block() .block() \endlink method can be used for any block operation, there are
67other methods for special cases, providing more specialized API and/or better performance. On the topic of performance, all what
68matters 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,
69using 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 +010070
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040071The rest of this page describes these specialized methods.
Carlos Becker97889a72010-06-28 18:42:59 +010072
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010073\section TutorialBlockOperationsSyntaxColumnRows Columns and rows
Carlos Becker97889a72010-06-28 18:42:59 +010074
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040075Individual columns and rows are special cases of blocks. Eigen provides methods to easily address them:
76\link DenseBase::col() .col() \endlink and \link DenseBase::row() .row()\endlink.
Carlos Becker97889a72010-06-28 18:42:59 +010077
Gael Guennebaudf66fe262010-10-19 11:40:49 +020078<table class="manual">
79<tr><th>%Block operation</th>
80<th>Method</th>
Carlos Becker97889a72010-06-28 18:42:59 +010081<tr><td>i<sup>th</sup> row
82 \link DenseBase::row() * \endlink</td>
83 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010084matrix.row(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010085</tr>
86<tr><td>j<sup>th</sup> column
87 \link DenseBase::col() * \endlink</td>
88 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010089matrix.col(j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010090</tr>
91</table>
92
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040093The 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 +010094
Gael Guennebaudf66fe262010-10-19 11:40:49 +020095<table class="example">
96<tr><th>Example:</th><th>Output:</th></tr>
97<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010098\include Tutorial_BlockOperations_colrow.cpp
99</td>
100<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100101\verbinclude Tutorial_BlockOperations_colrow.out
Carlos Becker97889a72010-06-28 18:42:59 +0100102</td></tr></table>
103
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400104That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression.
105
Carlos Becker97889a72010-06-28 18:42:59 +0100106
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100107\section TutorialBlockOperationsSyntaxCorners Corner-related operations
Carlos Becker97889a72010-06-28 18:42:59 +0100108
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100109Eigen also provides special methods for blocks that are flushed against one of the corners or sides of a
110matrix or array. For instance, \link DenseBase::topLeftCorner() .topLeftCorner() \endlink can be used to refer
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400111to a block in the top-left corner of a matrix.
Carlos Becker97889a72010-06-28 18:42:59 +0100112
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100113The different possibilities are summarized in the following table:
114
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200115<table class="manual">
116<tr><th>%Block \b operation</td>
117<th>Version constructing a \n dynamic-size block expression</th>
118<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100119<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100120 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100121matrix.topLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100122 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100123matrix.topLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100124</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100125<tr><td>Bottom-left p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100126 \link DenseBase::bottomLeftCorner() * \endlink</td>
127 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100128matrix.bottomLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100129 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100130matrix.bottomLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100131</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100132<tr><td>Top-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100133 \link DenseBase::topRightCorner() * \endlink</td>
134 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100135matrix.topRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100136 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100137matrix.topRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100138</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100139<tr><td>Bottom-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100140 \link DenseBase::bottomRightCorner() * \endlink</td>
141 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100142matrix.bottomRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100143 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100144matrix.bottomRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100145</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100146<tr><td>%Block containing the first q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100147 \link DenseBase::topRows() * \endlink</td>
148 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100149matrix.topRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100150 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100151matrix.topRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100152</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100153<tr><td>%Block containing the last q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100154 \link DenseBase::bottomRows() * \endlink</td>
155 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100156matrix.bottomRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100157 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100158matrix.bottomRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100159</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100160<tr><td>%Block containing the first p columns
Carlos Becker97889a72010-06-28 18:42:59 +0100161 \link DenseBase::leftCols() * \endlink</td>
162 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100163matrix.leftCols(p);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100164 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100165matrix.leftCols<p>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100166</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100167<tr><td>%Block containing the last q columns
Carlos Becker97889a72010-06-28 18:42:59 +0100168 \link DenseBase::rightCols() * \endlink</td>
169 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100170matrix.rightCols(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100171 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100172matrix.rightCols<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100173</tr>
174</table>
175
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100176Here is a simple example illustrating the use of the operations presented above:
Carlos Becker97889a72010-06-28 18:42:59 +0100177
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200178<table class="example">
179<tr><th>Example:</th><th>Output:</th></tr>
180<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100181\include Tutorial_BlockOperations_corner.cpp
182</td>
183<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100184\verbinclude Tutorial_BlockOperations_corner.out
Carlos Becker97889a72010-06-28 18:42:59 +0100185</td></tr></table>
186
187
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100188\section TutorialBlockOperationsSyntaxVectors Block operations for vectors
Carlos Becker97889a72010-06-28 18:42:59 +0100189
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400190Eigen 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 +0100191
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200192<table class="manual">
193<tr><th> %Block operation</th>
194<th>Version constructing a \n dynamic-size block expression</th>
195<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100196<tr><td>%Block containing the first \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100197 \link DenseBase::head() * \endlink</td>
198 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100199vector.head(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100200 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100201vector.head<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100202</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100203<tr><td>%Block containing the last \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100204 \link DenseBase::tail() * \endlink</td>
205 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100206vector.tail(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100207 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100208vector.tail<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100209</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100210<tr><td>%Block containing \p n elements, starting at position \p i
Carlos Becker97889a72010-06-28 18:42:59 +0100211 \link DenseBase::segment() * \endlink</td>
212 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100213vector.segment(i,n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100214 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100215vector.segment<n>(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100216</tr>
217</table>
218
219
220An example is presented below:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200221<table class="example">
222<tr><th>Example:</th><th>Output:</th></tr>
223<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100224\include Tutorial_BlockOperations_vector.cpp
225</td>
226<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100227\verbinclude Tutorial_BlockOperations_vector.out
Carlos Becker97889a72010-06-28 18:42:59 +0100228</td></tr></table>
229
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400230\li \b Next: \ref TutorialAdvancedInitialization
Carlos Becker97889a72010-06-28 18:42:59 +0100231
232*/
233
234}