diff --git a/Eigen/src/Eigenvalues/ComplexSchur.h b/Eigen/src/Eigenvalues/ComplexSchur.h index 1ab2a0287..04b9d6b80 100644 --- a/Eigen/src/Eigenvalues/ComplexSchur.h +++ b/Eigen/src/Eigenvalues/ComplexSchur.h @@ -86,7 +86,7 @@ template class ComplexSchur /** \brief Default constructor. * - * \param [in] size The size of the matrix whose Schur decomposition will be computed. + * \param [in] size Positive integer, size of the matrix whose Schur decomposition will be computed. * * The default constructor is useful in cases in which the user * intends to perform decompositions via compute(). The \p size @@ -95,7 +95,7 @@ template class ComplexSchur * * \sa compute() for an example. */ - ComplexSchur(int size = RowsAtCompileTime==Dynamic ? 0 : RowsAtCompileTime) + ComplexSchur(int size = RowsAtCompileTime==Dynamic ? 1 : RowsAtCompileTime) : m_matT(size,size), m_matU(size,size), m_isInitialized(false), m_matUisUptodate(false) {} @@ -157,7 +157,7 @@ template class ComplexSchur */ const ComplexMatrixType& matrixT() const { - ei_assert(m_isInitialized && "ComplexShur is not initialized."); + ei_assert(m_isInitialized && "ComplexSchur is not initialized."); return m_matT; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 072f63e1d..f7d5ed8e9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -138,7 +138,9 @@ ei_add_test(qr) ei_add_test(qr_colpivoting) ei_add_test(qr_fullpivoting) ei_add_test(upperbidiagonalization) -ei_add_test(hessenberg " " "${GSL_LIBRARIES}") +ei_add_test(hessenberg) +ei_add_test(schur_real) +ei_add_test(schur_complex) ei_add_test(eigensolver_selfadjoint " " "${GSL_LIBRARIES}") ei_add_test(eigensolver_generic " " "${GSL_LIBRARIES}") ei_add_test(eigensolver_complex) diff --git a/test/hessenberg.cpp b/test/hessenberg.cpp index d917be357..cba9c8fda 100644 --- a/test/hessenberg.cpp +++ b/test/hessenberg.cpp @@ -2,6 +2,7 @@ // for linear algebra. // // Copyright (C) 2009 Gael Guennebaud +// Copyright (C) 2010 Jitse Niesen // // Eigen is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public @@ -28,19 +29,36 @@ template void hessenberg(int size = Size) { typedef Matrix MatrixType; - MatrixType m = MatrixType::Random(size,size); - HessenbergDecomposition hess(m); - VERIFY_IS_APPROX(m, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint()); + // Test basic functionality: A = U H U* and H is Hessenberg + for(int counter = 0; counter < g_repeat; ++counter) { + MatrixType m = MatrixType::Random(size,size); + HessenbergDecomposition hess(m); + VERIFY_IS_APPROX(m, hess.matrixQ() * hess.matrixH() * hess.matrixQ().adjoint()); + MatrixType H = hess.matrixH(); + for(int row = 2; row < size; ++row) { + for(int col = 0; col < row-1; ++col) { + VERIFY(H(row,col) == (typename MatrixType::Scalar)0); + } + } + } + + // Test whether compute() and constructor returns same result + MatrixType A = MatrixType::Random(size, size); + HessenbergDecomposition cs1; + cs1.compute(A); + HessenbergDecomposition cs2(A); + VERIFY_IS_EQUAL(cs1.matrixQ(), cs2.matrixQ()); + VERIFY_IS_EQUAL(cs1.matrixH(), cs2.matrixH()); + + // TODO: Add tests for packedMatrix() and householderCoefficients() } void test_hessenberg() { - for(int i = 0; i < g_repeat; i++) { - CALL_SUBTEST_1(( hessenberg,1>() )); - CALL_SUBTEST_2(( hessenberg,2>() )); - CALL_SUBTEST_3(( hessenberg,4>() )); - CALL_SUBTEST_4(( hessenberg(ei_random(1,320)) )); - CALL_SUBTEST_5(( hessenberg,Dynamic>(ei_random(1,320)) )); - } + CALL_SUBTEST_1(( hessenberg,1>() )); + CALL_SUBTEST_2(( hessenberg,2>() )); + CALL_SUBTEST_3(( hessenberg,4>() )); + CALL_SUBTEST_4(( hessenberg(ei_random(1,320)) )); + CALL_SUBTEST_5(( hessenberg,Dynamic>(ei_random(1,320)) )); } diff --git a/test/schur_complex.cpp b/test/schur_complex.cpp new file mode 100644 index 000000000..3659a074c --- /dev/null +++ b/test/schur_complex.cpp @@ -0,0 +1,67 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2010 Jitse Niesen +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#include "main.h" +#include + +template void schur(int size = MatrixType::ColsAtCompileTime) +{ + typedef typename ComplexSchur::ComplexScalar ComplexScalar; + typedef typename ComplexSchur::ComplexMatrixType ComplexMatrixType; + + // Test basic functionality: T is triangular and A = U T U* + for(int counter = 0; counter < g_repeat; ++counter) { + MatrixType A = MatrixType::Random(size, size); + ComplexSchur schurOfA(A); + ComplexMatrixType U = schurOfA.matrixU(); + ComplexMatrixType T = schurOfA.matrixT(); + for(int row = 1; row < size; ++row) { + for(int col = 0; col < row; ++col) { + VERIFY(T(row,col) == (typename MatrixType::Scalar)0); + } + } + VERIFY_IS_APPROX(A.template cast(), U * T * U.adjoint()); + } + + // Test asserts when not initialized + ComplexSchur csUninitialized; + VERIFY_RAISES_ASSERT(csUninitialized.matrixT()); + VERIFY_RAISES_ASSERT(csUninitialized.matrixU()); + + // Test whether compute() and constructor returns same result + MatrixType A = MatrixType::Random(size, size); + ComplexSchur cs1; + cs1.compute(A); + ComplexSchur cs2(A); + VERIFY_IS_EQUAL(cs1.matrixT(), cs2.matrixT()); + VERIFY_IS_EQUAL(cs1.matrixU(), cs2.matrixU()); +} + +void test_schur_complex() +{ + CALL_SUBTEST_1(( schur() )); + CALL_SUBTEST_2(( schur(ei_random(1,50)) )); + CALL_SUBTEST_3(( schur, 1, 1> >() )); + CALL_SUBTEST_4(( schur >() )); +} diff --git a/test/schur_real.cpp b/test/schur_real.cpp new file mode 100644 index 000000000..77ef5e2dc --- /dev/null +++ b/test/schur_real.cpp @@ -0,0 +1,75 @@ +// This file is part of Eigen, a lightweight C++ template library +// for linear algebra. Eigen itself is part of the KDE project. +// +// Copyright (C) 2010 Jitse Niesen +// +// Eigen is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 3 of the License, or (at your option) any later version. +// +// Alternatively, you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of +// the License, or (at your option) any later version. +// +// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License and a copy of the GNU General Public License along with +// Eigen. If not, see . + +#include "main.h" +#include + +template void verifyIsQuasiTriangular(const MatrixType& T) +{ + const int size = T.cols(); + typedef typename MatrixType::Scalar Scalar; + typedef typename NumTraits::Real RealScalar; + + // The "zeros" in the real Schur decomposition are only approximately zero + RealScalar norm = T.norm(); + + // Check T is lower Hessenberg + for(int row = 2; row < size; ++row) { + for(int col = 0; col < row - 1; ++col) { + VERIFY_IS_MUCH_SMALLER_THAN(T(row,col), norm); + } + } + + // Check that any non-zero on the subdiagonal is followed by a zero and is + // part of a 2x2 diagonal block with imaginary eigenvalues. + for(int row = 1; row < size; ++row) { + if (!test_ei_isMuchSmallerThan(T(row,row-1), norm)) { + VERIFY(row == size-1 || test_ei_isMuchSmallerThan(T(row+1,row), norm)); + Scalar tr = T(row-1,row-1) + T(row,row); + Scalar det = T(row-1,row-1) * T(row,row) - T(row-1,row) * T(row,row-1); + VERIFY(4 * det > tr * tr); + } + } +} + +template void schur(int size = MatrixType::ColsAtCompileTime) +{ + // Test basic functionality: T is quasi-triangular and A = U T U* + for(int counter = 0; counter < g_repeat; ++counter) { + MatrixType A = MatrixType::Random(size, size); + RealSchur schurOfA(A); + MatrixType U = schurOfA.matrixU(); + MatrixType T = schurOfA.matrixT(); + verifyIsQuasiTriangular(T); + VERIFY_IS_APPROX(A, U * T * U.transpose()); + } +} + +void test_schur_real() +{ + CALL_SUBTEST_1(( schur() )); + CALL_SUBTEST_2(( schur(ei_random(1,50)) )); + CALL_SUBTEST_3(( schur >() )); + CALL_SUBTEST_4(( schur >() )); +}