This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen

Bug 1061

Summary: Matrix operator== does not check for correct size and returns true for empty matrix comparisons.
Product: Eigen Reporter: Simon Lynen <slynen>
Component: Core - generalAssignee: Nobody <eigen.nobody>
Status: DECISIONNEEDED ---    
Severity: Unknown CC: chtz, gael.guennebaud, jacob.benoit.1
Priority: Normal Keywords: Documentation
Version: 3.2   
Hardware: All   
OS: All   
Whiteboard:

Description Simon Lynen 2015-08-30 00:04:35 UTC
Operator == does not check for size and incorrectly returns true for comparison of (partically) dynamic matrix with random matrix.

TEST(A, B) {
  typedef Eigen::Matrix<double, 10, Eigen::Dynamic> Type;
  Type a;
  a.setRandom(10, 5);
  Type b;
  EXPECT_FALSE(a == b);  // Fails.
  Type c;
  c.setRandom(10, 5);
  EXPECT_FALSE(a == c);  // Passes.
}

TEST(A, C) {
  typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> Type;
  Type a;
  a.setRandom(10, 5);
  Type b;
  EXPECT_FALSE(a == b);  // Fails.
  Type c;
  c.setRandom(10, 5);
  EXPECT_FALSE(a == c);  // Passes.
}
Comment 1 Christoph Hertzberg 2015-08-31 11:53:04 UTC
So far, we had a==b be equivalent to a.cwiseEqual(b).all(), which means we assume (and assert) that the sizes match. But checking for matching sizes before that actually makes sense (and I doubt it would break anything). Should we want to keep the current behavior, we must at least document it.
If we change it, we should suggest the alternative .cwiseEqual().all(), in case someone really wishes to avoid the minor overhead of checking the dimension first.
Comment 2 Gael Guennebaud 2015-09-01 14:40:33 UTC
Such a change would make operator== equivalent to Matlab's isequal behavior, so why not.

Another solution would be to add a isEqual() member (and maybe even a isEqualN() to deal with NaN) mimicking Matlab's isequal*, but if we do so, then for consistency we should probably also update isApprox to check for the size first.
Comment 3 Nobody 2019-12-04 14:53:30 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/1061.