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;
47 typedef typename MatrixType::Index
Index;
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.");
311 size_t getNbrConvergedEigenValues()
const 312 {
return m_nbrConverged; }
314 size_t getNbrIterations()
const 315 {
return m_nbrIterations; }
321 bool m_isInitialized;
322 bool m_eigenvectorsOk;
324 size_t m_nbrConverged;
325 size_t 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>
348 ::compute(
const MatrixType&
A,
const MatrixType&
B, Index nbrEigenvalues,
349 std::string eigs_sigma,
int options,
RealScalar tol)
351 eigen_assert(A.cols() == A.rows());
352 eigen_assert(B.cols() == B.rows());
353 eigen_assert(B.rows() == 0 || A.cols() == B.rows());
354 eigen_assert((options &~ (EigVecMask | GenEigMask)) == 0
355 && (options & EigVecMask) != EigVecMask
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];
448 iparam[2] = std::max(300, (
int)std::ceil(2*n/std::max(ncv,1)));
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;
551 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.solve(Matrix<Scalar, Dynamic, 1>::Map(in, n));
558 if (ido == 1 || isBempty)
566 Scalar *in = workd + ipntr[0] - 1;
567 Scalar *out = workd + ipntr[1] - 1;
569 if (isBempty || mode == 1)
583 eigen_assert(
false &&
"Unknown ARPACK return value!");
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,
684 Scalar *v,
int *ldv,
int *iparam,
int *ipntr,
690 static inline void seupd(
int *rvec,
char *All,
int *select,
Scalar *d,
692 char *bmat,
int *n,
char *which,
int *nev,
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)
771 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.permutationP() * Matrix<Scalar, Dynamic, 1>::Map(out, n);
772 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.matrixL().solve(Matrix<Scalar, Dynamic, 1>::Map(out, n));
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)
790 eigen_assert(
false &&
"Should never be in here...");
793 static inline void project(MatrixSolver &OP,
int n,
int k, Scalar *vecs)
795 eigen_assert(
false &&
"Should never be in here...");
804 #endif // EIGEN_ARPACKSELFADJOINTEIGENSOLVER_H Definition: ArpackSelfAdjointEigenSolver.h:40
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...
Definition: ArpackSelfAdjointEigenSolver.h:348
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:85
MatrixType::Scalar Scalar
Scalar type for matrices of type MatrixType.
Definition: ArpackSelfAdjointEigenSolver.h:46
ArpackGeneralizedSelfAdjointEigenSolver(const MatrixType &A, Index nbrEigenvalues, std::string eigs_sigma="LM", int options=ComputeEigenvectors, RealScalar tol=0.0)
Constructor; computes eigenvalues of given matrix.
Definition: ArpackSelfAdjointEigenSolver.h:136
Holds information about the various numeric (i.e.
Definition: NumTraits.h:150
Definition: ArpackSelfAdjointEigenSolver.h:33
Matrix< Scalar, Dynamic, Dynamic > operatorSqrt() const
Computes the positive-definite square root of the matrix.
Definition: ArpackSelfAdjointEigenSolver.h:269
Matrix< Scalar, Dynamic, Dynamic > operatorInverseSqrt() const
Computes the inverse square root of the matrix.
Definition: ArpackSelfAdjointEigenSolver.h:294
const Matrix< Scalar, Dynamic, 1 > & eigenvalues() const
Returns the eigenvalues of given matrix.
Definition: ArpackSelfAdjointEigenSolver.h:245
const Matrix< Scalar, Dynamic, Dynamic > & eigenvectors() const
Returns the eigenvectors of given matrix.
Definition: ArpackSelfAdjointEigenSolver.h:223
Used in SelfAdjointEigenSolver and GeneralizedSelfAdjointEigenSolver to specify that both the eigenva...
Definition: Constants.h:395
ArpackGeneralizedSelfAdjointEigenSolver()
Default constructor.
Definition: ArpackSelfAdjointEigenSolver.h:70
The provided data did not satisfy the prerequisites.
Definition: Constants.h:434
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
NumTraits< Scalar >::Real RealScalar
Real scalar type for MatrixType.
Definition: ArpackSelfAdjointEigenSolver.h:55
internal::plain_col_type< MatrixType, RealScalar >::type RealVectorType
Type for vector of eigenvalues as returned by eigenvalues().
Definition: ArpackSelfAdjointEigenSolver.h:62
The inputs are invalid, or the algorithm has been improperly called.
Definition: Constants.h:439
Computation was successful.
Definition: Constants.h:432
Definition: BandTriangularSolver.h:13
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...
Definition: ArpackSelfAdjointEigenSolver.h:101
ComputationInfo info() const
Reports whether previous computation was successful.
Definition: ArpackSelfAdjointEigenSolver.h:305
Definition: ArpackSelfAdjointEigenSolver.h:34
Generic expression where a coefficient-wise unary operator is applied to an expression.
Definition: CwiseUnaryOp.h:55
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:178
ComputationInfo
Enum for reporting the status of a computation.
Definition: Constants.h:430
Iterative procedure did not converge.
Definition: Constants.h:436