eigen2_qr.cpp revision c981c48f5bc9aefeffc0bcb0cc3934c2fae179dd
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
10#include "main.h"
11#include <Eigen/QR>
12
13template<typename MatrixType> void qr(const MatrixType& m)
14{
15  /* this test covers the following files:
16     QR.h
17  */
18  int rows = m.rows();
19  int cols = m.cols();
20
21  typedef typename MatrixType::Scalar Scalar;
22  typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, MatrixType::ColsAtCompileTime> SquareMatrixType;
23  typedef Matrix<Scalar, MatrixType::ColsAtCompileTime, 1> VectorType;
24
25  MatrixType a = MatrixType::Random(rows,cols);
26  QR<MatrixType> qrOfA(a);
27  VERIFY_IS_APPROX(a, qrOfA.matrixQ() * qrOfA.matrixR());
28  VERIFY_IS_NOT_APPROX(a+MatrixType::Identity(rows, cols), qrOfA.matrixQ() * qrOfA.matrixR());
29
30  #if 0 // eigenvalues module not yet ready
31  SquareMatrixType b = a.adjoint() * a;
32
33  // check tridiagonalization
34  Tridiagonalization<SquareMatrixType> tridiag(b);
35  VERIFY_IS_APPROX(b, tridiag.matrixQ() * tridiag.matrixT() * tridiag.matrixQ().adjoint());
36
37  // check hessenberg decomposition
38  HessenbergDecomposition<SquareMatrixType> hess(b);
39  VERIFY_IS_APPROX(b, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint());
40  VERIFY_IS_APPROX(tridiag.matrixT(), hess.matrixH());
41  b = SquareMatrixType::Random(cols,cols);
42  hess.compute(b);
43  VERIFY_IS_APPROX(b, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint());
44  #endif
45}
46
47void test_eigen2_qr()
48{
49  for(int i = 0; i < 1; i++) {
50    CALL_SUBTEST_1( qr(Matrix2f()) );
51    CALL_SUBTEST_2( qr(Matrix4d()) );
52    CALL_SUBTEST_3( qr(MatrixXf(12,8)) );
53    CALL_SUBTEST_4( qr(MatrixXcd(5,5)) );
54    CALL_SUBTEST_4( qr(MatrixXcd(7,3)) );
55  }
56
57#ifdef EIGEN_TEST_PART_5
58  // small isFullRank test
59  {
60    Matrix3d mat;
61    mat << 1, 45, 1, 2, 2, 2, 1, 2, 3;
62    VERIFY(mat.qr().isFullRank());
63    mat << 1, 1, 1, 2, 2, 2, 1, 2, 3;
64    //always returns true in eigen2support
65    //VERIFY(!mat.qr().isFullRank());
66  }
67
68#endif
69}
70