op_sqrtmat_meat.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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_sqrtmat
  16. //! @{
  17. //! implementation partly based on:
  18. //! N. J. Higham.
  19. //! A New sqrtm for Matlab.
  20. //! Numerical Analysis Report No. 336, January 1999.
  21. //! Department of Mathematics, University of Manchester.
  22. //! ISSN 1360-1725
  23. //! http://www.maths.manchester.ac.uk/~higham/narep/narep336.ps.gz
  24. template<typename T1>
  25. inline
  26. void
  27. op_sqrtmat::apply(Mat< std::complex<typename T1::elem_type> >& out, const mtOp<std::complex<typename T1::elem_type>,T1,op_sqrtmat>& in)
  28. {
  29. arma_extra_debug_sigprint();
  30. const bool status = op_sqrtmat::apply_direct(out, in.m);
  31. if(status == false)
  32. {
  33. arma_debug_warn("sqrtmat(): given matrix seems singular; may not have a square root");
  34. }
  35. }
  36. template<typename T1>
  37. inline
  38. bool
  39. op_sqrtmat::apply_direct(Mat< std::complex<typename T1::elem_type> >& out, const Op<T1,op_diagmat>& expr)
  40. {
  41. arma_extra_debug_sigprint();
  42. typedef typename T1::elem_type T;
  43. const diagmat_proxy<T1> P(expr.m);
  44. arma_debug_check( (P.n_rows != P.n_cols), "sqrtmat(): given matrix must be square sized" );
  45. const uword N = P.n_rows;
  46. out.zeros(N,N);
  47. bool singular = false;
  48. for(uword i=0; i<N; ++i)
  49. {
  50. const T val = P[i];
  51. if(val >= T(0))
  52. {
  53. singular = (singular || (val == T(0)));
  54. out.at(i,i) = std::sqrt(val);
  55. }
  56. else
  57. {
  58. out.at(i,i) = std::sqrt( std::complex<T>(val) );
  59. }
  60. }
  61. return (singular) ? false : true;
  62. }
  63. template<typename T1>
  64. inline
  65. bool
  66. op_sqrtmat::apply_direct(Mat< std::complex<typename T1::elem_type> >& out, const Base<typename T1::elem_type,T1>& expr)
  67. {
  68. arma_extra_debug_sigprint();
  69. typedef typename T1::elem_type in_T;
  70. typedef typename std::complex<in_T> out_T;
  71. const quasi_unwrap<T1> expr_unwrap(expr.get_ref());
  72. const Mat<in_T>& A = expr_unwrap.M;
  73. arma_debug_check( (A.is_square() == false), "sqrtmat(): given matrix must be square sized" );
  74. if(A.n_elem == 0)
  75. {
  76. out.reset();
  77. return true;
  78. }
  79. else
  80. if(A.n_elem == 1)
  81. {
  82. out.set_size(1,1);
  83. out[0] = std::sqrt( std::complex<in_T>( A[0] ) );
  84. return true;
  85. }
  86. if(A.is_diagmat())
  87. {
  88. const uword N = A.n_rows;
  89. out.zeros(N,N); // aliasing can't happen as op_sqrtmat is defined as cx_mat = op(mat)
  90. for(uword i=0; i<N; ++i)
  91. {
  92. const in_T val = A.at(i,i);
  93. if(val >= in_T(0))
  94. {
  95. out.at(i,i) = std::sqrt(val);
  96. }
  97. else
  98. {
  99. out.at(i,i) = std::sqrt( out_T(val) );
  100. }
  101. }
  102. return true;
  103. }
  104. #if defined(ARMA_OPTIMISE_SYMPD)
  105. const bool try_sympd = sympd_helper::guess_sympd_anysize(A);
  106. #else
  107. const bool try_sympd = false;
  108. #endif
  109. if(try_sympd)
  110. {
  111. // if matrix A is sympd, all its eigenvalues are positive
  112. Col<in_T> eigval;
  113. Mat<in_T> eigvec;
  114. const bool eig_status = eig_sym_helper(eigval, eigvec, A, 'd', "sqrtmat()");
  115. if(eig_status)
  116. {
  117. // ensure each eigenvalue is > 0
  118. const uword N = eigval.n_elem;
  119. const in_T* eigval_mem = eigval.memptr();
  120. bool all_pos = true;
  121. for(uword i=0; i<N; ++i) { all_pos = (eigval_mem[i] <= in_T(0)) ? false : all_pos; }
  122. if(all_pos)
  123. {
  124. eigval = sqrt(eigval);
  125. out = conv_to< Mat<out_T> >::from( eigvec * diagmat(eigval) * eigvec.t() );
  126. return true;
  127. }
  128. }
  129. arma_extra_debug_print("warning: sympd optimisation failed");
  130. // fallthrough if eigen decomposition failed or an eigenvalue is zero
  131. }
  132. Mat<out_T> U;
  133. Mat<out_T> S(A.n_rows, A.n_cols);
  134. const in_T* Amem = A.memptr();
  135. out_T* Smem = S.memptr();
  136. const uword n_elem = A.n_elem;
  137. for(uword i=0; i<n_elem; ++i)
  138. {
  139. Smem[i] = std::complex<in_T>( Amem[i] );
  140. }
  141. const bool schur_ok = auxlib::schur(U,S);
  142. if(schur_ok == false)
  143. {
  144. arma_extra_debug_print("sqrtmat(): schur decomposition failed");
  145. out.soft_reset();
  146. return false;
  147. }
  148. const bool status = op_sqrtmat_cx::helper(S);
  149. const Mat<out_T> X = U*S;
  150. S.reset();
  151. out = X*U.t();
  152. return status;
  153. }
  154. template<typename T1>
  155. inline
  156. void
  157. op_sqrtmat_cx::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_sqrtmat_cx>& in)
  158. {
  159. arma_extra_debug_sigprint();
  160. const bool status = op_sqrtmat_cx::apply_direct(out, in.m);
  161. if(status == false)
  162. {
  163. arma_debug_warn("sqrtmat(): given matrix seems singular; may not have a square root");
  164. }
  165. }
  166. template<typename T1>
  167. inline
  168. bool
  169. op_sqrtmat_cx::apply_direct(Mat<typename T1::elem_type>& out, const Op<T1,op_diagmat>& expr)
  170. {
  171. arma_extra_debug_sigprint();
  172. typedef typename T1::elem_type eT;
  173. const diagmat_proxy<T1> P(expr.m);
  174. bool status = false;
  175. if(P.is_alias(out))
  176. {
  177. Mat<eT> tmp;
  178. status = op_sqrtmat_cx::apply_direct_noalias(tmp, P);
  179. out.steal_mem(tmp);
  180. }
  181. else
  182. {
  183. status = op_sqrtmat_cx::apply_direct_noalias(out, P);
  184. }
  185. return status;
  186. }
  187. template<typename T1>
  188. inline
  189. bool
  190. op_sqrtmat_cx::apply_direct_noalias(Mat<typename T1::elem_type>& out, const diagmat_proxy<T1>& P)
  191. {
  192. arma_extra_debug_sigprint();
  193. typedef typename T1::elem_type eT;
  194. arma_debug_check( (P.n_rows != P.n_cols), "sqrtmat(): given matrix must be square sized" );
  195. const uword N = P.n_rows;
  196. out.zeros(N,N);
  197. const eT zero = eT(0);
  198. bool singular = false;
  199. for(uword i=0; i<N; ++i)
  200. {
  201. const eT val = P[i];
  202. singular = (singular || (val == zero));
  203. out.at(i,i) = std::sqrt(val);
  204. }
  205. return (singular) ? false : true;
  206. }
  207. template<typename T1>
  208. inline
  209. bool
  210. op_sqrtmat_cx::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& expr)
  211. {
  212. arma_extra_debug_sigprint();
  213. typedef typename T1::pod_type T;
  214. typedef typename T1::elem_type eT;
  215. Mat<eT> U;
  216. Mat<eT> S = expr.get_ref();
  217. arma_debug_check( (S.n_rows != S.n_cols), "sqrtmat(): given matrix must be square sized" );
  218. if(S.n_elem == 0)
  219. {
  220. out.reset();
  221. return true;
  222. }
  223. else
  224. if(S.n_elem == 1)
  225. {
  226. out.set_size(1,1);
  227. out[0] = std::sqrt(S[0]);
  228. return true;
  229. }
  230. if(S.is_diagmat())
  231. {
  232. const uword N = S.n_rows;
  233. out.zeros(N,N); // aliasing can't happen as S is generated
  234. for(uword i=0; i<N; ++i) { out.at(i,i) = std::sqrt( S.at(i,i) ); }
  235. return true;
  236. }
  237. #if defined(ARMA_OPTIMISE_SYMPD)
  238. const bool try_sympd = sympd_helper::guess_sympd_anysize(S);
  239. #else
  240. const bool try_sympd = false;
  241. #endif
  242. if(try_sympd)
  243. {
  244. // if matrix S is sympd, all its eigenvalues are positive
  245. Col< T> eigval;
  246. Mat<eT> eigvec;
  247. const bool eig_status = eig_sym_helper(eigval, eigvec, S, 'd', "sqrtmat()");
  248. if(eig_status)
  249. {
  250. // ensure each eigenvalue is > 0
  251. const uword N = eigval.n_elem;
  252. const T* eigval_mem = eigval.memptr();
  253. bool all_pos = true;
  254. for(uword i=0; i<N; ++i) { all_pos = (eigval_mem[i] <= T(0)) ? false : all_pos; }
  255. if(all_pos)
  256. {
  257. eigval = sqrt(eigval);
  258. out = eigvec * diagmat(eigval) * eigvec.t();
  259. return true;
  260. }
  261. }
  262. arma_extra_debug_print("warning: sympd optimisation failed");
  263. // fallthrough if eigen decomposition failed or an eigenvalue is zero
  264. }
  265. const bool schur_ok = auxlib::schur(U, S);
  266. if(schur_ok == false)
  267. {
  268. arma_extra_debug_print("sqrtmat(): schur decomposition failed");
  269. out.soft_reset();
  270. return false;
  271. }
  272. const bool status = op_sqrtmat_cx::helper(S);
  273. const Mat<eT> X = U*S;
  274. S.reset();
  275. out = X*U.t();
  276. return status;
  277. }
  278. template<typename T>
  279. inline
  280. bool
  281. op_sqrtmat_cx::helper(Mat< std::complex<T> >& S)
  282. {
  283. typedef typename std::complex<T> eT;
  284. if(S.is_empty()) { return true; }
  285. const uword N = S.n_rows;
  286. const eT zero = eT(0);
  287. eT& S_00 = S[0];
  288. bool singular = (S_00 == zero);
  289. S_00 = std::sqrt(S_00);
  290. for(uword j=1; j < N; ++j)
  291. {
  292. eT* S_j = S.colptr(j);
  293. eT& S_jj = S_j[j];
  294. singular = (singular || (S_jj == zero));
  295. S_jj = std::sqrt(S_jj);
  296. for(uword ii=0; ii <= (j-1); ++ii)
  297. {
  298. const uword i = (j-1) - ii;
  299. const eT* S_i = S.colptr(i);
  300. //S_j[i] /= (S_i[i] + S_j[j]);
  301. S_j[i] /= (S_i[i] + S_jj);
  302. for(uword k=0; k < i; ++k)
  303. {
  304. S_j[k] -= S_i[k] * S_j[i];
  305. }
  306. }
  307. }
  308. return (singular) ? false : true;
  309. }
  310. template<typename T1>
  311. inline
  312. void
  313. op_sqrtmat_sympd::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_sqrtmat_sympd>& in)
  314. {
  315. arma_extra_debug_sigprint();
  316. const bool status = op_sqrtmat_sympd::apply_direct(out, in.m);
  317. if(status == false)
  318. {
  319. out.soft_reset();
  320. arma_stop_runtime_error("sqrtmat_sympd(): transformation failed");
  321. }
  322. }
  323. template<typename T1>
  324. inline
  325. bool
  326. op_sqrtmat_sympd::apply_direct(Mat<typename T1::elem_type>& out, const Base<typename T1::elem_type,T1>& expr)
  327. {
  328. arma_extra_debug_sigprint();
  329. #if defined(ARMA_USE_LAPACK)
  330. {
  331. typedef typename T1::pod_type T;
  332. typedef typename T1::elem_type eT;
  333. const unwrap<T1> U(expr.get_ref());
  334. const Mat<eT>& X = U.M;
  335. arma_debug_check( (X.is_square() == false), "sqrtmat_sympd(): given matrix must be square sized" );
  336. Col< T> eigval;
  337. Mat<eT> eigvec;
  338. const bool status = eig_sym_helper(eigval, eigvec, X, 'd', "sqrtmat_sympd()");
  339. if(status == false) { return false; }
  340. const uword N = eigval.n_elem;
  341. const T* eigval_mem = eigval.memptr();
  342. bool all_pos = true;
  343. for(uword i=0; i<N; ++i) { all_pos = (eigval_mem[i] < T(0)) ? false : all_pos; }
  344. if(all_pos == false) { return false; }
  345. eigval = sqrt(eigval);
  346. out = eigvec * diagmat(eigval) * eigvec.t();
  347. return true;
  348. }
  349. #else
  350. {
  351. arma_ignore(out);
  352. arma_ignore(expr);
  353. arma_stop_logic_error("sqrtmat_sympd(): use of LAPACK must be enabled");
  354. return false;
  355. }
  356. #endif
  357. }
  358. //! @}