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.5 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2012 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #include "main.h"
  10. #include <limits>
  11. #include <Eigen/Eigenvalues>
  12. template<typename MatrixType> void generalized_eigensolver_real(const MatrixType& m)
  13. {
  14. typedef typename MatrixType::Index Index;
  15. /* this test covers the following files:
  16. GeneralizedEigenSolver.h
  17. */
  18. Index rows = m.rows();
  19. Index cols = m.cols();
  20. typedef typename MatrixType::Scalar Scalar;
  21. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  22. MatrixType a = MatrixType::Random(rows,cols);
  23. MatrixType b = MatrixType::Random(rows,cols);
  24. MatrixType a1 = MatrixType::Random(rows,cols);
  25. MatrixType b1 = MatrixType::Random(rows,cols);
  26. MatrixType spdA = a.adjoint() * a + a1.adjoint() * a1;
  27. MatrixType spdB = b.adjoint() * b + b1.adjoint() * b1;
  28. // lets compare to GeneralizedSelfAdjointEigenSolver
  29. GeneralizedSelfAdjointEigenSolver<MatrixType> symmEig(spdA, spdB);
  30. GeneralizedEigenSolver<MatrixType> eig(spdA, spdB);
  31. VERIFY_IS_EQUAL(eig.eigenvalues().imag().cwiseAbs().maxCoeff(), 0);
  32. VectorType realEigenvalues = eig.eigenvalues().real();
  33. std::sort(realEigenvalues.data(), realEigenvalues.data()+realEigenvalues.size());
  34. VERIFY_IS_APPROX(realEigenvalues, symmEig.eigenvalues());
  35. // regression test for bug 1098
  36. {
  37. GeneralizedSelfAdjointEigenSolver<MatrixType> eig1(a.adjoint() * a,b.adjoint() * b);
  38. eig1.compute(a.adjoint() * a,b.adjoint() * b);
  39. GeneralizedEigenSolver<MatrixType> eig2(a.adjoint() * a,b.adjoint() * b);
  40. eig2.compute(a.adjoint() * a,b.adjoint() * b);
  41. }
  42. }
  43. void test_eigensolver_generalized_real()
  44. {
  45. for(int i = 0; i < g_repeat; i++) {
  46. int s = 0;
  47. CALL_SUBTEST_1( generalized_eigensolver_real(Matrix4f()) );
  48. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  49. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(s,s)) );
  50. // some trivial but implementation-wise tricky cases
  51. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(1,1)) );
  52. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(2,2)) );
  53. CALL_SUBTEST_3( generalized_eigensolver_real(Matrix<double,1,1>()) );
  54. CALL_SUBTEST_4( generalized_eigensolver_real(Matrix2d()) );
  55. TEST_SET_BUT_UNUSED_VARIABLE(s)
  56. }
  57. }