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.

81 lines
2.4 KiB

  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. #include "main.h"
  10. #include <limits>
  11. #include <Eigen/Eigenvalues>
  12. template<typename MatrixType> void real_qz(const MatrixType& m)
  13. {
  14. /* this test covers the following files:
  15. RealQZ.h
  16. */
  17. using std::abs;
  18. typedef typename MatrixType::Index Index;
  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(A,B);
  38. VERIFY_IS_EQUAL(qz.info(), Success);
  39. // check for zeros
  40. bool all_zeros = true;
  41. for (Index i=0; i<A.cols(); i++)
  42. for (Index j=0; j<i; j++) {
  43. if (abs(qz.matrixT()(i,j))!=Scalar(0.0))
  44. all_zeros = false;
  45. if (j<i-1 && abs(qz.matrixS()(i,j))!=Scalar(0.0))
  46. all_zeros = false;
  47. if (j==i-1 && j>0 && abs(qz.matrixS()(i,j))!=Scalar(0.0) && abs(qz.matrixS()(i-1,j-1))!=Scalar(0.0))
  48. all_zeros = false;
  49. }
  50. VERIFY_IS_EQUAL(all_zeros, true);
  51. VERIFY_IS_APPROX(qz.matrixQ()*qz.matrixS()*qz.matrixZ(), A);
  52. VERIFY_IS_APPROX(qz.matrixQ()*qz.matrixT()*qz.matrixZ(), B);
  53. VERIFY_IS_APPROX(qz.matrixQ()*qz.matrixQ().adjoint(), MatrixType::Identity(dim,dim));
  54. VERIFY_IS_APPROX(qz.matrixZ()*qz.matrixZ().adjoint(), MatrixType::Identity(dim,dim));
  55. }
  56. void test_real_qz()
  57. {
  58. int s = 0;
  59. for(int i = 0; i < g_repeat; i++) {
  60. CALL_SUBTEST_1( real_qz(Matrix4f()) );
  61. s = internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4);
  62. CALL_SUBTEST_2( real_qz(MatrixXd(s,s)) );
  63. // some trivial but implementation-wise tricky cases
  64. CALL_SUBTEST_2( real_qz(MatrixXd(1,1)) );
  65. CALL_SUBTEST_2( real_qz(MatrixXd(2,2)) );
  66. CALL_SUBTEST_3( real_qz(Matrix<double,1,1>()) );
  67. CALL_SUBTEST_4( real_qz(Matrix2d()) );
  68. }
  69. TEST_SET_BUT_UNUSED_VARIABLE(s)
  70. }