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 #ifndef EIGEN_EIGENSOLVER_H
00026 #define EIGEN_EIGENSOLVER_H
00027
00043 template<typename _MatrixType> class EigenSolver
00044 {
00045 public:
00046
00047 typedef _MatrixType MatrixType;
00048 typedef typename MatrixType::Scalar Scalar;
00049 typedef typename NumTraits<Scalar>::Real RealScalar;
00050 typedef std::complex<RealScalar> Complex;
00051 typedef Matrix<Complex, MatrixType::ColsAtCompileTime, 1> EigenvalueType;
00052 typedef Matrix<Complex, MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime> EigenvectorType;
00053 typedef Matrix<RealScalar, MatrixType::ColsAtCompileTime, 1> RealVectorType;
00054 typedef Matrix<RealScalar, Dynamic, 1> RealVectorTypeX;
00055
00062 EigenSolver() : m_eivec(), m_eivalues(), m_isInitialized(false) {}
00063
00064 EigenSolver(const MatrixType& matrix)
00065 : m_eivec(matrix.rows(), matrix.cols()),
00066 m_eivalues(matrix.cols()),
00067 m_isInitialized(false)
00068 {
00069 compute(matrix);
00070 }
00071
00072
00073 EigenvectorType eigenvectors(void) const;
00074
00106 const MatrixType& pseudoEigenvectors() const
00107 {
00108 ei_assert(m_isInitialized && "EigenSolver is not initialized.");
00109 return m_eivec;
00110 }
00111
00112 MatrixType pseudoEigenvalueMatrix() const;
00113
00115 EigenvalueType eigenvalues() const
00116 {
00117 ei_assert(m_isInitialized && "EigenSolver is not initialized.");
00118 return m_eivalues;
00119 }
00120
00121 void compute(const MatrixType& matrix);
00122
00123 private:
00124
00125 void orthes(MatrixType& matH, RealVectorType& ort);
00126 void hqr2(MatrixType& matH);
00127
00128 protected:
00129 MatrixType m_eivec;
00130 EigenvalueType m_eivalues;
00131 bool m_isInitialized;
00132 };
00133
00138 template<typename MatrixType>
00139 MatrixType EigenSolver<MatrixType>::pseudoEigenvalueMatrix() const
00140 {
00141 ei_assert(m_isInitialized && "EigenSolver is not initialized.");
00142 int n = m_eivec.cols();
00143 MatrixType matD = MatrixType::Zero(n,n);
00144 for (int i=0; i<n; ++i)
00145 {
00146 if (ei_isMuchSmallerThan(ei_imag(m_eivalues.coeff(i)), ei_real(m_eivalues.coeff(i))))
00147 matD.coeffRef(i,i) = ei_real(m_eivalues.coeff(i));
00148 else
00149 {
00150 matD.template block<2,2>(i,i) << ei_real(m_eivalues.coeff(i)), ei_imag(m_eivalues.coeff(i)),
00151 -ei_imag(m_eivalues.coeff(i)), ei_real(m_eivalues.coeff(i));
00152 ++i;
00153 }
00154 }
00155 return matD;
00156 }
00157
00162 template<typename MatrixType>
00163 typename EigenSolver<MatrixType>::EigenvectorType EigenSolver<MatrixType>::eigenvectors(void) const
00164 {
00165 ei_assert(m_isInitialized && "EigenSolver is not initialized.");
00166 int n = m_eivec.cols();
00167 EigenvectorType matV(n,n);
00168 for (int j=0; j<n; ++j)
00169 {
00170 if (ei_isMuchSmallerThan(ei_abs(ei_imag(m_eivalues.coeff(j))), ei_abs(ei_real(m_eivalues.coeff(j)))))
00171 {
00172
00173 matV.col(j) = m_eivec.col(j).template cast<Complex>();
00174 }
00175 else
00176 {
00177
00178 for (int i=0; i<n; ++i)
00179 {
00180 matV.coeffRef(i,j) = Complex(m_eivec.coeff(i,j), m_eivec.coeff(i,j+1));
00181 matV.coeffRef(i,j+1) = Complex(m_eivec.coeff(i,j), -m_eivec.coeff(i,j+1));
00182 }
00183 matV.col(j).normalize();
00184 matV.col(j+1).normalize();
00185 ++j;
00186 }
00187 }
00188 return matV;
00189 }
00190
00191 template<typename MatrixType>
00192 void EigenSolver<MatrixType>::compute(const MatrixType& matrix)
00193 {
00194 assert(matrix.cols() == matrix.rows());
00195 int n = matrix.cols();
00196 m_eivalues.resize(n,1);
00197
00198 MatrixType matH = matrix;
00199 RealVectorType ort(n);
00200
00201
00202 orthes(matH, ort);
00203
00204
00205 hqr2(matH);
00206
00207 m_isInitialized = true;
00208 }
00209
00210
00211 template<typename MatrixType>
00212 void EigenSolver<MatrixType>::orthes(MatrixType& matH, RealVectorType& ort)
00213 {
00214
00215
00216
00217
00218
00219 int n = m_eivec.cols();
00220 int low = 0;
00221 int high = n-1;
00222
00223 for (int m = low+1; m <= high-1; ++m)
00224 {
00225
00226 RealScalar scale = matH.block(m, m-1, high-m+1, 1).cwise().abs().sum();
00227 if (scale != 0.0)
00228 {
00229
00230 RealScalar h = 0.0;
00231
00232 for (int i = high; i >= m; i--)
00233 {
00234 ort.coeffRef(i) = matH.coeff(i,m-1)/scale;
00235 h += ort.coeff(i) * ort.coeff(i);
00236 }
00237 RealScalar g = ei_sqrt(h);
00238 if (ort.coeff(m) > 0)
00239 g = -g;
00240 h = h - ort.coeff(m) * g;
00241 ort.coeffRef(m) = ort.coeff(m) - g;
00242
00243
00244
00245 int bSize = high-m+1;
00246 matH.block(m, m, bSize, n-m) -= ((ort.segment(m, bSize)/h)
00247 * (ort.segment(m, bSize).transpose() * matH.block(m, m, bSize, n-m)).lazy()).lazy();
00248
00249 matH.block(0, m, high+1, bSize) -= ((matH.block(0, m, high+1, bSize) * ort.segment(m, bSize)).lazy()
00250 * (ort.segment(m, bSize)/h).transpose()).lazy();
00251
00252 ort.coeffRef(m) = scale*ort.coeff(m);
00253 matH.coeffRef(m,m-1) = scale*g;
00254 }
00255 }
00256
00257
00258 m_eivec.setIdentity();
00259
00260 for (int m = high-1; m >= low+1; m--)
00261 {
00262 if (matH.coeff(m,m-1) != 0.0)
00263 {
00264 ort.segment(m+1, high-m) = matH.col(m-1).segment(m+1, high-m);
00265
00266 int bSize = high-m+1;
00267 m_eivec.block(m, m, bSize, bSize) += ( (ort.segment(m, bSize) / (matH.coeff(m,m-1) * ort.coeff(m) ) )
00268 * (ort.segment(m, bSize).transpose() * m_eivec.block(m, m, bSize, bSize)).lazy());
00269 }
00270 }
00271 }
00272
00273
00274 template<typename Scalar>
00275 std::complex<Scalar> cdiv(Scalar xr, Scalar xi, Scalar yr, Scalar yi)
00276 {
00277 Scalar r,d;
00278 if (ei_abs(yr) > ei_abs(yi))
00279 {
00280 r = yi/yr;
00281 d = yr + r*yi;
00282 return std::complex<Scalar>((xr + r*xi)/d, (xi - r*xr)/d);
00283 }
00284 else
00285 {
00286 r = yr/yi;
00287 d = yi + r*yr;
00288 return std::complex<Scalar>((r*xr + xi)/d, (r*xi - xr)/d);
00289 }
00290 }
00291
00292
00293
00294 template<typename MatrixType>
00295 void EigenSolver<MatrixType>::hqr2(MatrixType& matH)
00296 {
00297
00298
00299
00300
00301
00302
00303 int nn = m_eivec.cols();
00304 int n = nn-1;
00305 int low = 0;
00306 int high = nn-1;
00307 Scalar eps = ei_pow(Scalar(2),ei_is_same_type<Scalar,float>::ret ? Scalar(-23) : Scalar(-52));
00308 Scalar exshift = 0.0;
00309 Scalar p=0,q=0,r=0,s=0,z=0,t,w,x,y;
00310
00311
00312
00313
00314 Scalar norm = 0.0;
00315 for (int j = 0; j < nn; ++j)
00316 {
00317
00318 if ((j < low) || (j > high))
00319 {
00320 m_eivalues.coeffRef(j) = Complex(matH.coeff(j,j), 0.0);
00321 }
00322 norm += matH.row(j).segment(std::max(j-1,0), nn-std::max(j-1,0)).cwise().abs().sum();
00323 }
00324
00325
00326 int iter = 0;
00327 while (n >= low)
00328 {
00329
00330 int l = n;
00331 while (l > low)
00332 {
00333 s = ei_abs(matH.coeff(l-1,l-1)) + ei_abs(matH.coeff(l,l));
00334 if (s == 0.0)
00335 s = norm;
00336 if (ei_abs(matH.coeff(l,l-1)) < eps * s)
00337 break;
00338 l--;
00339 }
00340
00341
00342
00343 if (l == n)
00344 {
00345 matH.coeffRef(n,n) = matH.coeff(n,n) + exshift;
00346 m_eivalues.coeffRef(n) = Complex(matH.coeff(n,n), 0.0);
00347 n--;
00348 iter = 0;
00349 }
00350 else if (l == n-1)
00351 {
00352 w = matH.coeff(n,n-1) * matH.coeff(n-1,n);
00353 p = (matH.coeff(n-1,n-1) - matH.coeff(n,n)) * Scalar(0.5);
00354 q = p * p + w;
00355 z = ei_sqrt(ei_abs(q));
00356 matH.coeffRef(n,n) = matH.coeff(n,n) + exshift;
00357 matH.coeffRef(n-1,n-1) = matH.coeff(n-1,n-1) + exshift;
00358 x = matH.coeff(n,n);
00359
00360
00361 if (q >= 0)
00362 {
00363 if (p >= 0)
00364 z = p + z;
00365 else
00366 z = p - z;
00367
00368 m_eivalues.coeffRef(n-1) = Complex(x + z, 0.0);
00369 m_eivalues.coeffRef(n) = Complex(z!=0.0 ? x - w / z : m_eivalues.coeff(n-1).real(), 0.0);
00370
00371 x = matH.coeff(n,n-1);
00372 s = ei_abs(x) + ei_abs(z);
00373 p = x / s;
00374 q = z / s;
00375 r = ei_sqrt(p * p+q * q);
00376 p = p / r;
00377 q = q / r;
00378
00379
00380 for (int j = n-1; j < nn; ++j)
00381 {
00382 z = matH.coeff(n-1,j);
00383 matH.coeffRef(n-1,j) = q * z + p * matH.coeff(n,j);
00384 matH.coeffRef(n,j) = q * matH.coeff(n,j) - p * z;
00385 }
00386
00387
00388 for (int i = 0; i <= n; ++i)
00389 {
00390 z = matH.coeff(i,n-1);
00391 matH.coeffRef(i,n-1) = q * z + p * matH.coeff(i,n);
00392 matH.coeffRef(i,n) = q * matH.coeff(i,n) - p * z;
00393 }
00394
00395
00396 for (int i = low; i <= high; ++i)
00397 {
00398 z = m_eivec.coeff(i,n-1);
00399 m_eivec.coeffRef(i,n-1) = q * z + p * m_eivec.coeff(i,n);
00400 m_eivec.coeffRef(i,n) = q * m_eivec.coeff(i,n) - p * z;
00401 }
00402 }
00403 else
00404 {
00405 m_eivalues.coeffRef(n-1) = Complex(x + p, z);
00406 m_eivalues.coeffRef(n) = Complex(x + p, -z);
00407 }
00408 n = n - 2;
00409 iter = 0;
00410 }
00411 else
00412 {
00413
00414 x = matH.coeff(n,n);
00415 y = 0.0;
00416 w = 0.0;
00417 if (l < n)
00418 {
00419 y = matH.coeff(n-1,n-1);
00420 w = matH.coeff(n,n-1) * matH.coeff(n-1,n);
00421 }
00422
00423
00424 if (iter == 10)
00425 {
00426 exshift += x;
00427 for (int i = low; i <= n; ++i)
00428 matH.coeffRef(i,i) -= x;
00429 s = ei_abs(matH.coeff(n,n-1)) + ei_abs(matH.coeff(n-1,n-2));
00430 x = y = Scalar(0.75) * s;
00431 w = Scalar(-0.4375) * s * s;
00432 }
00433
00434
00435 if (iter == 30)
00436 {
00437 s = Scalar((y - x) / 2.0);
00438 s = s * s + w;
00439 if (s > 0)
00440 {
00441 s = ei_sqrt(s);
00442 if (y < x)
00443 s = -s;
00444 s = Scalar(x - w / ((y - x) / 2.0 + s));
00445 for (int i = low; i <= n; ++i)
00446 matH.coeffRef(i,i) -= s;
00447 exshift += s;
00448 x = y = w = Scalar(0.964);
00449 }
00450 }
00451
00452 iter = iter + 1;
00453
00454
00455 int m = n-2;
00456 while (m >= l)
00457 {
00458 z = matH.coeff(m,m);
00459 r = x - z;
00460 s = y - z;
00461 p = (r * s - w) / matH.coeff(m+1,m) + matH.coeff(m,m+1);
00462 q = matH.coeff(m+1,m+1) - z - r - s;
00463 r = matH.coeff(m+2,m+1);
00464 s = ei_abs(p) + ei_abs(q) + ei_abs(r);
00465 p = p / s;
00466 q = q / s;
00467 r = r / s;
00468 if (m == l) {
00469 break;
00470 }
00471 if (ei_abs(matH.coeff(m,m-1)) * (ei_abs(q) + ei_abs(r)) <
00472 eps * (ei_abs(p) * (ei_abs(matH.coeff(m-1,m-1)) + ei_abs(z) +
00473 ei_abs(matH.coeff(m+1,m+1)))))
00474 {
00475 break;
00476 }
00477 m--;
00478 }
00479
00480 for (int i = m+2; i <= n; ++i)
00481 {
00482 matH.coeffRef(i,i-2) = 0.0;
00483 if (i > m+2)
00484 matH.coeffRef(i,i-3) = 0.0;
00485 }
00486
00487
00488 for (int k = m; k <= n-1; ++k)
00489 {
00490 int notlast = (k != n-1);
00491 if (k != m) {
00492 p = matH.coeff(k,k-1);
00493 q = matH.coeff(k+1,k-1);
00494 r = notlast ? matH.coeff(k+2,k-1) : Scalar(0);
00495 x = ei_abs(p) + ei_abs(q) + ei_abs(r);
00496 if (x != 0.0)
00497 {
00498 p = p / x;
00499 q = q / x;
00500 r = r / x;
00501 }
00502 }
00503
00504 if (x == 0.0)
00505 break;
00506
00507 s = ei_sqrt(p * p + q * q + r * r);
00508
00509 if (p < 0)
00510 s = -s;
00511
00512 if (s != 0)
00513 {
00514 if (k != m)
00515 matH.coeffRef(k,k-1) = -s * x;
00516 else if (l != m)
00517 matH.coeffRef(k,k-1) = -matH.coeff(k,k-1);
00518
00519 p = p + s;
00520 x = p / s;
00521 y = q / s;
00522 z = r / s;
00523 q = q / p;
00524 r = r / p;
00525
00526
00527 for (int j = k; j < nn; ++j)
00528 {
00529 p = matH.coeff(k,j) + q * matH.coeff(k+1,j);
00530 if (notlast)
00531 {
00532 p = p + r * matH.coeff(k+2,j);
00533 matH.coeffRef(k+2,j) = matH.coeff(k+2,j) - p * z;
00534 }
00535 matH.coeffRef(k,j) = matH.coeff(k,j) - p * x;
00536 matH.coeffRef(k+1,j) = matH.coeff(k+1,j) - p * y;
00537 }
00538
00539
00540 for (int i = 0; i <= std::min(n,k+3); ++i)
00541 {
00542 p = x * matH.coeff(i,k) + y * matH.coeff(i,k+1);
00543 if (notlast)
00544 {
00545 p = p + z * matH.coeff(i,k+2);
00546 matH.coeffRef(i,k+2) = matH.coeff(i,k+2) - p * r;
00547 }
00548 matH.coeffRef(i,k) = matH.coeff(i,k) - p;
00549 matH.coeffRef(i,k+1) = matH.coeff(i,k+1) - p * q;
00550 }
00551
00552
00553 for (int i = low; i <= high; ++i)
00554 {
00555 p = x * m_eivec.coeff(i,k) + y * m_eivec.coeff(i,k+1);
00556 if (notlast)
00557 {
00558 p = p + z * m_eivec.coeff(i,k+2);
00559 m_eivec.coeffRef(i,k+2) = m_eivec.coeff(i,k+2) - p * r;
00560 }
00561 m_eivec.coeffRef(i,k) = m_eivec.coeff(i,k) - p;
00562 m_eivec.coeffRef(i,k+1) = m_eivec.coeff(i,k+1) - p * q;
00563 }
00564 }
00565 }
00566 }
00567 }
00568
00569
00570 if (norm == 0.0)
00571 {
00572 return;
00573 }
00574
00575 for (n = nn-1; n >= 0; n--)
00576 {
00577 p = m_eivalues.coeff(n).real();
00578 q = m_eivalues.coeff(n).imag();
00579
00580
00581 if (q == 0)
00582 {
00583 int l = n;
00584 matH.coeffRef(n,n) = 1.0;
00585 for (int i = n-1; i >= 0; i--)
00586 {
00587 w = matH.coeff(i,i) - p;
00588 r = (matH.row(i).segment(l,n-l+1) * matH.col(n).segment(l, n-l+1))(0,0);
00589
00590 if (m_eivalues.coeff(i).imag() < 0.0)
00591 {
00592 z = w;
00593 s = r;
00594 }
00595 else
00596 {
00597 l = i;
00598 if (m_eivalues.coeff(i).imag() == 0.0)
00599 {
00600 if (w != 0.0)
00601 matH.coeffRef(i,n) = -r / w;
00602 else
00603 matH.coeffRef(i,n) = -r / (eps * norm);
00604 }
00605 else
00606 {
00607 x = matH.coeff(i,i+1);
00608 y = matH.coeff(i+1,i);
00609 q = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) + m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag();
00610 t = (x * s - z * r) / q;
00611 matH.coeffRef(i,n) = t;
00612 if (ei_abs(x) > ei_abs(z))
00613 matH.coeffRef(i+1,n) = (-r - w * t) / x;
00614 else
00615 matH.coeffRef(i+1,n) = (-s - y * t) / z;
00616 }
00617
00618
00619 t = ei_abs(matH.coeff(i,n));
00620 if ((eps * t) * t > 1)
00621 matH.col(n).end(nn-i) /= t;
00622 }
00623 }
00624 }
00625 else if (q < 0)
00626 {
00627 std::complex<Scalar> cc;
00628 int l = n-1;
00629
00630
00631 if (ei_abs(matH.coeff(n,n-1)) > ei_abs(matH.coeff(n-1,n)))
00632 {
00633 matH.coeffRef(n-1,n-1) = q / matH.coeff(n,n-1);
00634 matH.coeffRef(n-1,n) = -(matH.coeff(n,n) - p) / matH.coeff(n,n-1);
00635 }
00636 else
00637 {
00638 cc = cdiv<Scalar>(0.0,-matH.coeff(n-1,n),matH.coeff(n-1,n-1)-p,q);
00639 matH.coeffRef(n-1,n-1) = ei_real(cc);
00640 matH.coeffRef(n-1,n) = ei_imag(cc);
00641 }
00642 matH.coeffRef(n,n-1) = 0.0;
00643 matH.coeffRef(n,n) = 1.0;
00644 for (int i = n-2; i >= 0; i--)
00645 {
00646 Scalar ra,sa,vr,vi;
00647 ra = (matH.block(i,l, 1, n-l+1) * matH.block(l,n-1, n-l+1, 1)).lazy()(0,0);
00648 sa = (matH.block(i,l, 1, n-l+1) * matH.block(l,n, n-l+1, 1)).lazy()(0,0);
00649 w = matH.coeff(i,i) - p;
00650
00651 if (m_eivalues.coeff(i).imag() < 0.0)
00652 {
00653 z = w;
00654 r = ra;
00655 s = sa;
00656 }
00657 else
00658 {
00659 l = i;
00660 if (m_eivalues.coeff(i).imag() == 0)
00661 {
00662 cc = cdiv(-ra,-sa,w,q);
00663 matH.coeffRef(i,n-1) = ei_real(cc);
00664 matH.coeffRef(i,n) = ei_imag(cc);
00665 }
00666 else
00667 {
00668
00669 x = matH.coeff(i,i+1);
00670 y = matH.coeff(i+1,i);
00671 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;
00672 vi = (m_eivalues.coeff(i).real() - p) * Scalar(2) * q;
00673 if ((vr == 0.0) && (vi == 0.0))
00674 vr = eps * norm * (ei_abs(w) + ei_abs(q) + ei_abs(x) + ei_abs(y) + ei_abs(z));
00675
00676 cc= cdiv(x*r-z*ra+q*sa,x*s-z*sa-q*ra,vr,vi);
00677 matH.coeffRef(i,n-1) = ei_real(cc);
00678 matH.coeffRef(i,n) = ei_imag(cc);
00679 if (ei_abs(x) > (ei_abs(z) + ei_abs(q)))
00680 {
00681 matH.coeffRef(i+1,n-1) = (-ra - w * matH.coeff(i,n-1) + q * matH.coeff(i,n)) / x;
00682 matH.coeffRef(i+1,n) = (-sa - w * matH.coeff(i,n) - q * matH.coeff(i,n-1)) / x;
00683 }
00684 else
00685 {
00686 cc = cdiv(-r-y*matH.coeff(i,n-1),-s-y*matH.coeff(i,n),z,q);
00687 matH.coeffRef(i+1,n-1) = ei_real(cc);
00688 matH.coeffRef(i+1,n) = ei_imag(cc);
00689 }
00690 }
00691
00692
00693 t = std::max(ei_abs(matH.coeff(i,n-1)),ei_abs(matH.coeff(i,n)));
00694 if ((eps * t) * t > 1)
00695 matH.block(i, n-1, nn-i, 2) /= t;
00696
00697 }
00698 }
00699 }
00700 }
00701
00702
00703 for (int i = 0; i < nn; ++i)
00704 {
00705
00706
00707 if (i < low || i > high)
00708 {
00709 m_eivec.row(i).end(nn-i) = matH.row(i).end(nn-i);
00710 }
00711 }
00712
00713
00714 int bRows = high-low+1;
00715 for (int j = nn-1; j >= low; j--)
00716 {
00717 int bSize = std::min(j,high)-low+1;
00718 m_eivec.col(j).segment(low, bRows) = (m_eivec.block(low, low, bRows, bSize) * matH.col(j).segment(low, bSize));
00719 }
00720 }
00721
00722 #endif // EIGEN_EIGENSOLVER_H