Eigen-unsupported  5.0.1-dev+284dcc12
 
Loading...
Searching...
No Matches
ArpackSelfAdjointEigenSolver.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2012 David Harmon <dharmon@gmail.com>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_ARPACKGENERALIZEDSELFADJOINTEIGENSOLVER_H
11#define EIGEN_ARPACKGENERALIZEDSELFADJOINTEIGENSOLVER_H
12
13#include "../../../../Eigen/Dense"
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21template <typename Scalar, typename RealScalar>
22struct arpack_wrapper;
23template <typename MatrixSolver, typename MatrixType, typename Scalar, bool BisSPD>
24struct OP;
25} // namespace internal
26
27template <typename MatrixType, typename MatrixSolver = SimplicialLLT<MatrixType>, bool BisSPD = false>
28class ArpackGeneralizedSelfAdjointEigenSolver {
29 public:
30 // typedef typename MatrixSolver::MatrixType MatrixType;
31
33 typedef typename MatrixType::Scalar Scalar;
34 typedef typename MatrixType::Index Index;
35
42 typedef typename NumTraits<Scalar>::Real RealScalar;
43
49 typedef typename internal::plain_col_type<MatrixType, RealScalar>::type RealVectorType;
50
57 ArpackGeneralizedSelfAdjointEigenSolver()
58 : m_eivec(),
59 m_eivalues(),
60 m_isInitialized(false),
61 m_eigenvectorsOk(false),
62 m_nbrConverged(0),
63 m_nbrIterations(0) {}
64
87 ArpackGeneralizedSelfAdjointEigenSolver(const MatrixType &A, const MatrixType &B, Index nbrEigenvalues,
88 std::string eigs_sigma = "LM", int options = ComputeEigenvectors,
89 RealScalar tol = 0.0)
90 : m_eivec(),
91 m_eivalues(),
92 m_isInitialized(false),
93 m_eigenvectorsOk(false),
94 m_nbrConverged(0),
95 m_nbrIterations(0) {
96 compute(A, B, nbrEigenvalues, eigs_sigma, options, tol);
97 }
98
120
121 ArpackGeneralizedSelfAdjointEigenSolver(const MatrixType &A, Index nbrEigenvalues, std::string eigs_sigma = "LM",
122 int options = ComputeEigenvectors, RealScalar tol = 0.0)
123 : m_eivec(),
124 m_eivalues(),
125 m_isInitialized(false),
126 m_eigenvectorsOk(false),
127 m_nbrConverged(0),
128 m_nbrIterations(0) {
129 compute(A, nbrEigenvalues, eigs_sigma, options, tol);
130 }
131
155 ArpackGeneralizedSelfAdjointEigenSolver &compute(const MatrixType &A, const MatrixType &B, Index nbrEigenvalues,
156 std::string eigs_sigma = "LM", int options = ComputeEigenvectors,
157 RealScalar tol = 0.0);
158
181 ArpackGeneralizedSelfAdjointEigenSolver &compute(const MatrixType &A, Index nbrEigenvalues,
182 std::string eigs_sigma = "LM", int options = ComputeEigenvectors,
183 RealScalar tol = 0.0);
184
204 const Matrix<Scalar, Dynamic, Dynamic> &eigenvectors() const {
205 eigen_assert(m_isInitialized && "ArpackGeneralizedSelfAdjointEigenSolver is not initialized.");
206 eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
207 return m_eivec;
208 }
209
225 const Matrix<Scalar, Dynamic, 1> &eigenvalues() const {
226 eigen_assert(m_isInitialized && "ArpackGeneralizedSelfAdjointEigenSolver is not initialized.");
227 return m_eivalues;
228 }
229
248 Matrix<Scalar, Dynamic, Dynamic> operatorSqrt() const {
249 eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
250 eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
251 return m_eivec * m_eivalues.cwiseSqrt().asDiagonal() * m_eivec.adjoint();
252 }
253
272 Matrix<Scalar, Dynamic, Dynamic> operatorInverseSqrt() const {
273 eigen_assert(m_isInitialized && "SelfAdjointEigenSolver is not initialized.");
274 eigen_assert(m_eigenvectorsOk && "The eigenvectors have not been computed together with the eigenvalues.");
275 return m_eivec * m_eivalues.cwiseInverse().cwiseSqrt().asDiagonal() * m_eivec.adjoint();
276 }
277
282 ComputationInfo info() const {
283 eigen_assert(m_isInitialized && "ArpackGeneralizedSelfAdjointEigenSolver is not initialized.");
284 return m_info;
285 }
286
287 size_t getNbrConvergedEigenValues() const { return m_nbrConverged; }
288
289 size_t getNbrIterations() const { return m_nbrIterations; }
290
291 protected:
292 Matrix<Scalar, Dynamic, Dynamic> m_eivec;
293 Matrix<Scalar, Dynamic, 1> m_eivalues;
294 ComputationInfo m_info;
295 bool m_isInitialized;
296 bool m_eigenvectorsOk;
297
298 size_t m_nbrConverged;
299 size_t m_nbrIterations;
300};
301
302template <typename MatrixType, typename MatrixSolver, bool BisSPD>
303ArpackGeneralizedSelfAdjointEigenSolver<MatrixType, MatrixSolver, BisSPD> &
304ArpackGeneralizedSelfAdjointEigenSolver<MatrixType, MatrixSolver, BisSPD>::compute(const MatrixType &A,
305 Index nbrEigenvalues,
306 std::string eigs_sigma, int options,
307 RealScalar tol) {
308 MatrixType B(0, 0);
309 compute(A, B, nbrEigenvalues, eigs_sigma, options, tol);
310
311 return *this;
312}
313
314template <typename MatrixType, typename MatrixSolver, bool BisSPD>
315ArpackGeneralizedSelfAdjointEigenSolver<MatrixType, MatrixSolver, BisSPD> &
316ArpackGeneralizedSelfAdjointEigenSolver<MatrixType, MatrixSolver, BisSPD>::compute(const MatrixType &A,
317 const MatrixType &B,
318 Index nbrEigenvalues,
319 std::string eigs_sigma, int options,
320 RealScalar tol) {
321 eigen_assert(A.cols() == A.rows());
322 eigen_assert(B.cols() == B.rows());
323 eigen_assert(B.rows() == 0 || A.cols() == B.rows());
324 eigen_assert((options & ~(EigVecMask | GenEigMask)) == 0 && (options & EigVecMask) != EigVecMask &&
325 "invalid option parameter");
326
327 bool isBempty = (B.rows() == 0) || (B.cols() == 0);
328
329 // For clarity, all parameters match their ARPACK name
330 //
331 // Always 0 on the first call
332 //
333 int ido = 0;
334
335 int n = (int)A.cols();
336
337 // User options: "LA", "SA", "SM", "LM", "BE"
338 //
339 char whch[3] = "LM";
340
341 // Specifies the shift if iparam[6] = { 3, 4, 5 }, not used if iparam[6] = { 1, 2 }
342 //
343 RealScalar sigma = 0.0;
344
345 if (eigs_sigma.length() >= 2 && isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1])) {
346 eigs_sigma[0] = toupper(eigs_sigma[0]);
347 eigs_sigma[1] = toupper(eigs_sigma[1]);
348
349 // In the following special case we're going to invert the problem, since solving
350 // for larger magnitude is much much faster
351 // i.e., if 'SM' is specified, we're going to really use 'LM', the default
352 //
353 if (eigs_sigma.substr(0, 2) != "SM") {
354 whch[0] = eigs_sigma[0];
355 whch[1] = eigs_sigma[1];
356 }
357 } else {
358 eigen_assert(false && "Specifying clustered eigenvalues is not yet supported!");
359
360 // If it's not scalar values, then the user may be explicitly
361 // specifying the sigma value to cluster the evs around
362 //
363 sigma = atof(eigs_sigma.c_str());
364
365 // If atof fails, it returns 0.0, which is a fine default
366 //
367 }
368
369 // "I" means normal eigenvalue problem, "G" means generalized
370 //
371 char bmat[2] = "I";
372 if (eigs_sigma.substr(0, 2) == "SM" || !(isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1])) || (!isBempty && !BisSPD))
373 bmat[0] = 'G';
374
375 // Now we determine the mode to use
376 //
377 int mode = (bmat[0] == 'G') + 1;
378 if (eigs_sigma.substr(0, 2) == "SM" || !(isalpha(eigs_sigma[0]) && isalpha(eigs_sigma[1]))) {
379 // We're going to use shift-and-invert mode, and basically find
380 // the largest eigenvalues of the inverse operator
381 //
382 mode = 3;
383 }
384
385 // The user-specified number of eigenvalues/vectors to compute
386 //
387 int nev = (int)nbrEigenvalues;
388
389 // Allocate space for ARPACK to store the residual
390 //
391 Scalar *resid = new Scalar[n];
392
393 // Number of Lanczos vectors, must satisfy nev < ncv <= n
394 // Note that this indicates that nev != n, and we cannot compute
395 // all eigenvalues of a mtrix
396 //
397 int ncv = std::min(std::max(2 * nev, 20), n);
398
399 // The working n x ncv matrix, also store the final eigenvectors (if computed)
400 //
401 Scalar *v = new Scalar[n * ncv];
402 int ldv = n;
403
404 // Working space
405 //
406 Scalar *workd = new Scalar[3 * n];
407 int lworkl = ncv * ncv + 8 * ncv; // Must be at least this length
408 Scalar *workl = new Scalar[lworkl];
409
410 int *iparam = new int[11];
411 iparam[0] = 1; // 1 means we let ARPACK perform the shifts, 0 means we'd have to do it
412 iparam[2] = std::max(300, (int)std::ceil(2 * n / std::max(ncv, 1)));
413 iparam[6] = mode; // The mode, 1 is standard ev problem, 2 for generalized ev, 3 for shift-and-invert
414
415 // Used during reverse communicate to notify where arrays start
416 //
417 int *ipntr = new int[11];
418
419 // Error codes are returned in here, initial value of 0 indicates a random initial
420 // residual vector is used, any other values means resid contains the initial residual
421 // vector, possibly from a previous run
422 //
423 int info = 0;
424
425 Scalar scale = 1.0;
426 // if (!isBempty)
427 //{
428 // Scalar scale = B.norm() / std::sqrt(n);
429 // scale = std::pow(2, std::floor(std::log(scale+1)));
431 // for (size_t i=0; i<(size_t)B.outerSize(); i++)
432 // for (typename MatrixType::InnerIterator it(B, i); it; ++it)
433 // it.valueRef() /= scale;
434 // }
435
436 MatrixSolver OP;
437 if (mode == 1 || mode == 2) {
438 if (!isBempty) OP.compute(B);
439 } else if (mode == 3) {
440 if (sigma == 0.0) {
441 OP.compute(A);
442 } else {
443 // Note: We will never enter here because sigma must be 0.0
444 //
445 if (isBempty) {
446 MatrixType AminusSigmaB(A);
447 for (Index i = 0; i < A.rows(); ++i) AminusSigmaB.coeffRef(i, i) -= sigma;
448
449 OP.compute(AminusSigmaB);
450 } else {
451 MatrixType AminusSigmaB = A - sigma * B;
452 OP.compute(AminusSigmaB);
453 }
454 }
455 }
456
457 if (!(mode == 1 && isBempty) && !(mode == 2 && isBempty) && OP.info() != Success) {
458 m_info = OP.info() delete[] v;
459 delete[] iparam;
460 delete[] ipntr;
461 delete[] workd;
462 delete[] workl;
463 delete[] resid;
464 m_isInitialized = false;
465 return *this;
466 }
467
468 do {
469 internal::arpack_wrapper<Scalar, RealScalar>::saupd(&ido, bmat, &n, whch, &nev, &tol, resid, &ncv, v, &ldv, iparam,
470 ipntr, workd, workl, &lworkl, &info);
471
472 if (ido == -1 || ido == 1) {
473 Scalar *in = workd + ipntr[0] - 1;
474 Scalar *out = workd + ipntr[1] - 1;
475
476 if (ido == 1 && mode != 2) {
477 Scalar *out2 = workd + ipntr[2] - 1;
478 if (isBempty || mode == 1)
480 else
482
483 in = workd + ipntr[2] - 1;
484 }
485
486 if (mode == 1) {
487 if (isBempty) {
488 // OP = A
489 //
491 } else {
492 // OP = L^{-1}AL^{-T}
493 //
494 internal::OP<MatrixSolver, MatrixType, Scalar, BisSPD>::applyOP(OP, A, n, in, out);
495 }
496 } else if (mode == 2) {
498
499 // OP = B^{-1} A
500 //
502 } else if (mode == 3) {
503 // OP = (A-\sigmaB)B (\sigma could be 0, and B could be I)
504 // The B * in is already computed and stored at in if ido == 1
505 //
506 if (ido == 1 || isBempty)
508 else
510 }
511 } else if (ido == 2) {
512 Scalar *in = workd + ipntr[0] - 1;
513 Scalar *out = workd + ipntr[1] - 1;
514
515 if (isBempty || mode == 1)
517 else
519 }
520 } while (ido != 99);
521
522 if (info == 1)
523 m_info = NoConvergence;
524 else if (info == 3)
525 m_info = NumericalIssue;
526 else if (info < 0)
527 m_info = InvalidInput;
528 else if (info != 0)
529 eigen_assert(false && "Unknown ARPACK return value!");
530 else {
531 // Do we compute eigenvectors or not?
532 //
533 int rvec = (options & ComputeEigenvectors) == ComputeEigenvectors;
534
535 // "A" means "All", use "S" to choose specific eigenvalues (not yet supported in ARPACK))
536 //
537 char howmny[2] = "A";
538
539 // if howmny == "S", specifies the eigenvalues to compute (not implemented in ARPACK)
540 //
541 int *select = new int[ncv];
542
543 // Final eigenvalues
544 //
545 m_eivalues.resize(nev, 1);
546
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);
550
551 if (info == -14)
552 m_info = NoConvergence;
553 else if (info != 0)
554 m_info = InvalidInput;
555 else {
556 if (rvec) {
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;
560
561 if (mode == 1 && !isBempty && BisSPD)
562 internal::OP<MatrixSolver, MatrixType, Scalar, BisSPD>::project(OP, n, nev, m_eivec.data());
563
564 m_eigenvectorsOk = true;
565 }
566
567 m_nbrIterations = iparam[2];
568 m_nbrConverged = iparam[4];
569
570 m_info = Success;
571 }
572
573 delete[] select;
574 }
575
576 delete[] v;
577 delete[] iparam;
578 delete[] ipntr;
579 delete[] workd;
580 delete[] workl;
581 delete[] resid;
582
583 m_isInitialized = (m_info == Success);
584
585 return *this;
586}
587
588// Single precision
589//
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,
592 int *info);
593
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);
597
598// Double precision
599//
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,
602 int *info);
603
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);
607
608namespace internal {
609
610template <typename Scalar, typename RealScalar>
611struct arpack_wrapper {
612 static inline void saupd(int *ido, char *bmat, int *n, char *which, int *nev, RealScalar *tol, Scalar *resid,
613 int *ncv, Scalar *v, int *ldv, int *iparam, int *ipntr, Scalar *workd, Scalar *workl,
614 int *lworkl, int *info) {
615 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
616 }
617
618 static inline void seupd(int *rvec, char *All, int *select, Scalar *d, Scalar *z, int *ldz, RealScalar *sigma,
619 char *bmat, int *n, char *which, int *nev, RealScalar *tol, Scalar *resid, int *ncv,
620 Scalar *v, int *ldv, int *iparam, int *ipntr, Scalar *workd, Scalar *workl, int *lworkl,
621 int *ierr) {
622 EIGEN_STATIC_ASSERT(!NumTraits<Scalar>::IsComplex, NUMERIC_TYPE_MUST_BE_REAL)
623 }
624};
625
626template <>
627struct arpack_wrapper<float, float> {
628 static inline void saupd(int *ido, char *bmat, int *n, char *which, int *nev, float *tol, float *resid, int *ncv,
629 float *v, int *ldv, int *iparam, int *ipntr, float *workd, float *workl, int *lworkl,
630 int *info) {
631 ssaupd_(ido, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd, workl, lworkl, info);
632 }
633
634 static inline void seupd(int *rvec, char *All, int *select, float *d, float *z, int *ldz, float *sigma, char *bmat,
635 int *n, char *which, int *nev, float *tol, float *resid, int *ncv, float *v, int *ldv,
636 int *iparam, int *ipntr, float *workd, float *workl, int *lworkl, int *ierr) {
637 sseupd_(rvec, All, select, d, z, ldz, sigma, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd,
638 workl, lworkl, ierr);
639 }
640};
641
642template <>
643struct arpack_wrapper<double, double> {
644 static inline void saupd(int *ido, char *bmat, int *n, char *which, int *nev, double *tol, double *resid, int *ncv,
645 double *v, int *ldv, int *iparam, int *ipntr, double *workd, double *workl, int *lworkl,
646 int *info) {
647 dsaupd_(ido, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd, workl, lworkl, info);
648 }
649
650 static inline void seupd(int *rvec, char *All, int *select, double *d, double *z, int *ldz, double *sigma, char *bmat,
651 int *n, char *which, int *nev, double *tol, double *resid, int *ncv, double *v, int *ldv,
652 int *iparam, int *ipntr, double *workd, double *workl, int *lworkl, int *ierr) {
653 dseupd_(rvec, All, select, d, v, ldv, sigma, bmat, n, which, nev, tol, resid, ncv, v, ldv, iparam, ipntr, workd,
654 workl, lworkl, ierr);
655 }
656};
657
658template <typename MatrixSolver, typename MatrixType, typename Scalar, bool BisSPD>
659struct OP {
660 static inline void applyOP(MatrixSolver &OP, const MatrixType &A, int n, Scalar *in, Scalar *out);
661 static inline void project(MatrixSolver &OP, int n, int k, Scalar *vecs);
662};
663
664template <typename MatrixSolver, typename MatrixType, typename Scalar>
665struct OP<MatrixSolver, MatrixType, Scalar, true> {
666 static inline void applyOP(MatrixSolver &OP, const MatrixType &A, int n, Scalar *in, Scalar *out) {
667 // OP = L^{-1} A L^{-T} (B = LL^T)
668 //
669 // First solve L^T out = in
670 //
671 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.matrixU().solve(Matrix<Scalar, Dynamic, 1>::Map(in, n));
672 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.permutationPinv() * Matrix<Scalar, Dynamic, 1>::Map(out, n);
673
674 // Then compute out = A out
675 //
676 Matrix<Scalar, Dynamic, 1>::Map(out, n) = A * Matrix<Scalar, Dynamic, 1>::Map(out, n);
677
678 // Then solve L out = out
679 //
680 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.permutationP() * Matrix<Scalar, Dynamic, 1>::Map(out, n);
681 Matrix<Scalar, Dynamic, 1>::Map(out, n) = OP.matrixL().solve(Matrix<Scalar, Dynamic, 1>::Map(out, n));
682 }
683
684 static inline void project(MatrixSolver &OP, int n, int k, Scalar *vecs) {
685 // Solve L^T out = in
686 //
687 Matrix<Scalar, Dynamic, Dynamic>::Map(vecs, n, k) =
688 OP.matrixU().solve(Matrix<Scalar, Dynamic, Dynamic>::Map(vecs, n, k));
689 Matrix<Scalar, Dynamic, Dynamic>::Map(vecs, n, k) =
690 OP.permutationPinv() * Matrix<Scalar, Dynamic, Dynamic>::Map(vecs, n, k);
691 }
692};
693
694template <typename MatrixSolver, typename MatrixType, typename Scalar>
695struct OP<MatrixSolver, MatrixType, Scalar, false> {
696 static inline void applyOP(MatrixSolver &OP, const MatrixType &A, int n, Scalar *in, Scalar *out) {
697 eigen_assert(false && "Should never be in here...");
698 }
699
700 static inline void project(MatrixSolver &OP, int n, int k, Scalar *vecs) {
701 eigen_assert(false && "Should never be in here...");
702 }
703};
704
705} // end namespace internal
706
707} // end namespace Eigen
708
709#endif // EIGEN_ARPACKSELFADJOINTEIGENSOLVER_H
ComputationInfo
NumericalIssue
ComputeEigenvectors
Namespace containing all symbols from the Eigen library.
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index