op_trimat_meat.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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_trimat
  16. //! @{
  17. template<typename eT>
  18. inline
  19. void
  20. op_trimat::fill_zeros(Mat<eT>& out, const bool upper)
  21. {
  22. arma_extra_debug_sigprint();
  23. const uword N = out.n_rows;
  24. if(upper)
  25. {
  26. // upper triangular: set all elements below the diagonal to zero
  27. for(uword i=0; i<N; ++i)
  28. {
  29. eT* data = out.colptr(i);
  30. arrayops::inplace_set( &data[i+1], eT(0), (N-(i+1)) );
  31. }
  32. }
  33. else
  34. {
  35. // lower triangular: set all elements above the diagonal to zero
  36. for(uword i=1; i<N; ++i)
  37. {
  38. eT* data = out.colptr(i);
  39. arrayops::inplace_set( data, eT(0), i );
  40. }
  41. }
  42. }
  43. template<typename T1>
  44. inline
  45. void
  46. op_trimat::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_trimat>& in)
  47. {
  48. arma_extra_debug_sigprint();
  49. typedef typename T1::elem_type eT;
  50. const unwrap<T1> tmp(in.m);
  51. const Mat<eT>& A = tmp.M;
  52. arma_debug_check( (A.is_square() == false), "trimatu()/trimatl(): given matrix must be square sized" );
  53. const uword N = A.n_rows;
  54. const bool upper = (in.aux_uword_a == 0);
  55. if(&out != &A)
  56. {
  57. out.copy_size(A);
  58. if(upper)
  59. {
  60. // upper triangular: copy the diagonal and the elements above the diagonal
  61. for(uword i=0; i<N; ++i)
  62. {
  63. const eT* A_data = A.colptr(i);
  64. eT* out_data = out.colptr(i);
  65. arrayops::copy( out_data, A_data, i+1 );
  66. }
  67. }
  68. else
  69. {
  70. // lower triangular: copy the diagonal and the elements below the diagonal
  71. for(uword i=0; i<N; ++i)
  72. {
  73. const eT* A_data = A.colptr(i);
  74. eT* out_data = out.colptr(i);
  75. arrayops::copy( &out_data[i], &A_data[i], N-i );
  76. }
  77. }
  78. }
  79. op_trimat::fill_zeros(out, upper);
  80. }
  81. template<typename T1>
  82. inline
  83. void
  84. op_trimat::apply(Mat<typename T1::elem_type>& out, const Op<Op<T1, op_htrans>, op_trimat>& in)
  85. {
  86. arma_extra_debug_sigprint();
  87. typedef typename T1::elem_type eT;
  88. const unwrap<T1> tmp(in.m.m);
  89. const Mat<eT>& A = tmp.M;
  90. const bool upper = (in.aux_uword_a == 0);
  91. op_trimat::apply_htrans(out, A, upper);
  92. }
  93. template<typename eT>
  94. inline
  95. void
  96. op_trimat::apply_htrans
  97. (
  98. Mat<eT>& out,
  99. const Mat<eT>& A,
  100. const bool upper,
  101. const typename arma_not_cx<eT>::result* junk
  102. )
  103. {
  104. arma_extra_debug_sigprint();
  105. arma_ignore(junk);
  106. // This specialisation is for trimatl(trans(X)) = trans(trimatu(X)) and also
  107. // trimatu(trans(X)) = trans(trimatl(X)). We want to avoid the creation of an
  108. // extra temporary.
  109. // It doesn't matter if the input and output matrices are the same; we will
  110. // pull data from the upper or lower triangular to the lower or upper
  111. // triangular (respectively) and then set the rest to 0, so overwriting issues
  112. // aren't present.
  113. arma_debug_check( (A.is_square() == false), "trimatu()/trimatl(): given matrix must be square sized" );
  114. const uword N = A.n_rows;
  115. if(&out != &A)
  116. {
  117. out.copy_size(A);
  118. }
  119. // We can't really get away with any array copy operations here,
  120. // unfortunately...
  121. if(upper)
  122. {
  123. // Upper triangular: but since we're transposing, we're taking the lower
  124. // triangular and putting it in the upper half.
  125. for(uword row = 0; row < N; ++row)
  126. {
  127. eT* out_colptr = out.colptr(row);
  128. for(uword col = 0; col <= row; ++col)
  129. {
  130. //out.at(col, row) = A.at(row, col);
  131. out_colptr[col] = A.at(row, col);
  132. }
  133. }
  134. }
  135. else
  136. {
  137. // Lower triangular: but since we're transposing, we're taking the upper
  138. // triangular and putting it in the lower half.
  139. for(uword row = 0; row < N; ++row)
  140. {
  141. for(uword col = row; col < N; ++col)
  142. {
  143. out.at(col, row) = A.at(row, col);
  144. }
  145. }
  146. }
  147. op_trimat::fill_zeros(out, upper);
  148. }
  149. template<typename eT>
  150. inline
  151. void
  152. op_trimat::apply_htrans
  153. (
  154. Mat<eT>& out,
  155. const Mat<eT>& A,
  156. const bool upper,
  157. const typename arma_cx_only<eT>::result* junk
  158. )
  159. {
  160. arma_extra_debug_sigprint();
  161. arma_ignore(junk);
  162. arma_debug_check( (A.is_square() == false), "trimatu()/trimatl(): given matrix must be square sized" );
  163. const uword N = A.n_rows;
  164. if(&out != &A)
  165. {
  166. out.copy_size(A);
  167. }
  168. if(upper)
  169. {
  170. // Upper triangular: but since we're transposing, we're taking the lower
  171. // triangular and putting it in the upper half.
  172. for(uword row = 0; row < N; ++row)
  173. {
  174. eT* out_colptr = out.colptr(row);
  175. for(uword col = 0; col <= row; ++col)
  176. {
  177. //out.at(col, row) = std::conj( A.at(row, col) );
  178. out_colptr[col] = std::conj( A.at(row, col) );
  179. }
  180. }
  181. }
  182. else
  183. {
  184. // Lower triangular: but since we're transposing, we're taking the upper
  185. // triangular and putting it in the lower half.
  186. for(uword row = 0; row < N; ++row)
  187. {
  188. for(uword col = row; col < N; ++col)
  189. {
  190. out.at(col, row) = std::conj( A.at(row, col) );
  191. }
  192. }
  193. }
  194. op_trimat::fill_zeros(out, upper);
  195. }
  196. //
  197. template<typename T1>
  198. inline
  199. void
  200. op_trimatu_ext::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_trimatu_ext>& in)
  201. {
  202. arma_extra_debug_sigprint();
  203. typedef typename T1::elem_type eT;
  204. const unwrap<T1> tmp(in.m);
  205. const Mat<eT>& A = tmp.M;
  206. arma_debug_check( (A.is_square() == false), "trimatu(): given matrix must be square sized" );
  207. const uword row_offset = in.aux_uword_a;
  208. const uword col_offset = in.aux_uword_b;
  209. const uword n_rows = A.n_rows;
  210. const uword n_cols = A.n_cols;
  211. arma_debug_check( ((row_offset > 0) && (row_offset >= n_rows)) || ((col_offset > 0) && (col_offset >= n_cols)), "trimatu(): requested diagonal is out of bounds" );
  212. if(&out != &A)
  213. {
  214. out.copy_size(A);
  215. const uword N = (std::min)(n_rows - row_offset, n_cols - col_offset);
  216. for(uword i=0; i < n_cols; ++i)
  217. {
  218. const uword col = i + col_offset;
  219. if(i < N)
  220. {
  221. const uword end_row = i + row_offset;
  222. for(uword row=0; row <= end_row; ++row)
  223. {
  224. out.at(row,col) = A.at(row,col);
  225. }
  226. }
  227. else
  228. {
  229. if(col < n_cols)
  230. {
  231. arrayops::copy(out.colptr(col), A.colptr(col), n_rows);
  232. }
  233. }
  234. }
  235. }
  236. op_trimatu_ext::fill_zeros(out, row_offset, col_offset);
  237. }
  238. template<typename eT>
  239. inline
  240. void
  241. op_trimatu_ext::fill_zeros(Mat<eT>& out, const uword row_offset, const uword col_offset)
  242. {
  243. arma_extra_debug_sigprint();
  244. const uword n_rows = out.n_rows;
  245. const uword n_cols = out.n_cols;
  246. const uword N = (std::min)(n_rows - row_offset, n_cols - col_offset);
  247. for(uword col=0; col < col_offset; ++col)
  248. {
  249. arrayops::fill_zeros(out.colptr(col), n_rows);
  250. }
  251. for(uword i=0; i < N; ++i)
  252. {
  253. const uword start_row = i + row_offset + 1;
  254. const uword col = i + col_offset;
  255. for(uword row=start_row; row < n_rows; ++row)
  256. {
  257. out.at(row,col) = eT(0);
  258. }
  259. }
  260. }
  261. //
  262. template<typename T1>
  263. inline
  264. void
  265. op_trimatl_ext::apply(Mat<typename T1::elem_type>& out, const Op<T1,op_trimatl_ext>& in)
  266. {
  267. arma_extra_debug_sigprint();
  268. typedef typename T1::elem_type eT;
  269. const unwrap<T1> tmp(in.m);
  270. const Mat<eT>& A = tmp.M;
  271. arma_debug_check( (A.is_square() == false), "trimatl(): given matrix must be square sized" );
  272. const uword row_offset = in.aux_uword_a;
  273. const uword col_offset = in.aux_uword_b;
  274. const uword n_rows = A.n_rows;
  275. const uword n_cols = A.n_cols;
  276. arma_debug_check( ((row_offset > 0) && (row_offset >= n_rows)) || ((col_offset > 0) && (col_offset >= n_cols)), "trimatl(): requested diagonal is out of bounds" );
  277. if(&out != &A)
  278. {
  279. out.copy_size(A);
  280. const uword N = (std::min)(n_rows - row_offset, n_cols - col_offset);
  281. for(uword col=0; col < col_offset; ++col)
  282. {
  283. arrayops::copy( out.colptr(col), A.colptr(col), n_rows );
  284. }
  285. for(uword i=0; i<N; ++i)
  286. {
  287. const uword start_row = i + row_offset;
  288. const uword col = i + col_offset;
  289. for(uword row=start_row; row < n_rows; ++row)
  290. {
  291. out.at(row,col) = A.at(row,col);
  292. }
  293. }
  294. }
  295. op_trimatl_ext::fill_zeros(out, row_offset, col_offset);
  296. }
  297. template<typename eT>
  298. inline
  299. void
  300. op_trimatl_ext::fill_zeros(Mat<eT>& out, const uword row_offset, const uword col_offset)
  301. {
  302. arma_extra_debug_sigprint();
  303. const uword n_rows = out.n_rows;
  304. const uword n_cols = out.n_cols;
  305. const uword N = (std::min)(n_rows - row_offset, n_cols - col_offset);
  306. for(uword i=0; i < n_cols; ++i)
  307. {
  308. const uword col = i + col_offset;
  309. if(i < N)
  310. {
  311. const uword end_row = i + row_offset;
  312. for(uword row=0; row < end_row; ++row)
  313. {
  314. out.at(row,col) = eT(0);
  315. }
  316. }
  317. else
  318. {
  319. if(col < n_cols)
  320. {
  321. arrayops::fill_zeros(out.colptr(col), n_rows);
  322. }
  323. }
  324. }
  325. }
  326. //! @}