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 | |
| 26 | <table class="tutorial_code"><tr><td> |
| 27 | Example: \include Tutorial_commainit_01.cpp |
| 28 | </td> |
| 29 | <td> |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 30 | Output: \verbinclude Tutorial_commainit_01.out |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 31 | </td></tr></table> |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 32 | |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 33 | The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more |
| 34 | complicated way to get the same result as above: |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 35 | |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 36 | <table class="tutorial_code"><tr><td> |
| 37 | Example: \include Tutorial_commainit_01b.cpp |
| 38 | </td> |
| 39 | <td> |
| 40 | Output: \verbinclude Tutorial_commainit_01b.out |
| 41 | </td></tr></table> |
| 42 | |
| 43 | Moreover, the elements of the initialization list may themselves be matrices. Thus, we can use them to |
| 44 | initialize matrices with a block structure. |
| 45 | |
| 46 | <table class="tutorial_code"><tr><td> |
| 47 | Example: \include Tutorial_AdvancedInitialization_Block.cpp |
| 48 | </td> |
| 49 | <td> |
| 50 | Output: \verbinclude Tutorial_AdvancedInitialization_Block.out |
| 51 | </td></tr></table> |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 52 | |
| 53 | |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 54 | \section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 55 | |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 56 | The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be |
| 57 | used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments |
| 58 | and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need |
| 59 | to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional |
| 60 | dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional |
| 61 | objects. All three variants are illustrated in the following example: |
| 62 | |
| 63 | <table class="tutorial_code"><tr><td> |
| 64 | Example: \include Tutorial_AdvancedInitialization_Zero.cpp |
| 65 | </td> |
| 66 | <td> |
| 67 | Output: \verbinclude Tutorial_AdvancedInitialization_Zero.out |
| 68 | </td></tr></table> |
| 69 | |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 70 | Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c value. |
| 71 | 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] | 72 | argument, as in <tt>MatrixXd::Constant(rows, cols, value)</tt>. The method \link DenseBase::Random() Random() |
| 73 | \endlink fills the matrix or array with random coefficients. The identity matrix can be obtained by calling |
| 74 | \link MatrixBase::Identity() Identity()\endlink; this method is only available for Matrix, not for Array, |
| 75 | because "identity matrix" is a linear algebra concept. The method |
| 76 | \link DenseBase::LinSpaced LinSpaced\endlink(low, high, size) is only available for vectors and |
| 77 | one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between |
| 78 | \c low and \c high. The method \c LinSpaced() is illustrated in the following example, which prints a table |
| 79 | with angles in degrees, the corresponding angle in radians, and their sine and cosine. |
| 80 | |
| 81 | <table class="tutorial_code"><tr><td> |
| 82 | Example: \include Tutorial_AdvancedInitialization_LinSpaced.cpp |
| 83 | </td> |
| 84 | <td> |
| 85 | Output: \verbinclude Tutorial_AdvancedInitialization_LinSpaced.out |
| 86 | </td></tr></table> |
| 87 | |
| 88 | This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and |
| 89 | expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink, |
Jitse Niesen | 1420f8b | 2010-07-25 20:29:07 +0100 | [diff] [blame] | 90 | \link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 91 | conveniently. The following example contrasts three ways to construct the matrix |
| 92 | \f$ J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and |
| 93 | assignment, using static methods and the comma-initializer, or using the setXxx() methods. |
| 94 | |
| 95 | <table class="tutorial_code"><tr><td> |
| 96 | Example: \include Tutorial_AdvancedInitialization_ThreeWays.cpp |
| 97 | </td> |
| 98 | <td> |
| 99 | Output: \verbinclude Tutorial_AdvancedInitialization_ThreeWays.out |
| 100 | </td></tr></table> |
| 101 | |
| 102 | A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage. |
| 103 | |
| 104 | |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 105 | \section TutorialAdvancedInitializationTemporaryObjects Usage as temporary objects |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 106 | |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 107 | 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] | 108 | 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] | 109 | matrix or array; in fact, they return so-called \ref TopicEigenExpressionTemplates "expression objects" which |
| 110 | evaluate to a matrix or array when needed, so that this syntax does not incur any overhead. |
| 111 | |
| 112 | These expressions can also be used as a temporary object. The second example in |
| 113 | the \ref GettingStarted guide, which we reproduce here, already illustrates this. |
Jitse Niesen | 403e672 | 2010-07-22 15:53:21 +0100 | [diff] [blame] | 114 | |
| 115 | <table class="tutorial_code"><tr><td> |
| 116 | Example: \include QuickStart_example2_dynamic.cpp |
| 117 | </td> |
| 118 | <td> |
| 119 | Output: \verbinclude QuickStart_example2_dynamic.out |
| 120 | </td></tr></table> |
| 121 | |
Benoit Jacob | 3404d5f | 2010-10-18 09:09:30 -0400 | [diff] [blame] | 122 | The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix expression with all its coefficients |
| 123 | equal to 1.2 plus the corresponding coefficient of \a m. |
| 124 | |
| 125 | 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] | 126 | matrix of size 2-by-3, and then multiplies this matrix on the left with |
| 127 | \f$ \bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$. |
| 128 | |
| 129 | <table class="tutorial_code"><tr><td> |
| 130 | Example: \include Tutorial_AdvancedInitialization_CommaTemporary.cpp |
| 131 | </td> |
| 132 | <td> |
| 133 | Output: \verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out |
| 134 | </td></tr></table> |
| 135 | |
| 136 | The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix |
| 137 | object once the comma initialization of our temporary submatrix is done. |
| 138 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 139 | |
Benoit Jacob | 4d4a23c | 2010-06-30 10:11:55 -0400 | [diff] [blame] | 140 | \li \b Next: \ref TutorialLinearAlgebra |
| 141 | |
Benoit Jacob | e078bb2 | 2010-06-26 14:00:00 -0400 | [diff] [blame] | 142 | */ |
| 143 | |
| 144 | } |