newarp_UpperHessenbergEigen_meat.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. UpperHessenbergEigen<eT>::UpperHessenbergEigen()
  20. : n(0)
  21. , computed(false)
  22. {
  23. arma_extra_debug_sigprint();
  24. }
  25. template<typename eT>
  26. inline
  27. UpperHessenbergEigen<eT>::UpperHessenbergEigen(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. UpperHessenbergEigen<eT>::compute(const Mat<eT>& mat_obj)
  38. {
  39. arma_extra_debug_sigprint();
  40. arma_debug_check( (mat_obj.is_square() == false), "newarp::UpperHessenbergEigen::compute(): matrix must be square" );
  41. n = blas_int(mat_obj.n_rows);
  42. mat_Z.set_size(n, n);
  43. mat_T.set_size(n, n);
  44. evals.set_size(n);
  45. mat_Z.eye();
  46. mat_T = mat_obj;
  47. blas_int want_T = blas_int(1);
  48. blas_int want_Z = blas_int(1);
  49. blas_int ilo = blas_int(1);
  50. blas_int ihi = blas_int(n);
  51. blas_int iloz = blas_int(1);
  52. blas_int ihiz = blas_int(n);
  53. blas_int info = blas_int(0);
  54. podarray<eT> wr(static_cast<uword>(n));
  55. podarray<eT> wi(static_cast<uword>(n));
  56. lapack::lahqr(&want_T, &want_Z, &n, &ilo, &ihi, mat_T.memptr(), &n, wr.memptr(), wi.memptr(), &iloz, &ihiz, mat_Z.memptr(), &n, &info);
  57. for(blas_int i = 0; i < n; i++)
  58. {
  59. evals(i) = std::complex<eT>(wr[i], wi[i]);
  60. }
  61. if(info > 0) { arma_stop_runtime_error("lapack::lahqr(): failed to compute all eigenvalues"); return; }
  62. char side = 'R';
  63. char howmny = 'B';
  64. blas_int m = blas_int(0);
  65. podarray<eT> work(static_cast<uword>(3 * n));
  66. lapack::trevc(&side, &howmny, (blas_int*) NULL, &n, mat_T.memptr(), &n, (eT*) NULL, &n, mat_Z.memptr(), &n, &n, &m, work.memptr(), &info);
  67. if(info < 0) { arma_stop_logic_error("lapack::trevc(): illegal value"); return; }
  68. computed = true;
  69. }
  70. template<typename eT>
  71. inline
  72. Col< std::complex<eT> >
  73. UpperHessenbergEigen<eT>::eigenvalues()
  74. {
  75. arma_extra_debug_sigprint();
  76. arma_debug_check( (computed == false), "newarp::UpperHessenbergEigen::eigenvalues(): need to call compute() first" );
  77. return evals;
  78. }
  79. template<typename eT>
  80. inline
  81. Mat< std::complex<eT> >
  82. UpperHessenbergEigen<eT>::eigenvectors()
  83. {
  84. arma_extra_debug_sigprint();
  85. arma_debug_check( (computed == false), "newarp::UpperHessenbergEigen::eigenvectors(): need to call compute() first" );
  86. // Lapack will set the imaginary parts of real eigenvalues to be exact zero
  87. Mat< std::complex<eT> > evecs(n, n);
  88. std::complex<eT>* col_ptr = evecs.memptr();
  89. for(blas_int i = 0; i < n; i++)
  90. {
  91. if(cx_attrib::is_real(evals(i), eT(0)))
  92. {
  93. // for real eigenvector, normalise and copy
  94. eT z_norm = norm(mat_Z.col(i));
  95. for(blas_int j = 0; j < n; j++)
  96. {
  97. col_ptr[j] = std::complex<eT>(mat_Z(j, i) / z_norm, eT(0));
  98. }
  99. col_ptr += n;
  100. }
  101. else
  102. {
  103. // complex eigenvectors are stored in consecutive columns
  104. eT r2 = dot(mat_Z.col(i), mat_Z.col(i));
  105. eT i2 = dot(mat_Z.col(i + 1), mat_Z.col(i + 1));
  106. eT z_norm = std::sqrt(r2 + i2);
  107. eT* z_ptr = mat_Z.colptr(i);
  108. for(blas_int j = 0; j < n; j++)
  109. {
  110. col_ptr[j ] = std::complex<eT>(z_ptr[j] / z_norm, z_ptr[j + n] / z_norm);
  111. col_ptr[j + n] = std::conj(col_ptr[j]);
  112. }
  113. i++;
  114. col_ptr += 2 * n;
  115. }
  116. }
  117. return evecs;
  118. }
  119. } // namespace newarp