blob: 70773a4635a11c5ff897eedcdc8bc151560e38b2 [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
Carlos Becker97889a72010-06-28 18:42:59 +010016 - \ref TutorialBlockOperationsSyntax
17 - \ref TutorialBlockOperationsSyntaxColumnRows
18 - \ref TutorialBlockOperationsSyntaxCorners
19
Carlos Becker97889a72010-06-28 18:42:59 +010020\section TutorialBlockOperationsUsing Using block operations
Carlos Becker97889a72010-06-28 18:42:59 +010021
22The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
Jitse Niesen30701642010-06-29 11:42:51 +010023This function returns a block of size <tt>(p,q)</tt> whose origin is at <tt>(i,j)</tt> by using
Carlos Becker97889a72010-06-28 18:42:59 +010024the following syntax:
25
26<table class="tutorial_code" align="center">
27<tr><td align="center">\b Block \b operation</td>
28<td align="center">Default \b version</td>
29<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
Benoit Jacob5a52f282010-07-01 20:52:40 -040030<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
38Therefore, if we want to print the values of a block inside a matrix we can simply write:
39<table class="tutorial_code"><tr><td>
40\include Tutorial_BlockOperations_print_block.cpp
41</td>
42<td>
43Output:
44\verbinclude Tutorial_BlockOperations_print_block.out
45</td></tr></table>
46
47
48In the previous example the \link DenseBase::block() .block() \endlink function was employed
49to read the values inside matrix \p m . Blocks can also be used to perform operations and
50assignments within matrices or arrays of different size:
51
52<table class="tutorial_code"><tr><td>
53\include Tutorial_BlockOperations_block_assignment.cpp
54</td>
55<td>
56Output:
57\verbinclude Tutorial_BlockOperations_block_assignment.out
58</td></tr></table>
59
60
61Blocks can also be combined with matrices and arrays to create more complex expressions:
62
63\code
64 MatrixXf m(3,3), n(2,2);
65 MatrixXf p(3,3);
66
67 m.block(0,0,2,2) = m.block(0,0,2,2) * n + p.block(1,1,2,2);
68\endcode
69
70It is important to point out that \link DenseBase::block() .block() \endlink is the
71general case for a block operation but there are many other useful block operations,
72as described in the next section.
73
74\section TutorialBlockOperationsSyntax Block operation syntax
75The following tables show a summary of Eigen's block operations and how they are applied to
76fixed- and dynamic-sized Eigen objects.
77
78\subsection TutorialBlockOperationsSyntaxColumnRows Columns and rows
79Other extremely useful block operations are \link DenseBase::col() .col() \endlink and
80\link DenseBase::row() .row() \endlink which provide access to a
81specific row or column. This is a special case in the sense that the syntax for fixed- and
82dynamic-sized objects is exactly the same:
83
84<table class="tutorial_code" align="center">
85<tr><td align="center">\b Block \b operation</td>
86<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
91MatrixXf m;
92std::cout << m.row(i);\endcode </td>
93 <td>\code
94Matrix3f m;
95std::cout << m.row(i);\endcode </td>
96</tr>
97<tr><td>j<sup>th</sup> column
98 \link DenseBase::col() * \endlink</td>
99 <td>\code
100MatrixXf m;
101std::cout << m.col(j);\endcode </td>
102 <td>\code
103Matrix3f m;
104std::cout << m.col(j);\endcode </td>
105</tr>
106</table>
107
108A simple example demonstrating these feature follows:
109
110<table class="tutorial_code"><tr><td>
111C++ code:
112\include Tutorial_BlockOperations_colrow.cpp
113</td>
114<td>
115Output:
116\include Tutorial_BlockOperations_colrow.out
117</td></tr></table>
118
119
120\b NOTE: the argument for \p col() and \p row() is the index of the column or row to be accessed,
121starting at 0. Therefore, \p col(0) will access the first column and \p col(1) the second one.
122
123
124\subsection TutorialBlockOperationsSyntaxCorners Corner-related operations
125<table class="tutorial_code" align="center">
126<tr><td align="center">\b Block \b operation</td>
127<td align="center">Default version</td>
128<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100129<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
Carlos Becker97889a72010-06-28 18:42:59 +0100130 <td>\code
131MatrixXf m;
Jitse Niesen30701642010-06-29 11:42:51 +0100132std::cout << m.topLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100133 <td>\code
134Matrix3f m;
Jitse Niesen30701642010-06-29 11:42:51 +0100135std::cout << m.topLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100136</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100137<tr><td>Bottom-left p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100138 \link DenseBase::bottomLeftCorner() * \endlink</td>
139 <td>\code
140MatrixXf m;
Jitse Niesen30701642010-06-29 11:42:51 +0100141std::cout << m.bottomLeftCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100142 <td>\code
143Matrix3f m;
Jitse Niesen30701642010-06-29 11:42:51 +0100144std::cout << m.bottomLeftCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100145</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100146<tr><td>Top-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100147 \link DenseBase::topRightCorner() * \endlink</td>
148 <td>\code
149MatrixXf m;
Jitse Niesen30701642010-06-29 11:42:51 +0100150std::cout << m.topRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100151 <td>\code
152Matrix3f m;
Jitse Niesen30701642010-06-29 11:42:51 +0100153std::cout << m.topRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100154</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100155<tr><td>Bottom-right p by q block
Carlos Becker97889a72010-06-28 18:42:59 +0100156 \link DenseBase::bottomRightCorner() * \endlink</td>
157 <td>\code
158MatrixXf m;
Jitse Niesen30701642010-06-29 11:42:51 +0100159std::cout << m.bottomRightCorner(p,q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100160 <td>\code
161Matrix3f m;
Jitse Niesen30701642010-06-29 11:42:51 +0100162std::cout << m.bottomRightCorner<p,q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100163</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100164<tr><td>Block containing the first q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100165 \link DenseBase::topRows() * \endlink</td>
166 <td>\code
167MatrixXf m;
Jitse Niesen30701642010-06-29 11:42:51 +0100168std::cout << m.topRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100169 <td>\code
170Matrix3f m;
Jitse Niesen30701642010-06-29 11:42:51 +0100171std::cout << m.topRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100172</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100173<tr><td>Block containing the last q rows
Carlos Becker97889a72010-06-28 18:42:59 +0100174 \link DenseBase::bottomRows() * \endlink</td>
175 <td>\code
176MatrixXf m;
Jitse Niesen30701642010-06-29 11:42:51 +0100177std::cout << m.bottomRows(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100178 <td>\code
179Matrix3f m;
Jitse Niesen30701642010-06-29 11:42:51 +0100180std::cout << m.bottomRows<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100181</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100182<tr><td>Block containing the first p columns
Carlos Becker97889a72010-06-28 18:42:59 +0100183 \link DenseBase::leftCols() * \endlink</td>
184 <td>\code
185MatrixXf m;
Jitse Niesen30701642010-06-29 11:42:51 +0100186std::cout << m.leftCols(p);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100187 <td>\code
188Matrix3f m;
Jitse Niesen30701642010-06-29 11:42:51 +0100189std::cout << m.leftCols<p>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100190</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100191<tr><td>Block containing the last q columns
Carlos Becker97889a72010-06-28 18:42:59 +0100192 \link DenseBase::rightCols() * \endlink</td>
193 <td>\code
194MatrixXf m;
Jitse Niesen30701642010-06-29 11:42:51 +0100195std::cout << m.rightCols(q);\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100196 <td>\code
197Matrix3f m;
Jitse Niesen30701642010-06-29 11:42:51 +0100198std::cout << m.rightCols<q>();\endcode </td>
Carlos Becker97889a72010-06-28 18:42:59 +0100199</tr>
200</table>
201
202
Jitse Niesen30701642010-06-29 11:42:51 +0100203Here is a simple example showing the power of the operations presented above:
Carlos Becker97889a72010-06-28 18:42:59 +0100204
205<table class="tutorial_code"><tr><td>
206C++ code:
207\include Tutorial_BlockOperations_corner.cpp
208</td>
209<td>
210Output:
211\include Tutorial_BlockOperations_corner.out
212</td></tr></table>
213
214
215
216
217
218
219
220
221\subsection TutorialBlockOperationsSyntaxVectors Block operations for vectors
222Eigen also provides a set of block operations designed specifically for vectors:
223
224<table class="tutorial_code" align="center">
225<tr><td align="center">\b Block \b operation</td>
226<td align="center">Default version</td>
227<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100228<tr><td>Block containing the first \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100229 \link DenseBase::head() * \endlink</td>
230 <td>\code
231VectorXf v;
232std::cout << v.head(n);\endcode </td>
233 <td>\code
234Vector3f v;
235std::cout << v.head<n>();\endcode </td>
236</tr>
Jitse Niesen30701642010-06-29 11:42:51 +0100237<tr><td>Block containing the last \p n elements
Carlos Becker97889a72010-06-28 18:42:59 +0100238 \link DenseBase::tail() * \endlink</td>
239 <td>\code
240VectorXf v;
241std::cout << v.tail(n);\endcode </td>
242 <td>\code
243Vector3f m;
244std::cout << v.tail<n>();\endcode </td>
245</tr>
246<tr><td>Block containing \p n elements, starting at position \p i
247 \link DenseBase::segment() * \endlink</td>
248 <td>\code
249VectorXf v;
250std::cout << v.segment(i,n);\endcode </td>
251 <td>\code
252Vector3f m;
253std::cout << v.segment<n>(i);\endcode </td>
254</tr>
255</table>
256
257
258An example is presented below:
259<table class="tutorial_code"><tr><td>
260C++ code:
261\include Tutorial_BlockOperations_vector.cpp
262</td>
263<td>
264Output:
265\include Tutorial_BlockOperations_vector.out
266</td></tr></table>
267
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400268\li \b Next: \ref TutorialAdvancedInitialization
Carlos Becker97889a72010-06-28 18:42:59 +0100269
270*/
271
272}