TutorialAdvancedInitialization.dox 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. namespace Eigen {
  2. /** \eigenManualPage TutorialAdvancedInitialization Advanced initialization
  3. This page discusses several advanced methods for initializing matrices. It gives more details on the
  4. comma-initializer, which was introduced before. It also explains how to get special matrices such as the
  5. identity matrix and the zero matrix.
  6. \eigenAutoToc
  7. \section TutorialAdvancedInitializationCommaInitializer The comma initializer
  8. Eigen offers a comma initializer syntax which allows the user to easily set all the coefficients of a matrix,
  9. vector or array. Simply list the coefficients, starting at the top-left corner and moving from left to right
  10. and from the top to the bottom. The size of the object needs to be specified beforehand. If you list too few
  11. or too many coefficients, Eigen will complain.
  12. <table class="example">
  13. <tr><th>Example:</th><th>Output:</th></tr>
  14. <tr><td>
  15. \include Tutorial_commainit_01.cpp
  16. </td>
  17. <td>
  18. \verbinclude Tutorial_commainit_01.out
  19. </td></tr></table>
  20. Moreover, the elements of the initialization list may themselves be vectors or matrices. A common use is
  21. to join vectors or matrices together. For example, here is how to join two row vectors together. Remember
  22. that you have to set the size before you can use the comma initializer.
  23. <table class="example">
  24. <tr><th>Example:</th><th>Output:</th></tr>
  25. <tr><td>
  26. \include Tutorial_AdvancedInitialization_Join.cpp
  27. </td>
  28. <td>
  29. \verbinclude Tutorial_AdvancedInitialization_Join.out
  30. </td></tr></table>
  31. We can use the same technique to initialize matrices with a block structure.
  32. <table class="example">
  33. <tr><th>Example:</th><th>Output:</th></tr>
  34. <tr><td>
  35. \include Tutorial_AdvancedInitialization_Block.cpp
  36. </td>
  37. <td>
  38. \verbinclude Tutorial_AdvancedInitialization_Block.out
  39. </td></tr></table>
  40. The comma initializer can also be used to fill block expressions such as <tt>m.row(i)</tt>. Here is a more
  41. complicated way to get the same result as in the first example above:
  42. <table class="example">
  43. <tr><th>Example:</th><th>Output:</th></tr>
  44. <tr><td>
  45. \include Tutorial_commainit_01b.cpp
  46. </td>
  47. <td>
  48. \verbinclude Tutorial_commainit_01b.out
  49. </td></tr></table>
  50. \section TutorialAdvancedInitializationSpecialMatrices Special matrices and arrays
  51. The Matrix and Array classes have static methods like \link DenseBase::Zero() Zero()\endlink, which can be
  52. used to initialize all coefficients to zero. There are three variants. The first variant takes no arguments
  53. and can only be used for fixed-size objects. If you want to initialize a dynamic-size object to zero, you need
  54. to specify the size. Thus, the second variant requires one argument and can be used for one-dimensional
  55. dynamic-size objects, while the third variant requires two arguments and can be used for two-dimensional
  56. objects. All three variants are illustrated in the following example:
  57. <table class="example">
  58. <tr><th>Example:</th><th>Output:</th></tr>
  59. <tr><td>
  60. \include Tutorial_AdvancedInitialization_Zero.cpp
  61. </td>
  62. <td>
  63. \verbinclude Tutorial_AdvancedInitialization_Zero.out
  64. </td></tr></table>
  65. Similarly, the static method \link DenseBase::Constant() Constant\endlink(value) sets all coefficients to \c value.
  66. If the size of the object needs to be specified, the additional arguments go before the \c value
  67. argument, as in <tt>MatrixXd::Constant(rows, cols, value)</tt>. The method \link DenseBase::Random() Random()
  68. \endlink fills the matrix or array with random coefficients. The identity matrix can be obtained by calling
  69. \link MatrixBase::Identity() Identity()\endlink; this method is only available for Matrix, not for Array,
  70. because "identity matrix" is a linear algebra concept. The method
  71. \link DenseBase::LinSpaced LinSpaced\endlink(size, low, high) is only available for vectors and
  72. one-dimensional arrays; it yields a vector of the specified size whose coefficients are equally spaced between
  73. \c low and \c high. The method \c LinSpaced() is illustrated in the following example, which prints a table
  74. with angles in degrees, the corresponding angle in radians, and their sine and cosine.
  75. <table class="example">
  76. <tr><th>Example:</th><th>Output:</th></tr>
  77. <tr><td>
  78. \include Tutorial_AdvancedInitialization_LinSpaced.cpp
  79. </td>
  80. <td>
  81. \verbinclude Tutorial_AdvancedInitialization_LinSpaced.out
  82. </td></tr></table>
  83. This example shows that objects like the ones returned by LinSpaced() can be assigned to variables (and
  84. expressions). Eigen defines utility functions like \link DenseBase::setZero() setZero()\endlink,
  85. \link MatrixBase::setIdentity() \endlink and \link DenseBase::setLinSpaced() \endlink to do this
  86. conveniently. The following example contrasts three ways to construct the matrix
  87. \f$ J = \bigl[ \begin{smallmatrix} O & I \\ I & O \end{smallmatrix} \bigr] \f$: using static methods and
  88. assignment, using static methods and the comma-initializer, or using the setXxx() methods.
  89. <table class="example">
  90. <tr><th>Example:</th><th>Output:</th></tr>
  91. <tr><td>
  92. \include Tutorial_AdvancedInitialization_ThreeWays.cpp
  93. </td>
  94. <td>
  95. \verbinclude Tutorial_AdvancedInitialization_ThreeWays.out
  96. </td></tr></table>
  97. A summary of all pre-defined matrix, vector and array objects can be found in the \ref QuickRefPage.
  98. \section TutorialAdvancedInitializationTemporaryObjects Usage as temporary objects
  99. As shown above, static methods as Zero() and Constant() can be used to initialize variables at the time of
  100. declaration or at the right-hand side of an assignment operator. You can think of these methods as returning a
  101. matrix or array; in fact, they return so-called \ref TopicEigenExpressionTemplates "expression objects" which
  102. evaluate to a matrix or array when needed, so that this syntax does not incur any overhead.
  103. These expressions can also be used as a temporary object. The second example in
  104. the \ref GettingStarted guide, which we reproduce here, already illustrates this.
  105. <table class="example">
  106. <tr><th>Example:</th><th>Output:</th></tr>
  107. <tr><td>
  108. \include QuickStart_example2_dynamic.cpp
  109. </td>
  110. <td>
  111. \verbinclude QuickStart_example2_dynamic.out
  112. </td></tr></table>
  113. The expression <tt>m + MatrixXf::Constant(3,3,1.2)</tt> constructs the 3-by-3 matrix expression with all its coefficients
  114. equal to 1.2 plus the corresponding coefficient of \a m.
  115. The comma-initializer, too, can also be used to construct temporary objects. The following example constructs a random
  116. matrix of size 2-by-3, and then multiplies this matrix on the left with
  117. \f$ \bigl[ \begin{smallmatrix} 0 & 1 \\ 1 & 0 \end{smallmatrix} \bigr] \f$.
  118. <table class="example">
  119. <tr><th>Example:</th><th>Output:</th></tr>
  120. <tr><td>
  121. \include Tutorial_AdvancedInitialization_CommaTemporary.cpp
  122. </td>
  123. <td>
  124. \verbinclude Tutorial_AdvancedInitialization_CommaTemporary.out
  125. </td></tr></table>
  126. The \link CommaInitializer::finished() finished() \endlink method is necessary here to get the actual matrix
  127. object once the comma initialization of our temporary submatrix is done.
  128. */
  129. }