newarp_SymEigsSolver_bones.hpp 3.8 KB

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