op_expmat_meat.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 op_expmat
  16. //! @{
  17. //! implementation based on:
  18. //! Cleve Moler, Charles Van Loan.
  19. //! Nineteen Dubious Ways to Compute the Exponential of a Matrix, Twenty-Five Years Later.
  20. //! SIAM Review, Vol. 45, No. 1, 2003, pp. 3-49.
  21. //! http://dx.doi.org/10.1137/S00361445024180
  22. template<typename T1>
  23. inline
  24. void
  25. op_expmat::apply(Mat<typename T1::elem_type>& out, const Op<T1, op_expmat>& expr)
  26. {
  27. arma_extra_debug_sigprint();
  28. const bool status = op_expmat::apply_direct(out, expr.m);
  29. if(status == false)
  30. {
  31. out.soft_reset();
  32. arma_stop_runtime_error("expmat(): given matrix appears ill-conditioned");
  33. }
  34. }
  35. template<typename T1>
  36. inline
  37. bool
  38. op_expmat::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type, T1>& expr)
  39. {
  40. arma_extra_debug_sigprint();
  41. typedef typename T1::elem_type eT;
  42. typedef typename T1::pod_type T;
  43. if(is_op_diagmat<T1>::value)
  44. {
  45. out = expr.get_ref(); // force the evaluation of diagmat()
  46. arma_debug_check( (out.is_square() == false), "expmat(): given matrix must be square sized" );
  47. const uword N = (std::min)(out.n_rows, out.n_cols);
  48. for(uword i=0; i<N; ++i) { out.at(i,i) = std::exp( out.at(i,i) ); }
  49. }
  50. else
  51. {
  52. Mat<eT> A = expr.get_ref();
  53. arma_debug_check( (A.is_square() == false), "expmat(): given matrix must be square sized" );
  54. if(A.is_diagmat())
  55. {
  56. const uword N = (std::min)(A.n_rows, A.n_cols);
  57. out.zeros(N,N);
  58. for(uword i=0; i<N; ++i) { out.at(i,i) = std::exp( A.at(i,i) ); }
  59. return true;
  60. }
  61. #if defined(ARMA_OPTIMISE_SYMPD)
  62. const bool try_sympd = sympd_helper::guess_sympd_anysize(A);
  63. #else
  64. const bool try_sympd = false;
  65. #endif
  66. if(try_sympd)
  67. {
  68. // if matrix A is sympd, all its eigenvalues are positive
  69. Col< T> eigval;
  70. Mat<eT> eigvec;
  71. const bool eig_status = eig_sym_helper(eigval, eigvec, A, 'd', "expmat()");
  72. if(eig_status)
  73. {
  74. eigval = exp(eigval);
  75. out = eigvec * diagmat(eigval) * eigvec.t();
  76. return true;
  77. }
  78. arma_extra_debug_print("warning: sympd optimisation failed");
  79. // fallthrough if eigen decomposition failed
  80. }
  81. const T norm_val = arma::norm(A, "inf");
  82. const double log2_val = (norm_val > T(0)) ? double(eop_aux::log2(norm_val)) : double(0);
  83. int exponent = int(0); std::frexp(log2_val, &exponent);
  84. const uword s = uword( (std::max)(int(0), exponent + int(1)) );
  85. A /= eT(eop_aux::pow(double(2), double(s)));
  86. T c = T(0.5);
  87. Mat<eT> E(A.n_rows, A.n_rows, fill::eye); E += c * A;
  88. Mat<eT> D(A.n_rows, A.n_rows, fill::eye); D -= c * A;
  89. Mat<eT> X = A;
  90. bool positive = true;
  91. const uword N = 6;
  92. for(uword i = 2; i <= N; ++i)
  93. {
  94. c = c * T(N - i + 1) / T(i * (2*N - i + 1));
  95. X = A * X;
  96. E += c * X;
  97. if(positive) { D += c * X; } else { D -= c * X; }
  98. positive = (positive) ? false : true;
  99. }
  100. if( (D.is_finite() == false) || (E.is_finite() == false) ) { return false; }
  101. const bool status = solve(out, D, E);
  102. if(status == false) { return false; }
  103. for(uword i=0; i < s; ++i) { out = out * out; }
  104. }
  105. return true;
  106. }
  107. template<typename T1>
  108. inline
  109. void
  110. op_expmat_sym::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_expmat_sym>& in)
  111. {
  112. arma_extra_debug_sigprint();
  113. const bool status = op_expmat_sym::apply_direct(out, in.m);
  114. if(status == false)
  115. {
  116. out.soft_reset();
  117. arma_stop_runtime_error("expmat_sym(): transformation failed");
  118. }
  119. }
  120. template<typename T1>
  121. inline
  122. bool
  123. op_expmat_sym::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& expr)
  124. {
  125. arma_extra_debug_sigprint();
  126. #if defined(ARMA_USE_LAPACK)
  127. {
  128. typedef typename T1::pod_type T;
  129. typedef typename T1::elem_type eT;
  130. const unwrap<T1> U(expr.get_ref());
  131. const Mat<eT>& X = U.M;
  132. arma_debug_check( (X.is_square() == false), "expmat_sym(): given matrix must be square sized" );
  133. Col< T> eigval;
  134. Mat<eT> eigvec;
  135. const bool status = eig_sym_helper(eigval, eigvec, X, 'd', "expmat_sym()");
  136. if(status == false) { return false; }
  137. eigval = exp(eigval);
  138. out = eigvec * diagmat(eigval) * eigvec.t();
  139. return true;
  140. }
  141. #else
  142. {
  143. arma_ignore(out);
  144. arma_ignore(expr);
  145. arma_stop_logic_error("expmat_sym(): use of LAPACK must be enabled");
  146. return false;
  147. }
  148. #endif
  149. }
  150. //! @}