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.

63 lines
2.0 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <g.gael@free.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. /* this test covers the following files:
  15. Inverse.h
  16. */
  17. int rows = m.rows();
  18. int cols = m.cols();
  19. typedef typename MatrixType::Scalar Scalar;
  20. typedef typename NumTraits<Scalar>::Real RealScalar;
  21. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
  22. MatrixType m1 = MatrixType::Random(rows, cols),
  23. m2(rows, cols),
  24. mzero = MatrixType::Zero(rows, cols),
  25. identity = MatrixType::Identity(rows, rows);
  26. while(ei_abs(m1.determinant()) < RealScalar(0.1) && rows <= 8)
  27. {
  28. m1 = MatrixType::Random(rows, cols);
  29. }
  30. m2 = m1.inverse();
  31. VERIFY_IS_APPROX(m1, m2.inverse() );
  32. m1.computeInverse(&m2);
  33. VERIFY_IS_APPROX(m1, m2.inverse() );
  34. VERIFY_IS_APPROX((Scalar(2)*m2).inverse(), m2.inverse()*Scalar(0.5));
  35. VERIFY_IS_APPROX(identity, m1.inverse() * m1 );
  36. VERIFY_IS_APPROX(identity, m1 * m1.inverse() );
  37. VERIFY_IS_APPROX(m1, m1.inverse().inverse() );
  38. // since for the general case we implement separately row-major and col-major, test that
  39. VERIFY_IS_APPROX(m1.transpose().inverse(), m1.inverse().transpose());
  40. }
  41. void test_eigen2_inverse()
  42. {
  43. for(int i = 0; i < g_repeat; i++) {
  44. CALL_SUBTEST_1( inverse(Matrix<double,1,1>()) );
  45. CALL_SUBTEST_2( inverse(Matrix2d()) );
  46. CALL_SUBTEST_3( inverse(Matrix3f()) );
  47. CALL_SUBTEST_4( inverse(Matrix4f()) );
  48. CALL_SUBTEST_5( inverse(MatrixXf(8,8)) );
  49. CALL_SUBTEST_6( inverse(MatrixXcd(7,7)) );
  50. }
  51. }