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
10 #include "main.h"
11 #include <limits>
12 #include <Eigen/Eigenvalues>
13
generalized_eigensolver_real(const MatrixType & m)14 template<typename MatrixType> void generalized_eigensolver_real(const MatrixType& m)
15 {
16 typedef typename MatrixType::Index Index;
17 /* this test covers the following files:
18 GeneralizedEigenSolver.h
19 */
20 Index rows = m.rows();
21 Index cols = m.cols();
22
23 typedef typename MatrixType::Scalar Scalar;
24 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
25
26 MatrixType a = MatrixType::Random(rows,cols);
27 MatrixType b = MatrixType::Random(rows,cols);
28 MatrixType a1 = MatrixType::Random(rows,cols);
29 MatrixType b1 = MatrixType::Random(rows,cols);
30 MatrixType spdA = a.adjoint() * a + a1.adjoint() * a1;
31 MatrixType spdB = b.adjoint() * b + b1.adjoint() * b1;
32
33 // lets compare to GeneralizedSelfAdjointEigenSolver
34 GeneralizedSelfAdjointEigenSolver<MatrixType> symmEig(spdA, spdB);
35 GeneralizedEigenSolver<MatrixType> eig(spdA, spdB);
36
37 VERIFY_IS_EQUAL(eig.eigenvalues().imag().cwiseAbs().maxCoeff(), 0);
38
39 VectorType realEigenvalues = eig.eigenvalues().real();
40 std::sort(realEigenvalues.data(), realEigenvalues.data()+realEigenvalues.size());
41 VERIFY_IS_APPROX(realEigenvalues, symmEig.eigenvalues());
42 }
43
test_eigensolver_generalized_real()44 void test_eigensolver_generalized_real()
45 {
46 for(int i = 0; i < g_repeat; i++) {
47 int s = 0;
48 CALL_SUBTEST_1( generalized_eigensolver_real(Matrix4f()) );
49 s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
50 CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(s,s)) );
51
52 // some trivial but implementation-wise tricky cases
53 CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(1,1)) );
54 CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(2,2)) );
55 CALL_SUBTEST_3( generalized_eigensolver_real(Matrix<double,1,1>()) );
56 CALL_SUBTEST_4( generalized_eigensolver_real(Matrix2d()) );
57 TEST_SET_BUT_UNUSED_VARIABLE(s)
58 }
59 }
60