blob: 57a8858524d3cc24e0534784c013b37d10405896 [file] [log] [blame]
Carlos Becker97889a72010-06-28 18:42:59 +01001namespace Eigen {
2
3/** \page TutorialBlockOperations Tutorial page 4 - Block operations
4 \ingroup Tutorial
5
6\li \b Previous: \ref TutorialArrayClass
7\li \b Next: (not yet written)
8
9This tutorial explains the essentials of Block operations together with many examples.
10
11\b Table \b of \b contents
12 - \ref TutorialBlockOperationsWhatIs
13 - \ref TutorialBlockOperationsFixedAndDynamicSize
14 - \ref TutorialBlockOperationsSyntax
15 - \ref TutorialBlockOperationsSyntaxColumnRows
16 - \ref TutorialBlockOperationsSyntaxCorners
17
18
19\section TutorialBlockOperationsWhatIs What are Block operations?
20Block operations are a set of functions that provide an easy way to access a set of coefficients
21inside a \b Matrix or \link ArrayBase Array \endlink. A typical example is accessing a single row or
22column within a given matrix, as well as extracting a sub-matrix from the later.
23
24Blocks are highly flexible and can be used both as \b rvalues and \b lvalues in expressions, simplifying
25the task of writing combined expressions with Eigen.
26
27\subsection TutorialBlockOperationsFixedAndDynamicSize Block operations and compile-time optimizations
28As said earlier, a block operation is a way of accessing a group of coefficients inside a Matrix or
29Array object. Eigen considers two different cases in order to provide compile-time optimization for
30block operations, regarding whether the the size of the block to be accessed is known at compile time or not.
31
32To deal with these two situations, for each type of block operation Eigen provides a default version that
33is able to work with run-time dependant block sizes and another one for block operations whose block size is
34known at compile-time.
35
36Even though both functions can be applied to fixed-size objects, it is advisable to use special block operations
37in this case, allowing Eigen to perform more optimizations at compile-time.
38
39\section TutorialBlockOperationsUsing Using block operations
40Block operations are implemented such that they are easy to use and combine with operators and other
41matrices or arrays.
42
43The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
44This function returns a block of size <tt>(m,n)</tt> whose origin is at <tt>(i,j)</tt> by using
45the following syntax:
46
47<table class="tutorial_code" align="center">
48<tr><td align="center">\b Block \b operation</td>
49<td align="center">Default \b version</td>
50<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
51<tr><td>Block of length <tt>(m,n)</tt>, starting at <tt>(i,j)</tt></td>
52 <td>\code
53MatrixXf m;
54std::cout << m.block(i,j,m,n);\endcode </td>
55 <td>\code
56Matrix3f m;
57std::cout << m.block<m,n>(i,j);\endcode </td>
58</tr>
59</table>
60
61Therefore, if we want to print the values of a block inside a matrix we can simply write:
62<table class="tutorial_code"><tr><td>
63\include Tutorial_BlockOperations_print_block.cpp
64</td>
65<td>
66Output:
67\verbinclude Tutorial_BlockOperations_print_block.out
68</td></tr></table>
69
70
71In the previous example the \link DenseBase::block() .block() \endlink function was employed
72to read the values inside matrix \p m . Blocks can also be used to perform operations and
73assignments within matrices or arrays of different size:
74
75<table class="tutorial_code"><tr><td>
76\include Tutorial_BlockOperations_block_assignment.cpp
77</td>
78<td>
79Output:
80\verbinclude Tutorial_BlockOperations_block_assignment.out
81</td></tr></table>
82
83
84Blocks can also be combined with matrices and arrays to create more complex expressions:
85
86\code
87 MatrixXf m(3,3), n(2,2);
88 MatrixXf p(3,3);
89
90 m.block(0,0,2,2) = m.block(0,0,2,2) * n + p.block(1,1,2,2);
91\endcode
92
93It is important to point out that \link DenseBase::block() .block() \endlink is the
94general case for a block operation but there are many other useful block operations,
95as described in the next section.
96
97\section TutorialBlockOperationsSyntax Block operation syntax
98The following tables show a summary of Eigen's block operations and how they are applied to
99fixed- and dynamic-sized Eigen objects.
100
101\subsection TutorialBlockOperationsSyntaxColumnRows Columns and rows
102Other extremely useful block operations are \link DenseBase::col() .col() \endlink and
103\link DenseBase::row() .row() \endlink which provide access to a
104specific row or column. This is a special case in the sense that the syntax for fixed- and
105dynamic-sized objects is exactly the same:
106
107<table class="tutorial_code" align="center">
108<tr><td align="center">\b Block \b operation</td>
109<td align="center">Default version</td>
110<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
111<tr><td>i<sup>th</sup> row
112 \link DenseBase::row() * \endlink</td>
113 <td>\code
114MatrixXf m;
115std::cout << m.row(i);\endcode </td>
116 <td>\code
117Matrix3f m;
118std::cout << m.row(i);\endcode </td>
119</tr>
120<tr><td>j<sup>th</sup> column
121 \link DenseBase::col() * \endlink</td>
122 <td>\code
123MatrixXf m;
124std::cout << m.col(j);\endcode </td>
125 <td>\code
126Matrix3f m;
127std::cout << m.col(j);\endcode </td>
128</tr>
129</table>
130
131A simple example demonstrating these feature follows:
132
133<table class="tutorial_code"><tr><td>
134C++ code:
135\include Tutorial_BlockOperations_colrow.cpp
136</td>
137<td>
138Output:
139\include Tutorial_BlockOperations_colrow.out
140</td></tr></table>
141
142
143\b NOTE: the argument for \p col() and \p row() is the index of the column or row to be accessed,
144starting at 0. Therefore, \p col(0) will access the first column and \p col(1) the second one.
145
146
147\subsection TutorialBlockOperationsSyntaxCorners Corner-related operations
148<table class="tutorial_code" align="center">
149<tr><td align="center">\b Block \b operation</td>
150<td align="center">Default version</td>
151<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
152<tr><td>Top-left m by n block \link DenseBase::topLeftCorner() * \endlink</td>
153 <td>\code
154MatrixXf m;
155std::cout << m.topLeftCorner(m,n);\endcode </td>
156 <td>\code
157Matrix3f m;
158std::cout << m.topLeftCorner<m,n>();\endcode </td>
159</tr>
160<tr><td>Bottom-left m by n block
161 \link DenseBase::bottomLeftCorner() * \endlink</td>
162 <td>\code
163MatrixXf m;
164std::cout << m.bottomLeftCorner(m,n);\endcode </td>
165 <td>\code
166Matrix3f m;
167std::cout << m.bottomLeftCorner<m,n>();\endcode </td>
168</tr>
169<tr><td>Top-right m by n block
170 \link DenseBase::topRightCorner() * \endlink</td>
171 <td>\code
172MatrixXf m;
173std::cout << m.topRightCorner(m,n);\endcode </td>
174 <td>\code
175Matrix3f m;
176std::cout << m.topRightCorner<m,n>();\endcode </td>
177</tr>
178<tr><td>Bottom-right m by n block
179 \link DenseBase::bottomRightCorner() * \endlink</td>
180 <td>\code
181MatrixXf m;
182std::cout << m.bottomRightCorner(m,n);\endcode </td>
183 <td>\code
184Matrix3f m;
185std::cout << m.bottomRightCorner<m,n>();\endcode </td>
186</tr>
187<tr><td>Block containing the first n<sup>th</sup> rows
188 \link DenseBase::topRows() * \endlink</td>
189 <td>\code
190MatrixXf m;
191std::cout << m.topRows(n);\endcode </td>
192 <td>\code
193Matrix3f m;
194std::cout << m.topRows<n>();\endcode </td>
195</tr>
196<tr><td>Block containing the last n<sup>th</sup> rows
197 \link DenseBase::bottomRows() * \endlink</td>
198 <td>\code
199MatrixXf m;
200std::cout << m.bottomRows(n);\endcode </td>
201 <td>\code
202Matrix3f m;
203std::cout << m.bottomRows<n>();\endcode </td>
204</tr>
205<tr><td>Block containing the first n<sup>th</sup> columns
206 \link DenseBase::leftCols() * \endlink</td>
207 <td>\code
208MatrixXf m;
209std::cout << m.leftCols(n);\endcode </td>
210 <td>\code
211Matrix3f m;
212std::cout << m.leftCols<n>();\endcode </td>
213</tr>
214<tr><td>Block containing the last n<sup>th</sup> columns
215 \link DenseBase::rightCols() * \endlink</td>
216 <td>\code
217MatrixXf m;
218std::cout << m.rightCols(n);\endcode </td>
219 <td>\code
220Matrix3f m;
221std::cout << m.rightCols<n>();\endcode </td>
222</tr>
223</table>
224
225
226Here there is a simple example showing the power of the operations presented above:
227
228<table class="tutorial_code"><tr><td>
229C++ code:
230\include Tutorial_BlockOperations_corner.cpp
231</td>
232<td>
233Output:
234\include Tutorial_BlockOperations_corner.out
235</td></tr></table>
236
237
238
239
240
241
242
243
244\subsection TutorialBlockOperationsSyntaxVectors Block operations for vectors
245Eigen also provides a set of block operations designed specifically for vectors:
246
247<table class="tutorial_code" align="center">
248<tr><td align="center">\b Block \b operation</td>
249<td align="center">Default version</td>
250<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
251<tr><td>Block containing the first \p n <sup>th</sup> elements row
252 \link DenseBase::head() * \endlink</td>
253 <td>\code
254VectorXf v;
255std::cout << v.head(n);\endcode </td>
256 <td>\code
257Vector3f v;
258std::cout << v.head<n>();\endcode </td>
259</tr>
260<tr><td>Block containing the last \p n <sup>th</sup> elements
261 \link DenseBase::tail() * \endlink</td>
262 <td>\code
263VectorXf v;
264std::cout << v.tail(n);\endcode </td>
265 <td>\code
266Vector3f m;
267std::cout << v.tail<n>();\endcode </td>
268</tr>
269<tr><td>Block containing \p n elements, starting at position \p i
270 \link DenseBase::segment() * \endlink</td>
271 <td>\code
272VectorXf v;
273std::cout << v.segment(i,n);\endcode </td>
274 <td>\code
275Vector3f m;
276std::cout << v.segment<n>(i);\endcode </td>
277</tr>
278</table>
279
280
281An example is presented below:
282<table class="tutorial_code"><tr><td>
283C++ code:
284\include Tutorial_BlockOperations_vector.cpp
285</td>
286<td>
287Output:
288\include Tutorial_BlockOperations_vector.out
289</td></tr></table>
290
291
292*/
293
294}