real_qz.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2012 Alexey Korepanov <kaikaikai@yandex.ru>
  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. #define EIGEN_RUNTIME_NO_MALLOC
  10. #include "main.h"
  11. #include <limits>
  12. #include <Eigen/Eigenvalues>
  13. template<typename MatrixType> void real_qz(const MatrixType& m)
  14. {
  15. /* this test covers the following files:
  16. RealQZ.h
  17. */
  18. using std::abs;
  19. typedef typename MatrixType::Scalar Scalar;
  20. Index dim = m.cols();
  21. MatrixType A = MatrixType::Random(dim,dim),
  22. B = MatrixType::Random(dim,dim);
  23. // Regression test for bug 985: Randomly set rows or columns to zero
  24. Index k=internal::random<Index>(0, dim-1);
  25. switch(internal::random<int>(0,10)) {
  26. case 0:
  27. A.row(k).setZero(); break;
  28. case 1:
  29. A.col(k).setZero(); break;
  30. case 2:
  31. B.row(k).setZero(); break;
  32. case 3:
  33. B.col(k).setZero(); break;
  34. default:
  35. break;
  36. }
  37. RealQZ<MatrixType> qz(dim);
  38. // TODO enable full-prealocation of required memory, this probably requires an in-place mode for HessenbergDecomposition
  39. //Eigen::internal::set_is_malloc_allowed(false);
  40. qz.compute(A,B);
  41. //Eigen::internal::set_is_malloc_allowed(true);
  42. VERIFY_IS_EQUAL(qz.info(), Success);
  43. // check for zeros
  44. bool all_zeros = true;
  45. for (Index i=0; i<A.cols(); i++)
  46. for (Index j=0; j<i; j++) {
  47. if (abs(qz.matrixT()(i,j))!=Scalar(0.0))
  48. {
  49. std::cerr << "Error: T(" << i << "," << j << ") = " << qz.matrixT()(i,j) << std::endl;
  50. all_zeros = false;
  51. }
  52. if (j<i-1 && abs(qz.matrixS()(i,j))!=Scalar(0.0))
  53. {
  54. std::cerr << "Error: S(" << i << "," << j << ") = " << qz.matrixS()(i,j) << std::endl;
  55. all_zeros = false;
  56. }
  57. if (j==i-1 && j>0 && abs(qz.matrixS()(i,j))!=Scalar(0.0) && abs(qz.matrixS()(i-1,j-1))!=Scalar(0.0))
  58. {
  59. std::cerr << "Error: S(" << i << "," << j << ") = " << qz.matrixS()(i,j) << " && S(" << i-1 << "," << j-1 << ") = " << qz.matrixS()(i-1,j-1) << std::endl;
  60. all_zeros = false;
  61. }
  62. }
  63. VERIFY_IS_EQUAL(all_zeros, true);
  64. VERIFY_IS_APPROX(qz.matrixQ()*qz.matrixS()*qz.matrixZ(), A);
  65. VERIFY_IS_APPROX(qz.matrixQ()*qz.matrixT()*qz.matrixZ(), B);
  66. VERIFY_IS_APPROX(qz.matrixQ()*qz.matrixQ().adjoint(), MatrixType::Identity(dim,dim));
  67. VERIFY_IS_APPROX(qz.matrixZ()*qz.matrixZ().adjoint(), MatrixType::Identity(dim,dim));
  68. }
  69. void test_real_qz()
  70. {
  71. int s = 0;
  72. for(int i = 0; i < g_repeat; i++) {
  73. CALL_SUBTEST_1( real_qz(Matrix4f()) );
  74. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  75. CALL_SUBTEST_2( real_qz(MatrixXd(s,s)) );
  76. // some trivial but implementation-wise tricky cases
  77. CALL_SUBTEST_2( real_qz(MatrixXd(1,1)) );
  78. CALL_SUBTEST_2( real_qz(MatrixXd(2,2)) );
  79. CALL_SUBTEST_3( real_qz(Matrix<double,1,1>()) );
  80. CALL_SUBTEST_4( real_qz(Matrix2d()) );
  81. }
  82. TEST_SET_BUT_UNUSED_VARIABLE(s)
  83. }