11 #ifndef EIGEN_EIGENSOLVER_H
12 #define EIGEN_EIGENSOLVER_H
146 template<
typename InputType>
277 template<
typename InputType>
323 template<
typename MatrixType>
326 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
328 Index n = m_eivalues.rows();
344 template<
typename MatrixType>
347 eigen_assert(m_isInitialized &&
"EigenSolver is not initialized.");
348 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
357 matV.col(j) = m_eivec.col(j).template cast<ComplexScalar>();
358 matV.col(j).normalize();
368 matV.col(j).normalize();
369 matV.col(j+1).normalize();
376 template<
typename MatrixType>
377 template<
typename InputType>
381 check_template_parameters();
389 m_realSchur.compute(
matrix.derived(), computeEigenvectors);
391 m_info = m_realSchur.info();
395 m_matT = m_realSchur.matrixT();
396 if (computeEigenvectors)
397 m_eivec = m_realSchur.matrixU();
400 m_eivalues.resize(
matrix.cols());
404 if (i ==
matrix.cols() - 1 || m_matT.coeff(i+1, i) ==
Scalar(0))
406 m_eivalues.coeffRef(i) = m_matT.coeff(i, i);
407 if(!(
isfinite)(m_eivalues.coeffRef(i)))
409 m_isInitialized =
true;
410 m_eigenvectorsOk =
false;
418 Scalar p =
Scalar(0.5) * (m_matT.coeff(i, i) - m_matT.coeff(i+1, i+1));
423 Scalar t0 = m_matT.coeff(i+1, i);
424 Scalar t1 = m_matT.coeff(i, i+1);
425 Scalar maxval = numext::maxi<Scalar>(
abs(p),numext::maxi<Scalar>(
abs(t0),
abs(t1)));
429 z = maxval *
sqrt(
abs(p0 * p0 + t0 * t1));
432 m_eivalues.coeffRef(i) =
ComplexScalar(m_matT.coeff(i+1, i+1) + p, z);
433 m_eivalues.coeffRef(i+1) =
ComplexScalar(m_matT.coeff(i+1, i+1) + p, -z);
434 if(!((
isfinite)(m_eivalues.coeffRef(i)) && (
isfinite)(m_eivalues.coeffRef(i+1))))
436 m_isInitialized =
true;
437 m_eigenvectorsOk =
false;
446 if (computeEigenvectors)
447 doComputeEigenvectors();
450 m_isInitialized =
true;
451 m_eigenvectorsOk = computeEigenvectors;
457 template<
typename MatrixType>
479 Scalar p = m_eivalues.coeff(
n).real();
480 Scalar q = m_eivalues.coeff(
n).imag();
485 Scalar lastr(0), lastw(0);
489 for (
Index i =
n-1; i >= 0; i--)
491 Scalar w = m_matT.coeff(i,i) - p;
492 Scalar r = m_matT.row(i).segment(l,
n-l+1).dot(m_matT.col(
n).segment(l,
n-l+1));
494 if (m_eivalues.coeff(i).imag() <
Scalar(0))
502 if (m_eivalues.coeff(i).imag() ==
Scalar(0))
505 m_matT.coeffRef(i,
n) = -r / w;
507 m_matT.coeffRef(i,
n) = -r / (eps * norm);
511 Scalar x = m_matT.coeff(i,i+1);
512 Scalar y = m_matT.coeff(i+1,i);
513 Scalar denom = (m_eivalues.coeff(i).real() - p) * (m_eivalues.coeff(i).real() - p) + m_eivalues.coeff(i).imag() * m_eivalues.coeff(i).imag();
514 Scalar t = (
x * lastr - lastw * r) / denom;
515 m_matT.coeffRef(i,
n) = t;
517 m_matT.coeffRef(i+1,
n) = (-r - w * t) /
x;
519 m_matT.coeffRef(i+1,
n) = (-lastr -
y * t) / lastw;
524 if ((eps * t) * t >
Scalar(1))
525 m_matT.col(
n).tail(
size-i) /= t;
531 Scalar lastra(0), lastsa(0), lastw(0);
535 if (
abs(m_matT.coeff(
n,
n-1)) >
abs(m_matT.coeff(
n-1,
n)))
537 m_matT.coeffRef(
n-1,
n-1) =
q / m_matT.coeff(
n,
n-1);
538 m_matT.coeffRef(
n-1,
n) = -(m_matT.coeff(
n,
n) - p) / m_matT.coeff(
n,
n-1);
548 for (
Index i =
n-2; i >= 0; i--)
550 Scalar ra = m_matT.row(i).segment(l,
n-l+1).dot(m_matT.col(
n-1).segment(l,
n-l+1));
551 Scalar sa = m_matT.row(i).segment(l,
n-l+1).dot(m_matT.col(
n).segment(l,
n-l+1));
552 Scalar w = m_matT.coeff(i,i) - p;
554 if (m_eivalues.coeff(i).imag() <
Scalar(0))
563 if (m_eivalues.coeff(i).imag() ==
RealScalar(0))
572 Scalar x = m_matT.coeff(i,i+1);
573 Scalar y = m_matT.coeff(i+1,i);
574 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;
575 Scalar vi = (m_eivalues.coeff(i).real() - p) *
Scalar(2) *
q;
584 m_matT.coeffRef(i+1,
n-1) = (-ra - w * m_matT.coeff(i,
n-1) +
q * m_matT.coeff(i,
n)) /
x;
585 m_matT.coeffRef(i+1,
n) = (-sa - w * m_matT.coeff(i,
n) -
q * m_matT.coeff(i,
n-1)) /
x;
596 Scalar t = numext::maxi<Scalar>(
abs(m_matT.coeff(i,
n-1)),
abs(m_matT.coeff(i,
n)));
597 if ((eps * t) * t >
Scalar(1))
598 m_matT.block(i,
n-1,
size-i, 2) /= t;
608 eigen_assert(0 &&
"Internal bug in EigenSolver (INF or NaN has not been detected)");
615 m_tmp.noalias() = m_eivec.leftCols(j+1) * m_matT.col(j).segment(0, j+1);
616 m_eivec.col(j) = m_tmp;
622 #endif // EIGEN_EIGENSOLVER_H