newarp_DoubleShiftQR_meat.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. void
  20. DoubleShiftQR<eT>::compute_reflector(const eT& x1, const eT& x2, const eT& x3, uword ind)
  21. {
  22. arma_extra_debug_sigprint();
  23. // In general case the reflector affects 3 rows
  24. ref_nr(ind) = 3;
  25. eT x2x3 = eT(0);
  26. // If x3 is zero, decrease nr by 1
  27. if(std::abs(x3) < prec)
  28. {
  29. // If x2 is also zero, nr will be 1, and we can exit this function
  30. if(std::abs(x2) < prec)
  31. {
  32. ref_nr(ind) = 1;
  33. return;
  34. }
  35. else
  36. {
  37. ref_nr(ind) = 2;
  38. }
  39. x2x3 = std::abs(x2);
  40. }
  41. else
  42. {
  43. x2x3 = arma_hypot(x2, x3);
  44. }
  45. // x1' = x1 - rho * ||x||
  46. // rho = -sign(x1), if x1 == 0, we choose rho = 1
  47. eT x1_new = x1 - ((x1 <= 0) - (x1 > 0)) * arma_hypot(x1, x2x3);
  48. eT x_norm = arma_hypot(x1_new, x2x3);
  49. // Double check the norm of new x
  50. if(x_norm < prec)
  51. {
  52. ref_nr(ind) = 1;
  53. return;
  54. }
  55. ref_u(0, ind) = x1_new / x_norm;
  56. ref_u(1, ind) = x2 / x_norm;
  57. ref_u(2, ind) = x3 / x_norm;
  58. }
  59. template<typename eT>
  60. arma_inline
  61. void
  62. DoubleShiftQR<eT>::compute_reflector(const eT* x, uword ind)
  63. {
  64. arma_extra_debug_sigprint();
  65. compute_reflector(x[0], x[1], x[2], ind);
  66. }
  67. template<typename eT>
  68. inline
  69. void
  70. DoubleShiftQR<eT>::update_block(uword il, uword iu)
  71. {
  72. arma_extra_debug_sigprint();
  73. // Block size
  74. uword bsize = iu - il + 1;
  75. // If block size == 1, there is no need to apply reflectors
  76. if(bsize == 1)
  77. {
  78. ref_nr(il) = 1;
  79. return;
  80. }
  81. // For block size == 2, do a Givens rotation on M = X * X - s * X + t * I
  82. if(bsize == 2)
  83. {
  84. // m00 = x00 * (x00 - s) + x01 * x10 + t
  85. eT m00 = mat_H(il, il) * (mat_H(il, il) - shift_s) +
  86. mat_H(il, il + 1) * mat_H(il + 1, il) +
  87. shift_t;
  88. // m10 = x10 * (x00 + x11 - s)
  89. eT m10 = mat_H(il + 1, il) * (mat_H(il, il) + mat_H(il + 1, il + 1) - shift_s);
  90. // This causes nr=2
  91. compute_reflector(m00, m10, 0, il);
  92. // Apply the reflector to X
  93. apply_PX(mat_H, il, il, 2, n - il, il);
  94. apply_XP(mat_H, 0, il, il + 2, 2, il);
  95. ref_nr(il + 1) = 1;
  96. return;
  97. }
  98. // For block size >=3, use the regular strategy
  99. eT m00 = mat_H(il, il) * (mat_H(il, il) - shift_s) +
  100. mat_H(il, il + 1) * mat_H(il + 1, il) +
  101. shift_t;
  102. eT m10 = mat_H(il + 1, il) * (mat_H(il, il) + mat_H(il + 1, il + 1) - shift_s);
  103. // m20 = x21 * x10
  104. eT m20 = mat_H(il + 2, il + 1) * mat_H(il + 1, il);
  105. compute_reflector(m00, m10, m20, il);
  106. // Apply the first reflector
  107. apply_PX(mat_H, il, il, 3, n - il, il);
  108. apply_XP(mat_H, 0, il, il + std::min(bsize, uword(4)), 3, il);
  109. // Calculate the following reflectors
  110. // If entering this loop, block size is at least 4.
  111. for(uword i = 1; i < bsize - 2; i++)
  112. {
  113. compute_reflector(mat_H.colptr(il + i - 1) + il + i, il + i);
  114. // Apply the reflector to X
  115. apply_PX(mat_H, il + i, il + i - 1, 3, n + 1 - il - i, il + i);
  116. apply_XP(mat_H, 0, il + i, il + std::min(bsize, uword(i + 4)), 3, il + i);
  117. }
  118. // The last reflector
  119. // This causes nr=2
  120. compute_reflector(mat_H(iu - 1, iu - 2), mat_H(iu, iu - 2), 0, iu - 1);
  121. // Apply the reflector to X
  122. apply_PX(mat_H, iu - 1, iu - 2, 2, n + 2 - iu, iu - 1);
  123. apply_XP(mat_H, 0, iu - 1, il + bsize, 2, iu - 1);
  124. ref_nr(iu) = 1;
  125. }
  126. template<typename eT>
  127. inline
  128. void
  129. DoubleShiftQR<eT>::apply_PX(Mat<eT>& X, uword oi, uword oj, uword nrow, uword ncol, uword u_ind)
  130. {
  131. arma_extra_debug_sigprint();
  132. if(ref_nr(u_ind) == 1) { return; }
  133. // Householder reflectors at index u_ind
  134. Col<eT> u(ref_u.colptr(u_ind), 3, false);
  135. const uword stride = X.n_rows;
  136. const eT u0_2 = 2 * u(0);
  137. const eT u1_2 = 2 * u(1);
  138. eT* xptr = &X(oi, oj);
  139. if(ref_nr(u_ind) == 2 || nrow == 2)
  140. {
  141. for(uword i = 0; i < ncol; i++, xptr += stride)
  142. {
  143. eT tmp = u0_2 * xptr[0] + u1_2 * xptr[1];
  144. xptr[0] -= tmp * u(0);
  145. xptr[1] -= tmp * u(1);
  146. }
  147. }
  148. else
  149. {
  150. const eT u2_2 = 2 * u(2);
  151. for(uword i = 0; i < ncol; i++, xptr += stride)
  152. {
  153. eT tmp = u0_2 * xptr[0] + u1_2 * xptr[1] + u2_2 * xptr[2];
  154. xptr[0] -= tmp * u(0);
  155. xptr[1] -= tmp * u(1);
  156. xptr[2] -= tmp * u(2);
  157. }
  158. }
  159. }
  160. template<typename eT>
  161. inline
  162. void
  163. DoubleShiftQR<eT>::apply_PX(eT* x, uword u_ind)
  164. {
  165. arma_extra_debug_sigprint();
  166. if(ref_nr(u_ind) == 1) { return; }
  167. eT u0 = ref_u(0, u_ind),
  168. u1 = ref_u(1, u_ind),
  169. u2 = ref_u(2, u_ind);
  170. // When the reflector only contains two elements, u2 has been set to zero
  171. bool nr_is_2 = (ref_nr(u_ind) == 2);
  172. eT dot2 = x[0] * u0 + x[1] * u1 + (nr_is_2 ? 0 : (x[2] * u2));
  173. dot2 *= 2;
  174. x[0] -= dot2 * u0;
  175. x[1] -= dot2 * u1;
  176. if(!nr_is_2) { x[2] -= dot2 * u2; }
  177. }
  178. template<typename eT>
  179. inline
  180. void
  181. DoubleShiftQR<eT>::apply_XP(Mat<eT>& X, uword oi, uword oj, uword nrow, uword ncol, uword u_ind)
  182. {
  183. arma_extra_debug_sigprint();
  184. if(ref_nr(u_ind) == 1) { return; }
  185. // Householder reflectors at index u_ind
  186. Col<eT> u(ref_u.colptr(u_ind), 3, false);
  187. uword stride = X.n_rows;
  188. const eT u0_2 = 2 * u(0);
  189. const eT u1_2 = 2 * u(1);
  190. eT* X0 = &X(oi, oj);
  191. eT* X1 = X0 + stride; // X0 => X(oi, oj), X1 => X(oi, oj + 1)
  192. if(ref_nr(u_ind) == 2 || ncol == 2)
  193. {
  194. // tmp = 2 * u0 * X0 + 2 * u1 * X1
  195. // X0 => X0 - u0 * tmp
  196. // X1 => X1 - u1 * tmp
  197. for(uword i = 0; i < nrow; i++)
  198. {
  199. eT tmp = u0_2 * X0[i] + u1_2 * X1[i];
  200. X0[i] -= tmp * u(0);
  201. X1[i] -= tmp * u(1);
  202. }
  203. }
  204. else
  205. {
  206. eT* X2 = X1 + stride; // X2 => X(oi, oj + 2)
  207. const eT u2_2 = 2 * u(2);
  208. for(uword i = 0; i < nrow; i++)
  209. {
  210. eT tmp = u0_2 * X0[i] + u1_2 * X1[i] + u2_2 * X2[i];
  211. X0[i] -= tmp * u(0);
  212. X1[i] -= tmp * u(1);
  213. X2[i] -= tmp * u(2);
  214. }
  215. }
  216. }
  217. template<typename eT>
  218. inline
  219. DoubleShiftQR<eT>::DoubleShiftQR(uword size)
  220. : n(size)
  221. , prec(std::numeric_limits<eT>::epsilon())
  222. , eps_rel(prec)
  223. , eps_abs(prec)
  224. , computed(false)
  225. {
  226. arma_extra_debug_sigprint();
  227. }
  228. template<typename eT>
  229. inline
  230. DoubleShiftQR<eT>::DoubleShiftQR(const Mat<eT>& mat_obj, eT s, eT t)
  231. : n(mat_obj.n_rows)
  232. , mat_H(n, n)
  233. , shift_s(s)
  234. , shift_t(t)
  235. , ref_u(3, n)
  236. , ref_nr(n)
  237. , prec(std::numeric_limits<eT>::epsilon())
  238. , eps_rel(prec)
  239. , eps_abs(prec)
  240. , computed(false)
  241. {
  242. arma_extra_debug_sigprint();
  243. compute(mat_obj, s, t);
  244. }
  245. template<typename eT>
  246. void
  247. DoubleShiftQR<eT>::compute(const Mat<eT>& mat_obj, eT s, eT t)
  248. {
  249. arma_extra_debug_sigprint();
  250. arma_debug_check( (mat_obj.is_square() == false), "newarp::DoubleShiftQR::compute(): matrix must be square" );
  251. n = mat_obj.n_rows;
  252. mat_H.set_size(n, n);
  253. shift_s = s;
  254. shift_t = t;
  255. ref_u.set_size(3, n);
  256. ref_nr.set_size(n);
  257. // Make a copy of mat_obj
  258. mat_H = mat_obj;
  259. // Obtain the indices of zero elements in the subdiagonal,
  260. // so that H can be divided into several blocks
  261. std::vector<uword> zero_ind;
  262. zero_ind.reserve(n - 1);
  263. zero_ind.push_back(0);
  264. eT* Hii = mat_H.memptr();
  265. for(uword i = 0; i < n - 2; i++, Hii += (n + 1))
  266. {
  267. // Hii[1] => mat_H(i + 1, i)
  268. const eT h = std::abs(Hii[1]);
  269. if(h <= eps_abs || h <= eps_rel * (std::abs(Hii[0]) + std::abs(Hii[n + 1])))
  270. {
  271. Hii[1] = 0;
  272. zero_ind.push_back(i + 1);
  273. }
  274. // Make sure mat_H is upper Hessenberg
  275. // Zero the elements below mat_H(i + 1, i)
  276. std::fill(Hii + 2, Hii + n - i, eT(0));
  277. }
  278. zero_ind.push_back(n);
  279. for(std::vector<uword>::size_type i = 0; i < zero_ind.size() - 1; i++)
  280. {
  281. uword start = zero_ind[i];
  282. uword end = zero_ind[i + 1] - 1;
  283. // Compute refelctors from each block X
  284. update_block(start, end);
  285. }
  286. computed = true;
  287. }
  288. template<typename eT>
  289. Mat<eT>
  290. DoubleShiftQR<eT>::matrix_QtHQ()
  291. {
  292. arma_extra_debug_sigprint();
  293. arma_debug_check( (computed == false), "newarp::DoubleShiftQR::matrix_QtHQ(): need to call compute() first" );
  294. return mat_H;
  295. }
  296. template<typename eT>
  297. inline
  298. void
  299. DoubleShiftQR<eT>::apply_QtY(Col<eT>& y)
  300. {
  301. arma_extra_debug_sigprint();
  302. arma_debug_check( (computed == false), "newarp::DoubleShiftQR::apply_QtY(): need to call compute() first" );
  303. eT* y_ptr = y.memptr();
  304. for(uword i = 0; i < n - 1; i++, y_ptr++)
  305. {
  306. apply_PX(y_ptr, i);
  307. }
  308. }
  309. template<typename eT>
  310. inline
  311. void
  312. DoubleShiftQR<eT>::apply_YQ(Mat<eT>& Y)
  313. {
  314. arma_extra_debug_sigprint();
  315. arma_debug_check( (computed == false), "newarp::DoubleShiftQR::apply_YQ(): need to call compute() first" );
  316. uword nrow = Y.n_rows;
  317. for(uword i = 0; i < n - 2; i++)
  318. {
  319. apply_XP(Y, 0, i, nrow, 3, i);
  320. }
  321. apply_XP(Y, 0, n - 2, nrow, 2, n - 2);
  322. }
  323. } // namespace newarp