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