1
// This file is part of Eigen, a lightweight C++ template library
4
// Copyright (C) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5
// Copyright (C) 2010 Jitse Niesen <jitse@maths.leeds.ac.uk>
7
// Eigen is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 3 of the License, or (at your option) any later version.
12
// Alternatively, you can redistribute it and/or
13
// modify it under the terms of the GNU General Public License as
14
// published by the Free Software Foundation; either version 2 of
15
// the License, or (at your option) any later version.
17
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19
// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20
// GNU General Public License for more details.
22
// You should have received a copy of the GNU Lesser General Public
23
// License and a copy of the GNU General Public License along with
24
// Eigen. If not, see <http://www.gnu.org/licenses/>.
26
#ifndef EIGEN_SELFADJOINTEIGENSOLVER_H
27
#define EIGEN_SELFADJOINTEIGENSOLVER_H
29
#include "./EigenvaluesCommon.h"
30
#include "./Tridiagonalization.h"
32
template<typename _MatrixType>
33
class GeneralizedSelfAdjointEigenSolver;
35
/** \eigenvalues_module \ingroup Eigenvalues_Module
38
* \class SelfAdjointEigenSolver
40
* \brief Computes eigenvalues and eigenvectors of selfadjoint matrices
42
* \tparam _MatrixType the type of the matrix of which we are computing the
43
* eigendecomposition; this is expected to be an instantiation of the Matrix
46
* A matrix \f$ A \f$ is selfadjoint if it equals its adjoint. For real
47
* matrices, this means that the matrix is symmetric: it equals its
48
* transpose. This class computes the eigenvalues and eigenvectors of a
49
* selfadjoint matrix. These are the scalars \f$ \lambda \f$ and vectors
50
* \f$ v \f$ such that \f$ Av = \lambda v \f$. The eigenvalues of a
51
* selfadjoint matrix are always real. If \f$ D \f$ is a diagonal matrix with
52
* the eigenvalues on the diagonal, and \f$ V \f$ is a matrix with the
53
* eigenvectors as its columns, then \f$ A = V D V^{-1} \f$ (for selfadjoint
54
* matrices, the matrix \f$ V \f$ is always invertible). This is called the
57
* The algorithm exploits the fact that the matrix is selfadjoint, making it
58
* faster and more accurate than the general purpose eigenvalue algorithms
59
* implemented in EigenSolver and ComplexEigenSolver.
61
* Only the \b lower \b triangular \b part of the input matrix is referenced.
63
* Call the function compute() to compute the eigenvalues and eigenvectors of
64
* a given matrix. Alternatively, you can use the
65
* SelfAdjointEigenSolver(const MatrixType&, int) constructor which computes
66
* the eigenvalues and eigenvectors at construction time. Once the eigenvalue
67
* and eigenvectors are computed, they can be retrieved with the eigenvalues()
68
* and eigenvectors() functions.
70
* The documentation for SelfAdjointEigenSolver(const MatrixType&, int)
71
* contains an example of the typical use of this class.
73
* To solve the \em generalized eigenvalue problem \f$ Av = \lambda Bv \f$ and
74
* the likes, see the class GeneralizedSelfAdjointEigenSolver.
76
* \sa MatrixBase::eigenvalues(), class EigenSolver, class ComplexEigenSolver
78
template<typename _MatrixType> class SelfAdjointEigenSolver
82
typedef _MatrixType MatrixType;
84
Size = MatrixType::RowsAtCompileTime,
85
ColsAtCompileTime = MatrixType::ColsAtCompileTime,
86
Options = MatrixType::Options,
87
MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
90
/** \brief Scalar type for matrices of type \p _MatrixType. */
91
typedef typename MatrixType::Scalar Scalar;
92
typedef typename MatrixType::Index Index;
94
/** \brief Real scalar type for \p _MatrixType.
96
* This is just \c Scalar if #Scalar is real (e.g., \c float or
97
* \c double), and the type of the real part of \c Scalar if #Scalar is
100
typedef typename NumTraits<Scalar>::Real RealScalar;
102
/** \brief Type for vector of eigenvalues as returned by eigenvalues().
104
* This is a column vector with entries of type #RealScalar.
105
* The length of the vector is the size of \p _MatrixType.
107
typedef typename internal::plain_col_type<MatrixType, RealScalar>::type RealVectorType;
108
typedef Tridiagonalization<MatrixType> TridiagonalizationType;
110
/** \brief Default constructor for fixed-size matrices.
112
* The default constructor is useful in cases in which the user intends to
113
* perform decompositions via compute(). This constructor
114
* can only be used if \p _MatrixType is a fixed-size matrix; use
115
* SelfAdjointEigenSolver(Index) for dynamic-size matrices.
117
* Example: \include SelfAdjointEigenSolver_SelfAdjointEigenSolver.cpp
118
* Output: \verbinclude SelfAdjointEigenSolver_SelfAdjointEigenSolver.out
120
SelfAdjointEigenSolver()
124
m_isInitialized(false)
127
/** \brief Constructor, pre-allocates memory for dynamic-size matrices.
129
* \param [in] size Positive integer, size of the matrix whose
130
* eigenvalues and eigenvectors will be computed.
132
* This constructor is useful for dynamic-size matrices, when the user
133
* intends to perform decompositions via compute(). The \p size
134
* parameter is only used as a hint. It is not an error to give a wrong
135
* \p size, but it may impair performance.
137
* \sa compute() for an example
139
SelfAdjointEigenSolver(Index size)
140
: m_eivec(size, size),
142
m_subdiag(size > 1 ? size - 1 : 1),
143
m_isInitialized(false)
146
/** \brief Constructor; computes eigendecomposition of given matrix.
148
* \param[in] matrix Selfadjoint matrix whose eigendecomposition is to
149
* be computed. Only the lower triangular part of the matrix is referenced.
150
* \param[in] options Can be #ComputeEigenvectors (default) or #EigenvaluesOnly.
152
* This constructor calls compute(const MatrixType&, int) to compute the
153
* eigenvalues of the matrix \p matrix. The eigenvectors are computed if
154
* \p options equals #ComputeEigenvectors.
156
* Example: \include SelfAdjointEigenSolver_SelfAdjointEigenSolver_MatrixType.cpp
157
* Output: \verbinclude SelfAdjointEigenSolver_SelfAdjointEigenSolver_MatrixType.out
159
* \sa compute(const MatrixType&, int)
161
SelfAdjointEigenSolver(const MatrixType& matrix, int options = ComputeEigenvectors)
162
: m_eivec(matrix.rows(), matrix.cols()),
163
m_eivalues(matrix.cols()),
164
m_subdiag(matrix.rows() > 1 ? matrix.rows() - 1 : 1),
165
m_isInitialized(false)
167
compute(matrix, options);
170
/** \brief Computes eigendecomposition of given matrix.
172
* \param[in] matrix Selfadjoint matrix whose eigendecomposition is to
173
* be computed. Only the lower triangular part of the matrix is referenced.
174
* \param[in] options Can be #ComputeEigenvectors (default) or #EigenvaluesOnly.
175
* \returns Reference to \c *this
177
* This function computes the eigenvalues of \p matrix. The eigenvalues()
178
* function can be used to retrieve them. If \p options equals #ComputeEigenvectors,
179
* then the eigenvectors are also computed and can be retrieved by
180
* calling eigenvectors().
182
* This implementation uses a symmetric QR algorithm. The matrix is first
183
* reduced to tridiagonal form using the Tridiagonalization class. The
184
* tridiagonal matrix is then brought to diagonal form with implicit
185
* symmetric QR steps with Wilkinson shift. Details can be found in
186
* Section 8.3 of Golub \& Van Loan, <i>%Matrix Computations</i>.
188
* The cost of the computation is about \f$ 9n^3 \f$ if the eigenvectors
189
* are required and \f$ 4n^3/3 \f$ if they are not required.
191
* This method reuses the memory in the SelfAdjointEigenSolver object that
192
* was allocated when the object was constructed, if the size of the
193
* matrix does not change.
195
* Example: \include SelfAdjointEigenSolver_compute_MatrixType.cpp
196
* Output: \verbinclude SelfAdjointEigenSolver_compute_MatrixType.out
198
* \sa SelfAdjointEigenSolver(const MatrixType&, int)
200
SelfAdjointEigenSolver& compute(const MatrixType& matrix, int options = ComputeEigenvectors);
202
/** \brief Returns the eigenvectors of given matrix.
204
* \returns A const reference to the matrix whose columns are the eigenvectors.
206
* \pre The eigenvectors have been computed before.
208
* Column \f$ k \f$ of the returned matrix is an eigenvector corresponding
209
* to eigenvalue number \f$ k \f$ as returned by eigenvalues(). The
210
* eigenvectors are normalized to have (Euclidean) norm equal to one. If
211
* this object was used to solve the eigenproblem for the selfadjoint
212
* matrix \f$ A \f$, then the matrix returned by this function is the
213
* matrix \f$ V \f$ in the eigendecomposition \f$ A = V D V^{-1} \f$.
215
* Example: \include SelfAdjointEigenSolver_eigenvectors.cpp
216
* Output: \verbinclude SelfAdjointEigenSolver_eigenvectors.out
220
const MatrixType& eigenvectors() const
222
eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
223
eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
227
/** \brief Returns the eigenvalues of given matrix.
229
* \returns A const reference to the column vector containing the eigenvalues.
231
* \pre The eigenvalues have been computed before.
233
* The eigenvalues are repeated according to their algebraic multiplicity,
234
* so there are as many eigenvalues as rows in the matrix. The eigenvalues
235
* are sorted in increasing order.
237
* Example: \include SelfAdjointEigenSolver_eigenvalues.cpp
238
* Output: \verbinclude SelfAdjointEigenSolver_eigenvalues.out
240
* \sa eigenvectors(), MatrixBase::eigenvalues()
242
const RealVectorType& eigenvalues() const
244
eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
248
/** \brief Computes the positive-definite square root of the matrix.
250
* \returns the positive-definite square root of the matrix
252
* \pre The eigenvalues and eigenvectors of a positive-definite matrix
253
* have been computed before.
255
* The square root of a positive-definite matrix \f$ A \f$ is the
256
* positive-definite matrix whose square equals \f$ A \f$. This function
257
* uses the eigendecomposition \f$ A = V D V^{-1} \f$ to compute the
258
* square root as \f$ A^{1/2} = V D^{1/2} V^{-1} \f$.
260
* Example: \include SelfAdjointEigenSolver_operatorSqrt.cpp
261
* Output: \verbinclude SelfAdjointEigenSolver_operatorSqrt.out
263
* \sa operatorInverseSqrt(),
264
* \ref MatrixFunctions_Module "MatrixFunctions Module"
266
MatrixType operatorSqrt() const
268
eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
269
eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
270
return m_eivec * m_eivalues.cwiseSqrt().asDiagonal() * m_eivec.adjoint();
273
/** \brief Computes the inverse square root of the matrix.
275
* \returns the inverse positive-definite square root of the matrix
277
* \pre The eigenvalues and eigenvectors of a positive-definite matrix
278
* have been computed before.
280
* This function uses the eigendecomposition \f$ A = V D V^{-1} \f$ to
281
* compute the inverse square root as \f$ V D^{-1/2} V^{-1} \f$. This is
282
* cheaper than first computing the square root with operatorSqrt() and
283
* then its inverse with MatrixBase::inverse().
285
* Example: \include SelfAdjointEigenSolver_operatorInverseSqrt.cpp
286
* Output: \verbinclude SelfAdjointEigenSolver_operatorInverseSqrt.out
288
* \sa operatorSqrt(), MatrixBase::inverse(),
289
* \ref MatrixFunctions_Module "MatrixFunctions Module"
291
MatrixType operatorInverseSqrt() const
293
eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
294
eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
295
return m_eivec * m_eivalues.cwiseInverse().cwiseSqrt().asDiagonal() * m_eivec.adjoint();
298
/** \brief Reports whether previous computation was successful.
300
* \returns \c Success if computation was succesful, \c NoConvergence otherwise.
302
ComputationInfo info() const
304
eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
308
/** \brief Maximum number of iterations.
310
* Maximum number of iterations allowed for an eigenvalue to converge.
312
static const int m_maxIterations = 30;
314
#ifdef EIGEN2_SUPPORT
315
SelfAdjointEigenSolver(const MatrixType& matrix, bool computeEigenvectors)
316
: m_eivec(matrix.rows(), matrix.cols()),
317
m_eivalues(matrix.cols()),
318
m_subdiag(matrix.rows() > 1 ? matrix.rows() - 1 : 1),
319
m_isInitialized(false)
321
compute(matrix, computeEigenvectors);
324
SelfAdjointEigenSolver(const MatrixType& matA, const MatrixType& matB, bool computeEigenvectors = true)
325
: m_eivec(matA.cols(), matA.cols()),
326
m_eivalues(matA.cols()),
327
m_subdiag(matA.cols() > 1 ? matA.cols() - 1 : 1),
328
m_isInitialized(false)
330
static_cast<GeneralizedSelfAdjointEigenSolver<MatrixType>*>(this)->compute(matA, matB, computeEigenvectors ? ComputeEigenvectors : EigenvaluesOnly);
333
void compute(const MatrixType& matrix, bool computeEigenvectors)
335
compute(matrix, computeEigenvectors ? ComputeEigenvectors : EigenvaluesOnly);
338
void compute(const MatrixType& matA, const MatrixType& matB, bool computeEigenvectors = true)
340
compute(matA, matB, computeEigenvectors ? ComputeEigenvectors : EigenvaluesOnly);
342
#endif // EIGEN2_SUPPORT
346
RealVectorType m_eivalues;
347
typename TridiagonalizationType::SubDiagonalType m_subdiag;
348
ComputationInfo m_info;
349
bool m_isInitialized;
350
bool m_eigenvectorsOk;
355
* \eigenvalues_module \ingroup Eigenvalues_Module
357
* Performs a QR step on a tridiagonal symmetric matrix represented as a
358
* pair of two vectors \a diag and \a subdiag.
360
* \param matA the input selfadjoint matrix
361
* \param hCoeffs returned Householder coefficients
363
* For compilation efficiency reasons, this procedure does not use eigen expression
366
* Implemented from Golub's "Matrix Computations", algorithm 8.3.2:
367
* "implicit symmetric QR step with Wilkinson shift"
370
template<int StorageOrder,typename RealScalar, typename Scalar, typename Index>
371
static void tridiagonal_qr_step(RealScalar* diag, RealScalar* subdiag, Index start, Index end, Scalar* matrixQ, Index n);
374
template<typename MatrixType>
375
SelfAdjointEigenSolver<MatrixType>& SelfAdjointEigenSolver<MatrixType>
376
::compute(const MatrixType& matrix, int options)
378
eigen_assert(matrix.cols() == matrix.rows());
379
eigen_assert((options&~(EigVecMask|GenEigMask))==0
380
&& (options&EigVecMask)!=EigVecMask
381
&& "invalid option parameter");
382
bool computeEigenvectors = (options&ComputeEigenvectors)==ComputeEigenvectors;
383
Index n = matrix.cols();
384
m_eivalues.resize(n,1);
388
m_eivalues.coeffRef(0,0) = internal::real(matrix.coeff(0,0));
389
if(computeEigenvectors)
390
m_eivec.setOnes(n,n);
392
m_isInitialized = true;
393
m_eigenvectorsOk = computeEigenvectors;
397
// declare some aliases
398
RealVectorType& diag = m_eivalues;
399
MatrixType& mat = m_eivec;
401
// map the matrix coefficients to [-1:1] to avoid over- and underflow.
402
RealScalar scale = matrix.cwiseAbs().maxCoeff();
403
if(scale==Scalar(0)) scale = 1;
404
mat = matrix / scale;
405
m_subdiag.resize(n-1);
406
internal::tridiagonalization_inplace(mat, diag, m_subdiag, computeEigenvectors);
410
Index iter = 0; // number of iterations we are working on one element
414
for (Index i = start; i<end; ++i)
415
if (internal::isMuchSmallerThan(internal::abs(m_subdiag[i]),(internal::abs(diag[i])+internal::abs(diag[i+1]))))
418
// find the largest unreduced block
419
while (end>0 && m_subdiag[end-1]==0)
427
// if we spent too many iterations on the current element, we give up
429
if(iter > m_maxIterations) break;
432
while (start>0 && m_subdiag[start-1]!=0)
435
internal::tridiagonal_qr_step<MatrixType::Flags&RowMajorBit ? RowMajor : ColMajor>(diag.data(), m_subdiag.data(), start, end, computeEigenvectors ? m_eivec.data() : (Scalar*)0, n);
438
if (iter <= m_maxIterations)
441
m_info = NoConvergence;
443
// Sort eigenvalues and corresponding vectors.
444
// TODO make the sort optional ?
445
// TODO use a better sort algorithm !!
446
if (m_info == Success)
448
for (Index i = 0; i < n-1; ++i)
451
m_eivalues.segment(i,n-i).minCoeff(&k);
454
std::swap(m_eivalues[i], m_eivalues[k+i]);
455
if(computeEigenvectors)
456
m_eivec.col(i).swap(m_eivec.col(k+i));
461
// scale back the eigen values
464
m_isInitialized = true;
465
m_eigenvectorsOk = computeEigenvectors;
470
template<int StorageOrder,typename RealScalar, typename Scalar, typename Index>
471
static void tridiagonal_qr_step(RealScalar* diag, RealScalar* subdiag, Index start, Index end, Scalar* matrixQ, Index n)
473
// NOTE this version avoids over & underflow, however since the matrix is prescaled, overflow cannot occur,
474
// and underflows should be meaningless anyway. So I don't any reason to enable this version, but I keep
475
// it here for reference:
476
// RealScalar td = (diag[end-1] - diag[end])*RealScalar(0.5);
477
// RealScalar e = subdiag[end-1];
478
// RealScalar mu = diag[end] - (e / (td + (td>0 ? 1 : -1))) * (e / hypot(td,e));
479
RealScalar td = (diag[end-1] - diag[end])*RealScalar(0.5);
480
RealScalar e2 = abs2(subdiag[end-1]);
481
RealScalar mu = diag[end] - e2 / (td + (td>0 ? 1 : -1) * sqrt(td*td + e2));
482
RealScalar x = diag[start] - mu;
483
RealScalar z = subdiag[start];
484
for (Index k = start; k < end; ++k)
486
JacobiRotation<RealScalar> rot;
487
rot.makeGivens(x, z);
490
RealScalar sdk = rot.s() * diag[k] + rot.c() * subdiag[k];
491
RealScalar dkp1 = rot.s() * subdiag[k] + rot.c() * diag[k+1];
493
diag[k] = rot.c() * (rot.c() * diag[k] - rot.s() * subdiag[k]) - rot.s() * (rot.c() * subdiag[k] - rot.s() * diag[k+1]);
494
diag[k+1] = rot.s() * sdk + rot.c() * dkp1;
495
subdiag[k] = rot.c() * sdk - rot.s() * dkp1;
499
subdiag[k - 1] = rot.c() * subdiag[k-1] - rot.s() * z;
505
z = -rot.s() * subdiag[k+1];
506
subdiag[k + 1] = rot.c() * subdiag[k+1];
509
// apply the givens rotation to the unit matrix Q = Q * G
512
// FIXME if StorageOrder == RowMajor this operation is not very efficient
513
Map<Matrix<Scalar,Dynamic,Dynamic,StorageOrder> > q(matrixQ,n,n);
514
q.applyOnTheRight(k,k+1,rot);
518
} // end namespace internal
520
#endif // EIGEN_SELFADJOINTEIGENSOLVER_H