This forum has been archived. All content is frozen. Please use KDE Discuss instead.

colwise() with sum() give weird result.

Tags: None
(comma "," separated)
cherishing
Registered Member
Posts
2
Karma
0
Here is the code.
Code: Select all
   {
      Eigen::MatrixXd m=Eigen::MatrixXd::Ones(3,4);
      m.array().rowwise()/=m.array().colwise().sum();
      std::cout<<m<<std::endl;
   }
   {
      Eigen::MatrixXd m=Eigen::MatrixXd::Ones(3,4);
      m.array().rowwise()/=m.array().colwise().sum().eval();
      htmlExpln(m)
      std::cout<<m<<std::endl;
   }


And the results:
1:
0.333333 0.333333 0.333333 0.333333
0.428571 0.428571 0.428571 0.428571
0.567568 0.567568 0.567568 0.567568

2:
0.333333 0.333333 0.333333 0.333333
0.333333 0.333333 0.333333 0.333333
0.333333 0.333333 0.333333 0.333333

It seems that if we don't eval the summation, it's still an expression. Each time we access the elements of it, the value will be re-calculated.
Although this approach results in no-temporary variable, it's inefficient and weird.

Is there anyother method to work like the second one without temporary variable?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This is an aliasing issue discussed there: http://eigen.tuxfamily.org/dox/group__T ... asing.html


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot], Yahoo [Bot]