diff options
author | Benoit Jacob <jacob.benoit.1@gmail.com> | 2009-11-23 10:13:21 -0500 |
---|---|---|
committer | Benoit Jacob <jacob.benoit.1@gmail.com> | 2009-11-23 10:13:21 -0500 |
commit | 44d0d667cd629a18e29b9c248633891c1b04f75c (patch) | |
tree | 9df7106246634e052c2686371f20e1c07325eeb0 | |
parent | 06f11f337951982f240c161933229812c391e979 (diff) |
4x4 inverse:
* change block selection threshold from 1e-2 to 1e-1
* add rigorous precision test
-rw-r--r-- | Eigen/src/LU/Inverse.h | 7 | ||||
-rw-r--r-- | test/CMakeLists.txt | 2 | ||||
-rw-r--r-- | test/inverse.cpp | 8 | ||||
-rw-r--r-- | test/main.h | 13 | ||||
-rw-r--r-- | test/prec_inverse_4x4.cpp | 84 |
5 files changed, 100 insertions, 14 deletions
diff --git a/Eigen/src/LU/Inverse.h b/Eigen/src/LU/Inverse.h index 306b5f60a..9d5e86845 100644 --- a/Eigen/src/LU/Inverse.h +++ b/Eigen/src/LU/Inverse.h @@ -235,8 +235,11 @@ struct ei_compute_inverse<MatrixType, ResultType, 4> int good_row0, good_row1, good_i; Matrix<RealScalar,6,1> 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 30668a2aa..c91ec328b 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -160,6 +160,8 @@ ei_add_test(swap) ei_add_test(conservative_resize) ei_add_test(permutationmatrices) +ei_add_test(prec_inverse_4x4) + ei_add_property(EIGEN_TESTING_SUMMARY "CXX: ${CMAKE_CXX_COMPILER}\n") if(CMAKE_COMPILER_IS_GNUCXX) execute_process(COMMAND ${CMAKE_CXX_COMPILER} --version COMMAND head -n 1 OUTPUT_VARIABLE EIGEN_CXX_VERSION_STRING OUTPUT_STRIP_TRAILING_WHITESPACE) diff --git a/test/inverse.cpp b/test/inverse.cpp index 3ed61d356..59b791507 100644 --- a/test/inverse.cpp +++ b/test/inverse.cpp @@ -104,12 +104,4 @@ void test_inverse() s = ei_random<int>(25,100); CALL_SUBTEST_6( inverse(MatrixXcd(s,s)) ); } - -#ifdef EIGEN_TEST_PART_4 - // test some tricky cases for 4x4 matrices - VERIFY_IS_APPROX((Matrix4f() << 0,0,1,0, 1,0,0,0, 0,1,0,0, 0,0,0,1).finished().inverse(), - (Matrix4f() << 0,1,0,0, 0,0,1,0, 1,0,0,0, 0,0,0,1).finished()); - VERIFY_IS_APPROX((Matrix4f() << 1,0,0,0, 0,0,1,0, 0,0,0,1, 0,1,0,0).finished().inverse(), - (Matrix4f() << 1,0,0,0, 0,0,0,1, 0,1,0,0, 0,0,1,0).finished()); -#endif } diff --git a/test/main.h b/test/main.h index 0b9b0bc4c..3ae573c1e 100644 --- a/test/main.h +++ b/test/main.h @@ -372,6 +372,14 @@ template<> struct GetDifferentType<double> { typedef float type; }; template<typename T> struct GetDifferentType<std::complex<T> > { typedef std::complex<typename GetDifferentType<T>::type> type; }; +template<typename T> std::string type_name() { return "other"; } +template<> std::string type_name<float>() { return "float"; } +template<> std::string type_name<double>() { return "double"; } +template<> std::string type_name<int>() { return "int"; } +template<> std::string type_name<std::complex<float> >() { return "complex<float>"; } +template<> std::string type_name<std::complex<double> >() { return "complex<double>"; } +template<> std::string type_name<std::complex<int> >() { return "complex<int>"; } + // forward declaration of the main test function void EIGEN_CAT(test_,EIGEN_TEST_FUNC)(); @@ -444,7 +452,4 @@ int main(int argc, char *argv[]) EIGEN_CAT(test_,EIGEN_TEST_FUNC)(); return 0; -} - - - +}
\ No newline at end of file diff --git a/test/prec_inverse_4x4.cpp b/test/prec_inverse_4x4.cpp new file mode 100644 index 000000000..77763f830 --- /dev/null +++ b/test/prec_inverse_4x4.cpp @@ -0,0 +1,84 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. +// +// Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com> +// +// 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 <http://www.gnu.org/licenses/>. + +#include "main.h" +#include <Eigen/LU> +#include <algorithm> + +template<typename MatrixType> 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 = PermutationMatrix<4>(indices); + MatrixType inv = m.inverse(); + double error = double( (m*inv-MatrixType::Identity()).norm() / epsilon<Scalar>() ); + error_max = std::max(error_max, error); + std::next_permutation(indices.data(),indices.data()+4); + } + std::cerr << "inverse_permutation_4x4, Scalar = " << type_name<Scalar>() << std::endl; + EIGEN_DEBUG_VAR(error_max); + VERIFY(error_max < (NumTraits<Scalar>::IsComplex ? 150.0 : 60.) ); +} + +template<typename MatrixType> 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<Scalar>() ); + error_sum += error; + error_max = std::max(error_max, error); + } + std::cerr << "inverse_general_4x4, Scalar = " << type_name<Scalar>() << std::endl; + double error_avg = error_sum / repeat; + EIGEN_DEBUG_VAR(error_avg); + EIGEN_DEBUG_VAR(error_max); + VERIFY(error_avg < (NumTraits<Scalar>::IsComplex ? 8.4 : 1.4) ); + VERIFY(error_max < (NumTraits<Scalar>::IsComplex ? 150.0 : 60.) ); +} + +void test_prec_inverse_4x4() +{ + CALL_SUBTEST_1((inverse_permutation_4x4<Matrix4f>())); + CALL_SUBTEST_1(( inverse_general_4x4<Matrix4f>(200000 * g_repeat) )); + + CALL_SUBTEST_2((inverse_permutation_4x4<Matrix<double,4,4,RowMajor> >())); + CALL_SUBTEST_2(( inverse_general_4x4<Matrix<double,4,4,RowMajor> >(200000 * g_repeat) )); + + CALL_SUBTEST_3((inverse_permutation_4x4<Matrix4cf>())); + CALL_SUBTEST_3((inverse_general_4x4<Matrix4cf>(50000 * g_repeat))); +} |