blob: cb8fb6633c238e67a3014c10d0055de845ecc3ec [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 Niesen403e6722010-07-22 15:53:21 +010035The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more
36complicated way to get the same result as above:
Benoit Jacobe078bb22010-06-26 14:00:00 -040037
Gael Guennebaudf66fe262010-10-19 11:40:49 +020038<table class="example">
39<tr><th>Example:</th><th>Output:</th></tr>
40<tr><td>
41\include Tutorial_commainit_01b.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010042</td>
43<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020044\verbinclude Tutorial_commainit_01b.out
Jitse Niesen403e6722010-07-22 15:53:21 +010045</td></tr></table>
46
47Moreover, the elements of the initialization list may themselves be matrices. Thus, we can use them to
48initialize matrices with a block structure.
49
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
59
Jitse Niesen403e6722010-07-22 15:53:21 +010060\section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays
Benoit Jacobe078bb22010-06-26 14:00:00 -040061
Jitse Niesen403e6722010-07-22 15:53:21 +010062The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be
63used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments
64and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need
65to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional
66dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional
67objects. All three variants are illustrated in the following example:
68
Gael Guennebaudf66fe262010-10-19 11:40:49 +020069<table class="example">
70<tr><th>Example:</th><th>Output:</th></tr>
71<tr><td>
72\include Tutorial_AdvancedInitialization_Zero.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010073</td>
74<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020075\verbinclude Tutorial_AdvancedInitialization_Zero.out
Jitse Niesen403e6722010-07-22 15:53:21 +010076</td></tr></table>
77
Benoit Jacob3404d5f2010-10-18 09:09:30 -040078Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c value.
79If the size of the object needs to be specified, the additional arguments go before the \c value
Jitse Niesen403e6722010-07-22 15:53:21 +010080argument, as in <tt>MatrixXd::Constant(rows, cols, value)</tt>. The method \link DenseBase::Random() Random()
81\endlink fills the matrix or array with random coefficients. The identity matrix can be obtained by calling
82\link MatrixBase::Identity() Identity()\endlink; this method is only available for Matrix, not for Array,
83because "identity matrix" is a linear algebra concept. The method
84\link DenseBase::LinSpaced LinSpaced\endlink(low, high, size) is only available for vectors and
85one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between
86\c low and \c high. The method \c LinSpaced() is illustrated in the following example, which prints a table
87with angles in degrees, the corresponding angle in radians, and their sine and cosine.
88
Gael Guennebaudf66fe262010-10-19 11:40:49 +020089<table class="example">
90<tr><th>Example:</th><th>Output:</th></tr>
91<tr><td>
92\include Tutorial_AdvancedInitialization_LinSpaced.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010093</td>
94<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020095\verbinclude Tutorial_AdvancedInitialization_LinSpaced.out
Jitse Niesen403e6722010-07-22 15:53:21 +010096</td></tr></table>
97
98This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and
99expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink,
Jitse Niesen1420f8b2010-07-25 20:29:07 +0100100\link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this
Jitse Niesen403e6722010-07-22 15:53:21 +0100101conveniently. The following example contrasts three ways to construct the matrix
102\f$ J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and
103assignment, using static methods and the comma-initializer, or using the setXxx() methods.
104
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200105<table class="example">
106<tr><th>Example:</th><th>Output:</th></tr>
107<tr><td>
108\include Tutorial_AdvancedInitialization_ThreeWays.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100109</td>
110<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200111\verbinclude Tutorial_AdvancedInitialization_ThreeWays.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100112</td></tr></table>
113
114A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage.
115
116
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400117\section TutorialAdvancedInitializationTemporaryObjects Usage as temporary objects
Jitse Niesen403e6722010-07-22 15:53:21 +0100118
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400119As 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 +0100120declaration 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 -0400121matrix or array; in fact, they return so-called \ref TopicEigenExpressionTemplates "expression objects" which
122evaluate to a matrix or array when needed, so that this syntax does not incur any overhead.
123
124These expressions can also be used as a temporary object. The second example in
125the \ref GettingStarted guide, which we reproduce here, already illustrates this.
Jitse Niesen403e6722010-07-22 15:53:21 +0100126
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200127<table class="example">
128<tr><th>Example:</th><th>Output:</th></tr>
129<tr><td>
130\include QuickStart_example2_dynamic.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100131</td>
132<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200133\verbinclude QuickStart_example2_dynamic.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100134</td></tr></table>
135
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400136The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix expression with all its coefficients
137equal to 1.2 plus the corresponding coefficient of \a m.
138
139The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random
Jitse Niesen403e6722010-07-22 15:53:21 +0100140matrix of size 2-by-3, and then multiplies this matrix on the left with
141\f$ \bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$.
142
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200143<table class="example">
144<tr><th>Example:</th><th>Output:</th></tr>
145<tr><td>
146\include Tutorial_AdvancedInitialization_CommaTemporary.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100147</td>
148<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200149\verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100150</td></tr></table>
151
152The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix
153object once the comma initialization of our temporary submatrix is done.
154
Benoit Jacobe078bb22010-06-26 14:00:00 -0400155
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400156\li \b Next: \ref TutorialLinearAlgebra
157
Benoit Jacobe078bb22010-06-26 14:00:00 -0400158*/
159
160}