Re: [eigen] on fuzzy comparisons |
[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]
Hi, Cf attached diff, I tried reimplementing the matrix fuzzy compares using the Hilbert-Schmidt norm a.k.a. L2-norm. The result however is that 7 tests fail out of 17. So, I believe that indeed the Hilbert-Schmidt norm is less good a choice than the current implementation of matrix fuzzy compares. I then tried using the matrixNorm, but the compilation took far too long so I didn't even wait for it to complete. I had made sure, though, that EigenSolver and Product types were instantiated only once per Eval type. So, I think I'll just leave things as-is. Cheers, Benoit On Thursday 05 June 2008 19:24:15 Gael Guennebaud wrote: > What about the Frobenius norm which simply considers the matrix as a 1D > vector and compute the L2 norm ? It is also invariant under rotation and > very cheap.
Index: Eigen/src/Core/Fuzzy.h =================================================================== --- Eigen/src/Core/Fuzzy.h (revision 817437) +++ Eigen/src/Core/Fuzzy.h (working copy) @@ -125,21 +125,17 @@ { EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Derived,OtherDerived); ei_assert(self.rows() == other.rows() && self.cols() == other.cols()); - typename Derived::Nested nested(self); - typename OtherDerived::Nested otherNested(other); - for(int i = 0; i < self.cols(); i++) - if((nested.col(i) - otherNested.col(i)).norm2() - > std::min(nested.col(i).norm2(), otherNested.col(i).norm2()) * prec * prec) - return false; - return true; + typename ei_nested<Derived,2>::type nested(self); + typename ei_nested<OtherDerived,2>::type otherNested(other); + RealScalar nestedSquareNorm = nested.cwiseAbs2().sum(); + RealScalar otherNestedSquareNorm = otherNested.cwiseAbs2().sum(); + RealScalar diffSquareNorm = (nested - otherNested).cwiseAbs2().sum(); + return diffSquareNorm < prec * prec * std::min(nestedSquareNorm, otherNestedSquareNorm); } static bool isMuchSmallerThan(const Derived& self, const RealScalar& other, RealScalar prec) { - typename Derived::Nested nested(self); - for(int i = 0; i < self.cols(); i++) - if(nested.col(i).norm2() > ei_abs2(other * prec)) - return false; - return true; + RealScalar x = prec * other; + return self.cwiseAbs2().sum() < x * x; } static bool isMuchSmallerThan(const Derived& self, const OtherDerived& other, RealScalar prec) { @@ -147,10 +143,9 @@ ei_assert(self.rows() == other.rows() && self.cols() == other.cols()); typename Derived::Nested nested(self); typename OtherDerived::Nested otherNested(other); - for(int i = 0; i < self.cols(); i++) - if(nested.col(i).norm2() > otherNested.col(i).norm2() * prec * prec) - return false; - return true; + RealScalar nestedSquareNorm = nested.cwiseAbs2().sum(); + RealScalar otherNestedSquareNorm = otherNested.cwiseAbs2().sum(); + return nestedSquareNorm < prec * prec * otherNestedSquareNorm; } };
Attachment:
signature.asc
Description: This is a digitally signed message part.
Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |