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.

59 lines
2.2 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. }
  36. void test_eigensolver_generalized_real()
  37. {
  38. for(int i = 0; i < g_repeat; i++) {
  39. int s = 0;
  40. CALL_SUBTEST_1( generalized_eigensolver_real(Matrix4f()) );
  41. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  42. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(s,s)) );
  43. // some trivial but implementation-wise tricky cases
  44. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(1,1)) );
  45. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(2,2)) );
  46. CALL_SUBTEST_3( generalized_eigensolver_real(Matrix<double,1,1>()) );
  47. CALL_SUBTEST_4( generalized_eigensolver_real(Matrix2d()) );
  48. TEST_SET_BUT_UNUSED_VARIABLE(s)
  49. }
  50. }