blob: db84f94a76e7cf0dec3954c1bb04ff9f56b558ea [file] [log] [blame]
Benoit Jacobe078bb22010-06-26 14:00:00 -04001namespace Eigen {
2
Benoit Jacob4d4a23c2010-06-30 10:11:55 -04003/** \page TutorialAdvancedInitialization Tutorial page 5 - Advanced initialization
Benoit Jacobe078bb22010-06-26 14:00:00 -04004 \ingroup Tutorial
5
Benoit Jacob4d4a23c2010-06-30 10:11:55 -04006\li \b Previous: \ref TutorialBlockOperations
7\li \b Next: \ref TutorialLinearAlgebra
8
Jitse Niesen403e6722010-07-22 15:53:21 +01009This page discusses several advanced methods for initializing matrices. It gives more details on the
10comma-initializer, which was introduced before. It also explains how to get special matrices such as the
11identity matrix and the zero matrix.
Benoit Jacobe078bb22010-06-26 14:00:00 -040012
Jitse Niesen403e6722010-07-22 15:53:21 +010013\b Table \b of \b contents
14 - \ref TutorialAdvancedInitializationCommaInitializer
15 - \ref TutorialAdvancedInitializationSpecialMatrices
16 - \ref TutorialAdvancedInitializationTemporaryObjects
17
18
19\section TutorialAdvancedInitializationCommaInitializer The comma initializer
20
21Eigen offers a comma initializer syntax which allows the user to easily set all the coefficients of a matrix,
22vector or array. Simply list the coefficients, starting at the top-left corner and moving from left to right
23and from the top to the bottom. The size of the object needs to be specified beforehand. If you list too few
24or too many coefficients, Eigen will complain.
25
26<table class="tutorial_code"><tr><td>
27Example: \include Tutorial_commainit_01.cpp
28</td>
29<td>
Benoit Jacobe078bb22010-06-26 14:00:00 -040030Output: \verbinclude Tutorial_commainit_01.out
Jitse Niesen403e6722010-07-22 15:53:21 +010031</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040032
Jitse Niesen403e6722010-07-22 15:53:21 +010033The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more
34complicated way to get the same result as above:
Benoit Jacobe078bb22010-06-26 14:00:00 -040035
Jitse Niesen403e6722010-07-22 15:53:21 +010036<table class="tutorial_code"><tr><td>
37Example: \include Tutorial_commainit_01b.cpp
38</td>
39<td>
40Output: \verbinclude Tutorial_commainit_01b.out
41</td></tr></table>
42
43Moreover, the elements of the initialization list may themselves be matrices. Thus, we can use them to
44initialize matrices with a block structure.
45
46<table class="tutorial_code"><tr><td>
47Example: \include Tutorial_AdvancedInitialization_Block.cpp
48</td>
49<td>
50Output: \verbinclude Tutorial_AdvancedInitialization_Block.out
51</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040052
53
Jitse Niesen403e6722010-07-22 15:53:21 +010054\section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays
Benoit Jacobe078bb22010-06-26 14:00:00 -040055
Jitse Niesen403e6722010-07-22 15:53:21 +010056The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be
57used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments
58and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need
59to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional
60dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional
61objects. All three variants are illustrated in the following example:
62
63<table class="tutorial_code"><tr><td>
64Example: \include Tutorial_AdvancedInitialization_Zero.cpp
65</td>
66<td>
67Output: \verbinclude Tutorial_AdvancedInitialization_Zero.out
68</td></tr></table>
69
70Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c
71value. If the size of the object needs to be specified, the additional arguments go before the \c value
72argument, as in <tt>MatrixXd::Constant(rows, cols, value)</tt>. The method \link DenseBase::Random() Random()
73\endlink fills the matrix or array with random coefficients. The identity matrix can be obtained by calling
74\link MatrixBase::Identity() Identity()\endlink; this method is only available for Matrix, not for Array,
75because "identity matrix" is a linear algebra concept. The method
76\link DenseBase::LinSpaced LinSpaced\endlink(low, high, size) is only available for vectors and
77one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between
78\c low and \c high. The method \c LinSpaced() is illustrated in the following example, which prints a table
79with angles in degrees, the corresponding angle in radians, and their sine and cosine.
80
81<table class="tutorial_code"><tr><td>
82Example: \include Tutorial_AdvancedInitialization_LinSpaced.cpp
83</td>
84<td>
85Output: \verbinclude Tutorial_AdvancedInitialization_LinSpaced.out
86</td></tr></table>
87
88This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and
89expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink,
Jitse Niesen1420f8b2010-07-25 20:29:07 +010090\link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this
Jitse Niesen403e6722010-07-22 15:53:21 +010091conveniently. The following example contrasts three ways to construct the matrix
92\f$ J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and
93assignment, using static methods and the comma-initializer, or using the setXxx() methods.
94
95<table class="tutorial_code"><tr><td>
96Example: \include Tutorial_AdvancedInitialization_ThreeWays.cpp
97</td>
98<td>
99Output: \verbinclude Tutorial_AdvancedInitialization_ThreeWays.out
100</td></tr></table>
101
102A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage.
103
104
105\section TutorialAdvancedInitializationTemporaryObjects Temporary matrices and arrays
106
107As shown above, static methods as Zero() and Constant() can be used to initialize to variables at the time of
108declaration or at the right-hand side of an assignment operator. You can think of these methods as returning a
109matrix or array (in fact, they return a so-called \ref TopicEigenExpressionTemplates "expression object" which
110evaluates to a matrix when needed). This matrix can also be used as a temporary object. The second example in
111the \ref GettingStarted guide, which we reproduced here, already illustrates this.
112
113<table class="tutorial_code"><tr><td>
114Example: \include QuickStart_example2_dynamic.cpp
115</td>
116<td>
117Output: \verbinclude QuickStart_example2_dynamic.out
118</td></tr></table>
119
120The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix with all its coefficients
121equal to 1.2 and adds it to \c m ; in other words, it adds 1.2 to all the coefficients of \c m . The
122comma-initializer can also be used to construct temporary objects. The following example constructs a random
123matrix of size 2-by-3, and then multiplies this matrix on the left with
124\f$ \bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$.
125
126<table class="tutorial_code"><tr><td>
127Example: \include Tutorial_AdvancedInitialization_CommaTemporary.cpp
128</td>
129<td>
130Output: \verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out
131</td></tr></table>
132
133The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix
134object once the comma initialization of our temporary submatrix is done.
135
Benoit Jacobe078bb22010-06-26 14:00:00 -0400136
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400137\li \b Next: \ref TutorialLinearAlgebra
138
Benoit Jacobe078bb22010-06-26 14:00:00 -0400139*/
140
141}