Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 1 | namespace Eigen { |
| 2 | |
Gael Guennebaud | 93ee82b | 2013-01-05 16:37:11 +0100 | [diff] [blame^] | 3 | /** \eigenManualPage TutorialAdvancedInitialization Advanced initialization |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 4 | |
Benoit Jacob | 4d4a23c | 2010-06-30 10:11:55 -0400 | [diff] [blame] | 5 | \li \b Previous: \ref TutorialBlockOperations |
| 6 | \li \b Next: \ref TutorialLinearAlgebra |
| 7 | |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 8 | This page discusses several advanced methods for initializing matrices. It gives more details on the |
| 9 | comma-initializer, which was introduced before. It also explains how to get special matrices such as the |
| 10 | identity matrix and the zero matrix. |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 11 | |
Gael Guennebaud | 93ee82b | 2013-01-05 16:37:11 +0100 | [diff] [blame^] | 12 | \eigenAutoToc |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 13 | |
| 14 | \section TutorialAdvancedInitializationCommaInitializer The comma initializer |
| 15 | |
| 16 | Eigen offers a comma initializer syntax which allows the user to easily set all the coefficients of a matrix, |
| 17 | vector or array. Simply list the coefficients, starting at the top-left corner and moving from left to right |
| 18 | and from the top to the bottom. The size of the object needs to be specified beforehand. If you list too few |
| 19 | or too many coefficients, Eigen will complain. |
| 20 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 21 | <table class="example"> |
| 22 | <tr><th>Example:</th><th>Output:</th></tr> |
| 23 | <tr><td> |
| 24 | \include Tutorial_commainit_01.cpp |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 25 | </td> |
| 26 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 27 | \verbinclude Tutorial_commainit_01.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 28 | </td></tr></table> |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 29 | |
Jitse Niesen | 8bca23b | 2011-02-12 23:17:31 +0000 | [diff] [blame] | 30 | Moreover, the elements of the initialization list may themselves be vectors or matrices. A common use is |
| 31 | to join vectors or matrices together. For example, here is how to join two row vectors together. Remember |
| 32 | that you have to set the size before you can use the comma initializer. |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 33 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 34 | <table class="example"> |
| 35 | <tr><th>Example:</th><th>Output:</th></tr> |
| 36 | <tr><td> |
Jitse Niesen | 8bca23b | 2011-02-12 23:17:31 +0000 | [diff] [blame] | 37 | \include Tutorial_AdvancedInitialization_Join.cpp |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 38 | </td> |
| 39 | <td> |
Jitse Niesen | 8bca23b | 2011-02-12 23:17:31 +0000 | [diff] [blame] | 40 | \verbinclude Tutorial_AdvancedInitialization_Join.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 41 | </td></tr></table> |
| 42 | |
Jitse Niesen | 8bca23b | 2011-02-12 23:17:31 +0000 | [diff] [blame] | 43 | We can use the same technique to initialize matrices with a block structure. |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 44 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 45 | <table class="example"> |
| 46 | <tr><th>Example:</th><th>Output:</th></tr> |
| 47 | <tr><td> |
| 48 | \include Tutorial_AdvancedInitialization_Block.cpp |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 49 | </td> |
| 50 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 51 | \verbinclude Tutorial_AdvancedInitialization_Block.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 52 | </td></tr></table> |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 53 | |
Jitse Niesen | 8bca23b | 2011-02-12 23:17:31 +0000 | [diff] [blame] | 54 | The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more |
| 55 | complicated 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 Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 66 | |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 67 | \section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 68 | |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 69 | The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be |
| 70 | used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments |
| 71 | and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need |
| 72 | to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional |
| 73 | dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional |
| 74 | objects. All three variants are illustrated in the following example: |
| 75 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 76 | <table class="example"> |
| 77 | <tr><th>Example:</th><th>Output:</th></tr> |
| 78 | <tr><td> |
| 79 | \include Tutorial_AdvancedInitialization_Zero.cpp |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 80 | </td> |
| 81 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 82 | \verbinclude Tutorial_AdvancedInitialization_Zero.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 83 | </td></tr></table> |
| 84 | |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 85 | Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c value. |
| 86 | If the size of the object needs to be specified, the additional arguments go before the \c value |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 87 | argument, 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, |
| 90 | because "identity matrix" is a linear algebra concept. The method |
Benoit Jacob | 6f2ba1f | 2011-01-28 10:00:34 -0500 | [diff] [blame] | 91 | \link DenseBase::LinSpaced LinSpaced\endlink(size, low, high) is only available for vectors and |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 92 | one-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 |
| 94 | with angles in degrees, the corresponding angle in radians, and their sine and cosine. |
| 95 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 96 | <table class="example"> |
| 97 | <tr><th>Example:</th><th>Output:</th></tr> |
| 98 | <tr><td> |
| 99 | \include Tutorial_AdvancedInitialization_LinSpaced.cpp |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 100 | </td> |
| 101 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 102 | \verbinclude Tutorial_AdvancedInitialization_LinSpaced.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 103 | </td></tr></table> |
| 104 | |
| 105 | This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and |
| 106 | expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink, |
Jitse Niesen | 1420f8b | 2010-07-25 20:29:07 +0100 | [diff] [blame] | 107 | \link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 108 | conveniently. 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 |
| 110 | assignment, using static methods and the comma-initializer, or using the setXxx() methods. |
| 111 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 112 | <table class="example"> |
| 113 | <tr><th>Example:</th><th>Output:</th></tr> |
| 114 | <tr><td> |
| 115 | \include Tutorial_AdvancedInitialization_ThreeWays.cpp |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 116 | </td> |
| 117 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 118 | \verbinclude Tutorial_AdvancedInitialization_ThreeWays.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 119 | </td></tr></table> |
| 120 | |
| 121 | A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage. |
| 122 | |
| 123 | |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 124 | \section TutorialAdvancedInitializationTemporaryObjects Usage as temporary objects |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 125 | |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 126 | As shown above, static methods as Zero() and Constant() can be used to initialize variables at the time of |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 127 | declaration or at the right-hand side of an assignment operator. You can think of these methods as returning a |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 128 | matrix or array; in fact, they return so-called \ref TopicEigenExpressionTemplates "expression objects" which |
| 129 | evaluate to a matrix or array when needed, so that this syntax does not incur any overhead. |
| 130 | |
| 131 | These expressions can also be used as a temporary object. The second example in |
| 132 | the \ref GettingStarted guide, which we reproduce here, already illustrates this. |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 133 | |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 134 | <table class="example"> |
| 135 | <tr><th>Example:</th><th>Output:</th></tr> |
| 136 | <tr><td> |
| 137 | \include QuickStart_example2_dynamic.cpp |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 138 | </td> |
| 139 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 140 | \verbinclude QuickStart_example2_dynamic.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 141 | </td></tr></table> |
| 142 | |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 143 | The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix expression with all its coefficients |
| 144 | equal to 1.2 plus the corresponding coefficient of \a m. |
| 145 | |
| 146 | The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 147 | matrix 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 Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 150 | <table class="example"> |
| 151 | <tr><th>Example:</th><th>Output:</th></tr> |
| 152 | <tr><td> |
| 153 | \include Tutorial_AdvancedInitialization_CommaTemporary.cpp |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 154 | </td> |
| 155 | <td> |
Gael Guennebaud | f66fe26 | 2010-10-19 11:40:49 +0200 | [diff] [blame] | 156 | \verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 157 | </td></tr></table> |
| 158 | |
| 159 | The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix |
| 160 | object once the comma initialization of our temporary submatrix is done. |
| 161 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 162 | |
Benoit Jacob | 4d4a23c | 2010-06-30 10:11:55 -0400 | [diff] [blame] | 163 | \li \b Next: \ref TutorialLinearAlgebra |
| 164 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 165 | */ |
| 166 | |
| 167 | } |