op_htrans_meat.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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_htrans
  16. //! @{
  17. template<typename eT>
  18. arma_hot
  19. arma_inline
  20. void
  21. op_htrans::apply_mat_noalias(Mat<eT>& out, const Mat<eT>& A, const typename arma_not_cx<eT>::result* junk)
  22. {
  23. arma_extra_debug_sigprint();
  24. arma_ignore(junk);
  25. op_strans::apply_mat_noalias(out, A);
  26. }
  27. template<typename eT>
  28. arma_hot
  29. inline
  30. void
  31. op_htrans::apply_mat_noalias(Mat<eT>& out, const Mat<eT>& A, const typename arma_cx_only<eT>::result* junk)
  32. {
  33. arma_extra_debug_sigprint();
  34. arma_ignore(junk);
  35. const uword A_n_rows = A.n_rows;
  36. const uword A_n_cols = A.n_cols;
  37. out.set_size(A_n_cols, A_n_rows);
  38. if( (A_n_cols == 1) || (A_n_rows == 1) )
  39. {
  40. const uword n_elem = A.n_elem;
  41. const eT* A_mem = A.memptr();
  42. eT* out_mem = out.memptr();
  43. for(uword i=0; i < n_elem; ++i)
  44. {
  45. out_mem[i] = std::conj(A_mem[i]);
  46. }
  47. }
  48. else
  49. if( (A_n_rows >= 512) && (A_n_cols >= 512) )
  50. {
  51. op_htrans::apply_mat_noalias_large(out, A);
  52. }
  53. else
  54. {
  55. eT* outptr = out.memptr();
  56. for(uword k=0; k < A_n_rows; ++k)
  57. {
  58. const eT* Aptr = &(A.at(k,0));
  59. for(uword j=0; j < A_n_cols; ++j)
  60. {
  61. (*outptr) = std::conj(*Aptr);
  62. Aptr += A_n_rows;
  63. outptr++;
  64. }
  65. }
  66. }
  67. }
  68. template<typename T>
  69. arma_hot
  70. inline
  71. void
  72. op_htrans::block_worker(std::complex<T>* Y, const std::complex<T>* X, const uword X_n_rows, const uword Y_n_rows, const uword n_rows, const uword n_cols)
  73. {
  74. for(uword row = 0; row < n_rows; ++row)
  75. {
  76. const uword Y_offset = row * Y_n_rows;
  77. for(uword col = 0; col < n_cols; ++col)
  78. {
  79. const uword X_offset = col * X_n_rows;
  80. Y[col + Y_offset] = std::conj(X[row + X_offset]);
  81. }
  82. }
  83. }
  84. template<typename T>
  85. arma_hot
  86. inline
  87. void
  88. op_htrans::apply_mat_noalias_large(Mat< std::complex<T> >& out, const Mat< std::complex<T> >& A)
  89. {
  90. arma_extra_debug_sigprint();
  91. const uword n_rows = A.n_rows;
  92. const uword n_cols = A.n_cols;
  93. const uword block_size = 64;
  94. const uword n_rows_base = block_size * (n_rows / block_size);
  95. const uword n_cols_base = block_size * (n_cols / block_size);
  96. const uword n_rows_extra = n_rows - n_rows_base;
  97. const uword n_cols_extra = n_cols - n_cols_base;
  98. const std::complex<T>* X = A.memptr();
  99. std::complex<T>* Y = out.memptr();
  100. for(uword row = 0; row < n_rows_base; row += block_size)
  101. {
  102. const uword Y_offset = row * n_cols;
  103. for(uword col = 0; col < n_cols_base; col += block_size)
  104. {
  105. const uword X_offset = col * n_rows;
  106. op_htrans::block_worker(&Y[col + Y_offset], &X[row + X_offset], n_rows, n_cols, block_size, block_size);
  107. }
  108. const uword X_offset = n_cols_base * n_rows;
  109. op_htrans::block_worker(&Y[n_cols_base + Y_offset], &X[row + X_offset], n_rows, n_cols, block_size, n_cols_extra);
  110. }
  111. if(n_rows_extra == 0) { return; }
  112. const uword Y_offset = n_rows_base * n_cols;
  113. for(uword col = 0; col < n_cols_base; col += block_size)
  114. {
  115. const uword X_offset = col * n_rows;
  116. op_htrans::block_worker(&Y[col + Y_offset], &X[n_rows_base + X_offset], n_rows, n_cols, n_rows_extra, block_size);
  117. }
  118. const uword X_offset = n_cols_base * n_rows;
  119. op_htrans::block_worker(&Y[n_cols_base + Y_offset], &X[n_rows_base + X_offset], n_rows, n_cols, n_rows_extra, n_cols_extra);
  120. }
  121. template<typename eT>
  122. arma_hot
  123. arma_inline
  124. void
  125. op_htrans::apply_mat_inplace(Mat<eT>& out, const typename arma_not_cx<eT>::result* junk)
  126. {
  127. arma_extra_debug_sigprint();
  128. arma_ignore(junk);
  129. op_strans::apply_mat_inplace(out);
  130. }
  131. template<typename eT>
  132. arma_hot
  133. inline
  134. void
  135. op_htrans::apply_mat_inplace(Mat<eT>& out, const typename arma_cx_only<eT>::result* junk)
  136. {
  137. arma_extra_debug_sigprint();
  138. arma_ignore(junk);
  139. const uword n_rows = out.n_rows;
  140. const uword n_cols = out.n_cols;
  141. if(n_rows == n_cols)
  142. {
  143. arma_extra_debug_print("doing in-place hermitian transpose of a square matrix");
  144. for(uword col=0; col < n_cols; ++col)
  145. {
  146. eT* coldata = out.colptr(col);
  147. out.at(col,col) = std::conj( out.at(col,col) );
  148. for(uword row=(col+1); row < n_rows; ++row)
  149. {
  150. const eT val1 = std::conj(coldata[row]);
  151. const eT val2 = std::conj(out.at(col,row));
  152. out.at(col,row) = val1;
  153. coldata[row] = val2;
  154. }
  155. }
  156. }
  157. else
  158. {
  159. Mat<eT> tmp;
  160. op_htrans::apply_mat_noalias(tmp, out);
  161. out.steal_mem(tmp);
  162. }
  163. }
  164. template<typename eT>
  165. arma_hot
  166. arma_inline
  167. void
  168. op_htrans::apply_mat(Mat<eT>& out, const Mat<eT>& A, const typename arma_not_cx<eT>::result* junk)
  169. {
  170. arma_extra_debug_sigprint();
  171. arma_ignore(junk);
  172. op_strans::apply_mat(out, A);
  173. }
  174. template<typename eT>
  175. arma_hot
  176. inline
  177. void
  178. op_htrans::apply_mat(Mat<eT>& out, const Mat<eT>& A, const typename arma_cx_only<eT>::result* junk)
  179. {
  180. arma_extra_debug_sigprint();
  181. arma_ignore(junk);
  182. if(&out != &A)
  183. {
  184. op_htrans::apply_mat_noalias(out, A);
  185. }
  186. else
  187. {
  188. op_htrans::apply_mat_inplace(out);
  189. }
  190. }
  191. template<typename T1>
  192. arma_hot
  193. inline
  194. void
  195. op_htrans::apply_proxy(Mat<typename T1::elem_type>& out, const T1& X)
  196. {
  197. arma_extra_debug_sigprint();
  198. typedef typename T1::elem_type eT;
  199. const Proxy<T1> P(X);
  200. const uword n_rows = P.get_n_rows();
  201. const uword n_cols = P.get_n_cols();
  202. const bool is_alias = P.is_alias(out);
  203. if( (resolves_to_vector<T1>::yes) && (Proxy<T1>::use_at == false) )
  204. {
  205. if(is_alias == false)
  206. {
  207. out.set_size(n_cols, n_rows);
  208. eT* out_mem = out.memptr();
  209. const uword n_elem = P.get_n_elem();
  210. typename Proxy<T1>::ea_type Pea = P.get_ea();
  211. for(uword i=0; i < n_elem; ++i)
  212. {
  213. out_mem[i] = std::conj(Pea[i]);
  214. }
  215. }
  216. else // aliasing
  217. {
  218. Mat<eT> out2(n_cols, n_rows);
  219. eT* out_mem = out2.memptr();
  220. const uword n_elem = P.get_n_elem();
  221. typename Proxy<T1>::ea_type Pea = P.get_ea();
  222. for(uword i=0; i < n_elem; ++i)
  223. {
  224. out_mem[i] = std::conj(Pea[i]);
  225. }
  226. out.steal_mem(out2);
  227. }
  228. }
  229. else
  230. {
  231. if(is_alias == false)
  232. {
  233. out.set_size(n_cols, n_rows);
  234. eT* outptr = out.memptr();
  235. for(uword k=0; k < n_rows; ++k)
  236. {
  237. for(uword j=0; j < n_cols; ++j)
  238. {
  239. (*outptr) = std::conj(P.at(k,j));
  240. outptr++;
  241. }
  242. }
  243. }
  244. else // aliasing
  245. {
  246. Mat<eT> out2(n_cols, n_rows);
  247. eT* out2ptr = out2.memptr();
  248. for(uword k=0; k < n_rows; ++k)
  249. {
  250. for(uword j=0; j < n_cols; ++j)
  251. {
  252. (*out2ptr) = std::conj(P.at(k,j));
  253. out2ptr++;
  254. }
  255. }
  256. out.steal_mem(out2);
  257. }
  258. }
  259. }
  260. template<typename T1>
  261. arma_hot
  262. inline
  263. void
  264. op_htrans::apply_direct(Mat<typename T1::elem_type>& out, const T1& X)
  265. {
  266. arma_extra_debug_sigprint();
  267. // allow detection of in-place transpose
  268. if(is_Mat<T1>::value || is_Mat<typename Proxy<T1>::stored_type>::value)
  269. {
  270. const unwrap<T1> U(X);
  271. op_htrans::apply_mat(out, U.M);
  272. }
  273. else
  274. {
  275. op_htrans::apply_proxy(out, X);
  276. }
  277. }
  278. template<typename T1>
  279. arma_hot
  280. inline
  281. void
  282. op_htrans::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_htrans>& in, const typename arma_not_cx<typename T1::elem_type>::result* junk)
  283. {
  284. arma_extra_debug_sigprint();
  285. arma_ignore(junk);
  286. op_strans::apply_direct(out, in.m);
  287. }
  288. template<typename T1>
  289. arma_hot
  290. inline
  291. void
  292. op_htrans::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_htrans>& in, const typename arma_cx_only<typename T1::elem_type>::result* junk)
  293. {
  294. arma_extra_debug_sigprint();
  295. arma_ignore(junk);
  296. op_htrans::apply_direct(out, in.m);
  297. }
  298. template<typename T1>
  299. arma_hot
  300. inline
  301. void
  302. op_htrans::apply(Mat<typename T1::elem_type>& out, const Op< Op<T1, op_trimat>, op_htrans>& in)
  303. {
  304. arma_extra_debug_sigprint();
  305. typedef typename T1::elem_type eT;
  306. const unwrap<T1> tmp(in.m.m);
  307. const Mat<eT>& A = tmp.M;
  308. const bool upper = in.m.aux_uword_a;
  309. op_trimat::apply_htrans(out, A, upper);
  310. }
  311. //
  312. // op_htrans2
  313. template<typename T1>
  314. inline
  315. void
  316. op_htrans2::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_htrans2>& in, const typename arma_not_cx<typename T1::elem_type>::result* junk)
  317. {
  318. arma_extra_debug_sigprint();
  319. arma_ignore(junk);
  320. op_strans::apply_direct(out, in.m);
  321. arrayops::inplace_mul(out.memptr(), in.aux, out.n_elem);
  322. }
  323. template<typename T1>
  324. inline
  325. void
  326. op_htrans2::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_htrans2>& in, const typename arma_cx_only<typename T1::elem_type>::result* junk)
  327. {
  328. arma_extra_debug_sigprint();
  329. arma_ignore(junk);
  330. op_htrans::apply_direct(out, in.m);
  331. arrayops::inplace_mul(out.memptr(), in.aux, out.n_elem);
  332. }
  333. //! @}