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) 2009-2011 Jitse Niesen <jitse (at) maths.leeds.ac.uk>
      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 <unsupported/Eigen/MatrixFunctions>
     12 
     13 // For complex matrices, any matrix is fine.
     14 template<typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
     15 struct processTriangularMatrix
     16 {
     17   static void run(MatrixType&, MatrixType&, const MatrixType&)
     18   { }
     19 };
     20 
     21 // For real matrices, make sure none of the eigenvalues are negative.
     22 template<typename MatrixType>
     23 struct processTriangularMatrix<MatrixType,0>
     24 {
     25   static void run(MatrixType& m, MatrixType& T, const MatrixType& U)
     26   {
     27     const Index size = m.cols();
     28 
     29     for (Index i=0; i < size; ++i) {
     30       if (i == size - 1 || T.coeff(i+1,i) == 0)
     31         T.coeffRef(i,i) = std::abs(T.coeff(i,i));
     32       else
     33         ++i;
     34     }
     35     m = U * T * U.transpose();
     36   }
     37 };
     38 
     39 template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
     40 struct generateTestMatrix;
     41 
     42 template <typename MatrixType>
     43 struct generateTestMatrix<MatrixType,0>
     44 {
     45   static void run(MatrixType& result, typename MatrixType::Index size)
     46   {
     47     result = MatrixType::Random(size, size);
     48     RealSchur<MatrixType> schur(result);
     49     MatrixType T = schur.matrixT();
     50     processTriangularMatrix<MatrixType>::run(result, T, schur.matrixU());
     51   }
     52 };
     53 
     54 template <typename MatrixType>
     55 struct generateTestMatrix<MatrixType,1>
     56 {
     57   static void run(MatrixType& result, typename MatrixType::Index size)
     58   {
     59     result = MatrixType::Random(size, size);
     60   }
     61 };
     62 
     63 template <typename Derived, typename OtherDerived>
     64 typename Derived::RealScalar relerr(const MatrixBase<Derived>& A, const MatrixBase<OtherDerived>& B)
     65 {
     66   return std::sqrt((A - B).cwiseAbs2().sum() / (std::min)(A.cwiseAbs2().sum(), B.cwiseAbs2().sum()));
     67 }
     68