Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
ComplexEigenSolver.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2009 Claire Maurice
5// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
6// Copyright (C) 2010,2012 Jitse Niesen <jitse@maths.leeds.ac.uk>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11// SPDX-License-Identifier: MPL-2.0
12
13#ifndef EIGEN_COMPLEX_EIGEN_SOLVER_H
14#define EIGEN_COMPLEX_EIGEN_SOLVER_H
15
16#include "./ComplexSchur.h"
17
18// IWYU pragma: private
19#include "./InternalHeaderCheck.h"
20
21namespace Eigen {
22
49template <typename MatrixType_>
51 public:
53 using MatrixType = MatrixType_;
54
55 enum {
56 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
57 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
58 Options = internal::plain_object_options<MatrixType>::value,
59 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
60 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
61 };
62
64 using Scalar = typename MatrixType::Scalar;
65 using RealScalar = typename NumTraits<Scalar>::Real;
66 using Index = Eigen::Index;
67
74 using ComplexScalar = internal::make_complex_t<Scalar>;
75
81 using EigenvalueType = Matrix<ComplexScalar, ColsAtCompileTime, 1, Options & (~RowMajor), MaxColsAtCompileTime, 1>;
82
90
96 ComplexEigenSolver() : m_eivec(), m_eivalues(), m_schur(), m_isInitialized(false), m_eigenvectorsOk(false) {}
97
105 : m_eivec(size, size), m_eivalues(size), m_schur(size), m_isInitialized(false), m_eigenvectorsOk(false) {}
106
116 template <typename InputType>
117 explicit ComplexEigenSolver(const EigenBase<InputType>& matrix, bool computeEigenvectors = true)
118 : m_eivec(matrix.rows(), matrix.cols()),
119 m_eivalues(matrix.cols()),
120 m_schur(matrix.derived(), computeEigenvectors),
121 m_isInitialized(false),
122 m_eigenvectorsOk(false) {
123 computeFromSchur(computeEigenvectors);
124 }
125
137 template <typename InputType>
138 explicit ComplexEigenSolver(EigenBase<InputType>& matrix, bool computeEigenvectors = true)
139 : m_eivec(matrix.rows(), matrix.cols()),
140 m_eivalues(matrix.cols()),
141 m_schur(matrix.derived(), computeEigenvectors),
142 m_isInitialized(false),
143 m_eigenvectorsOk(false) {
144 computeFromSchur(computeEigenvectors);
145 }
146
168 eigen_assert(m_isInitialized && "ComplexEigenSolver is not initialized.");
169 eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
170 return m_eivec;
171 }
172
192 eigen_assert(m_isInitialized && "ComplexEigenSolver is not initialized.");
193 return m_eivalues;
194 }
195
220 template <typename InputType>
221 ComplexEigenSolver& compute(const EigenBase<InputType>& matrix, bool computeEigenvectors = true);
222
228 eigen_assert(m_isInitialized && "ComplexEigenSolver is not initialized.");
229 return m_schur.info();
230 }
231
234 m_schur.setMaxIterations(maxIters);
235 return *this;
236 }
237
239 Index getMaxIterations() const { return m_schur.getMaxIterations(); }
240
241 protected:
242 EIGEN_STATIC_ASSERT_NON_INTEGER(Scalar)
243
244 EigenvectorType m_eivec;
245 EigenvalueType m_eivalues;
246 // Holds the Schur form of the last matrix; computing the eigenvectors overwrites T with the eigenvectors of T.
248 bool m_isInitialized;
249 bool m_eigenvectorsOk;
250
251 private:
252 ComplexEigenSolver& computeFromSchur(bool computeEigenvectors);
253 void doComputeEigenvectors(RealScalar matrixnorm);
254 void sortEigenvalues(bool computeEigenvectors);
255};
256
257template <typename MatrixType>
258template <typename InputType>
259ComplexEigenSolver<MatrixType>& ComplexEigenSolver<MatrixType>::compute(const EigenBase<InputType>& matrix,
260 bool computeEigenvectors) {
261 // this code is inspired from Jampack
262 EIGEN_INTERNAL_ASSERT_SQUARE(matrix);
263
264 // Do a complex Schur decomposition, A = U T U^*
265 // The eigenvalues are on the diagonal of T.
266 m_schur.compute(matrix.derived(), computeEigenvectors);
267 return computeFromSchur(computeEigenvectors);
268}
269
272template <typename MatrixType>
273ComplexEigenSolver<MatrixType>& ComplexEigenSolver<MatrixType>::computeFromSchur(bool computeEigenvectors) {
274 if (m_schur.info() == Success) {
275 m_eivalues = m_schur.matrixT().diagonal();
276 // |T|_F is infinite for a finite T with entries near max; every use below needs only a bound on max |t_ij|.
277 if (computeEigenvectors)
278 doComputeEigenvectors(numext::mini(m_schur.matrixT().stableNorm(), NumTraits<RealScalar>::highest()));
279 sortEigenvalues(computeEigenvectors);
280 }
281
282 m_isInitialized = true;
283 m_eigenvectorsOk = computeEigenvectors;
284 return *this;
285}
286
287template <typename MatrixType>
288void ComplexEigenSolver<MatrixType>::doComputeEigenvectors(RealScalar matrixnorm) {
289 const Index n = m_eivalues.size();
290
291 // Compute X such that T = X D X^(-1), where D is the diagonal of T.
292 // The matrix X is unit triangular. It overwrites T column by column from the last one: X(i,k) reads T(i,k) before
293 // replacing it, T(i,i+1..k-1) and T(i,i), T(k,k), which lie in columns not yet overwritten, and X(i+1..k-1,k),
294 // already computed.
295 typename ComplexSchur<MatrixType>::MatrixTType& matX = m_schur.m_matT;
296 matX.template triangularView<StrictlyLower>().setZero();
297 // Equal eigenvalues give z = eps * |T|, so one step can multiply the column by 1/eps and a chain of them overflows.
298 // A column of X is only defined up to a factor: if |X(i,k)| would exceed B, divide by g the part of the column
299 // computed so far, the pending entries T(0..i-1,k) and the diagonal entry (which tracks the factor), so that
300 // |X(i,k)| = |num| / (g |z|) = B. All entries of X then stay below B, and the products with T below max / 4
301 // for B <= max / (4 n |T|_F). Sizes are measured by the larger of the real and imaginary parts.
302 RealScalar bigThreshold = numext::sqrt(NumTraits<RealScalar>::highest());
303 // Divide in this order: 4 n |T|_F overflows for |T|_F > max / (4 n), and the bound would become 0.
304 if (matrixnorm > RealScalar(0))
305 bigThreshold =
306 numext::mini<RealScalar>(bigThreshold, NumTraits<RealScalar>::highest() / matrixnorm / RealScalar(4 * n));
307 for (Index k = n - 1; k >= 0; k--) {
308 RealScalar diagonalScale(1); // X(k,k) after the rescalings of column k
309 // Compute X(i,k) using the (i,k) entry of the equation X T = D X
310 for (Index i = k - 1; i >= 0; i--) {
311 matX.coeffRef(i, k) = -matX.coeff(i, k);
312 if (k - i - 1 > 0)
313 matX.coeffRef(i, k) -= (matX.row(i).segment(i + 1, k - i - 1) * matX.col(k).segment(i + 1, k - i - 1)).value();
314 ComplexScalar z = matX.coeff(i, i) - matX.coeff(k, k);
315 if (z == ComplexScalar(0)) {
316 // If the i-th and k-th eigenvalue are equal, then z equals 0.
317 // Use a small value instead, to prevent division by zero.
318 // The perturbation eps |T| can be subnormal (|T| below min() / eps); only a zero one is replaced.
319 numext::real_ref(z) = std::numeric_limits<RealScalar>::epsilon() * matrixnorm;
320 if (numext::is_exactly_zero(numext::real(z))) numext::real_ref(z) = (std::numeric_limits<RealScalar>::min)();
321 }
322 const RealScalar numMax =
323 numext::maxi(numext::abs(numext::real(matX.coeff(i, k))), numext::abs(numext::imag(matX.coeff(i, k))));
324 const RealScalar zMax = numext::maxi(numext::abs(numext::real(z)), numext::abs(numext::imag(z)));
325 if (numMax > zMax * bigThreshold) {
326 // zMax * B < numMax, so g = zMax * B / numMax is below one and forming it cannot overflow. It can underflow
327 // to zero; then divide and multiply separately, which is the same scaling of the column.
328 const RealScalar bound = zMax * bigThreshold;
329 const RealScalar g = bound / numMax;
330 if (g >= (std::numeric_limits<RealScalar>::min)()) {
331 matX.col(k).head(k) *= g;
332 diagonalScale *= g;
333 } else {
334 matX.col(k).head(k) /= numMax;
335 matX.col(k).head(k) *= bound;
336 diagonalScale = diagonalScale / numMax * bound;
337 }
338 }
339 matX.coeffRef(i, k) /= z;
340 }
341 matX.coeffRef(k, k) = ComplexScalar(diagonalScale, RealScalar(0));
342 }
343
344 // Compute V as V = U X; now A = U T U^* = U X D X^(-1) U^* = V D V^(-1)
345 m_eivec.noalias() = m_schur.matrixU() * matX;
346 // .. and normalize the eigenvectors
347 for (Index k = 0; k < n; k++) {
348 m_eivec.col(k).stableNormalize();
349 }
350}
351
352template <typename MatrixType>
353void ComplexEigenSolver<MatrixType>::sortEigenvalues(bool computeEigenvectors) {
354 const Index n = m_eivalues.size();
355 for (Index i = 0; i < n; i++) {
356 Index k;
357 m_eivalues.cwiseAbs().tail(n - i).minCoeff(&k);
358 if (k != 0) {
359 k += i;
360 std::swap(m_eivalues[k], m_eivalues[i]);
361 if (computeEigenvectors) m_eivec.col(i).swap(m_eivec.col(k));
362 }
363 }
364}
365
366} // end namespace Eigen
367
368#endif // EIGEN_COMPLEX_EIGEN_SOLVER_H
Computes eigenvalues and eigenvectors of general complex matrices.
Definition ComplexEigenSolver.h:50
MatrixType_ MatrixType
Synonym for the template parameter MatrixType_.
Definition ComplexEigenSolver.h:53
ComplexEigenSolver & compute(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Computes eigendecomposition of given matrix.
ComplexEigenSolver(Index size)
Default Constructor with memory preallocation.
Definition ComplexEigenSolver.h:104
internal::make_complex_t< Scalar > ComplexScalar
Complex scalar type for MatrixType.
Definition ComplexEigenSolver.h:74
ComplexEigenSolver()
Default constructor.
Definition ComplexEigenSolver.h:96
ComplexEigenSolver & setMaxIterations(Index maxIters)
Sets the maximum number of iterations allowed.
Definition ComplexEigenSolver.h:233
typename MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition ComplexEigenSolver.h:64
ComplexEigenSolver(const EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Constructor; computes eigendecomposition of given matrix.
Definition ComplexEigenSolver.h:117
ComplexEigenSolver(EigenBase< InputType > &matrix, bool computeEigenvectors=true)
Constructor for inplace decomposition .
Definition ComplexEigenSolver.h:138
const EigenvectorType & eigenvectors() const
Returns the eigenvectors of given matrix.
Definition ComplexEigenSolver.h:167
Matrix< ComplexScalar, ColsAtCompileTime, 1, Options &(~RowMajor), MaxColsAtCompileTime, 1 > EigenvalueType
Type for vector of eigenvalues as returned by eigenvalues().
Definition ComplexEigenSolver.h:81
Matrix< ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime > EigenvectorType
Type for matrix of eigenvectors as returned by eigenvectors().
Definition ComplexEigenSolver.h:88
Index getMaxIterations() const
Returns the maximum number of iterations.
Definition ComplexEigenSolver.h:239
const EigenvalueType & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition ComplexEigenSolver.h:191
ComputationInfo info() const
Reports whether previous computation was successful.
Definition ComplexEigenSolver.h:227
Eigen::Index Index
Definition ComplexEigenSolver.h:66
Performs a complex Schur decomposition of a real or complex square matrix.
Definition ComplexSchur.h:60
std::conditional_t< internal::is_ref< MatrixType >::value, MatrixType, ComplexMatrixType > MatrixTType
Type of the matrix returned by matrixT().
Definition ComplexSchur.h:96
The matrix class, also used for vectors and row-vectors.
Definition Matrix.h:188
ComputationInfo
Definition Constants.h:455
@ Success
Definition Constants.h:457
@ RowMajor
Definition Constants.h:321
Definition EigenBase.h:34