Committer:
jsoh91
Date:
Tue Sep 24 00:18:23 2019 +0000
Revision:
1:3b8049da21b8
Parent:
0:13a5d365ba16
ignore and revise some of error parts

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ykuroda 0:13a5d365ba16 1 // This file is part of Eigen, a lightweight C++ template library
ykuroda 0:13a5d365ba16 2 // for linear algebra.
ykuroda 0:13a5d365ba16 3 //
ykuroda 0:13a5d365ba16 4 // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
ykuroda 0:13a5d365ba16 5 // Copyright (C) 2010,2012 Jitse Niesen <jitse@maths.leeds.ac.uk>
ykuroda 0:13a5d365ba16 6 //
ykuroda 0:13a5d365ba16 7 // This Source Code Form is subject to the terms of the Mozilla
ykuroda 0:13a5d365ba16 8 // Public License v. 2.0. If a copy of the MPL was not distributed
ykuroda 0:13a5d365ba16 9 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
ykuroda 0:13a5d365ba16 10
ykuroda 0:13a5d365ba16 11 #ifndef EIGEN_REAL_SCHUR_H
ykuroda 0:13a5d365ba16 12 #define EIGEN_REAL_SCHUR_H
ykuroda 0:13a5d365ba16 13
ykuroda 0:13a5d365ba16 14 #include "./HessenbergDecomposition.h"
ykuroda 0:13a5d365ba16 15
ykuroda 0:13a5d365ba16 16 namespace Eigen {
ykuroda 0:13a5d365ba16 17
ykuroda 0:13a5d365ba16 18 /** \eigenvalues_module \ingroup Eigenvalues_Module
ykuroda 0:13a5d365ba16 19 *
ykuroda 0:13a5d365ba16 20 *
ykuroda 0:13a5d365ba16 21 * \class RealSchur
ykuroda 0:13a5d365ba16 22 *
ykuroda 0:13a5d365ba16 23 * \brief Performs a real Schur decomposition of a square matrix
ykuroda 0:13a5d365ba16 24 *
ykuroda 0:13a5d365ba16 25 * \tparam _MatrixType the type of the matrix of which we are computing the
ykuroda 0:13a5d365ba16 26 * real Schur decomposition; this is expected to be an instantiation of the
ykuroda 0:13a5d365ba16 27 * Matrix class template.
ykuroda 0:13a5d365ba16 28 *
ykuroda 0:13a5d365ba16 29 * Given a real square matrix A, this class computes the real Schur
ykuroda 0:13a5d365ba16 30 * decomposition: \f$ A = U T U^T \f$ where U is a real orthogonal matrix and
ykuroda 0:13a5d365ba16 31 * T is a real quasi-triangular matrix. An orthogonal matrix is a matrix whose
ykuroda 0:13a5d365ba16 32 * inverse is equal to its transpose, \f$ U^{-1} = U^T \f$. A quasi-triangular
ykuroda 0:13a5d365ba16 33 * matrix is a block-triangular matrix whose diagonal consists of 1-by-1
ykuroda 0:13a5d365ba16 34 * blocks and 2-by-2 blocks with complex eigenvalues. The eigenvalues of the
ykuroda 0:13a5d365ba16 35 * blocks on the diagonal of T are the same as the eigenvalues of the matrix
ykuroda 0:13a5d365ba16 36 * A, and thus the real Schur decomposition is used in EigenSolver to compute
ykuroda 0:13a5d365ba16 37 * the eigendecomposition of a matrix.
ykuroda 0:13a5d365ba16 38 *
ykuroda 0:13a5d365ba16 39 * Call the function compute() to compute the real Schur decomposition of a
ykuroda 0:13a5d365ba16 40 * given matrix. Alternatively, you can use the RealSchur(const MatrixType&, bool)
ykuroda 0:13a5d365ba16 41 * constructor which computes the real Schur decomposition at construction
ykuroda 0:13a5d365ba16 42 * time. Once the decomposition is computed, you can use the matrixU() and
ykuroda 0:13a5d365ba16 43 * matrixT() functions to retrieve the matrices U and T in the decomposition.
ykuroda 0:13a5d365ba16 44 *
ykuroda 0:13a5d365ba16 45 * The documentation of RealSchur(const MatrixType&, bool) contains an example
ykuroda 0:13a5d365ba16 46 * of the typical use of this class.
ykuroda 0:13a5d365ba16 47 *
ykuroda 0:13a5d365ba16 48 * \note The implementation is adapted from
ykuroda 0:13a5d365ba16 49 * <a href="http://math.nist.gov/javanumerics/jama/">JAMA</a> (public domain).
ykuroda 0:13a5d365ba16 50 * Their code is based on EISPACK.
ykuroda 0:13a5d365ba16 51 *
ykuroda 0:13a5d365ba16 52 * \sa class ComplexSchur, class EigenSolver, class ComplexEigenSolver
ykuroda 0:13a5d365ba16 53 */
ykuroda 0:13a5d365ba16 54 template<typename _MatrixType> class RealSchur
ykuroda 0:13a5d365ba16 55 {
ykuroda 0:13a5d365ba16 56 public:
ykuroda 0:13a5d365ba16 57 typedef _MatrixType MatrixType;
ykuroda 0:13a5d365ba16 58 enum {
ykuroda 0:13a5d365ba16 59 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
ykuroda 0:13a5d365ba16 60 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
ykuroda 0:13a5d365ba16 61 Options = MatrixType::Options,
ykuroda 0:13a5d365ba16 62 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
ykuroda 0:13a5d365ba16 63 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
ykuroda 0:13a5d365ba16 64 };
ykuroda 0:13a5d365ba16 65 typedef typename MatrixType::Scalar Scalar;
ykuroda 0:13a5d365ba16 66 typedef std::complex<typename NumTraits<Scalar>::Real> ComplexScalar;
ykuroda 0:13a5d365ba16 67 typedef typename MatrixType::Index Index;
ykuroda 0:13a5d365ba16 68
ykuroda 0:13a5d365ba16 69 typedef Matrix<ComplexScalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> EigenvalueType;
ykuroda 0:13a5d365ba16 70 typedef Matrix<Scalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> ColumnVectorType;
ykuroda 0:13a5d365ba16 71
ykuroda 0:13a5d365ba16 72 /** \brief Default constructor.
ykuroda 0:13a5d365ba16 73 *
ykuroda 0:13a5d365ba16 74 * \param [in] size Positive integer, size of the matrix whose Schur decomposition will be computed.
ykuroda 0:13a5d365ba16 75 *
ykuroda 0:13a5d365ba16 76 * The default constructor is useful in cases in which the user intends to
ykuroda 0:13a5d365ba16 77 * perform decompositions via compute(). The \p size parameter is only
ykuroda 0:13a5d365ba16 78 * used as a hint. It is not an error to give a wrong \p size, but it may
ykuroda 0:13a5d365ba16 79 * impair performance.
ykuroda 0:13a5d365ba16 80 *
ykuroda 0:13a5d365ba16 81 * \sa compute() for an example.
ykuroda 0:13a5d365ba16 82 */
ykuroda 0:13a5d365ba16 83 RealSchur(Index size = RowsAtCompileTime==Dynamic ? 1 : RowsAtCompileTime)
ykuroda 0:13a5d365ba16 84 : m_matT(size, size),
ykuroda 0:13a5d365ba16 85 m_matU(size, size),
ykuroda 0:13a5d365ba16 86 m_workspaceVector(size),
ykuroda 0:13a5d365ba16 87 m_hess(size),
ykuroda 0:13a5d365ba16 88 m_isInitialized(false),
ykuroda 0:13a5d365ba16 89 m_matUisUptodate(false),
ykuroda 0:13a5d365ba16 90 m_maxIters(-1)
ykuroda 0:13a5d365ba16 91 { }
ykuroda 0:13a5d365ba16 92
ykuroda 0:13a5d365ba16 93 /** \brief Constructor; computes real Schur decomposition of given matrix.
ykuroda 0:13a5d365ba16 94 *
ykuroda 0:13a5d365ba16 95 * \param[in] matrix Square matrix whose Schur decomposition is to be computed.
ykuroda 0:13a5d365ba16 96 * \param[in] computeU If true, both T and U are computed; if false, only T is computed.
ykuroda 0:13a5d365ba16 97 *
ykuroda 0:13a5d365ba16 98 * This constructor calls compute() to compute the Schur decomposition.
ykuroda 0:13a5d365ba16 99 *
ykuroda 0:13a5d365ba16 100 * Example: \include RealSchur_RealSchur_MatrixType.cpp
ykuroda 0:13a5d365ba16 101 * Output: \verbinclude RealSchur_RealSchur_MatrixType.out
ykuroda 0:13a5d365ba16 102 */
ykuroda 0:13a5d365ba16 103 RealSchur(const MatrixType& matrix, bool computeU = true)
ykuroda 0:13a5d365ba16 104 : m_matT(matrix.rows(),matrix.cols()),
ykuroda 0:13a5d365ba16 105 m_matU(matrix.rows(),matrix.cols()),
ykuroda 0:13a5d365ba16 106 m_workspaceVector(matrix.rows()),
ykuroda 0:13a5d365ba16 107 m_hess(matrix.rows()),
ykuroda 0:13a5d365ba16 108 m_isInitialized(false),
ykuroda 0:13a5d365ba16 109 m_matUisUptodate(false),
ykuroda 0:13a5d365ba16 110 m_maxIters(-1)
ykuroda 0:13a5d365ba16 111 {
ykuroda 0:13a5d365ba16 112 compute(matrix, computeU);
ykuroda 0:13a5d365ba16 113 }
ykuroda 0:13a5d365ba16 114
ykuroda 0:13a5d365ba16 115 /** \brief Returns the orthogonal matrix in the Schur decomposition.
ykuroda 0:13a5d365ba16 116 *
ykuroda 0:13a5d365ba16 117 * \returns A const reference to the matrix U.
ykuroda 0:13a5d365ba16 118 *
ykuroda 0:13a5d365ba16 119 * \pre Either the constructor RealSchur(const MatrixType&, bool) or the
ykuroda 0:13a5d365ba16 120 * member function compute(const MatrixType&, bool) has been called before
ykuroda 0:13a5d365ba16 121 * to compute the Schur decomposition of a matrix, and \p computeU was set
ykuroda 0:13a5d365ba16 122 * to true (the default value).
ykuroda 0:13a5d365ba16 123 *
ykuroda 0:13a5d365ba16 124 * \sa RealSchur(const MatrixType&, bool) for an example
ykuroda 0:13a5d365ba16 125 */
ykuroda 0:13a5d365ba16 126 const MatrixType& matrixU() const
ykuroda 0:13a5d365ba16 127 {
ykuroda 0:13a5d365ba16 128 eigen_assert(m_isInitialized && "RealSchur is not initialized.");
ykuroda 0:13a5d365ba16 129 eigen_assert(m_matUisUptodate && "The matrix U has not been computed during the RealSchur decomposition.");
ykuroda 0:13a5d365ba16 130 return m_matU;
ykuroda 0:13a5d365ba16 131 }
ykuroda 0:13a5d365ba16 132
ykuroda 0:13a5d365ba16 133 /** \brief Returns the quasi-triangular matrix in the Schur decomposition.
ykuroda 0:13a5d365ba16 134 *
ykuroda 0:13a5d365ba16 135 * \returns A const reference to the matrix T.
ykuroda 0:13a5d365ba16 136 *
ykuroda 0:13a5d365ba16 137 * \pre Either the constructor RealSchur(const MatrixType&, bool) or the
ykuroda 0:13a5d365ba16 138 * member function compute(const MatrixType&, bool) has been called before
ykuroda 0:13a5d365ba16 139 * to compute the Schur decomposition of a matrix.
ykuroda 0:13a5d365ba16 140 *
ykuroda 0:13a5d365ba16 141 * \sa RealSchur(const MatrixType&, bool) for an example
ykuroda 0:13a5d365ba16 142 */
ykuroda 0:13a5d365ba16 143 const MatrixType& matrixT() const
ykuroda 0:13a5d365ba16 144 {
ykuroda 0:13a5d365ba16 145 eigen_assert(m_isInitialized && "RealSchur is not initialized.");
ykuroda 0:13a5d365ba16 146 return m_matT;
ykuroda 0:13a5d365ba16 147 }
ykuroda 0:13a5d365ba16 148
ykuroda 0:13a5d365ba16 149 /** \brief Computes Schur decomposition of given matrix.
ykuroda 0:13a5d365ba16 150 *
ykuroda 0:13a5d365ba16 151 * \param[in] matrix Square matrix whose Schur decomposition is to be computed.
ykuroda 0:13a5d365ba16 152 * \param[in] computeU If true, both T and U are computed; if false, only T is computed.
ykuroda 0:13a5d365ba16 153 * \returns Reference to \c *this
ykuroda 0:13a5d365ba16 154 *
ykuroda 0:13a5d365ba16 155 * The Schur decomposition is computed by first reducing the matrix to
ykuroda 0:13a5d365ba16 156 * Hessenberg form using the class HessenbergDecomposition. The Hessenberg
ykuroda 0:13a5d365ba16 157 * matrix is then reduced to triangular form by performing Francis QR
ykuroda 0:13a5d365ba16 158 * iterations with implicit double shift. The cost of computing the Schur
ykuroda 0:13a5d365ba16 159 * decomposition depends on the number of iterations; as a rough guide, it
ykuroda 0:13a5d365ba16 160 * may be taken to be \f$25n^3\f$ flops if \a computeU is true and
ykuroda 0:13a5d365ba16 161 * \f$10n^3\f$ flops if \a computeU is false.
ykuroda 0:13a5d365ba16 162 *
ykuroda 0:13a5d365ba16 163 * Example: \include RealSchur_compute.cpp
ykuroda 0:13a5d365ba16 164 * Output: \verbinclude RealSchur_compute.out
ykuroda 0:13a5d365ba16 165 *
ykuroda 0:13a5d365ba16 166 * \sa compute(const MatrixType&, bool, Index)
ykuroda 0:13a5d365ba16 167 */
ykuroda 0:13a5d365ba16 168 RealSchur& compute(const MatrixType& matrix, bool computeU = true);
ykuroda 0:13a5d365ba16 169
ykuroda 0:13a5d365ba16 170 /** \brief Computes Schur decomposition of a Hessenberg matrix H = Z T Z^T
ykuroda 0:13a5d365ba16 171 * \param[in] matrixH Matrix in Hessenberg form H
ykuroda 0:13a5d365ba16 172 * \param[in] matrixQ orthogonal matrix Q that transform a matrix A to H : A = Q H Q^T
ykuroda 0:13a5d365ba16 173 * \param computeU Computes the matriX U of the Schur vectors
ykuroda 0:13a5d365ba16 174 * \return Reference to \c *this
ykuroda 0:13a5d365ba16 175 *
ykuroda 0:13a5d365ba16 176 * This routine assumes that the matrix is already reduced in Hessenberg form matrixH
ykuroda 0:13a5d365ba16 177 * using either the class HessenbergDecomposition or another mean.
ykuroda 0:13a5d365ba16 178 * It computes the upper quasi-triangular matrix T of the Schur decomposition of H
ykuroda 0:13a5d365ba16 179 * When computeU is true, this routine computes the matrix U such that
ykuroda 0:13a5d365ba16 180 * A = U T U^T = (QZ) T (QZ)^T = Q H Q^T where A is the initial matrix
ykuroda 0:13a5d365ba16 181 *
ykuroda 0:13a5d365ba16 182 * NOTE Q is referenced if computeU is true; so, if the initial orthogonal matrix
ykuroda 0:13a5d365ba16 183 * is not available, the user should give an identity matrix (Q.setIdentity())
ykuroda 0:13a5d365ba16 184 *
ykuroda 0:13a5d365ba16 185 * \sa compute(const MatrixType&, bool)
ykuroda 0:13a5d365ba16 186 */
ykuroda 0:13a5d365ba16 187 template<typename HessMatrixType, typename OrthMatrixType>
ykuroda 0:13a5d365ba16 188 RealSchur& computeFromHessenberg(const HessMatrixType& matrixH, const OrthMatrixType& matrixQ, bool computeU);
ykuroda 0:13a5d365ba16 189 /** \brief Reports whether previous computation was successful.
ykuroda 0:13a5d365ba16 190 *
ykuroda 0:13a5d365ba16 191 * \returns \c Success if computation was succesful, \c NoConvergence otherwise.
ykuroda 0:13a5d365ba16 192 */
ykuroda 0:13a5d365ba16 193 ComputationInfo info() const
ykuroda 0:13a5d365ba16 194 {
ykuroda 0:13a5d365ba16 195 eigen_assert(m_isInitialized && "RealSchur is not initialized.");
ykuroda 0:13a5d365ba16 196 return m_info;
ykuroda 0:13a5d365ba16 197 }
ykuroda 0:13a5d365ba16 198
ykuroda 0:13a5d365ba16 199 /** \brief Sets the maximum number of iterations allowed.
ykuroda 0:13a5d365ba16 200 *
ykuroda 0:13a5d365ba16 201 * If not specified by the user, the maximum number of iterations is m_maxIterationsPerRow times the size
ykuroda 0:13a5d365ba16 202 * of the matrix.
ykuroda 0:13a5d365ba16 203 */
ykuroda 0:13a5d365ba16 204 RealSchur& setMaxIterations(Index maxIters)
ykuroda 0:13a5d365ba16 205 {
ykuroda 0:13a5d365ba16 206 m_maxIters = maxIters;
ykuroda 0:13a5d365ba16 207 return *this;
ykuroda 0:13a5d365ba16 208 }
ykuroda 0:13a5d365ba16 209
ykuroda 0:13a5d365ba16 210 /** \brief Returns the maximum number of iterations. */
ykuroda 0:13a5d365ba16 211 Index getMaxIterations()
ykuroda 0:13a5d365ba16 212 {
ykuroda 0:13a5d365ba16 213 return m_maxIters;
ykuroda 0:13a5d365ba16 214 }
ykuroda 0:13a5d365ba16 215
ykuroda 0:13a5d365ba16 216 /** \brief Maximum number of iterations per row.
ykuroda 0:13a5d365ba16 217 *
ykuroda 0:13a5d365ba16 218 * If not otherwise specified, the maximum number of iterations is this number times the size of the
ykuroda 0:13a5d365ba16 219 * matrix. It is currently set to 40.
ykuroda 0:13a5d365ba16 220 */
ykuroda 0:13a5d365ba16 221 static const int m_maxIterationsPerRow = 40;
ykuroda 0:13a5d365ba16 222
ykuroda 0:13a5d365ba16 223 private:
ykuroda 0:13a5d365ba16 224
ykuroda 0:13a5d365ba16 225 MatrixType m_matT;
ykuroda 0:13a5d365ba16 226 MatrixType m_matU;
ykuroda 0:13a5d365ba16 227 ColumnVectorType m_workspaceVector;
ykuroda 0:13a5d365ba16 228 HessenbergDecomposition<MatrixType> m_hess;
ykuroda 0:13a5d365ba16 229 ComputationInfo m_info;
ykuroda 0:13a5d365ba16 230 bool m_isInitialized;
ykuroda 0:13a5d365ba16 231 bool m_matUisUptodate;
ykuroda 0:13a5d365ba16 232 Index m_maxIters;
ykuroda 0:13a5d365ba16 233
ykuroda 0:13a5d365ba16 234 typedef Matrix<Scalar,3,1> Vector3s;
ykuroda 0:13a5d365ba16 235
ykuroda 0:13a5d365ba16 236 Scalar computeNormOfT();
ykuroda 0:13a5d365ba16 237 Index findSmallSubdiagEntry(Index iu);
ykuroda 0:13a5d365ba16 238 void splitOffTwoRows(Index iu, bool computeU, const Scalar& exshift);
ykuroda 0:13a5d365ba16 239 void computeShift(Index iu, Index iter, Scalar& exshift, Vector3s& shiftInfo);
ykuroda 0:13a5d365ba16 240 void initFrancisQRStep(Index il, Index iu, const Vector3s& shiftInfo, Index& im, Vector3s& firstHouseholderVector);
ykuroda 0:13a5d365ba16 241 void performFrancisQRStep(Index il, Index im, Index iu, bool computeU, const Vector3s& firstHouseholderVector, Scalar* workspace);
ykuroda 0:13a5d365ba16 242 };
ykuroda 0:13a5d365ba16 243
ykuroda 0:13a5d365ba16 244
ykuroda 0:13a5d365ba16 245 template<typename MatrixType>
ykuroda 0:13a5d365ba16 246 RealSchur<MatrixType>& RealSchur<MatrixType>::compute(const MatrixType& matrix, bool computeU)
ykuroda 0:13a5d365ba16 247 {
ykuroda 0:13a5d365ba16 248 eigen_assert(matrix.cols() == matrix.rows());
ykuroda 0:13a5d365ba16 249 Index maxIters = m_maxIters;
ykuroda 0:13a5d365ba16 250 if (maxIters == -1)
ykuroda 0:13a5d365ba16 251 maxIters = m_maxIterationsPerRow * matrix.rows();
ykuroda 0:13a5d365ba16 252
ykuroda 0:13a5d365ba16 253 // Step 1. Reduce to Hessenberg form
ykuroda 0:13a5d365ba16 254 m_hess.compute(matrix);
ykuroda 0:13a5d365ba16 255
ykuroda 0:13a5d365ba16 256 // Step 2. Reduce to real Schur form
ykuroda 0:13a5d365ba16 257 computeFromHessenberg(m_hess.matrixH(), m_hess.matrixQ(), computeU);
ykuroda 0:13a5d365ba16 258
ykuroda 0:13a5d365ba16 259 return *this;
ykuroda 0:13a5d365ba16 260 }
ykuroda 0:13a5d365ba16 261 template<typename MatrixType>
ykuroda 0:13a5d365ba16 262 template<typename HessMatrixType, typename OrthMatrixType>
ykuroda 0:13a5d365ba16 263 RealSchur<MatrixType>& RealSchur<MatrixType>::computeFromHessenberg(const HessMatrixType& matrixH, const OrthMatrixType& matrixQ, bool computeU)
ykuroda 0:13a5d365ba16 264 {
ykuroda 0:13a5d365ba16 265 m_matT = matrixH;
ykuroda 0:13a5d365ba16 266 if(computeU)
ykuroda 0:13a5d365ba16 267 m_matU = matrixQ;
ykuroda 0:13a5d365ba16 268
ykuroda 0:13a5d365ba16 269 Index maxIters = m_maxIters;
ykuroda 0:13a5d365ba16 270 if (maxIters == -1)
ykuroda 0:13a5d365ba16 271 maxIters = m_maxIterationsPerRow * matrixH.rows();
ykuroda 0:13a5d365ba16 272 m_workspaceVector.resize(m_matT.cols());
ykuroda 0:13a5d365ba16 273 Scalar* workspace = &m_workspaceVector.coeffRef(0);
ykuroda 0:13a5d365ba16 274
ykuroda 0:13a5d365ba16 275 // The matrix m_matT is divided in three parts.
ykuroda 0:13a5d365ba16 276 // Rows 0,...,il-1 are decoupled from the rest because m_matT(il,il-1) is zero.
ykuroda 0:13a5d365ba16 277 // Rows il,...,iu is the part we are working on (the active window).
ykuroda 0:13a5d365ba16 278 // Rows iu+1,...,end are already brought in triangular form.
ykuroda 0:13a5d365ba16 279 Index iu = m_matT.cols() - 1;
ykuroda 0:13a5d365ba16 280 Index iter = 0; // iteration count for current eigenvalue
ykuroda 0:13a5d365ba16 281 Index totalIter = 0; // iteration count for whole matrix
ykuroda 0:13a5d365ba16 282 Scalar exshift(0); // sum of exceptional shifts
ykuroda 0:13a5d365ba16 283 Scalar norm = computeNormOfT();
ykuroda 0:13a5d365ba16 284
ykuroda 0:13a5d365ba16 285 if(norm!=0)
ykuroda 0:13a5d365ba16 286 {
ykuroda 0:13a5d365ba16 287 while (iu >= 0)
ykuroda 0:13a5d365ba16 288 {
ykuroda 0:13a5d365ba16 289 Index il = findSmallSubdiagEntry(iu);
ykuroda 0:13a5d365ba16 290
ykuroda 0:13a5d365ba16 291 // Check for convergence
ykuroda 0:13a5d365ba16 292 if (il == iu) // One root found
ykuroda 0:13a5d365ba16 293 {
ykuroda 0:13a5d365ba16 294 m_matT.coeffRef(iu,iu) = m_matT.coeff(iu,iu) + exshift;
ykuroda 0:13a5d365ba16 295 if (iu > 0)
ykuroda 0:13a5d365ba16 296 m_matT.coeffRef(iu, iu-1) = Scalar(0);
ykuroda 0:13a5d365ba16 297 iu--;
ykuroda 0:13a5d365ba16 298 iter = 0;
ykuroda 0:13a5d365ba16 299 }
ykuroda 0:13a5d365ba16 300 else if (il == iu-1) // Two roots found
ykuroda 0:13a5d365ba16 301 {
ykuroda 0:13a5d365ba16 302 splitOffTwoRows(iu, computeU, exshift);
ykuroda 0:13a5d365ba16 303 iu -= 2;
ykuroda 0:13a5d365ba16 304 iter = 0;
ykuroda 0:13a5d365ba16 305 }
ykuroda 0:13a5d365ba16 306 else // No convergence yet
ykuroda 0:13a5d365ba16 307 {
ykuroda 0:13a5d365ba16 308 // The firstHouseholderVector vector has to be initialized to something to get rid of a silly GCC warning (-O1 -Wall -DNDEBUG )
ykuroda 0:13a5d365ba16 309 Vector3s firstHouseholderVector(0,0,0), shiftInfo;
ykuroda 0:13a5d365ba16 310 computeShift(iu, iter, exshift, shiftInfo);
ykuroda 0:13a5d365ba16 311 iter = iter + 1;
ykuroda 0:13a5d365ba16 312 totalIter = totalIter + 1;
ykuroda 0:13a5d365ba16 313 if (totalIter > maxIters) break;
ykuroda 0:13a5d365ba16 314 Index im;
ykuroda 0:13a5d365ba16 315 initFrancisQRStep(il, iu, shiftInfo, im, firstHouseholderVector);
ykuroda 0:13a5d365ba16 316 performFrancisQRStep(il, im, iu, computeU, firstHouseholderVector, workspace);
ykuroda 0:13a5d365ba16 317 }
ykuroda 0:13a5d365ba16 318 }
ykuroda 0:13a5d365ba16 319 }
ykuroda 0:13a5d365ba16 320 if(totalIter <= maxIters)
ykuroda 0:13a5d365ba16 321 m_info = Success;
ykuroda 0:13a5d365ba16 322 else
ykuroda 0:13a5d365ba16 323 m_info = NoConvergence;
ykuroda 0:13a5d365ba16 324
ykuroda 0:13a5d365ba16 325 m_isInitialized = true;
ykuroda 0:13a5d365ba16 326 m_matUisUptodate = computeU;
ykuroda 0:13a5d365ba16 327 return *this;
ykuroda 0:13a5d365ba16 328 }
ykuroda 0:13a5d365ba16 329
ykuroda 0:13a5d365ba16 330 /** \internal Computes and returns vector L1 norm of T */
ykuroda 0:13a5d365ba16 331 template<typename MatrixType>
ykuroda 0:13a5d365ba16 332 inline typename MatrixType::Scalar RealSchur<MatrixType>::computeNormOfT()
ykuroda 0:13a5d365ba16 333 {
ykuroda 0:13a5d365ba16 334 const Index size = m_matT.cols();
ykuroda 0:13a5d365ba16 335 // FIXME to be efficient the following would requires a triangular reduxion code
ykuroda 0:13a5d365ba16 336 // Scalar norm = m_matT.upper().cwiseAbs().sum()
ykuroda 0:13a5d365ba16 337 // + m_matT.bottomLeftCorner(size-1,size-1).diagonal().cwiseAbs().sum();
ykuroda 0:13a5d365ba16 338 Scalar norm(0);
ykuroda 0:13a5d365ba16 339 for (Index j = 0; j < size; ++j)
ykuroda 0:13a5d365ba16 340 norm += m_matT.col(j).segment(0, (std::min)(size,j+2)).cwiseAbs().sum();
ykuroda 0:13a5d365ba16 341 return norm;
ykuroda 0:13a5d365ba16 342 }
ykuroda 0:13a5d365ba16 343
ykuroda 0:13a5d365ba16 344 /** \internal Look for single small sub-diagonal element and returns its index */
ykuroda 0:13a5d365ba16 345 template<typename MatrixType>
ykuroda 0:13a5d365ba16 346 inline typename MatrixType::Index RealSchur<MatrixType>::findSmallSubdiagEntry(Index iu)
ykuroda 0:13a5d365ba16 347 {
ykuroda 0:13a5d365ba16 348 using std::abs;
ykuroda 0:13a5d365ba16 349 Index res = iu;
ykuroda 0:13a5d365ba16 350 while (res > 0)
ykuroda 0:13a5d365ba16 351 {
ykuroda 0:13a5d365ba16 352 Scalar s = abs(m_matT.coeff(res-1,res-1)) + abs(m_matT.coeff(res,res));
ykuroda 0:13a5d365ba16 353 if (abs(m_matT.coeff(res,res-1)) <= NumTraits<Scalar>::epsilon() * s)
ykuroda 0:13a5d365ba16 354 break;
ykuroda 0:13a5d365ba16 355 res--;
ykuroda 0:13a5d365ba16 356 }
ykuroda 0:13a5d365ba16 357 return res;
ykuroda 0:13a5d365ba16 358 }
ykuroda 0:13a5d365ba16 359
ykuroda 0:13a5d365ba16 360 /** \internal Update T given that rows iu-1 and iu decouple from the rest. */
ykuroda 0:13a5d365ba16 361 template<typename MatrixType>
ykuroda 0:13a5d365ba16 362 inline void RealSchur<MatrixType>::splitOffTwoRows(Index iu, bool computeU, const Scalar& exshift)
ykuroda 0:13a5d365ba16 363 {
ykuroda 0:13a5d365ba16 364 using std::sqrt;
ykuroda 0:13a5d365ba16 365 using std::abs;
ykuroda 0:13a5d365ba16 366 const Index size = m_matT.cols();
ykuroda 0:13a5d365ba16 367
ykuroda 0:13a5d365ba16 368 // The eigenvalues of the 2x2 matrix [a b; c d] are
ykuroda 0:13a5d365ba16 369 // trace +/- sqrt(discr/4) where discr = tr^2 - 4*det, tr = a + d, det = ad - bc
ykuroda 0:13a5d365ba16 370 Scalar p = Scalar(0.5) * (m_matT.coeff(iu-1,iu-1) - m_matT.coeff(iu,iu));
ykuroda 0:13a5d365ba16 371 Scalar q = p * p + m_matT.coeff(iu,iu-1) * m_matT.coeff(iu-1,iu); // q = tr^2 / 4 - det = discr/4
ykuroda 0:13a5d365ba16 372 m_matT.coeffRef(iu,iu) += exshift;
ykuroda 0:13a5d365ba16 373 m_matT.coeffRef(iu-1,iu-1) += exshift;
ykuroda 0:13a5d365ba16 374
ykuroda 0:13a5d365ba16 375 if (q >= Scalar(0)) // Two real eigenvalues
ykuroda 0:13a5d365ba16 376 {
ykuroda 0:13a5d365ba16 377 Scalar z = sqrt(abs(q));
ykuroda 0:13a5d365ba16 378 JacobiRotation<Scalar> rot;
ykuroda 0:13a5d365ba16 379 if (p >= Scalar(0))
ykuroda 0:13a5d365ba16 380 rot.makeGivens(p + z, m_matT.coeff(iu, iu-1));
ykuroda 0:13a5d365ba16 381 else
ykuroda 0:13a5d365ba16 382 rot.makeGivens(p - z, m_matT.coeff(iu, iu-1));
ykuroda 0:13a5d365ba16 383
ykuroda 0:13a5d365ba16 384 m_matT.rightCols(size-iu+1).applyOnTheLeft(iu-1, iu, rot.adjoint());
ykuroda 0:13a5d365ba16 385 m_matT.topRows(iu+1).applyOnTheRight(iu-1, iu, rot);
ykuroda 0:13a5d365ba16 386 m_matT.coeffRef(iu, iu-1) = Scalar(0);
ykuroda 0:13a5d365ba16 387 if (computeU)
ykuroda 0:13a5d365ba16 388 m_matU.applyOnTheRight(iu-1, iu, rot);
ykuroda 0:13a5d365ba16 389 }
ykuroda 0:13a5d365ba16 390
ykuroda 0:13a5d365ba16 391 if (iu > 1)
ykuroda 0:13a5d365ba16 392 m_matT.coeffRef(iu-1, iu-2) = Scalar(0);
ykuroda 0:13a5d365ba16 393 }
ykuroda 0:13a5d365ba16 394
ykuroda 0:13a5d365ba16 395 /** \internal Form shift in shiftInfo, and update exshift if an exceptional shift is performed. */
ykuroda 0:13a5d365ba16 396 template<typename MatrixType>
ykuroda 0:13a5d365ba16 397 inline void RealSchur<MatrixType>::computeShift(Index iu, Index iter, Scalar& exshift, Vector3s& shiftInfo)
ykuroda 0:13a5d365ba16 398 {
ykuroda 0:13a5d365ba16 399 using std::sqrt;
ykuroda 0:13a5d365ba16 400 using std::abs;
ykuroda 0:13a5d365ba16 401 shiftInfo.coeffRef(0) = m_matT.coeff(iu,iu);
ykuroda 0:13a5d365ba16 402 shiftInfo.coeffRef(1) = m_matT.coeff(iu-1,iu-1);
ykuroda 0:13a5d365ba16 403 shiftInfo.coeffRef(2) = m_matT.coeff(iu,iu-1) * m_matT.coeff(iu-1,iu);
ykuroda 0:13a5d365ba16 404
ykuroda 0:13a5d365ba16 405 // Wilkinson's original ad hoc shift
ykuroda 0:13a5d365ba16 406 if (iter == 10)
ykuroda 0:13a5d365ba16 407 {
ykuroda 0:13a5d365ba16 408 exshift += shiftInfo.coeff(0);
ykuroda 0:13a5d365ba16 409 for (Index i = 0; i <= iu; ++i)
ykuroda 0:13a5d365ba16 410 m_matT.coeffRef(i,i) -= shiftInfo.coeff(0);
ykuroda 0:13a5d365ba16 411 Scalar s = abs(m_matT.coeff(iu,iu-1)) + abs(m_matT.coeff(iu-1,iu-2));
ykuroda 0:13a5d365ba16 412 shiftInfo.coeffRef(0) = Scalar(0.75) * s;
ykuroda 0:13a5d365ba16 413 shiftInfo.coeffRef(1) = Scalar(0.75) * s;
ykuroda 0:13a5d365ba16 414 shiftInfo.coeffRef(2) = Scalar(-0.4375) * s * s;
ykuroda 0:13a5d365ba16 415 }
ykuroda 0:13a5d365ba16 416
ykuroda 0:13a5d365ba16 417 // MATLAB's new ad hoc shift
ykuroda 0:13a5d365ba16 418 if (iter == 30)
ykuroda 0:13a5d365ba16 419 {
ykuroda 0:13a5d365ba16 420 Scalar s = (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
ykuroda 0:13a5d365ba16 421 s = s * s + shiftInfo.coeff(2);
ykuroda 0:13a5d365ba16 422 if (s > Scalar(0))
ykuroda 0:13a5d365ba16 423 {
ykuroda 0:13a5d365ba16 424 s = sqrt(s);
ykuroda 0:13a5d365ba16 425 if (shiftInfo.coeff(1) < shiftInfo.coeff(0))
ykuroda 0:13a5d365ba16 426 s = -s;
ykuroda 0:13a5d365ba16 427 s = s + (shiftInfo.coeff(1) - shiftInfo.coeff(0)) / Scalar(2.0);
ykuroda 0:13a5d365ba16 428 s = shiftInfo.coeff(0) - shiftInfo.coeff(2) / s;
ykuroda 0:13a5d365ba16 429 exshift += s;
ykuroda 0:13a5d365ba16 430 for (Index i = 0; i <= iu; ++i)
ykuroda 0:13a5d365ba16 431 m_matT.coeffRef(i,i) -= s;
ykuroda 0:13a5d365ba16 432 shiftInfo.setConstant(Scalar(0.964));
ykuroda 0:13a5d365ba16 433 }
ykuroda 0:13a5d365ba16 434 }
ykuroda 0:13a5d365ba16 435 }
ykuroda 0:13a5d365ba16 436
ykuroda 0:13a5d365ba16 437 /** \internal Compute index im at which Francis QR step starts and the first Householder vector. */
ykuroda 0:13a5d365ba16 438 template<typename MatrixType>
ykuroda 0:13a5d365ba16 439 inline void RealSchur<MatrixType>::initFrancisQRStep(Index il, Index iu, const Vector3s& shiftInfo, Index& im, Vector3s& firstHouseholderVector)
ykuroda 0:13a5d365ba16 440 {
ykuroda 0:13a5d365ba16 441 using std::abs;
ykuroda 0:13a5d365ba16 442 Vector3s& v = firstHouseholderVector; // alias to save typing
ykuroda 0:13a5d365ba16 443
ykuroda 0:13a5d365ba16 444 for (im = iu-2; im >= il; --im)
ykuroda 0:13a5d365ba16 445 {
ykuroda 0:13a5d365ba16 446 const Scalar Tmm = m_matT.coeff(im,im);
ykuroda 0:13a5d365ba16 447 const Scalar r = shiftInfo.coeff(0) - Tmm;
ykuroda 0:13a5d365ba16 448 const Scalar s = shiftInfo.coeff(1) - Tmm;
ykuroda 0:13a5d365ba16 449 v.coeffRef(0) = (r * s - shiftInfo.coeff(2)) / m_matT.coeff(im+1,im) + m_matT.coeff(im,im+1);
ykuroda 0:13a5d365ba16 450 v.coeffRef(1) = m_matT.coeff(im+1,im+1) - Tmm - r - s;
ykuroda 0:13a5d365ba16 451 v.coeffRef(2) = m_matT.coeff(im+2,im+1);
ykuroda 0:13a5d365ba16 452 if (im == il) {
ykuroda 0:13a5d365ba16 453 break;
ykuroda 0:13a5d365ba16 454 }
ykuroda 0:13a5d365ba16 455 const Scalar lhs = m_matT.coeff(im,im-1) * (abs(v.coeff(1)) + abs(v.coeff(2)));
ykuroda 0:13a5d365ba16 456 const Scalar rhs = v.coeff(0) * (abs(m_matT.coeff(im-1,im-1)) + abs(Tmm) + abs(m_matT.coeff(im+1,im+1)));
ykuroda 0:13a5d365ba16 457 if (abs(lhs) < NumTraits<Scalar>::epsilon() * rhs)
ykuroda 0:13a5d365ba16 458 break;
ykuroda 0:13a5d365ba16 459 }
ykuroda 0:13a5d365ba16 460 }
ykuroda 0:13a5d365ba16 461
ykuroda 0:13a5d365ba16 462 /** \internal Perform a Francis QR step involving rows il:iu and columns im:iu. */
ykuroda 0:13a5d365ba16 463 template<typename MatrixType>
ykuroda 0:13a5d365ba16 464 inline void RealSchur<MatrixType>::performFrancisQRStep(Index il, Index im, Index iu, bool computeU, const Vector3s& firstHouseholderVector, Scalar* workspace)
ykuroda 0:13a5d365ba16 465 {
ykuroda 0:13a5d365ba16 466 eigen_assert(im >= il);
ykuroda 0:13a5d365ba16 467 eigen_assert(im <= iu-2);
ykuroda 0:13a5d365ba16 468
ykuroda 0:13a5d365ba16 469 const Index size = m_matT.cols();
ykuroda 0:13a5d365ba16 470
ykuroda 0:13a5d365ba16 471 for (Index k = im; k <= iu-2; ++k)
ykuroda 0:13a5d365ba16 472 {
ykuroda 0:13a5d365ba16 473 bool firstIteration = (k == im);
ykuroda 0:13a5d365ba16 474
ykuroda 0:13a5d365ba16 475 Vector3s v;
ykuroda 0:13a5d365ba16 476 if (firstIteration)
ykuroda 0:13a5d365ba16 477 v = firstHouseholderVector;
ykuroda 0:13a5d365ba16 478 else
ykuroda 0:13a5d365ba16 479 v = m_matT.template block<3,1>(k,k-1);
ykuroda 0:13a5d365ba16 480
ykuroda 0:13a5d365ba16 481 Scalar tau, beta;
ykuroda 0:13a5d365ba16 482 Matrix<Scalar, 2, 1> ess;
ykuroda 0:13a5d365ba16 483 v.makeHouseholder(ess, tau, beta);
ykuroda 0:13a5d365ba16 484
ykuroda 0:13a5d365ba16 485 if (beta != Scalar(0)) // if v is not zero
ykuroda 0:13a5d365ba16 486 {
ykuroda 0:13a5d365ba16 487 if (firstIteration && k > il)
ykuroda 0:13a5d365ba16 488 m_matT.coeffRef(k,k-1) = -m_matT.coeff(k,k-1);
ykuroda 0:13a5d365ba16 489 else if (!firstIteration)
ykuroda 0:13a5d365ba16 490 m_matT.coeffRef(k,k-1) = beta;
ykuroda 0:13a5d365ba16 491
ykuroda 0:13a5d365ba16 492 // These Householder transformations form the O(n^3) part of the algorithm
ykuroda 0:13a5d365ba16 493 m_matT.block(k, k, 3, size-k).applyHouseholderOnTheLeft(ess, tau, workspace);
ykuroda 0:13a5d365ba16 494 m_matT.block(0, k, (std::min)(iu,k+3) + 1, 3).applyHouseholderOnTheRight(ess, tau, workspace);
ykuroda 0:13a5d365ba16 495 if (computeU)
ykuroda 0:13a5d365ba16 496 m_matU.block(0, k, size, 3).applyHouseholderOnTheRight(ess, tau, workspace);
ykuroda 0:13a5d365ba16 497 }
ykuroda 0:13a5d365ba16 498 }
ykuroda 0:13a5d365ba16 499
ykuroda 0:13a5d365ba16 500 Matrix<Scalar, 2, 1> v = m_matT.template block<2,1>(iu-1, iu-2);
ykuroda 0:13a5d365ba16 501 Scalar tau, beta;
ykuroda 0:13a5d365ba16 502 Matrix<Scalar, 1, 1> ess;
ykuroda 0:13a5d365ba16 503 v.makeHouseholder(ess, tau, beta);
ykuroda 0:13a5d365ba16 504
ykuroda 0:13a5d365ba16 505 if (beta != Scalar(0)) // if v is not zero
ykuroda 0:13a5d365ba16 506 {
ykuroda 0:13a5d365ba16 507 m_matT.coeffRef(iu-1, iu-2) = beta;
ykuroda 0:13a5d365ba16 508 m_matT.block(iu-1, iu-1, 2, size-iu+1).applyHouseholderOnTheLeft(ess, tau, workspace);
ykuroda 0:13a5d365ba16 509 m_matT.block(0, iu-1, iu+1, 2).applyHouseholderOnTheRight(ess, tau, workspace);
ykuroda 0:13a5d365ba16 510 if (computeU)
ykuroda 0:13a5d365ba16 511 m_matU.block(0, iu-1, size, 2).applyHouseholderOnTheRight(ess, tau, workspace);
ykuroda 0:13a5d365ba16 512 }
ykuroda 0:13a5d365ba16 513
ykuroda 0:13a5d365ba16 514 // clean up pollution due to round-off errors
ykuroda 0:13a5d365ba16 515 for (Index i = im+2; i <= iu; ++i)
ykuroda 0:13a5d365ba16 516 {
ykuroda 0:13a5d365ba16 517 m_matT.coeffRef(i,i-2) = Scalar(0);
ykuroda 0:13a5d365ba16 518 if (i > im+2)
ykuroda 0:13a5d365ba16 519 m_matT.coeffRef(i,i-3) = Scalar(0);
ykuroda 0:13a5d365ba16 520 }
ykuroda 0:13a5d365ba16 521 }
ykuroda 0:13a5d365ba16 522
ykuroda 0:13a5d365ba16 523 } // end namespace Eigen
ykuroda 0:13a5d365ba16 524
ykuroda 0:13a5d365ba16 525 #endif // EIGEN_REAL_SCHUR_H