glue_affmul_meat.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 glue_affmul
  16. //! @{
  17. template<typename T1, typename T2>
  18. inline
  19. void
  20. glue_affmul::apply(Mat<typename T1::elem_type>& out, const Glue<T1,T2,glue_affmul>& X)
  21. {
  22. arma_extra_debug_sigprint();
  23. typedef typename T1::elem_type eT;
  24. const quasi_unwrap<T1> U1(X.A);
  25. const quasi_unwrap<T2> U2(X.B);
  26. const bool is_alias = (U1.is_alias(out) || U2.is_alias(out));
  27. if(is_alias == false)
  28. {
  29. glue_affmul::apply_noalias(out, U1.M, U2.M);
  30. }
  31. else
  32. {
  33. Mat<eT> tmp;
  34. glue_affmul::apply_noalias(tmp, U1.M, U2.M);
  35. out.steal_mem(tmp);
  36. }
  37. }
  38. template<typename T1, typename T2>
  39. inline
  40. void
  41. glue_affmul::apply_noalias(Mat<typename T1::elem_type>& out, const T1& A, const T2& B)
  42. {
  43. arma_extra_debug_sigprint();
  44. const uword A_n_cols = A.n_cols;
  45. const uword A_n_rows = A.n_rows;
  46. const uword B_n_rows = B.n_rows;
  47. arma_debug_check( (A_n_cols != B_n_rows+1), "affmul(): size mismatch" );
  48. if(A_n_rows == A_n_cols)
  49. {
  50. glue_affmul::apply_noalias_square(out, A, B);
  51. }
  52. else
  53. if(A_n_rows == B_n_rows)
  54. {
  55. glue_affmul::apply_noalias_rectangle(out, A, B);
  56. }
  57. else
  58. {
  59. glue_affmul::apply_noalias_generic(out, A, B);
  60. }
  61. }
  62. template<typename T1, typename T2>
  63. inline
  64. void
  65. glue_affmul::apply_noalias_square(Mat<typename T1::elem_type>& out, const T1& A, const T2& B)
  66. {
  67. arma_extra_debug_sigprint();
  68. typedef typename T1::elem_type eT;
  69. // assuming that A is square sized, and A.n_cols = B.n_rows+1
  70. const uword N = A.n_rows;
  71. const uword B_n_cols = B.n_cols;
  72. out.set_size(N, B_n_cols);
  73. if(out.n_elem == 0) { return; }
  74. const eT* A_mem = A.memptr();
  75. switch(N)
  76. {
  77. case 0:
  78. break;
  79. case 1: // A is 1x1
  80. out.fill(A_mem[0]);
  81. break;
  82. case 2: // A is 2x2
  83. {
  84. if(B_n_cols == 1)
  85. {
  86. const eT* B_mem = B.memptr();
  87. eT* out_mem = out.memptr();
  88. const eT x = B_mem[0];
  89. out_mem[0] = A_mem[0]*x + A_mem[2];
  90. out_mem[1] = A_mem[1]*x + A_mem[3];
  91. }
  92. else
  93. for(uword col=0; col < B_n_cols; ++col)
  94. {
  95. const eT* B_mem = B.colptr(col);
  96. eT* out_mem = out.colptr(col);
  97. const eT x = B_mem[0];
  98. out_mem[0] = A_mem[0]*x + A_mem[2];
  99. out_mem[1] = A_mem[1]*x + A_mem[3];
  100. }
  101. }
  102. break;
  103. case 3: // A is 3x3
  104. {
  105. if(B_n_cols == 1)
  106. {
  107. const eT* B_mem = B.memptr();
  108. eT* out_mem = out.memptr();
  109. const eT x = B_mem[0];
  110. const eT y = B_mem[1];
  111. out_mem[0] = A_mem[0]*x + A_mem[3]*y + A_mem[6];
  112. out_mem[1] = A_mem[1]*x + A_mem[4]*y + A_mem[7];
  113. out_mem[2] = A_mem[2]*x + A_mem[5]*y + A_mem[8];
  114. }
  115. else
  116. for(uword col=0; col < B_n_cols; ++col)
  117. {
  118. const eT* B_mem = B.colptr(col);
  119. eT* out_mem = out.colptr(col);
  120. const eT x = B_mem[0];
  121. const eT y = B_mem[1];
  122. out_mem[0] = A_mem[0]*x + A_mem[3]*y + A_mem[6];
  123. out_mem[1] = A_mem[1]*x + A_mem[4]*y + A_mem[7];
  124. out_mem[2] = A_mem[2]*x + A_mem[5]*y + A_mem[8];
  125. }
  126. }
  127. break;
  128. case 4: // A is 4x4
  129. {
  130. if(B_n_cols == 1)
  131. {
  132. const eT* B_mem = B.memptr();
  133. eT* out_mem = out.memptr();
  134. const eT x = B_mem[0];
  135. const eT y = B_mem[1];
  136. const eT z = B_mem[2];
  137. out_mem[0] = A_mem[ 0]*x + A_mem[ 4]*y + A_mem[ 8]*z + A_mem[12];
  138. out_mem[1] = A_mem[ 1]*x + A_mem[ 5]*y + A_mem[ 9]*z + A_mem[13];
  139. out_mem[2] = A_mem[ 2]*x + A_mem[ 6]*y + A_mem[10]*z + A_mem[14];
  140. out_mem[3] = A_mem[ 3]*x + A_mem[ 7]*y + A_mem[11]*z + A_mem[15];
  141. }
  142. else
  143. for(uword col=0; col < B_n_cols; ++col)
  144. {
  145. const eT* B_mem = B.colptr(col);
  146. eT* out_mem = out.colptr(col);
  147. const eT x = B_mem[0];
  148. const eT y = B_mem[1];
  149. const eT z = B_mem[2];
  150. out_mem[0] = A_mem[ 0]*x + A_mem[ 4]*y + A_mem[ 8]*z + A_mem[12];
  151. out_mem[1] = A_mem[ 1]*x + A_mem[ 5]*y + A_mem[ 9]*z + A_mem[13];
  152. out_mem[2] = A_mem[ 2]*x + A_mem[ 6]*y + A_mem[10]*z + A_mem[14];
  153. out_mem[3] = A_mem[ 3]*x + A_mem[ 7]*y + A_mem[11]*z + A_mem[15];
  154. }
  155. }
  156. break;
  157. case 5: // A is 5x5
  158. {
  159. if(B_n_cols == 1)
  160. {
  161. const eT* B_mem = B.memptr();
  162. eT* out_mem = out.memptr();
  163. const eT x = B_mem[0];
  164. const eT y = B_mem[1];
  165. const eT z = B_mem[2];
  166. const eT w = B_mem[3];
  167. out_mem[0] = A_mem[ 0]*x + A_mem[ 5]*y + A_mem[10]*z + A_mem[15]*w + A_mem[20];
  168. out_mem[1] = A_mem[ 1]*x + A_mem[ 6]*y + A_mem[11]*z + A_mem[16]*w + A_mem[21];
  169. out_mem[2] = A_mem[ 2]*x + A_mem[ 7]*y + A_mem[12]*z + A_mem[17]*w + A_mem[22];
  170. out_mem[3] = A_mem[ 3]*x + A_mem[ 8]*y + A_mem[13]*z + A_mem[18]*w + A_mem[23];
  171. out_mem[4] = A_mem[ 4]*x + A_mem[ 9]*y + A_mem[14]*z + A_mem[19]*w + A_mem[24];
  172. }
  173. else
  174. for(uword col=0; col < B_n_cols; ++col)
  175. {
  176. const eT* B_mem = B.colptr(col);
  177. eT* out_mem = out.colptr(col);
  178. const eT x = B_mem[0];
  179. const eT y = B_mem[1];
  180. const eT z = B_mem[2];
  181. const eT w = B_mem[3];
  182. out_mem[0] = A_mem[ 0]*x + A_mem[ 5]*y + A_mem[10]*z + A_mem[15]*w + A_mem[20];
  183. out_mem[1] = A_mem[ 1]*x + A_mem[ 6]*y + A_mem[11]*z + A_mem[16]*w + A_mem[21];
  184. out_mem[2] = A_mem[ 2]*x + A_mem[ 7]*y + A_mem[12]*z + A_mem[17]*w + A_mem[22];
  185. out_mem[3] = A_mem[ 3]*x + A_mem[ 8]*y + A_mem[13]*z + A_mem[18]*w + A_mem[23];
  186. out_mem[4] = A_mem[ 4]*x + A_mem[ 9]*y + A_mem[14]*z + A_mem[19]*w + A_mem[24];
  187. }
  188. }
  189. break;
  190. default:
  191. {
  192. if(B_n_cols == 1)
  193. {
  194. Col<eT> tmp(N);
  195. eT* tmp_mem = tmp.memptr();
  196. arrayops::copy(tmp_mem, B.memptr(), N-1);
  197. tmp_mem[N-1] = eT(1);
  198. out = A * tmp;
  199. }
  200. else
  201. {
  202. Mat<eT> tmp(N, B_n_cols);
  203. for(uword col=0; col < B_n_cols; ++col)
  204. {
  205. const eT* B_mem = B.colptr(col);
  206. eT* tmp_mem = tmp.colptr(col);
  207. arrayops::copy(tmp_mem, B_mem, N-1);
  208. tmp_mem[N-1] = eT(1);
  209. }
  210. out = A * tmp;
  211. }
  212. }
  213. }
  214. }
  215. template<typename T1, typename T2>
  216. inline
  217. void
  218. glue_affmul::apply_noalias_rectangle(Mat<typename T1::elem_type>& out, const T1& A, const T2& B)
  219. {
  220. arma_extra_debug_sigprint();
  221. typedef typename T1::elem_type eT;
  222. // assuming that A.n_rows = A.n_cols-1, and A.n_cols = B.n_rows+1
  223. // (A and B have the same number of rows)
  224. const uword A_n_rows = A.n_rows;
  225. const uword B_n_cols = B.n_cols;
  226. out.set_size(A_n_rows, B_n_cols);
  227. if(out.n_elem == 0) { return; }
  228. const eT* A_mem = A.memptr();
  229. switch(A_n_rows)
  230. {
  231. case 0:
  232. break;
  233. case 1: // A is 1x2
  234. {
  235. if(B_n_cols == 1)
  236. {
  237. const eT* B_mem = B.memptr();
  238. eT* out_mem = out.memptr();
  239. const eT x = B_mem[0];
  240. out_mem[0] = A_mem[0]*x + A_mem[1];
  241. }
  242. else
  243. for(uword col=0; col < B_n_cols; ++col)
  244. {
  245. const eT* B_mem = B.colptr(col);
  246. eT* out_mem = out.colptr(col);
  247. const eT x = B_mem[0];
  248. out_mem[0] = A_mem[0]*x + A_mem[1];
  249. }
  250. }
  251. break;
  252. case 2: // A is 2x3
  253. {
  254. if(B_n_cols == 1)
  255. {
  256. const eT* B_mem = B.memptr();
  257. eT* out_mem = out.memptr();
  258. const eT x = B_mem[0];
  259. const eT y = B_mem[1];
  260. out_mem[0] = A_mem[0]*x + A_mem[2]*y + A_mem[4];
  261. out_mem[1] = A_mem[1]*x + A_mem[3]*y + A_mem[5];
  262. }
  263. else
  264. for(uword col=0; col < B_n_cols; ++col)
  265. {
  266. const eT* B_mem = B.colptr(col);
  267. eT* out_mem = out.colptr(col);
  268. const eT x = B_mem[0];
  269. const eT y = B_mem[1];
  270. out_mem[0] = A_mem[0]*x + A_mem[2]*y + A_mem[4];
  271. out_mem[1] = A_mem[1]*x + A_mem[3]*y + A_mem[5];
  272. }
  273. }
  274. break;
  275. case 3: // A is 3x4
  276. {
  277. if(B_n_cols == 1)
  278. {
  279. const eT* B_mem = B.memptr();
  280. eT* out_mem = out.memptr();
  281. const eT x = B_mem[0];
  282. const eT y = B_mem[1];
  283. const eT z = B_mem[2];
  284. out_mem[0] = A_mem[ 0]*x + A_mem[ 3]*y + A_mem[ 6]*z + A_mem[ 9];
  285. out_mem[1] = A_mem[ 1]*x + A_mem[ 4]*y + A_mem[ 7]*z + A_mem[10];
  286. out_mem[2] = A_mem[ 2]*x + A_mem[ 5]*y + A_mem[ 8]*z + A_mem[11];
  287. }
  288. else
  289. for(uword col=0; col < B_n_cols; ++col)
  290. {
  291. const eT* B_mem = B.colptr(col);
  292. eT* out_mem = out.colptr(col);
  293. const eT x = B_mem[0];
  294. const eT y = B_mem[1];
  295. const eT z = B_mem[2];
  296. out_mem[0] = A_mem[ 0]*x + A_mem[ 3]*y + A_mem[ 6]*z + A_mem[ 9];
  297. out_mem[1] = A_mem[ 1]*x + A_mem[ 4]*y + A_mem[ 7]*z + A_mem[10];
  298. out_mem[2] = A_mem[ 2]*x + A_mem[ 5]*y + A_mem[ 8]*z + A_mem[11];
  299. }
  300. }
  301. break;
  302. case 4: // A is 4x5
  303. {
  304. if(B_n_cols == 1)
  305. {
  306. const eT* B_mem = B.memptr();
  307. eT* out_mem = out.memptr();
  308. const eT x = B_mem[0];
  309. const eT y = B_mem[1];
  310. const eT z = B_mem[2];
  311. const eT w = B_mem[3];
  312. out_mem[0] = A_mem[ 0]*x + A_mem[ 4]*y + A_mem[ 8]*z + A_mem[12]*w + A_mem[16];
  313. out_mem[1] = A_mem[ 1]*x + A_mem[ 5]*y + A_mem[ 9]*z + A_mem[13]*w + A_mem[17];
  314. out_mem[2] = A_mem[ 2]*x + A_mem[ 6]*y + A_mem[10]*z + A_mem[14]*w + A_mem[18];
  315. out_mem[3] = A_mem[ 3]*x + A_mem[ 7]*y + A_mem[11]*z + A_mem[15]*w + A_mem[19];
  316. }
  317. else
  318. for(uword col=0; col < B_n_cols; ++col)
  319. {
  320. const eT* B_mem = B.colptr(col);
  321. eT* out_mem = out.colptr(col);
  322. const eT x = B_mem[0];
  323. const eT y = B_mem[1];
  324. const eT z = B_mem[2];
  325. const eT w = B_mem[3];
  326. out_mem[0] = A_mem[ 0]*x + A_mem[ 4]*y + A_mem[ 8]*z + A_mem[12]*w + A_mem[16];
  327. out_mem[1] = A_mem[ 1]*x + A_mem[ 5]*y + A_mem[ 9]*z + A_mem[13]*w + A_mem[17];
  328. out_mem[2] = A_mem[ 2]*x + A_mem[ 6]*y + A_mem[10]*z + A_mem[14]*w + A_mem[18];
  329. out_mem[3] = A_mem[ 3]*x + A_mem[ 7]*y + A_mem[11]*z + A_mem[15]*w + A_mem[19];
  330. }
  331. }
  332. break;
  333. default:
  334. {
  335. const uword A_n_cols = A.n_cols;
  336. if(B_n_cols == 1)
  337. {
  338. Col<eT> tmp(A_n_cols);
  339. eT* tmp_mem = tmp.memptr();
  340. arrayops::copy(tmp_mem, B.memptr(), A_n_cols-1);
  341. tmp_mem[A_n_cols-1] = eT(1);
  342. out = A * tmp;
  343. }
  344. else
  345. {
  346. Mat<eT> tmp(A_n_cols, B_n_cols);
  347. for(uword col=0; col < B_n_cols; ++col)
  348. {
  349. const eT* B_mem = B.colptr(col);
  350. eT* tmp_mem = tmp.colptr(col);
  351. arrayops::copy(tmp_mem, B_mem, A_n_cols-1);
  352. tmp_mem[A_n_cols-1] = eT(1);
  353. }
  354. out = A * tmp;
  355. }
  356. }
  357. }
  358. }
  359. template<typename T1, typename T2>
  360. inline
  361. void
  362. glue_affmul::apply_noalias_generic(Mat<typename T1::elem_type>& out, const T1& A, const T2& B)
  363. {
  364. arma_extra_debug_sigprint();
  365. typedef typename T1::elem_type eT;
  366. // assuming that A.n_cols = B.n_rows+1
  367. const uword B_n_rows = B.n_rows;
  368. const uword B_n_cols = B.n_cols;
  369. Mat<eT> tmp(B_n_rows+1, B_n_cols);
  370. for(uword col=0; col < B_n_cols; ++col)
  371. {
  372. const eT* B_mem = B.colptr(col);
  373. eT* tmp_mem = tmp.colptr(col);
  374. arrayops::copy(tmp_mem, B_mem, B_n_rows);
  375. tmp_mem[B_n_rows] = eT(1);
  376. }
  377. out = A * tmp;
  378. }
  379. //! @}