Home | History | Annotate | Download | only in test
      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud (at) inria.fr>
      5 // Copyright (C) 2010 Jitse Niesen <jitse (at) maths.leeds.ac.uk>
      6 //
      7 // This Source Code Form is subject to the terms of the Mozilla
      8 // Public License v. 2.0. If a copy of the MPL was not distributed
      9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
     10 
     11 #include "main.h"
     12 #include <limits>
     13 #include <Eigen/Eigenvalues>
     14 
     15 template<typename MatrixType> void eigensolver(const MatrixType& m)
     16 {
     17   typedef typename MatrixType::Index Index;
     18   /* this test covers the following files:
     19      EigenSolver.h
     20   */
     21   Index rows = m.rows();
     22   Index cols = m.cols();
     23 
     24   typedef typename MatrixType::Scalar Scalar;
     25   typedef typename NumTraits<Scalar>::Real RealScalar;
     26   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
     27   typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
     28   typedef typename std::complex<typename NumTraits<typename MatrixType::Scalar>::Real> Complex;
     29 
     30   MatrixType a = MatrixType::Random(rows,cols);
     31   MatrixType a1 = MatrixType::Random(rows,cols);
     32   MatrixType symmA =  a.adjoint() * a + a1.adjoint() * a1;
     33 
     34   EigenSolver<MatrixType> ei0(symmA);
     35   VERIFY_IS_EQUAL(ei0.info(), Success);
     36   VERIFY_IS_APPROX(symmA * ei0.pseudoEigenvectors(), ei0.pseudoEigenvectors() * ei0.pseudoEigenvalueMatrix());
     37   VERIFY_IS_APPROX((symmA.template cast<Complex>()) * (ei0.pseudoEigenvectors().template cast<Complex>()),
     38     (ei0.pseudoEigenvectors().template cast<Complex>()) * (ei0.eigenvalues().asDiagonal()));
     39 
     40   EigenSolver<MatrixType> ei1(a);
     41   VERIFY_IS_EQUAL(ei1.info(), Success);
     42   VERIFY_IS_APPROX(a * ei1.pseudoEigenvectors(), ei1.pseudoEigenvectors() * ei1.pseudoEigenvalueMatrix());
     43   VERIFY_IS_APPROX(a.template cast<Complex>() * ei1.eigenvectors(),
     44                    ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
     45   VERIFY_IS_APPROX(ei1.eigenvectors().colwise().norm(), RealVectorType::Ones(rows).transpose());
     46   VERIFY_IS_APPROX(a.eigenvalues(), ei1.eigenvalues());
     47 
     48   EigenSolver<MatrixType> eiNoEivecs(a, false);
     49   VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
     50   VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
     51   VERIFY_IS_APPROX(ei1.pseudoEigenvalueMatrix(), eiNoEivecs.pseudoEigenvalueMatrix());
     52 
     53   MatrixType id = MatrixType::Identity(rows, cols);
     54   VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
     55 
     56   if (rows > 2)
     57   {
     58     // Test matrix with NaN
     59     a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
     60     EigenSolver<MatrixType> eiNaN(a);
     61     VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
     62   }
     63 }
     64 
     65 template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
     66 {
     67   EigenSolver<MatrixType> eig;
     68   VERIFY_RAISES_ASSERT(eig.eigenvectors());
     69   VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
     70   VERIFY_RAISES_ASSERT(eig.pseudoEigenvalueMatrix());
     71   VERIFY_RAISES_ASSERT(eig.eigenvalues());
     72 
     73   MatrixType a = MatrixType::Random(m.rows(),m.cols());
     74   eig.compute(a, false);
     75   VERIFY_RAISES_ASSERT(eig.eigenvectors());
     76   VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
     77 }
     78 
     79 void test_eigensolver_generic()
     80 {
     81   int s;
     82   for(int i = 0; i < g_repeat; i++) {
     83     CALL_SUBTEST_1( eigensolver(Matrix4f()) );
     84     s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
     85     CALL_SUBTEST_2( eigensolver(MatrixXd(s,s)) );
     86 
     87     // some trivial but implementation-wise tricky cases
     88     CALL_SUBTEST_2( eigensolver(MatrixXd(1,1)) );
     89     CALL_SUBTEST_2( eigensolver(MatrixXd(2,2)) );
     90     CALL_SUBTEST_3( eigensolver(Matrix<double,1,1>()) );
     91     CALL_SUBTEST_4( eigensolver(Matrix2d()) );
     92   }
     93 
     94   CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4f()) );
     95   s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
     96   CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXd(s,s)) );
     97   CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<double,1,1>()) );
     98   CALL_SUBTEST_4( eigensolver_verify_assert(Matrix2d()) );
     99 
    100   // Test problem size constructors
    101   CALL_SUBTEST_5(EigenSolver<MatrixXf>(s));
    102 
    103   // regression test for bug 410
    104   CALL_SUBTEST_2(
    105   {
    106      MatrixXd A(1,1);
    107      A(0,0) = std::sqrt(-1.);
    108      Eigen::EigenSolver<MatrixXd> solver(A);
    109      MatrixXd V(1, 1);
    110      V(0,0) = solver.eigenvectors()(0,0).real();
    111   }
    112   );
    113 
    114   EIGEN_UNUSED_VARIABLE(s)
    115 }
    116