spop_misc_meat.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 spop_misc
  16. //! @{
  17. namespace priv
  18. {
  19. template<typename eT>
  20. struct functor_scalar_times
  21. {
  22. const eT k;
  23. functor_scalar_times(const eT in_k) : k(in_k) {}
  24. arma_inline eT operator()(const eT val) const { return val * k; }
  25. };
  26. }
  27. template<typename T1>
  28. inline
  29. void
  30. spop_scalar_times::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_scalar_times>& in)
  31. {
  32. arma_extra_debug_sigprint();
  33. typedef typename T1::elem_type eT;
  34. if(in.aux != eT(0))
  35. {
  36. out.init_xform(in.m, priv::functor_scalar_times<eT>(in.aux));
  37. }
  38. else
  39. {
  40. const SpProxy<T1> P(in.m);
  41. out.zeros( P.get_n_rows(), P.get_n_cols() );
  42. }
  43. }
  44. namespace priv
  45. {
  46. template<typename T>
  47. struct functor_cx_scalar_times
  48. {
  49. typedef std::complex<T> out_eT;
  50. const out_eT k;
  51. functor_cx_scalar_times(const out_eT in_k) : k(in_k) {}
  52. arma_inline out_eT operator()(const T val) const { return val * k; }
  53. };
  54. }
  55. template<typename T1>
  56. inline
  57. void
  58. spop_cx_scalar_times::apply(SpMat< std::complex<typename T1::pod_type> >& out, const mtSpOp< std::complex<typename T1::pod_type>, T1, spop_cx_scalar_times >& in)
  59. {
  60. arma_extra_debug_sigprint();
  61. typedef typename T1::pod_type T;
  62. typedef typename std::complex<T> out_eT;
  63. if(in.aux_out_eT != out_eT(0))
  64. {
  65. out.init_xform_mt(in.m, priv::functor_cx_scalar_times<T>(in.aux_out_eT));
  66. }
  67. else
  68. {
  69. const SpProxy<T1> P(in.m);
  70. out.zeros( P.get_n_rows(), P.get_n_cols() );
  71. }
  72. }
  73. namespace priv
  74. {
  75. struct functor_square
  76. {
  77. template<typename eT>
  78. arma_inline eT operator()(const eT val) const { return val*val; }
  79. };
  80. }
  81. template<typename T1>
  82. inline
  83. void
  84. spop_square::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_square>& in)
  85. {
  86. arma_extra_debug_sigprint();
  87. out.init_xform(in.m, priv::functor_square());
  88. }
  89. namespace priv
  90. {
  91. struct functor_sqrt
  92. {
  93. template<typename eT>
  94. arma_inline eT operator()(const eT val) const { return eop_aux::sqrt(val); }
  95. };
  96. }
  97. template<typename T1>
  98. inline
  99. void
  100. spop_sqrt::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_sqrt>& in)
  101. {
  102. arma_extra_debug_sigprint();
  103. out.init_xform(in.m, priv::functor_sqrt());
  104. }
  105. namespace priv
  106. {
  107. struct functor_abs
  108. {
  109. template<typename eT>
  110. arma_inline eT operator()(const eT val) const { return eop_aux::arma_abs(val); }
  111. };
  112. }
  113. template<typename T1>
  114. inline
  115. void
  116. spop_abs::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_abs>& in)
  117. {
  118. arma_extra_debug_sigprint();
  119. out.init_xform(in.m, priv::functor_abs());
  120. }
  121. namespace priv
  122. {
  123. struct functor_cx_abs
  124. {
  125. template<typename T>
  126. arma_inline T operator()(const std::complex<T>& val) const { return std::abs(val); }
  127. };
  128. }
  129. template<typename T1>
  130. inline
  131. void
  132. spop_cx_abs::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_cx_abs>& in)
  133. {
  134. arma_extra_debug_sigprint();
  135. out.init_xform_mt(in.m, priv::functor_cx_abs());
  136. }
  137. namespace priv
  138. {
  139. struct functor_arg
  140. {
  141. template<typename eT>
  142. arma_inline eT operator()(const eT val) const { return arma_arg<eT>::eval(val); }
  143. };
  144. }
  145. template<typename T1>
  146. inline
  147. void
  148. spop_arg::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_arg>& in)
  149. {
  150. arma_extra_debug_sigprint();
  151. out.init_xform(in.m, priv::functor_arg());
  152. }
  153. namespace priv
  154. {
  155. struct functor_cx_arg
  156. {
  157. template<typename T>
  158. arma_inline T operator()(const std::complex<T>& val) const { return std::arg(val); }
  159. };
  160. }
  161. template<typename T1>
  162. inline
  163. void
  164. spop_cx_arg::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_cx_arg>& in)
  165. {
  166. arma_extra_debug_sigprint();
  167. out.init_xform_mt(in.m, priv::functor_cx_arg());
  168. }
  169. namespace priv
  170. {
  171. struct functor_real
  172. {
  173. template<typename T>
  174. arma_inline T operator()(const std::complex<T>& val) const { return val.real(); }
  175. };
  176. }
  177. template<typename T1>
  178. inline
  179. void
  180. spop_real::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_real>& in)
  181. {
  182. arma_extra_debug_sigprint();
  183. out.init_xform_mt(in.m, priv::functor_real());
  184. }
  185. namespace priv
  186. {
  187. struct functor_imag
  188. {
  189. template<typename T>
  190. arma_inline T operator()(const std::complex<T>& val) const { return val.imag(); }
  191. };
  192. }
  193. template<typename T1>
  194. inline
  195. void
  196. spop_imag::apply(SpMat<typename T1::pod_type>& out, const mtSpOp<typename T1::pod_type, T1, spop_imag>& in)
  197. {
  198. arma_extra_debug_sigprint();
  199. out.init_xform_mt(in.m, priv::functor_imag());
  200. }
  201. namespace priv
  202. {
  203. struct functor_conj
  204. {
  205. template<typename eT>
  206. arma_inline eT operator()(const eT val) const { return eop_aux::conj(val); }
  207. };
  208. }
  209. template<typename T1>
  210. inline
  211. void
  212. spop_conj::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_conj>& in)
  213. {
  214. arma_extra_debug_sigprint();
  215. out.init_xform(in.m, priv::functor_conj());
  216. }
  217. template<typename T1>
  218. inline
  219. void
  220. spop_repelem::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_repelem>& in)
  221. {
  222. arma_extra_debug_sigprint();
  223. typedef typename T1::elem_type eT;
  224. const unwrap_spmat<T1> U(in.m);
  225. const SpMat<eT>& X = U.M;
  226. const uword copies_per_row = in.aux_uword_a;
  227. const uword copies_per_col = in.aux_uword_b;
  228. const uword out_n_rows = X.n_rows * copies_per_row;
  229. const uword out_n_cols = X.n_cols * copies_per_col;
  230. const uword out_nnz = X.n_nonzero * copies_per_row * copies_per_col;
  231. if( (out_n_rows > 0) && (out_n_cols > 0) && (out_nnz > 0) )
  232. {
  233. umat locs(2, out_nnz);
  234. Col<eT> vals( out_nnz);
  235. uword* locs_mem = locs.memptr();
  236. eT* vals_mem = vals.memptr();
  237. typename SpMat<eT>::const_iterator X_it = X.begin();
  238. typename SpMat<eT>::const_iterator X_end = X.end();
  239. for(; X_it != X_end; ++X_it)
  240. {
  241. const uword col_base = copies_per_col * X_it.col();
  242. const uword row_base = copies_per_row * X_it.row();
  243. const eT X_val = (*X_it);
  244. for(uword cols = 0; cols < copies_per_col; cols++)
  245. for(uword rows = 0; rows < copies_per_row; rows++)
  246. {
  247. (*locs_mem) = row_base + rows; ++locs_mem;
  248. (*locs_mem) = col_base + cols; ++locs_mem;
  249. (*vals_mem) = X_val; ++vals_mem;
  250. }
  251. }
  252. out = SpMat<eT>(locs, vals, out_n_rows, out_n_cols);
  253. }
  254. else
  255. {
  256. out.zeros(out_n_rows, out_n_cols);
  257. }
  258. }
  259. template<typename T1>
  260. inline
  261. void
  262. spop_reshape::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_reshape>& in)
  263. {
  264. arma_extra_debug_sigprint();
  265. out = in.m;
  266. out.reshape(in.aux_uword_a, in.aux_uword_b);
  267. }
  268. template<typename T1>
  269. inline
  270. void
  271. spop_resize::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_resize>& in)
  272. {
  273. arma_extra_debug_sigprint();
  274. out = in.m;
  275. out.resize(in.aux_uword_a, in.aux_uword_b);
  276. }
  277. namespace priv
  278. {
  279. struct functor_floor
  280. {
  281. template<typename eT>
  282. arma_inline eT operator()(const eT val) const { return eop_aux::floor(val); }
  283. };
  284. }
  285. template<typename T1>
  286. inline
  287. void
  288. spop_floor::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_floor>& in)
  289. {
  290. arma_extra_debug_sigprint();
  291. out.init_xform(in.m, priv::functor_floor());
  292. }
  293. namespace priv
  294. {
  295. struct functor_ceil
  296. {
  297. template<typename eT>
  298. arma_inline eT operator()(const eT val) const { return eop_aux::ceil(val); }
  299. };
  300. }
  301. template<typename T1>
  302. inline
  303. void
  304. spop_ceil::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_ceil>& in)
  305. {
  306. arma_extra_debug_sigprint();
  307. out.init_xform(in.m, priv::functor_ceil());
  308. }
  309. namespace priv
  310. {
  311. struct functor_round
  312. {
  313. template<typename eT>
  314. arma_inline eT operator()(const eT val) const { return eop_aux::round(val); }
  315. };
  316. }
  317. template<typename T1>
  318. inline
  319. void
  320. spop_round::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_round>& in)
  321. {
  322. arma_extra_debug_sigprint();
  323. out.init_xform(in.m, priv::functor_round());
  324. }
  325. namespace priv
  326. {
  327. struct functor_trunc
  328. {
  329. template<typename eT>
  330. arma_inline eT operator()(const eT val) const { return eop_aux::trunc(val); }
  331. };
  332. }
  333. template<typename T1>
  334. inline
  335. void
  336. spop_trunc::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_trunc>& in)
  337. {
  338. arma_extra_debug_sigprint();
  339. out.init_xform(in.m, priv::functor_trunc());
  340. }
  341. namespace priv
  342. {
  343. struct functor_sign
  344. {
  345. template<typename eT>
  346. arma_inline eT operator()(const eT val) const { return arma_sign(val); }
  347. };
  348. }
  349. template<typename T1>
  350. inline
  351. void
  352. spop_sign::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_sign>& in)
  353. {
  354. arma_extra_debug_sigprint();
  355. out.init_xform(in.m, priv::functor_sign());
  356. }
  357. template<typename T1>
  358. inline
  359. void
  360. spop_diagvec::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_diagvec>& in)
  361. {
  362. arma_extra_debug_sigprint();
  363. typedef typename T1::elem_type eT;
  364. const unwrap_spmat<T1> U(in.m);
  365. const SpMat<eT>& X = U.M;
  366. const uword a = in.aux_uword_a;
  367. const uword b = in.aux_uword_b;
  368. const uword row_offset = (b > 0) ? a : 0;
  369. const uword col_offset = (b == 0) ? a : 0;
  370. arma_debug_check
  371. (
  372. ((row_offset > 0) && (row_offset >= X.n_rows)) || ((col_offset > 0) && (col_offset >= X.n_cols)),
  373. "diagvec(): requested diagonal out of bounds"
  374. );
  375. const uword len = (std::min)(X.n_rows - row_offset, X.n_cols - col_offset);
  376. Col<eT> cache(len);
  377. eT* cache_mem = cache.memptr();
  378. uword n_nonzero = 0;
  379. for(uword i=0; i < len; ++i)
  380. {
  381. const eT val = X.at(i + row_offset, i + col_offset);
  382. cache_mem[i] = val;
  383. n_nonzero += (val != eT(0)) ? uword(1) : uword(0);
  384. }
  385. out.reserve(len, 1, n_nonzero);
  386. uword count = 0;
  387. for(uword i=0; i < len; ++i)
  388. {
  389. const eT val = cache_mem[i];
  390. if(val != eT(0))
  391. {
  392. access::rw(out.row_indices[count]) = i;
  393. access::rw(out.values[count]) = val;
  394. ++count;
  395. }
  396. }
  397. access::rw(out.col_ptrs[0]) = 0;
  398. access::rw(out.col_ptrs[1]) = n_nonzero;
  399. }
  400. template<typename T1>
  401. inline
  402. void
  403. spop_flipud::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_flipud>& in)
  404. {
  405. arma_extra_debug_sigprint();
  406. out = reverse(in.m, 0);
  407. }
  408. template<typename T1>
  409. inline
  410. void
  411. spop_fliplr::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1,spop_fliplr>& in)
  412. {
  413. arma_extra_debug_sigprint();
  414. out = reverse(in.m, 1);
  415. }
  416. //! @}