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.

215 lines
9.3 KiB

  1. namespace StormEigen {
  2. /** \eigenManualPage TopicAliasing Aliasing
  3. In %Eigen, aliasing refers to assignment statement in which the same matrix (or array or vector) appears on the
  4. left and on the right of the assignment operators. Statements like <tt>mat = 2 * mat;</tt> or <tt>mat =
  5. mat.transpose();</tt> exhibit aliasing. The aliasing in the first example is harmless, but the aliasing in the
  6. second example leads to unexpected results. This page explains what aliasing is, when it is harmful, and what
  7. to do about it.
  8. \eigenAutoToc
  9. \section TopicAliasingExamples Examples
  10. Here is a simple example exhibiting aliasing:
  11. <table class="example">
  12. <tr><th>Example</th><th>Output</th></tr>
  13. <tr><td>
  14. \include TopicAliasing_block.cpp
  15. </td>
  16. <td>
  17. \verbinclude TopicAliasing_block.out
  18. </td></tr></table>
  19. The output is not what one would expect. The problem is the assignment
  20. \code
  21. mat.bottomRightCorner(2,2) = mat.topLeftCorner(2,2);
  22. \endcode
  23. This assignment exhibits aliasing: the coefficient \c mat(1,1) appears both in the block
  24. <tt>mat.bottomRightCorner(2,2)</tt> on the left-hand side of the assignment and the block
  25. <tt>mat.topLeftCorner(2,2)</tt> on the right-hand side. After the assignment, the (2,2) entry in the bottom
  26. right corner should have the value of \c mat(1,1) before the assignment, which is 5. However, the output shows
  27. that \c mat(2,2) is actually 1. The problem is that %Eigen uses lazy evaluation (see
  28. \ref TopicEigenExpressionTemplates) for <tt>mat.topLeftCorner(2,2)</tt>. The result is similar to
  29. \code
  30. mat(1,1) = mat(0,0);
  31. mat(1,2) = mat(0,1);
  32. mat(2,1) = mat(1,0);
  33. mat(2,2) = mat(1,1);
  34. \endcode
  35. Thus, \c mat(2,2) is assigned the \e new value of \c mat(1,1) instead of the old value. The next section
  36. explains how to solve this problem by calling \link DenseBase::eval() eval()\endlink.
  37. Aliasing occurs more naturally when trying to shrink a matrix. For example, the expressions <tt>vec =
  38. vec.head(n)</tt> and <tt>mat = mat.block(i,j,r,c)</tt> exhibit aliasing.
  39. In general, aliasing cannot be detected at compile time: if \c mat in the first example were a bit bigger,
  40. then the blocks would not overlap, and there would be no aliasing problem. However, %Eigen does detect some
  41. instances of aliasing, albeit at run time. The following example exhibiting aliasing was mentioned in \ref
  42. TutorialMatrixArithmetic :
  43. <table class="example">
  44. <tr><th>Example</th><th>Output</th></tr>
  45. <tr><td>
  46. \include tut_arithmetic_transpose_aliasing.cpp
  47. </td>
  48. <td>
  49. \verbinclude tut_arithmetic_transpose_aliasing.out
  50. </td></tr></table>
  51. Again, the output shows the aliasing issue. However, by default %Eigen uses a run-time assertion to detect this
  52. and exits with a message like
  53. \verbatim
  54. void StormEigen::DenseBase<Derived>::checkTransposeAliasing(const OtherDerived&) const
  55. [with OtherDerived = StormEigen::Transpose<StormEigen::Matrix<int, 2, 2, 0, 2, 2> >, Derived = Eigen::Matrix<int, 2, 2, 0, 2, 2>]:
  56. Assertion `(!internal::check_transpose_aliasing_selector<Scalar,internal::blas_traits<Derived>::IsTransposed,OtherDerived>::run(internal::extract_data(derived()), other))
  57. && "aliasing detected during transposition, use transposeInPlace() or evaluate the rhs into a temporary using .eval()"' failed.
  58. \endverbatim
  59. The user can turn %Eigen's run-time assertions like the one to detect this aliasing problem off by defining the
  60. STORMEIGEN_NO_DEBUG macro, and the above program was compiled with this macro turned off in order to illustrate the
  61. aliasing problem. See \ref TopicAssertions for more information about %Eigen's run-time assertions.
  62. \section TopicAliasingSolution Resolving aliasing issues
  63. If you understand the cause of the aliasing issue, then it is obvious what must happen to solve it: %Eigen has
  64. to evaluate the right-hand side fully into a temporary matrix/array and then assign it to the left-hand
  65. side. The function \link DenseBase::eval() eval() \endlink does precisely that.
  66. For example, here is the corrected version of the first example above:
  67. <table class="example">
  68. <tr><th>Example</th><th>Output</th></tr>
  69. <tr><td>
  70. \include TopicAliasing_block_correct.cpp
  71. </td>
  72. <td>
  73. \verbinclude TopicAliasing_block_correct.out
  74. </td></tr></table>
  75. Now, \c mat(2,2) equals 5 after the assignment, as it should be.
  76. The same solution also works for the second example, with the transpose: simply replace the line
  77. <tt>a = a.transpose();</tt> with <tt>a = a.transpose().eval();</tt>. However, in this common case there is a
  78. better solution. %Eigen provides the special-purpose function
  79. \link DenseBase::transposeInPlace() transposeInPlace() \endlink which replaces a matrix by its transpose.
  80. This is shown below:
  81. <table class="example">
  82. <tr><th>Example</th><th>Output</th></tr>
  83. <tr><td>
  84. \include tut_arithmetic_transpose_inplace.cpp
  85. </td>
  86. <td>
  87. \verbinclude tut_arithmetic_transpose_inplace.out
  88. </td></tr></table>
  89. If an xxxInPlace() function is available, then it is best to use it, because it indicates more clearly what you
  90. are doing. This may also allow %Eigen to optimize more aggressively. These are some of the xxxInPlace()
  91. functions provided:
  92. <table class="manual">
  93. <tr><th>Original function</th><th>In-place function</th></tr>
  94. <tr> <td> MatrixBase::adjoint() </td> <td> MatrixBase::adjointInPlace() </td> </tr>
  95. <tr class="alt"> <td> DenseBase::reverse() </td> <td> DenseBase::reverseInPlace() </td> </tr>
  96. <tr> <td> LDLT::solve() </td> <td> LDLT::solveInPlace() </td> </tr>
  97. <tr class="alt"> <td> LLT::solve() </td> <td> LLT::solveInPlace() </td> </tr>
  98. <tr> <td> TriangularView::solve() </td> <td> TriangularView::solveInPlace() </td> </tr>
  99. <tr class="alt"> <td> DenseBase::transpose() </td> <td> DenseBase::transposeInPlace() </td> </tr>
  100. </table>
  101. In the special case where a matrix or vector is shrunk using an expression like <tt>vec = vec.head(n)</tt>,
  102. you can use \link PlainObjectBase::conservativeResize() conservativeResize() \endlink.
  103. \section TopicAliasingCwise Aliasing and component-wise operations
  104. As explained above, it may be dangerous if the same matrix or array occurs on both the left-hand side and the
  105. right-hand side of an assignment operator, and it is then often necessary to evaluate the right-hand side
  106. explicitly. However, applying component-wise operations (such as matrix addition, scalar multiplication and
  107. array multiplication) is safe.
  108. The following example has only component-wise operations. Thus, there is no need for \link DenseBase::eval()
  109. eval() \endlink even though the same matrix appears on both sides of the assignments.
  110. <table class="example">
  111. <tr><th>Example</th><th>Output</th></tr>
  112. <tr><td>
  113. \include TopicAliasing_cwise.cpp
  114. </td>
  115. <td>
  116. \verbinclude TopicAliasing_cwise.out
  117. </td></tr></table>
  118. In general, an assignment is safe if the (i,j) entry of the expression on the right-hand side depends only on
  119. the (i,j) entry of the matrix or array on the left-hand side and not on any other entries. In that case it is
  120. not necessary to evaluate the right-hand side explicitly.
  121. \section TopicAliasingMatrixMult Aliasing and matrix multiplication
  122. Matrix multiplication is the only operation in %Eigen that assumes aliasing by default. Thus, if \c matA is a
  123. matrix, then the statement <tt>matA = matA * matA;</tt> is safe. All other operations in %Eigen assume that
  124. there are no aliasing problems, either because the result is assigned to a different matrix or because it is a
  125. component-wise operation.
  126. <table class="example">
  127. <tr><th>Example</th><th>Output</th></tr>
  128. <tr><td>
  129. \include TopicAliasing_mult1.cpp
  130. </td>
  131. <td>
  132. \verbinclude TopicAliasing_mult1.out
  133. </td></tr></table>
  134. However, this comes at a price. When executing the expression <tt>matA = matA * matA</tt>, %Eigen evaluates the
  135. product in a temporary matrix which is assigned to \c matA after the computation. This is fine. But %Eigen does
  136. the same when the product is assigned to a different matrix (e.g., <tt>matB = matA * matA</tt>). In that case,
  137. it is more efficient to evaluate the product directly into \c matB instead of evaluating it first into a
  138. temporary matrix and copying that matrix to \c matB.
  139. The user can indicate with the \link MatrixBase::noalias() noalias()\endlink function that there is no
  140. aliasing, as follows: <tt>matB.noalias() = matA * matA</tt>. This allows %Eigen to evaluate the matrix product
  141. <tt>matA * matA</tt> directly into \c matB.
  142. <table class="example">
  143. <tr><th>Example</th><th>Output</th></tr>
  144. <tr><td>
  145. \include TopicAliasing_mult2.cpp
  146. </td>
  147. <td>
  148. \verbinclude TopicAliasing_mult2.out
  149. </td></tr></table>
  150. Of course, you should not use \c noalias() when there is in fact aliasing taking place. If you do, then you
  151. may get wrong results:
  152. <table class="example">
  153. <tr><th>Example</th><th>Output</th></tr>
  154. <tr><td>
  155. \include TopicAliasing_mult3.cpp
  156. </td>
  157. <td>
  158. \verbinclude TopicAliasing_mult3.out
  159. </td></tr></table>
  160. \section TopicAliasingSummary Summary
  161. Aliasing occurs when the same matrix or array coefficients appear both on the left- and the right-hand side of
  162. an assignment operator.
  163. - Aliasing is harmless with coefficient-wise computations; this includes scalar multiplication and matrix or
  164. array addition.
  165. - When you multiply two matrices, %Eigen assumes that aliasing occurs. If you know that there is no aliasing,
  166. then you can use \link MatrixBase::noalias() noalias()\endlink.
  167. - In all other situations, %Eigen assumes that there is no aliasing issue and thus gives the wrong result if
  168. aliasing does in fact occur. To prevent this, you have to use \link DenseBase::eval() eval() \endlink or
  169. one of the xxxInPlace() functions.
  170. */
  171. }