1 // SPDX-License-Identifier: Apache-2.0
2 //
3 // Copyright 2008-2016 Conrad Sanderson (http://conradsanderson.id.au)
4 // Copyright 2008-2016 National ICT Australia (NICTA)
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 // ------------------------------------------------------------------------
17 
18 
19 namespace newarp
20 {
21 
22 
23 //! This class implements the eigen solver for general real matrices.
24 template<typename eT, int SelectionRule, typename OpType>
25 class GenEigsSolver
26   {
27   protected:
28 
29   const OpType&           op;        // object to conduct matrix operation, eg. matrix-vector product
30   const uword             nev;       // number of eigenvalues requested
31   Col< std::complex<eT> > ritz_val;  // ritz values
32 
33   // Sort the first nev Ritz pairs in decreasing magnitude order
34   // This is used to return the final results
35   virtual void sort_ritzpair();
36 
37 
38   private:
39 
40   const uword             dim_n;     // dimension of matrix A
41   const uword             ncv;       // number of ritz values
42   uword                   nmatop;    // number of matrix operations called
43   uword                   niter;     // number of restarting iterations
44   Mat<eT>                 fac_V;     // V matrix in the Arnoldi factorisation
45   Mat<eT>                 fac_H;     // H matrix in the Arnoldi factorisation
46   Col<eT>                 fac_f;     // residual in the Arnoldi factorisation
47   Mat< std::complex<eT> > ritz_vec;  // ritz vectors
48   Col< std::complex<eT> > ritz_est;  // last row of ritz_vec
49   std::vector<bool>       ritz_conv; // indicator of the convergence of ritz values
50   const eT                eps;       // the machine precision
51                                      // eg. ~= 1e-16 for double type
52   const eT                approx0;   // a number that is approximately zero
53                                      // approx0 = eps^(2/3)
54                                      // used to test the orthogonality of vectors,
55                                      // and in convergence test, tol*approx0 is
56                                      // the absolute tolerance
57 
58   // Arnoldi factorisation starting from step-k
59   inline void factorise_from(uword from_k, uword to_m, const Col<eT>& fk);
60 
61   // Implicitly restarted Arnoldi factorisation
62   inline void restart(uword k);
63 
64   // Calculate the number of converged Ritz values
65   inline uword num_converged(eT tol);
66 
67   // Return the adjusted nev for restarting
68   inline uword nev_adjusted(uword nconv);
69 
70   // Retrieve and sort ritz values and ritz vectors
71   inline void retrieve_ritzpair();
72 
73 
74   public:
75 
76   //! Constructor to create a solver object.
77   inline GenEigsSolver(const OpType& op_, uword nev_, uword ncv_);
78 
79   //! Providing the initial residual vector for the algorithm.
80   inline void init(eT* init_resid);
81 
82   //! Providing a random initial residual vector.
83   inline void init();
84 
85   //! Conducting the major computation procedure.
86   inline uword compute(uword maxit = 1000, eT tol = 1e-10);
87 
88   //! Returning the number of iterations used in the computation.
num_iterations()89   inline int num_iterations() { return niter; }
90 
91   //! Returning the number of matrix operations used in the computation.
num_operations()92   inline int num_operations() { return nmatop; }
93 
94   //! Returning the converged eigenvalues.
95   inline Col< std::complex<eT> > eigenvalues();
96 
97   //! Returning the eigenvectors associated with the converged eigenvalues.
98   inline Mat< std::complex<eT> > eigenvectors(uword nvec);
99 
100   //! Returning all converged eigenvectors.
eigenvectors()101   inline Mat< std::complex<eT> > eigenvectors() { return eigenvectors(nev); }
102   };
103 
104 
105 }  // namespace newarp
106