fn_mean.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. // Copyright 2011-2017 Ryan Curtin (http://www.ratml.org/)
  2. // Copyright 2017 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_mean_spmat_empty_test")
  19. {
  20. SpMat<double> m(20, 25);
  21. SpRow<double> result = mean(m, 0);
  22. REQUIRE( result.n_nonzero == 0 );
  23. REQUIRE( result.n_rows == 1 );
  24. REQUIRE( result.n_cols == 25 );
  25. SpCol<double> result2 = mean(m, 1);
  26. REQUIRE( result2.n_nonzero == 0 );
  27. REQUIRE( result2.n_rows == 20 );
  28. REQUIRE( result2.n_cols == 1 );
  29. double r = mean(mean(m));
  30. REQUIRE( r == Approx(0.0) );
  31. // Now the same with subviews.
  32. result = mean(m.submat(2, 2, 11, 16));
  33. REQUIRE( result.n_nonzero == 0 );
  34. REQUIRE( result.n_rows == 1 );
  35. REQUIRE( result.n_cols == 15 );
  36. result2 = mean(m.submat(2, 2, 11, 16), 1);
  37. REQUIRE( result2.n_nonzero == 0 );
  38. REQUIRE( result2.n_rows == 10 );
  39. REQUIRE( result2.n_cols == 1 );
  40. r = mean(mean(m.submat(2, 2, 11, 16)));
  41. REQUIRE( r == Approx(0.0) );
  42. // And with an operation.
  43. result = mean(trans(m));
  44. REQUIRE( result.n_nonzero == 0 );
  45. REQUIRE( result.n_rows == 1 );
  46. REQUIRE( result.n_cols == 20 );
  47. result2 = mean(trans(m), 1);
  48. REQUIRE( result2.n_nonzero == 0 );
  49. REQUIRE( result2.n_rows == 25 );
  50. REQUIRE( result2.n_cols == 1 );
  51. r = mean(mean(trans(m)));
  52. REQUIRE( r == Approx(0.0) );
  53. }
  54. TEST_CASE("fn_mean_spcxmat_empty_test")
  55. {
  56. // Now with complex numbers.
  57. SpMat<std::complex<double> > m(20, 25);
  58. SpRow<std::complex<double> > result = mean(m, 0);
  59. REQUIRE( result.n_nonzero == 0 );
  60. REQUIRE( result.n_rows == 1 );
  61. REQUIRE( result.n_cols == 25 );
  62. SpCol<std::complex<double> > result2 = mean(m, 1);
  63. REQUIRE( result2.n_nonzero == 0 );
  64. REQUIRE( result2.n_rows == 20 );
  65. REQUIRE( result2.n_cols == 1 );
  66. std::complex<double> r = mean(mean(m));
  67. REQUIRE( real(r) == Approx(0.0) );
  68. REQUIRE( imag(r) == Approx(0.0) );
  69. // Now the same with subviews.
  70. result = mean(m.submat(2, 2, 11, 16));
  71. REQUIRE( result.n_nonzero == 0 );
  72. REQUIRE( result.n_rows == 1 );
  73. REQUIRE( result.n_cols == 15 );
  74. result2 = mean(m.submat(2, 2, 11, 16), 1);
  75. REQUIRE( result2.n_nonzero == 0 );
  76. REQUIRE( result2.n_rows == 10 );
  77. REQUIRE( result2.n_cols == 1 );
  78. r = mean(mean(m.submat(2, 2, 11, 16)));
  79. REQUIRE( real(r) == Approx(0.0) );
  80. REQUIRE( imag(r) == Approx(0.0) );
  81. // And with an operation.
  82. result = mean(trans(m));
  83. REQUIRE( result.n_nonzero == 0 );
  84. REQUIRE( result.n_rows == 1 );
  85. REQUIRE( result.n_cols == 20 );
  86. result2 = mean(trans(m), 1);
  87. REQUIRE( result2.n_nonzero == 0 );
  88. REQUIRE( result2.n_rows == 25 );
  89. REQUIRE( result2.n_cols == 1 );
  90. r = mean(mean(trans(m)));
  91. REQUIRE( real(r) == Approx(0.0) );
  92. REQUIRE( imag(r) == Approx(0.0) );
  93. }
  94. TEST_CASE("fn_mean_spmat_test")
  95. {
  96. // Create a random matrix and do mean testing on it, with varying levels of
  97. // nonzero (eventually this becomes a fully dense matrix).
  98. for (int i = 0; i < 10; ++i)
  99. {
  100. SpMat<double> x;
  101. x.sprandu(50, 75, ((double) (i + 1)) / 10);
  102. mat d(x);
  103. SpRow<double> rr = mean(x);
  104. rowvec drr = mean(d);
  105. REQUIRE( rr.n_rows == 1 );
  106. REQUIRE( rr.n_cols == 75 );
  107. for (uword j = 0; j < 75; ++j)
  108. REQUIRE( drr[j] == Approx((double) rr[j]) );
  109. SpCol<double> cr = mean(x, 1);
  110. vec dcr = mean(d, 1);
  111. REQUIRE( cr.n_rows == 50 );
  112. REQUIRE( cr.n_cols == 1 );
  113. for (uword j = 0; j < 50; ++j)
  114. REQUIRE( dcr[j] == Approx((double) cr[j]) );
  115. double dr = mean(mean(x));
  116. double ddr = mean(mean(d));
  117. REQUIRE( dr == Approx(ddr) );
  118. // Now on a subview.
  119. rr = mean(x.submat(11, 11, 30, 45), 0);
  120. drr = mean(d.submat(11, 11, 30, 45), 0);
  121. REQUIRE( rr.n_rows == 1 );
  122. REQUIRE( rr.n_cols == 35 );
  123. for (uword j = 0; j < 35; ++j)
  124. REQUIRE( drr[j] == Approx((double) rr[j]) );
  125. cr = mean(x.submat(11, 11, 30, 45), 1);
  126. dcr = mean(d.submat(11, 11, 30, 45), 1);
  127. REQUIRE( cr.n_rows == 20 );
  128. REQUIRE( cr.n_cols == 1 );
  129. for (uword j = 0; j < 20; ++j)
  130. REQUIRE( dcr[j] == Approx((double) cr[j]) );
  131. dr = mean(mean(x.submat(11, 11, 30, 45)));
  132. ddr = mean(mean(d.submat(11, 11, 30, 45)));
  133. REQUIRE( dr == Approx(ddr) );
  134. // Now on an SpOp (spop_scalar_times)
  135. rr = mean(3.0 * x);
  136. drr = mean(3.0 * d);
  137. REQUIRE( rr.n_rows == 1 );
  138. REQUIRE( rr.n_cols == 75 );
  139. for (uword j = 0; j < 75; ++j)
  140. REQUIRE( drr[j] == Approx((double) rr[j]) );
  141. cr = mean(4.5 * x, 1);
  142. dcr = mean(4.5 * d, 1);
  143. REQUIRE( cr.n_rows == 50 );
  144. REQUIRE( cr.n_cols == 1 );
  145. for (uword j = 0; j < 50; ++j)
  146. REQUIRE( dcr[j] == Approx((double) cr[j]) );
  147. dr = mean(mean(1.2 * x));
  148. ddr = mean(mean(1.2 * d));
  149. REQUIRE( dr == Approx(ddr) );
  150. // Now on an SpGlue!
  151. SpMat<double> y;
  152. y.sprandu(50, 75, 0.3);
  153. mat e(y);
  154. rr = mean(x + y);
  155. drr = mean(d + e);
  156. REQUIRE( rr.n_rows == 1 );
  157. REQUIRE( rr.n_cols == 75 );
  158. for (uword j = 0; j < 75; ++j)
  159. REQUIRE( drr[j] == Approx((double) rr[j]) );
  160. cr = mean(x + y, 1);
  161. dcr = mean(d + e, 1);
  162. REQUIRE( cr.n_rows == 50 );
  163. REQUIRE( cr.n_cols == 1 );
  164. for (uword j = 0; j < 50; ++j)
  165. REQUIRE( dcr[j] == Approx((double) cr[j]) );
  166. dr = mean(mean(x + y));
  167. ddr = mean(mean(d + e));
  168. REQUIRE( dr == Approx(ddr) );
  169. }
  170. }
  171. TEST_CASE("fn_mean_spcxmat_test")
  172. {
  173. // Create a random matrix and do mean testing on it, with varying levels of
  174. // nonzero (eventually this becomes a fully dense matrix).
  175. for (int i = 0; i < 10; ++i)
  176. {
  177. SpMat<std::complex<double> > x;
  178. x.sprandu(50, 75, ((double) (i + 1)) / 10);
  179. cx_mat d(x);
  180. SpRow<std::complex<double> > rr = mean(x);
  181. cx_rowvec drr = mean(d);
  182. REQUIRE( rr.n_rows == 1 );
  183. REQUIRE( rr.n_cols == 75 );
  184. for (uword j = 0; j < 75; ++j)
  185. {
  186. REQUIRE( real(drr[j]) == Approx(real((std::complex<double>) rr[j])) );
  187. REQUIRE( imag(drr[j]) == Approx(imag((std::complex<double>) rr[j])) );
  188. }
  189. SpCol<std::complex<double> > cr = mean(x, 1);
  190. cx_vec dcr = mean(d, 1);
  191. REQUIRE( cr.n_rows == 50 );
  192. REQUIRE( cr.n_cols == 1 );
  193. for (uword j = 0; j < 50; ++j)
  194. {
  195. REQUIRE( real(dcr[j]) == Approx(real((std::complex<double>) cr[j])) );
  196. REQUIRE( imag(dcr[j]) == Approx(imag((std::complex<double>) cr[j])) );
  197. }
  198. std::complex<double> dr = mean(mean(x));
  199. std::complex<double> ddr = mean(mean(d));
  200. REQUIRE( real(dr) == Approx(real(ddr)) );
  201. REQUIRE( imag(dr) == Approx(imag(ddr)) );
  202. // Now on a subview.
  203. rr = mean(x.submat(11, 11, 30, 45), 0);
  204. drr = mean(d.submat(11, 11, 30, 45), 0);
  205. REQUIRE( rr.n_rows == 1 );
  206. REQUIRE( rr.n_cols == 35 );
  207. for (uword j = 0; j < 35; ++j)
  208. {
  209. REQUIRE( real(drr[j]) == Approx(real((std::complex<double>) rr[j])) );
  210. REQUIRE( imag(drr[j]) == Approx(imag((std::complex<double>) rr[j])) );
  211. }
  212. cr = mean(x.submat(11, 11, 30, 45), 1);
  213. dcr = mean(d.submat(11, 11, 30, 45), 1);
  214. REQUIRE( cr.n_rows == 20 );
  215. REQUIRE( cr.n_cols == 1 );
  216. for (uword j = 0; j < 20; ++j)
  217. {
  218. REQUIRE( real(dcr[j]) == Approx(real((std::complex<double>) cr[j])) );
  219. REQUIRE( imag(dcr[j]) == Approx(imag((std::complex<double>) cr[j])) );
  220. }
  221. dr = mean(mean(x.submat(11, 11, 30, 45)));
  222. ddr = mean(mean(d.submat(11, 11, 30, 45)));
  223. REQUIRE( real(dr) == Approx(real(ddr)) );
  224. REQUIRE( imag(dr) == Approx(imag(ddr)) );
  225. // Now on an SpOp (spop_scalar_times)
  226. rr = mean(3.0 * x);
  227. drr = mean(3.0 * d);
  228. REQUIRE( rr.n_rows == 1 );
  229. REQUIRE( rr.n_cols == 75 );
  230. for (uword j = 0; j < 75; ++j)
  231. {
  232. REQUIRE( real(drr[j]) == Approx(real((std::complex<double>) rr[j])) );
  233. REQUIRE( imag(drr[j]) == Approx(imag((std::complex<double>) rr[j])) );
  234. }
  235. cr = mean(4.5 * x, 1);
  236. dcr = mean(4.5 * d, 1);
  237. REQUIRE( cr.n_rows == 50 );
  238. REQUIRE( cr.n_cols == 1 );
  239. for (uword j = 0; j < 50; ++j)
  240. {
  241. REQUIRE( real(dcr[j]) == Approx(real((std::complex<double>) cr[j])) );
  242. REQUIRE( imag(dcr[j]) == Approx(imag((std::complex<double>) cr[j])) );
  243. }
  244. dr = mean(mean(1.2 * x));
  245. ddr = mean(mean(1.2 * d));
  246. REQUIRE( real(dr) == Approx(real(ddr)) );
  247. REQUIRE( imag(dr) == Approx(imag(ddr)) );
  248. // Now on an SpGlue!
  249. SpMat<std::complex<double> > y;
  250. y.sprandu(50, 75, 0.3);
  251. cx_mat e(y);
  252. rr = mean(x + y);
  253. drr = mean(d + e);
  254. REQUIRE( rr.n_rows == 1 );
  255. REQUIRE( rr.n_cols == 75 );
  256. for (uword j = 0; j < 75; ++j)
  257. {
  258. REQUIRE( real(drr[j]) == Approx(real((std::complex<double>) rr[j])) );
  259. REQUIRE( imag(drr[j]) == Approx(imag((std::complex<double>) rr[j])) );
  260. }
  261. cr = mean(x + y, 1);
  262. dcr = mean(d + e, 1);
  263. REQUIRE( cr.n_rows == 50 );
  264. REQUIRE( cr.n_cols == 1 );
  265. for (uword j = 0; j < 50; ++j)
  266. {
  267. REQUIRE( real(dcr[j]) == Approx(real((std::complex<double>) cr[j])) );
  268. REQUIRE( imag(dcr[j]) == Approx(imag((std::complex<double>) cr[j])) );
  269. }
  270. dr = mean(mean(x + y));
  271. ddr = mean(mean(d + e));
  272. REQUIRE( real(dr) == Approx(real(ddr)) );
  273. REQUIRE( imag(dr) == Approx(imag(ddr)) );
  274. }
  275. }
  276. TEST_CASE("fn_mean_sp_vector_test")
  277. {
  278. // Test mean() on vectors.
  279. SpCol<double> c(1000);
  280. SpCol<double> cr = mean(c, 0);
  281. REQUIRE( cr.n_rows == 1 );
  282. REQUIRE( cr.n_cols == 1 );
  283. REQUIRE( (double) cr[0] == Approx(0.0) );
  284. cr = mean(c, 1);
  285. REQUIRE( cr.n_rows == 1000 );
  286. REQUIRE( cr.n_cols == 1 );
  287. for (uword i = 0; i < 1000; ++i)
  288. {
  289. REQUIRE( (double) cr[i] == Approx(0.0) );
  290. }
  291. double ddcr = mean(c);
  292. REQUIRE( ddcr == Approx(0.0) );
  293. c.sprandu(1000, 1, 0.3);
  294. vec dc(c);
  295. cr = mean(c, 0);
  296. vec dcr = mean(dc, 0);
  297. REQUIRE( cr.n_rows == 1 );
  298. REQUIRE( cr.n_cols == 1 );
  299. REQUIRE( (double) cr[0] == Approx(dcr[0]) );
  300. cr = mean(c, 1);
  301. dcr = mean(dc, 1);
  302. REQUIRE( cr.n_rows == 1000 );
  303. REQUIRE( cr.n_cols == 1 );
  304. for (uword i = 0; i < 1000; ++i)
  305. {
  306. REQUIRE( (double) cr[i] == Approx(dcr[i]) );
  307. }
  308. ddcr = mean(c);
  309. double dddr = mean(dc);
  310. REQUIRE( ddcr == Approx(dddr) );
  311. SpRow<double> r;
  312. r.sprandu(1, 1000, 0.3);
  313. rowvec dr(r);
  314. SpRow<double> rr = mean(r, 0);
  315. rowvec drr = mean(dr, 0);
  316. REQUIRE( rr.n_rows == 1 );
  317. REQUIRE( rr.n_cols == 1000 );
  318. for (uword i = 0; i < 1000; ++i)
  319. {
  320. REQUIRE( (double) rr[i] == Approx(drr[i]) );
  321. }
  322. rr = mean(r, 1);
  323. drr = mean(dr, 1);
  324. REQUIRE( rr.n_rows == 1 );
  325. REQUIRE( rr.n_cols == 1 );
  326. REQUIRE( (double) rr[0] == Approx(drr[0]) );
  327. ddcr = mean(r);
  328. dddr = mean(dr);
  329. REQUIRE( ddcr == Approx(dddr) );
  330. }
  331. TEST_CASE("fn_mean_sp_cx_vector_test")
  332. {
  333. // Test mean() on vectors.
  334. SpCol<std::complex<double> > c(1000);
  335. SpCol<std::complex<double> > cr = mean(c, 0);
  336. REQUIRE( cr.n_rows == 1 );
  337. REQUIRE( cr.n_cols == 1 );
  338. REQUIRE( real((std::complex<double>) cr[0]) == Approx(0.0) );
  339. REQUIRE( imag((std::complex<double>) cr[0]) == Approx(0.0) );
  340. cr = mean(c, 1);
  341. REQUIRE( cr.n_rows == 1000 );
  342. REQUIRE( cr.n_cols == 1 );
  343. for (uword i = 0; i < 1000; ++i)
  344. {
  345. REQUIRE( real((std::complex<double>) cr[i]) == Approx(0.0) );
  346. REQUIRE( imag((std::complex<double>) cr[i]) == Approx(0.0) );
  347. }
  348. std::complex<double> ddcr = mean(c);
  349. REQUIRE( real(ddcr) == Approx(0.0) );
  350. REQUIRE( imag(ddcr) == Approx(0.0) );
  351. c.sprandu(1000, 1, 0.3);
  352. cx_vec dc(c);
  353. cr = mean(c, 0);
  354. cx_vec dcr = mean(dc, 0);
  355. REQUIRE( cr.n_rows == 1 );
  356. REQUIRE( cr.n_cols == 1 );
  357. REQUIRE( real((std::complex<double>) cr[0]) == Approx(real(dcr[0])) );
  358. REQUIRE( imag((std::complex<double>) cr[0]) == Approx(imag(dcr[0])) );
  359. cr = mean(c, 1);
  360. dcr = mean(dc, 1);
  361. REQUIRE( cr.n_rows == 1000 );
  362. REQUIRE( cr.n_cols == 1 );
  363. for (uword i = 0; i < 1000; ++i)
  364. {
  365. REQUIRE( real((std::complex<double>) cr[i]) == Approx(real(dcr[i])) );
  366. REQUIRE( imag((std::complex<double>) cr[i]) == Approx(imag(dcr[i])) );
  367. }
  368. ddcr = mean(c);
  369. std::complex<double> dddr = mean(dc);
  370. REQUIRE( real(ddcr) == Approx(real(dddr)) );
  371. REQUIRE( imag(ddcr) == Approx(imag(dddr)) );
  372. SpRow<std::complex<double> > r;
  373. r.sprandu(1, 1000, 0.3);
  374. cx_rowvec dr(r);
  375. SpRow<std::complex<double> > rr = mean(r, 0);
  376. cx_rowvec drr = mean(dr, 0);
  377. REQUIRE( rr.n_rows == 1 );
  378. REQUIRE( rr.n_cols == 1000 );
  379. for (uword i = 0; i < 1000; ++i)
  380. {
  381. REQUIRE( real((std::complex<double>) rr[i]) == Approx(real(drr[i])) );
  382. REQUIRE( imag((std::complex<double>) rr[i]) == Approx(imag(drr[i])) );
  383. }
  384. rr = mean(r, 1);
  385. drr = mean(dr, 1);
  386. REQUIRE( rr.n_rows == 1 );
  387. REQUIRE( rr.n_cols == 1 );
  388. REQUIRE( real((std::complex<double>) rr[0]) == Approx(real(drr[0])) );
  389. REQUIRE( imag((std::complex<double>) rr[0]) == Approx(imag(drr[0])) );
  390. ddcr = mean(r);
  391. dddr = mean(dr);
  392. REQUIRE( real(ddcr) == Approx(real(dddr)) );
  393. REQUIRE( imag(ddcr) == Approx(imag(dddr)) );
  394. }
  395. TEST_CASE("fn_mean_robust_sparse_test")
  396. {
  397. // Create a sparse matrix with values that will overflow.
  398. SpMat<double> x;
  399. x.sprandu(50, 75, 0.1);
  400. for (SpMat<double>::iterator i = x.begin(); i != x.end(); ++i)
  401. {
  402. (*i) *= std::numeric_limits<double>::max();
  403. }
  404. mat d(x);
  405. SpRow<double> rr = mean(x);
  406. rowvec drr = mean(d);
  407. REQUIRE( rr.n_rows == 1 );
  408. REQUIRE( rr.n_cols == 75 );
  409. for (uword j = 0; j < 75; ++j)
  410. {
  411. REQUIRE( drr[j] == Approx((double) rr[j]) );
  412. }
  413. SpCol<double> cr = mean(x, 1);
  414. vec dcr = mean(d, 1);
  415. REQUIRE( cr.n_rows == 50 );
  416. REQUIRE( cr.n_cols == 1 );
  417. for (uword j = 0; j < 50; ++j)
  418. {
  419. REQUIRE( dcr[j] == Approx((double) cr[j]) );
  420. }
  421. double dr = mean(mean(x));
  422. double ddr = mean(mean(d));
  423. REQUIRE( dr == Approx(ddr) );
  424. // Now on a subview.
  425. rr = mean(x.submat(11, 11, 30, 45), 0);
  426. drr = mean(d.submat(11, 11, 30, 45), 0);
  427. REQUIRE( rr.n_rows == 1 );
  428. REQUIRE( rr.n_cols == 35 );
  429. for (uword j = 0; j < 35; ++j)
  430. {
  431. REQUIRE( drr[j] == Approx((double) rr[j]) );
  432. }
  433. cr = mean(x.submat(11, 11, 30, 45), 1);
  434. dcr = mean(d.submat(11, 11, 30, 45), 1);
  435. REQUIRE( cr.n_rows == 20 );
  436. REQUIRE( cr.n_cols == 1 );
  437. for (uword j = 0; j < 20; ++j)
  438. {
  439. REQUIRE( dcr[j] == Approx((double) cr[j]) );
  440. }
  441. dr = mean(mean(x.submat(11, 11, 30, 45)));
  442. ddr = mean(mean(d.submat(11, 11, 30, 45)));
  443. REQUIRE( dr == Approx(ddr) );
  444. // Now on an SpOp (spop_scalar_times)
  445. rr = mean(0.4 * x);
  446. drr = mean(0.4 * d);
  447. REQUIRE( rr.n_rows == 1 );
  448. REQUIRE( rr.n_cols == 75 );
  449. for (uword j = 0; j < 75; ++j)
  450. {
  451. REQUIRE( drr[j] == Approx((double) rr[j]) );
  452. }
  453. cr = mean(0.1 * x, 1);
  454. dcr = mean(0.1 * d, 1);
  455. REQUIRE( cr.n_rows == 50 );
  456. REQUIRE( cr.n_cols == 1 );
  457. for (uword j = 0; j < 50; ++j)
  458. {
  459. REQUIRE( dcr[j] == Approx((double) cr[j]) );
  460. }
  461. dr = mean(mean(0.7 * x));
  462. ddr = mean(mean(0.7 * d));
  463. REQUIRE( dr == Approx(ddr) );
  464. // Now on an SpGlue!
  465. SpMat<double> y;
  466. y.sprandu(50, 75, 0.3);
  467. for (SpMat<double>::iterator i = y.begin(); i != y.end(); ++i)
  468. {
  469. (*i) *= std::numeric_limits<double>::max();
  470. }
  471. mat e(y);
  472. rr = mean(0.5 * x + 0.5 * y);
  473. drr = mean(0.5 * d + 0.5 * e);
  474. REQUIRE( rr.n_rows == 1 );
  475. REQUIRE( rr.n_cols == 75 );
  476. for (uword j = 0; j < 75; ++j)
  477. {
  478. REQUIRE( drr[j] == Approx((double) rr[j]) );
  479. }
  480. cr = mean(0.5 * x + 0.5 * y, 1);
  481. dcr = mean(0.5 * d + 0.5 * e, 1);
  482. REQUIRE( cr.n_rows == 50 );
  483. REQUIRE( cr.n_cols == 1 );
  484. for (uword j = 0; j < 50; ++j)
  485. {
  486. REQUIRE( dcr[j] == Approx((double) cr[j]) );
  487. }
  488. dr = mean(mean(0.5 * x + 0.5 * y));
  489. ddr = mean(mean(0.5 * d + 0.5 * e));
  490. REQUIRE( dr == Approx(ddr) );
  491. }
  492. TEST_CASE("fn_mean_robust_cx_sparse_test")
  493. {
  494. SpMat<std::complex<double> > x;
  495. x.sprandu(50, 75, 0.3);
  496. for (SpMat<std::complex<double> >::iterator i = x.begin(); i != x.end(); ++i)
  497. {
  498. (*i) *= std::numeric_limits<double>::max();
  499. }
  500. cx_mat d(x);
  501. SpRow<std::complex<double> > rr = mean(x);
  502. cx_rowvec drr = mean(d);
  503. REQUIRE( rr.n_rows == 1 );
  504. REQUIRE( rr.n_cols == 75 );
  505. for (uword j = 0; j < 75; ++j)
  506. {
  507. REQUIRE( real(drr[j]) == Approx(real((std::complex<double>) rr[j])) );
  508. REQUIRE( imag(drr[j]) == Approx(imag((std::complex<double>) rr[j])) );
  509. }
  510. SpCol<std::complex<double> > cr = mean(x, 1);
  511. cx_vec dcr = mean(d, 1);
  512. REQUIRE( cr.n_rows == 50 );
  513. REQUIRE( cr.n_cols == 1 );
  514. for (uword j = 0; j < 50; ++j)
  515. {
  516. REQUIRE( real(dcr[j]) == Approx(real((std::complex<double>) cr[j])) );
  517. REQUIRE( imag(dcr[j]) == Approx(imag((std::complex<double>) cr[j])) );
  518. }
  519. std::complex<double> dr = mean(mean(x));
  520. std::complex<double> ddr = mean(mean(d));
  521. REQUIRE( real(dr) == Approx(real(ddr)) );
  522. REQUIRE( imag(dr) == Approx(imag(ddr)) );
  523. // Now on a subview.
  524. rr = mean(x.submat(11, 11, 30, 45), 0);
  525. drr = mean(d.submat(11, 11, 30, 45), 0);
  526. REQUIRE( rr.n_rows == 1 );
  527. REQUIRE( rr.n_cols == 35 );
  528. for (uword j = 0; j < 35; ++j)
  529. {
  530. REQUIRE( real(drr[j]) == Approx(real((std::complex<double>) rr[j])) );
  531. REQUIRE( imag(drr[j]) == Approx(imag((std::complex<double>) rr[j])) );
  532. }
  533. cr = mean(x.submat(11, 11, 30, 45), 1);
  534. dcr = mean(d.submat(11, 11, 30, 45), 1);
  535. REQUIRE( cr.n_rows == 20 );
  536. REQUIRE( cr.n_cols == 1 );
  537. for (uword j = 0; j < 20; ++j)
  538. {
  539. REQUIRE( real(dcr[j]) == Approx(real((std::complex<double>) cr[j])) );
  540. REQUIRE( imag(dcr[j]) == Approx(imag((std::complex<double>) cr[j])) );
  541. }
  542. dr = mean(mean(x.submat(11, 11, 30, 45)));
  543. ddr = mean(mean(d.submat(11, 11, 30, 45)));
  544. REQUIRE( real(dr) == Approx(real(ddr)) );
  545. REQUIRE( imag(dr) == Approx(imag(ddr)) );
  546. // Now on an SpOp (spop_scalar_times)
  547. rr = mean(0.5 * x);
  548. drr = mean(0.5 * d);
  549. REQUIRE( rr.n_rows == 1 );
  550. REQUIRE( rr.n_cols == 75 );
  551. for (uword j = 0; j < 75; ++j)
  552. {
  553. REQUIRE( real(drr[j]) == Approx(real((std::complex<double>) rr[j])) );
  554. REQUIRE( imag(drr[j]) == Approx(imag((std::complex<double>) rr[j])) );
  555. }
  556. cr = mean(0.7 * x, 1);
  557. dcr = mean(0.7 * d, 1);
  558. REQUIRE( cr.n_rows == 50 );
  559. REQUIRE( cr.n_cols == 1 );
  560. for (uword j = 0; j < 50; ++j)
  561. {
  562. REQUIRE( real(dcr[j]) == Approx(real((std::complex<double>) cr[j])) );
  563. REQUIRE( imag(dcr[j]) == Approx(imag((std::complex<double>) cr[j])) );
  564. }
  565. dr = mean(mean(0.6 * x));
  566. ddr = mean(mean(0.6 * d));
  567. REQUIRE( real(dr) == Approx(real(ddr)) );
  568. REQUIRE( imag(dr) == Approx(imag(ddr)) );
  569. // Now on an SpGlue!
  570. SpMat<std::complex<double> > y;
  571. y.sprandu(50, 75, 0.3);
  572. for (SpMat<std::complex<double> >::iterator i = y.begin(); i != y.end(); ++i)
  573. {
  574. (*i) *= std::numeric_limits<double>::max();
  575. }
  576. cx_mat e(y);
  577. rr = mean(0.5 * x + 0.5 * y);
  578. drr = mean(0.5 * d + 0.5 * e);
  579. REQUIRE( rr.n_rows == 1 );
  580. REQUIRE( rr.n_cols == 75 );
  581. for (uword j = 0; j < 75; ++j)
  582. {
  583. REQUIRE( real(drr[j]) == Approx(real((std::complex<double>) rr[j])) );
  584. REQUIRE( imag(drr[j]) == Approx(imag((std::complex<double>) rr[j])) );
  585. }
  586. cr = mean(0.5 * x + 0.5 * y, 1);
  587. dcr = mean(0.5 * d + 0.5 * e, 1);
  588. REQUIRE( cr.n_rows == 50 );
  589. REQUIRE( cr.n_cols == 1 );
  590. for (uword j = 0; j < 50; ++j)
  591. {
  592. REQUIRE( real(dcr[j]) == Approx(real((std::complex<double>) cr[j])) );
  593. REQUIRE( imag(dcr[j]) == Approx(imag((std::complex<double>) cr[j])) );
  594. }
  595. dr = mean(mean(0.5 * x + 0.5 * y));
  596. ddr = mean(mean(0.5 * d + 0.5 * e));
  597. REQUIRE( real(dr) == Approx(real(ddr)) );
  598. REQUIRE( imag(dr) == Approx(imag(ddr)) );
  599. }
  600. TEST_CASE("fn_mean_robust_sparse_vector_test")
  601. {
  602. // Test mean() on vectors.
  603. SpCol<double> c(1000);
  604. SpCol<double> cr;
  605. double ddcr;
  606. c.sprandu(1000, 1, 0.3);
  607. for (SpCol<double>::iterator i = c.begin(); i != c.end(); ++i)
  608. {
  609. (*i) *= (std::numeric_limits<double>::max());
  610. }
  611. vec dc(c);
  612. cr = mean(c, 0);
  613. vec dcr = mean(dc, 0);
  614. REQUIRE( cr.n_rows == 1 );
  615. REQUIRE( cr.n_cols == 1 );
  616. REQUIRE( (double) cr[0] == Approx(dcr[0]) );
  617. cr = mean(c, 1);
  618. dcr = mean(dc, 1);
  619. REQUIRE( cr.n_rows == 1000 );
  620. REQUIRE( cr.n_cols == 1 );
  621. for (uword i = 0; i < 1000; ++i)
  622. {
  623. REQUIRE( (double) cr[i] == Approx(dcr[i]) );
  624. }
  625. ddcr = mean(c);
  626. double dddr = mean(dc);
  627. REQUIRE( ddcr == Approx(dddr) );
  628. SpRow<double> r;
  629. r.sprandu(1, 1000, 0.3);
  630. for (SpRow<double>::iterator i = r.begin(); i != r.end(); ++i)
  631. {
  632. (*i) *= (std::numeric_limits<double>::max());
  633. }
  634. rowvec dr(r);
  635. SpRow<double> rr = mean(r, 0);
  636. rowvec drr = mean(dr, 0);
  637. REQUIRE( rr.n_rows == 1 );
  638. REQUIRE( rr.n_cols == 1000 );
  639. for (uword i = 0; i < 1000; ++i)
  640. {
  641. REQUIRE( (double) rr[i] == Approx(drr[i]) );
  642. }
  643. rr = mean(r, 1);
  644. drr = mean(dr, 1);
  645. REQUIRE( rr.n_rows == 1 );
  646. REQUIRE( rr.n_cols == 1 );
  647. REQUIRE( (double) rr[0] == Approx(drr[0]) );
  648. ddcr = mean(r);
  649. dddr = mean(dr);
  650. REQUIRE( ddcr == Approx(dddr) );
  651. }
  652. TEST_CASE("fn_mean_robust_cx_sparse_vector_test")
  653. {
  654. // Test mean() on vectors.
  655. SpCol<std::complex<double> > c(1000);
  656. SpCol<std::complex<double> > cr;
  657. std::complex<double> ddcr;
  658. c.sprandu(1000, 1, 0.3);
  659. for (SpCol<std::complex<double> >::iterator i = c.begin(); i != c.end(); ++i)
  660. {
  661. (*i) *= (std::numeric_limits<double>::max());
  662. }
  663. cx_vec dc(c);
  664. cr = mean(c, 0);
  665. cx_vec dcr = mean(dc, 0);
  666. REQUIRE( cr.n_rows == 1 );
  667. REQUIRE( cr.n_cols == 1 );
  668. REQUIRE( real((std::complex<double>) cr[0]) == Approx(real(dcr[0])) );
  669. REQUIRE( imag((std::complex<double>) cr[0]) == Approx(imag(dcr[0])) );
  670. cr = mean(c, 1);
  671. dcr = mean(dc, 1);
  672. REQUIRE( cr.n_rows == 1000 );
  673. REQUIRE( cr.n_cols == 1 );
  674. for (uword i = 0; i < 1000; ++i)
  675. {
  676. REQUIRE( real((std::complex<double>) cr[i]) == Approx(real(dcr[i])) );
  677. REQUIRE( imag((std::complex<double>) cr[i]) == Approx(imag(dcr[i])) );
  678. }
  679. ddcr = mean(c);
  680. std::complex<double> dddr = mean(dc);
  681. REQUIRE( real(ddcr) == Approx(real(dddr)) );
  682. REQUIRE( imag(ddcr) == Approx(imag(dddr)) );
  683. SpRow<std::complex<double> > r;
  684. r.sprandu(1, 1000, 0.3);
  685. cx_rowvec dr(r);
  686. SpRow<std::complex<double> > rr = mean(r, 0);
  687. cx_rowvec drr = mean(dr, 0);
  688. REQUIRE( rr.n_rows == 1 );
  689. REQUIRE( rr.n_cols == 1000 );
  690. for (uword i = 0; i < 1000; ++i)
  691. {
  692. REQUIRE( real((std::complex<double>) rr[i]) == Approx(real(drr[i])) );
  693. REQUIRE( imag((std::complex<double>) rr[i]) == Approx(imag(drr[i])) );
  694. }
  695. rr = mean(r, 1);
  696. drr = mean(dr, 1);
  697. REQUIRE( rr.n_rows == 1 );
  698. REQUIRE( rr.n_cols == 1 );
  699. REQUIRE( real((std::complex<double>) rr[0]) == Approx(real(drr[0])) );
  700. REQUIRE( imag((std::complex<double>) rr[0]) == Approx(imag(drr[0])) );
  701. ddcr = mean(r);
  702. dddr = mean(dr);
  703. REQUIRE( real(ddcr) == Approx(real(dddr)) );
  704. REQUIRE( imag(ddcr) == Approx(imag(dddr)) );
  705. }
  706. TEST_CASE("fn_mean_sparse_alias_test")
  707. {
  708. sp_mat s;
  709. s.sprandu(70, 70, 0.3);
  710. mat d(s);
  711. s = mean(s);
  712. d = mean(d);
  713. REQUIRE( d.n_rows == s.n_rows );
  714. REQUIRE( d.n_cols == s.n_cols );
  715. for (uword i = 0; i < d.n_elem; ++i)
  716. {
  717. REQUIRE( d[i] == Approx((double) s[i]) );
  718. }
  719. s.sprandu(70, 70, 0.3);
  720. d = s;
  721. s = mean(s, 1);
  722. d = mean(d, 1);
  723. for (uword i = 0; i < d.n_elem; ++i)
  724. {
  725. REQUIRE( d[i] == Approx((double) s[i]) );
  726. }
  727. }