blob: 50374d0d0b4b3bdb9664f9ce8b0e95771b897ead [file] [log] [blame]
Benoit Jacobe078bb22010-06-26 14:00:00 -04001namespace Eigen {
2
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01003/** \eigenManualPage TutorialAdvancedInitialization Advanced initialization
Benoit Jacobe078bb22010-06-26 14:00:00 -04004
Jitse Niesen403e6722010-07-22 15:53:21 +01005This page discusses several advanced methods for initializing matrices. It gives more details on the
6comma-initializer, which was introduced before. It also explains how to get special matrices such as the
7identity matrix and the zero matrix.
Benoit Jacobe078bb22010-06-26 14:00:00 -04008
Gael Guennebaud93ee82b2013-01-05 16:37:11 +01009\eigenAutoToc
Jitse Niesen403e6722010-07-22 15:53:21 +010010
11\section TutorialAdvancedInitializationCommaInitializer The comma initializer
12
13Eigen offers a comma initializer syntax which allows the user to easily set all the coefficients of a matrix,
14vector or array. Simply list the coefficients, starting at the top-left corner and moving from left to right
15and from the top to the bottom. The size of the object needs to be specified beforehand. If you list too few
16or too many coefficients, Eigen will complain.
17
Gael Guennebaudf66fe262010-10-19 11:40:49 +020018<table class="example">
19<tr><th>Example:</th><th>Output:</th></tr>
20<tr><td>
21\include Tutorial_commainit_01.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010022</td>
23<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020024\verbinclude Tutorial_commainit_01.out
Jitse Niesen403e6722010-07-22 15:53:21 +010025</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040026
Jitse Niesen8bca23b2011-02-12 23:17:31 +000027Moreover, the elements of the initialization list may themselves be vectors or matrices. A common use is
28to join vectors or matrices together. For example, here is how to join two row vectors together. Remember
29that you have to set the size before you can use the comma initializer.
Benoit Jacobe078bb22010-06-26 14:00:00 -040030
Gael Guennebaudf66fe262010-10-19 11:40:49 +020031<table class="example">
32<tr><th>Example:</th><th>Output:</th></tr>
33<tr><td>
Jitse Niesen8bca23b2011-02-12 23:17:31 +000034\include Tutorial_AdvancedInitialization_Join.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010035</td>
36<td>
Jitse Niesen8bca23b2011-02-12 23:17:31 +000037\verbinclude Tutorial_AdvancedInitialization_Join.out
Jitse Niesen403e6722010-07-22 15:53:21 +010038</td></tr></table>
39
Jitse Niesen8bca23b2011-02-12 23:17:31 +000040We can use the same technique to initialize matrices with a block structure.
Jitse Niesen403e6722010-07-22 15:53:21 +010041
Gael Guennebaudf66fe262010-10-19 11:40:49 +020042<table class="example">
43<tr><th>Example:</th><th>Output:</th></tr>
44<tr><td>
45\include Tutorial_AdvancedInitialization_Block.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010046</td>
47<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020048\verbinclude Tutorial_AdvancedInitialization_Block.out
Jitse Niesen403e6722010-07-22 15:53:21 +010049</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040050
Jitse Niesen8bca23b2011-02-12 23:17:31 +000051The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more
52complicated way to get the same result as in the first example above:
53
54<table class="example">
55<tr><th>Example:</th><th>Output:</th></tr>
56<tr><td>
57\include Tutorial_commainit_01b.cpp
58</td>
59<td>
60\verbinclude Tutorial_commainit_01b.out
61</td></tr></table>
62
Benoit Jacobe078bb22010-06-26 14:00:00 -040063
Jitse Niesen403e6722010-07-22 15:53:21 +010064\section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays
Benoit Jacobe078bb22010-06-26 14:00:00 -040065
Jitse Niesen403e6722010-07-22 15:53:21 +010066The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be
67used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments
68and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need
69to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional
70dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional
71objects. All three variants are illustrated in the following example:
72
Gael Guennebaudf66fe262010-10-19 11:40:49 +020073<table class="example">
74<tr><th>Example:</th><th>Output:</th></tr>
75<tr><td>
76\include Tutorial_AdvancedInitialization_Zero.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010077</td>
78<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020079\verbinclude Tutorial_AdvancedInitialization_Zero.out
Jitse Niesen403e6722010-07-22 15:53:21 +010080</td></tr></table>
81
Benoit Jacob3404d5f2010-10-18 09:09:30 -040082Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c value.
83If the size of the object needs to be specified, the additional arguments go before the \c value
Jitse Niesen403e6722010-07-22 15:53:21 +010084argument, as in <tt>MatrixXd::Constant(rows, cols, value)</tt>. The method \link DenseBase::Random() Random()
85\endlink fills the matrix or array with random coefficients. The identity matrix can be obtained by calling
86\link MatrixBase::Identity() Identity()\endlink; this method is only available for Matrix, not for Array,
87because "identity matrix" is a linear algebra concept. The method
Benoit Jacob6f2ba1f2011-01-28 10:00:34 -050088\link DenseBase::LinSpaced LinSpaced\endlink(size, low, high) is only available for vectors and
Jitse Niesen403e6722010-07-22 15:53:21 +010089one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between
90\c low and \c high. The method \c LinSpaced() is illustrated in the following example, which prints a table
91with angles in degrees, the corresponding angle in radians, and their sine and cosine.
92
Gael Guennebaudf66fe262010-10-19 11:40:49 +020093<table class="example">
94<tr><th>Example:</th><th>Output:</th></tr>
95<tr><td>
96\include Tutorial_AdvancedInitialization_LinSpaced.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010097</td>
98<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020099\verbinclude Tutorial_AdvancedInitialization_LinSpaced.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100100</td></tr></table>
101
102This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and
103expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink,
Jitse Niesen1420f8b2010-07-25 20:29:07 +0100104\link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this
Jitse Niesen403e6722010-07-22 15:53:21 +0100105conveniently. The following example contrasts three ways to construct the matrix
106\f$ J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and
107assignment, using static methods and the comma-initializer, or using the setXxx() methods.
108
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200109<table class="example">
110<tr><th>Example:</th><th>Output:</th></tr>
111<tr><td>
112\include Tutorial_AdvancedInitialization_ThreeWays.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100113</td>
114<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200115\verbinclude Tutorial_AdvancedInitialization_ThreeWays.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100116</td></tr></table>
117
118A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage.
119
120
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400121\section TutorialAdvancedInitializationTemporaryObjects Usage as temporary objects
Jitse Niesen403e6722010-07-22 15:53:21 +0100122
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400123As shown above, static methods as Zero() and Constant() can be used to initialize variables at the time of
Jitse Niesen403e6722010-07-22 15:53:21 +0100124declaration or at the right-hand side of an assignment operator. You can think of these methods as returning a
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400125matrix or array; in fact, they return so-called \ref TopicEigenExpressionTemplates "expression objects" which
126evaluate to a matrix or array when needed, so that this syntax does not incur any overhead.
127
128These expressions can also be used as a temporary object. The second example in
129the \ref GettingStarted guide, which we reproduce here, already illustrates this.
Jitse Niesen403e6722010-07-22 15:53:21 +0100130
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200131<table class="example">
132<tr><th>Example:</th><th>Output:</th></tr>
133<tr><td>
134\include QuickStart_example2_dynamic.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100135</td>
136<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200137\verbinclude QuickStart_example2_dynamic.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100138</td></tr></table>
139
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400140The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix expression with all its coefficients
141equal to 1.2 plus the corresponding coefficient of \a m.
142
143The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random
Jitse Niesen403e6722010-07-22 15:53:21 +0100144matrix of size 2-by-3, and then multiplies this matrix on the left with
145\f$ \bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$.
146
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200147<table class="example">
148<tr><th>Example:</th><th>Output:</th></tr>
149<tr><td>
150\include Tutorial_AdvancedInitialization_CommaTemporary.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100151</td>
152<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200153\verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100154</td></tr></table>
155
156The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix
157object once the comma initialization of our temporary submatrix is done.
158
Benoit Jacobe078bb22010-06-26 14:00:00 -0400159
Benoit Jacobe078bb22010-06-26 14:00:00 -0400160*/
161
162}