schur_real.cpp
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2010,2012 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 
10 #include "main.h"
11 #include <limits>
12 #include <Eigen/Eigenvalues>
13 
14 template<typename MatrixType> void verifyIsQuasiTriangular(const MatrixType& T)
15 {
16  const Index size = T.cols();
17  typedef typename MatrixType::Scalar Scalar;
18 
19  // Check T is lower Hessenberg
20  for(int row = 2; row < size; ++row) {
21  for(int col = 0; col < row - 1; ++col) {
22  VERIFY(T(row,col) == Scalar(0));
23  }
24  }
25 
26  // Check that any non-zero on the subdiagonal is followed by a zero and is
27  // part of a 2x2 diagonal block with imaginary eigenvalues.
28  for(int row = 1; row < size; ++row) {
29  if (T(row,row-1) != Scalar(0)) {
30  VERIFY(row == size-1 || T(row+1,row) == 0);
31  Scalar tr = T(row-1,row-1) + T(row,row);
32  Scalar det = T(row-1,row-1) * T(row,row) - T(row-1,row) * T(row,row-1);
33  VERIFY(4 * det > tr * tr);
34  }
35  }
36 }
37 
38 template<typename MatrixType> void schur(int size = MatrixType::ColsAtCompileTime)
39 {
40  // Test basic functionality: T is quasi-triangular and A = U T U*
41  for(int counter = 0; counter < g_repeat; ++counter) {
42  MatrixType A = MatrixType::Random(size, size);
45  MatrixType U = schurOfA.matrixU();
46  MatrixType T = schurOfA.matrixT();
48  VERIFY_IS_APPROX(A, U * T * U.transpose());
49  }
50 
51  // Test asserts when not initialized
52  RealSchur<MatrixType> rsUninitialized;
53  VERIFY_RAISES_ASSERT(rsUninitialized.matrixT());
54  VERIFY_RAISES_ASSERT(rsUninitialized.matrixU());
55  VERIFY_RAISES_ASSERT(rsUninitialized.info());
56 
57  // Test whether compute() and constructor returns same result
58  MatrixType A = MatrixType::Random(size, size);
60  rs1.compute(A);
64  VERIFY_IS_EQUAL(rs1.matrixT(), rs2.matrixT());
65  VERIFY_IS_EQUAL(rs1.matrixU(), rs2.matrixU());
66 
67  // Test maximum number of iterations
71  VERIFY_IS_EQUAL(rs3.matrixT(), rs1.matrixT());
72  VERIFY_IS_EQUAL(rs3.matrixU(), rs1.matrixU());
73  if (size > 2) {
74  rs3.setMaxIterations(1).compute(A);
77  }
78 
79  MatrixType Atriangular = A;
80  Atriangular.template triangularView<StrictlyLower>().setZero();
81  rs3.setMaxIterations(1).compute(Atriangular); // triangular matrices do not need any iterations
83  VERIFY_IS_APPROX(rs3.matrixT(), Atriangular); // approx because of scaling...
84  VERIFY_IS_EQUAL(rs3.matrixU(), MatrixType::Identity(size, size));
85 
86  // Test computation of only T, not U
87  RealSchur<MatrixType> rsOnlyT(A, false);
88  VERIFY_IS_EQUAL(rsOnlyT.info(), Success);
89  VERIFY_IS_EQUAL(rs1.matrixT(), rsOnlyT.matrixT());
90  VERIFY_RAISES_ASSERT(rsOnlyT.matrixU());
91 
92  if (size > 2 && size < 20)
93  {
94  // Test matrix with NaN
95  A(0,0) = std::numeric_limits<typename MatrixType::Scalar>::quiet_NaN();
96  RealSchur<MatrixType> rsNaN(A);
98  }
99 }
100 
102 {
103  CALL_SUBTEST_1(( schur<Matrix4f>() ));
104  CALL_SUBTEST_2(( schur<MatrixXd>(internal::random<int>(1,EIGEN_TEST_MAX_SIZE/4)) ));
107 
108  // Test problem size constructors
110 }
verifyIsQuasiTriangular
void verifyIsQuasiTriangular(const MatrixType &T)
Definition: schur_real.cpp:14
col
m col(1)
MatrixType
MatrixXf MatrixType
Definition: benchmark-blocking-sizes.cpp:52
schur
void schur(int size=MatrixType::ColsAtCompileTime)
Definition: schur_real.cpp:38
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
Eigen::RealSchur::matrixT
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
Definition: RealSchur.h:144
Eigen::Success
@ Success
Definition: Constants.h:442
T
Eigen::Triplet< double > T
Definition: Tutorial_sparse_example.cpp:6
schurOfA
cout<< "Here is a random 4x4 matrix, A:"<< endl<< A<< endl<< endl;ComplexSchur< MatrixXcf > schurOfA(A, false)
Eigen::RealSchur::compute
RealSchur & compute(const EigenBase< InputType > &matrix, bool computeU=true)
Computes Schur decomposition of given matrix.
Eigen::RealSchur::info
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: RealSchur.h:195
VERIFY_RAISES_ASSERT
#define VERIFY_RAISES_ASSERT(a)
Definition: main.h:340
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
CALL_SUBTEST_4
#define CALL_SUBTEST_4(FUNC)
Definition: split_test_helper.h:22
CALL_SUBTEST_3
#define CALL_SUBTEST_3(FUNC)
Definition: split_test_helper.h:16
CALL_SUBTEST_1
#define CALL_SUBTEST_1(FUNC)
Definition: split_test_helper.h:4
Eigen::RealSchur< MatrixType >
Eigen::NoConvergence
@ NoConvergence
Definition: Constants.h:446
Eigen::RealSchur::getMaxIterations
Index getMaxIterations()
Returns the maximum number of iterations.
Definition: RealSchur.h:213
CALL_SUBTEST_5
#define CALL_SUBTEST_5(FUNC)
Definition: split_test_helper.h:28
Eigen::g_repeat
static int g_repeat
Definition: main.h:169
Eigen::Triplet< double >
CALL_SUBTEST_2
#define CALL_SUBTEST_2(FUNC)
Definition: split_test_helper.h:10
VERIFY_IS_APPROX
#define VERIFY_IS_APPROX(a, b)
Definition: integer_types.cpp:15
main.h
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(schur_real)
Definition: schur_real.cpp:101
row
m row(1)
EIGEN_TEST_MAX_SIZE
#define EIGEN_TEST_MAX_SIZE
Definition: boostmultiprec.cpp:16
U
@ U
Definition: testDecisionTree.cpp:349
Eigen::Matrix
The matrix class, also used for vectors and row-vectors.
Definition: 3rdparty/Eigen/Eigen/src/Core/Matrix.h:178
Eigen::RealSchur::matrixU
const MatrixType & matrixU() const
Returns the orthogonal matrix in the Schur decomposition.
Definition: RealSchur.h:127
Eigen::RealSchur::setMaxIterations
RealSchur & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition: RealSchur.h:206
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
VERIFY
#define VERIFY(a)
Definition: main.h:380
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:04:02