[136] | 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@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 | template <typename MatrixType, int IsComplex = NumTraits<typename internal::traits<MatrixType>::Scalar>::IsComplex>
|
---|
| 14 | struct generateTestMatrix;
|
---|
| 15 |
|
---|
| 16 | // for real matrices, make sure none of the eigenvalues are negative
|
---|
| 17 | template <typename MatrixType>
|
---|
| 18 | struct generateTestMatrix<MatrixType,0>
|
---|
| 19 | {
|
---|
| 20 | static void run(MatrixType& result, typename MatrixType::Index size)
|
---|
| 21 | {
|
---|
| 22 | MatrixType mat = MatrixType::Random(size, size);
|
---|
| 23 | EigenSolver<MatrixType> es(mat);
|
---|
| 24 | typename EigenSolver<MatrixType>::EigenvalueType eivals = es.eigenvalues();
|
---|
| 25 | for (typename MatrixType::Index i = 0; i < size; ++i) {
|
---|
| 26 | if (eivals(i).imag() == 0 && eivals(i).real() < 0)
|
---|
| 27 | eivals(i) = -eivals(i);
|
---|
| 28 | }
|
---|
| 29 | result = (es.eigenvectors() * eivals.asDiagonal() * es.eigenvectors().inverse()).real();
|
---|
| 30 | }
|
---|
| 31 | };
|
---|
| 32 |
|
---|
| 33 | // for complex matrices, any matrix is fine
|
---|
| 34 | template <typename MatrixType>
|
---|
| 35 | struct generateTestMatrix<MatrixType,1>
|
---|
| 36 | {
|
---|
| 37 | static void run(MatrixType& result, typename MatrixType::Index size)
|
---|
| 38 | {
|
---|
| 39 | result = MatrixType::Random(size, size);
|
---|
| 40 | }
|
---|
| 41 | };
|
---|
| 42 |
|
---|
| 43 | template <typename Derived, typename OtherDerived>
|
---|
| 44 | double relerr(const MatrixBase<Derived>& A, const MatrixBase<OtherDerived>& B)
|
---|
| 45 | {
|
---|
| 46 | return std::sqrt((A - B).cwiseAbs2().sum() / (std::min)(A.cwiseAbs2().sum(), B.cwiseAbs2().sum()));
|
---|
| 47 | }
|
---|