You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
7.1 KiB

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