aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/C04_TutorialBlockOperations.dox
blob: 70773a4635a11c5ff897eedcdc8bc151560e38b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
namespace Eigen {

/** \page TutorialBlockOperations Tutorial page 4 - %Block operations
    \ingroup Tutorial

\li \b Previous: \ref TutorialArrayClass
\li \b Next: \ref TutorialAdvancedInitialization

This tutorial page explains the essentials of block operations.
A block is a rectangular part of a matrix or array. Blocks expressions can be used both
as rvalues and as lvalues. As usual with Eigen expressions, this abstraction has zero runtime cost
provided that you let your compiler optimize.

\b Table \b of \b contents
  - \ref TutorialBlockOperationsUsing
  - \ref TutorialBlockOperationsSyntax
     - \ref TutorialBlockOperationsSyntaxColumnRows
     - \ref TutorialBlockOperationsSyntaxCorners

\section TutorialBlockOperationsUsing Using block operations

The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
This function returns a block of size <tt>(p,q)</tt> whose origin is at <tt>(i,j)</tt> by using 
the following syntax:

<table class="tutorial_code" align="center">
<tr><td align="center">\b Block \b operation</td>
<td align="center">Default \b version</td>
<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
<tr><td>Block of size <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td>
    <td>\code
matrix.block(i,j,p,q);\endcode </td>
    <td>\code 
matrix.block<p,q>(i,j);\endcode </td>
</tr>
</table>

Therefore, if we want to print the values of a block inside a matrix we can simply write:
<table class="tutorial_code"><tr><td>
\include Tutorial_BlockOperations_print_block.cpp
</td>
<td>
Output:
\verbinclude Tutorial_BlockOperations_print_block.out
</td></tr></table>


In the previous example the \link DenseBase::block() .block() \endlink function was employed 
to read the values inside matrix \p m . Blocks can also be used to perform operations and 
assignments within matrices or arrays of different size:

<table class="tutorial_code"><tr><td>
\include Tutorial_BlockOperations_block_assignment.cpp
</td>
<td>
Output:
\verbinclude Tutorial_BlockOperations_block_assignment.out
</td></tr></table>


Blocks can also be combined with matrices and arrays to create more complex expressions:

\code
  MatrixXf m(3,3), n(2,2);
  MatrixXf p(3,3);
  
  m.block(0,0,2,2) = m.block(0,0,2,2) * n + p.block(1,1,2,2);
\endcode

It is important to point out that \link DenseBase::block() .block() \endlink is the 
general case for a block operation but there are many other useful block operations, 
as described in the next section.

\section TutorialBlockOperationsSyntax Block operation syntax
The following tables show a summary of Eigen's block operations and how they are applied to 
fixed- and dynamic-sized Eigen objects.

\subsection TutorialBlockOperationsSyntaxColumnRows Columns and rows
Other extremely useful block operations are \link DenseBase::col() .col() \endlink and 
\link DenseBase::row() .row() \endlink which provide access to a 
specific row or column. This is a special case in the sense that the syntax for fixed- and 
dynamic-sized objects is exactly the same:

<table class="tutorial_code" align="center">
<tr><td align="center">\b Block \b operation</td>
<td align="center">Default version</td>
<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
<tr><td>i<sup>th</sup> row
                    \link DenseBase::row() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.row(i);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.row(i);\endcode </td>
</tr>
<tr><td>j<sup>th</sup> column
                    \link DenseBase::col() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.col(j);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.col(j);\endcode </td>
</tr>
</table>

A simple example demonstrating these feature follows:

<table class="tutorial_code"><tr><td>
C++ code:
\include Tutorial_BlockOperations_colrow.cpp
</td>
<td>
Output:
\include Tutorial_BlockOperations_colrow.out
</td></tr></table>


\b NOTE: the argument for \p col() and \p row() is the index of the column or row to be accessed, 
starting at 0. Therefore, \p col(0) will access the first column and \p col(1) the second one.


\subsection TutorialBlockOperationsSyntaxCorners Corner-related operations
<table class="tutorial_code" align="center">
<tr><td align="center">\b Block \b operation</td>
<td align="center">Default version</td>
<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.topLeftCorner(p,q);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.topLeftCorner<p,q>();\endcode </td>
</tr>
<tr><td>Bottom-left p by q block
              \link DenseBase::bottomLeftCorner() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.bottomLeftCorner(p,q);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.bottomLeftCorner<p,q>();\endcode </td>
</tr>
<tr><td>Top-right p by q block
              \link DenseBase::topRightCorner() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.topRightCorner(p,q);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.topRightCorner<p,q>();\endcode </td>
</tr>
<tr><td>Bottom-right p by q block
               \link DenseBase::bottomRightCorner() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.bottomRightCorner(p,q);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.bottomRightCorner<p,q>();\endcode </td>
</tr>
<tr><td>Block containing the first q rows
                   \link DenseBase::topRows() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.topRows(q);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.topRows<q>();\endcode </td>
</tr>
<tr><td>Block containing the last q rows
                    \link DenseBase::bottomRows() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.bottomRows(q);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.bottomRows<q>();\endcode </td>
</tr>
<tr><td>Block containing the first p columns
                    \link DenseBase::leftCols() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.leftCols(p);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.leftCols<p>();\endcode </td>
</tr>
<tr><td>Block containing the last q columns
                    \link DenseBase::rightCols() * \endlink</td>
    <td>\code
MatrixXf m;
std::cout << m.rightCols(q);\endcode </td>
    <td>\code 
Matrix3f m;
std::cout << m.rightCols<q>();\endcode </td>
</tr>
</table>


Here is a simple example showing the power of the operations presented above:

<table class="tutorial_code"><tr><td>
C++ code:
\include Tutorial_BlockOperations_corner.cpp
</td>
<td>
Output:
\include Tutorial_BlockOperations_corner.out
</td></tr></table>








\subsection TutorialBlockOperationsSyntaxVectors Block operations for vectors
Eigen also provides a set of block operations designed specifically for vectors:

<table class="tutorial_code" align="center">
<tr><td align="center">\b Block \b operation</td>
<td align="center">Default version</td>
<td align="center">Optimized version when the<br>size is known at compile time</td></tr>
<tr><td>Block containing the first \p n elements 
                    \link DenseBase::head() * \endlink</td>
    <td>\code
VectorXf v;
std::cout << v.head(n);\endcode </td>
    <td>\code 
Vector3f v;
std::cout << v.head<n>();\endcode </td>
</tr>
<tr><td>Block containing the last \p n elements
                    \link DenseBase::tail() * \endlink</td>
    <td>\code
VectorXf v;
std::cout << v.tail(n);\endcode </td>
    <td>\code 
Vector3f m;
std::cout << v.tail<n>();\endcode </td>
</tr>
<tr><td>Block containing \p n elements, starting at position \p i
                    \link DenseBase::segment() * \endlink</td>
    <td>\code
VectorXf v;
std::cout << v.segment(i,n);\endcode </td>
    <td>\code 
Vector3f m;
std::cout << v.segment<n>(i);\endcode </td>
</tr>
</table>


An example is presented below:
<table class="tutorial_code"><tr><td>
C++ code:
\include Tutorial_BlockOperations_vector.cpp
</td>
<td>
Output:
\include Tutorial_BlockOperations_vector.out
</td></tr></table>

\li \b Next: \ref TutorialAdvancedInitialization

*/

}