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.

67 lines
2.2 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  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 determinant(const MatrixType& m)
  13. {
  14. /* this test covers the following files:
  15. Determinant.h
  16. */
  17. typedef typename MatrixType::Index Index;
  18. Index size = m.rows();
  19. MatrixType m1(size, size), m2(size, size);
  20. m1.setRandom();
  21. m2.setRandom();
  22. typedef typename MatrixType::Scalar Scalar;
  23. Scalar x = internal::random<Scalar>();
  24. VERIFY_IS_APPROX(MatrixType::Identity(size, size).determinant(), Scalar(1));
  25. VERIFY_IS_APPROX((m1*m2).eval().determinant(), m1.determinant() * m2.determinant());
  26. if(size==1) return;
  27. Index i = internal::random<Index>(0, size-1);
  28. Index j;
  29. do {
  30. j = internal::random<Index>(0, size-1);
  31. } while(j==i);
  32. m2 = m1;
  33. m2.row(i).swap(m2.row(j));
  34. VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
  35. m2 = m1;
  36. m2.col(i).swap(m2.col(j));
  37. VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
  38. VERIFY_IS_APPROX(m2.determinant(), m2.transpose().determinant());
  39. VERIFY_IS_APPROX(internal::conj(m2.determinant()), m2.adjoint().determinant());
  40. m2 = m1;
  41. m2.row(i) += x*m2.row(j);
  42. VERIFY_IS_APPROX(m2.determinant(), m1.determinant());
  43. m2 = m1;
  44. m2.row(i) *= x;
  45. VERIFY_IS_APPROX(m2.determinant(), m1.determinant() * x);
  46. // check empty matrix
  47. VERIFY_IS_APPROX(m2.block(0,0,0,0).determinant(), Scalar(1));
  48. }
  49. void test_determinant()
  50. {
  51. int s;
  52. for(int i = 0; i < g_repeat; i++) {
  53. CALL_SUBTEST_1( determinant(Matrix<float, 1, 1>()) );
  54. CALL_SUBTEST_2( determinant(Matrix<double, 2, 2>()) );
  55. CALL_SUBTEST_3( determinant(Matrix<double, 3, 3>()) );
  56. CALL_SUBTEST_4( determinant(Matrix<double, 4, 4>()) );
  57. CALL_SUBTEST_5( determinant(Matrix<std::complex<double>, 10, 10>()) );
  58. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  59. CALL_SUBTEST_6( determinant(MatrixXd(s, s)) );
  60. }
  61. EIGEN_UNUSED_VARIABLE(s)
  62. }