This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
Bug 883 - Error result when using auto and a long expression
Summary: Error result when using auto and a long expression
Status: RESOLVED INVALID
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - expression templates (show other bugs)
Version: 3.2
Hardware: x86 - general Windows
: Normal Wrong Result
Assignee: Nobody
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-23 15:58 UTC by Chao Liang
Modified: 2019-12-04 13:46 UTC (History)
3 users (show)



Attachments
output of my program. (20.59 KB, image/png)
2014-09-23 15:58 UTC, Chao Liang
no flags Details

Description Chao Liang 2014-09-23 15:58:02 UTC
Created attachment 495 [details]
output of my program.

"MatrixXi E= expression" and "auto E=expression" give different result!(see E and EAuto below.) 
and even "expression" and "expression.eval()"  give different result!

What I expected is that D, E, and EAuto are with the same value!

Here is my full program:

#include <iostream>
#include <Eigen/Core>
void main() {
#define printlnExpln(exp) std::cout<<#exp<<" is:\n"<<exp<<std::endl; 
    using namespace Eigen;
    MatrixXi A = MatrixXi::Ones (2, 3);

    MatrixXi B = MatrixXi::Ones (2, 3);
    auto ABColSum = (A.array() * B.array() ).colwise().sum().eval();
    auto D = A.array().rowwise() - ABColSum;
    printlnExpln (A);
    printlnExpln (B);
    printlnExpln (ABColSum);
    printlnExpln (D);

    MatrixXi E = (A.array().rowwise() ) - ( (A.array() * B.array() ).colwise().sum().eval() );
    printlnExpln (E);

    auto EAuto = (A.array().rowwise() ) - ( (A.array() * B.array() ).colwise().sum().eval() );
    printlnExpln (typeid (EAuto).name() );
	printlnExpln(EAuto);
    printlnExpln (EAuto.eval() );
   
}
Comment 1 Christoph Hertzberg 2014-09-23 16:30:56 UTC
Basically the same problem as bug 505.

*** This bug has been marked as a duplicate of bug 505 ***
Comment 2 Gael Guennebaud 2014-09-24 09:52:12 UTC
In reply to comment http://eigen.tuxfamily.org/bz/show_bug.cgi?id=505#c13, I agree that this bug report is slightly different. I would call it invalid because in the line:

auto EAuto = (A.array().rowwise() ) - ( (A.array() * B.array() ).colwise().sum().eval() );

you *explicitly* introduced a temporary by calling eval. Eigen cannot know this temporary is nested inside a bugger expression which is not evaluated within the current statement. What would you expect? Eigen is an expression template library, so auto has to be used with care. If you wanted a MatrixXi why writing 'auto' instead of MatrixXi? If you wanted the result of the expression while letting Eigen picks the "best" type, then you should encapsulate the whole right hand side with (...).eval().
Comment 3 Nobody 2019-12-04 13:46:07 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/883.

Note You need to log in before you can comment on or make changes to this bug.