namespace Eigen { /** \page TutorialBlockOperations Tutorial page 4 - %Block operations \ingroup Tutorial \li \b Previous: \ref TutorialArrayClass \li \b Next: \ref TutorialAdvancedInitialization This tutorial explains the essentials of Block operations together with many examples. \b Table \b of \b contents - \ref TutorialBlockOperationsWhatIs - \ref TutorialBlockOperationsFixedAndDynamicSize - \ref TutorialBlockOperationsSyntax - \ref TutorialBlockOperationsSyntaxColumnRows - \ref TutorialBlockOperationsSyntaxCorners \section TutorialBlockOperationsWhatIs What are Block operations? Block operations are a set of functions that provide an easy way to access a set of coefficients inside a \b Matrix or \link ArrayBase Array \endlink. A typical example is accessing a single row or column within a given matrix, as well as extracting a sub-matrix from the latter. Blocks are highly flexible and can be used both as \b rvalues and \b lvalues in expressions, simplifying the task of writing combined expressions with Eigen. \subsection TutorialBlockOperationsFixedAndDynamicSize Block operations and compile-time optimizations As said earlier, a block operation is a way of accessing a group of coefficients inside a Matrix or Array object. Eigen considers two different cases in order to provide compile-time optimization for block operations, depending on whether the the size of the block to be accessed is known at compile time or not. To deal with these two situations, for each type of block operation Eigen provides a default version that is able to work with run-time dependant block sizes and another one for block operations whose block size is known at compile-time. Even though both functions can be applied to fixed-size objects, it is advisable to use special block operations in this case, allowing Eigen to perform more optimizations at compile-time. \section TutorialBlockOperationsUsing Using block operations Block operations are implemented such that they are easy to use and combine with operators and other matrices or arrays. 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 length (p,q), starting at (i,j) | \code MatrixXf m; std::cout << m.block(i,j,p,q);\endcode | \code
Matrix3f m;
std::cout << m.block (i,j);\endcode |
\include Tutorial_BlockOperations_print_block.cpp | Output: \verbinclude Tutorial_BlockOperations_print_block.out |
\include Tutorial_BlockOperations_block_assignment.cpp | Output: \verbinclude Tutorial_BlockOperations_block_assignment.out |
\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 |
C++ code: \include Tutorial_BlockOperations_colrow.cpp | Output: \include Tutorial_BlockOperations_colrow.out |
\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 |
C++ code: \include Tutorial_BlockOperations_corner.cpp | Output: \include Tutorial_BlockOperations_corner.out |
\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 |
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 |
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 |
C++ code: \include Tutorial_BlockOperations_vector.cpp | Output: \include Tutorial_BlockOperations_vector.out |