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.

102 lines
3.3 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  6. //
  7. // This Source Code Form is subject to the terms of the Mozilla
  8. // Public License v. 2.0. If a copy of the MPL was not distributed
  9. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  10. #include "main.h"
  11. #include <Eigen/LU>
  12. template<typename MatrixType> void inverse(const MatrixType& m)
  13. {
  14. typedef typename MatrixType::Index Index;
  15. /* this test covers the following files:
  16. Inverse.h
  17. */
  18. Index rows = m.rows();
  19. Index cols = m.cols();
  20. typedef typename MatrixType::Scalar Scalar;
  21. typedef typename NumTraits<Scalar>::Real RealScalar;
  22. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
  23. MatrixType m1(rows, cols),
  24. m2(rows, cols),
  25. identity = MatrixType::Identity(rows, rows);
  26. createRandomPIMatrixOfRank(rows,rows,rows,m1);
  27. m2 = m1.inverse();
  28. VERIFY_IS_APPROX(m1, m2.inverse() );
  29. VERIFY_IS_APPROX((Scalar(2)*m2).inverse(), m2.inverse()*Scalar(0.5));
  30. VERIFY_IS_APPROX(identity, m1.inverse() * m1 );
  31. VERIFY_IS_APPROX(identity, m1 * m1.inverse() );
  32. VERIFY_IS_APPROX(m1, m1.inverse().inverse() );
  33. // since for the general case we implement separately row-major and col-major, test that
  34. VERIFY_IS_APPROX(MatrixType(m1.transpose().inverse()), MatrixType(m1.inverse().transpose()));
  35. #if !defined(EIGEN_TEST_PART_5) && !defined(EIGEN_TEST_PART_6)
  36. //computeInverseAndDetWithCheck tests
  37. //First: an invertible matrix
  38. bool invertible;
  39. RealScalar det;
  40. m2.setZero();
  41. m1.computeInverseAndDetWithCheck(m2, det, invertible);
  42. VERIFY(invertible);
  43. VERIFY_IS_APPROX(identity, m1*m2);
  44. VERIFY_IS_APPROX(det, m1.determinant());
  45. m2.setZero();
  46. m1.computeInverseWithCheck(m2, invertible);
  47. VERIFY(invertible);
  48. VERIFY_IS_APPROX(identity, m1*m2);
  49. //Second: a rank one matrix (not invertible, except for 1x1 matrices)
  50. VectorType v3 = VectorType::Random(rows);
  51. MatrixType m3 = v3*v3.transpose(), m4(rows,cols);
  52. m3.computeInverseAndDetWithCheck(m4, det, invertible);
  53. VERIFY( rows==1 ? invertible : !invertible );
  54. VERIFY_IS_MUCH_SMALLER_THAN(internal::abs(det-m3.determinant()), RealScalar(1));
  55. m3.computeInverseWithCheck(m4, invertible);
  56. VERIFY( rows==1 ? invertible : !invertible );
  57. #endif
  58. // check in-place inversion
  59. if(MatrixType::RowsAtCompileTime>=2 && MatrixType::RowsAtCompileTime<=4)
  60. {
  61. // in-place is forbidden
  62. VERIFY_RAISES_ASSERT(m1 = m1.inverse());
  63. }
  64. else
  65. {
  66. m2 = m1.inverse();
  67. m1 = m1.inverse();
  68. VERIFY_IS_APPROX(m1,m2);
  69. }
  70. }
  71. void test_inverse()
  72. {
  73. int s;
  74. for(int i = 0; i < g_repeat; i++) {
  75. CALL_SUBTEST_1( inverse(Matrix<double,1,1>()) );
  76. CALL_SUBTEST_2( inverse(Matrix2d()) );
  77. CALL_SUBTEST_3( inverse(Matrix3f()) );
  78. CALL_SUBTEST_4( inverse(Matrix4f()) );
  79. CALL_SUBTEST_4( inverse(Matrix<float,4,4,DontAlign>()) );
  80. s = internal::random<int>(50,320);
  81. CALL_SUBTEST_5( inverse(MatrixXf(s,s)) );
  82. s = internal::random<int>(25,100);
  83. CALL_SUBTEST_6( inverse(MatrixXcd(s,s)) );
  84. CALL_SUBTEST_7( inverse(Matrix4d()) );
  85. CALL_SUBTEST_7( inverse(Matrix<double,4,4,DontAlign>()) );
  86. }
  87. EIGEN_UNUSED_VARIABLE(s)
  88. }