blob: ce6bc4717d74dfbd4484e6b24bd983d2e5acdba2 [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
26<table class="tutorial_code" align="center">
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010027<tr><td align="center">\b %Block \b operation</td>
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040028<td align="center">Version constructing a dynamic-size block expression</td>
29<td align="center">Version constructing a fixed-size block expression</td></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
Carlos Becker97889a72010-06-28 18:42:59 +010048<table class="tutorial_code"><tr><td>
49\include Tutorial_BlockOperations_print_block.cpp
50</td>
51<td>
52Output:
53\verbinclude Tutorial_BlockOperations_print_block.out
54</td></tr></table>
55
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040056In the above example the \link DenseBase::block() .block() \endlink function was employed as a \em rvalue, i.e.
57it 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 +010058
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040059This 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 +010060
61<table class="tutorial_code"><tr><td>
62\include Tutorial_BlockOperations_block_assignment.cpp
63</td>
64<td>
65Output:
66\verbinclude Tutorial_BlockOperations_block_assignment.out
67</td></tr></table>
68
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040069While the \link DenseBase::block() .block() \endlink method can be used for any block operation, there are
70other methods for special cases, providing more specialized API and/or better performance. On the topic of performance, all what
71matters 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,
72using 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 +010073
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040074The rest of this page describes these specialized methods.
Carlos Becker97889a72010-06-28 18:42:59 +010075
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010076\section TutorialBlockOperationsSyntaxColumnRows Columns and rows
Carlos Becker97889a72010-06-28 18:42:59 +010077
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040078Individual columns and rows are special cases of blocks. Eigen provides methods to easily address them:
79\link DenseBase::col() .col() \endlink and \link DenseBase::row() .row()\endlink.
Carlos Becker97889a72010-06-28 18:42:59 +010080
81<table class="tutorial_code" align="center">
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010082<tr><td align="center">\b %Block \b operation</td>
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040083<td align="center">Method</td>
Carlos Becker97889a72010-06-28 18:42:59 +010084<tr><td>i<sup>th</sup> row
85 \link DenseBase::row() * \endlink</td>
86 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010087matrix.row(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010088</tr>
89<tr><td>j<sup>th</sup> column
90 \link DenseBase::col() * \endlink</td>
91 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010092matrix.col(j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010093</tr>
94</table>
95
Benoit Jacob1c15a6d2010-10-18 08:44:27 -040096The 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 +010097
98<table class="tutorial_code"><tr><td>
99C++ code:
100\include Tutorial_BlockOperations_colrow.cpp
101</td>
102<td>
103Output:
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100104\verbinclude Tutorial_BlockOperations_colrow.out
Carlos Becker97889a72010-06-28 18:42:59 +0100105</td></tr></table>
106
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400107That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression.
108
Carlos Becker97889a72010-06-28 18:42:59 +0100109
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100110\section TutorialBlockOperationsSyntaxCorners Corner-related operations
Carlos Becker97889a72010-06-28 18:42:59 +0100111
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100112Eigen also provides special methods for blocks that are flushed against one of the corners or sides of a
113matrix or array. For instance, \link DenseBase::topLeftCorner() .topLeftCorner() \endlink can be used to refer
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400114to a block in the top-left corner of a matrix.
Carlos Becker97889a72010-06-28 18:42:59 +0100115
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100116The different possibilities are summarized in the following table:
117
Carlos Becker97889a72010-06-28 18:42:59 +0100118<table class="tutorial_code" align="center">
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100119<tr><td align="center">\b %Block \b operation</td>
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400120<td align="center">Version constructing a dynamic-size block expression</td>
121<td align="center">Version constructing a fixed-size block expression</td></tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100122<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100123 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100124matrix.topLeftCorner(p,q);\endcode </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</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100128<tr><td>Bottom-left p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100129 \link DenseBase::bottomLeftCorner() * \endlink</td>
130 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100131matrix.bottomLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100132 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100133matrix.bottomLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100134</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100135<tr><td>Top-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100136 \link DenseBase::topRightCorner() * \endlink</td>
137 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100138matrix.topRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100139 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100140matrix.topRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100141</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100142<tr><td>Bottom-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100143 \link DenseBase::bottomRightCorner() * \endlink</td>
144 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100145matrix.bottomRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100146 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100147matrix.bottomRightCorner<p,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 first q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100150 \link DenseBase::topRows() * \endlink</td>
151 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100152matrix.topRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100153 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100154matrix.topRows<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 last q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100157 \link DenseBase::bottomRows() * \endlink</td>
158 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100159matrix.bottomRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100160 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100161matrix.bottomRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100162</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100163<tr><td>%Block containing the first p columns
Carlos Becker97889a72010-06-28 18:42:59 +0100164 \link DenseBase::leftCols() * \endlink</td>
165 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100166matrix.leftCols(p);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100167 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100168matrix.leftCols<p>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100169</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100170<tr><td>%Block containing the last q columns
Carlos Becker97889a72010-06-28 18:42:59 +0100171 \link DenseBase::rightCols() * \endlink</td>
172 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100173matrix.rightCols(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100174 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100175matrix.rightCols<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100176</tr>
177</table>
178
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100179Here is a simple example illustrating the use of the operations presented above:
Carlos Becker97889a72010-06-28 18:42:59 +0100180
181<table class="tutorial_code"><tr><td>
182C++ code:
183\include Tutorial_BlockOperations_corner.cpp
184</td>
185<td>
186Output:
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100187\verbinclude Tutorial_BlockOperations_corner.out
Carlos Becker97889a72010-06-28 18:42:59 +0100188</td></tr></table>
189
190
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100191\section TutorialBlockOperationsSyntaxVectors Block operations for vectors
Carlos Becker97889a72010-06-28 18:42:59 +0100192
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400193Eigen 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 +0100194
195<table class="tutorial_code" align="center">
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100196<tr><td align="center">\b %Block \b operation</td>
Benoit Jacob1c15a6d2010-10-18 08:44:27 -0400197<td align="center">Version constructing a dynamic-size block expression</td>
198<td align="center">Version constructing a fixed-size block expression</td></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100199<tr><td>%Block containing the first \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100200 \link DenseBase::head() * \endlink</td>
201 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100202vector.head(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100203 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100204vector.head<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100205</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100206<tr><td>%Block containing the last \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100207 \link DenseBase::tail() * \endlink</td>
208 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100209vector.tail(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100210 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100211vector.tail<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100212</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100213<tr><td>%Block containing \p n elements, starting at position \p i
Carlos Becker97889a72010-06-28 18:42:59 +0100214 \link DenseBase::segment() * \endlink</td>
215 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100216vector.segment(i,n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100217 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100218vector.segment<n>(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100219</tr>
220</table>
221
222
223An example is presented below:
224<table class="tutorial_code"><tr><td>
225C++ code:
226\include Tutorial_BlockOperations_vector.cpp
227</td>
228<td>
229Output:
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100230\verbinclude Tutorial_BlockOperations_vector.out
Carlos Becker97889a72010-06-28 18:42:59 +0100231</td></tr></table>
232
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400233\li \b Next: \ref TutorialAdvancedInitialization
Carlos Becker97889a72010-06-28 18:42:59 +0100234
235*/
236
237}