fn_eig_sym.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //! \addtogroup fn_eig_sym
  16. //! @{
  17. //! Eigenvalues of real/complex symmetric/hermitian matrix X
  18. template<typename T1>
  19. inline
  20. typename enable_if2< is_supported_blas_type<typename T1::elem_type>::value, bool >::result
  21. eig_sym
  22. (
  23. Col<typename T1::pod_type>& eigval,
  24. const Base<typename T1::elem_type,T1>& X
  25. )
  26. {
  27. arma_extra_debug_sigprint();
  28. // unwrap_check not used as T1::elem_type and T1::pod_type may not be the same.
  29. // furthermore, it doesn't matter if X is an alias of eigval, as auxlib::eig_sym() makes a copy of X
  30. const bool status = auxlib::eig_sym(eigval, X);
  31. if(status == false)
  32. {
  33. eigval.soft_reset();
  34. arma_debug_warn("eig_sym(): decomposition failed");
  35. }
  36. return status;
  37. }
  38. //! Eigenvalues of real/complex symmetric/hermitian matrix X
  39. template<typename T1>
  40. arma_warn_unused
  41. inline
  42. typename enable_if2< is_supported_blas_type<typename T1::elem_type>::value, Col<typename T1::pod_type> >::result
  43. eig_sym
  44. (
  45. const Base<typename T1::elem_type,T1>& X
  46. )
  47. {
  48. arma_extra_debug_sigprint();
  49. Col<typename T1::pod_type> out;
  50. const bool status = auxlib::eig_sym(out, X);
  51. if(status == false)
  52. {
  53. out.soft_reset();
  54. arma_stop_runtime_error("eig_sym(): decomposition failed");
  55. }
  56. return out;
  57. }
  58. //! internal helper function
  59. template<typename eT>
  60. inline
  61. bool
  62. eig_sym_helper
  63. (
  64. Col<typename get_pod_type<eT>::result>& eigval,
  65. Mat<eT>& eigvec,
  66. const Mat<eT>& X,
  67. const char method_sig,
  68. const char* caller_sig
  69. )
  70. {
  71. arma_extra_debug_sigprint();
  72. // if(auxlib::rudimentary_sym_check(X) == false)
  73. // {
  74. // if(is_cx<eT>::no ) { arma_debug_warn(caller_sig, ": given matrix is not symmetric"); }
  75. // if(is_cx<eT>::yes) { arma_debug_warn(caller_sig, ": given matrix is not hermitian"); }
  76. // return false;
  77. // }
  78. if((arma_config::debug) && (auxlib::rudimentary_sym_check(X) == false))
  79. {
  80. if(is_cx<eT>::no ) { arma_debug_warn(caller_sig, ": given matrix is not symmetric"); }
  81. if(is_cx<eT>::yes) { arma_debug_warn(caller_sig, ": given matrix is not hermitian"); }
  82. }
  83. bool status = false;
  84. if(method_sig == 'd') { status = auxlib::eig_sym_dc(eigval, eigvec, X); }
  85. if(status == false) { status = auxlib::eig_sym(eigval, eigvec, X); }
  86. return status;
  87. }
  88. //! Eigenvalues and eigenvectors of real/complex symmetric/hermitian matrix X
  89. template<typename T1>
  90. inline
  91. typename enable_if2< is_supported_blas_type<typename T1::elem_type>::value, bool >::result
  92. eig_sym
  93. (
  94. Col<typename T1::pod_type>& eigval,
  95. Mat<typename T1::elem_type>& eigvec,
  96. const Base<typename T1::elem_type,T1>& expr,
  97. const char* method = "dc"
  98. )
  99. {
  100. arma_extra_debug_sigprint();
  101. typedef typename T1::elem_type eT;
  102. const char sig = (method != NULL) ? method[0] : char(0);
  103. arma_debug_check( ((sig != 's') && (sig != 'd')), "eig_sym(): unknown method specified" );
  104. arma_debug_check( void_ptr(&eigval) == void_ptr(&eigvec), "eig_sym(): parameter 'eigval' is an alias of parameter 'eigvec'" );
  105. const quasi_unwrap<T1> U(expr.get_ref());
  106. const bool is_alias = U.is_alias(eigvec);
  107. Mat<eT> eigvec_tmp;
  108. Mat<eT>& eigvec_out = (is_alias == false) ? eigvec : eigvec_tmp;
  109. const bool status = eig_sym_helper(eigval, eigvec_out, U.M, sig, "eig_sym()");
  110. if(status == false)
  111. {
  112. eigval.soft_reset();
  113. eigvec.soft_reset();
  114. arma_debug_warn("eig_sym(): decomposition failed");
  115. }
  116. else
  117. {
  118. if(is_alias) { eigvec.steal_mem(eigvec_tmp); }
  119. }
  120. return status;
  121. }
  122. //! @}