blob: acbf9b8cb5752af7f16bfbb0afa728b8bb9b3bc6 [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
Benoit Jacob4d4a23c2010-06-30 10:11:55 -04005\li \b Previous: \ref TutorialBlockOperations
6\li \b Next: \ref TutorialLinearAlgebra
7
Jitse Niesen403e6722010-07-22 15:53:21 +01008This page discusses several advanced methods for initializing matrices. It gives more details on the
9comma-initializer, which was introduced before. It also explains how to get special matrices such as the
10identity matrix and the zero matrix.
Benoit Jacobe078bb22010-06-26 14:00:00 -040011
Gael Guennebaud93ee82b2013-01-05 16:37:11 +010012\eigenAutoToc
Jitse Niesen403e6722010-07-22 15:53:21 +010013
14\section TutorialAdvancedInitializationCommaInitializer The comma initializer
15
16Eigen offers a comma initializer syntax which allows the user to easily set all the coefficients of a matrix,
17vector or array. Simply list the coefficients, starting at the top-left corner and moving from left to right
18and from the top to the bottom. The size of the object needs to be specified beforehand. If you list too few
19or too many coefficients, Eigen will complain.
20
Gael Guennebaudf66fe262010-10-19 11:40:49 +020021<table class="example">
22<tr><th>Example:</th><th>Output:</th></tr>
23<tr><td>
24\include Tutorial_commainit_01.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010025</td>
26<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020027\verbinclude Tutorial_commainit_01.out
Jitse Niesen403e6722010-07-22 15:53:21 +010028</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040029
Jitse Niesen8bca23b2011-02-12 23:17:31 +000030Moreover, the elements of the initialization list may themselves be vectors or matrices. A common use is
31to join vectors or matrices together. For example, here is how to join two row vectors together. Remember
32that you have to set the size before you can use the comma initializer.
Benoit Jacobe078bb22010-06-26 14:00:00 -040033
Gael Guennebaudf66fe262010-10-19 11:40:49 +020034<table class="example">
35<tr><th>Example:</th><th>Output:</th></tr>
36<tr><td>
Jitse Niesen8bca23b2011-02-12 23:17:31 +000037\include Tutorial_AdvancedInitialization_Join.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010038</td>
39<td>
Jitse Niesen8bca23b2011-02-12 23:17:31 +000040\verbinclude Tutorial_AdvancedInitialization_Join.out
Jitse Niesen403e6722010-07-22 15:53:21 +010041</td></tr></table>
42
Jitse Niesen8bca23b2011-02-12 23:17:31 +000043We can use the same technique to initialize matrices with a block structure.
Jitse Niesen403e6722010-07-22 15:53:21 +010044
Gael Guennebaudf66fe262010-10-19 11:40:49 +020045<table class="example">
46<tr><th>Example:</th><th>Output:</th></tr>
47<tr><td>
48\include Tutorial_AdvancedInitialization_Block.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010049</td>
50<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020051\verbinclude Tutorial_AdvancedInitialization_Block.out
Jitse Niesen403e6722010-07-22 15:53:21 +010052</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040053
Jitse Niesen8bca23b2011-02-12 23:17:31 +000054The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more
55complicated way to get the same result as in the first example above:
56
57<table class="example">
58<tr><th>Example:</th><th>Output:</th></tr>
59<tr><td>
60\include Tutorial_commainit_01b.cpp
61</td>
62<td>
63\verbinclude Tutorial_commainit_01b.out
64</td></tr></table>
65
Benoit Jacobe078bb22010-06-26 14:00:00 -040066
Jitse Niesen403e6722010-07-22 15:53:21 +010067\section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays
Benoit Jacobe078bb22010-06-26 14:00:00 -040068
Jitse Niesen403e6722010-07-22 15:53:21 +010069The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be
70used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments
71and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need
72to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional
73dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional
74objects. All three variants are illustrated in the following example:
75
Gael Guennebaudf66fe262010-10-19 11:40:49 +020076<table class="example">
77<tr><th>Example:</th><th>Output:</th></tr>
78<tr><td>
79\include Tutorial_AdvancedInitialization_Zero.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010080</td>
81<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020082\verbinclude Tutorial_AdvancedInitialization_Zero.out
Jitse Niesen403e6722010-07-22 15:53:21 +010083</td></tr></table>
84
Benoit Jacob3404d5f2010-10-18 09:09:30 -040085Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c value.
86If the size of the object needs to be specified, the additional arguments go before the \c value
Jitse Niesen403e6722010-07-22 15:53:21 +010087argument, as in <tt>MatrixXd::Constant(rows, cols, value)</tt>. The method \link DenseBase::Random() Random()
88\endlink fills the matrix or array with random coefficients. The identity matrix can be obtained by calling
89\link MatrixBase::Identity() Identity()\endlink; this method is only available for Matrix, not for Array,
90because "identity matrix" is a linear algebra concept. The method
Benoit Jacob6f2ba1f2011-01-28 10:00:34 -050091\link DenseBase::LinSpaced LinSpaced\endlink(size, low, high) is only available for vectors and
Jitse Niesen403e6722010-07-22 15:53:21 +010092one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between
93\c low and \c high. The method \c LinSpaced() is illustrated in the following example, which prints a table
94with angles in degrees, the corresponding angle in radians, and their sine and cosine.
95
Gael Guennebaudf66fe262010-10-19 11:40:49 +020096<table class="example">
97<tr><th>Example:</th><th>Output:</th></tr>
98<tr><td>
99\include Tutorial_AdvancedInitialization_LinSpaced.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100100</td>
101<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200102\verbinclude Tutorial_AdvancedInitialization_LinSpaced.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100103</td></tr></table>
104
105This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and
106expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink,
Jitse Niesen1420f8b2010-07-25 20:29:07 +0100107\link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this
Jitse Niesen403e6722010-07-22 15:53:21 +0100108conveniently. The following example contrasts three ways to construct the matrix
109\f$ J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and
110assignment, using static methods and the comma-initializer, or using the setXxx() methods.
111
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200112<table class="example">
113<tr><th>Example:</th><th>Output:</th></tr>
114<tr><td>
115\include Tutorial_AdvancedInitialization_ThreeWays.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100116</td>
117<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200118\verbinclude Tutorial_AdvancedInitialization_ThreeWays.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100119</td></tr></table>
120
121A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage.
122
123
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400124\section TutorialAdvancedInitializationTemporaryObjects Usage as temporary objects
Jitse Niesen403e6722010-07-22 15:53:21 +0100125
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400126As 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 +0100127declaration 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 -0400128matrix or array; in fact, they return so-called \ref TopicEigenExpressionTemplates "expression objects" which
129evaluate to a matrix or array when needed, so that this syntax does not incur any overhead.
130
131These expressions can also be used as a temporary object. The second example in
132the \ref GettingStarted guide, which we reproduce here, already illustrates this.
Jitse Niesen403e6722010-07-22 15:53:21 +0100133
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200134<table class="example">
135<tr><th>Example:</th><th>Output:</th></tr>
136<tr><td>
137\include QuickStart_example2_dynamic.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100138</td>
139<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200140\verbinclude QuickStart_example2_dynamic.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100141</td></tr></table>
142
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400143The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix expression with all its coefficients
144equal to 1.2 plus the corresponding coefficient of \a m.
145
146The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random
Jitse Niesen403e6722010-07-22 15:53:21 +0100147matrix of size 2-by-3, and then multiplies this matrix on the left with
148\f$ \bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$.
149
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200150<table class="example">
151<tr><th>Example:</th><th>Output:</th></tr>
152<tr><td>
153\include Tutorial_AdvancedInitialization_CommaTemporary.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100154</td>
155<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200156\verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100157</td></tr></table>
158
159The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix
160object once the comma initialization of our temporary submatrix is done.
161
Benoit Jacobe078bb22010-06-26 14:00:00 -0400162
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400163\li \b Next: \ref TutorialLinearAlgebra
164
Benoit Jacobe078bb22010-06-26 14:00:00 -0400165*/
166
167}