Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the location of the maximum coefficient of an Eigen C++ library Vector

Tags:

c++

eigen

I came across a problem due to either my misunderstanding of the documentation, or a bug.

I want to retrieve the position of the maximum coefficient of a vector:

#include <Eigen/Core>
#include <iostream>

int main(int argc, char *argv[])
{
  Eigen::Vector3f v;
  int pos;

  v <<  1, 2, 3;

  std::cout << v.maxCoeff(&pos) << "\n" << "pos=" <<  pos << "\n";

  return 0;
}

the returned result (pos) is always zero, no matter where the maximum value is. Through debugging, I have seen that indeed the correct value (i, j) is computed in the visitor, but it seems that maxVisitor.col is returned instead of maxVisitor.row.

This happens also with the latest mercurial version.

Do you think it is a bug? Should I file a bug report?

like image 996
user2658323 Avatar asked Oct 18 '25 19:10

user2658323


1 Answers

There is no sequence point between the 2 uses of pos in your line. The compiler is free to evaluate the second pos before calling maxCoeff. If you initialize int pos = 42; it will make it more obvious.

like image 50
Marc Glisse Avatar answered Oct 20 '25 09:10

Marc Glisse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!