1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2012-2016 Gael Guennebaud <gael.guennebaud (at) 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 #define EIGEN_RUNTIME_NO_MALLOC 11 #include "main.h" 12 #include <limits> 13 #include <Eigen/Eigenvalues> 14 #include <Eigen/LU> 15 16 template<typename MatrixType> void generalized_eigensolver_real(const MatrixType& m) 17 { 18 typedef typename MatrixType::Index Index; 19 /* this test covers the following files: 20 GeneralizedEigenSolver.h 21 */ 22 Index rows = m.rows(); 23 Index cols = m.cols(); 24 25 typedef typename MatrixType::Scalar Scalar; 26 typedef std::complex<Scalar> ComplexScalar; 27 typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType; 28 29 MatrixType a = MatrixType::Random(rows,cols); 30 MatrixType b = MatrixType::Random(rows,cols); 31 MatrixType a1 = MatrixType::Random(rows,cols); 32 MatrixType b1 = MatrixType::Random(rows,cols); 33 MatrixType spdA = a.adjoint() * a + a1.adjoint() * a1; 34 MatrixType spdB = b.adjoint() * b + b1.adjoint() * b1; 35 36 // lets compare to GeneralizedSelfAdjointEigenSolver 37 { 38 GeneralizedSelfAdjointEigenSolver<MatrixType> symmEig(spdA, spdB); 39 GeneralizedEigenSolver<MatrixType> eig(spdA, spdB); 40 41 VERIFY_IS_EQUAL(eig.eigenvalues().imag().cwiseAbs().maxCoeff(), 0); 42 43 VectorType realEigenvalues = eig.eigenvalues().real(); 44 std::sort(realEigenvalues.data(), realEigenvalues.data()+realEigenvalues.size()); 45 VERIFY_IS_APPROX(realEigenvalues, symmEig.eigenvalues()); 46 47 // check eigenvectors 48 typename GeneralizedEigenSolver<MatrixType>::EigenvectorsType D = eig.eigenvalues().asDiagonal(); 49 typename GeneralizedEigenSolver<MatrixType>::EigenvectorsType V = eig.eigenvectors(); 50 VERIFY_IS_APPROX(spdA*V, spdB*V*D); 51 } 52 53 // non symmetric case: 54 { 55 GeneralizedEigenSolver<MatrixType> eig(rows); 56 // TODO enable full-prealocation of required memory, this probably requires an in-place mode for HessenbergDecomposition 57 //Eigen::internal::set_is_malloc_allowed(false); 58 eig.compute(a,b); 59 //Eigen::internal::set_is_malloc_allowed(true); 60 for(Index k=0; k<cols; ++k) 61 { 62 Matrix<ComplexScalar,Dynamic,Dynamic> tmp = (eig.betas()(k)*a).template cast<ComplexScalar>() - eig.alphas()(k)*b; 63 if(tmp.size()>1 && tmp.norm()>(std::numeric_limits<Scalar>::min)()) 64 tmp /= tmp.norm(); 65 VERIFY_IS_MUCH_SMALLER_THAN( std::abs(tmp.determinant()), Scalar(1) ); 66 } 67 // check eigenvectors 68 typename GeneralizedEigenSolver<MatrixType>::EigenvectorsType D = eig.eigenvalues().asDiagonal(); 69 typename GeneralizedEigenSolver<MatrixType>::EigenvectorsType V = eig.eigenvectors(); 70 VERIFY_IS_APPROX(a*V, b*V*D); 71 } 72 73 // regression test for bug 1098 74 { 75 GeneralizedSelfAdjointEigenSolver<MatrixType> eig1(a.adjoint() * a,b.adjoint() * b); 76 eig1.compute(a.adjoint() * a,b.adjoint() * b); 77 GeneralizedEigenSolver<MatrixType> eig2(a.adjoint() * a,b.adjoint() * b); 78 eig2.compute(a.adjoint() * a,b.adjoint() * b); 79 } 80 } 81 82 void test_eigensolver_generalized_real() 83 { 84 for(int i = 0; i < g_repeat; i++) { 85 int s = 0; 86 CALL_SUBTEST_1( generalized_eigensolver_real(Matrix4f()) ); 87 s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4); 88 CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(s,s)) ); 89 90 // some trivial but implementation-wise special cases 91 CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(1,1)) ); 92 CALL_SUBTEST_2( generalized_eigensolver_real(MatrixXd(2,2)) ); 93 CALL_SUBTEST_3( generalized_eigensolver_real(Matrix<double,1,1>()) ); 94 CALL_SUBTEST_4( generalized_eigensolver_real(Matrix2d()) ); 95 TEST_SET_BUT_UNUSED_VARIABLE(s) 96 } 97 } 98