blob: b45cbfbc82160e70f98ded66864263dad2b68b42 [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 +010024This function returns a block of size <tt>(p,q)</tt> whose origin is at <tt>(i,j)</tt>.
25There are two versions, whose syntax is as follows:
Carlos Becker97889a72010-06-28 18:42:59 +010026
27<table class="tutorial_code" align="center">
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010028<tr><td align="center">\b %Block \b operation</td>
29<td align="center">Default version</td>
Carlos Becker97889a72010-06-28 18:42:59 +010030<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010031<tr><td>%Block of size <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td>
Carlos Becker97889a72010-06-28 18:42:59 +010032 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040033matrix.block(i,j,p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010034 <td>\code
Benoit Jacob5a52f282010-07-01 20:52:40 -040035matrix.block<p,q>(i,j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010036</tr>
37</table>
38
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010039The default version is a method which takes four arguments. It can always be used. The optimized version
40takes two template arguments (the size of the block) and two normal arguments (the position of the block).
41It can only be used if the size of the block is known at compile time, but it may be faster than the
42non-optimized version, especially if the size of the block is small. Both versions can be used on fixed-size
43and dynamic-size matrices and arrays.
44
45The following program uses the default and optimized versions to print the values of several blocks inside a
46matrix.
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
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010056In the above example the \link DenseBase::block() .block() \endlink function was employed
57to read the values inside matrix \p m . However, blocks can also be used as lvalues, meaning that you can
58assign to a block.
Carlos Becker97889a72010-06-28 18:42:59 +010059
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010060This is illustrated in the following example, which uses arrays instead of matrices. The coefficients of the
615-by-5 array \c n are first all set to 0.6, but then the 3-by-3 block in the middle is set to the values in
62\c m . The penultimate line shows that blocks can be combined with matrices and arrays to create more complex
63expressions. Blocks of an array are an array expression, and thus the multiplication here is coefficient-wise
64multiplication.
Carlos Becker97889a72010-06-28 18:42:59 +010065
66<table class="tutorial_code"><tr><td>
67\include Tutorial_BlockOperations_block_assignment.cpp
68</td>
69<td>
70Output:
71\verbinclude Tutorial_BlockOperations_block_assignment.out
72</td></tr></table>
73
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010074The \link DenseBase::block() .block() \endlink method is used for general block operations, but there are
75other methods for special cases. These are described in the rest of this page.
Carlos Becker97889a72010-06-28 18:42:59 +010076
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
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010080Individual columns and rows are special cases of blocks. Eigen provides methods to easily access them:
81\link DenseBase::col() .col() \endlink and \link DenseBase::row() .row()\endlink. There is no syntax variant
82for an optimized version.
Carlos Becker97889a72010-06-28 18:42:59 +010083
84<table class="tutorial_code" align="center">
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010085<tr><td align="center">\b %Block \b operation</td>
Carlos Becker97889a72010-06-28 18:42:59 +010086<td align="center">Default version</td>
87<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
88<tr><td>i<sup>th</sup> row
89 \link DenseBase::row() * \endlink</td>
90 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010091matrix.row(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010092 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010093matrix.row(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010094</tr>
95<tr><td>j<sup>th</sup> column
96 \link DenseBase::col() * \endlink</td>
97 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +010098matrix.col(j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +010099 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100100matrix.col(j);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100101</tr>
102</table>
103
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100104The argument for \p col() and \p row() is the index of the column or row to be accessed, starting at
1050. Therefore, \p col(0) will access the first column and \p col(1) the second one.
Carlos Becker97889a72010-06-28 18:42:59 +0100106
107<table class="tutorial_code"><tr><td>
108C++ code:
109\include Tutorial_BlockOperations_colrow.cpp
110</td>
111<td>
112Output:
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100113\verbinclude Tutorial_BlockOperations_colrow.out
Carlos Becker97889a72010-06-28 18:42:59 +0100114</td></tr></table>
115
116
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100117\section TutorialBlockOperationsSyntaxCorners Corner-related operations
Carlos Becker97889a72010-06-28 18:42:59 +0100118
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100119Eigen also provides special methods for blocks that are flushed against one of the corners or sides of a
120matrix or array. For instance, \link DenseBase::topLeftCorner() .topLeftCorner() \endlink can be used to refer
121to a block in the top-left corner of a matrix. Use <tt>matrix.topLeftCorner(p,q)</tt> to access the block
122consisting of the coefficients <tt>matrix(i,j)</tt> with \c i &lt; \c p and \c j &lt; \c q. As an other
123example, blocks consisting of whole rows flushed against the top side of the matrix can be accessed by
124\link DenseBase::topRows() .topRows() \endlink.
Carlos Becker97889a72010-06-28 18:42:59 +0100125
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100126The different possibilities are summarized in the following table:
127
Carlos Becker97889a72010-06-28 18:42:59 +0100128<table class="tutorial_code" align="center">
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100129<tr><td align="center">\b %Block \b operation</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100130<td align="center">Default version</td>
131<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100132<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100133 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100134matrix.topLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100135 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100136matrix.topLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100137</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100138<tr><td>Bottom-left p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100139 \link DenseBase::bottomLeftCorner() * \endlink</td>
140 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100141matrix.bottomLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100142 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100143matrix.bottomLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100144</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100145<tr><td>Top-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100146 \link DenseBase::topRightCorner() * \endlink</td>
147 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100148matrix.topRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100149 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100150matrix.topRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100151</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100152<tr><td>Bottom-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100153 \link DenseBase::bottomRightCorner() * \endlink</td>
154 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100155matrix.bottomRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100156 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100157matrix.bottomRightCorner<p,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 q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100160 \link DenseBase::topRows() * \endlink</td>
161 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100162matrix.topRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100163 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100164matrix.topRows<q>();\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 rows
Carlos Becker97889a72010-06-28 18:42:59 +0100167 \link DenseBase::bottomRows() * \endlink</td>
168 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100169matrix.bottomRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100170 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100171matrix.bottomRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100172</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100173<tr><td>%Block containing the first p columns
Carlos Becker97889a72010-06-28 18:42:59 +0100174 \link DenseBase::leftCols() * \endlink</td>
175 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100176matrix.leftCols(p);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100177 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100178matrix.leftCols<p>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100179</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100180<tr><td>%Block containing the last q columns
Carlos Becker97889a72010-06-28 18:42:59 +0100181 \link DenseBase::rightCols() * \endlink</td>
182 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100183matrix.rightCols(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100184 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100185matrix.rightCols<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100186</tr>
187</table>
188
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100189Here is a simple example illustrating the use of the operations presented above:
Carlos Becker97889a72010-06-28 18:42:59 +0100190
191<table class="tutorial_code"><tr><td>
192C++ code:
193\include Tutorial_BlockOperations_corner.cpp
194</td>
195<td>
196Output:
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100197\verbinclude Tutorial_BlockOperations_corner.out
Carlos Becker97889a72010-06-28 18:42:59 +0100198</td></tr></table>
199
200
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100201\section TutorialBlockOperationsSyntaxVectors Block operations for vectors
Carlos Becker97889a72010-06-28 18:42:59 +0100202
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100203Eigen also provides a set of block operations designed specifically for vectors and one-dimensional arrays:
Carlos Becker97889a72010-06-28 18:42:59 +0100204
205<table class="tutorial_code" align="center">
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100206<tr><td align="center">\b %Block \b operation</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100207<td align="center">Default version</td>
208<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100209<tr><td>%Block containing the first \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100210 \link DenseBase::head() * \endlink</td>
211 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100212vector.head(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100213 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100214vector.head<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100215</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100216<tr><td>%Block containing the last \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100217 \link DenseBase::tail() * \endlink</td>
218 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100219vector.tail(n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100220 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100221vector.tail<n>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100222</tr>
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100223<tr><td>%Block containing \p n elements, starting at position \p i
Carlos Becker97889a72010-06-28 18:42:59 +0100224 \link DenseBase::segment() * \endlink</td>
225 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100226vector.segment(i,n);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100227 <td>\code
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100228vector.segment<n>(i);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100229</tr>
230</table>
231
232
233An example is presented below:
234<table class="tutorial_code"><tr><td>
235C++ code:
236\include Tutorial_BlockOperations_vector.cpp
237</td>
238<td>
239Output:
Jitse Niesenb0bd1cf2010-07-14 10:16:12 +0100240\verbinclude Tutorial_BlockOperations_vector.out
Carlos Becker97889a72010-06-28 18:42:59 +0100241</td></tr></table>
242
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400243\li \b Next: \ref TutorialAdvancedInitialization
Carlos Becker97889a72010-06-28 18:42:59 +0100244
245*/
246
247}