fn_trans.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. // Copyright 2015 Conrad Sanderson (http://conradsanderson.id.au)
  2. // Copyright 2015 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. #include <armadillo>
  16. #include "catch.hpp"
  17. using namespace arma;
  18. TEST_CASE("fn_trans_1")
  19. {
  20. mat A =
  21. "\
  22. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  23. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  24. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  25. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  26. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  27. ";
  28. mat At =
  29. "\
  30. 0.061198 0.437242 -0.492474 0.336352 0.239585;\
  31. 0.201990 0.058956 -0.031309 0.411541 -0.428913;\
  32. 0.019678 -0.149362 0.314156 0.458476 -0.406953;\
  33. -0.493936 -0.045465 0.419733 -0.393139 -0.291020;\
  34. -0.126745 0.296153 0.068317 -0.135040 -0.353768;\
  35. 0.051408 0.035437 -0.454499 0.373833 0.258704;\
  36. ";
  37. rowvec At_sum0 = { -0.28641, 0.63296, -0.17608, 1.05202, -0.98237 };
  38. colvec At_sum1 =
  39. {
  40. 0.58190,
  41. 0.21227,
  42. 0.23599,
  43. -0.80383,
  44. -0.25108,
  45. 0.26488
  46. };
  47. rowvec A_col1_t = { 0.201990, 0.058956, -0.031309, 0.411541, -0.428913 };
  48. colvec A_row1_t =
  49. {
  50. 0.437242,
  51. 0.058956,
  52. -0.149362,
  53. -0.045465,
  54. 0.296153,
  55. 0.035437
  56. };
  57. double accu_A_col1_t = 0.21227;
  58. double accu_A_row1_t = 0.63296;
  59. REQUIRE( accu(abs(mat(A.t().t()) - A)) == Approx(0.0) );
  60. REQUIRE( accu(abs( A.t().t() - A)) == Approx(0.0) );
  61. REQUIRE( accu(abs(mat(A.t() ) - At)) == Approx(0.0) );
  62. REQUIRE( accu(abs(mat(A.st() ) - At)) == Approx(0.0) );
  63. REQUIRE( accu(abs(mat(A.ht() ) - At)) == Approx(0.0) );
  64. REQUIRE( accu(abs(mat( trans(A)) - At)) == Approx(0.0) );
  65. REQUIRE( accu(abs(mat(strans(A)) - At)) == Approx(0.0) );
  66. REQUIRE( accu(abs(A.t() - At)) == Approx(0.0) );
  67. REQUIRE( accu(abs(A.st() - At)) == Approx(0.0) );
  68. REQUIRE( accu(abs(A.ht() - At)) == Approx(0.0) );
  69. REQUIRE( accu(abs( trans(A) - At)) == Approx(0.0) );
  70. REQUIRE( accu(abs(strans(A) - At)) == Approx(0.0) );
  71. REQUIRE( accu(abs(mat(At.t() ) - A)) == Approx(0.0) );
  72. REQUIRE( accu(abs(mat(At.st() ) - A)) == Approx(0.0) );
  73. REQUIRE( accu(abs(mat(At.ht() ) - A)) == Approx(0.0) );
  74. REQUIRE( accu(abs(mat( trans(At)) - A)) == Approx(0.0) );
  75. REQUIRE( accu(abs(mat(strans(At)) - A)) == Approx(0.0) );
  76. REQUIRE( accu(abs(At.t() - A)) == Approx(0.0) );
  77. REQUIRE( accu(abs(At.st() - A)) == Approx(0.0) );
  78. REQUIRE( accu(abs(At.ht() - A)) == Approx(0.0) );
  79. REQUIRE( accu(abs( trans(At) - A)) == Approx(0.0) );
  80. REQUIRE( accu(abs(strans(At) - A)) == Approx(0.0) );
  81. REQUIRE( accu(abs((0 + At.t() ) - A)) == Approx(0.0) );
  82. REQUIRE( accu(abs((0 + At.st() ) - A)) == Approx(0.0) );
  83. REQUIRE( accu(abs((0 + At.ht() ) - A)) == Approx(0.0) );
  84. REQUIRE( accu(abs((0 + trans(At)) - A)) == Approx(0.0) );
  85. REQUIRE( accu(abs((0 + strans(At)) - A)) == Approx(0.0) );
  86. REQUIRE( accu(abs(mat(0 + At.t() ) - A)) == Approx(0.0) );
  87. REQUIRE( accu(abs(mat(0 + At.st() ) - A)) == Approx(0.0) );
  88. REQUIRE( accu(abs(mat(0 + At.ht() ) - A)) == Approx(0.0) );
  89. REQUIRE( accu(abs(mat(0 + trans(At)) - A)) == Approx(0.0) );
  90. REQUIRE( accu(abs(mat(0 + strans(At)) - A)) == Approx(0.0) );
  91. REQUIRE( accu(abs(2*A.t() - 2*At)) == Approx(0.0) );
  92. REQUIRE( accu(abs(2*trans(A) - 2*At)) == Approx(0.0) );
  93. REQUIRE( accu(abs((2*A).t() - 2*At)) == Approx(0.0) );
  94. REQUIRE( accu(abs(trans(2*A) - 2*At)) == Approx(0.0) );
  95. REQUIRE( accu(abs((A+A).t() - 2*At)) == Approx(0.0) );
  96. REQUIRE( accu(abs(trans(A+A) - 2*At)) == Approx(0.0) );
  97. REQUIRE( accu(abs((A.t() + At) - 2*At)) == Approx(0.0) );
  98. REQUIRE( accu(abs((trans(A) + At) - 2*At)) == Approx(0.0) );
  99. REQUIRE( accu(abs(mat(2*A.t()) - 2*At)) == Approx(0.0) );
  100. REQUIRE( accu(abs(mat(2*trans(A)) - 2*At)) == Approx(0.0) );
  101. REQUIRE( accu(abs(mat((2*A).t()) - 2*At)) == Approx(0.0) );
  102. REQUIRE( accu(abs(mat(trans(2*A)) - 2*At)) == Approx(0.0) );
  103. REQUIRE( accu(abs(mat((A+A).t()) - 2*At)) == Approx(0.0) );
  104. REQUIRE( accu(abs(mat(trans(A+A)) - 2*At)) == Approx(0.0) );
  105. REQUIRE( accu(abs(mat(A.t() + At) - 2*At)) == Approx(0.0) );
  106. REQUIRE( accu(abs(mat(trans(A) + At) - 2*At)) == Approx(0.0) );
  107. REQUIRE( accu(abs(rowvec(A.col(1).t()) - A_col1_t)) == Approx(0.0) );
  108. REQUIRE( accu(abs(colvec(A.row(1).t()) - A_row1_t)) == Approx(0.0) );
  109. REQUIRE( accu(abs(A.col(1).t() - A_col1_t)) == Approx(0.0) );
  110. REQUIRE( accu(abs(A.row(1).t() - A_row1_t)) == Approx(0.0) );
  111. REQUIRE( accu(abs(2*A.col(1).t() - 2*A_col1_t)) == Approx(0.0) );
  112. REQUIRE( accu(abs(2*A.row(1).t() - 2*A_row1_t)) == Approx(0.0) );
  113. REQUIRE( accu(abs( (A.col(1).t() + A_col1_t) - 2*A_col1_t)) == Approx(0.0) );
  114. REQUIRE( accu(abs( (A.row(1).t() + A_row1_t) - 2*A_row1_t)) == Approx(0.0) );
  115. REQUIRE( abs( accu(A.col(1).t()) - accu_A_col1_t ) == Approx(0.0) );
  116. REQUIRE( abs( accu(A.row(1).t()) - accu_A_row1_t ) == Approx(0.0) );
  117. REQUIRE( abs( accu(A.col(1).t()) - accu(A.col(1)) ) == Approx(0.0) );
  118. REQUIRE( abs( accu(A.row(1).t()) - accu(A.row(1)) ) == Approx(0.0) );
  119. REQUIRE( abs( sum(A.col(1).t()) - accu_A_col1_t ) == Approx(0.0) );
  120. REQUIRE( abs( sum(A.row(1).t()) - accu_A_row1_t ) == Approx(0.0) );
  121. REQUIRE_THROWS( A + A.t() );
  122. }
  123. TEST_CASE("fn_trans_2")
  124. {
  125. mat A =
  126. "\
  127. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  128. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  129. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  130. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  131. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  132. ";
  133. cx_mat C = cx_mat(A,fliplr(A));
  134. cx_mat Ct =
  135. {
  136. { cx_double( 0.061198, -0.051408), cx_double( 0.437242, -0.035437), cx_double(-0.492474, +0.454499), cx_double( 0.336352, -0.373833), cx_double( 0.239585, -0.258704) },
  137. { cx_double( 0.201990, +0.126745), cx_double( 0.058956, -0.296153), cx_double(-0.031309, -0.068317), cx_double( 0.411541, +0.135040), cx_double(-0.428913, +0.353768) },
  138. { cx_double( 0.019678, +0.493936), cx_double(-0.149362, +0.045465), cx_double( 0.314156, -0.419733), cx_double( 0.458476, +0.393139), cx_double(-0.406953, +0.291020) },
  139. { cx_double(-0.493936, -0.019678), cx_double(-0.045465, +0.149362), cx_double( 0.419733, -0.314156), cx_double(-0.393139, -0.458476), cx_double(-0.291020, +0.406953) },
  140. { cx_double(-0.126745, -0.201990), cx_double( 0.296153, -0.058956), cx_double( 0.068317, +0.031309), cx_double(-0.135040, -0.411541), cx_double(-0.353768, +0.428913) },
  141. { cx_double( 0.051408, -0.061198), cx_double( 0.035437, -0.437242), cx_double(-0.454499, +0.492474), cx_double( 0.373833, -0.336352), cx_double( 0.258704, -0.239585) }
  142. };
  143. cx_rowvec C_col1_t = { cx_double(0.201990, +0.126745), cx_double(0.058956, -0.296153), cx_double(-0.031309, -0.068317), cx_double(0.411541, +0.135040), cx_double(-0.428913, +0.353768) };
  144. cx_colvec C_row1_t =
  145. {
  146. cx_double( 0.437242, -0.035437),
  147. cx_double( 0.058956, -0.296153),
  148. cx_double(-0.149362, +0.045465),
  149. cx_double(-0.045465, +0.149362),
  150. cx_double( 0.296153, -0.058956),
  151. cx_double( 0.035437, -0.437242)
  152. };
  153. REQUIRE( accu(abs(C.t().t() - C)) == Approx(0.0) );
  154. REQUIRE( accu(abs(cx_mat(C.t() ) - Ct)) == Approx(0.0) );
  155. REQUIRE( accu(abs(cx_mat(C.ht() ) - Ct)) == Approx(0.0) );
  156. REQUIRE( accu(abs(cx_mat(trans(C)) - Ct)) == Approx(0.0) );
  157. REQUIRE( accu(abs(C.t() - Ct)) == Approx(0.0) );
  158. REQUIRE( accu(abs(C.ht() - Ct)) == Approx(0.0) );
  159. REQUIRE( accu(abs(trans(C) - Ct)) == Approx(0.0) );
  160. REQUIRE( accu(abs(cx_mat(Ct.t() ) - C)) == Approx(0.0) );
  161. REQUIRE( accu(abs(cx_mat(Ct.ht() ) - C)) == Approx(0.0) );
  162. REQUIRE( accu(abs(cx_mat(trans(Ct)) - C)) == Approx(0.0) );
  163. REQUIRE( accu(abs(Ct.t() - C)) == Approx(0.0) );
  164. REQUIRE( accu(abs(Ct.ht() - C)) == Approx(0.0) );
  165. REQUIRE( accu(abs(trans(Ct) - C)) == Approx(0.0) );
  166. REQUIRE( accu(abs(2*C.t() - 2*Ct)) == Approx(0.0) );
  167. REQUIRE( accu(abs(2*trans(C) - 2*Ct)) == Approx(0.0) );
  168. REQUIRE( accu(abs((2*C).t() - 2*Ct)) == Approx(0.0) );
  169. REQUIRE( accu(abs(trans(2*C) - 2*Ct)) == Approx(0.0) );
  170. REQUIRE( accu(abs((C+C).t() - 2*Ct)) == Approx(0.0) );
  171. REQUIRE( accu(abs(trans(C+C) - 2*Ct)) == Approx(0.0) );
  172. REQUIRE( accu(abs(cx_double(2,3)*C.t() - cx_double(2,3)*Ct)) == Approx(0.0) );
  173. REQUIRE( accu(abs(cx_double(2,3)*trans(C) - cx_double(2,3)*Ct)) == Approx(0.0) );
  174. REQUIRE( accu(abs(cx_mat(2*C.t()) - 2*Ct)) == Approx(0.0) );
  175. REQUIRE( accu(abs(cx_mat(2*trans(C)) - 2*Ct)) == Approx(0.0) );
  176. REQUIRE( accu(abs(cx_mat((2*C).t()) - 2*Ct)) == Approx(0.0) );
  177. REQUIRE( accu(abs(cx_mat(trans(2*C)) - 2*Ct)) == Approx(0.0) );
  178. REQUIRE( accu(abs(cx_mat((C+C).t()) - 2*Ct)) == Approx(0.0) );
  179. REQUIRE( accu(abs(cx_mat(trans(C+C)) - 2*Ct)) == Approx(0.0) );
  180. REQUIRE( accu(abs(cx_mat(cx_double(2,3)*C.t()) - cx_double(2,3)*Ct)) == Approx(0.0) );
  181. REQUIRE( accu(abs(cx_mat(cx_double(2,3)*trans(C)) - cx_double(2,3)*Ct)) == Approx(0.0) );
  182. REQUIRE( accu(abs((C.t() + Ct) - 2*Ct)) == Approx(0.0) );
  183. REQUIRE( accu(abs((trans(C) + Ct) - 2*Ct)) == Approx(0.0) );
  184. REQUIRE( accu(abs(cx_rowvec(C.col(1).t()) - C_col1_t)) == Approx(0.0) );
  185. REQUIRE( accu(abs(cx_colvec(C.row(1).t()) - C_row1_t)) == Approx(0.0) );
  186. REQUIRE( accu(abs(C.col(1).t() - C_col1_t)) == Approx(0.0) );
  187. REQUIRE( accu(abs(C.row(1).t() - C_row1_t)) == Approx(0.0) );
  188. REQUIRE( accu(abs(2*C.col(1).t() - 2*C_col1_t)) == Approx(0.0) );
  189. REQUIRE( accu(abs(2*C.row(1).t() - 2*C_row1_t)) == Approx(0.0) );
  190. REQUIRE( accu(abs( (C.col(1).t() + C_col1_t) - 2*C_col1_t)) == Approx(0.0) );
  191. REQUIRE( accu(abs( (C.row(1).t() + C_row1_t) - 2*C_row1_t)) == Approx(0.0) );
  192. //
  193. REQUIRE( accu(abs(cx_mat(C.st()) - conj(Ct))) == Approx(0.0) );
  194. REQUIRE( accu(abs(cx_mat(strans(C)) - conj(Ct))) == Approx(0.0) );
  195. REQUIRE( accu(abs(C.st() - conj(Ct))) == Approx(0.0) );
  196. REQUIRE( accu(abs(strans(C) - conj(Ct))) == Approx(0.0) );
  197. REQUIRE( accu(abs(2*C.st() - conj(2*Ct))) == Approx(0.0) );
  198. REQUIRE( accu(abs(2*strans(C) - conj(2*Ct))) == Approx(0.0) );
  199. REQUIRE( accu(abs(cx_double(2,3)*C.st() - cx_double(2,3)*conj(Ct))) == Approx(0.0) );
  200. REQUIRE( accu(abs(cx_double(2,3)*strans(C) - cx_double(2,3)*conj(Ct))) == Approx(0.0) );
  201. REQUIRE( accu(abs((C.st() + C.st()) - conj(2*Ct))) == Approx(0.0) );
  202. REQUIRE( accu(abs((strans(C) + C.st()) - conj(2*Ct))) == Approx(0.0) );
  203. REQUIRE( accu(abs(cx_rowvec(C.col(1).st()) - conj(C_col1_t))) == Approx(0.0) );
  204. REQUIRE( accu(abs(cx_colvec(C.row(1).st()) - conj(C_row1_t))) == Approx(0.0) );
  205. REQUIRE( accu(abs(C.col(1).st() - conj(C_col1_t))) == Approx(0.0) );
  206. REQUIRE( accu(abs(C.row(1).st() - conj(C_row1_t))) == Approx(0.0) );
  207. REQUIRE( accu(abs(2*C.col(1).st() - conj(2*C_col1_t))) == Approx(0.0) );
  208. REQUIRE( accu(abs(2*C.row(1).st() - conj(2*C_row1_t))) == Approx(0.0) );
  209. REQUIRE( accu(abs( (C.col(1).st() + conj(C_col1_t)) - conj(2*C_col1_t))) == Approx(0.0) );
  210. REQUIRE( accu(abs( (C.row(1).st() + conj(C_row1_t)) - conj(2*C_row1_t))) == Approx(0.0) );
  211. //
  212. REQUIRE_THROWS( C + C.t() );
  213. }
  214. TEST_CASE("fn_trans_3")
  215. {
  216. mat A =
  217. "\
  218. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  219. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  220. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  221. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  222. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  223. ";
  224. mat At =
  225. "\
  226. 0.061198 0.437242 -0.492474 0.336352 0.239585;\
  227. 0.201990 0.058956 -0.031309 0.411541 -0.428913;\
  228. 0.019678 -0.149362 0.314156 0.458476 -0.406953;\
  229. -0.493936 -0.045465 0.419733 -0.393139 -0.291020;\
  230. -0.126745 0.296153 0.068317 -0.135040 -0.353768;\
  231. 0.051408 0.035437 -0.454499 0.373833 0.258704;\
  232. ";
  233. mat B = A.head_cols(5);
  234. mat Bt = At.head_rows(5);
  235. mat X;
  236. mat Y;
  237. X = A; X = X.t();
  238. Y = B; Y = Y.t();
  239. REQUIRE( accu(abs(X - At)) == Approx(0.0) );
  240. REQUIRE( accu(abs(Y - Bt)) == Approx(0.0) );
  241. X = A; X = 0 + X.t();
  242. Y = B; Y = 0 + Y.t();
  243. REQUIRE( accu(abs(X - At)) == Approx(0.0) );
  244. REQUIRE( accu(abs(Y - Bt)) == Approx(0.0) );
  245. X = A; X = 2*X.t();
  246. Y = B; Y = 2*Y.t();
  247. REQUIRE( accu(abs(X - 2*At)) == Approx(0.0) );
  248. REQUIRE( accu(abs(Y - 2*Bt)) == Approx(0.0) );
  249. X = A; X = 0 + 2*X.t();
  250. Y = B; Y = 0 + 2*Y.t();
  251. REQUIRE( accu(abs(X - 2*At)) == Approx(0.0) );
  252. REQUIRE( accu(abs(Y - 2*Bt)) == Approx(0.0) );
  253. X = A; X = (2*X).t();
  254. Y = B; Y = (2*Y).t();
  255. REQUIRE( accu(abs(X - 2*At)) == Approx(0.0) );
  256. REQUIRE( accu(abs(Y - 2*Bt)) == Approx(0.0) );
  257. X = A; X = (X+X).t();
  258. Y = B; Y = (Y+Y).t();
  259. REQUIRE( accu(abs(X - 2*At)) == Approx(0.0) );
  260. REQUIRE( accu(abs(Y - 2*Bt)) == Approx(0.0) );
  261. colvec q = A.col(1);
  262. rowvec r = A.row(1);
  263. REQUIRE_THROWS( q = q.t() );
  264. REQUIRE_THROWS( r = r.t() );
  265. }
  266. TEST_CASE("fn_trans_4")
  267. {
  268. mat A =
  269. "\
  270. 0.061198 0.201990 0.019678 -0.493936 -0.126745 0.051408;\
  271. 0.437242 0.058956 -0.149362 -0.045465 0.296153 0.035437;\
  272. -0.492474 -0.031309 0.314156 0.419733 0.068317 -0.454499;\
  273. 0.336352 0.411541 0.458476 -0.393139 -0.135040 0.373833;\
  274. 0.239585 -0.428913 -0.406953 -0.291020 -0.353768 0.258704;\
  275. ";
  276. cx_mat C = cx_mat(A,fliplr(A));
  277. cx_mat Ct =
  278. {
  279. { cx_double( 0.061198, -0.051408), cx_double( 0.437242, -0.035437), cx_double(-0.492474, +0.454499), cx_double( 0.336352, -0.373833), cx_double( 0.239585, -0.258704) },
  280. { cx_double( 0.201990, +0.126745), cx_double( 0.058956, -0.296153), cx_double(-0.031309, -0.068317), cx_double( 0.411541, +0.135040), cx_double(-0.428913, +0.353768) },
  281. { cx_double( 0.019678, +0.493936), cx_double(-0.149362, +0.045465), cx_double( 0.314156, -0.419733), cx_double( 0.458476, +0.393139), cx_double(-0.406953, +0.291020) },
  282. { cx_double(-0.493936, -0.019678), cx_double(-0.045465, +0.149362), cx_double( 0.419733, -0.314156), cx_double(-0.393139, -0.458476), cx_double(-0.291020, +0.406953) },
  283. { cx_double(-0.126745, -0.201990), cx_double( 0.296153, -0.058956), cx_double( 0.068317, +0.031309), cx_double(-0.135040, -0.411541), cx_double(-0.353768, +0.428913) },
  284. { cx_double( 0.051408, -0.061198), cx_double( 0.035437, -0.437242), cx_double(-0.454499, +0.492474), cx_double( 0.373833, -0.336352), cx_double( 0.258704, -0.239585) }
  285. };
  286. cx_mat D = C.head_cols(5);
  287. cx_mat Dt = Ct.head_rows(5);
  288. cx_mat X;
  289. cx_mat Y;
  290. //
  291. X = C; X = X.t();
  292. Y = D; Y = Y.t();
  293. REQUIRE( accu(abs(X - Ct)) == Approx(0.0) );
  294. REQUIRE( accu(abs(Y - Dt)) == Approx(0.0) );
  295. X = C; X = 0 + X.t();
  296. Y = D; Y = 0 + Y.t();
  297. REQUIRE( accu(abs(X - Ct)) == Approx(0.0) );
  298. REQUIRE( accu(abs(Y - Dt)) == Approx(0.0) );
  299. X = C; X = 2*X.t();
  300. Y = D; Y = 2*Y.t();
  301. REQUIRE( accu(abs(X - 2*Ct)) == Approx(0.0) );
  302. REQUIRE( accu(abs(Y - 2*Dt)) == Approx(0.0) );
  303. X = C; X = 0 + 2*X.t();
  304. Y = D; Y = 0 + 2*Y.t();
  305. REQUIRE( accu(abs(X - 2*Ct)) == Approx(0.0) );
  306. REQUIRE( accu(abs(Y - 2*Dt)) == Approx(0.0) );
  307. X = C; X = (2*X).t();
  308. Y = D; Y = (2*Y).t();
  309. REQUIRE( accu(abs(X - 2*Ct)) == Approx(0.0) );
  310. REQUIRE( accu(abs(Y - 2*Dt)) == Approx(0.0) );
  311. X = C; X = (X+X).t();
  312. Y = D; Y = (Y+Y).t();
  313. REQUIRE( accu(abs(X - 2*Ct)) == Approx(0.0) );
  314. REQUIRE( accu(abs(Y - 2*Dt)) == Approx(0.0) );
  315. X = C; X = cx_double(2,3)*X.t();
  316. Y = D; Y = cx_double(2,3)*Y.t();
  317. REQUIRE( accu(abs(X - cx_double(2,3)*Ct)) == Approx(0.0) );
  318. REQUIRE( accu(abs(Y - cx_double(2,3)*Dt)) == Approx(0.0) );
  319. //
  320. X = C; X = X.st();
  321. Y = D; Y = Y.st();
  322. REQUIRE( accu(abs(X - conj(Ct))) == Approx(0.0) );
  323. REQUIRE( accu(abs(Y - conj(Dt))) == Approx(0.0) );
  324. X = C; X = 0 + X.st();
  325. Y = D; Y = 0 + Y.st();
  326. REQUIRE( accu(abs(X - conj(Ct))) == Approx(0.0) );
  327. REQUIRE( accu(abs(Y - conj(Dt))) == Approx(0.0) );
  328. X = C; X = 2*X.st();
  329. Y = D; Y = 2*Y.st();
  330. REQUIRE( accu(abs(X - 2*conj(Ct))) == Approx(0.0) );
  331. REQUIRE( accu(abs(Y - 2*conj(Dt))) == Approx(0.0) );
  332. X = C; X = 0 + 2*X.st();
  333. Y = D; Y = 0 + 2*Y.st();
  334. REQUIRE( accu(abs(X - 2*conj(Ct))) == Approx(0.0) );
  335. REQUIRE( accu(abs(Y - 2*conj(Dt))) == Approx(0.0) );
  336. X = C; X = (2*X).st();
  337. Y = D; Y = (2*Y).st();
  338. REQUIRE( accu(abs(X - conj(2*Ct))) == Approx(0.0) );
  339. REQUIRE( accu(abs(Y - conj(2*Dt))) == Approx(0.0) );
  340. X = C; X = (X+X).st();
  341. Y = D; Y = (Y+Y).st();
  342. REQUIRE( accu(abs(X - conj(2*Ct))) == Approx(0.0) );
  343. REQUIRE( accu(abs(Y - conj(2*Dt))) == Approx(0.0) );
  344. X = C; X = cx_double(2,3)*X.st();
  345. Y = D; Y = cx_double(2,3)*Y.st();
  346. REQUIRE( accu(abs(X - cx_double(2,3)*conj(Ct))) == Approx(0.0) );
  347. REQUIRE( accu(abs(Y - cx_double(2,3)*conj(Dt))) == Approx(0.0) );
  348. }
  349. TEST_CASE("op_trans_sp_mat")
  350. {
  351. SpMat<unsigned int> a(4, 4);
  352. a(1, 0) = 5;
  353. a(2, 2) = 3;
  354. a(3, 3) = 4;
  355. a(1, 3) = 6;
  356. a(3, 1) = 8;
  357. SpMat<unsigned int> b = trans(a);
  358. REQUIRE( (unsigned int) b(0, 0) == 0 );
  359. REQUIRE( (unsigned int) b(1, 0) == 0 );
  360. REQUIRE( (unsigned int) b(2, 0) == 0 );
  361. REQUIRE( (unsigned int) b(3, 0) == 0 );
  362. REQUIRE( (unsigned int) b(0, 1) == 5 );
  363. REQUIRE( (unsigned int) b(1, 1) == 0 );
  364. REQUIRE( (unsigned int) b(2, 1) == 0 );
  365. REQUIRE( (unsigned int) b(3, 1) == 6 );
  366. REQUIRE( (unsigned int) b(0, 2) == 0 );
  367. REQUIRE( (unsigned int) b(1, 2) == 0 );
  368. REQUIRE( (unsigned int) b(2, 2) == 3 );
  369. REQUIRE( (unsigned int) b(3, 2) == 0 );
  370. REQUIRE( (unsigned int) b(0, 3) == 0 );
  371. REQUIRE( (unsigned int) b(1, 3) == 8 );
  372. REQUIRE( (unsigned int) b(2, 3) == 0 );
  373. REQUIRE( (unsigned int) b(3, 3) == 4 );
  374. b = trans(b); // inplace
  375. REQUIRE( (unsigned int) b(0, 0) == 0 );
  376. REQUIRE( (unsigned int) b(1, 0) == 5 );
  377. REQUIRE( (unsigned int) b(2, 0) == 0 );
  378. REQUIRE( (unsigned int) b(3, 0) == 0 );
  379. REQUIRE( (unsigned int) b(0, 1) == 0 );
  380. REQUIRE( (unsigned int) b(1, 1) == 0 );
  381. REQUIRE( (unsigned int) b(2, 1) == 0 );
  382. REQUIRE( (unsigned int) b(3, 1) == 8 );
  383. REQUIRE( (unsigned int) b(0, 2) == 0 );
  384. REQUIRE( (unsigned int) b(1, 2) == 0 );
  385. REQUIRE( (unsigned int) b(2, 2) == 3 );
  386. REQUIRE( (unsigned int) b(3, 2) == 0 );
  387. REQUIRE( (unsigned int) b(0, 3) == 0 );
  388. REQUIRE( (unsigned int) b(1, 3) == 6 );
  389. REQUIRE( (unsigned int) b(2, 3) == 0 );
  390. REQUIRE( (unsigned int) b(3, 3) == 4 );
  391. b = trans(a + a); // on an op
  392. REQUIRE( (unsigned int) b(0, 0) == 0 );
  393. REQUIRE( (unsigned int) b(1, 0) == 0 );
  394. REQUIRE( (unsigned int) b(2, 0) == 0 );
  395. REQUIRE( (unsigned int) b(3, 0) == 0 );
  396. REQUIRE( (unsigned int) b(0, 1) == 10 );
  397. REQUIRE( (unsigned int) b(1, 1) == 0 );
  398. REQUIRE( (unsigned int) b(2, 1) == 0 );
  399. REQUIRE( (unsigned int) b(3, 1) == 12 );
  400. REQUIRE( (unsigned int) b(0, 2) == 0 );
  401. REQUIRE( (unsigned int) b(1, 2) == 0 );
  402. REQUIRE( (unsigned int) b(2, 2) == 6 );
  403. REQUIRE( (unsigned int) b(3, 2) == 0 );
  404. REQUIRE( (unsigned int) b(0, 3) == 0 );
  405. REQUIRE( (unsigned int) b(1, 3) == 16 );
  406. REQUIRE( (unsigned int) b(2, 3) == 0 );
  407. REQUIRE( (unsigned int) b(3, 3) == 8 );
  408. b = trans(trans(a)); // on another trans
  409. REQUIRE( (unsigned int) b(0, 0) == 0 );
  410. REQUIRE( (unsigned int) b(1, 0) == 5 );
  411. REQUIRE( (unsigned int) b(2, 0) == 0 );
  412. REQUIRE( (unsigned int) b(3, 0) == 0 );
  413. REQUIRE( (unsigned int) b(0, 1) == 0 );
  414. REQUIRE( (unsigned int) b(1, 1) == 0 );
  415. REQUIRE( (unsigned int) b(2, 1) == 0 );
  416. REQUIRE( (unsigned int) b(3, 1) == 8 );
  417. REQUIRE( (unsigned int) b(0, 2) == 0 );
  418. REQUIRE( (unsigned int) b(1, 2) == 0 );
  419. REQUIRE( (unsigned int) b(2, 2) == 3 );
  420. REQUIRE( (unsigned int) b(3, 2) == 0 );
  421. REQUIRE( (unsigned int) b(0, 3) == 0 );
  422. REQUIRE( (unsigned int) b(1, 3) == 6 );
  423. REQUIRE( (unsigned int) b(2, 3) == 0 );
  424. REQUIRE( (unsigned int) b(3, 3) == 4 );
  425. }
  426. TEST_CASE("op_trans_sp_cxmat")
  427. {
  428. SpMat<std::complex<double> > a(10, 10);
  429. for (uword c = 0; c < 7; ++c)
  430. {
  431. a(c, c) = std::complex<double>(1.3, 2.4);
  432. a(c + 1, c) = std::complex<double>(0.0, -1.3);
  433. a(c + 2, c) = std::complex<double>(2.1, 0.0);
  434. }
  435. SpMat<std::complex<double> > b = trans(a);
  436. REQUIRE( b.n_nonzero == a.n_nonzero );
  437. for (uword r = 0; r < 10; ++r)
  438. {
  439. for (uword c = 0; c < 10; ++c)
  440. {
  441. double lr = real(std::complex<double>(a(r, c)));
  442. double rr = real(std::complex<double>(b(c, r)));
  443. double li = imag(std::complex<double>(a(r, c)));
  444. double ri = imag(std::complex<double>(b(c, r)));
  445. REQUIRE( lr == Approx(rr) );
  446. REQUIRE( li == Approx(-ri) );
  447. }
  448. }
  449. b = trans(a.submat(3, 3, 7, 7));
  450. REQUIRE( b.n_nonzero == a.submat(3, 3, 7, 7).n_nonzero );
  451. for (uword r = 0; r < 5; ++r)
  452. {
  453. for (uword c = 0; c < 5; ++c)
  454. {
  455. double lr = real(std::complex<double>(a(r + 3, c + 3)));
  456. double rr = real(std::complex<double>(b(c, r)));
  457. double li = imag(std::complex<double>(a(r + 3, c + 3)));
  458. double ri = imag(std::complex<double>(b(c, r)));
  459. REQUIRE( lr == Approx(rr) );
  460. REQUIRE( li == Approx(-ri) );
  461. }
  462. }
  463. }