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 (p,q) whose origin is at (i,j) by using the following syntax:
\b Block \b operation Default \b version Optimized version when the
size is known at compile time
Block of size (p,q), starting at (i,j) \code matrix.block(i,j,p,q);\endcode \code matrix.block(i,j);\endcode
Therefore, if we want to print the values of a block inside a matrix we can simply write:
\include Tutorial_BlockOperations_print_block.cpp Output: \verbinclude Tutorial_BlockOperations_print_block.out
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:
\include Tutorial_BlockOperations_block_assignment.cpp Output: \verbinclude Tutorial_BlockOperations_block_assignment.out
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:
\b Block \b operation Default version Optimized version when the
size is known at compile time
ith row \link DenseBase::row() * \endlink \code MatrixXf m; std::cout << m.row(i);\endcode \code Matrix3f m; std::cout << m.row(i);\endcode
jth column \link DenseBase::col() * \endlink \code MatrixXf m; std::cout << m.col(j);\endcode \code Matrix3f m; std::cout << m.col(j);\endcode
A simple example demonstrating these feature follows:
C++ code: \include Tutorial_BlockOperations_colrow.cpp Output: \include Tutorial_BlockOperations_colrow.out
\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
\b Block \b operation Default version Optimized version when the
size is known at compile time
Top-left p by q block \link DenseBase::topLeftCorner() * \endlink \code MatrixXf m; std::cout << m.topLeftCorner(p,q);\endcode \code Matrix3f m; std::cout << m.topLeftCorner();\endcode
Bottom-left p by q block \link DenseBase::bottomLeftCorner() * \endlink \code MatrixXf m; std::cout << m.bottomLeftCorner(p,q);\endcode \code Matrix3f m; std::cout << m.bottomLeftCorner();\endcode
Top-right p by q block \link DenseBase::topRightCorner() * \endlink \code MatrixXf m; std::cout << m.topRightCorner(p,q);\endcode \code Matrix3f m; std::cout << m.topRightCorner();\endcode
Bottom-right p by q block \link DenseBase::bottomRightCorner() * \endlink \code MatrixXf m; std::cout << m.bottomRightCorner(p,q);\endcode \code Matrix3f m; std::cout << m.bottomRightCorner();\endcode
Block containing the first q rows \link DenseBase::topRows() * \endlink \code MatrixXf m; std::cout << m.topRows(q);\endcode \code Matrix3f m; std::cout << m.topRows();\endcode
Block containing the last q rows \link DenseBase::bottomRows() * \endlink \code MatrixXf m; std::cout << m.bottomRows(q);\endcode \code Matrix3f m; std::cout << m.bottomRows();\endcode
Block containing the first p columns \link DenseBase::leftCols() * \endlink \code MatrixXf m; std::cout << m.leftCols(p);\endcode \code Matrix3f m; std::cout << m.leftCols

();\endcode

Block containing the last q columns \link DenseBase::rightCols() * \endlink \code MatrixXf m; std::cout << m.rightCols(q);\endcode \code Matrix3f m; std::cout << m.rightCols();\endcode
Here is a simple example showing the power of the operations presented above:
C++ code: \include Tutorial_BlockOperations_corner.cpp Output: \include Tutorial_BlockOperations_corner.out
\subsection TutorialBlockOperationsSyntaxVectors Block operations for vectors Eigen also provides a set of block operations designed specifically for vectors:
\b Block \b operation Default version Optimized version when the
size is known at compile time
Block containing the first \p n elements \link DenseBase::head() * \endlink \code VectorXf v; std::cout << v.head(n);\endcode \code Vector3f v; std::cout << v.head();\endcode
Block containing the last \p n elements \link DenseBase::tail() * \endlink \code VectorXf v; std::cout << v.tail(n);\endcode \code Vector3f m; std::cout << v.tail();\endcode
Block containing \p n elements, starting at position \p i \link DenseBase::segment() * \endlink \code VectorXf v; std::cout << v.segment(i,n);\endcode \code Vector3f m; std::cout << v.segment(i);\endcode
An example is presented below:
C++ code: \include Tutorial_BlockOperations_vector.cpp Output: \include Tutorial_BlockOperations_vector.out
\li \b Next: \ref TutorialAdvancedInitialization */ }