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.

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