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.

61 lines
2.1 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 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. // Copyright (C) 2008 Gael Guennebaud <g.gael@free.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. int size = m.rows();
  18. MatrixType m1(size, size), m2(size, size);
  19. m1.setRandom();
  20. m2.setRandom();
  21. typedef typename MatrixType::Scalar Scalar;
  22. Scalar x = ei_random<Scalar>();
  23. VERIFY_IS_APPROX(MatrixType::Identity(size, size).determinant(), Scalar(1));
  24. VERIFY_IS_APPROX((m1*m2).determinant(), m1.determinant() * m2.determinant());
  25. if(size==1) return;
  26. int i = ei_random<int>(0, size-1);
  27. int j;
  28. do {
  29. j = ei_random<int>(0, size-1);
  30. } while(j==i);
  31. m2 = m1;
  32. m2.row(i).swap(m2.row(j));
  33. VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
  34. m2 = m1;
  35. m2.col(i).swap(m2.col(j));
  36. VERIFY_IS_APPROX(m2.determinant(), -m1.determinant());
  37. VERIFY_IS_APPROX(m2.determinant(), m2.transpose().determinant());
  38. VERIFY_IS_APPROX(ei_conj(m2.determinant()), m2.adjoint().determinant());
  39. m2 = m1;
  40. m2.row(i) += x*m2.row(j);
  41. VERIFY_IS_APPROX(m2.determinant(), m1.determinant());
  42. m2 = m1;
  43. m2.row(i) *= x;
  44. VERIFY_IS_APPROX(m2.determinant(), m1.determinant() * x);
  45. }
  46. void test_eigen2_determinant()
  47. {
  48. for(int i = 0; i < g_repeat; i++) {
  49. CALL_SUBTEST_1( determinant(Matrix<float, 1, 1>()) );
  50. CALL_SUBTEST_2( determinant(Matrix<double, 2, 2>()) );
  51. CALL_SUBTEST_3( determinant(Matrix<double, 3, 3>()) );
  52. CALL_SUBTEST_4( determinant(Matrix<double, 4, 4>()) );
  53. CALL_SUBTEST_5( determinant(Matrix<std::complex<double>, 10, 10>()) );
  54. CALL_SUBTEST_6( determinant(MatrixXd(20, 20)) );
  55. }
  56. CALL_SUBTEST_6( determinant(MatrixXd(200, 200)) );
  57. }