2010-06-29 01:42:59 +08:00
namespace Eigen {
2010-06-30 22:11:55 +08:00
/** \page TutorialBlockOperations Tutorial page 4 - %Block operations
2010-06-29 01:42:59 +08:00
\ingroup Tutorial
\li \b Previous: \ref TutorialArrayClass
2010-06-30 22:11:55 +08:00
\li \b Next: \ref TutorialAdvancedInitialization
2010-06-29 01:42:59 +08:00
2010-07-02 08:52:40 +08:00
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.
2010-06-29 01:42:59 +08:00
\b Table \b of \b contents
2010-07-02 08:52:40 +08:00
- \ref TutorialBlockOperationsUsing
2010-07-14 17:16:12 +08:00
- \ref TutorialBlockOperationsSyntaxColumnRows
- \ref TutorialBlockOperationsSyntaxCorners
- \ref TutorialBlockOperationsSyntaxVectors
2010-06-29 01:42:59 +08:00
\section TutorialBlockOperationsUsing Using block operations
The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink.
2010-07-14 17:16:12 +08:00
There are two versions, whose syntax is as follows:
2010-06-29 01:42:59 +08:00
2010-10-19 17:40:49 +08:00
<table class="manual">
<tr><th>\b %Block \b operation</td>
<th>Version constructing a \n dynamic-size block expression</th>
<th>Version constructing a \n fixed-size block expression</th></tr>
2010-07-14 17:16:12 +08:00
<tr><td>%Block of size <tt>(p,q)</tt>, starting at <tt>(i,j)</tt></td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-02 08:52:40 +08:00
matrix.block(i,j,p,q);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-02 08:52:40 +08:00
matrix.block<p,q>(i,j);\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
</table>
2010-10-18 20:44:27 +08:00
As always in Eigen, indices start at 0.
2010-07-14 17:16:12 +08:00
2010-10-18 20:44:27 +08:00
Both versions can be used on fixed-size and dynamic-size matrices and arrays.
These two expressions are semantically equivalent.
The only difference is that the fixed-size version will typically give you faster code if the block size is small,
but requires this size to be known at compile time.
The following program uses the dynamic-size and fixed-size versions to print the values of several blocks inside a
2010-07-14 17:16:12 +08:00
matrix.
2010-10-19 17:40:49 +08:00
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
2010-06-29 01:42:59 +08:00
\include Tutorial_BlockOperations_print_block.cpp
</td>
<td>
\verbinclude Tutorial_BlockOperations_print_block.out
</td></tr></table>
2010-10-18 20:44:27 +08:00
In the above example the \link DenseBase::block() .block() \endlink function was employed as a \em rvalue, i.e.
it was only read from. However, blocks can also be used as \em lvalues, meaning that you can assign to a block.
2010-06-29 01:42:59 +08:00
2010-10-18 20:44:27 +08:00
This is illustrated in the following example. This example also demonstrates blocks in arrays, which works exactly like the above-demonstrated blocks in matrices.
2010-06-29 01:42:59 +08:00
2010-10-19 17:40:49 +08:00
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
2010-06-29 01:42:59 +08:00
\include Tutorial_BlockOperations_block_assignment.cpp
</td>
<td>
\verbinclude Tutorial_BlockOperations_block_assignment.out
</td></tr></table>
2010-10-18 20:44:27 +08:00
While the \link DenseBase::block() .block() \endlink method can be used for any block operation, there are
other methods for special cases, providing more specialized API and/or better performance. On the topic of performance, all what
matters 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,
using the specialized \link DenseBase::col() .col() \endlink function described below lets Eigen know that, which can give it optimization opportunities.
2010-06-29 01:42:59 +08:00
2010-10-18 20:44:27 +08:00
The rest of this page describes these specialized methods.
2010-06-29 01:42:59 +08:00
2010-07-14 17:16:12 +08:00
\section TutorialBlockOperationsSyntaxColumnRows Columns and rows
2010-06-29 01:42:59 +08:00
2010-10-18 20:44:27 +08:00
Individual columns and rows are special cases of blocks. Eigen provides methods to easily address them:
\link DenseBase::col() .col() \endlink and \link DenseBase::row() .row()\endlink.
2010-06-29 01:42:59 +08:00
2010-10-19 17:40:49 +08:00
<table class="manual">
<tr><th>%Block operation</th>
<th>Method</th>
2010-06-29 01:42:59 +08:00
<tr><td>i<sup>th</sup> row
\link DenseBase::row() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.row(i);\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
<tr><td>j<sup>th</sup> column
\link DenseBase::col() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.col(j);\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
</table>
2010-10-18 20:44:27 +08:00
The 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.
2010-06-29 01:42:59 +08:00
2010-10-19 17:40:49 +08:00
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
2010-06-29 01:42:59 +08:00
\include Tutorial_BlockOperations_colrow.cpp
</td>
<td>
2010-07-14 17:16:12 +08:00
\verbinclude Tutorial_BlockOperations_colrow.out
2010-06-29 01:42:59 +08:00
</td></tr></table>
2010-10-18 20:44:27 +08:00
That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression.
2010-06-29 01:42:59 +08:00
2010-07-14 17:16:12 +08:00
\section TutorialBlockOperationsSyntaxCorners Corner-related operations
Eigen also provides special methods for blocks that are flushed against one of the corners or sides of a
matrix or array. For instance, \link DenseBase::topLeftCorner() .topLeftCorner() \endlink can be used to refer
2010-10-18 20:44:27 +08:00
to a block in the top-left corner of a matrix.
2010-06-29 01:42:59 +08:00
2010-07-14 17:16:12 +08:00
The different possibilities are summarized in the following table:
2010-06-29 01:42:59 +08:00
2010-10-19 17:40:49 +08:00
<table class="manual">
<tr><th>%Block \b operation</td>
<th>Version constructing a \n dynamic-size block expression</th>
<th>Version constructing a \n fixed-size block expression</th></tr>
2010-06-29 18:42:51 +08:00
<tr><td>Top-left p by q block \link DenseBase::topLeftCorner() * \endlink</td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.topLeftCorner(p,q);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.topLeftCorner<p,q>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-06-29 18:42:51 +08:00
<tr><td>Bottom-left p by q block
2010-06-29 01:42:59 +08:00
\link DenseBase::bottomLeftCorner() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.bottomLeftCorner(p,q);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.bottomLeftCorner<p,q>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-06-29 18:42:51 +08:00
<tr><td>Top-right p by q block
2010-06-29 01:42:59 +08:00
\link DenseBase::topRightCorner() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.topRightCorner(p,q);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.topRightCorner<p,q>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-06-29 18:42:51 +08:00
<tr><td>Bottom-right p by q block
2010-06-29 01:42:59 +08:00
\link DenseBase::bottomRightCorner() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.bottomRightCorner(p,q);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.bottomRightCorner<p,q>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-07-14 17:16:12 +08:00
<tr><td>%Block containing the first q rows
2010-06-29 01:42:59 +08:00
\link DenseBase::topRows() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.topRows(q);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.topRows<q>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-07-14 17:16:12 +08:00
<tr><td>%Block containing the last q rows
2010-06-29 01:42:59 +08:00
\link DenseBase::bottomRows() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.bottomRows(q);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.bottomRows<q>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-07-14 17:16:12 +08:00
<tr><td>%Block containing the first p columns
2010-06-29 01:42:59 +08:00
\link DenseBase::leftCols() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.leftCols(p);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.leftCols<p>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-07-14 17:16:12 +08:00
<tr><td>%Block containing the last q columns
2010-06-29 01:42:59 +08:00
\link DenseBase::rightCols() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
matrix.rightCols(q);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
matrix.rightCols<q>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
</table>
2010-07-14 17:16:12 +08:00
Here is a simple example illustrating the use of the operations presented above:
2010-06-29 01:42:59 +08:00
2010-10-19 17:40:49 +08:00
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
2010-06-29 01:42:59 +08:00
\include Tutorial_BlockOperations_corner.cpp
</td>
<td>
2010-07-14 17:16:12 +08:00
\verbinclude Tutorial_BlockOperations_corner.out
2010-06-29 01:42:59 +08:00
</td></tr></table>
2010-07-14 17:16:12 +08:00
\section TutorialBlockOperationsSyntaxVectors Block operations for vectors
2010-06-29 01:42:59 +08:00
2010-10-18 20:44:27 +08:00
Eigen also provides a set of block operations designed specifically for the special case of vectors and one-dimensional arrays:
2010-06-29 01:42:59 +08:00
2010-10-19 17:40:49 +08:00
<table class="manual">
<tr><th> %Block operation</th>
<th>Version constructing a \n dynamic-size block expression</th>
<th>Version constructing a \n fixed-size block expression</th></tr>
2010-07-14 17:16:12 +08:00
<tr><td>%Block containing the first \p n elements
2010-06-29 01:42:59 +08:00
\link DenseBase::head() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
vector.head(n);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
vector.head<n>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-07-14 17:16:12 +08:00
<tr><td>%Block containing the last \p n elements
2010-06-29 01:42:59 +08:00
\link DenseBase::tail() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
vector.tail(n);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
vector.tail<n>();\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
2010-07-14 17:16:12 +08:00
<tr><td>%Block containing \p n elements, starting at position \p i
2010-06-29 01:42:59 +08:00
\link DenseBase::segment() * \endlink</td>
<td>\code
2010-07-14 17:16:12 +08:00
vector.segment(i,n);\endcode </td>
2010-06-29 01:42:59 +08:00
<td>\code
2010-07-14 17:16:12 +08:00
vector.segment<n>(i);\endcode </td>
2010-06-29 01:42:59 +08:00
</tr>
</table>
An example is presented below:
2010-10-19 17:40:49 +08:00
<table class="example">
<tr><th>Example:</th><th>Output:</th></tr>
<tr><td>
2010-06-29 01:42:59 +08:00
\include Tutorial_BlockOperations_vector.cpp
</td>
<td>
2010-07-14 17:16:12 +08:00
\verbinclude Tutorial_BlockOperations_vector.out
2010-06-29 01:42:59 +08:00
</td></tr></table>
2010-06-30 22:11:55 +08:00
\li \b Next: \ref TutorialAdvancedInitialization
2010-06-29 01:42:59 +08:00
*/
}