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 real symmetric matrices.
24 template<typename eT, int SelectionRule, typename OpType>
25 class SymEigsSolver
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<eT>           ritz_val;  // ritz values
32 
33   // Sort the first nev Ritz pairs in ascending algebraic 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<eT>           ritz_vec;  // ritz vectors
48   Col<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          eps23;     // eps^(2/3), used in convergence test
53                                // tol*eps23 is the absolute tolerance
54   const eT          near0;     // a very small value, but 1/near0 does not overflow
55 
56   // Arnoldi factorisation starting from step-k
57   inline void factorise_from(uword from_k, uword to_m, const Col<eT>& fk);
58 
59   // Implicitly restarted Arnoldi factorisation
60   inline void restart(uword k);
61 
62   // Calculate the number of converged Ritz values
63   inline uword num_converged(eT tol);
64 
65   // Return the adjusted nev for restarting
66   inline uword nev_adjusted(uword nconv);
67 
68   // Retrieve and sort ritz values and ritz vectors
69   inline void retrieve_ritzpair();
70 
71 
72   public:
73 
74   //! Constructor to create a solver object.
75   inline SymEigsSolver(const OpType& op_, uword nev_, uword ncv_);
76 
77   //! Providing the initial residual vector for the algorithm.
78   inline void init(eT* init_resid);
79 
80   //! Providing a random initial residual vector.
81   inline void init();
82 
83   //! Conducting the major computation procedure.
84   inline uword compute(uword maxit = 1000, eT tol = 1e-10);
85 
86   //! Returning the number of iterations used in the computation.
num_iterations()87   inline uword num_iterations() { return niter; }
88 
89   //! Returning the number of matrix operations used in the computation.
num_operations()90   inline uword num_operations() { return nmatop; }
91 
92   //! Returning the converged eigenvalues.
93   inline Col<eT> eigenvalues();
94 
95   //! Returning the eigenvectors associated with the converged eigenvalues.
96   inline Mat<eT> eigenvectors(uword nvec);
97   //! Returning all converged eigenvectors.
eigenvectors()98   inline Mat<eT> eigenvectors() { return eigenvectors(nev); }
99   };
100 
101 
102 }  // namespace newarp
103