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