aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/I11_Aliasing.dox
blob: 9b3c00af1f1661cad6e022b44a32c683b4d91cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace Eigen {

/** \page TopicAliasing Aliasing

In Eigen, aliasing refers to assignment statement in which the same matrix (or array or vector) appears on the
left and on the right of the assignment operators. Statements like <tt>mat = 2 * mat;</tt> or <tt>mat =
mat.transpose();</tt> exhibit aliasing. The aliasing in the first example is harmless, but the aliasing in the
second example leads to unexpected results. This page explains what aliasing is, when it is harmful, and what
to do about it.

<b>Table of contents</b>
  - \ref TopicAliasingExamples
  - \ref TopicAliasingSolution
  - \ref TopicAliasingCwise
  - \ref TopicAliasingMatrixMult
  - \ref TopicAliasingSummary


\section TopicAliasingExamples Examples

The following example exhibiting aliasing was mentioned in \ref TutorialMatrixArithmetic :

<table class="tutorial_code"><tr><td>
Example: \include tut_arithmetic_transpose_aliasing.cpp
</td>
<td>
Output: \verbinclude tut_arithmetic_transpose_aliasing.out
</td></tr></table>

The output is not what one would expect. In fact, the transpose of the matrix is
\f[
\mbox{a.transpose()} = \begin{bmatrix} 1 & 3 \\ 2 & 4 \end{bmatrix}.
\f]
The problem here is that Eigen's implementation uses lazy evaluation 
(see \ref TopicEigenExpressionTemplates).  The result is similar to
\code
for (Matrix2i::Index j = 0; j < a.cols(); ++j)
  for (Matrix2i::Index i = 0; i < a.rows(); ++i)
    a(i,j) = a(j,i);
\endcode
Thus, when <tt>a(1,0)</tt> is written to, it uses the new value of <tt>a(0,1)</tt> instead of the old one, and
this leads to the wrong result. 

The next section explains how to solve this problem, but first we want to show one more example to illustrate
that aliasing can be a bit more subtle.

<table class="tutorial_code"><tr><td>
Example: \include TopicAliasing_block.cpp
</td>
<td>
Output: \verbinclude TopicAliasing_block.out
</td></tr></table>

The blocks <tt>mat.bottomRightCorner(2,2)</tt> and <tt>mat.topLeftCorner(2,2)</tt> overlap, because both
contain the coefficient <tt>mat(1,1)</tt> at the centre of the 3-by-3 matrix \c mat . Thus, this example
exhibits aliasing, and indeed the result is wrong: the (2,2) entry in the bottom right corner should be
5. However, if \c mat were a 4-by-4 matrix, then the blocks would not overlop, and there would be no aliasing.


\section TopicAliasingSolution Resolving aliasing issues

Synopsis: xxxInPlace(), eval().


\section TopicAliasingCwise Aliasing and component-wise operations

Synopsis: Things like mat = 2 * mat, matA = matA + matB and arr = arr.sin() are safe.


\section TopicAliasingMatrixMult Aliasing and matrix multiplication

Synopsis: %Matrix multiplication assumes aliasing by default. Use noalias() to improve performance if there is
no aliasing.


\section TopicAliasingSummary Summary

Aliasing occurs when the same matrix or array coefficients appear both on the left- and the right-hand side of
an assignment operator.
 - Aliasing is harmless with coefficient-wise computations; this includes scalar multiplication and matrix or
   array addition.
 - When you multiply two matrices, Eigen assumes that aliasing occurs. If you know that there is no aliasing,
   then you can use \link MatrixBase::noalias() noalias()\endlink.
 - In all other situations, Eigen assumes that there is no aliasing issue and thus gives the wrong result if
   aliasing does in fact occur. To prevent this, you have to use \link DenseBase::eval() eval() \endlink or
   one of the xxxInPlace() functions.

*/
}