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.

117 lines
3.8 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. using std::abs;
  15. typedef typename MatrixType::Index Index;
  16. /* this test covers the following files:
  17. Inverse.h
  18. */
  19. Index rows = m.rows();
  20. Index cols = m.cols();
  21. typedef typename MatrixType::Scalar Scalar;
  22. MatrixType m1(rows, cols),
  23. m2(rows, cols),
  24. identity = MatrixType::Identity(rows, rows);
  25. createRandomPIMatrixOfRank(rows,rows,rows,m1);
  26. m2 = m1.inverse();
  27. VERIFY_IS_APPROX(m1, m2.inverse() );
  28. VERIFY_IS_APPROX((Scalar(2)*m2).inverse(), m2.inverse()*Scalar(0.5));
  29. VERIFY_IS_APPROX(identity, m1.inverse() * m1 );
  30. VERIFY_IS_APPROX(identity, m1 * m1.inverse() );
  31. VERIFY_IS_APPROX(m1, m1.inverse().inverse() );
  32. // since for the general case we implement separately row-major and col-major, test that
  33. VERIFY_IS_APPROX(MatrixType(m1.transpose().inverse()), MatrixType(m1.inverse().transpose()));
  34. #if !defined(EIGEN_TEST_PART_5) && !defined(EIGEN_TEST_PART_6)
  35. typedef typename NumTraits<Scalar>::Real RealScalar;
  36. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
  37. //computeInverseAndDetWithCheck tests
  38. //First: an invertible matrix
  39. bool invertible;
  40. RealScalar det;
  41. m2.setZero();
  42. m1.computeInverseAndDetWithCheck(m2, det, invertible);
  43. VERIFY(invertible);
  44. VERIFY_IS_APPROX(identity, m1*m2);
  45. VERIFY_IS_APPROX(det, m1.determinant());
  46. m2.setZero();
  47. m1.computeInverseWithCheck(m2, invertible);
  48. VERIFY(invertible);
  49. VERIFY_IS_APPROX(identity, m1*m2);
  50. //Second: a rank one matrix (not invertible, except for 1x1 matrices)
  51. VectorType v3 = VectorType::Random(rows);
  52. MatrixType m3 = v3*v3.transpose(), m4(rows,cols);
  53. m3.computeInverseAndDetWithCheck(m4, det, invertible);
  54. VERIFY( rows==1 ? invertible : !invertible );
  55. VERIFY_IS_MUCH_SMALLER_THAN(abs(det-m3.determinant()), RealScalar(1));
  56. m3.computeInverseWithCheck(m4, invertible);
  57. VERIFY( rows==1 ? invertible : !invertible );
  58. // check with submatrices
  59. {
  60. Matrix<Scalar, MatrixType::RowsAtCompileTime+1, MatrixType::RowsAtCompileTime+1, MatrixType::Options> m5;
  61. m5.setRandom();
  62. m5.topLeftCorner(rows,rows) = m1;
  63. m2 = m5.template topLeftCorner<MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime>().inverse();
  64. VERIFY_IS_APPROX( (m5.template topLeftCorner<MatrixType::RowsAtCompileTime,MatrixType::ColsAtCompileTime>()), m2.inverse() );
  65. }
  66. #endif
  67. // check in-place inversion
  68. if(MatrixType::RowsAtCompileTime>=2 && MatrixType::RowsAtCompileTime<=4)
  69. {
  70. // in-place is forbidden
  71. VERIFY_RAISES_ASSERT(m1 = m1.inverse());
  72. }
  73. else
  74. {
  75. m2 = m1.inverse();
  76. m1 = m1.inverse();
  77. VERIFY_IS_APPROX(m1,m2);
  78. }
  79. }
  80. void test_inverse()
  81. {
  82. int s = 0;
  83. for(int i = 0; i < g_repeat; i++) {
  84. CALL_SUBTEST_1( inverse(Matrix<double,1,1>()) );
  85. CALL_SUBTEST_2( inverse(Matrix2d()) );
  86. CALL_SUBTEST_3( inverse(Matrix3f()) );
  87. CALL_SUBTEST_4( inverse(Matrix4f()) );
  88. CALL_SUBTEST_4( inverse(Matrix<float,4,4,DontAlign>()) );
  89. s = internal::random<int>(50,320);
  90. CALL_SUBTEST_5( inverse(MatrixXf(s,s)) );
  91. TEST_SET_BUT_UNUSED_VARIABLE(s)
  92. s = internal::random<int>(25,100);
  93. CALL_SUBTEST_6( inverse(MatrixXcd(s,s)) );
  94. TEST_SET_BUT_UNUSED_VARIABLE(s)
  95. CALL_SUBTEST_7( inverse(Matrix4d()) );
  96. CALL_SUBTEST_7( inverse(Matrix<double,4,4,DontAlign>()) );
  97. }
  98. }