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.

69 lines
2.2 KiB

  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra. Eigen itself is part of the KDE project.
  3. //
  4. // Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
  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 <Eigen/QR>
  11. template<typename MatrixType> void qr(const MatrixType& m)
  12. {
  13. /* this test covers the following files:
  14. QR.h
  15. */
  16. int rows = m.rows();
  17. int cols = m.cols();
  18. typedef typename MatrixType::Scalar Scalar;
  19. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> SquareMatrixType;
  20. typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
  21. MatrixType a = MatrixType::Random(rows,cols);
  22. QR<MatrixType> qrOfA(a);
  23. VERIFY_IS_APPROX(a, qrOfA.matrixQ() * qrOfA.matrixR());
  24. VERIFY_IS_NOT_APPROX(a+MatrixType::Identity(rows, cols), qrOfA.matrixQ() * qrOfA.matrixR());
  25. #if 0 // eigenvalues module not yet ready
  26. SquareMatrixType b = a.adjoint() * a;
  27. // check tridiagonalization
  28. Tridiagonalization<SquareMatrixType> tridiag(b);
  29. VERIFY_IS_APPROX(b, tridiag.matrixQ() * tridiag.matrixT() * tridiag.matrixQ().adjoint());
  30. // check hessenberg decomposition
  31. HessenbergDecomposition<SquareMatrixType> hess(b);
  32. VERIFY_IS_APPROX(b, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint());
  33. VERIFY_IS_APPROX(tridiag.matrixT(), hess.matrixH());
  34. b = SquareMatrixType::Random(cols,cols);
  35. hess.compute(b);
  36. VERIFY_IS_APPROX(b, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint());
  37. #endif
  38. }
  39. void test_eigen2_qr()
  40. {
  41. for(int i = 0; i < 1; i++) {
  42. CALL_SUBTEST_1( qr(Matrix2f()) );
  43. CALL_SUBTEST_2( qr(Matrix4d()) );
  44. CALL_SUBTEST_3( qr(MatrixXf(12,8)) );
  45. CALL_SUBTEST_4( qr(MatrixXcd(5,5)) );
  46. CALL_SUBTEST_4( qr(MatrixXcd(7,3)) );
  47. }
  48. #ifdef EIGEN_TEST_PART_5
  49. // small isFullRank test
  50. {
  51. Matrix3d mat;
  52. mat << 1, 45, 1, 2, 2, 2, 1, 2, 3;
  53. VERIFY(mat.qr().isFullRank());
  54. mat << 1, 1, 1, 2, 2, 2, 1, 2, 3;
  55. //always returns true in eigen2support
  56. //VERIFY(!mat.qr().isFullRank());
  57. }
  58. #endif
  59. }