blob: df277482c897e23675520e21e0fb07b50ff0cce5 [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
Gael Guennebaud091a49c2013-01-06 23:48:59 +01005This page explains the essentials of block operations.
Benoit Jacob5a52f282010-07-01 20:52:40 -04006A block is a rectangular part of a matrix or array. Blocks expressions can be used both
7as rvalues and as lvalues. As usual with Eigen expressions, this abstraction has zero runtime cost
8provided that you let your compiler optimize.
Carlos Becker97889a72010-06-28 18:42:59 +01009
Gael Guennebaud93ee82b2013-01-05 16:37:11 +010010\eigenAutoToc
Carlos Becker97889a72010-06-28 18:42:59 +010011
Carlos Becker97889a72010-06-28 18:42:59 +010012\section TutorialBlockOperationsUsing Using block operations
Carlos Becker97889a72010-06-28 18:42:59 +010013
14The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010015There are two versions, whose syntax is as follows:
Carlos Becker97889a72010-06-28 18:42:59 +010016
Gael Guennebaudf66fe262010-10-19 11:40:49 +020017<table class="manual">
18<tr><th>\b %Block \b operation</td>
19<th>Version constructing a \n dynamic-size block expression</th>
20<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010021<tr><td>%Block of size <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td>
Carlos Becker97889a72010-06-28 18:42:59 +010022 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040023matrix.block(i,j,p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010024 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040025matrix.block<p,q>(i,j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010026</tr>
27</table>
28
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040029As always in Eigen, indices start at 0.
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010030
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040031Both versions can be used on fixed-size and dynamic-size matrices and arrays.
32These two expressions are semantically equivalent.
33The only difference is that the fixed-size version will typically give you faster code if the block size is small,
34but requires this size to be known at compile time.
35
36The 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 +010037matrix.
38
Gael Guennebaudf66fe262010-10-19 11:40:49 +020039<table class="example">
40<tr><th>Example:</th><th>Output:</th></tr>
41<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010042\include Tutorial_BlockOperations_print_block.cpp
43</td>
44<td>
Carlos Becker97889a72010-06-28 18:42:59 +010045\verbinclude Tutorial_BlockOperations_print_block.out
46</td></tr></table>
47
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040048In the above example the \link DenseBase::block() .block() \endlink function was employed as a \em rvalue, i.e.
49it 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 +010050
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040051This 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 +010052
Gael Guennebaudf66fe262010-10-19 11:40:49 +020053<table class="example">
54<tr><th>Example:</th><th>Output:</th></tr>
55<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010056\include Tutorial_BlockOperations_block_assignment.cpp
57</td>
58<td>
Carlos Becker97889a72010-06-28 18:42:59 +010059\verbinclude Tutorial_BlockOperations_block_assignment.out
60</td></tr></table>
61
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040062While the \link DenseBase::block() .block() \endlink method can be used for any block operation, there are
63other methods for special cases, providing more specialized API and/or better performance. On the topic of performance, all what
64matters 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,
65using 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 +010066
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040067The rest of this page describes these specialized methods.
Carlos Becker97889a72010-06-28 18:42:59 +010068
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010069\section TutorialBlockOperationsSyntaxColumnRows Columns and rows
Carlos Becker97889a72010-06-28 18:42:59 +010070
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040071Individual columns and rows are special cases of blocks. Eigen provides methods to easily address them:
72\link DenseBase::col() .col() \endlink and \link DenseBase::row() .row()\endlink.
Carlos Becker97889a72010-06-28 18:42:59 +010073
Gael Guennebaudf66fe262010-10-19 11:40:49 +020074<table class="manual">
75<tr><th>%Block operation</th>
76<th>Method</th>
Carlos Becker97889a72010-06-28 18:42:59 +010077<tr><td>i<sup>th</sup> row
78 \link DenseBase::row() * \endlink</td>
79 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010080matrix.row(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010081</tr>
82<tr><td>j<sup>th</sup> column
83 \link DenseBase::col() * \endlink</td>
84 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010085matrix.col(j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010086</tr>
87</table>
88
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040089The 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 +010090
Gael Guennebaudf66fe262010-10-19 11:40:49 +020091<table class="example">
92<tr><th>Example:</th><th>Output:</th></tr>
93<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +010094\include Tutorial_BlockOperations_colrow.cpp
95</td>
96<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010097\verbinclude Tutorial_BlockOperations_colrow.out
Carlos Becker97889a72010-06-28 18:42:59 +010098</td></tr></table>
99
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400100That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression.
101
Carlos Becker97889a72010-06-28 18:42:59 +0100102
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100103\section TutorialBlockOperationsSyntaxCorners Corner-related operations
Carlos Becker97889a72010-06-28 18:42:59 +0100104
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100105Eigen also provides special methods for blocks that are flushed against one of the corners or sides of a
106matrix or array. For instance, \link DenseBase::topLeftCorner() .topLeftCorner() \endlink can be used to refer
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400107to a block in the top-left corner of a matrix.
Carlos Becker97889a72010-06-28 18:42:59 +0100108
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100109The different possibilities are summarized in the following table:
110
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200111<table class="manual">
112<tr><th>%Block \b operation</td>
113<th>Version constructing a \n dynamic-size block expression</th>
114<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100115<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100116 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100117matrix.topLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100118 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100119matrix.topLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100120</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100121<tr><td>Bottom-left p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100122 \link DenseBase::bottomLeftCorner() * \endlink</td>
123 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100124matrix.bottomLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100125 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100126matrix.bottomLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100127</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100128<tr><td>Top-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100129 \link DenseBase::topRightCorner() * \endlink</td>
130 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100131matrix.topRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100132 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100133matrix.topRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100134</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100135<tr><td>Bottom-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100136 \link DenseBase::bottomRightCorner() * \endlink</td>
137 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100138matrix.bottomRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100139 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100140matrix.bottomRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100141</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100142<tr><td>%Block containing the first q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100143 \link DenseBase::topRows() * \endlink</td>
144 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100145matrix.topRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100146 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100147matrix.topRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100148</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100149<tr><td>%Block containing the last q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100150 \link DenseBase::bottomRows() * \endlink</td>
151 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100152matrix.bottomRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100153 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100154matrix.bottomRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100155</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100156<tr><td>%Block containing the first p columns
Carlos Becker97889a72010-06-28 18:42:59 +0100157 \link DenseBase::leftCols() * \endlink</td>
158 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100159matrix.leftCols(p);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100160 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100161matrix.leftCols<p>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100162</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100163<tr><td>%Block containing the last q columns
Carlos Becker97889a72010-06-28 18:42:59 +0100164 \link DenseBase::rightCols() * \endlink</td>
165 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100166matrix.rightCols(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100167 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100168matrix.rightCols<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100169</tr>
Jens Wehner4d870c42021-08-05 17:21:16 +0000170<tr><td>%Block containing the q columns starting from i
171 \link DenseBase::middleCols() * \endlink</td>
172 <td>\code
173matrix.middleCols(i,q);\endcode </td>
174 <td>\code
175matrix.middleCols<q>(i);\endcode </td>
176</tr>
177<tr><td>%Block containing the q rows starting from i
178 \link DenseBase::middleRows() * \endlink</td>
179 <td>\code
180matrix.middleRows(i,q);\endcode </td>
181 <td>\code
182matrix.middleRows<q>(i);\endcode </td>
183</tr>
Carlos Becker97889a72010-06-28 18:42:59 +0100184</table>
185
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100186Here is a simple example illustrating the use of the operations presented above:
Carlos Becker97889a72010-06-28 18:42:59 +0100187
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200188<table class="example">
189<tr><th>Example:</th><th>Output:</th></tr>
190<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100191\include Tutorial_BlockOperations_corner.cpp
192</td>
193<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100194\verbinclude Tutorial_BlockOperations_corner.out
Carlos Becker97889a72010-06-28 18:42:59 +0100195</td></tr></table>
196
197
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100198\section TutorialBlockOperationsSyntaxVectors Block operations for vectors
Carlos Becker97889a72010-06-28 18:42:59 +0100199
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400200Eigen 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 +0100201
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200202<table class="manual">
203<tr><th> %Block operation</th>
204<th>Version constructing a \n dynamic-size block expression</th>
205<th>Version constructing a \n fixed-size block expression</th></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100206<tr><td>%Block containing the first \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100207 \link DenseBase::head() * \endlink</td>
208 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100209vector.head(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100210 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100211vector.head<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100212</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100213<tr><td>%Block containing the last \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100214 \link DenseBase::tail() * \endlink</td>
215 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100216vector.tail(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100217 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100218vector.tail<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100219</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100220<tr><td>%Block containing \p n elements, starting at position \p i
Carlos Becker97889a72010-06-28 18:42:59 +0100221 \link DenseBase::segment() * \endlink</td>
222 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100223vector.segment(i,n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100224 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100225vector.segment<n>(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100226</tr>
227</table>
228
229
230An example is presented below:
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200231<table class="example">
232<tr><th>Example:</th><th>Output:</th></tr>
233<tr><td>
Carlos Becker97889a72010-06-28 18:42:59 +0100234\include Tutorial_BlockOperations_vector.cpp
235</td>
236<td>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100237\verbinclude Tutorial_BlockOperations_vector.out
Carlos Becker97889a72010-06-28 18:42:59 +0100238</td></tr></table>
239
Carlos Becker97889a72010-06-28 18:42:59 +0100240*/
241
242}