namespace Eigen { /** \eigenManualPage TutorialAdvancedInitialization Advanced initialization This page discusses several advanced methods for initializing matrices. It gives more details on the comma-initializer, which was introduced before. It also explains how to get special matrices such as the identity matrix and the zero matrix. \eigenAutoToc \section TutorialAdvancedInitializationCommaInitializer The comma initializer Eigen offers a comma initializer syntax which allows the user to easily set all the coefficients of a matrix, vector or array. Simply list the coefficients, starting at the top-left corner and moving from left to right and from the top to the bottom. The size of the object needs to be specified beforehand. If you list too few or too many coefficients, Eigen will complain.
Example:Output:
\include Tutorial_commainit_01.cpp \verbinclude Tutorial_commainit_01.out
Moreover, the elements of the initialization list may themselves be vectors or matrices. A common use is to join vectors or matrices together. For example, here is how to join two row vectors together. Remember that you have to set the size before you can use the comma initializer.
Example:Output:
We can use the same technique to initialize matrices with a block structure.
Example:Output:
The comma initializer can also be used to fill block expressions such as m.row(i). Here is a more complicated way to get the same result as in the first example above:
Example:Output:
\include Tutorial_commainit_01b.cpp \verbinclude Tutorial_commainit_01b.out
\section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional objects. All three variants are illustrated in the following example:
Example:Output:
This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink, \link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this conveniently. The following example contrasts three ways to construct the matrix \f$J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and assignment, using static methods and the comma-initializer, or using the setXxx() methods.
The expression m + MatrixXf::Constant(3,3,1.2) constructs the 3-by-3 matrix expression with all its coefficients equal to 1.2 plus the corresponding coefficient of \a m. The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random matrix of size 2-by-3, and then multiplies this matrix on the left with \f$\bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$.