newarp_TridiagEigen_meat.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. template<typename eT>
  18. inline
  19. TridiagEigen<eT>::TridiagEigen()
  20. : n(0)
  21. , computed(false)
  22. {
  23. arma_extra_debug_sigprint();
  24. }
  25. template<typename eT>
  26. inline
  27. TridiagEigen<eT>::TridiagEigen(const Mat<eT>& mat_obj)
  28. : n(mat_obj.n_rows)
  29. , computed(false)
  30. {
  31. arma_extra_debug_sigprint();
  32. compute(mat_obj);
  33. }
  34. template<typename eT>
  35. inline
  36. void
  37. TridiagEigen<eT>::compute(const Mat<eT>& mat_obj)
  38. {
  39. arma_extra_debug_sigprint();
  40. arma_debug_check( (mat_obj.is_square() == false), "newarp::TridiagEigen::compute(): matrix must be square" );
  41. n = blas_int(mat_obj.n_rows);
  42. main_diag = mat_obj.diag();
  43. sub_diag = mat_obj.diag(-1);
  44. evecs.set_size(n, n);
  45. char compz = 'I';
  46. blas_int lwork = blas_int(-1);
  47. eT lwork_opt = eT(0);
  48. blas_int liwork = blas_int(-1);
  49. blas_int liwork_opt = blas_int(0);
  50. blas_int info = blas_int(0);
  51. // query for lwork and liwork
  52. lapack::stedc(&compz, &n, main_diag.memptr(), sub_diag.memptr(), evecs.memptr(), &n, &lwork_opt, &lwork, &liwork_opt, &liwork, &info);
  53. if(info == 0)
  54. {
  55. lwork = blas_int(lwork_opt);
  56. liwork = liwork_opt;
  57. }
  58. else
  59. {
  60. lwork = 1 + 4 * n + n * n;
  61. liwork = 3 + 5 * n;
  62. }
  63. info = blas_int(0);
  64. podarray<eT> work(static_cast<uword>(lwork) );
  65. podarray<blas_int> iwork(static_cast<uword>(liwork));
  66. lapack::stedc(&compz, &n, main_diag.memptr(), sub_diag.memptr(), evecs.memptr(), &n, work.memptr(), &lwork, iwork.memptr(), &liwork, &info);
  67. if(info < 0) { arma_stop_logic_error("lapack::stedc(): illegal value"); return; }
  68. if(info > 0) { arma_stop_runtime_error("lapack::stedc(): failed to compute all eigenvalues"); return; }
  69. computed = true;
  70. }
  71. template<typename eT>
  72. inline
  73. Col<eT>
  74. TridiagEigen<eT>::eigenvalues()
  75. {
  76. arma_extra_debug_sigprint();
  77. arma_debug_check( (computed == false), "newarp::TridiagEigen::eigenvalues(): need to call compute() first" );
  78. // After calling compute(), main_diag will contain the eigenvalues.
  79. return main_diag;
  80. }
  81. template<typename eT>
  82. inline
  83. Mat<eT>
  84. TridiagEigen<eT>::eigenvectors()
  85. {
  86. arma_extra_debug_sigprint();
  87. arma_debug_check( (computed == false), "newarp::TridiagEigen::eigenvectors(): need to call compute() first" );
  88. return evecs;
  89. }
  90. } // namespace newarp