diff options
author | Gael Guennebaud <g.gael@free.fr> | 2016-01-25 15:50:55 +0100 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2016-01-25 15:50:55 +0100 |
commit | c10021c00a6cb6033bc479a46aef058c48836efd (patch) | |
tree | d6da7e56dfc2ea6d8c7a7cf4cdbe4acb954fc804 /doc | |
parent | b114e6fd3b61c7ef93f6b94e194d316f0ab19036 (diff) |
bug #1144: clarify the doc about aliasing in case of resizing and matrix product.
Diffstat (limited to 'doc')
-rw-r--r-- | doc/TopicAliasing.dox | 30 | ||||
-rw-r--r-- | doc/snippets/TopicAliasing_mult4.cpp | 5 | ||||
-rw-r--r-- | doc/snippets/TopicAliasing_mult5.cpp | 5 |
3 files changed, 36 insertions, 4 deletions
diff --git a/doc/TopicAliasing.dox b/doc/TopicAliasing.dox index c2654aed2..a8f164428 100644 --- a/doc/TopicAliasing.dox +++ b/doc/TopicAliasing.dox @@ -153,10 +153,11 @@ not necessary to evaluate the right-hand side explicitly. \section TopicAliasingMatrixMult Aliasing and matrix multiplication -Matrix multiplication is the only operation in %Eigen that assumes aliasing by default. Thus, if \c matA is a -matrix, then the statement <tt>matA = matA * matA;</tt> is safe. All other operations in %Eigen assume that -there are no aliasing problems, either because the result is assigned to a different matrix or because it is a -component-wise operation. +Matrix multiplication is the only operation in %Eigen that assumes aliasing by default, <strong>under the +condition that the destination matrix is not resized</strong>. +Thus, if \c matA is a \b squared matrix, then the statement <tt>matA = matA * matA;</tt> is safe. +All other operations in %Eigen assume that there are no aliasing problems, +either because the result is assigned to a different matrix or because it is a component-wise operation. <table class="example"> <tr><th>Example</th><th>Output</th></tr> @@ -198,6 +199,27 @@ may get wrong results: \verbinclude TopicAliasing_mult3.out </td></tr></table> +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. +Therefore, the following example is also wrong: + +<table class="example"> +<tr><th>Example</th><th>Output</th></tr> +<tr><td> +\include TopicAliasing_mult4.cpp +</td> +<td> +\verbinclude TopicAliasing_mult4.out +</td></tr></table> + +As for any aliasing issue, you can resolve it by explicitly evaluating the expression prior to assignment: +<table class="example"> +<tr><th>Example</th><th>Output</th></tr> +<tr><td> +\include TopicAliasing_mult5.cpp +</td> +<td> +\verbinclude TopicAliasing_mult5.out +</td></tr></table> \section TopicAliasingSummary Summary diff --git a/doc/snippets/TopicAliasing_mult4.cpp b/doc/snippets/TopicAliasing_mult4.cpp new file mode 100644 index 000000000..8a8992f6c --- /dev/null +++ b/doc/snippets/TopicAliasing_mult4.cpp @@ -0,0 +1,5 @@ +MatrixXf A(2,2), B(3,2); +B << 2, 0, 0, 3, 1, 1; +A << 2, 0, 0, -2; +A = (B * A).cwiseAbs(); +cout << A;
\ No newline at end of file diff --git a/doc/snippets/TopicAliasing_mult5.cpp b/doc/snippets/TopicAliasing_mult5.cpp new file mode 100644 index 000000000..1a36defde --- /dev/null +++ b/doc/snippets/TopicAliasing_mult5.cpp @@ -0,0 +1,5 @@ +MatrixXf A(2,2), B(3,2); +B << 2, 0, 0, 3, 1, 1; +A << 2, 0, 0, -2; +A = (B * A).eval().cwiseAbs(); +cout << A; |