From fc7f39980c7ce66764ddf0ef6a98bf5659513145 Mon Sep 17 00:00:00 2001 From: Benoit Jacob Date: Mon, 23 Nov 2009 10:27:10 -0500 Subject: [PATCH] backport improvement in 4x4 inverse precisio, and rigorous precision test --- Eigen/src/LU/Inverse.h | 7 ++- test/CMakeLists.txt | 2 + test/prec_inverse_4x4.cpp | 103 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 test/prec_inverse_4x4.cpp diff --git a/Eigen/src/LU/Inverse.h b/Eigen/src/LU/Inverse.h index 1e2da88d2..2f15f0953 100644 --- a/Eigen/src/LU/Inverse.h +++ b/Eigen/src/LU/Inverse.h @@ -133,8 +133,11 @@ void ei_compute_inverse_in_size4_case(const MatrixType& _matrix, MatrixType* res int good_row0, good_row1, good_i; Matrix absdet; - // any 2x2 block with determinant above this threshold will be considered good enough - RealScalar d = (matrix.col(0).squaredNorm()+matrix.col(1).squaredNorm()) * RealScalar(1e-2); + // any 2x2 block with determinant above this threshold will be considered good enough. + // The magic value 1e-1 here comes from experimentation. The bigger it is, the higher the precision, + // the slower the computation. This value 1e-1 gives precision almost as good as the brutal cofactors + // algorithm, both in average and in worst-case precision. + RealScalar d = (matrix.col(0).squaredNorm()+matrix.col(1).squaredNorm()) * RealScalar(1e-1); #define ei_inv_size4_helper_macro(i,row0,row1) \ absdet[i] = ei_abs(matrix.coeff(row0,0)*matrix.coeff(row1,1) \ - matrix.coeff(row0,1)*matrix.coeff(row1,0)); \ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c5868bed4..ffcd77654 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -222,6 +222,8 @@ ei_add_test(sparse_product) ei_add_test(swap) ei_add_test(visitor) +ei_add_test(prec_inverse_4x4 ${EI_OFLAG}) + # print a summary of the different options message("************************************************************") message("*** Eigen's unit tests configuration summary ***") diff --git a/test/prec_inverse_4x4.cpp b/test/prec_inverse_4x4.cpp new file mode 100644 index 000000000..e29bc3ca2 --- /dev/null +++ b/test/prec_inverse_4x4.cpp @@ -0,0 +1,103 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2009 Benoit Jacob +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#include "main.h" +#include +#include + +template std::string type_name() { return "other"; } +template<> std::string type_name() { return "float"; } +template<> std::string type_name() { return "double"; } +template<> std::string type_name() { return "int"; } +template<> std::string type_name >() { return "complex"; } +template<> std::string type_name >() { return "complex"; } +template<> std::string type_name >() { return "complex"; } + +#define EIGEN_DEBUG_VAR(x) std::cerr << #x << " = " << x << std::endl; + +template inline typename NumTraits::Real epsilon() +{ + return std::numeric_limits::Real>::epsilon(); +} + +template void inverse_permutation_4x4() +{ + typedef typename MatrixType::Scalar Scalar; + typedef typename MatrixType::RealScalar RealScalar; + double error_max = 0.; + Vector4i indices(0,1,2,3); + for(int i = 0; i < 24; ++i) + { + MatrixType m = MatrixType::Zero(); + m(indices(0),0) = 1; + m(indices(1),1) = 1; + m(indices(2),2) = 1; + m(indices(3),3) = 1; + MatrixType inv = m.inverse(); + double error = double( (m*inv-MatrixType::Identity()).norm() / epsilon() ); + error_max = std::max(error_max, error); + std::next_permutation(indices.data(),indices.data()+4); + } + std::cerr << "inverse_permutation_4x4, Scalar = " << type_name() << std::endl; + EIGEN_DEBUG_VAR(error_max); + VERIFY(error_max < (NumTraits::IsComplex ? 150.0 : 60.) ); +} + +template void inverse_general_4x4(int repeat) +{ + typedef typename MatrixType::Scalar Scalar; + typedef typename MatrixType::RealScalar RealScalar; + double error_sum = 0., error_max = 0.; + for(int i = 0; i < repeat; ++i) + { + MatrixType m; + RealScalar absdet; + do { + m = MatrixType::Random(); + absdet = ei_abs(m.determinant()); + } while(absdet == RealScalar(0)); + MatrixType inv = m.inverse(); + double error = double( (m*inv-MatrixType::Identity()).norm() * absdet / epsilon() ); + error_sum += error; + error_max = std::max(error_max, error); + } + std::cerr << "inverse_general_4x4, Scalar = " << type_name() << std::endl; + double error_avg = error_sum / repeat; + EIGEN_DEBUG_VAR(error_avg); + EIGEN_DEBUG_VAR(error_max); + VERIFY(error_avg < (NumTraits::IsComplex ? 8.4 : 1.4) ); + VERIFY(error_max < (NumTraits::IsComplex ? 150.0 : 60.) ); +} + +void test_prec_inverse_4x4() +{ + CALL_SUBTEST((inverse_permutation_4x4())); + CALL_SUBTEST(( inverse_general_4x4(200000 * g_repeat) )); + + CALL_SUBTEST((inverse_permutation_4x4 >())); + CALL_SUBTEST(( inverse_general_4x4 >(200000 * g_repeat) )); + + CALL_SUBTEST((inverse_permutation_4x4())); + CALL_SUBTEST((inverse_general_4x4(50000 * g_repeat))); +}