Re: [eigen] gtest helpers for eigen

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


Hi,

I didn't know about gtest and won't be able to reply regarding that
before this weekend.
But I can already comment on testing.h:

#define EXPECT_MATRIX_NEAR(a, b, tolerance) \
do { \
  bool dims_match = (a.rows() == b.rows()) && (a.cols() == b.cols()); \
  EXPECT_EQ(a.rows(), b.rows()) << "Matrix rows don't match."; \
  EXPECT_EQ(a.cols(), b.cols()) << "Matrix cols don't match."; \
  if (dims_match) { \
    for (int r = 0; r < a.rows(); ++r) { \
      for (int c = 0; c < a.cols(); ++c) { \
        EXPECT_NEAR(a(r, c), b(r, c), tolerance) \
          << "r=" << r << ", c=" << c << "."; \
      } \
    } \
  } \
} while(false);

This is taking the l^\infty norm to compare matrices. It's not bad,
but it's not the best norm either.
The best norm from a math point of view is the operator norm, but it's
extremely expensive to compute.
A good compromise, that we take in eigen, is to use the
Hilbert-Schmidt norm aka the L2 norm.
Anyway Eigen offers a.isApprox(b, tolerance) already, using the L2
norm; you could just use that.

#define EXPECT_MATRIX_NEAR_ZERO(a, tolerance) \
do { \
  for (int r = 0; r < a.rows(); ++r) { \
    for (int c = 0; c < a.cols(); ++c) { \
      EXPECT_NEAR(0.0, a(r, c), tolerance) \
        << "r=" << r << ", c=" << c << "."; \
    } \
  } \
} while(false);

This is potentially dangerous, as with floating point there shouldn't
be any "reference" order of magnitude.
So instead if "near_zero(tolerance)" the correct check is
"much_smaller_than(reference)" where reference depends on your context
-- there is no absolute prefered reference.

Cheers,
Benoit


2009/1/14 Keir Mierle <mierle@xxxxxxxxx>:
> Would anyone be interested in having a extensions for testing numeric code
> based on eigen2 with gtest? (http://code.google.com/p/googletest)
> I have added the following to
> libmv: http://code.google.com/p/libmv/source/browse/trunk/src/testing/testing.h,
> and it has proved very useful.
> I thought others might be interested. Perhaps have something similar in a
> contrib/ directory?
> Keir



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