namespace Eigen {
/** \page TutorialBlockOperations Tutorial page 4 - Block operations
\ingroup Tutorial
\li \b Previous: \ref TutorialArrayClass
\li \b Next: (not yet written)
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 later.
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, regarding 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 (m,n) whose origin is at (i,j) by using
the following syntax:
\b Block \b operation |
Default version |
Optimized version when the size is known at compile time |
Top-left m by n block \link DenseBase::topLeftCorner() * \endlink |
\code
MatrixXf m;
std::cout << m.topLeftCorner(m,n);\endcode |
\code
Matrix3f m;
std::cout << m.topLeftCorner();\endcode |
Bottom-left m by n block
\link DenseBase::bottomLeftCorner() * \endlink |
\code
MatrixXf m;
std::cout << m.bottomLeftCorner(m,n);\endcode |
\code
Matrix3f m;
std::cout << m.bottomLeftCorner();\endcode |
Top-right m by n block
\link DenseBase::topRightCorner() * \endlink |
\code
MatrixXf m;
std::cout << m.topRightCorner(m,n);\endcode |
\code
Matrix3f m;
std::cout << m.topRightCorner();\endcode |
Bottom-right m by n block
\link DenseBase::bottomRightCorner() * \endlink |
\code
MatrixXf m;
std::cout << m.bottomRightCorner(m,n);\endcode |
\code
Matrix3f m;
std::cout << m.bottomRightCorner();\endcode |
Block containing the first nth rows
\link DenseBase::topRows() * \endlink |
\code
MatrixXf m;
std::cout << m.topRows(n);\endcode |
\code
Matrix3f m;
std::cout << m.topRows();\endcode |
Block containing the last nth rows
\link DenseBase::bottomRows() * \endlink |
\code
MatrixXf m;
std::cout << m.bottomRows(n);\endcode |
\code
Matrix3f m;
std::cout << m.bottomRows();\endcode |
Block containing the first nth columns
\link DenseBase::leftCols() * \endlink |
\code
MatrixXf m;
std::cout << m.leftCols(n);\endcode |
\code
Matrix3f m;
std::cout << m.leftCols();\endcode |
Block containing the last nth columns
\link DenseBase::rightCols() * \endlink |
\code
MatrixXf m;
std::cout << m.rightCols(n);\endcode |
\code
Matrix3f m;
std::cout << m.rightCols();\endcode |
Here there is a simple example showing the power of the operations presented above:
\b Block \b operation |
Default version |
Optimized version when the size is known at compile time |
Block containing the first \p n th elements row
\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 th 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: