blob: 70aa7be7477708ae9bcafc03ef5ffe970606156b [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
Gael Guennebaud2ea1e492012-12-28 18:58:07 +010013\tableofcontents
Jitse Niesen403e6722010-07-22 15:53:21 +010014
15\section TutorialAdvancedInitializationCommaInitializer The comma initializer
16
17Eigen offers a comma initializer syntax which allows the user to easily set all the coefficients of a matrix,
18vector or array. Simply list the coefficients, starting at the top-left corner and moving from left to right
19and from the top to the bottom. The size of the object needs to be specified beforehand. If you list too few
20or too many coefficients, Eigen will complain.
21
Gael Guennebaudf66fe262010-10-19 11:40:49 +020022<table class="example">
23<tr><th>Example:</th><th>Output:</th></tr>
24<tr><td>
25\include Tutorial_commainit_01.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010026</td>
27<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020028\verbinclude Tutorial_commainit_01.out
Jitse Niesen403e6722010-07-22 15:53:21 +010029</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040030
Jitse Niesen8bca23b2011-02-12 23:17:31 +000031Moreover, the elements of the initialization list may themselves be vectors or matrices. A common use is
32to join vectors or matrices together. For example, here is how to join two row vectors together. Remember
33that you have to set the size before you can use the comma initializer.
Benoit Jacobe078bb22010-06-26 14:00:00 -040034
Gael Guennebaudf66fe262010-10-19 11:40:49 +020035<table class="example">
36<tr><th>Example:</th><th>Output:</th></tr>
37<tr><td>
Jitse Niesen8bca23b2011-02-12 23:17:31 +000038\include Tutorial_AdvancedInitialization_Join.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010039</td>
40<td>
Jitse Niesen8bca23b2011-02-12 23:17:31 +000041\verbinclude Tutorial_AdvancedInitialization_Join.out
Jitse Niesen403e6722010-07-22 15:53:21 +010042</td></tr></table>
43
Jitse Niesen8bca23b2011-02-12 23:17:31 +000044We can use the same technique to initialize matrices with a block structure.
Jitse Niesen403e6722010-07-22 15:53:21 +010045
Gael Guennebaudf66fe262010-10-19 11:40:49 +020046<table class="example">
47<tr><th>Example:</th><th>Output:</th></tr>
48<tr><td>
49\include Tutorial_AdvancedInitialization_Block.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010050</td>
51<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020052\verbinclude Tutorial_AdvancedInitialization_Block.out
Jitse Niesen403e6722010-07-22 15:53:21 +010053</td></tr></table>
Benoit Jacobe078bb22010-06-26 14:00:00 -040054
Jitse Niesen8bca23b2011-02-12 23:17:31 +000055The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more
56complicated way to get the same result as in the first example above:
57
58<table class="example">
59<tr><th>Example:</th><th>Output:</th></tr>
60<tr><td>
61\include Tutorial_commainit_01b.cpp
62</td>
63<td>
64\verbinclude Tutorial_commainit_01b.out
65</td></tr></table>
66
Benoit Jacobe078bb22010-06-26 14:00:00 -040067
Jitse Niesen403e6722010-07-22 15:53:21 +010068\section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays
Benoit Jacobe078bb22010-06-26 14:00:00 -040069
Jitse Niesen403e6722010-07-22 15:53:21 +010070The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be
71used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments
72and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need
73to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional
74dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional
75objects. All three variants are illustrated in the following example:
76
Gael Guennebaudf66fe262010-10-19 11:40:49 +020077<table class="example">
78<tr><th>Example:</th><th>Output:</th></tr>
79<tr><td>
80\include Tutorial_AdvancedInitialization_Zero.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +010081</td>
82<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +020083\verbinclude Tutorial_AdvancedInitialization_Zero.out
Jitse Niesen403e6722010-07-22 15:53:21 +010084</td></tr></table>
85
Benoit Jacob3404d5f2010-10-18 09:09:30 -040086Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c value.
87If the size of the object needs to be specified, the additional arguments go before the \c value
Jitse Niesen403e6722010-07-22 15:53:21 +010088argument, as in <tt>MatrixXd::Constant(rows, cols, value)</tt>. The method \link DenseBase::Random() Random()
89\endlink fills the matrix or array with random coefficients. The identity matrix can be obtained by calling
90\link MatrixBase::Identity() Identity()\endlink; this method is only available for Matrix, not for Array,
91because "identity matrix" is a linear algebra concept. The method
Benoit Jacob6f2ba1f2011-01-28 10:00:34 -050092\link DenseBase::LinSpaced LinSpaced\endlink(size, low, high) is only available for vectors and
Jitse Niesen403e6722010-07-22 15:53:21 +010093one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between
94\c low and \c high. The method \c LinSpaced() is illustrated in the following example, which prints a table
95with angles in degrees, the corresponding angle in radians, and their sine and cosine.
96
Gael Guennebaudf66fe262010-10-19 11:40:49 +020097<table class="example">
98<tr><th>Example:</th><th>Output:</th></tr>
99<tr><td>
100\include Tutorial_AdvancedInitialization_LinSpaced.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100101</td>
102<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200103\verbinclude Tutorial_AdvancedInitialization_LinSpaced.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100104</td></tr></table>
105
106This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and
107expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink,
Jitse Niesen1420f8b2010-07-25 20:29:07 +0100108\link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this
Jitse Niesen403e6722010-07-22 15:53:21 +0100109conveniently. The following example contrasts three ways to construct the matrix
110\f$ J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and
111assignment, using static methods and the comma-initializer, or using the setXxx() methods.
112
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200113<table class="example">
114<tr><th>Example:</th><th>Output:</th></tr>
115<tr><td>
116\include Tutorial_AdvancedInitialization_ThreeWays.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100117</td>
118<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200119\verbinclude Tutorial_AdvancedInitialization_ThreeWays.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100120</td></tr></table>
121
122A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage.
123
124
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400125\section TutorialAdvancedInitializationTemporaryObjects Usage as temporary objects
Jitse Niesen403e6722010-07-22 15:53:21 +0100126
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400127As 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 +0100128declaration 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 -0400129matrix or array; in fact, they return so-called \ref TopicEigenExpressionTemplates "expression objects" which
130evaluate to a matrix or array when needed, so that this syntax does not incur any overhead.
131
132These expressions can also be used as a temporary object. The second example in
133the \ref GettingStarted guide, which we reproduce here, already illustrates this.
Jitse Niesen403e6722010-07-22 15:53:21 +0100134
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200135<table class="example">
136<tr><th>Example:</th><th>Output:</th></tr>
137<tr><td>
138\include QuickStart_example2_dynamic.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100139</td>
140<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200141\verbinclude QuickStart_example2_dynamic.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100142</td></tr></table>
143
Benoit Jacob3404d5f2010-10-18 09:09:30 -0400144The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix expression with all its coefficients
145equal to 1.2 plus the corresponding coefficient of \a m.
146
147The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random
Jitse Niesen403e6722010-07-22 15:53:21 +0100148matrix of size 2-by-3, and then multiplies this matrix on the left with
149\f$ \bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$.
150
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200151<table class="example">
152<tr><th>Example:</th><th>Output:</th></tr>
153<tr><td>
154\include Tutorial_AdvancedInitialization_CommaTemporary.cpp
Jitse Niesen403e6722010-07-22 15:53:21 +0100155</td>
156<td>
Gael Guennebaudf66fe262010-10-19 11:40:49 +0200157\verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out
Jitse Niesen403e6722010-07-22 15:53:21 +0100158</td></tr></table>
159
160The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix
161object once the comma initialization of our temporary submatrix is done.
162
Benoit Jacobe078bb22010-06-26 14:00:00 -0400163
Benoit Jacob4d4a23c2010-06-30 10:11:55 -0400164\li \b Next: \ref TutorialLinearAlgebra
165
Benoit Jacobe078bb22010-06-26 14:00:00 -0400166*/
167
168}