25 #ifndef EIGEN_ARPACKGENERALIZEDSELFADJOINTEIGENSOLVER_H 26 #define EIGEN_ARPACKGENERALIZEDSELFADJOINTEIGENSOLVER_H 28 #include <Eigen/Dense> 34 template<
typename MatrixSolver,
typename MatrixType,
typename Scalar,
bool BisSPD>
struct OP;
39 template<
typename MatrixType,
typename MatrixSolver=SimplicialLLT<MatrixType>,
bool BisSPD=false>
46 typedef typename MatrixType::Scalar
Scalar;
73 m_isInitialized(false),
74 m_eigenvectorsOk(false),
102 Index nbrEigenvalues, std::string eigs_sigma=
"LM",
106 m_isInitialized(false),
107 m_eigenvectorsOk(false),
111 compute(A, B, nbrEigenvalues, eigs_sigma,
options, tol);
137 Index nbrEigenvalues, std::string eigs_sigma=
"LM",
141 m_isInitialized(false),
142 m_eigenvectorsOk(false),
146 compute(A, nbrEigenvalues, eigs_sigma,
options, tol);
174 Index nbrEigenvalues, std::string eigs_sigma=
"LM",
200 Index nbrEigenvalues, std::string eigs_sigma=
"LM",
225 eigen_assert(m_isInitialized &&
"ArpackGeneralizedSelfAdjointEigenSolver is not initialized.");
226 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
247 eigen_assert(m_isInitialized &&
"ArpackGeneralizedSelfAdjointEigenSolver is not initialized.");
271 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
272 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
273 return m_eivec * m_eivalues.cwiseSqrt().asDiagonal() * m_eivec.adjoint();
296 eigen_assert(m_isInitialized &&
"SelfAdjointEigenSolver is not initialized.");
297 eigen_assert(m_eigenvectorsOk &&
"The eigenvectors have not been computed together with the eigenvalues.");
298 return m_eivec * m_eivalues.cwiseInverse().cwiseSqrt().asDiagonal() * m_eivec.adjoint();
307 eigen_assert(m_isInitialized &&
"ArpackGeneralizedSelfAdjointEigenSolver is not initialized.");
312 {
return m_nbrConverged; }
315 {
return m_nbrIterations; }
332 template<
typename MatrixType,
typename MatrixSolver,
bool BisSPD>
336 std::string eigs_sigma,
int options,
RealScalar tol)
339 compute(A, B, nbrEigenvalues, eigs_sigma, options, tol);
345 template<
typename MatrixType,
typename MatrixSolver,
bool BisSPD>
349 std::string eigs_sigma,
int options,
RealScalar tol)
356 &&
"invalid option parameter");
358 bool isBempty = (B.rows() == 0) || (B.cols() == 0);
366 int n = (int)A.cols();
376 if (eigs_sigma.length() >= 2 && isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1]))
378 eigs_sigma[0] = toupper(eigs_sigma[0]);
379 eigs_sigma[1] = toupper(eigs_sigma[1]);
385 if (eigs_sigma.substr(0,2) !=
"SM")
387 whch[0] = eigs_sigma[0];
388 whch[1] = eigs_sigma[1];
393 eigen_assert(
false &&
"Specifying clustered eigenvalues is not yet supported!");
398 sigma = atof(eigs_sigma.c_str());
407 if (eigs_sigma.substr(0,2) ==
"SM" || !(isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1])) || (!isBempty && !BisSPD))
412 int mode = (bmat[0] ==
'G') + 1;
413 if (eigs_sigma.substr(0,2) ==
"SM" || !(isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1])))
423 int nev = (int)nbrEigenvalues;
433 int ncv = std::min(
std::max(2*nev, 20), n);
443 int lworkl = ncv*ncv+8*ncv;
446 int *iparam=
new int[11];
453 int *ipntr =
new int[11];
473 if (mode == 1 || mode == 2)
490 MatrixType AminusSigmaB(A);
491 for (
Index i=0; i<A.rows(); ++i)
492 AminusSigmaB.coeffRef(i,i) -= sigma;
494 OP.compute(AminusSigmaB);
498 MatrixType AminusSigmaB = A - sigma * B;
499 OP.compute(AminusSigmaB);
504 if (!(mode == 1 && isBempty) && !(mode == 2 && isBempty) && OP.info() !=
Success)
505 std::cout <<
"Error factoring matrix" << std::endl;
510 &ncv, v, &ldv, iparam, ipntr, workd, workl,
513 if (ido == -1 || ido == 1)
515 Scalar *in = workd + ipntr[0] - 1;
516 Scalar *out = workd + ipntr[1] - 1;
518 if (ido == 1 && mode != 2)
520 Scalar *out2 = workd + ipntr[2] - 1;
521 if (isBempty || mode == 1)
526 in = workd + ipntr[2] - 1;
558 if (ido == 1 || isBempty)
566 Scalar *in = workd + ipntr[0] - 1;
567 Scalar *out = workd + ipntr[1] - 1;
569 if (isBempty || mode == 1)
592 char howmny[2] =
"A";
596 int *select =
new int[ncv];
600 m_eivalues.resize(nev, 1);
603 &sigma, bmat, &n, whch, &nev, &tol, resid, &ncv,
604 v, &ldv, iparam, ipntr, workd, workl, &lworkl, &info);
614 m_eivec.resize(A.rows(), nev);
615 for (
int i=0; i<nev; i++)
616 for (
int j=0; j<n; j++)
617 m_eivec(j,i) = v[i*n+j] / scale;
619 if (mode == 1 && !isBempty && BisSPD)
622 m_eigenvectorsOk =
true;
625 m_nbrIterations = iparam[2];
626 m_nbrConverged = iparam[4];
641 m_isInitialized =
true;
649 extern "C" void ssaupd_(
int *ido,
char *bmat,
int *n,
char *which,
650 int *nev,
float *tol,
float *resid,
int *ncv,
651 float *v,
int *ldv,
int *iparam,
int *ipntr,
652 float *workd,
float *workl,
int *lworkl,
655 extern "C" void sseupd_(
int *rvec,
char *All,
int *select,
float *
d,
656 float *
z,
int *ldz,
float *sigma,
657 char *bmat,
int *n,
char *which,
int *nev,
658 float *tol,
float *resid,
int *ncv,
float *v,
659 int *ldv,
int *iparam,
int *ipntr,
float *workd,
660 float *workl,
int *lworkl,
int *ierr);
664 extern "C" void dsaupd_(
int *ido,
char *bmat,
int *n,
char *which,
665 int *nev,
double *tol,
double *resid,
int *ncv,
666 double *v,
int *ldv,
int *iparam,
int *ipntr,
667 double *workd,
double *workl,
int *lworkl,
670 extern "C" void dseupd_(
int *rvec,
char *All,
int *select,
double *d,
671 double *z,
int *ldz,
double *sigma,
672 char *bmat,
int *n,
char *which,
int *nev,
673 double *tol,
double *resid,
int *ncv,
double *v,
674 int *ldv,
int *iparam,
int *ipntr,
double *workd,
675 double *workl,
int *lworkl,
int *ierr);
680 template<
typename Scalar,
typename RealScalar>
struct arpack_wrapper
682 static inline void saupd(
int *ido,
char *bmat,
int *n,
char *which,
683 int *nev, RealScalar *tol, Scalar *resid,
int *ncv,
684 Scalar *v,
int *ldv,
int *iparam,
int *ipntr,
685 Scalar *workd, Scalar *workl,
int *lworkl,
int *info)
690 static inline void seupd(
int *rvec,
char *All,
int *select, Scalar *d,
691 Scalar *z,
int *ldz, RealScalar *sigma,
692 char *bmat,
int *n,
char *which,
int *nev,
693 RealScalar *tol, Scalar *resid,
int *ncv, Scalar *v,
694 int *ldv,
int *iparam,
int *ipntr, Scalar *workd,
695 Scalar *workl,
int *lworkl,
int *ierr)
703 static inline void saupd(
int *ido,
char *bmat,
int *n,
char *which,
704 int *nev,
float *tol,
float *resid,
int *ncv,
705 float *v,
int *ldv,
int *iparam,
int *ipntr,
706 float *workd,
float *workl,
int *lworkl,
int *info)
708 ssaupd_(ido, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd, workl, lworkl, info);
711 static inline void seupd(
int *rvec,
char *All,
int *select,
float *d,
712 float *z,
int *ldz,
float *sigma,
713 char *bmat,
int *n,
char *which,
int *nev,
714 float *tol,
float *resid,
int *ncv,
float *v,
715 int *ldv,
int *iparam,
int *ipntr,
float *workd,
716 float *workl,
int *lworkl,
int *ierr)
718 sseupd_(rvec, All, select, d, z, ldz, sigma, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr,
719 workd, workl, lworkl, ierr);
725 static inline void saupd(
int *ido,
char *bmat,
int *n,
char *which,
726 int *nev,
double *tol,
double *resid,
int *ncv,
727 double *v,
int *ldv,
int *iparam,
int *ipntr,
728 double *workd,
double *workl,
int *lworkl,
int *info)
730 dsaupd_(ido, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd, workl, lworkl, info);
733 static inline void seupd(
int *rvec,
char *All,
int *select,
double *d,
734 double *z,
int *ldz,
double *sigma,
735 char *bmat,
int *n,
char *which,
int *nev,
736 double *tol,
double *resid,
int *ncv,
double *v,
737 int *ldv,
int *iparam,
int *ipntr,
double *workd,
738 double *workl,
int *lworkl,
int *ierr)
740 dseupd_(rvec, All, select, d, v, ldv, sigma, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr,
741 workd, workl, lworkl, ierr);
746 template<
typename MatrixSolver,
typename MatrixType,
typename Scalar,
bool BisSPD>
749 static inline void applyOP(MatrixSolver &
OP,
const MatrixType &
A,
int n, Scalar *in, Scalar *out);
750 static inline void project(MatrixSolver &OP,
int n,
int k, Scalar *vecs);
753 template<
typename MatrixSolver,
typename MatrixType,
typename Scalar>
754 struct OP<MatrixSolver, MatrixType, Scalar, true>
756 static inline void applyOP(MatrixSolver &
OP,
const MatrixType &
A,
int n, Scalar *in, Scalar *out)
775 static inline void project(MatrixSolver &
OP,
int n,
int k, Scalar *vecs)
785 template<
typename MatrixSolver,
typename MatrixType,
typename Scalar>
786 struct OP<MatrixSolver, MatrixType, Scalar, false>
788 static inline void applyOP(MatrixSolver &
OP,
const MatrixType &
A,
int n, Scalar *in, Scalar *out)
793 static inline void project(MatrixSolver &
OP,
int n,
int k, Scalar *vecs)
804 #endif // EIGEN_ARPACKSELFADJOINTEIGENSOLVER_H void sseupd_(int *rvec, char *All, int *select, float *d, float *z, int *ldz, float *sigma, char *bmat, int *n, char *which, int *nev, float *tol, float *resid, int *ncv, float *v, int *ldv, int *iparam, int *ipntr, float *workd, float *workl, int *lworkl, int *ierr)
size_t getNbrConvergedEigenValues() const
static void saupd(int *ido, char *bmat, int *n, char *which, int *nev, double *tol, double *resid, int *ncv, double *v, int *ldv, int *iparam, int *ipntr, double *workd, double *workl, int *lworkl, int *info)
Matrix< Scalar, Dynamic, Dynamic > operatorInverseSqrt() const
Computes the inverse square root of the matrix.
Matrix< Scalar, Dynamic, Dynamic > m_eivec
ArpackGeneralizedSelfAdjointEigenSolver & compute(const MatrixType &A, const MatrixType &B, Index nbrEigenvalues, std::string eigs_sigma="LM", int options=ComputeEigenvectors, RealScalar tol=0.0)
Computes generalized eigenvalues / eigenvectors of given matrix using the external ARPACK library...
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
ArpackGeneralizedSelfAdjointEigenSolver(const MatrixType &A, Index nbrEigenvalues, std::string eigs_sigma="LM", int options=ComputeEigenvectors, RealScalar tol=0.0)
Constructor; computes eigenvalues of given matrix.
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
ComputationInfo info() const
Reports whether previous computation was successful.
static void applyOP(MatrixSolver &OP, const MatrixType &A, int n, Scalar *in, Scalar *out)
static void seupd(int *rvec, char *All, int *select, Scalar *d, Scalar *z, int *ldz, RealScalar *sigma, char *bmat, int *n, char *which, int *nev, RealScalar *tol, Scalar *resid, int *ncv, Scalar *v, int *ldv, int *iparam, int *ipntr, Scalar *workd, Scalar *workl, int *lworkl, int *ierr)
static void seupd(int *rvec, char *All, int *select, double *d, double *z, int *ldz, double *sigma, char *bmat, int *n, char *which, int *nev, double *tol, double *resid, int *ncv, double *v, int *ldv, int *iparam, int *ipntr, double *workd, double *workl, int *lworkl, int *ierr)
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half() max(const half &a, const half &b)
static void project(MatrixSolver &OP, int n, int k, Scalar *vecs)
static void seupd(int *rvec, char *All, int *select, float *d, float *z, int *ldz, float *sigma, char *bmat, int *n, char *which, int *nev, float *tol, float *resid, int *ncv, float *v, int *ldv, int *iparam, int *ipntr, float *workd, float *workl, int *lworkl, int *ierr)
EIGEN_DEVICE_FUNC const CeilReturnType ceil() const
const Matrix< Scalar, Dynamic, Dynamic > & eigenvectors() const
Returns the eigenvectors of given matrix.
size_t getNbrIterations() const
static void saupd(int *ido, char *bmat, int *n, char *which, int *nev, RealScalar *tol, Scalar *resid, int *ncv, Scalar *v, int *ldv, int *iparam, int *ipntr, Scalar *workd, Scalar *workl, int *lworkl, int *info)
ArpackGeneralizedSelfAdjointEigenSolver()
Default constructor.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
NumTraits< Scalar >::Real RealScalar
Real scalar type for MatrixType.
static void applyOP(MatrixSolver &OP, const MatrixType &A, int n, Scalar *in, Scalar *out)
const Matrix< Scalar, Dynamic, 1 > & eigenvalues() const
Returns the eigenvalues of given matrix.
internal::plain_col_type< MatrixType, RealScalar >::type RealVectorType
Type for vector of eigenvalues as returned by eigenvalues().
TFSIMD_FORCE_INLINE const tfScalar & z() const
Matrix< Scalar, Dynamic, 1 > m_eivalues
static void applyOP(MatrixSolver &OP, const MatrixType &A, int n, Scalar *in, Scalar *out)
static void project(MatrixSolver &OP, int n, int k, Scalar *vecs)
void dsaupd_(int *ido, char *bmat, int *n, char *which, int *nev, double *tol, double *resid, int *ncv, double *v, int *ldv, int *iparam, int *ipntr, double *workd, double *workl, int *lworkl, int *info)
static void saupd(int *ido, char *bmat, int *n, char *which, int *nev, float *tol, float *resid, int *ncv, float *v, int *ldv, int *iparam, int *ipntr, float *workd, float *workl, int *lworkl, int *info)
ArpackGeneralizedSelfAdjointEigenSolver(const MatrixType &A, const MatrixType &B, Index nbrEigenvalues, std::string eigs_sigma="LM", int options=ComputeEigenvectors, RealScalar tol=0.0)
Constructor; computes generalized eigenvalues of given matrix with respect to another matrix...
Matrix< Scalar, Dynamic, Dynamic > operatorSqrt() const
Computes the positive-definite square root of the matrix.
static ConstMapType Map(const Scalar *data)
void dseupd_(int *rvec, char *All, int *select, double *d, double *z, int *ldz, double *sigma, char *bmat, int *n, char *which, int *nev, double *tol, double *resid, int *ncv, double *v, int *ldv, int *iparam, int *ipntr, double *workd, double *workl, int *lworkl, int *ierr)
void ssaupd_(int *ido, char *bmat, int *n, char *which, int *nev, float *tol, float *resid, int *ncv, float *v, int *ldv, int *iparam, int *ipntr, float *workd, float *workl, int *lworkl, int *info)
static void project(MatrixSolver &OP, int n, int k, Scalar *vecs)