Enable custom scalar types in some unit tests.

This commit is contained in:
Gael Guennebaud 2016-07-20 15:19:17 +02:00
parent 87d480d785
commit 5e4dda8a12
4 changed files with 26 additions and 18 deletions

View File

@ -243,11 +243,13 @@ template<typename MatrixType> 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<RealScalar>::max_exponent10/8);
Matrix<Scalar,Dynamic,Dynamic> a = Matrix<Scalar,Dynamic,Dynamic>::Random(rows,rows);
Matrix<RealScalar,Dynamic,1> d = Matrix<RealScalar,Dynamic,1>::Random(rows);
for(Index k=0; k<rows; ++k)
d(k) = d(k)*std::pow(RealScalar(10),internal::random<RealScalar>(-s,s));
d(k) = d(k)*pow(RealScalar(10),internal::random<RealScalar>(-s,s));
SquareMatrixType A = a * d.asDiagonal() * a.adjoint();
// Make sure a solution exists:
vecX.setRandom();
@ -263,7 +265,7 @@ template<typename MatrixType> void cholesky(const MatrixType& m)
}
else
{
RealScalar large_tol = std::sqrt(test_precision<RealScalar>());
RealScalar large_tol = sqrt(test_precision<RealScalar>());
VERIFY((A * vecX).isApprox(vecB, large_tol));
++g_test_level;

View File

@ -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 T>
typename NumTraits<typename T::Scalar>::Real get_test_precision(const typename T::Scalar* = 0)
typename NumTraits<typename T::Scalar>::Real get_test_precision(const T*, const typename T::Scalar* = 0)
{
return test_precision<typename NumTraits<typename T::Scalar>::Real>();
}
template<typename T>
typename NumTraits<T>::Real get_test_precision(typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T>::Real>::value, T>::type* = 0)
typename NumTraits<T>::Real get_test_precision(const T*,typename internal::enable_if<internal::is_arithmetic<typename NumTraits<T>::Real>::value, T>::type* = 0)
{
return test_precision<typename NumTraits<T>::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<Type1>() << ", relative error is: " << test_relative_error(a,b) << std::endl;
std::cerr << "Difference too large wrt tolerance " << get_test_precision(static_cast<Type1*>(0)) << ", relative error is: " << test_relative_error(a,b) << std::endl;
}
return ret;
}

View File

@ -93,6 +93,7 @@ void cod_fixedsize() {
template<typename MatrixType> void qr()
{
using std::sqrt;
typedef typename MatrixType::Index Index;
Index rows = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE), cols2 = internal::random<Index>(2,EIGEN_TEST_MAX_SIZE);
@ -120,14 +121,14 @@ template<typename MatrixType> 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<Scalar>::epsilon();
sqrt(RealScalar(rows)) * numext::abs(r(0, 0)) * NumTraits<Scalar>::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<typename MatrixType> void qr()
template<typename MatrixType, int Cols2> 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<typename MatrixType, int Cols2> 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<Scalar>::epsilon();
sqrt(RealScalar(Rows)) * (std::abs)(r(0, 0)) * NumTraits<Scalar>::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<typename MatrixType, int Cols2> void qr_fixedsize()
// page 3 for more detail.
template<typename MatrixType> 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<typename MatrixType> void qr_kahan_matrix()
MatrixType r = qr.matrixQR().template triangularView<Upper>();
RealScalar threshold =
std::sqrt(RealScalar(rows)) * (std::abs)(r(0, 0)) * NumTraits<Scalar>::epsilon();
std::sqrt(RealScalar(rows)) * numext::abs(r(0, 0)) * NumTraits<Scalar>::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;

View File

@ -10,6 +10,7 @@
template<typename MatrixType>
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<RealScalar>(1,s);
Matrix<RealScalar,Dynamic,1> d = Matrix<RealScalar,Dynamic,1>::Random(diagSize);
for(Index k=0; k<diagSize; ++k)
d(k) = d(k)*std::pow(RealScalar(10),internal::random<RealScalar>(-s,s));
d(k) = d(k)*pow(RealScalar(10),internal::random<RealScalar>(-s,s));
bool dup = internal::random<int>(0,10) < 3;
bool unit_uv = internal::random<int>(0,10) < (dup?7:3); // if we duplicate some diagonal entries, then increase the chance to preserve them using unitary U and V factors