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