You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.6 KiB

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