TopicAliasing.dox 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. namespace Eigen {
  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 Eigen::DenseBase<Derived>::checkTransposeAliasing(const OtherDerived&) const
  55. [with OtherDerived = Eigen::Transpose<Eigen::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. EIGEN_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, <strong>under the
  123. condition that the destination matrix is not resized</strong>.
  124. Thus, if \c matA is a \b squared matrix, then the statement <tt>matA = matA * matA;</tt> is safe.
  125. All other operations in %Eigen assume that there are no aliasing problems,
  126. either because the result is assigned to a different matrix or because it is a component-wise operation.
  127. <table class="example">
  128. <tr><th>Example</th><th>Output</th></tr>
  129. <tr><td>
  130. \include TopicAliasing_mult1.cpp
  131. </td>
  132. <td>
  133. \verbinclude TopicAliasing_mult1.out
  134. </td></tr></table>
  135. However, this comes at a price. When executing the expression <tt>matA = matA * matA</tt>, %Eigen evaluates the
  136. product in a temporary matrix which is assigned to \c matA after the computation. This is fine. But %Eigen does
  137. the same when the product is assigned to a different matrix (e.g., <tt>matB = matA * matA</tt>). In that case,
  138. it is more efficient to evaluate the product directly into \c matB instead of evaluating it first into a
  139. temporary matrix and copying that matrix to \c matB.
  140. The user can indicate with the \link MatrixBase::noalias() noalias()\endlink function that there is no
  141. aliasing, as follows: <tt>matB.noalias() = matA * matA</tt>. This allows %Eigen to evaluate the matrix product
  142. <tt>matA * matA</tt> directly into \c matB.
  143. <table class="example">
  144. <tr><th>Example</th><th>Output</th></tr>
  145. <tr><td>
  146. \include TopicAliasing_mult2.cpp
  147. </td>
  148. <td>
  149. \verbinclude TopicAliasing_mult2.out
  150. </td></tr></table>
  151. Of course, you should not use \c noalias() when there is in fact aliasing taking place. If you do, then you
  152. may get wrong results:
  153. <table class="example">
  154. <tr><th>Example</th><th>Output</th></tr>
  155. <tr><td>
  156. \include TopicAliasing_mult3.cpp
  157. </td>
  158. <td>
  159. \verbinclude TopicAliasing_mult3.out
  160. </td></tr></table>
  161. Moreover, starting in Eigen 3.3, aliasing is \b not assumed if the destination matrix is resized and the product is not directly assigned to the destination.
  162. Therefore, the following example is also wrong:
  163. <table class="example">
  164. <tr><th>Example</th><th>Output</th></tr>
  165. <tr><td>
  166. \include TopicAliasing_mult4.cpp
  167. </td>
  168. <td>
  169. \verbinclude TopicAliasing_mult4.out
  170. </td></tr></table>
  171. As for any aliasing issue, you can resolve it by explicitly evaluating the expression prior to assignment:
  172. <table class="example">
  173. <tr><th>Example</th><th>Output</th></tr>
  174. <tr><td>
  175. \include TopicAliasing_mult5.cpp
  176. </td>
  177. <td>
  178. \verbinclude TopicAliasing_mult5.out
  179. </td></tr></table>
  180. \section TopicAliasingSummary Summary
  181. Aliasing occurs when the same matrix or array coefficients appear both on the left- and the right-hand side of
  182. an assignment operator.
  183. - Aliasing is harmless with coefficient-wise computations; this includes scalar multiplication and matrix or
  184. array addition.
  185. - When you multiply two matrices, %Eigen assumes that aliasing occurs. If you know that there is no aliasing,
  186. then you can use \link MatrixBase::noalias() noalias()\endlink.
  187. - In all other situations, %Eigen assumes that there is no aliasing issue and thus gives the wrong result if
  188. aliasing does in fact occur. To prevent this, you have to use \link DenseBase::eval() eval() \endlink or
  189. one of the xxxInPlace() functions.
  190. */
  191. }