prec_inverse_4x4.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <Eigen/LU>
  11. #include <algorithm>
  12. template<typename MatrixType> void inverse_permutation_4x4()
  13. {
  14. typedef typename MatrixType::Scalar Scalar;
  15. Vector4i indices(0,1,2,3);
  16. for(int i = 0; i < 24; ++i)
  17. {
  18. MatrixType m = PermutationMatrix<4>(indices);
  19. MatrixType inv = m.inverse();
  20. double error = double( (m*inv-MatrixType::Identity()).norm() / NumTraits<Scalar>::epsilon() );
  21. EIGEN_DEBUG_VAR(error)
  22. VERIFY(error == 0.0);
  23. std::next_permutation(indices.data(),indices.data()+4);
  24. }
  25. }
  26. template<typename MatrixType> void inverse_general_4x4(int repeat)
  27. {
  28. using std::abs;
  29. typedef typename MatrixType::Scalar Scalar;
  30. double error_sum = 0., error_max = 0.;
  31. for(int i = 0; i < repeat; ++i)
  32. {
  33. MatrixType m;
  34. bool is_invertible;
  35. do {
  36. m = MatrixType::Random();
  37. is_invertible = Eigen::FullPivLU<MatrixType>(m).isInvertible();
  38. } while(!is_invertible);
  39. MatrixType inv = m.inverse();
  40. double error = double( (m*inv-MatrixType::Identity()).norm());
  41. error_sum += error;
  42. error_max = (std::max)(error_max, error);
  43. }
  44. std::cerr << "inverse_general_4x4, Scalar = " << type_name<Scalar>() << std::endl;
  45. double error_avg = error_sum / repeat;
  46. EIGEN_DEBUG_VAR(error_avg);
  47. EIGEN_DEBUG_VAR(error_max);
  48. // FIXME that 1.25 used to be a 1.0 until the NumTraits changes on 28 April 2010, what's going wrong??
  49. // FIXME that 1.25 used to be 1.2 until we tested gcc 4.1 on 30 June 2010 and got 1.21.
  50. VERIFY(error_avg < (NumTraits<Scalar>::IsComplex ? 8.0 : 1.25));
  51. VERIFY(error_max < (NumTraits<Scalar>::IsComplex ? 64.0 : 20.0));
  52. {
  53. int s = 5;//internal::random<int>(4,10);
  54. int i = 0;//internal::random<int>(0,s-4);
  55. int j = 0;//internal::random<int>(0,s-4);
  56. Matrix<Scalar,5,5> mat(s,s);
  57. mat.setRandom();
  58. MatrixType submat = mat.template block<4,4>(i,j);
  59. MatrixType mat_inv = mat.template block<4,4>(i,j).inverse();
  60. VERIFY_IS_APPROX(mat_inv, submat.inverse());
  61. mat.template block<4,4>(i,j) = submat.inverse();
  62. VERIFY_IS_APPROX(mat_inv, (mat.template block<4,4>(i,j)));
  63. }
  64. }
  65. EIGEN_DECLARE_TEST(prec_inverse_4x4)
  66. {
  67. CALL_SUBTEST_1((inverse_permutation_4x4<Matrix4f>()));
  68. CALL_SUBTEST_1(( inverse_general_4x4<Matrix4f>(200000 * g_repeat) ));
  69. CALL_SUBTEST_1(( inverse_general_4x4<Matrix<float,4,4,RowMajor> >(200000 * g_repeat) ));
  70. CALL_SUBTEST_2((inverse_permutation_4x4<Matrix<double,4,4,RowMajor> >()));
  71. CALL_SUBTEST_2(( inverse_general_4x4<Matrix<double,4,4,ColMajor> >(200000 * g_repeat) ));
  72. CALL_SUBTEST_2(( inverse_general_4x4<Matrix<double,4,4,RowMajor> >(200000 * g_repeat) ));
  73. CALL_SUBTEST_3((inverse_permutation_4x4<Matrix4cf>()));
  74. CALL_SUBTEST_3((inverse_general_4x4<Matrix4cf>(50000 * g_repeat)));
  75. }