diff options
author | Gael Guennebaud <g.gael@free.fr> | 2011-02-18 17:39:04 +0100 |
---|---|---|
committer | Gael Guennebaud <g.gael@free.fr> | 2011-02-18 17:39:04 +0100 |
commit | 86ca05b324f3f27249eb74b5ec839e2c331c8bbb (patch) | |
tree | 6bc852a2e421e7a6cd0037f1a5b57c5321775687 /test | |
parent | 8f8c67b8bd2915d1927c848e557e5076a00e25f9 (diff) |
remove largeEps in adjoint unit test and use a more accurate test_isApproxWithRef test.
Diffstat (limited to 'test')
-rw-r--r-- | test/adjoint.cpp | 20 | ||||
-rw-r--r-- | test/main.h | 12 |
2 files changed, 22 insertions, 10 deletions
diff --git a/test/adjoint.cpp b/test/adjoint.cpp index bed409191..bc0bad4d0 100644 --- a/test/adjoint.cpp +++ b/test/adjoint.cpp @@ -40,10 +40,6 @@ template<typename MatrixType> void adjoint(const MatrixType& m) Index rows = m.rows(); Index cols = m.cols(); - RealScalar largerEps = test_precision<RealScalar>(); - if (internal::is_same<RealScalar,float>::value) - largerEps = RealScalar(1e-3f); - MatrixType m1 = MatrixType::Random(rows, cols), m2 = MatrixType::Random(rows, cols), m3(rows, cols), @@ -68,8 +64,10 @@ template<typename MatrixType> void adjoint(const MatrixType& m) // check basic properties of dot, norm, norm2 typedef typename NumTraits<Scalar>::Real RealScalar; - VERIFY(internal::isApprox((s1 * v1 + s2 * v2).dot(v3), internal::conj(s1) * v1.dot(v3) + internal::conj(s2) * v2.dot(v3), largerEps)); - VERIFY(internal::isApprox(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), largerEps)); + + RealScalar ref = NumTraits<Scalar>::IsInteger ? 0 : std::max((s1 * v1 + s2 * v2).norm(),v3.norm()); + VERIFY(test_isApproxWithRef((s1 * v1 + s2 * v2).dot(v3), internal::conj(s1) * v1.dot(v3) + internal::conj(s2) * v2.dot(v3), ref)); + VERIFY(test_isApproxWithRef(v3.dot(s1 * v1 + s2 * v2), s1*v3.dot(v1)+s2*v3.dot(v2), ref)); VERIFY_IS_APPROX(internal::conj(v1.dot(v2)), v2.dot(v1)); VERIFY_IS_APPROX(internal::real(v1.dot(v1)), v1.squaredNorm()); if(!NumTraits<Scalar>::IsInteger) @@ -77,7 +75,9 @@ template<typename MatrixType> void adjoint(const MatrixType& m) VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(vzero.dot(v1)), static_cast<RealScalar>(1)); // check compatibility of dot and adjoint - VERIFY(internal::isApprox(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), largerEps)); + + ref = NumTraits<Scalar>::IsInteger ? 0 : std::max(std::max(v1.norm(),v2.norm()),std::max((square * v2).norm(),(square.adjoint() * v1).norm())); + VERIFY(test_isApproxWithRef(v1.dot(square * v2), (square.adjoint() * v1).dot(v2), ref)); // like in testBasicStuff, test operator() to check const-qualification Index r = internal::random<Index>(0, rows-1), @@ -119,9 +119,9 @@ void test_adjoint() CALL_SUBTEST_1( adjoint(Matrix<float, 1, 1>()) ); CALL_SUBTEST_2( adjoint(Matrix3d()) ); CALL_SUBTEST_3( adjoint(Matrix4f()) ); - CALL_SUBTEST_4( adjoint(MatrixXcf(4, 4)) ); - CALL_SUBTEST_5( adjoint(MatrixXi(8, 12)) ); - CALL_SUBTEST_6( adjoint(MatrixXf(21, 21)) ); + CALL_SUBTEST_4( adjoint(MatrixXcf(internal::random<int>(1,50), internal::random<int>(1,50))) ); + CALL_SUBTEST_5( adjoint(MatrixXi(internal::random<int>(1,50), internal::random<int>(1,50))) ); + CALL_SUBTEST_6( adjoint(MatrixXf(internal::random<int>(1,50), internal::random<int>(1,50))) ); } // test a large matrix only once CALL_SUBTEST_7( adjoint(Matrix<float, 100, 100>()) ); diff --git a/test/main.h b/test/main.h index 671555aa3..51eca5453 100644 --- a/test/main.h +++ b/test/main.h @@ -346,6 +346,18 @@ inline bool test_isApprox(const Type1& a, const Type2& b) return a.isApprox(b, test_precision<typename Type1::Scalar>()); } +// The idea behind this function is to compare the two scalars a and b where +// the scalar ref is a hint about the expected order of magnitude of a and b. +// Therefore, if for some reason a and b are very small compared to ref, +// we won't issue a false negative. +// This test could be: abs(a-b) <= eps * ref +// However, it seems that simply comparing a+ref and b+ref is more sensitive to true error. +template<typename Scalar,typename ScalarRef> +inline bool test_isApproxWithRef(const Scalar& a, const Scalar& b, const ScalarRef& ref) +{ + return test_isApprox(a+ref, b+ref); +} + template<typename Derived1, typename Derived2> inline bool test_isMuchSmallerThan(const MatrixBase<Derived1>& m1, const MatrixBase<Derived2>& m2) |