You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.1 KiB

  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 T> std::string type_name() { return "other"; }
  13. template<> std::string type_name<float>() { return "float"; }
  14. template<> std::string type_name<double>() { return "double"; }
  15. template<> std::string type_name<int>() { return "int"; }
  16. template<> std::string type_name<std::complex<float> >() { return "complex<float>"; }
  17. template<> std::string type_name<std::complex<double> >() { return "complex<double>"; }
  18. template<> std::string type_name<std::complex<int> >() { return "complex<int>"; }
  19. #define EIGEN_DEBUG_VAR(x) std::cerr << #x << " = " << x << std::endl;
  20. template<typename T> inline typename NumTraits<T>::Real epsilon()
  21. {
  22. return std::numeric_limits<typename NumTraits<T>::Real>::epsilon();
  23. }
  24. template<typename MatrixType> void inverse_permutation_4x4()
  25. {
  26. typedef typename MatrixType::Scalar Scalar;
  27. typedef typename MatrixType::RealScalar RealScalar;
  28. Vector4i indices(0,1,2,3);
  29. for(int i = 0; i < 24; ++i)
  30. {
  31. MatrixType m = MatrixType::Zero();
  32. m(indices(0),0) = 1;
  33. m(indices(1),1) = 1;
  34. m(indices(2),2) = 1;
  35. m(indices(3),3) = 1;
  36. MatrixType inv = m.inverse();
  37. double error = double( (m*inv-MatrixType::Identity()).norm() / epsilon<Scalar>() );
  38. VERIFY(error == 0.0);
  39. std::next_permutation(indices.data(),indices.data()+4);
  40. }
  41. }
  42. template<typename MatrixType> void inverse_general_4x4(int repeat)
  43. {
  44. typedef typename MatrixType::Scalar Scalar;
  45. typedef typename MatrixType::RealScalar RealScalar;
  46. double error_sum = 0., error_max = 0.;
  47. for(int i = 0; i < repeat; ++i)
  48. {
  49. MatrixType m;
  50. RealScalar absdet;
  51. do {
  52. m = MatrixType::Random();
  53. absdet = ei_abs(m.determinant());
  54. } while(absdet < 10 * epsilon<Scalar>());
  55. MatrixType inv = m.inverse();
  56. double error = double( (m*inv-MatrixType::Identity()).norm() * absdet / epsilon<Scalar>() );
  57. error_sum += error;
  58. error_max = std::max(error_max, error);
  59. }
  60. std::cerr << "inverse_general_4x4, Scalar = " << type_name<Scalar>() << std::endl;
  61. double error_avg = error_sum / repeat;
  62. EIGEN_DEBUG_VAR(error_avg);
  63. EIGEN_DEBUG_VAR(error_max);
  64. VERIFY(error_avg < (NumTraits<Scalar>::IsComplex ? 8.0 : 1.25));
  65. VERIFY(error_max < (NumTraits<Scalar>::IsComplex ? 64.0 : 20.0));
  66. }
  67. void test_eigen2_prec_inverse_4x4()
  68. {
  69. CALL_SUBTEST_1((inverse_permutation_4x4<Matrix4f>()));
  70. CALL_SUBTEST_1(( inverse_general_4x4<Matrix4f>(200000 * g_repeat) ));
  71. CALL_SUBTEST_2((inverse_permutation_4x4<Matrix<double,4,4,RowMajor> >()));
  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. }