eigensolver_generic.cpp
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 // Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #include "main.h"
00027 #include <limits>
00028 #include <Eigen/Eigenvalues>
00029 
00030 #ifdef HAS_GSL
00031 #include "gsl_helper.h"
00032 #endif
00033 
00034 template<typename MatrixType> void eigensolver(const MatrixType& m)
00035 {
00036   typedef typename MatrixType::Index Index;
00037   /* this test covers the following files:
00038      EigenSolver.h
00039   */
00040   Index rows = m.rows();
00041   Index cols = m.cols();
00042 
00043   typedef typename MatrixType::Scalar Scalar;
00044   typedef typename NumTraits<Scalar>::Real RealScalar;
00045   typedef Matrix<Scalar, MatrixType::RowsAtCompileTime, 1> VectorType;
00046   typedef Matrix<RealScalar, MatrixType::RowsAtCompileTime, 1> RealVectorType;
00047   typedef typename std::complex<typename NumTraits<typename MatrixType::Scalar>::Real> Complex;
00048 
00049   MatrixType a = MatrixType::Random(rows,cols);
00050   MatrixType a1 = MatrixType::Random(rows,cols);
00051   MatrixType symmA =  a.adjoint() * a + a1.adjoint() * a1;
00052 
00053   EigenSolver<MatrixType> ei0(symmA);
00054   VERIFY_IS_EQUAL(ei0.info(), Success);
00055   VERIFY_IS_APPROX(symmA * ei0.pseudoEigenvectors(), ei0.pseudoEigenvectors() * ei0.pseudoEigenvalueMatrix());
00056   VERIFY_IS_APPROX((symmA.template cast<Complex>()) * (ei0.pseudoEigenvectors().template cast<Complex>()),
00057     (ei0.pseudoEigenvectors().template cast<Complex>()) * (ei0.eigenvalues().asDiagonal()));
00058 
00059   EigenSolver<MatrixType> ei1(a);
00060   VERIFY_IS_EQUAL(ei1.info(), Success);
00061   VERIFY_IS_APPROX(a * ei1.pseudoEigenvectors(), ei1.pseudoEigenvectors() * ei1.pseudoEigenvalueMatrix());
00062   VERIFY_IS_APPROX(a.template cast<Complex>() * ei1.eigenvectors(),
00063                    ei1.eigenvectors() * ei1.eigenvalues().asDiagonal());
00064   VERIFY_IS_APPROX(ei1.eigenvectors().colwise().norm(), RealVectorType::Ones(rows).transpose());
00065   VERIFY_IS_APPROX(a.eigenvalues(), ei1.eigenvalues());
00066 
00067   EigenSolver<MatrixType> eiNoEivecs(a, false);
00068   VERIFY_IS_EQUAL(eiNoEivecs.info(), Success);
00069   VERIFY_IS_APPROX(ei1.eigenvalues(), eiNoEivecs.eigenvalues());
00070   VERIFY_IS_APPROX(ei1.pseudoEigenvalueMatrix(), eiNoEivecs.pseudoEigenvalueMatrix());
00071 
00072   MatrixType id = MatrixType::Identity(rows, cols);
00073   VERIFY_IS_APPROX(id.operatorNorm(), RealScalar(1));
00074 
00075   if (rows > 2)
00076   {
00077     // Test matrix with NaN
00078     a(0,0) = std::numeric_limits<typename MatrixType::RealScalar>::quiet_NaN();
00079     EigenSolver<MatrixType> eiNaN(a);
00080     VERIFY_IS_EQUAL(eiNaN.info(), NoConvergence);
00081   }
00082 }
00083 
00084 template<typename MatrixType> void eigensolver_verify_assert(const MatrixType& m)
00085 {
00086   EigenSolver<MatrixType> eig;
00087   VERIFY_RAISES_ASSERT(eig.eigenvectors());
00088   VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
00089   VERIFY_RAISES_ASSERT(eig.pseudoEigenvalueMatrix());
00090   VERIFY_RAISES_ASSERT(eig.eigenvalues());
00091 
00092   MatrixType a = MatrixType::Random(m.rows(),m.cols());
00093   eig.compute(a, false);
00094   VERIFY_RAISES_ASSERT(eig.eigenvectors());
00095   VERIFY_RAISES_ASSERT(eig.pseudoEigenvectors());
00096 }
00097 
00098 void test_eigensolver_generic()
00099 {
00100   for(int i = 0; i < g_repeat; i++) {
00101     CALL_SUBTEST_1( eigensolver(Matrix4f()) );
00102     CALL_SUBTEST_2( eigensolver(MatrixXd(17,17)) );
00103 
00104     // some trivial but implementation-wise tricky cases
00105     CALL_SUBTEST_2( eigensolver(MatrixXd(1,1)) );
00106     CALL_SUBTEST_2( eigensolver(MatrixXd(2,2)) );
00107     CALL_SUBTEST_3( eigensolver(Matrix<double,1,1>()) );
00108     CALL_SUBTEST_4( eigensolver(Matrix2d()) );
00109   }
00110 
00111   CALL_SUBTEST_1( eigensolver_verify_assert(Matrix4f()) );
00112   CALL_SUBTEST_2( eigensolver_verify_assert(MatrixXd(17,17)) );
00113   CALL_SUBTEST_3( eigensolver_verify_assert(Matrix<double,1,1>()) );
00114   CALL_SUBTEST_4( eigensolver_verify_assert(Matrix2d()) );
00115 
00116   // Test problem size constructors
00117   CALL_SUBTEST_5(EigenSolver<MatrixXf>(10));
00118 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:07