blob: 4f27f1e4db1a6891c9b2b1a9cf5f487552947abb [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
Gael Guennebaudf66fe262010-10-19 11:40:49 +020026<table class="example">
27<tr><th>Example:</th><th>Output:</th></tr>
28<tr><td>
29\include Tutorial_commainit_01.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010030</td>
31<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020032\verbinclude Tutorial_commainit_01.out
Jitse Niesen403e6722010-07-22 15:53:21 +010033</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040034
Jitse Niesen8bca23b2011-02-12 23:17:31 +000035Moreover, the elements of the initialization list may themselves be vectors or matrices. A common use is
36to join vectors or matrices together. For example, here is how to join two row vectors together. Remember
37that you have to set the size before you can use the comma initializer.
Benoit Jacobe078bb22010-06-26 14:00:00 -040038
Gael Guennebaudf66fe262010-10-19 11:40:49 +020039<table class="example">
40<tr><th>Example:</th><th>Output:</th></tr>
41<tr><td>
Jitse Niesen8bca23b2011-02-12 23:17:31 +000042\include Tutorial_AdvancedInitialization_Join.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010043</td>
44<td>
Jitse Niesen8bca23b2011-02-12 23:17:31 +000045\verbinclude Tutorial_AdvancedInitialization_Join.out
Jitse Niesen403e6722010-07-22 15:53:21 +010046</td></tr></table>
47
Jitse Niesen8bca23b2011-02-12 23:17:31 +000048We can use the same technique to initialize matrices with a block structure.
Jitse Niesen403e6722010-07-22 15:53:21 +010049
Gael Guennebaudf66fe262010-10-19 11:40:49 +020050<table class="example">
51<tr><th>Example:</th><th>Output:</th></tr>
52<tr><td>
53\include Tutorial_AdvancedInitialization_Block.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010054</td>
55<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020056\verbinclude Tutorial_AdvancedInitialization_Block.out
Jitse Niesen403e6722010-07-22 15:53:21 +010057</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040058
Jitse Niesen8bca23b2011-02-12 23:17:31 +000059The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more
60complicated way to get the same result as in the first example above:
61
62<table class="example">
63<tr><th>Example:</th><th>Output:</th></tr>
64<tr><td>
65\include Tutorial_commainit_01b.cpp
66</td>
67<td>
68\verbinclude Tutorial_commainit_01b.out
69</td></tr></table>
70
Benoit Jacobe078bb22010-06-26 14:00:00 -040071
Jitse Niesen403e6722010-07-22 15:53:21 +010072\section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays
Benoit Jacobe078bb22010-06-26 14:00:00 -040073
Jitse Niesen403e6722010-07-22 15:53:21 +010074The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be
75used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments
76and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need
77to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional
78dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional
79objects. All three variants are illustrated in the following example:
80
Gael Guennebaudf66fe262010-10-19 11:40:49 +020081<table class="example">
82<tr><th>Example:</th><th>Output:</th></tr>
83<tr><td>
84\include Tutorial_AdvancedInitialization_Zero.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010085</td>
86<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020087\verbinclude Tutorial_AdvancedInitialization_Zero.out
Jitse Niesen403e6722010-07-22 15:53:21 +010088</td></tr></table>
89
Benoit Jacob3404d5f2010-10-18 09:09:30 -040090Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c value.
91If the size of the object needs to be specified, the additional arguments go before the \c value
Jitse Niesen403e6722010-07-22 15:53:21 +010092argument, as in <tt>MatrixXd::Constant(rows, cols, value)</tt>. The method \link DenseBase::Random() Random()
93\endlink fills the matrix or array with random coefficients. The identity matrix can be obtained by calling
94\link MatrixBase::Identity() Identity()\endlink; this method is only available for Matrix, not for Array,
95because "identity matrix" is a linear algebra concept. The method
Benoit Jacob6f2ba1f2011-01-28 10:00:34 -050096\link DenseBase::LinSpaced LinSpaced\endlink(size, low, high) is only available for vectors and
Jitse Niesen403e6722010-07-22 15:53:21 +010097one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between
98\c low and \c high. The method \c LinSpaced() is illustrated in the following example, which prints a table
99with angles in degrees, the corresponding angle in radians, and their sine and cosine.
100
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200101<table class="example">
102<tr><th>Example:</th><th>Output:</th></tr>
103<tr><td>
104\include Tutorial_AdvancedInitialization_LinSpaced.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100105</td>
106<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200107\verbinclude Tutorial_AdvancedInitialization_LinSpaced.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100108</td></tr></table>
109
110This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and
111expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink,
Jitse Niesen1420f8b2010-07-25 20:29:07 +0100112\link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this
Jitse Niesen403e6722010-07-22 15:53:21 +0100113conveniently. The following example contrasts three ways to construct the matrix
114\f$ J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and
115assignment, using static methods and the comma-initializer, or using the setXxx() methods.
116
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200117<table class="example">
118<tr><th>Example:</th><th>Output:</th></tr>
119<tr><td>
120\include Tutorial_AdvancedInitialization_ThreeWays.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100121</td>
122<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200123\verbinclude Tutorial_AdvancedInitialization_ThreeWays.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100124</td></tr></table>
125
126A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage.
127
128
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400129\section TutorialAdvancedInitializationTemporaryObjects Usage as temporary objects
Jitse Niesen403e6722010-07-22 15:53:21 +0100130
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400131As 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 +0100132declaration 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 -0400133matrix or array; in fact, they return so-called \ref TopicEigenExpressionTemplates "expression objects" which
134evaluate to a matrix or array when needed, so that this syntax does not incur any overhead.
135
136These expressions can also be used as a temporary object. The second example in
137the \ref GettingStarted guide, which we reproduce here, already illustrates this.
Jitse Niesen403e6722010-07-22 15:53:21 +0100138
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200139<table class="example">
140<tr><th>Example:</th><th>Output:</th></tr>
141<tr><td>
142\include QuickStart_example2_dynamic.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100143</td>
144<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200145\verbinclude QuickStart_example2_dynamic.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100146</td></tr></table>
147
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400148The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix expression with all its coefficients
149equal to 1.2 plus the corresponding coefficient of \a m.
150
151The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random
Jitse Niesen403e6722010-07-22 15:53:21 +0100152matrix of size 2-by-3, and then multiplies this matrix on the left with
153\f$ \bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$.
154
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200155<table class="example">
156<tr><th>Example:</th><th>Output:</th></tr>
157<tr><td>
158\include Tutorial_AdvancedInitialization_CommaTemporary.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100159</td>
160<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200161\verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100162</td></tr></table>
163
164The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix
165object once the comma initialization of our temporary submatrix is done.
166
Benoit Jacobe078bb22010-06-26 14:00:00 -0400167
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400168\li \b Next: \ref TutorialLinearAlgebra
169
Benoit Jacobe078bb22010-06-26 14:00:00 -0400170*/
171
172}