spop_diagmat_meat.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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_diagmat
  16. //! @{
  17. template<typename T1>
  18. inline
  19. void
  20. spop_diagmat::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_diagmat>& in)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. if(in.is_alias(out) == false)
  25. {
  26. spop_diagmat::apply_noalias(out, in.m);
  27. }
  28. else
  29. {
  30. SpMat<eT> tmp;
  31. spop_diagmat::apply_noalias(tmp, in.m);
  32. out.steal_mem(tmp);
  33. }
  34. }
  35. template<typename T1>
  36. inline
  37. void
  38. spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpBase<typename T1::elem_type, T1>& expr)
  39. {
  40. arma_extra_debug_sigprint();
  41. typedef typename T1::elem_type eT;
  42. const SpProxy<T1> P(expr.get_ref());
  43. const uword P_n_rows = P.get_n_rows();
  44. const uword P_n_cols = P.get_n_cols();
  45. const uword P_n_nz = P.get_n_nonzero();
  46. const bool P_is_vec = (P_n_rows == 1) || (P_n_cols == 1);
  47. if(P_is_vec) // generate a diagonal matrix out of a vector
  48. {
  49. const uword N = (P_n_rows == 1) ? P_n_cols : P_n_rows;
  50. out.zeros(N, N);
  51. if(P_n_nz == 0) { return; }
  52. typename SpProxy<T1>::const_iterator_type it = P.begin();
  53. if(P_n_cols == 1)
  54. {
  55. for(uword i=0; i < P_n_nz; ++i)
  56. {
  57. const uword row = it.row();
  58. out.at(row,row) = (*it);
  59. ++it;
  60. }
  61. }
  62. else
  63. if(P_n_rows == 1)
  64. {
  65. for(uword i=0; i < P_n_nz; ++i)
  66. {
  67. const uword col = it.col();
  68. out.at(col,col) = (*it);
  69. ++it;
  70. }
  71. }
  72. }
  73. else // generate a diagonal matrix out of a matrix
  74. {
  75. out.zeros(P_n_rows, P_n_cols);
  76. const uword N = (std::min)(P_n_rows, P_n_cols);
  77. if( (is_SpMat<typename SpProxy<T1>::stored_type>::value) && (P_n_nz >= 5*N) )
  78. {
  79. const unwrap_spmat<typename SpProxy<T1>::stored_type> U(P.Q);
  80. const SpMat<eT>& X = U.M;
  81. for(uword i=0; i < N; ++i)
  82. {
  83. const eT val = X.at(i,i); // use binary search
  84. if(val != eT(0)) { out.at(i,i) = val; }
  85. }
  86. }
  87. else
  88. {
  89. if(P_n_nz == 0) { return; }
  90. typename SpProxy<T1>::const_iterator_type it = P.begin();
  91. for(uword i=0; i < P_n_nz; ++i)
  92. {
  93. const uword row = it.row();
  94. const uword col = it.col();
  95. if(row == col) { out.at(row,row) = (*it); }
  96. ++it;
  97. }
  98. }
  99. }
  100. }
  101. template<typename T1, typename T2>
  102. inline
  103. void
  104. spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_plus>& expr)
  105. {
  106. arma_extra_debug_sigprint();
  107. typedef typename T1::elem_type eT;
  108. const unwrap_spmat<T1> UA(expr.A);
  109. const unwrap_spmat<T2> UB(expr.B);
  110. const SpMat<eT>& A = UA.M;
  111. const SpMat<eT>& B = UB.M;
  112. arma_debug_assert_same_size(A.n_rows, A.n_cols, B.n_rows, B.n_cols, "addition");
  113. const bool is_vec = (A.n_rows == 1) || (A.n_cols == 1);
  114. if(is_vec) // generate a diagonal matrix out of a vector
  115. {
  116. const uword N = (A.n_rows == 1) ? A.n_cols : A.n_rows;
  117. out.zeros(N,N);
  118. if(A.n_rows == 1)
  119. {
  120. for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(0,i) + B.at(0,i); }
  121. }
  122. else
  123. {
  124. for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(i,0) + B.at(i,0); }
  125. }
  126. }
  127. else // generate a diagonal matrix out of a matrix
  128. {
  129. SpMat<eT> AA; spop_diagmat::apply_noalias(AA, A);
  130. SpMat<eT> BB; spop_diagmat::apply_noalias(BB, B);
  131. out = AA + BB;
  132. }
  133. }
  134. template<typename T1, typename T2>
  135. inline
  136. void
  137. spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_minus>& expr)
  138. {
  139. arma_extra_debug_sigprint();
  140. typedef typename T1::elem_type eT;
  141. const unwrap_spmat<T1> UA(expr.A);
  142. const unwrap_spmat<T2> UB(expr.B);
  143. const SpMat<eT>& A = UA.M;
  144. const SpMat<eT>& B = UB.M;
  145. arma_debug_assert_same_size(A.n_rows, A.n_cols, B.n_rows, B.n_cols, "subtraction");
  146. const bool is_vec = (A.n_rows == 1) || (A.n_cols == 1);
  147. if(is_vec) // generate a diagonal matrix out of a vector
  148. {
  149. const uword N = (A.n_rows == 1) ? A.n_cols : A.n_rows;
  150. out.zeros(N,N);
  151. if(A.n_rows == 1)
  152. {
  153. for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(0,i) - B.at(0,i); }
  154. }
  155. else
  156. {
  157. for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(i,0) - B.at(i,0); }
  158. }
  159. }
  160. else // generate a diagonal matrix out of a matrix
  161. {
  162. SpMat<eT> AA; spop_diagmat::apply_noalias(AA, A);
  163. SpMat<eT> BB; spop_diagmat::apply_noalias(BB, B);
  164. out = AA - BB;
  165. }
  166. }
  167. template<typename T1, typename T2>
  168. inline
  169. void
  170. spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_schur>& expr)
  171. {
  172. arma_extra_debug_sigprint();
  173. typedef typename T1::elem_type eT;
  174. const unwrap_spmat<T1> UA(expr.A);
  175. const unwrap_spmat<T2> UB(expr.B);
  176. const SpMat<eT>& A = UA.M;
  177. const SpMat<eT>& B = UB.M;
  178. arma_debug_assert_same_size(A.n_rows, A.n_cols, B.n_rows, B.n_cols, "element-wise multiplication");
  179. const bool is_vec = (A.n_rows == 1) || (A.n_cols == 1);
  180. if(is_vec) // generate a diagonal matrix out of a vector
  181. {
  182. const uword N = (A.n_rows == 1) ? A.n_cols : A.n_rows;
  183. out.zeros(N,N);
  184. if(A.n_rows == 1)
  185. {
  186. for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(0,i) * B.at(0,i); }
  187. }
  188. else
  189. {
  190. for(uword i=0; i < N; ++i) { out.at(i,i) = A.at(i,0) * B.at(i,0); }
  191. }
  192. }
  193. else // generate a diagonal matrix out of a matrix
  194. {
  195. SpMat<eT> AA; spop_diagmat::apply_noalias(AA, A);
  196. SpMat<eT> BB; spop_diagmat::apply_noalias(BB, B);
  197. out = AA % BB;
  198. }
  199. }
  200. template<typename T1, typename T2>
  201. inline
  202. void
  203. spop_diagmat::apply_noalias(SpMat<typename T1::elem_type>& out, const SpGlue<T1,T2,spglue_times>& expr)
  204. {
  205. arma_extra_debug_sigprint();
  206. typedef typename T1::elem_type eT;
  207. const unwrap_spmat<T1> UA(expr.A);
  208. const unwrap_spmat<T2> UB(expr.B);
  209. const SpMat<eT>& A = UA.M;
  210. const SpMat<eT>& B = UB.M;
  211. arma_debug_assert_mul_size(A.n_rows, A.n_cols, B.n_rows, B.n_cols, "matrix multiplication");
  212. const uword C_n_rows = A.n_rows;
  213. const uword C_n_cols = B.n_cols;
  214. const bool is_vec = (C_n_rows == 1) || (C_n_cols == 1);
  215. if(is_vec) // generate a diagonal matrix out of a vector
  216. {
  217. const SpMat<eT> C = A*B;
  218. spop_diagmat::apply_noalias(out, C);
  219. }
  220. else // generate a diagonal matrix out of a matrix
  221. {
  222. const uword N = (std::min)(C_n_rows, C_n_cols);
  223. if( (A.n_nonzero >= 5*N) || (B.n_nonzero >= 5*N) )
  224. {
  225. out.zeros(C_n_rows, C_n_cols);
  226. for(uword k=0; k < N; ++k)
  227. {
  228. typename SpMat<eT>::const_col_iterator B_it = B.begin_col_no_sync(k);
  229. typename SpMat<eT>::const_col_iterator B_it_end = B.end_col_no_sync(k);
  230. eT acc = eT(0);
  231. while(B_it != B_it_end)
  232. {
  233. const eT B_val = (*B_it);
  234. const uword i = B_it.row();
  235. acc += A.at(k,i) * B_val;
  236. ++B_it;
  237. }
  238. out(k,k) = acc;
  239. }
  240. }
  241. else
  242. {
  243. const SpMat<eT> C = A*B;
  244. spop_diagmat::apply_noalias(out, C);
  245. }
  246. }
  247. }
  248. //
  249. //
  250. template<typename T1>
  251. inline
  252. void
  253. spop_diagmat2::apply(SpMat<typename T1::elem_type>& out, const SpOp<T1, spop_diagmat2>& in)
  254. {
  255. arma_extra_debug_sigprint();
  256. typedef typename T1::elem_type eT;
  257. const uword row_offset = in.aux_uword_a;
  258. const uword col_offset = in.aux_uword_b;
  259. const unwrap_spmat<T1> U(in.m);
  260. if(U.is_alias(out))
  261. {
  262. SpMat<eT> tmp;
  263. spop_diagmat2::apply_noalias(tmp, U.M, row_offset, col_offset);
  264. out.steal_mem(tmp);
  265. }
  266. else
  267. {
  268. spop_diagmat2::apply_noalias(out, U.M, row_offset, col_offset);
  269. }
  270. }
  271. template<typename eT>
  272. inline
  273. void
  274. spop_diagmat2::apply_noalias(SpMat<eT>& out, const SpMat<eT>& X, const uword row_offset, const uword col_offset)
  275. {
  276. arma_extra_debug_sigprint();
  277. const uword n_rows = X.n_rows;
  278. const uword n_cols = X.n_cols;
  279. const uword n_elem = X.n_elem;
  280. if(n_elem == 0) { out.reset(); return; }
  281. const bool X_is_vec = (n_rows == 1) || (n_cols == 1);
  282. if(X_is_vec) // generate a diagonal matrix out of a vector
  283. {
  284. const uword n_pad = (std::max)(row_offset, col_offset);
  285. out.zeros(n_elem + n_pad, n_elem + n_pad);
  286. const uword X_n_nz = X.n_nonzero;
  287. if(X_n_nz == 0) { return; }
  288. typename SpMat<eT>::const_iterator it = X.begin();
  289. if(n_cols == 1)
  290. {
  291. for(uword i=0; i < X_n_nz; ++i)
  292. {
  293. const uword row = it.row();
  294. out.at(row_offset + row, col_offset + row) = (*it);
  295. ++it;
  296. }
  297. }
  298. else
  299. if(n_rows == 1)
  300. {
  301. for(uword i=0; i < X_n_nz; ++i)
  302. {
  303. const uword col = it.col();
  304. out.at(row_offset + col, col_offset + col) = (*it);
  305. ++it;
  306. }
  307. }
  308. }
  309. else // generate a diagonal matrix out of a matrix
  310. {
  311. arma_debug_check
  312. (
  313. ((row_offset > 0) && (row_offset >= n_rows)) || ((col_offset > 0) && (col_offset >= n_cols)),
  314. "diagmat(): requested diagonal out of bounds"
  315. );
  316. out.zeros(n_rows, n_cols);
  317. if(X.n_nonzero == 0) { return; }
  318. const uword N = (std::min)(n_rows - row_offset, n_cols - col_offset);
  319. for(uword i=0; i<N; ++i)
  320. {
  321. const uword row = i + row_offset;
  322. const uword col = i + col_offset;
  323. const eT val = X.at(row,col);
  324. if(val != eT(0)) { out.at(row,col) = val; }
  325. }
  326. }
  327. }
  328. //! @}