From 5e4dda8a1201ada051fba9fc3cd0e645595238d5 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Wed, 20 Jul 2016 15:19:17 +0200 Subject: Enable custom scalar types in some unit tests. --- test/cholesky.cpp | 6 ++++-- test/main.h | 6 +++--- test/qr_colpivoting.cpp | 29 +++++++++++++++++------------ test/svd_fill.h | 3 ++- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/test/cholesky.cpp b/test/cholesky.cpp index b7abc230b..12efd2d60 100644 --- a/test/cholesky.cpp +++ b/test/cholesky.cpp @@ -243,11 +243,13 @@ template void cholesky(const MatrixType& m) // check matrices with a wide spectrum if(rows>=3) { + using std::pow; + using std::sqrt; RealScalar s = (std::min)(16,std::numeric_limits::max_exponent10/8); Matrix a = Matrix::Random(rows,rows); Matrix d = Matrix::Random(rows); for(Index k=0; k(-s,s)); + d(k) = d(k)*pow(RealScalar(10),internal::random(-s,s)); SquareMatrixType A = a * d.asDiagonal() * a.adjoint(); // Make sure a solution exists: vecX.setRandom(); @@ -263,7 +265,7 @@ template void cholesky(const MatrixType& m) } else { - RealScalar large_tol = std::sqrt(test_precision()); + RealScalar large_tol = sqrt(test_precision()); VERIFY((A * vecX).isApprox(vecB, large_tol)); ++g_test_level; diff --git a/test/main.h b/test/main.h index bda6d38c6..b435298cc 100644 --- a/test/main.h +++ b/test/main.h @@ -459,13 +459,13 @@ inline bool test_isApprox(const Type1& a, const Type2& b) // get_test_precision is a small wrapper to test_precision allowing to return the scalar precision for either scalars or expressions template -typename NumTraits::Real get_test_precision(const typename T::Scalar* = 0) +typename NumTraits::Real get_test_precision(const T*, const typename T::Scalar* = 0) { return test_precision::Real>(); } template -typename NumTraits::Real get_test_precision(typename internal::enable_if::Real>::value, T>::type* = 0) +typename NumTraits::Real get_test_precision(const T*,typename internal::enable_if::Real>::value, T>::type* = 0) { return test_precision::Real>(); } @@ -477,7 +477,7 @@ inline bool verifyIsApprox(const Type1& a, const Type2& b) bool ret = test_isApprox(a,b); if(!ret) { - std::cerr << "Difference too large wrt tolerance " << get_test_precision() << ", relative error is: " << test_relative_error(a,b) << std::endl; + std::cerr << "Difference too large wrt tolerance " << get_test_precision(static_cast(0)) << ", relative error is: " << test_relative_error(a,b) << std::endl; } return ret; } diff --git a/test/qr_colpivoting.cpp b/test/qr_colpivoting.cpp index 38de635a7..057bb014c 100644 --- a/test/qr_colpivoting.cpp +++ b/test/qr_colpivoting.cpp @@ -93,6 +93,7 @@ void cod_fixedsize() { template void qr() { + using std::sqrt; typedef typename MatrixType::Index Index; Index rows = internal::random(2,EIGEN_TEST_MAX_SIZE), cols = internal::random(2,EIGEN_TEST_MAX_SIZE), cols2 = internal::random(2,EIGEN_TEST_MAX_SIZE); @@ -120,14 +121,14 @@ template void qr() // Verify that the absolute value of the diagonal elements in R are // non-increasing until they reach the singularity threshold. RealScalar threshold = - std::sqrt(RealScalar(rows)) * (std::abs)(r(0, 0)) * NumTraits::epsilon(); + sqrt(RealScalar(rows)) * numext::abs(r(0, 0)) * NumTraits::epsilon(); for (Index i = 0; i < (std::min)(rows, cols) - 1; ++i) { - RealScalar x = (std::abs)(r(i, i)); - RealScalar y = (std::abs)(r(i + 1, i + 1)); + RealScalar x = numext::abs(r(i, i)); + RealScalar y = numext::abs(r(i + 1, i + 1)); if (x < threshold && y < threshold) continue; if (!test_isApproxOrLessThan(y, x)) { for (Index j = 0; j < (std::min)(rows, cols); ++j) { - std::cout << "i = " << j << ", |r_ii| = " << (std::abs)(r(j, j)) << std::endl; + std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl; } std::cout << "Failure at i=" << i << ", rank=" << rank << ", threshold=" << threshold << std::endl; @@ -144,6 +145,8 @@ template void qr() template void qr_fixedsize() { + using std::sqrt; + using std::abs; enum { Rows = MatrixType::RowsAtCompileTime, Cols = MatrixType::ColsAtCompileTime }; typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::RealScalar RealScalar; @@ -169,14 +172,14 @@ template void qr_fixedsize() // Verify that the absolute value of the diagonal elements in R are // non-increasing until they reache the singularity threshold. RealScalar threshold = - std::sqrt(RealScalar(Rows)) * (std::abs)(r(0, 0)) * NumTraits::epsilon(); + sqrt(RealScalar(Rows)) * (std::abs)(r(0, 0)) * NumTraits::epsilon(); for (Index i = 0; i < (std::min)(int(Rows), int(Cols)) - 1; ++i) { - RealScalar x = (std::abs)(r(i, i)); - RealScalar y = (std::abs)(r(i + 1, i + 1)); + RealScalar x = numext::abs(r(i, i)); + RealScalar y = numext::abs(r(i + 1, i + 1)); if (x < threshold && y < threshold) continue; if (!test_isApproxOrLessThan(y, x)) { for (Index j = 0; j < (std::min)(int(Rows), int(Cols)); ++j) { - std::cout << "i = " << j << ", |r_ii| = " << (std::abs)(r(j, j)) << std::endl; + std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl; } std::cout << "Failure at i=" << i << ", rank=" << rank << ", threshold=" << threshold << std::endl; @@ -194,6 +197,8 @@ template void qr_fixedsize() // page 3 for more detail. template void qr_kahan_matrix() { + using std::sqrt; + using std::abs; typedef typename MatrixType::Index Index; typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::RealScalar RealScalar; @@ -215,14 +220,14 @@ template void qr_kahan_matrix() MatrixType r = qr.matrixQR().template triangularView(); RealScalar threshold = - std::sqrt(RealScalar(rows)) * (std::abs)(r(0, 0)) * NumTraits::epsilon(); + std::sqrt(RealScalar(rows)) * numext::abs(r(0, 0)) * NumTraits::epsilon(); for (Index i = 0; i < (std::min)(rows, cols) - 1; ++i) { - RealScalar x = (std::abs)(r(i, i)); - RealScalar y = (std::abs)(r(i + 1, i + 1)); + RealScalar x = numext::abs(r(i, i)); + RealScalar y = numext::abs(r(i + 1, i + 1)); if (x < threshold && y < threshold) continue; if (!test_isApproxOrLessThan(y, x)) { for (Index j = 0; j < (std::min)(rows, cols); ++j) { - std::cout << "i = " << j << ", |r_ii| = " << (std::abs)(r(j, j)) << std::endl; + std::cout << "i = " << j << ", |r_ii| = " << numext::abs(r(j, j)) << std::endl; } std::cout << "Failure at i=" << i << ", rank=" << qr.rank() << ", threshold=" << threshold << std::endl; diff --git a/test/svd_fill.h b/test/svd_fill.h index 500954d47..e0951ca57 100644 --- a/test/svd_fill.h +++ b/test/svd_fill.h @@ -10,6 +10,7 @@ template void svd_fill_random(MatrixType &m, int Option = 0) { + using std::pow; typedef typename MatrixType::Scalar Scalar; typedef typename MatrixType::RealScalar RealScalar; typedef typename MatrixType::Index Index; @@ -18,7 +19,7 @@ void svd_fill_random(MatrixType &m, int Option = 0) s = internal::random(1,s); Matrix d = Matrix::Random(diagSize); for(Index k=0; k(-s,s)); + d(k) = d(k)*pow(RealScalar(10),internal::random(-s,s)); bool dup = internal::random(0,10) < 3; bool unit_uv = internal::random(0,10) < (dup?7:3); // if we duplicate some diagonal entries, then increase the chance to preserve them using unitary U and V factors -- cgit v1.2.3