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.

192 lines
8.3 KiB

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