blob: a2d8c97cc094f62b0bd64278658272792362295a [file] [log] [blame]
 namespace Eigen { /** \eigenManualPage TutorialBlockOperations Block operations This 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. \eigenAutoToc \section TutorialBlockOperationsUsing Using block operations The most general block operation in Eigen is called \link DenseBase::block() .block() \endlink. There are two versions, whose syntax is as follows:
\b %Block \b operation Version constructing a \n dynamic-size block expression Version constructing a \n fixed-size block expression
%Block of size (p,q), starting at (i,j) \code matrix.block(i,j,p,q);\endcode \code matrix.block(i,j);\endcode
As always in Eigen, indices start at 0. 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 matrix.
Example:Output:
\include Tutorial_BlockOperations_print_block.cpp \verbinclude Tutorial_BlockOperations_print_block.out
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. This is illustrated in the following example. This example also demonstrates blocks in arrays, which works exactly like the above-demonstrated blocks in matrices.
Example:Output:
\include Tutorial_BlockOperations_block_assignment.cpp \verbinclude Tutorial_BlockOperations_block_assignment.out
%Block operation Method
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.
Example:Output:
\include Tutorial_BlockOperations_colrow.cpp \verbinclude Tutorial_BlockOperations_colrow.out
That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression. \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 to a block in the top-left corner of a matrix. The different possibilities are summarized in the following table:
%Block \b operation Version constructing a \n dynamic-size block expression Version constructing a \n fixed-size block expression
Top-left p by q block \link DenseBase::topLeftCorner() * \endlink \code matrix.topLeftCorner(p,q);\endcode \code matrix.topLeftCorner();\endcode
Bottom-left p by q block \link DenseBase::bottomLeftCorner() * \endlink \code matrix.bottomLeftCorner(p,q);\endcode \code matrix.bottomLeftCorner();\endcode
Top-right p by q block \link DenseBase::topRightCorner() * \endlink \code matrix.topRightCorner(p,q);\endcode \code matrix.topRightCorner();\endcode
Bottom-right p by q block \link DenseBase::bottomRightCorner() * \endlink \code matrix.bottomRightCorner(p,q);\endcode \code matrix.bottomRightCorner();\endcode
%Block containing the first q rows \link DenseBase::topRows() * \endlink \code matrix.topRows(q);\endcode \code matrix.topRows();\endcode
%Block containing the last q rows \link DenseBase::bottomRows() * \endlink \code matrix.bottomRows(q);\endcode \code matrix.bottomRows();\endcode
%Block containing the first p columns \link DenseBase::leftCols() * \endlink \code matrix.leftCols(p);\endcode \code matrix.leftCols

();\endcode

%Block containing the last q columns \link DenseBase::rightCols() * \endlink \code matrix.rightCols(q);\endcode \code matrix.rightCols();\endcode
Here is a simple example illustrating the use of the operations presented above:
Example:Output:
\include Tutorial_BlockOperations_corner.cpp \verbinclude Tutorial_BlockOperations_corner.out
\section TutorialBlockOperationsSyntaxVectors Block operations for vectors Eigen also provides a set of block operations designed specifically for the special case of vectors and one-dimensional arrays:
%Block operation Version constructing a \n dynamic-size block expression Version constructing a \n fixed-size block expression