op_misc_meat.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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_misc
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. op_real::apply( Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_type, T1, op_real>& X )
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::pod_type T;
  24. const Proxy<T1> P(X.m);
  25. const uword n_rows = P.get_n_rows();
  26. const uword n_cols = P.get_n_cols();
  27. out.set_size(n_rows, n_cols);
  28. T* out_mem = out.memptr();
  29. if(Proxy<T1>::use_at == false)
  30. {
  31. typedef typename Proxy<T1>::ea_type ea_type;
  32. const uword n_elem = P.get_n_elem();
  33. ea_type A = P.get_ea();
  34. for(uword i=0; i < n_elem; ++i)
  35. {
  36. out_mem[i] = std::real( A[i] );
  37. }
  38. }
  39. else
  40. {
  41. for(uword col=0; col < n_cols; ++col)
  42. for(uword row=0; row < n_rows; ++row)
  43. {
  44. *out_mem = std::real( P.at(row,col) );
  45. out_mem++;
  46. }
  47. }
  48. }
  49. template<typename T1>
  50. inline
  51. void
  52. op_real::apply( Cube<typename T1::pod_type>& out, const mtOpCube<typename T1::pod_type, T1, op_real>& X )
  53. {
  54. arma_extra_debug_sigprint();
  55. typedef typename T1::pod_type T;
  56. const ProxyCube<T1> P(X.m);
  57. const uword n_rows = P.get_n_rows();
  58. const uword n_cols = P.get_n_cols();
  59. const uword n_slices = P.get_n_slices();
  60. out.set_size(n_rows, n_cols, n_slices);
  61. T* out_mem = out.memptr();
  62. if(ProxyCube<T1>::use_at == false)
  63. {
  64. typedef typename ProxyCube<T1>::ea_type ea_type;
  65. const uword n_elem = P.get_n_elem();
  66. ea_type A = P.get_ea();
  67. for(uword i=0; i < n_elem; ++i)
  68. {
  69. out_mem[i] = std::real( A[i] );
  70. }
  71. }
  72. else
  73. {
  74. for(uword slice=0; slice < n_slices; ++slice)
  75. for(uword col=0; col < n_cols; ++col )
  76. for(uword row=0; row < n_rows; ++row )
  77. {
  78. *out_mem = std::real( P.at(row,col,slice) );
  79. out_mem++;
  80. }
  81. }
  82. }
  83. template<typename T1>
  84. inline
  85. void
  86. op_imag::apply( Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_type, T1, op_imag>& X )
  87. {
  88. arma_extra_debug_sigprint();
  89. typedef typename T1::pod_type T;
  90. const Proxy<T1> P(X.m);
  91. const uword n_rows = P.get_n_rows();
  92. const uword n_cols = P.get_n_cols();
  93. out.set_size(n_rows, n_cols);
  94. T* out_mem = out.memptr();
  95. if(Proxy<T1>::use_at == false)
  96. {
  97. typedef typename Proxy<T1>::ea_type ea_type;
  98. const uword n_elem = P.get_n_elem();
  99. ea_type A = P.get_ea();
  100. for(uword i=0; i < n_elem; ++i)
  101. {
  102. out_mem[i] = std::imag( A[i] );
  103. }
  104. }
  105. else
  106. {
  107. for(uword col=0; col < n_cols; ++col)
  108. for(uword row=0; row < n_rows; ++row)
  109. {
  110. *out_mem = std::imag( P.at(row,col) );
  111. out_mem++;
  112. }
  113. }
  114. }
  115. template<typename T1>
  116. inline
  117. void
  118. op_imag::apply( Cube<typename T1::pod_type>& out, const mtOpCube<typename T1::pod_type, T1, op_imag>& X )
  119. {
  120. arma_extra_debug_sigprint();
  121. typedef typename T1::pod_type T;
  122. const ProxyCube<T1> P(X.m);
  123. const uword n_rows = P.get_n_rows();
  124. const uword n_cols = P.get_n_cols();
  125. const uword n_slices = P.get_n_slices();
  126. out.set_size(n_rows, n_cols, n_slices);
  127. T* out_mem = out.memptr();
  128. if(ProxyCube<T1>::use_at == false)
  129. {
  130. typedef typename ProxyCube<T1>::ea_type ea_type;
  131. const uword n_elem = P.get_n_elem();
  132. ea_type A = P.get_ea();
  133. for(uword i=0; i < n_elem; ++i)
  134. {
  135. out_mem[i] = std::imag( A[i] );
  136. }
  137. }
  138. else
  139. {
  140. for(uword slice=0; slice < n_slices; ++slice)
  141. for(uword col=0; col < n_cols; ++col )
  142. for(uword row=0; row < n_rows; ++row )
  143. {
  144. *out_mem = std::imag( P.at(row,col,slice) );
  145. out_mem++;
  146. }
  147. }
  148. }
  149. template<typename T1>
  150. inline
  151. void
  152. op_abs::apply( Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_type, T1, op_abs>& X )
  153. {
  154. arma_extra_debug_sigprint();
  155. typedef typename T1::pod_type T;
  156. const Proxy<T1> P(X.m);
  157. const uword n_rows = P.get_n_rows();
  158. const uword n_cols = P.get_n_cols();
  159. out.set_size(n_rows, n_cols);
  160. T* out_mem = out.memptr();
  161. if(Proxy<T1>::use_at == false)
  162. {
  163. typedef typename Proxy<T1>::ea_type ea_type;
  164. const uword n_elem = P.get_n_elem();
  165. ea_type A = P.get_ea();
  166. #if defined(ARMA_USE_OPENMP)
  167. {
  168. const int n_threads = mp_thread_limit::get();
  169. #pragma omp parallel for schedule(static) num_threads(n_threads)
  170. for(uword i=0; i < n_elem; ++i)
  171. {
  172. out_mem[i] = std::abs( A[i] );
  173. }
  174. }
  175. #else
  176. {
  177. for(uword i=0; i < n_elem; ++i)
  178. {
  179. out_mem[i] = std::abs( A[i] );
  180. }
  181. }
  182. #endif
  183. }
  184. else
  185. {
  186. for(uword col=0; col < n_cols; ++col)
  187. for(uword row=0; row < n_rows; ++row)
  188. {
  189. *out_mem = std::abs( P.at(row,col) );
  190. out_mem++;
  191. }
  192. }
  193. }
  194. template<typename T1>
  195. inline
  196. void
  197. op_abs::apply( Cube<typename T1::pod_type>& out, const mtOpCube<typename T1::pod_type, T1, op_abs>& X )
  198. {
  199. arma_extra_debug_sigprint();
  200. typedef typename T1::pod_type T;
  201. const ProxyCube<T1> P(X.m);
  202. const uword n_rows = P.get_n_rows();
  203. const uword n_cols = P.get_n_cols();
  204. const uword n_slices = P.get_n_slices();
  205. out.set_size(n_rows, n_cols, n_slices);
  206. T* out_mem = out.memptr();
  207. if(ProxyCube<T1>::use_at == false)
  208. {
  209. typedef typename ProxyCube<T1>::ea_type ea_type;
  210. const uword n_elem = P.get_n_elem();
  211. ea_type A = P.get_ea();
  212. #if defined(ARMA_USE_OPENMP)
  213. {
  214. const int n_threads = mp_thread_limit::get();
  215. #pragma omp parallel for schedule(static) num_threads(n_threads)
  216. for(uword i=0; i < n_elem; ++i)
  217. {
  218. out_mem[i] = std::abs( A[i] );
  219. }
  220. }
  221. #else
  222. {
  223. for(uword i=0; i < n_elem; ++i)
  224. {
  225. out_mem[i] = std::abs( A[i] );
  226. }
  227. }
  228. #endif
  229. }
  230. else
  231. {
  232. for(uword slice=0; slice < n_slices; ++slice)
  233. for(uword col=0; col < n_cols; ++col )
  234. for(uword row=0; row < n_rows; ++row )
  235. {
  236. *out_mem = std::abs( P.at(row,col,slice) );
  237. out_mem++;
  238. }
  239. }
  240. }
  241. template<typename T1>
  242. inline
  243. void
  244. op_arg::apply( Mat<typename T1::pod_type>& out, const mtOp<typename T1::pod_type, T1, op_arg>& X )
  245. {
  246. arma_extra_debug_sigprint();
  247. typedef typename T1::elem_type eT;
  248. typedef typename T1::pod_type T;
  249. const Proxy<T1> P(X.m);
  250. const uword n_rows = P.get_n_rows();
  251. const uword n_cols = P.get_n_cols();
  252. out.set_size(n_rows, n_cols);
  253. T* out_mem = out.memptr();
  254. if(Proxy<T1>::use_at == false)
  255. {
  256. typedef typename Proxy<T1>::ea_type ea_type;
  257. const uword n_elem = P.get_n_elem();
  258. ea_type A = P.get_ea();
  259. for(uword i=0; i < n_elem; ++i)
  260. {
  261. out_mem[i] = arma_arg<eT>::eval( A[i] );
  262. }
  263. }
  264. else
  265. {
  266. for(uword col=0; col < n_cols; ++col)
  267. for(uword row=0; row < n_rows; ++row)
  268. {
  269. *out_mem = arma_arg<eT>::eval( P.at(row,col) );
  270. out_mem++;
  271. }
  272. }
  273. }
  274. template<typename T1>
  275. inline
  276. void
  277. op_arg::apply( Cube<typename T1::pod_type>& out, const mtOpCube<typename T1::pod_type, T1, op_arg>& X )
  278. {
  279. arma_extra_debug_sigprint();
  280. typedef typename T1::elem_type eT;
  281. typedef typename T1::pod_type T;
  282. const ProxyCube<T1> P(X.m);
  283. const uword n_rows = P.get_n_rows();
  284. const uword n_cols = P.get_n_cols();
  285. const uword n_slices = P.get_n_slices();
  286. out.set_size(n_rows, n_cols, n_slices);
  287. T* out_mem = out.memptr();
  288. if(ProxyCube<T1>::use_at == false)
  289. {
  290. typedef typename ProxyCube<T1>::ea_type ea_type;
  291. const uword n_elem = P.get_n_elem();
  292. ea_type A = P.get_ea();
  293. for(uword i=0; i < n_elem; ++i)
  294. {
  295. out_mem[i] = arma_arg<eT>::eval( A[i] );
  296. }
  297. }
  298. else
  299. {
  300. for(uword slice=0; slice < n_slices; ++slice)
  301. for(uword col=0; col < n_cols; ++col )
  302. for(uword row=0; row < n_rows; ++row )
  303. {
  304. *out_mem = arma_arg<eT>::eval( P.at(row,col,slice) );
  305. out_mem++;
  306. }
  307. }
  308. }
  309. //! @}