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.

205 lines
8.7 KiB

  1. namespace Eigen {
  2. /** \page TutorialArrayClass Tutorial page 3 - The %Array class and coefficient-wise operations
  3. \ingroup Tutorial
  4. \li \b Previous: \ref TutorialMatrixArithmetic
  5. \li \b Next: \ref TutorialBlockOperations
  6. This tutorial aims to provide an overview and explanations on how to use
  7. Eigen's Array class.
  8. \b Table \b of \b contents
  9. - \ref TutorialArrayClassIntro
  10. - \ref TutorialArrayClassTypes
  11. - \ref TutorialArrayClassAccess
  12. - \ref TutorialArrayClassAddSub
  13. - \ref TutorialArrayClassMult
  14. - \ref TutorialArrayClassCwiseOther
  15. - \ref TutorialArrayClassConvert
  16. \section TutorialArrayClassIntro What is the Array class?
  17. The Array class provides general-purpose arrays, as opposed to the Matrix class which
  18. is intended for linear algebra. Furthermore, the Array class provides an easy way to
  19. perform coefficient-wise operations, which might not have a linear algebraic meaning,
  20. such as adding a constant to every coefficient in the array or multiplying two arrays coefficient-wise.
  21. \section TutorialArrayClassTypes Array types
  22. Array is a class template taking the same template parameters as Matrix.
  23. As with Matrix, the first three template parameters are mandatory:
  24. \code
  25. Array<typename Scalar, int RowsAtCompileTime, int ColsAtCompileTime>
  26. \endcode
  27. The last three template parameters are optional. Since this is exactly the same as for Matrix,
  28. we won't explain it again here and just refer to \ref TutorialMatrixClass.
  29. Eigen also provides typedefs for some common cases, in a way that is similar to the Matrix typedefs
  30. but with some slight differences, as the word "array" is used for both 1-dimensional and 2-dimensional arrays.
  31. We adopt the convention that typedefs of the form ArrayNt stand for 1-dimensional arrays, where N and t are
  32. the size and the scalar type, as in the Matrix typedefs explained on \ref TutorialMatrixClass "this page". For 2-dimensional arrays, we
  33. use typedefs of the form ArrayNNt. Some examples are shown in the following table:
  34. <table class="manual">
  35. <tr>
  36. <th>Type </th>
  37. <th>Typedef </th>
  38. </tr>
  39. <tr>
  40. <td> \code Array<float,Dynamic,1> \endcode </td>
  41. <td> \code ArrayXf \endcode </td>
  42. </tr>
  43. <tr>
  44. <td> \code Array<float,3,1> \endcode </td>
  45. <td> \code Array3f \endcode </td>
  46. </tr>
  47. <tr>
  48. <td> \code Array<double,Dynamic,Dynamic> \endcode </td>
  49. <td> \code ArrayXXd \endcode </td>
  50. </tr>
  51. <tr>
  52. <td> \code Array<double,3,3> \endcode </td>
  53. <td> \code Array33d \endcode </td>
  54. </tr>
  55. </table>
  56. \section TutorialArrayClassAccess Accessing values inside an Array
  57. The parenthesis operator is overloaded to provide write and read access to the coefficients of an array, just as with matrices.
  58. Furthermore, the \c << operator can be used to initialize arrays (via the comma initializer) or to print them.
  59. <table class="example">
  60. <tr><th>Example:</th><th>Output:</th></tr>
  61. <tr><td>
  62. \include Tutorial_ArrayClass_accessors.cpp
  63. </td>
  64. <td>
  65. \verbinclude Tutorial_ArrayClass_accessors.out
  66. </td></tr></table>
  67. For more information about the comma initializer, see \ref TutorialAdvancedInitialization.
  68. \section TutorialArrayClassAddSub Addition and subtraction
  69. Adding and subtracting two arrays is the same as for matrices.
  70. The operation is valid if both arrays have the same size, and the addition or subtraction is done coefficient-wise.
  71. Arrays also support expressions of the form <tt>array + scalar</tt> which add a scalar to each coefficient in the array.
  72. This provides a functionality that is not directly available for Matrix objects.
  73. <table class="example">
  74. <tr><th>Example:</th><th>Output:</th></tr>
  75. <tr><td>
  76. \include Tutorial_ArrayClass_addition.cpp
  77. </td>
  78. <td>
  79. \verbinclude Tutorial_ArrayClass_addition.out
  80. </td></tr></table>
  81. \section TutorialArrayClassMult Array multiplication
  82. First of all, of course you can multiply an array by a scalar, this works in the same way as matrices. Where arrays
  83. are fundamentally different from matrices, is when you multiply two together. Matrices interpret
  84. multiplication as matrix product and arrays interpret multiplication as coefficient-wise product. Thus, two
  85. arrays can be multiplied if and only if they have the same dimensions.
  86. <table class="example">
  87. <tr><th>Example:</th><th>Output:</th></tr>
  88. <tr><td>
  89. \include Tutorial_ArrayClass_mult.cpp
  90. </td>
  91. <td>
  92. \verbinclude Tutorial_ArrayClass_mult.out
  93. </td></tr></table>
  94. \section TutorialArrayClassCwiseOther Other coefficient-wise operations
  95. The Array class defines other coefficient-wise operations besides the addition, subtraction and multiplication
  96. operators described above. For example, the \link ArrayBase::abs() .abs() \endlink method takes the absolute
  97. value of each coefficient, while \link ArrayBase::sqrt() .sqrt() \endlink computes the square root of the
  98. coefficients. If you have two arrays of the same size, you can call \link ArrayBase::min() .min() \endlink to
  99. construct the array whose coefficients are the minimum of the corresponding coefficients of the two given
  100. arrays. These operations are illustrated in the following example.
  101. <table class="example">
  102. <tr><th>Example:</th><th>Output:</th></tr>
  103. <tr><td>
  104. \include Tutorial_ArrayClass_cwise_other.cpp
  105. </td>
  106. <td>
  107. \verbinclude Tutorial_ArrayClass_cwise_other.out
  108. </td></tr></table>
  109. More coefficient-wise operations can be found in the \ref QuickRefPage.
  110. \section TutorialArrayClassConvert Converting between array and matrix expressions
  111. When should you use objects of the Matrix class and when should you use objects of the Array class? You cannot
  112. apply Matrix operations on arrays, or Array operations on matrices. Thus, if you need to do linear algebraic
  113. operations such as matrix multiplication, then you should use matrices; if you need to do coefficient-wise
  114. operations, then you should use arrays. However, sometimes it is not that simple, but you need to use both
  115. Matrix and Array operations. In that case, you need to convert a matrix to an array or reversely. This gives
  116. access to all operations regardless of the choice of declaring objects as arrays or as matrices.
  117. \link MatrixBase Matrix expressions \endlink have an \link MatrixBase::array() .array() \endlink method that
  118. 'converts' them into \link ArrayBase array expressions\endlink, so that coefficient-wise operations
  119. can be applied easily. Conversely, \link ArrayBase array expressions \endlink
  120. have a \link ArrayBase::matrix() .matrix() \endlink method. As with all Eigen expression abstractions,
  121. this doesn't have any runtime cost (provided that you let your compiler optimize).
  122. Both \link MatrixBase::array() .array() \endlink and \link ArrayBase::matrix() .matrix() \endlink
  123. can be used as rvalues and as lvalues.
  124. Mixing matrices and arrays in an expression is forbidden with Eigen. For instance, you cannot add a matrix and
  125. array directly; the operands of a \c + operator should either both be matrices or both be arrays. However,
  126. it is easy to convert from one to the other with \link MatrixBase::array() .array() \endlink and
  127. \link ArrayBase::matrix() .matrix()\endlink. The exception to this rule is the assignment operator: it is
  128. allowed to assign a matrix expression to an array variable, or to assign an array expression to a matrix
  129. variable.
  130. The following example shows how to use array operations on a Matrix object by employing the
  131. \link MatrixBase::array() .array() \endlink method. For example, the statement
  132. <tt>result = m.array() * n.array()</tt> takes two matrices \c m and \c n, converts them both to an array, uses
  133. * to multiply them coefficient-wise and assigns the result to the matrix variable \c result (this is legal
  134. because Eigen allows assigning array expressions to matrix variables).
  135. As a matter of fact, this usage case is so common that Eigen provides a \link MatrixBase::cwiseProduct()
  136. .cwiseProduct() \endlink method for matrices to compute the coefficient-wise product. This is also shown in
  137. the example program.
  138. <table class="example">
  139. <tr><th>Example:</th><th>Output:</th></tr>
  140. <tr><td>
  141. \include Tutorial_ArrayClass_interop_matrix.cpp
  142. </td>
  143. <td>
  144. \verbinclude Tutorial_ArrayClass_interop_matrix.out
  145. </td></tr></table>
  146. Similarly, if \c array1 and \c array2 are arrays, then the expression <tt>array1.matrix() * array2.matrix()</tt>
  147. computes their matrix product.
  148. Here is a more advanced example. The expression <tt>(m.array() + 4).matrix() * m</tt> adds 4 to every
  149. coefficient in the matrix \c m and then computes the matrix product of the result with \c m. Similarly, the
  150. expression <tt>(m.array() * n.array()).matrix() * m</tt> computes the coefficient-wise product of the matrices
  151. \c m and \c n and then the matrix product of the result with \c m.
  152. <table class="example">
  153. <tr><th>Example:</th><th>Output:</th></tr>
  154. <tr><td>
  155. \include Tutorial_ArrayClass_interop.cpp
  156. </td>
  157. <td>
  158. \verbinclude Tutorial_ArrayClass_interop.out
  159. </td></tr></table>
  160. \li \b Next: \ref TutorialBlockOperations
  161. */
  162. }