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.4 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 typename NumTraits<Scalar>::Real RealScalar;
  22. typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
  23. typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
  24. typedef typename std::complex<typename NumTraits<typename MatrixType::Scalar>::Real> Complex;
  25. MatrixType a = MatrixType::Random(rows,cols);
  26. MatrixType b = MatrixType::Random(rows,cols);
  27. MatrixType a1 = MatrixType::Random(rows,cols);
  28. MatrixType b1 = MatrixType::Random(rows,cols);
  29. MatrixType spdA = a.adjoint() * a + a1.adjoint() * a1;
  30. MatrixType spdB = b.adjoint() * b + b1.adjoint() * b1;
  31. // lets compare to GeneralizedSelfAdjointEigenSolver
  32. GeneralizedSelfAdjointEigenSolver<MatrixType> symmEig(spdA, spdB);
  33. GeneralizedEigenSolver<MatrixType> eig(spdA, spdB);
  34. VERIFY_IS_EQUAL(eig.eigenvalues().imag().cwiseAbs().maxCoeff(), 0);
  35. VectorType realEigenvalues = eig.eigenvalues().real();
  36. std::sort(realEigenvalues.data(), realEigenvalues.data()+realEigenvalues.size());
  37. VERIFY_IS_APPROX(realEigenvalues, symmEig.eigenvalues());
  38. }
  39. void test_eigensolver_generalized_real()
  40. {
  41. int s;
  42. for(int i = 0; i < g_repeat; i++) {
  43. CALL_SUBTEST_1( generalized_eigensolver_real(Matrix4f()) );
  44. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  45. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(s,s)) );
  46. // some trivial but implementation-wise tricky cases
  47. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(1,1)) );
  48. CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(2,2)) );
  49. CALL_SUBTEST_3( generalized_eigensolver_real(Matrix<double,1,1>()) );
  50. CALL_SUBTEST_4( generalized_eigensolver_real(Matrix2d()) );
  51. }
  52. EIGEN_UNUSED_VARIABLE(s)
  53. }