fn_hess.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_hess
  16. //! @{
  17. template<typename T1>
  18. inline
  19. bool
  20. hess
  21. (
  22. Mat<typename T1::elem_type>& H,
  23. const Base<typename T1::elem_type,T1>& X,
  24. const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0
  25. )
  26. {
  27. arma_extra_debug_sigprint();
  28. arma_ignore(junk);
  29. typedef typename T1::elem_type eT;
  30. Col<eT> tao;
  31. const bool status = auxlib::hess(H, X.get_ref(), tao);
  32. if(H.n_rows > 2)
  33. {
  34. for(uword i=0; i < H.n_rows-2; ++i)
  35. {
  36. H(span(i+2, H.n_rows-1), i).zeros();
  37. }
  38. }
  39. if(status == false)
  40. {
  41. H.soft_reset();
  42. arma_debug_warn("hess(): decomposition failed");
  43. }
  44. return status;
  45. }
  46. template<typename T1>
  47. arma_warn_unused
  48. inline
  49. Mat<typename T1::elem_type>
  50. hess
  51. (
  52. const Base<typename T1::elem_type,T1>& X,
  53. const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0
  54. )
  55. {
  56. arma_extra_debug_sigprint();
  57. arma_ignore(junk);
  58. typedef typename T1::elem_type eT;
  59. Mat<eT> H;
  60. Col<eT> tao;
  61. const bool status = auxlib::hess(H, X.get_ref(), tao);
  62. if(H.n_rows > 2)
  63. {
  64. for(uword i=0; i < H.n_rows-2; ++i)
  65. {
  66. H(span(i+2, H.n_rows-1), i).zeros();
  67. }
  68. }
  69. if(status == false)
  70. {
  71. H.soft_reset();
  72. arma_stop_runtime_error("hess(): decomposition failed");
  73. }
  74. return H;
  75. }
  76. template<typename T1>
  77. inline
  78. bool
  79. hess
  80. (
  81. Mat<typename T1::elem_type>& U,
  82. Mat<typename T1::elem_type>& H,
  83. const Base<typename T1::elem_type,T1>& X,
  84. const typename arma_blas_type_only<typename T1::elem_type>::result* junk = 0
  85. )
  86. {
  87. arma_extra_debug_sigprint();
  88. arma_ignore(junk);
  89. arma_debug_check( void_ptr(&U) == void_ptr(&H), "hess(): 'U' is an alias of 'H'" );
  90. typedef typename T1::elem_type eT;
  91. Col<eT> tao;
  92. const bool status = auxlib::hess(H, X.get_ref(), tao);
  93. if(H.n_rows == 0)
  94. {
  95. U.reset();
  96. }
  97. else
  98. if(H.n_rows == 1)
  99. {
  100. U.ones(1, 1);
  101. }
  102. else
  103. if(H.n_rows == 2)
  104. {
  105. U.eye(2, 2);
  106. }
  107. else
  108. {
  109. U.eye(size(H));
  110. Col<eT> v;
  111. for(uword i=0; i < H.n_rows-2; ++i)
  112. {
  113. // TODO: generate v in a more efficient manner;
  114. // TODO: the .ones() operation is an overkill, as most of v is overwritten afterwards
  115. v.ones(H.n_rows-i-1);
  116. v(span(1, H.n_rows-i-2)) = H(span(i+2, H.n_rows-1), i);
  117. U(span::all, span(i+1, H.n_rows-1)) -= tao(i) * (U(span::all, span(i+1, H.n_rows-1)) * v * v.t());
  118. }
  119. U(span::all, H.n_rows-1) = U(span::all, H.n_rows-1) * (eT(1) - tao(H.n_rows-2));
  120. for(uword i=0; i < H.n_rows-2; ++i)
  121. {
  122. H(span(i+2, H.n_rows-1), i).zeros();
  123. }
  124. }
  125. if(status == false)
  126. {
  127. U.soft_reset();
  128. H.soft_reset();
  129. arma_debug_warn("hess(): decomposition failed");
  130. }
  131. return status;
  132. }
  133. //! @}