00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef EIGEN_EIGENSOLVER_H
00027 #define EIGEN_EIGENSOLVER_H
00028
00029 #include "./EigenvaluesCommon.h"
00030 #include "./RealSchur.h"
00031
00078 template<typename _MatrixType> class EigenSolver
00079 {
00080 public:
00081
00083 typedef _MatrixType MatrixType;
00084
00085 enum {
00086 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
00087 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
00088 Options = MatrixType::Options,
00089 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
00090 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime
00091 };
00092
00094 typedef typename MatrixType::Scalar Scalar;
00095 typedef typename NumTraits<Scalar>::Real RealScalar;
00096 typedef typename MatrixType::Index Index;
00097
00104 typedef std::complex<RealScalar> ComplexScalar;
00105
00111 typedef Matrix<ComplexScalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> EigenvalueType;
00112
00118 typedef Matrix<ComplexScalar, RowsAtCompileTime, ColsAtCompileTime, Options, MaxRowsAtCompileTime, MaxColsAtCompileTime> EigenvectorsType;
00119
00127 EigenSolver() : m_eivec(), m_eivalues(), m_isInitialized(false), m_realSchur(), m_matT(), m_tmp() {}
00128
00135 EigenSolver(Index size)
00136 : m_eivec(size, size),
00137 m_eivalues(size),
00138 m_isInitialized(false),
00139 m_eigenvectorsOk(false),
00140 m_realSchur(size),
00141 m_matT(size, size),
00142 m_tmp(size)
00143 {}
00144
00160 EigenSolver(const MatrixType& matrix, bool computeEigenvectors = true)
00161 : m_eivec(matrix.rows(), matrix.cols()),
00162 m_eivalues(matrix.cols()),
00163 m_isInitialized(false),
00164 m_eigenvectorsOk(false),
00165 m_realSchur(matrix.cols()),
00166 m_matT(matrix.rows(), matrix.cols()),
00167 m_tmp(matrix.cols())
00168 {
00169 compute(matrix, computeEigenvectors);
00170 }
00171
00192 EigenvectorsType eigenvectors() const;
00193
00212 const MatrixType& pseudoEigenvectors() const
00213 {
00214 eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00215 eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
00216 return m_eivec;
00217 }
00218
00237 MatrixType pseudoEigenvalueMatrix() const;
00238
00257 const EigenvalueType& eigenvalues() const
00258 {
00259 eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00260 return m_eivalues;
00261 }
00262
00290 EigenSolver& compute(const MatrixType& matrix, bool computeEigenvectors = true);
00291
00292 ComputationInfo info() const
00293 {
00294 eigen_assert(m_isInitialized && "ComplexEigenSolver is not initialized.");
00295 return m_realSchur.info();
00296 }
00297
00298 private:
00299 void doComputeEigenvectors();
00300
00301 protected:
00302 MatrixType m_eivec;
00303 EigenvalueType m_eivalues;
00304 bool m_isInitialized;
00305 bool m_eigenvectorsOk;
00306 RealSchur<MatrixType> m_realSchur;
00307 MatrixType m_matT;
00308
00309 typedef Matrix<Scalar, ColsAtCompileTime, 1, Options & ~RowMajor, MaxColsAtCompileTime, 1> ColumnVectorType;
00310 ColumnVectorType m_tmp;
00311 };
00312
00313 template<typename MatrixType>
00314 MatrixType EigenSolver<MatrixType>::pseudoEigenvalueMatrix() const
00315 {
00316 eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00317 Index n = m_eivalues.rows();
00318 MatrixType matD = MatrixType::Zero(n,n);
00319 for (Index i=0; i<n; ++i)
00320 {
00321 if (internal::isMuchSmallerThan(internal::imag(m_eivalues.coeff(i)), internal::real(m_eivalues.coeff(i))))
00322 matD.coeffRef(i,i) = internal::real(m_eivalues.coeff(i));
00323 else
00324 {
00325 matD.template block<2,2>(i,i) << internal::real(m_eivalues.coeff(i)), internal::imag(m_eivalues.coeff(i)),
00326 -internal::imag(m_eivalues.coeff(i)), internal::real(m_eivalues.coeff(i));
00327 ++i;
00328 }
00329 }
00330 return matD;
00331 }
00332
00333 template<typename MatrixType>
00334 typename EigenSolver<MatrixType>::EigenvectorsType EigenSolver<MatrixType>::eigenvectors() const
00335 {
00336 eigen_assert(m_isInitialized && "EigenSolver is not initialized.");
00337 eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
00338 Index n = m_eivec.cols();
00339 EigenvectorsType matV(n,n);
00340 for (Index j=0; j<n; ++j)
00341 {
00342 if (internal::isMuchSmallerThan(internal::imag(m_eivalues.coeff(j)), internal::real(m_eivalues.coeff(j))))
00343 {
00344
00345 matV.col(j) = m_eivec.col(j).template cast<ComplexScalar>();
00346 }
00347 else
00348 {
00349
00350 for (Index i=0; i<n; ++i)
00351 {
00352 matV.coeffRef(i,j) = ComplexScalar(m_eivec.coeff(i,j), m_eivec.coeff(i,j+1));
00353 matV.coeffRef(i,j+1) = ComplexScalar(m_eivec.coeff(i,j), -m_eivec.coeff(i,j+1));
00354 }
00355 matV.col(j).normalize();
00356 matV.col(j+1).normalize();
00357 ++j;
00358 }
00359 }
00360 return matV;
00361 }
00362
00363 template<typename MatrixType>
00364 EigenSolver<MatrixType>& EigenSolver<MatrixType>::compute(const MatrixType& matrix, bool computeEigenvectors)
00365 {
00366 assert(matrix.cols() == matrix.rows());
00367
00368
00369 m_realSchur.compute(matrix, computeEigenvectors);
00370 if (m_realSchur.info() == Success)
00371 {
00372 m_matT = m_realSchur.matrixT();
00373 if (computeEigenvectors)
00374 m_eivec = m_realSchur.matrixU();
00375
00376
00377 m_eivalues.resize(matrix.cols());
00378 Index i = 0;
00379 while (i < matrix.cols())
00380 {
00381 if (i == matrix.cols() - 1 || m_matT.coeff(i+1, i) == Scalar(0))
00382 {
00383 m_eivalues.coeffRef(i) = m_matT.coeff(i, i);
00384 ++i;
00385 }
00386 else
00387 {
00388 Scalar p = Scalar(0.5) * (m_matT.coeff(i, i) - m_matT.coeff(i+1, i+1));
00389 Scalar z = internal::sqrt(internal::abs(p * p + m_matT.coeff(i+1, i) * m_matT.coeff(i, i+1)));
00390 m_eivalues.coeffRef(i) = ComplexScalar(m_matT.coeff(i+1, i+1) + p, z);
00391 m_eivalues.coeffRef(i+1) = ComplexScalar(m_matT.coeff(i+1, i+1) + p, -z);
00392 i += 2;
00393 }
00394 }
00395
00396
00397 if (computeEigenvectors)
00398 doComputeEigenvectors();
00399 }
00400
00401 m_isInitialized = true;
00402 m_eigenvectorsOk = computeEigenvectors;
00403
00404 return *this;
00405 }
00406
00407
00408 template<typename Scalar>
00409 std::complex<Scalar> cdiv(Scalar xr, Scalar xi, Scalar yr, Scalar yi)
00410 {
00411 Scalar r,d;
00412 if (internal::abs(yr) > internal::abs(yi))
00413 {
00414 r = yi/yr;
00415 d = yr + r*yi;
00416 return std::complex<Scalar>((xr + r*xi)/d, (xi - r*xr)/d);
00417 }
00418 else
00419 {
00420 r = yr/yi;
00421 d = yi + r*yr;
00422 return std::complex<Scalar>((r*xr + xi)/d, (r*xi - xr)/d);
00423 }
00424 }
00425
00426
00427 template<typename MatrixType>
00428 void EigenSolver<MatrixType>::doComputeEigenvectors()
00429 {
00430 const Index size = m_eivec.cols();
00431 const Scalar eps = NumTraits<Scalar>::epsilon();
00432
00433
00434 Scalar norm = 0.0;
00435 for (Index j = 0; j < size; ++j)
00436 {
00437 norm += m_matT.row(j).segment(std::max(j-1,Index(0)), size-std::max(j-1,Index(0))).cwiseAbs().sum();
00438 }
00439
00440
00441 if (norm == 0.0)
00442 {
00443 return;
00444 }
00445
00446 for (Index n = size-1; n >= 0; n--)
00447 {
00448 Scalar p = m_eivalues.coeff(n).real();
00449 Scalar q = m_eivalues.coeff(n).imag();
00450
00451
00452 if (q == 0)
00453 {
00454 Scalar lastr=0, lastw=0;
00455 Index l = n;
00456
00457 m_matT.coeffRef(n,n) = 1.0;
00458 for (Index i = n-1; i >= 0; i--)
00459 {
00460 Scalar w = m_matT.coeff(i,i) - p;
00461 Scalar r = m_matT.row(i).segment(l,n-l+1).dot(m_matT.col(n).segment(l, n-l+1));
00462
00463 if (m_eivalues.coeff(i).imag() < 0.0)
00464 {
00465 lastw = w;
00466 lastr = r;
00467 }
00468 else
00469 {
00470 l = i;
00471 if (m_eivalues.coeff(i).imag() == 0.0)
00472 {
00473 if (w != 0.0)
00474 m_matT.coeffRef(i,n) = -r / w;
00475 else
00476 m_matT.coeffRef(i,n) = -r / (eps * norm);
00477 }
00478 else
00479 {
00480 Scalar x = m_matT.coeff(i,i+1);
00481 Scalar y = m_matT.coeff(i+1,i);
00482 Scalar denom = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) + m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag();
00483 Scalar t = (x * lastr - lastw * r) / denom;
00484 m_matT.coeffRef(i,n) = t;
00485 if (internal::abs(x) > internal::abs(lastw))
00486 m_matT.coeffRef(i+1,n) = (-r - w * t) / x;
00487 else
00488 m_matT.coeffRef(i+1,n) = (-lastr - y * t) / lastw;
00489 }
00490
00491
00492 Scalar t = internal::abs(m_matT.coeff(i,n));
00493 if ((eps * t) * t > 1)
00494 m_matT.col(n).tail(size-i) /= t;
00495 }
00496 }
00497 }
00498 else if (q < 0)
00499 {
00500 Scalar lastra=0, lastsa=0, lastw=0;
00501 Index l = n-1;
00502
00503
00504 if (internal::abs(m_matT.coeff(n,n-1)) > internal::abs(m_matT.coeff(n-1,n)))
00505 {
00506 m_matT.coeffRef(n-1,n-1) = q / m_matT.coeff(n,n-1);
00507 m_matT.coeffRef(n-1,n) = -(m_matT.coeff(n,n) - p) / m_matT.coeff(n,n-1);
00508 }
00509 else
00510 {
00511 std::complex<Scalar> cc = cdiv<Scalar>(0.0,-m_matT.coeff(n-1,n),m_matT.coeff(n-1,n-1)-p,q);
00512 m_matT.coeffRef(n-1,n-1) = internal::real(cc);
00513 m_matT.coeffRef(n-1,n) = internal::imag(cc);
00514 }
00515 m_matT.coeffRef(n,n-1) = 0.0;
00516 m_matT.coeffRef(n,n) = 1.0;
00517 for (Index i = n-2; i >= 0; i--)
00518 {
00519 Scalar ra = m_matT.row(i).segment(l, n-l+1).dot(m_matT.col(n-1).segment(l, n-l+1));
00520 Scalar sa = m_matT.row(i).segment(l, n-l+1).dot(m_matT.col(n).segment(l, n-l+1));
00521 Scalar w = m_matT.coeff(i,i) - p;
00522
00523 if (m_eivalues.coeff(i).imag() < 0.0)
00524 {
00525 lastw = w;
00526 lastra = ra;
00527 lastsa = sa;
00528 }
00529 else
00530 {
00531 l = i;
00532 if (m_eivalues.coeff(i).imag() == 0)
00533 {
00534 std::complex<Scalar> cc = cdiv(-ra,-sa,w,q);
00535 m_matT.coeffRef(i,n-1) = internal::real(cc);
00536 m_matT.coeffRef(i,n) = internal::imag(cc);
00537 }
00538 else
00539 {
00540
00541 Scalar x = m_matT.coeff(i,i+1);
00542 Scalar y = m_matT.coeff(i+1,i);
00543 Scalar vr = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) + m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag() - q * q;
00544 Scalar vi = (m_eivalues.coeff(i).real() - p) * Scalar(2) * q;
00545 if ((vr == 0.0) && (vi == 0.0))
00546 vr = eps * norm * (internal::abs(w) + internal::abs(q) + internal::abs(x) + internal::abs(y) + internal::abs(lastw));
00547
00548 std::complex<Scalar> cc = cdiv(x*lastra-lastw*ra+q*sa,x*lastsa-lastw*sa-q*ra,vr,vi);
00549 m_matT.coeffRef(i,n-1) = internal::real(cc);
00550 m_matT.coeffRef(i,n) = internal::imag(cc);
00551 if (internal::abs(x) > (internal::abs(lastw) + internal::abs(q)))
00552 {
00553 m_matT.coeffRef(i+1,n-1) = (-ra - w * m_matT.coeff(i,n-1) + q * m_matT.coeff(i,n)) / x;
00554 m_matT.coeffRef(i+1,n) = (-sa - w * m_matT.coeff(i,n) - q * m_matT.coeff(i,n-1)) / x;
00555 }
00556 else
00557 {
00558 cc = cdiv(-lastra-y*m_matT.coeff(i,n-1),-lastsa-y*m_matT.coeff(i,n),lastw,q);
00559 m_matT.coeffRef(i+1,n-1) = internal::real(cc);
00560 m_matT.coeffRef(i+1,n) = internal::imag(cc);
00561 }
00562 }
00563
00564
00565 Scalar t = std::max(internal::abs(m_matT.coeff(i,n-1)),internal::abs(m_matT.coeff(i,n)));
00566 if ((eps * t) * t > 1)
00567 m_matT.block(i, n-1, size-i, 2) /= t;
00568
00569 }
00570 }
00571 }
00572 }
00573
00574
00575 for (Index j = size-1; j >= 0; j--)
00576 {
00577 m_tmp.noalias() = m_eivec.leftCols(j+1) * m_matT.col(j).segment(0, j+1);
00578 m_eivec.col(j) = m_tmp;
00579 }
00580 }
00581
00582 #endif // EIGEN_EIGENSOLVER_H