Re: [eigen] Strange operator behaviour

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Thanks for the quick answer, that solved it!

In this case, an alternative solution would be to replace

the critical line by



MatrixXd B  = flag ? MatrixXd(A.transpose()) : A;



though you can probably nothing compared to an if-then-else statement.



Personally, I would prefer to make expression ctors explicit, since the

current behavior is quite unintuitive and potentially dangerous, though

you probably get used to it. 



mwb





On Fri, 19 Sep 2014 10:50:56 +0200, Gael Guennebaud

<gael.guennebaud@xxxxxxxxx> wrote:

> This is because the 'then' and 'else' operands of operator?: have to be

of

> the same type. Since the type of the 'then' operand is

Transpose<MatrixXd>,

> the compiler tries to convert the 'else' operand to that type.

> Since Transpose<MatrixXd> has a constructor from MatrixXd, the compiler

> manages to do so silently... Making all expression ctor explicit would

fix

> such issues but also makes their use more painful...

> 

> The only solution in your case is to use a 'if-then-else' statement.

> 

> gael

> 

> On Fri, Sep 19, 2014 at 10:37 AM, <mwb@xxxxxxxxxxxx> wrote:

> 

>>

>>

>>

>> I have observed a very strange behaviour using the ?: operator.

>>

>> If I run the following progrom:

>>

>>

>>

>> #include <iostream>

>>

>> #include <Eigen/Core>

>>

>>

>>

>> using namespace Eigen;

>>

>> using namespace std;

>>

>>

>>

>> int main(int argc, char* argv[])

>>

>> {

>>

>>   MatrixXd A(2,2);

>>

>>   A << 11, 12,

>>

>>     21, 22;

>>

>>   bool flag = false;

>>

>>

>>

>>   cout << "Matrix A:\n" << A << endl<<endl;

>>

>>   MatrixXd B  = flag ? A.transpose() : A;

>>

>>   cout << "Matrix B:\n" << B << endl<<endl;

>>

>>   cout << "Matrix A after:\n" << A << endl<<endl;

>>

>>   cout << "Flag: "<< (flag ? "true" : "false") <<endl;

>>

>> }

>>

>>

>>

>> I get this surprising output:

>>

>>

>>

>> Matrix A:

>>

>> 11 12

>>

>> 21 22

>>

>>

>>

>> Matrix B:

>>

>> 11 21

>>

>> 12 22

>>

>>

>>

>> Matrix A after:

>>

>> 11 12

>>

>> 21 22

>>

>>

>>

>> Flag: false

>>

>>

>>

>> Does anybody have an idea, why B is initialized with A.transpose()

>> instead

>>

>> of A?

>>

>> I am using Visual Studio 2012 Update4 and Eigen3.2.2

>>

>> Does Eigen3.2.2 somewhere overrides the ?: operator with some strange

>> side

>>

>> effects?

>>

>>

>>

>> I am very confused.

>>

>>

>>

>> mwb

>>

>>

>>



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/