mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-04 03:00:39 +08:00
bug #1073: backport common pitfalls page
This commit is contained in:
parent
4bd69750ed
commit
f1583e86f6
@ -11,6 +11,7 @@ namespace Eigen {
|
||||
- \subpage TopicCustomizingEigen
|
||||
- \subpage TopicMultiThreading
|
||||
- \subpage TopicUsingIntelMKL
|
||||
- \subpage TopicPitfalls
|
||||
- \subpage TopicTemplateKeyword
|
||||
- \subpage UserManual_UnderstandingEigen
|
||||
*/
|
||||
|
38
doc/Pitfalls.dox
Normal file
38
doc/Pitfalls.dox
Normal file
@ -0,0 +1,38 @@
|
||||
namespace Eigen {
|
||||
|
||||
/** \page TopicPitfalls Common pitfalls
|
||||
|
||||
\section TopicPitfalls_template_keyword Compilation error with template methods
|
||||
|
||||
See this \link TopicTemplateKeyword page \endlink.
|
||||
|
||||
\section TopicPitfalls_auto_keyword C++11 and the auto keyword
|
||||
|
||||
In short: do not use the auto keywords with Eigen's expressions, unless you are 100% sure about what you are doing. In particular, do not use the auto keyword as a replacement for a Matrix<> type. Here is an example:
|
||||
|
||||
\code
|
||||
MatrixXd A, B;
|
||||
auto C = A*B;
|
||||
for(...) { ... w = C * v; ...}
|
||||
\endcode
|
||||
|
||||
In this example, the type of C is not a MatrixXd but an abstract expression representing a matrix product and storing references to A and B. Therefore, the product of A*B will be carried out multiple times, once per iteration of the for loop. Moreover, if the coefficients of A or B change during the iteration, then C will evaluate to different values.
|
||||
|
||||
Here is another example leading to a segfault:
|
||||
\code
|
||||
auto C = ((A+B).eval()).transpose();
|
||||
// do something with C
|
||||
\endcode
|
||||
The problem is that eval() returns a temporary object (in this case a MatrixXd) which is then referenced by the Transpose<> expression. However, this temporary is deleted right after the first line, and there the C expression reference a dead object. The same issue might occur when sub expressions are automatically evaluated by Eigen as in the following example:
|
||||
\code
|
||||
VectorXd u, v;
|
||||
auto C = u + (A*v).normalized();
|
||||
// do something with C
|
||||
\endcode
|
||||
where the normalized() method has to evaluate the expensive product A*v to avoid evaluating it twice. On the other hand, the following example is perfectly fine:
|
||||
\code
|
||||
auto C = (u + (A*v).normalized()).eval();
|
||||
\endcode
|
||||
In this case, C will be a regular VectorXd object.
|
||||
*/
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user