11#ifndef EIGEN_ARPACKSELFADJOINTEIGENSOLVER_H
12#define EIGEN_ARPACKSELFADJOINTEIGENSOLVER_H
14#include "../../../../Eigen/Dense"
17#include "./InternalHeaderCheck.h"
22template <
typename Scalar,
typename RealScalar>
24template <
typename MatrixSolver,
typename MatrixType,
typename Scalar,
bool BisSPD>
28template <
typename MatrixType,
typename MatrixSolver = SimplicialLLT<MatrixType>,
bool BisSPD = false>
29class ArpackGeneralizedSelfAdjointEigenSolver {
32 using Scalar =
typename MatrixType::Scalar;
33 using Index =
typename MatrixType::Index;
41 using RealScalar =
typename NumTraits<Scalar>::Real;
48 using RealVectorType =
typename internal::plain_col_type<MatrixType, RealScalar>::type;
56 ArpackGeneralizedSelfAdjointEigenSolver()
60 m_isInitialized(false),
61 m_eigenvectorsOk(false),
87 ArpackGeneralizedSelfAdjointEigenSolver(
const MatrixType &A,
const MatrixType &B, Index nbrEigenvalues,
93 m_isInitialized(false),
94 m_eigenvectorsOk(false),
97 compute(A, B, nbrEigenvalues, eigs_sigma, options, tol);
122 ArpackGeneralizedSelfAdjointEigenSolver(
const MatrixType &A, Index nbrEigenvalues, std::string eigs_sigma =
"LM",
127 m_isInitialized(false),
128 m_eigenvectorsOk(false),
131 compute(A, nbrEigenvalues, eigs_sigma, options, tol);
157 ArpackGeneralizedSelfAdjointEigenSolver &compute(
const MatrixType &A,
const MatrixType &B, Index nbrEigenvalues,
159 RealScalar tol = 0.0);
183 ArpackGeneralizedSelfAdjointEigenSolver &compute(
const MatrixType &A, Index nbrEigenvalues,
185 RealScalar tol = 0.0);
206 const Matrix<Scalar, Dynamic, Dynamic> &eigenvectors()
const {
207 eigen_assert(m_isInitialized &&
"ArpackGeneralizedSelfAdjointEigenSolver is not initialized.");
208 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
227 const Matrix<Scalar, Dynamic, 1> &eigenvalues()
const {
228 eigen_assert(m_isInitialized &&
"ArpackGeneralizedSelfAdjointEigenSolver is not initialized.");
250 Matrix<Scalar, Dynamic, Dynamic> operatorSqrt()
const {
251 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
252 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
253 return m_eivec * m_eivalues.cwiseSqrt().asDiagonal() * m_eivec.adjoint();
274 Matrix<Scalar, Dynamic, Dynamic> operatorInverseSqrt()
const {
275 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
276 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
277 return m_eivec * m_eivalues.cwiseInverse().cwiseSqrt().asDiagonal() * m_eivec.adjoint();
289 size_t getNbrConvergedEigenValues()
const {
return m_nbrConverged; }
291 size_t getNbrIterations()
const {
return m_nbrIterations; }
294 Matrix<Scalar, Dynamic, Dynamic> m_eivec;
295 Matrix<Scalar, Dynamic, 1> m_eivalues;
297 bool m_isInitialized;
298 bool m_eigenvectorsOk;
300 size_t m_nbrConverged;
301 size_t m_nbrIterations;
304template <
typename MatrixType,
typename MatrixSolver,
bool BisSPD>
305ArpackGeneralizedSelfAdjointEigenSolver<MatrixType, MatrixSolver, BisSPD> &
306ArpackGeneralizedSelfAdjointEigenSolver<MatrixType, MatrixSolver, BisSPD>::compute(
const MatrixType &A,
307 Index nbrEigenvalues,
308 std::string eigs_sigma,
int options,
311 compute(A, B, nbrEigenvalues, eigs_sigma, options, tol);
316template <
typename MatrixType,
typename MatrixSolver,
bool BisSPD>
317ArpackGeneralizedSelfAdjointEigenSolver<MatrixType, MatrixSolver, BisSPD> &
318ArpackGeneralizedSelfAdjointEigenSolver<MatrixType, MatrixSolver, BisSPD>::compute(
const MatrixType &A,
320 Index nbrEigenvalues,
321 std::string eigs_sigma,
int options,
323 eigen_assert(A.cols() == A.rows());
324 eigen_assert(B.cols() == B.rows());
325 eigen_assert(B.rows() == 0 || A.cols() == B.rows());
326 eigen_assert((options & ~(EigVecMask | GenEigMask)) == 0 && (options & EigVecMask) != EigVecMask &&
327 "invalid option parameter");
330 m_isInitialized =
false;
331 m_eigenvectorsOk =
false;
335 bool isBempty = (B.rows() == 0) || (B.cols() == 0);
343 int n = (int)A.cols();
351 RealScalar sigma = 0.0;
353 if (eigs_sigma.length() >= 2 && isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1])) {
354 eigs_sigma[0] = toupper(eigs_sigma[0]);
355 eigs_sigma[1] = toupper(eigs_sigma[1]);
361 if (eigs_sigma.substr(0, 2) !=
"SM") {
362 whch[0] = eigs_sigma[0];
363 whch[1] = eigs_sigma[1];
366 eigen_assert(
false &&
"Specifying clustered eigenvalues is not yet supported!");
371 sigma = atof(eigs_sigma.c_str());
380 if (eigs_sigma.substr(0, 2) ==
"SM" || !(isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1])) || (!isBempty && !BisSPD))
385 int mode = (bmat[0] ==
'G') + 1;
386 if (eigs_sigma.substr(0, 2) ==
"SM" || !(isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1]))) {
395 int nev = (int)nbrEigenvalues;
399 Scalar *resid =
new Scalar[n];
405 int ncv = std::min(std::max(2 * nev, 20), n);
409 Scalar *v =
new Scalar[n * ncv];
414 Scalar *workd =
new Scalar[3 * n];
415 int lworkl = ncv * ncv + 8 * ncv;
416 Scalar *workl =
new Scalar[lworkl];
418 int *iparam =
new int[11];
420 iparam[2] = std::max(300, numext::div_ceil(2 * n, std::max(ncv, 1)));
425 int *ipntr =
new int[11];
436 if (mode == 1 || mode == 2) {
437 if (!isBempty) OP.compute(B);
438 }
else if (mode == 3) {
445 MatrixType AminusSigmaB(A);
446 for (Index i = 0; i < A.rows(); ++i) AminusSigmaB.coeffRef(i, i) -= sigma;
448 OP.compute(AminusSigmaB);
450 MatrixType AminusSigmaB = A - sigma * B;
451 OP.compute(AminusSigmaB);
456 if (!(mode == 1 && isBempty) && !(mode == 2 && isBempty) && OP.info() !=
Success) {
464 m_isInitialized =
false;
469 internal::arpack_wrapper<Scalar, RealScalar>::saupd(&ido, bmat, &n, whch, &nev, &tol, resid, &ncv, v, &ldv, iparam,
470 ipntr, workd, workl, &lworkl, &info);
472 if (ido == -1 || ido == 1) {
473 Scalar *in = workd + ipntr[0] - 1;
474 Scalar *out = workd + ipntr[1] - 1;
476 if (ido == 1 && mode != 2) {
477 Scalar *out2 = workd + ipntr[2] - 1;
478 if (isBempty || mode == 1)
483 in = workd + ipntr[2] - 1;
494 internal::OP<MatrixSolver, MatrixType, Scalar, BisSPD>::applyOP(OP, A, n, in, out);
496 }
else if (mode == 2) {
502 }
else if (mode == 3) {
506 if (ido == 1 || isBempty)
511 }
else if (ido == 2) {
512 Scalar *in = workd + ipntr[0] - 1;
513 Scalar *out = workd + ipntr[1] - 1;
515 if (isBempty || mode == 1)
528 else if (info != 0) {
529 eigen_assert(
false &&
"Unknown ARPACK return value!");
537 char howmny[2] =
"A";
541 int *select =
new int[ncv];
545 m_eivalues.resize(nev, 1);
547 internal::arpack_wrapper<Scalar, RealScalar>::seupd(&rvec, howmny, select, m_eivalues.data(), v, &ldv, &sigma, bmat,
548 &n, whch, &nev, &tol, resid, &ncv, v, &ldv, iparam, ipntr,
549 workd, workl, &lworkl, &info);
557 m_eivec.resize(A.rows(), nev);
558 for (
int i = 0; i < nev; i++)
559 for (
int j = 0; j < n; j++) m_eivec(j, i) = v[i * n + j] / scale;
561 if (mode == 1 && !isBempty && BisSPD)
562 internal::OP<MatrixSolver, MatrixType, Scalar, BisSPD>::project(OP, n, nev, m_eivec.data());
564 m_eigenvectorsOk =
true;
567 m_nbrIterations = iparam[2];
568 m_nbrConverged = iparam[4];
583 m_isInitialized = (m_info ==
Success);
590extern "C" void ssaupd_(
int *ido,
char *bmat,
int *n,
char *which,
int *nev,
float *tol,
float *resid,
int *ncv,
591 float *v,
int *ldv,
int *iparam,
int *ipntr,
float *workd,
float *workl,
int *lworkl,
594extern "C" void sseupd_(
int *rvec,
char *All,
int *select,
float *d,
float *z,
int *ldz,
float *sigma,
char *bmat,
595 int *n,
char *which,
int *nev,
float *tol,
float *resid,
int *ncv,
float *v,
int *ldv,
596 int *iparam,
int *ipntr,
float *workd,
float *workl,
int *lworkl,
int *ierr);
600extern "C" void dsaupd_(
int *ido,
char *bmat,
int *n,
char *which,
int *nev,
double *tol,
double *resid,
int *ncv,
601 double *v,
int *ldv,
int *iparam,
int *ipntr,
double *workd,
double *workl,
int *lworkl,
604extern "C" void dseupd_(
int *rvec,
char *All,
int *select,
double *d,
double *z,
int *ldz,
double *sigma,
char *bmat,
605 int *n,
char *which,
int *nev,
double *tol,
double *resid,
int *ncv,
double *v,
int *ldv,
606 int *iparam,
int *ipntr,
double *workd,
double *workl,
int *lworkl,
int *ierr);
610template <
typename Scalar,
typename RealScalar>
611struct arpack_wrapper {
613 static inline void saupd(
int *,
char *,
int *,
char *,
int *, RealScalar *, Scalar *,
int *, Scalar *,
int *,
int *,
614 int *, Scalar *, Scalar *,
int *,
int *) {
615 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
618 static inline void seupd(
int *,
char *,
int *, Scalar *, Scalar *,
int *, RealScalar *,
char *,
int *,
char *,
int *,
619 RealScalar *, Scalar *,
int *, Scalar *,
int *,
int *,
int *, Scalar *, Scalar *,
int *,
621 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
626struct arpack_wrapper<float, float> {
627 static inline void saupd(
int *ido,
char *bmat,
int *n,
char *which,
int *nev,
float *tol,
float *resid,
int *ncv,
628 float *v,
int *ldv,
int *iparam,
int *ipntr,
float *workd,
float *workl,
int *lworkl,
630 ssaupd_(ido, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd, workl, lworkl, info);
633 static inline void seupd(
int *rvec,
char *All,
int *select,
float *d,
float *z,
int *ldz,
float *sigma,
char *bmat,
634 int *n,
char *which,
int *nev,
float *tol,
float *resid,
int *ncv,
float *v,
int *ldv,
635 int *iparam,
int *ipntr,
float *workd,
float *workl,
int *lworkl,
int *ierr) {
636 sseupd_(rvec, All, select, d, z, ldz, sigma, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd,
637 workl, lworkl, ierr);
642struct arpack_wrapper<double, double> {
643 static inline void saupd(
int *ido,
char *bmat,
int *n,
char *which,
int *nev,
double *tol,
double *resid,
int *ncv,
644 double *v,
int *ldv,
int *iparam,
int *ipntr,
double *workd,
double *workl,
int *lworkl,
646 dsaupd_(ido, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd, workl, lworkl, info);
649 static inline void seupd(
int *rvec,
char *All,
int *select,
double *d,
double *z,
int *ldz,
double *sigma,
char *bmat,
650 int *n,
char *which,
int *nev,
double *tol,
double *resid,
int *ncv,
double *v,
int *ldv,
651 int *iparam,
int *ipntr,
double *workd,
double *workl,
int *lworkl,
int *ierr) {
652 dseupd_(rvec, All, select, d, z, ldz, sigma, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd,
653 workl, lworkl, ierr);
657template <
typename MatrixSolver,
typename MatrixType,
typename Scalar,
bool BisSPD>
659 static inline void applyOP(MatrixSolver &OP,
const MatrixType &A,
int n, Scalar *in, Scalar *out);
660 static inline void project(MatrixSolver &OP,
int n,
int k, Scalar *vecs);
663template <
typename MatrixSolver,
typename MatrixType,
typename Scalar>
664struct OP<MatrixSolver, MatrixType, Scalar, true> {
665 static inline void applyOP(MatrixSolver &OP,
const MatrixType &A,
int n, Scalar *in, Scalar *out) {
670 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.matrixU().solve(Matrix<Scalar, Dynamic, 1>::Map(in, n));
671 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.permutationPinv() * Matrix<Scalar, Dynamic, 1>::Map(out, n);
675 Matrix<Scalar, Dynamic, 1>::Map(out, n) = A * Matrix<Scalar, Dynamic, 1>::Map(out, n);
679 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.permutationP() * Matrix<Scalar, Dynamic, 1>::Map(out, n);
680 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.matrixL().solve(Matrix<Scalar, Dynamic, 1>::Map(out, n));
683 static inline void project(MatrixSolver &OP,
int n,
int k, Scalar *vecs) {
686 Matrix<Scalar, Dynamic, Dynamic>::Map(vecs, n, k) =
687 OP.matrixU().solve(Matrix<Scalar, Dynamic, Dynamic>::Map(vecs, n, k));
688 Matrix<Scalar, Dynamic, Dynamic>::Map(vecs, n, k) =
689 OP.permutationPinv() * Matrix<Scalar, Dynamic, Dynamic>::Map(vecs, n, k);
693template <
typename MatrixSolver,
typename MatrixType,
typename Scalar>
694struct OP<MatrixSolver, MatrixType, Scalar, false> {
695 static inline void applyOP(MatrixSolver &,
const MatrixType &,
int, Scalar *, Scalar *) {
696 eigen_assert(
false &&
"Should never be in here...");
699 static inline void project(MatrixSolver &,
int,
int, Scalar *) {
700 eigen_assert(
false &&
"Should never be in here...");
Namespace containing all symbols from the Eigen library.