spmat.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231
  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. // Does the matrix correctly report when it is empty?
  19. TEST_CASE("empty_test")
  20. {
  21. bool testPassed = true;
  22. sp_imat test;
  23. REQUIRE( test.is_empty() );
  24. test.set_size(3, 4);
  25. REQUIRE( test.is_empty() == false );
  26. }
  27. // Can we insert items into the matrix correctly?
  28. TEST_CASE("insertion_test")
  29. {
  30. int correctResult[3][4] =
  31. {{1, 0, 0, 0},
  32. {2, 3, 1, 0},
  33. {0, 9, 4, 0}};
  34. // Now run the same test for the Armadillo sparse matrix.
  35. SpMat<int> arma_test;
  36. arma_test.set_size(3, 4);
  37. // Fill the matrix (hopefully).
  38. arma_test(0, 0) = 1;
  39. arma_test(1, 0) = 2;
  40. arma_test(1, 1) = 3;
  41. arma_test(2, 1) = 9;
  42. arma_test(1, 2) = 1;
  43. arma_test(2, 2) = 4;
  44. for (uword i = 0; i < 3; i++)
  45. {
  46. for (uword j = 0; j < 4; j++)
  47. {
  48. REQUIRE( (int) arma_test(i, j) == correctResult[i][j] );
  49. }
  50. }
  51. }
  52. // Does sparse-sparse matrix multiplication work?
  53. TEST_CASE("full_sparse_sparse_matrix_multiplication_test")
  54. {
  55. // Now perform the test again for SpMat.
  56. SpMat<int> spa(3, 3);
  57. SpMat<int> spb(3, 2);
  58. int correctResult[3][2] =
  59. {{ 46, 60},
  60. { 40, 52},
  61. {121, 160}};
  62. spa(0, 0) = 1;
  63. spa(0, 1) = 10;
  64. spa(0, 2) = 3;
  65. spa(1, 0) = 3;
  66. spa(1, 1) = 4;
  67. spa(1, 2) = 5;
  68. spa(2, 0) = 12;
  69. spa(2, 1) = 13;
  70. spa(2, 2) = 14;
  71. spb(0, 0) = 1;
  72. spb(0, 1) = 2;
  73. spb(1, 0) = 3;
  74. spb(1, 1) = 4;
  75. spb(2, 0) = 5;
  76. spb(2, 1) = 6;
  77. spa *= spb;
  78. REQUIRE( spa.n_rows == 3 );
  79. REQUIRE( spa.n_cols == 2 );
  80. for (uword i = 0; i < 3; i++)
  81. {
  82. for (uword j = 0; j < 2; j++)
  83. {
  84. REQUIRE( (int) spa(i, j) == correctResult[i][j] );
  85. }
  86. }
  87. }
  88. TEST_CASE("sparse_sparse_matrix_multiplication_test")
  89. {
  90. SpMat<double> spaa(10, 10);
  91. spaa(1, 5) = 0.4;
  92. spaa(0, 4) = 0.3;
  93. spaa(0, 8) = 1.2;
  94. spaa(3, 0) = 1.1;
  95. spaa(3, 1) = 1.1;
  96. spaa(3, 2) = 1.1;
  97. spaa(4, 4) = 0.2;
  98. spaa(4, 9) = 0.1;
  99. spaa(6, 2) = 4.1;
  100. spaa(6, 8) = 4.1;
  101. spaa(7, 5) = 1.0;
  102. spaa(8, 9) = 0.4;
  103. spaa(9, 4) = 0.4;
  104. double correctResultB[10][10] =
  105. {{ 0.00, 0.00, 0.00, 0.00, 0.06, 0.00, 0.00, 0.00, 0.00, 0.51 },
  106. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
  107. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
  108. { 0.00, 0.00, 0.00, 0.00, 0.33, 0.44, 0.00, 0.00, 1.32, 0.00 },
  109. { 0.00, 0.00, 0.00, 0.00, 0.08, 0.00, 0.00, 0.00, 0.00, 0.02 },
  110. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
  111. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.64 },
  112. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
  113. { 0.00, 0.00, 0.00, 0.00, 0.16, 0.00, 0.00, 0.00, 0.00, 0.00 },
  114. { 0.00, 0.00, 0.00, 0.00, 0.08, 0.00, 0.00, 0.00, 0.00, 0.04 }};
  115. spaa *= spaa;
  116. for (uword i = 0; i < 10; i++)
  117. {
  118. for (uword j = 0; j < 10; j++)
  119. {
  120. REQUIRE( (double) spaa(i, j) == Approx(correctResultB[i][j]) );
  121. }
  122. }
  123. }
  124. TEST_CASE("hadamard_product_test")
  125. {
  126. SpMat<int> a(4, 4), b(4, 4);
  127. a(1, 1) = 1;
  128. a(2, 1) = 1;
  129. a(3, 3) = 1;
  130. a(3, 0) = 1;
  131. a(0, 2) = 1;
  132. b(1, 1) = 1;
  133. b(2, 2) = 1;
  134. b(3, 3) = 1;
  135. b(3, 0) = 1;
  136. b(0, 3) = 1;
  137. b(3, 1) = 1;
  138. double correctResult[4][4] =
  139. {{ 0, 0, 0, 0 },
  140. { 0, 1, 0, 0 },
  141. { 0, 0, 0, 0 },
  142. { 1, 0, 0, 1 }};
  143. a %= b;
  144. for (uword i = 0; i < 4; i++)
  145. {
  146. for (uword j = 0; j < 4; j++)
  147. {
  148. REQUIRE( a(i, j) == correctResult[i][j] );
  149. }
  150. }
  151. SpMat<double> c, d;
  152. c.sprandu(30, 25, 0.1);
  153. d.sprandu(30, 25, 0.1);
  154. mat e, f;
  155. e = c;
  156. f = d;
  157. c %= d;
  158. e %= f;
  159. for (uword i = 0; i < 25; ++i)
  160. {
  161. for(uword j = 0; j < 30; ++j)
  162. {
  163. REQUIRE( (double) c(j, i) == Approx(e(j, i)) );
  164. }
  165. }
  166. }
  167. TEST_CASE("division_test")
  168. {
  169. SpMat<double> a(2, 2), b(2, 2);
  170. a(0, 1) = 0.5;
  171. b(0, 1) = 1.0;
  172. b(1, 0) = 5.0;
  173. a /= b;
  174. REQUIRE( std::isnan((double) a(0, 0)) );
  175. REQUIRE( (double) a(0, 1) == Approx(0.5) );
  176. REQUIRE( (double) a(1, 0) == Approx(1e-5) );
  177. REQUIRE( std::isnan((double) a(1, 1)) );
  178. }
  179. TEST_CASE("insert_delete_test")
  180. {
  181. SpMat<double> sp;
  182. sp.set_size(10, 10);
  183. // Ensure everything is empty.
  184. for (uword i = 0; i < 100; i++)
  185. {
  186. REQUIRE( sp(i) == 0.0 );
  187. }
  188. // Add an element.
  189. sp(5, 5) = 43.234;
  190. REQUIRE( sp.n_nonzero == 1 );
  191. REQUIRE( (double) sp(5, 5) == Approx(43.234) );
  192. // Remove the element.
  193. sp(5, 5) = 0.0;
  194. REQUIRE( sp.n_nonzero == 0 );
  195. }
  196. TEST_CASE("value_operator_test")
  197. {
  198. // Test operators that work with a single value.
  199. // =(double), /=(double), *=(double)
  200. SpMat<double> sp(3, 4);
  201. double correctResult[3][4] = {{1.5, 0.0, 0.0, 0.0},
  202. {2.1, 3.2, 0.9, 0.0},
  203. {0.0, 9.3, 4.0, -1.5}};
  204. sp(0, 0) = 1.5;
  205. sp(1, 0) = 2.1;
  206. sp(1, 1) = 3.2;
  207. sp(1, 2) = 0.9;
  208. sp(2, 1) = 9.3;
  209. sp(2, 2) = 4.0;
  210. sp(2, 3) = -1.5;
  211. // operator=(double)
  212. SpMat<double> work = sp;
  213. work = 5.0;
  214. REQUIRE( work.n_nonzero == 1 );
  215. REQUIRE( work.n_elem == 1 );
  216. REQUIRE( (double) work(0) == Approx(5.0) );
  217. // operator*=(double)
  218. work = sp;
  219. work *= 2;
  220. REQUIRE( work.n_nonzero == 7 );
  221. for (uword i = 0; i < 3; i++)
  222. {
  223. for (uword j = 0; j < 4; j++)
  224. {
  225. REQUIRE((double) work(i, j) == Approx(correctResult[i][j] * 2.0) );
  226. }
  227. }
  228. // operator/=(double)
  229. work = sp;
  230. work /= 5.5;
  231. REQUIRE( work.n_nonzero == 7 );
  232. for (uword i = 0; i < 3; i++)
  233. {
  234. for (uword j = 0; j < 4; j++)
  235. {
  236. REQUIRE((double) work(i, j) == Approx(correctResult[i][j] / 5.5) );
  237. }
  238. }
  239. }
  240. TEST_CASE("iterator_test")
  241. {
  242. SpMat<double> x(5, 5);
  243. x(4, 1) = 3.1;
  244. x(1, 2) = 4.2;
  245. x(1, 3) = 3.3;
  246. x(1, 3) = 5.5; // overwrite
  247. x(2, 3) = 4.5;
  248. x(4, 4) = 6.4;
  249. SpMat<double>::iterator it = x.begin();
  250. REQUIRE( (double) *it == Approx(3.1) );
  251. REQUIRE( it.row() == 4 );
  252. REQUIRE( it.col() == 1 );
  253. ++it;
  254. REQUIRE( (double) *it == Approx(4.2) );
  255. REQUIRE( it.row() == 1 );
  256. REQUIRE( it.col() == 2 );
  257. ++it;
  258. REQUIRE( (double) *it == Approx(5.5) );
  259. REQUIRE( it.row() == 1 );
  260. REQUIRE( it.col() == 3 );
  261. ++it;
  262. REQUIRE( (double) *it == Approx(4.5) );
  263. REQUIRE( it.row() == 2 );
  264. REQUIRE( it.col() == 3 );
  265. ++it;
  266. REQUIRE( (double) *it == Approx(6.4) );
  267. REQUIRE( it.row() == 4 );
  268. REQUIRE( it.col() == 4 );
  269. ++it;
  270. REQUIRE( it == x.end() );
  271. // Now let's go backwards.
  272. --it; // Get it off the end.
  273. REQUIRE( (double) *it == Approx(6.4) );
  274. REQUIRE( it.row() == 4 );
  275. REQUIRE( it.col() == 4 );
  276. --it;
  277. REQUIRE( (double) *it == Approx(4.5) );
  278. REQUIRE( it.row() == 2 );
  279. REQUIRE( it.col() == 3 );
  280. --it;
  281. REQUIRE( (double) *it == Approx(5.5) );
  282. REQUIRE( it.row() == 1 );
  283. REQUIRE( it.col() == 3 );
  284. --it;
  285. REQUIRE( (double) *it == Approx(4.2) );
  286. REQUIRE( it.row() == 1 );
  287. REQUIRE( it.col() == 2 );
  288. --it;
  289. REQUIRE( (double) *it == Approx(3.1) );
  290. REQUIRE( it.row() == 4 );
  291. REQUIRE( it.col() == 1 );
  292. REQUIRE( it == x.begin() );
  293. // Try removing an element we iterated to.
  294. ++it;
  295. ++it;
  296. *it = 0;
  297. REQUIRE( x.n_nonzero == 4 );
  298. }
  299. TEST_CASE("row_iterator_test")
  300. {
  301. SpMat<double> x(5, 5);
  302. x(4, 1) = 3.1;
  303. x(1, 2) = 4.2;
  304. x(1, 3) = 3.3;
  305. x(1, 3) = 5.5; // overwrite
  306. x(2, 3) = 4.5;
  307. x(4, 4) = 6.4;
  308. SpMat<double>::row_iterator it = x.begin_row();
  309. REQUIRE( (double) *it == Approx(4.2) );
  310. REQUIRE( it.row() == 1 );
  311. REQUIRE( it.col() == 2 );
  312. ++it;
  313. REQUIRE( (double) *it == Approx(5.5) );
  314. REQUIRE( it.row() == 1 );
  315. REQUIRE( it.col() == 3 );
  316. ++it;
  317. REQUIRE( (double) *it == Approx(4.5) );
  318. REQUIRE( it.row() == 2 );
  319. REQUIRE( it.col() == 3 );
  320. ++it;
  321. REQUIRE( (double) *it == Approx(3.1) );
  322. REQUIRE( it.row() == 4 );
  323. REQUIRE( it.col() == 1 );
  324. ++it;
  325. REQUIRE( (double) *it == Approx(6.4) );
  326. REQUIRE( it.row() == 4 );
  327. REQUIRE( it.col() == 4 );
  328. ++it;
  329. // REQUIRE( it == x.end_row() );
  330. // Now let's go backwards.
  331. --it; // Get it off the end.
  332. REQUIRE( (double) *it == Approx(6.4) );
  333. REQUIRE( it.row() == 4 );
  334. REQUIRE( it.col() == 4 );
  335. --it;
  336. REQUIRE( (double) *it == Approx(3.1) );
  337. REQUIRE( it.row() == 4 );
  338. REQUIRE( it.col() == 1 );
  339. --it;
  340. REQUIRE( (double) *it == Approx(4.5) );
  341. REQUIRE( it.row() == 2 );
  342. REQUIRE( it.col() == 3 );
  343. --it;
  344. REQUIRE( (double) *it == Approx(5.5) );
  345. REQUIRE( it.row() == 1 );
  346. REQUIRE( it.col() == 3 );
  347. --it;
  348. REQUIRE( (double) *it == Approx(4.2) );
  349. REQUIRE( it.row() == 1 );
  350. REQUIRE( it.col() == 2 );
  351. REQUIRE( it == x.begin_row() );
  352. // Try removing an element we iterated to.
  353. ++it;
  354. ++it;
  355. *it = 0;
  356. REQUIRE( x.n_nonzero == 4 );
  357. }
  358. TEST_CASE("basic_sp_mat_operator_test")
  359. {
  360. // +=, -=, *=, /=, %=
  361. SpMat<double> a(6, 5);
  362. a(0, 0) = 3.4;
  363. a(4, 1) = 4.1;
  364. a(5, 1) = 1.5;
  365. a(3, 2) = 2.6;
  366. a(4, 2) = 3.0;
  367. a(1, 3) = 9.8;
  368. a(4, 3) = 0.1;
  369. a(2, 4) = 0.2;
  370. a(3, 4) = 0.2;
  371. a(4, 4) = 0.2;
  372. a(5, 4) = 8.3;
  373. SpMat<double> b(6, 5);
  374. b(0, 0) = 3.4;
  375. b(3, 0) = 0.4;
  376. b(3, 1) = 0.5;
  377. b(4, 1) = 1.2;
  378. b(4, 2) = 3.0;
  379. b(5, 2) = 1.1;
  380. b(1, 3) = 0.6;
  381. b(3, 3) = 1.0;
  382. b(4, 4) = 7.3;
  383. b(5, 4) = 7.4;
  384. double addResult[6][5] = {{6.8 , 0 , 0 , 0 , 0 },
  385. {0 , 0 , 0 , 10.4, 0 },
  386. {0 , 0 , 0 , 0 , 0.2 },
  387. {0.4 , 0.5 , 2.6 , 1.0 , 0.2 },
  388. {0 , 5.3 , 6.0 , 0.1 , 7.5 },
  389. {0 , 1.5 , 1.1 , 0 , 15.7}};
  390. double subResult[6][5] = {{0 , 0 , 0 , 0 , 0 },
  391. {0 , 0 , 0 , 9.2 , 0 },
  392. {0 , 0 , 0 , 0 , 0.2 },
  393. {-0.4, -0.5, 2.6 , -1.0, 0.2 },
  394. {0 , 2.9 , 0 , 0.1 , -7.1},
  395. {0 , 1.5 , -1.1, 0 , 0.9 }};
  396. SpMat<double> out = a;
  397. out += b;
  398. REQUIRE( out.n_nonzero == 15 );
  399. for (uword r = 0; r < 6; r++)
  400. {
  401. for (uword c = 0; c < 5; c++)
  402. {
  403. REQUIRE( (double) out(r, c) == Approx(addResult[r][c]) );
  404. }
  405. }
  406. out = a;
  407. out -= b;
  408. REQUIRE( out.n_nonzero == 13 );
  409. for (uword r = 0; r < 6; r++)
  410. {
  411. for (uword c = 0; c < 5; c++)
  412. {
  413. REQUIRE( (double) out(r, c) == Approx(subResult[r][c]) );
  414. }
  415. }
  416. }
  417. TEST_CASE("min_max_test")
  418. {
  419. SpMat<double> a(6, 5);
  420. a(0, 0) = 3.4;
  421. a(4, 1) = 4.1;
  422. a(5, 1) = 1.5;
  423. a(3, 2) = 2.6;
  424. a(4, 2) = 3.0;
  425. a(1, 3) = 9.8;
  426. a(4, 3) = 0.1;
  427. a(2, 4) = 0.2;
  428. a(3, 4) = -0.2;
  429. a(4, 4) = 0.2;
  430. a(5, 4) = 8.3;
  431. uword index, row, col;
  432. REQUIRE( a.min() == Approx(-0.2) );
  433. REQUIRE( a.min(index) == Approx(-0.2) );
  434. REQUIRE( index == 27 );
  435. REQUIRE( a.min(row, col) == Approx(-0.2) );
  436. REQUIRE( row == 3 );
  437. REQUIRE( col == 4 );
  438. REQUIRE( a.max() == Approx(9.8) );
  439. REQUIRE( a.max(index) == Approx(9.8) );
  440. REQUIRE( index == 19 );
  441. REQUIRE( a.max(row, col) == Approx(9.8) );
  442. REQUIRE( row == 1 );
  443. REQUIRE( col == 3 );
  444. }
  445. TEST_CASE("swap_row_test")
  446. {
  447. SpMat<double> a(6, 5);
  448. a(0, 0) = 3.4;
  449. a(4, 1) = 4.1;
  450. a(5, 1) = 1.5;
  451. a(3, 2) = 2.6;
  452. a(4, 2) = 3.0;
  453. a(1, 3) = 9.8;
  454. a(4, 3) = 0.1;
  455. a(2, 4) = 0.2;
  456. a(3, 4) = -0.2;
  457. a(4, 4) = 0.2;
  458. a(5, 4) = 8.3;
  459. /**
  460. * [[3.4 0.0 0.0 0.0 0.0]
  461. * [0.0 0.0 0.0 9.8 0.0]
  462. * [0.0 0.0 0.0 0.0 0.2]
  463. * [0.0 0.0 2.6 0.0 -0.2]
  464. * [0.0 4.1 3.0 0.1 0.2]
  465. * [0.0 1.5 0.0 0.0 8.3]]
  466. */
  467. double swapOne[6][5] =
  468. {{ 0.0, 0.0, 2.6, 0.0, -0.2},
  469. { 0.0, 0.0, 0.0, 9.8, 0.0},
  470. { 0.0, 0.0, 0.0, 0.0, 0.2},
  471. { 3.4, 0.0, 0.0, 0.0, 0.0},
  472. { 0.0, 4.1, 3.0, 0.1, 0.2},
  473. { 0.0, 1.5, 0.0, 0.0, 8.3}};
  474. double swapTwo[6][5] =
  475. {{ 0.0, 0.0, 2.6, 0.0, -0.2},
  476. { 0.0, 0.0, 0.0, 9.8, 0.0},
  477. { 0.0, 0.0, 0.0, 0.0, 0.2},
  478. { 3.4, 0.0, 0.0, 0.0, 0.0},
  479. { 0.0, 1.5, 0.0, 0.0, 8.3},
  480. { 0.0, 4.1, 3.0, 0.1, 0.2}};
  481. a.swap_rows(0, 3);
  482. for (uword row = 0; row < a.n_rows; row++)
  483. {
  484. for (uword col = 0; col < a.n_cols; col++)
  485. {
  486. REQUIRE( (double) a(row, col) == Approx(swapOne[row][col]) );
  487. }
  488. }
  489. a.swap_rows(4, 5);
  490. for (uword row = 0; row < a.n_rows; row++)
  491. {
  492. for (uword col = 0; col < a.n_cols; col++)
  493. {
  494. REQUIRE( (double) a(row, col) == Approx(swapTwo[row][col]) );
  495. }
  496. }
  497. }
  498. TEST_CASE("swap_col_test")
  499. {
  500. SpMat<double> a(6, 5);
  501. a(0, 0) = 3.4;
  502. a(4, 1) = 4.1;
  503. a(5, 1) = 1.5;
  504. a(3, 2) = 2.6;
  505. a(4, 2) = 3.0;
  506. a(1, 3) = 9.8;
  507. a(4, 3) = 0.1;
  508. a(2, 4) = 0.2;
  509. a(3, 4) = -0.2;
  510. a(4, 4) = 0.2;
  511. a(5, 4) = 8.3;
  512. mat b(6, 5);
  513. b.zeros(6, 5);
  514. b(0, 0) = 3.4;
  515. b(4, 1) = 4.1;
  516. b(5, 1) = 1.5;
  517. b(3, 2) = 2.6;
  518. b(4, 2) = 3.0;
  519. b(1, 3) = 9.8;
  520. b(4, 3) = 0.1;
  521. b(2, 4) = 0.2;
  522. b(3, 4) = -0.2;
  523. b(4, 4) = 0.2;
  524. b(5, 4) = 8.3;
  525. /**
  526. * [[3.4 0.0 0.0 0.0 0.0]
  527. * [0.0 0.0 0.0 9.8 0.0]
  528. * [0.0 0.0 0.0 0.0 0.2]
  529. * [0.0 0.0 2.6 0.0 -0.2]
  530. * [0.0 4.1 3.0 0.1 0.2]
  531. * [0.0 1.5 0.0 0.0 8.3]]
  532. */
  533. a.swap_cols(2, 3);
  534. b.swap_cols(2, 3);
  535. for (uword row = 0; row < a.n_rows; row++)
  536. {
  537. for (uword col = 0; col < a.n_cols; col++)
  538. {
  539. REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
  540. }
  541. }
  542. a.swap_cols(0, 4);
  543. b.swap_cols(0, 4);
  544. for (uword row = 0; row < a.n_rows; row++)
  545. {
  546. for (uword col = 0; col < a.n_cols; col++)
  547. {
  548. REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
  549. }
  550. }
  551. a.swap_cols(1, 4);
  552. b.swap_cols(1, 4);
  553. for (uword row = 0; row < a.n_rows; row++)
  554. {
  555. for (uword col = 0; col < a.n_cols; col++)
  556. {
  557. REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
  558. }
  559. }
  560. }
  561. TEST_CASE("shed_col_test")
  562. {
  563. SpMat<int> a(2, 2);
  564. a(0, 0) = 1;
  565. a(1, 1) = 1;
  566. /**
  567. * [[1 0]
  568. * [0 1]]
  569. *
  570. * becomes
  571. *
  572. * [[0]
  573. * [1]]
  574. */
  575. a.shed_col(0);
  576. REQUIRE( a.n_cols == 1 );
  577. REQUIRE( a.n_rows == 2 );
  578. REQUIRE( a.n_elem == 2 );
  579. REQUIRE( a.n_nonzero == 1 );
  580. REQUIRE( a(0, 0) == 0 );
  581. REQUIRE( a(1, 0) == 1 );
  582. }
  583. TEST_CASE("shed_cols_test")
  584. {
  585. SpMat<int> a(3, 3);
  586. a(0, 0) = 1;
  587. a(1, 1) = 1;
  588. a(2, 2) = 1;
  589. SpMat<int> b(3, 3);
  590. b(0, 0) = 1;
  591. b(1, 1) = 1;
  592. b(2, 2) = 1;
  593. SpMat<int> c(3, 3);
  594. c(0, 0) = 1;
  595. c(1, 1) = 1;
  596. c(2, 2) = 1;
  597. /**
  598. * [[1 0 0]
  599. * [0 1 0]
  600. * [0 0 1]]
  601. *
  602. * becomes
  603. *
  604. * [[0]
  605. * [0]
  606. * [1]]
  607. */
  608. a.shed_cols(0, 1);
  609. REQUIRE( a.n_cols == 1 );
  610. REQUIRE( a.n_rows == 3 );
  611. REQUIRE( a.n_elem == 3 );
  612. REQUIRE( a.n_nonzero == 1 );
  613. REQUIRE( a(0, 0) == 0 );
  614. REQUIRE( a(1, 0) == 0 );
  615. REQUIRE( a(2, 0) == 1 );
  616. b.shed_cols(1, 2);
  617. REQUIRE( b.n_cols == 1 );
  618. REQUIRE( b.n_rows == 3 );
  619. REQUIRE( b.n_elem == 3 );
  620. REQUIRE( b.n_nonzero == 1 );
  621. REQUIRE( b(0, 0) == 1 );
  622. REQUIRE( b(1, 0) == 0 );
  623. REQUIRE( b(2, 0) == 0 );
  624. c.shed_cols(0, 0);
  625. c.shed_cols(1, 1);
  626. REQUIRE( c.n_cols == 1 );
  627. REQUIRE( c.n_rows == 3 );
  628. REQUIRE( c.n_elem == 3 );
  629. REQUIRE( c.n_nonzero == 1 );
  630. REQUIRE( c(0, 0) == 0 );
  631. REQUIRE( c(1, 0) == 1 );
  632. REQUIRE( c(2, 0) == 0 );
  633. }
  634. TEST_CASE("shed_row_test")
  635. {
  636. SpMat<int> a(3, 3);
  637. a(0, 0) = 1;
  638. a(1, 1) = 1;
  639. a(2, 2) = 1;
  640. Mat<int> b(3, 3);
  641. b.zeros(3, 3);
  642. b(0, 0) = 1;
  643. b(1, 1) = 1;
  644. b(2, 2) = 1;
  645. /**
  646. * [[1 0 0]
  647. * [0 1 0]
  648. * [0 0 1]]
  649. *
  650. * becomes
  651. *
  652. * [[1 0 0]
  653. * [0 1 0]]
  654. */
  655. a.shed_row(2);
  656. b.shed_row(2);
  657. REQUIRE( a.n_cols == 3 );
  658. REQUIRE( a.n_rows == 2 );
  659. REQUIRE( a.n_elem == 6 );
  660. REQUIRE( a.n_nonzero == 2 );
  661. for (uword row = 0; row < a.n_rows; row++)
  662. {
  663. for (uword col = 0; col < a.n_cols; col++)
  664. {
  665. REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
  666. }
  667. }
  668. }
  669. TEST_CASE("shed_rows_test")
  670. {
  671. SpMat<int> a(5, 5);
  672. a(0, 0) = 1;
  673. a(1, 1) = 1;
  674. a(2, 2) = 1;
  675. a(3, 3) = 1;
  676. a(4, 4) = 1;
  677. Mat<int> b(5, 5);
  678. b.zeros(5, 5);
  679. b(0, 0) = 1;
  680. b(1, 1) = 1;
  681. b(2, 2) = 1;
  682. b(3, 3) = 1;
  683. b(4, 4) = 1;
  684. SpMat<int> c = a;
  685. Mat<int> d = b;
  686. /**
  687. * [[1 0 0 0 0]
  688. * [0 1 0 0 0]
  689. * [0 0 1 0 0]
  690. * [0 0 0 1 0]
  691. * [0 0 0 0 1]]
  692. *
  693. * becomes
  694. *
  695. * [[1 0 0 0 0]
  696. * [0 1 0 0 0]]
  697. */
  698. a.shed_rows(2,4);
  699. b.shed_rows(2,4);
  700. REQUIRE( a.n_cols == 5 );
  701. REQUIRE( a.n_rows == 2 );
  702. REQUIRE( a.n_elem == 10 );
  703. REQUIRE( a.n_nonzero == 2 );
  704. for (uword row = 0; row < a.n_rows; row++)
  705. {
  706. for (uword col = 0; col < a.n_cols; col++)
  707. {
  708. REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
  709. }
  710. }
  711. c.shed_rows(0, 2);
  712. d.shed_rows(0, 2);
  713. REQUIRE( c.n_cols == 5 );
  714. REQUIRE( c.n_rows == 2 );
  715. REQUIRE( c.n_elem == 10 );
  716. REQUIRE( c.n_nonzero == 2 );
  717. for (uword row = 0; row < c.n_rows; ++row)
  718. {
  719. for (uword col = 0; col < c.n_cols; ++col)
  720. {
  721. REQUIRE( (int) c(row, col) == d(row, col) );
  722. }
  723. }
  724. }
  725. TEST_CASE("sp_mat_reshape_columnwise_test")
  726. {
  727. // Input matrix:
  728. // [[0 2 0]
  729. // [1 3 0]
  730. // [0 0 5]
  731. // [0 4 6]]
  732. //
  733. // Output matrix:
  734. // [[0 0 0 0]
  735. // [1 2 4 5]
  736. // [0 3 0 6]]
  737. SpMat<unsigned int> ref(4, 3);
  738. ref(1, 0) = 1;
  739. ref(0, 1) = 2;
  740. ref(1, 1) = 3;
  741. ref(3, 1) = 4;
  742. ref(2, 2) = 5;
  743. ref(3, 2) = 6;
  744. // Now reshape.
  745. ref.reshape(3, 4);
  746. // Check everything.
  747. REQUIRE( ref.n_cols == 4 );
  748. REQUIRE( ref.n_rows == 3 );
  749. REQUIRE( (unsigned int) ref(0, 0) == 0 );
  750. REQUIRE( (unsigned int) ref(1, 0) == 1 );
  751. REQUIRE( (unsigned int) ref(2, 0) == 0 );
  752. REQUIRE( (unsigned int) ref(0, 1) == 0 );
  753. REQUIRE( (unsigned int) ref(1, 1) == 2 );
  754. REQUIRE( (unsigned int) ref(2, 1) == 3 );
  755. REQUIRE( (unsigned int) ref(0, 2) == 0 );
  756. REQUIRE( (unsigned int) ref(1, 2) == 4 );
  757. REQUIRE( (unsigned int) ref(2, 2) == 0 );
  758. REQUIRE( (unsigned int) ref(0, 3) == 0 );
  759. REQUIRE( (unsigned int) ref(1, 3) == 5 );
  760. REQUIRE( (unsigned int) ref(2, 3) == 6 );
  761. }
  762. // TEST_CASE("sp_mat_reshape_rowwise_test")
  763. // {
  764. // // Input matrix:
  765. // // [[0 2 0]
  766. // // [1 3 0]
  767. // // [0 0 5]
  768. // // [0 4 6]]
  769. // //
  770. // // Output matrix:
  771. // // [[0 2 0 1]
  772. // // [3 0 0 0]
  773. // // [5 0 4 6]]
  774. // SpMat<unsigned int> ref(4, 3);
  775. // ref(1, 0) = 1;
  776. // ref(0, 1) = 2;
  777. // ref(1, 1) = 3;
  778. // ref(3, 1) = 4;
  779. // ref(2, 2) = 5;
  780. // ref(3, 2) = 6;
  781. //
  782. // // Now reshape.
  783. // ref.reshape(3, 4, 1 /* row-wise */);
  784. //
  785. // // Check everything.
  786. // REQUIRE( ref.n_cols == 4 );
  787. // REQUIRE( ref.n_rows == 3 );
  788. //
  789. // REQUIRE( (unsigned int) ref(0, 0) == 0 );
  790. // REQUIRE( (unsigned int) ref(1, 0) == 3 );
  791. // REQUIRE( (unsigned int) ref(2, 0) == 5 );
  792. // REQUIRE( (unsigned int) ref(0, 1) == 2 );
  793. // REQUIRE( (unsigned int) ref(1, 1) == 0 );
  794. // REQUIRE( (unsigned int) ref(2, 1) == 0 );
  795. // REQUIRE( (unsigned int) ref(0, 2) == 0 );
  796. // REQUIRE( (unsigned int) ref(1, 2) == 0 );
  797. // REQUIRE( (unsigned int) ref(2, 2) == 4 );
  798. // REQUIRE( (unsigned int) ref(0, 3) == 1 );
  799. // REQUIRE( (unsigned int) ref(1, 3) == 0 );
  800. // REQUIRE( (unsigned int) ref(2, 3) == 6 );
  801. // }
  802. TEST_CASE("sp_mat_zeros_tests")
  803. {
  804. SpMat<double> m(4, 3);
  805. m(1, 0) = 1;
  806. m(0, 1) = 2;
  807. m(1, 1) = 3;
  808. m(3, 1) = 4;
  809. m(2, 2) = 5;
  810. m(3, 2) = 6;
  811. // Now zero it out.
  812. SpMat<double> d = m;
  813. d.zeros();
  814. REQUIRE( d.values[0] == 0 );
  815. REQUIRE( d.row_indices[0] == 0);
  816. REQUIRE( d.col_ptrs[0] == 0 );
  817. REQUIRE( d.col_ptrs[1] == 0 );
  818. REQUIRE( d.col_ptrs[2] == 0 );
  819. REQUIRE( d.col_ptrs[3] == 0 );
  820. REQUIRE( d.n_cols == 3 );
  821. REQUIRE( d.n_rows == 4 );
  822. REQUIRE( d.n_elem == 12 );
  823. REQUIRE( d.n_nonzero == 0 );
  824. // Now zero it out again.
  825. d = m;
  826. d.zeros(10);
  827. REQUIRE( d.values[0] == 0 );
  828. REQUIRE( d.row_indices[0] == 0);
  829. REQUIRE( d.col_ptrs[0] == 0 );
  830. REQUIRE( d.col_ptrs[1] == 0 );
  831. REQUIRE( d.n_cols == 1 );
  832. REQUIRE( d.n_rows == 10 );
  833. REQUIRE( d.n_elem == 10 );
  834. REQUIRE( d.n_nonzero == 0 );
  835. // Now zero it out again.
  836. d = m;
  837. d.zeros(5, 5);
  838. REQUIRE( d.values[0] == 0 );
  839. REQUIRE( d.row_indices[0] == 0);
  840. REQUIRE( d.col_ptrs[0] == 0 );
  841. REQUIRE( d.col_ptrs[1] == 0 );
  842. REQUIRE( d.col_ptrs[2] == 0 );
  843. REQUIRE( d.col_ptrs[3] == 0 );
  844. REQUIRE( d.col_ptrs[4] == 0 );
  845. REQUIRE( d.col_ptrs[5] == 0 );
  846. REQUIRE( d.n_cols == 5 );
  847. REQUIRE( d.n_rows == 5 );
  848. REQUIRE( d.n_elem == 25 );
  849. REQUIRE( d.n_nonzero == 0 );
  850. }
  851. /**
  852. * Check that eye() works.
  853. */
  854. TEST_CASE("sp_mat_eye_test")
  855. {
  856. SpMat<double> e = eye<SpMat<double> >(5, 5);
  857. REQUIRE( e.n_elem == 25 );
  858. REQUIRE( e.n_rows == 5 );
  859. REQUIRE( e.n_cols == 5 );
  860. REQUIRE( e.n_nonzero == 5 );
  861. for (uword i = 0; i < 5; i++)
  862. {
  863. for (uword j = 0; j < 5; j++)
  864. {
  865. if (i == j)
  866. REQUIRE( (double) e(i, j) == Approx(1.0) );
  867. else
  868. REQUIRE( (double) e(i, j) == Approx(1e-5) );
  869. }
  870. }
  871. // Just check that these compile and run.
  872. e = eye<SpMat<double> >(5, 5);
  873. e *= eye<SpMat<double> >(5, 5);
  874. e %= eye<SpMat<double> >(5, 5);
  875. e /= eye<SpMat<double> >(5, 5);
  876. }
  877. /**
  878. * Check that pow works.
  879. *
  880. TEST_CASE("sp_mat_pow_test")
  881. {
  882. SpMat<double> a(3, 3);
  883. a(0, 2) = 4.3;
  884. a(1, 1) = -5.5;
  885. a(2, 2) = -6.3;
  886. a += pow(a, 2);
  887. REQUIRE( (double) a(0, 0) == 0 );
  888. REQUIRE( (double) a(1, 0) == 0 );
  889. REQUIRE( (double) a(2, 0) == 0 );
  890. REQUIRE( (double) a(0, 1) == 0 );
  891. REQUIRE( (double) a(1, 1) == Approx(24.75) );
  892. REQUIRE( (double) a(2, 1) == 0 );
  893. REQUIRE( (double) a(0, 2) == Approx(22.79) );
  894. REQUIRE( (double) a(1, 2) == 0 );
  895. REQUIRE( (double) a(2, 2) == Approx(33.39) );
  896. a = pow(a, 2);
  897. a *= pow(a, 2);
  898. a %= pow(a, 2);
  899. a /= pow(a, 2);
  900. }
  901. */
  902. // I hate myself.
  903. #undef TEST_OPERATOR
  904. #define TEST_OPERATOR(EOP_TEST, EOP) \
  905. TEST_CASE(EOP_TEST) \
  906. {\
  907. SpMat<double> a(3, 3);\
  908. a(0, 2) = 4.3;\
  909. a(1, 1) = -5.5;\
  910. a(2, 2) = -6.3;\
  911. a(1, 0) = 0.001;\
  912. Mat<double> b(3, 3);\
  913. b.zeros();\
  914. b(0, 2) = 4.3;\
  915. b(1, 1) = -5.5;\
  916. b(2, 2) = -6.3;\
  917. b(1, 0) = 0.001;\
  918. \
  919. SpMat<double> c = EOP(a);\
  920. Mat<double> d = EOP(b);\
  921. \
  922. if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
  923. REQUIRE( c(0, 0) == d(0, 0) );\
  924. if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
  925. REQUIRE( c(1, 0) == d(1, 0) );\
  926. if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
  927. REQUIRE( c(2, 0) == d(2, 0) );\
  928. if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
  929. REQUIRE( c(0, 1) == d(0, 1) );\
  930. if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
  931. REQUIRE( c(1, 1) == d(1, 1) );\
  932. if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
  933. REQUIRE( c(2, 1) == d(2, 1) );\
  934. if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
  935. REQUIRE( c(0, 2) == d(0, 2) );\
  936. if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
  937. REQUIRE( c(1, 2) == d(1, 2) );\
  938. if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
  939. REQUIRE( c(2, 2) == d(2, 2) );\
  940. \
  941. c -= EOP(a);\
  942. d -= EOP(b);\
  943. \
  944. if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
  945. REQUIRE( c(0, 0) == d(0, 0) );\
  946. if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
  947. REQUIRE( c(1, 0) == d(1, 0) );\
  948. if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
  949. REQUIRE( c(2, 0) == d(2, 0) );\
  950. if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
  951. REQUIRE( c(0, 1) == d(0, 1) );\
  952. if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
  953. REQUIRE( c(1, 1) == d(1, 1) );\
  954. if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
  955. REQUIRE( c(2, 1) == d(2, 1) );\
  956. if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
  957. REQUIRE( c(0, 2) == d(0, 2) );\
  958. if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
  959. REQUIRE( c(1, 2) == d(1, 2) );\
  960. if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
  961. REQUIRE( c(2, 2) == d(2, 2) );\
  962. \
  963. c %= EOP(a);\
  964. d %= EOP(b);\
  965. \
  966. if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
  967. REQUIRE( c(0, 0) == d(0, 0) );\
  968. if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
  969. REQUIRE( c(1, 0) == d(1, 0) );\
  970. if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
  971. REQUIRE( c(2, 0) == d(2, 0) );\
  972. if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
  973. REQUIRE( c(0, 1) == d(0, 1) );\
  974. if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
  975. REQUIRE( c(1, 1) == d(1, 1) );\
  976. if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
  977. REQUIRE( c(2, 1) == d(2, 1) );\
  978. if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
  979. REQUIRE( c(0, 2) == d(0, 2) );\
  980. if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
  981. REQUIRE( c(1, 2) == d(1, 2) );\
  982. if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
  983. REQUIRE( c(2, 2) == d(2, 2) );\
  984. \
  985. c *= EOP(a);\
  986. d *= EOP(b);\
  987. \
  988. if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
  989. REQUIRE( c(0, 0) == d(0, 0) );\
  990. if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
  991. REQUIRE( c(1, 0) == d(1, 0) );\
  992. if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
  993. REQUIRE( c(2, 0) == d(2, 0) );\
  994. if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
  995. REQUIRE( c(0, 1) == d(0, 1) );\
  996. if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
  997. REQUIRE( c(1, 1) == d(1, 1) );\
  998. if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
  999. REQUIRE( c(2, 1) == d(2, 1) );\
  1000. if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
  1001. REQUIRE( c(0, 2) == d(0, 2) );\
  1002. if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
  1003. REQUIRE( c(1, 2) == d(1, 2) );\
  1004. if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
  1005. REQUIRE( c(2, 2) == d(2, 2) );\
  1006. \
  1007. c /= EOP(a);\
  1008. d /= EOP(b);\
  1009. \
  1010. if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
  1011. REQUIRE( c(0, 0) == d(0, 0) );\
  1012. if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
  1013. REQUIRE( c(1, 0) == d(1, 0) );\
  1014. if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
  1015. REQUIRE( c(2, 0) == d(2, 0) );\
  1016. if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
  1017. REQUIRE( c(0, 1) == d(0, 1) );\
  1018. if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
  1019. REQUIRE( c(1, 1) == d(1, 1) );\
  1020. if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
  1021. REQUIRE( c(2, 1) == d(2, 1) );\
  1022. if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
  1023. REQUIRE( c(0, 2) == d(0, 2) );\
  1024. if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
  1025. REQUIRE( c(1, 2) == d(1, 2) );\
  1026. if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
  1027. REQUIRE( c(2, 2) == d(2, 2) );\
  1028. }
  1029. // Now run all the operators...
  1030. TEST_OPERATOR("sp_mat_abs_test", abs)
  1031. //TEST_OPERATOR("sp_mat_eps_test", eps);
  1032. //TEST_OPERATOR(expTest, exp);
  1033. //TEST_OPERATOR(exp2Test, exp2);
  1034. //TEST_OPERATOR(exp10Test, exp10);
  1035. //TEST_OPERATOR(trunc_expTest, trunc_exp);
  1036. //TEST_OPERATOR(logTest, log);
  1037. //TEST_OPERATOR(log2Test, log2);
  1038. //TEST_OPERATOR(log10Test, log10);
  1039. //TEST_OPERATOR(trunc_logTest, trunc_log);
  1040. TEST_OPERATOR("sp_mat_sqrt_test", sqrt)
  1041. TEST_OPERATOR("sp_mat_square_test", square)
  1042. TEST_OPERATOR("sp_mat_floor_test", floor)
  1043. TEST_OPERATOR("sp_mat_ceil_test", ceil)
  1044. //TEST_OPERATOR(cosTest, cos);
  1045. //TEST_OPERATOR(acosTest, acos);
  1046. //TEST_OPERATOR(coshTest, cosh);
  1047. //TEST_OPERATOR(acoshTest, acosh);
  1048. //TEST_OPERATOR(sinTest, sin);
  1049. //TEST_OPERATOR(asinTest, asin);
  1050. //TEST_OPERATOR(sinhTest, sinh);
  1051. //TEST_OPERATOR(asinhTest, asinh);
  1052. //TEST_OPERATOR(tanTest, tan);
  1053. //TEST_OPERATOR(tanhTest, tanh);
  1054. //TEST_OPERATOR(atanTest, atan);
  1055. //TEST_OPERATOR(atanhTest, atanh);
  1056. /*
  1057. TEST_CASE("spmat_diskio_tests")
  1058. {
  1059. std::string file_names[] = {"raw_ascii.txt",
  1060. "raw_binary.bin",
  1061. "arma_ascii.csv",
  1062. "csv_ascii.csv",
  1063. "arma_binary.bin",
  1064. "pgm_binary.bin",
  1065. "coord_ascii.txt"};
  1066. diskio dio;
  1067. SpMat<int> m(4, 3);
  1068. m(0, 0) = 1;
  1069. m(3, 0) = 2;
  1070. m(0, 2) = 3;
  1071. m(3, 2) = 4;
  1072. m(2, 1) = 5;
  1073. m(1, 2) = 6;
  1074. // Save the matrix.
  1075. REQUIRE( dio.save_raw_ascii(m, file_names[0]) );
  1076. // REQUIRE( dio.save_raw_binary(m, file_names[1]) );
  1077. // REQUIRE( dio.save_arma_ascii(m, file_names[2]) );
  1078. // REQUIRE( dio.save_csv_ascii(m, file_names[3]) );
  1079. REQUIRE( dio.save_arma_binary(m, file_names[4]) );
  1080. // REQUIRE( dio.save_pgm_binary(m, file_names[5]) );
  1081. REQUIRE( dio.save_coord_ascii(m, file_names[6]) );
  1082. // Load the files.
  1083. SpMat<int> lm[7];
  1084. std::string err;
  1085. REQUIRE( dio.load_raw_ascii(lm[0], file_names[0], err) );
  1086. // REQUIRE( dio.load_raw_binary(lm[1], file_names[1], err) );
  1087. // REQUIRE( dio.load_arma_ascii(lm[2], file_names[2], err) );
  1088. // REQUIRE( dio.load_csv_ascii(lm[3], file_names[3], err) );
  1089. REQUIRE( dio.load_arma_binary(lm[4], file_names[4], err) );
  1090. // REQUIRE( dio.load_pgm_binary(lm[5], file_names[5], err) );
  1091. REQUIRE( dio.load_coord_ascii(lm[6], file_names[6], err) );
  1092. // Now make sure all the matrices are identical.
  1093. for (uword i = 0; i < 7; i++)
  1094. {
  1095. for (uword r = 0; r < 4; r++)
  1096. {
  1097. for (uword c = 0; c < 3; c++)
  1098. {
  1099. REQUIRE( m(r, c) == lm[i](r, c) );
  1100. }
  1101. }
  1102. }
  1103. for (uword i = 0; i < 7; ++i)
  1104. {
  1105. remove(file_names[i].c_str());
  1106. }
  1107. }
  1108. */
  1109. TEST_CASE("min_test")
  1110. {
  1111. SpCol<double> a(5);
  1112. a(0) = 3.0;
  1113. a(2) = 1.0;
  1114. double res = min(a);
  1115. REQUIRE( res == Approx(1e-5) );
  1116. a(0) = -3.0;
  1117. a(2) = -1.0;
  1118. res = min(a);
  1119. REQUIRE( res == Approx(-3.0) );
  1120. a(0) = 1.3;
  1121. a(1) = 2.4;
  1122. a(2) = 3.1;
  1123. a(3) = 4.4;
  1124. a(4) = 1.4;
  1125. res = min(a);
  1126. REQUIRE( res == Approx(1.3) );
  1127. SpRow<double> b(5);
  1128. b(0) = 3.0;
  1129. b(2) = 1.0;
  1130. res = min(b);
  1131. REQUIRE( res == Approx(1e-5) );
  1132. b(0) = -3.0;
  1133. b(2) = -1.0;
  1134. res = min(b);
  1135. REQUIRE( res == Approx(-3.0) );
  1136. b(0) = 1.3;
  1137. b(1) = 2.4;
  1138. b(2) = 3.1;
  1139. b(3) = 4.4;
  1140. b(4) = 1.4;
  1141. res = min(b);
  1142. REQUIRE( res == Approx(1.3) );
  1143. SpMat<double> c(6, 5);
  1144. c(0, 0) = 1.0;
  1145. c(1, 0) = 3.0;
  1146. c(2, 0) = 4.0;
  1147. c(3, 0) = 0.6;
  1148. c(4, 0) = 1.4;
  1149. c(5, 0) = 1.2;
  1150. c(3, 2) = 1.3;
  1151. c(2, 3) = -4.0;
  1152. c(4, 3) = -1.4;
  1153. c(5, 2) = -3.4;
  1154. c(5, 3) = -4.1;
  1155. SpMat<double> r = min(c, 0);
  1156. REQUIRE( r.n_rows == 1 );
  1157. REQUIRE( r.n_cols == 5 );
  1158. REQUIRE( (double) r(0, 0) == Approx(0.6) );
  1159. REQUIRE( (double) r(0, 1) == Approx(1e-5) );
  1160. REQUIRE( (double) r(0, 2) == Approx(-3.4) );
  1161. REQUIRE( (double) r(0, 3) == Approx(-4.1) );
  1162. REQUIRE( (double) r(0, 4) == Approx(1e-5) );
  1163. r = min(c, 1);
  1164. REQUIRE( r.n_rows == 6 );
  1165. REQUIRE( r.n_cols == 1 );
  1166. REQUIRE( (double) r(0, 0) == Approx(1e-5) );
  1167. REQUIRE( (double) r(1, 0) == Approx(1e-5) );
  1168. REQUIRE( (double) r(2, 0) == Approx(-4.0) );
  1169. REQUIRE( (double) r(3, 0) == Approx(1e-5) );
  1170. REQUIRE( (double) r(4, 0) == Approx(-1.4) );
  1171. REQUIRE( (double) r(5, 0) == Approx(-4.1) );
  1172. }
  1173. TEST_CASE("max_test")
  1174. {
  1175. SpCol<double> a(5);
  1176. a(0) = -3.0;
  1177. a(2) = -1.0;
  1178. double resa = max(a);
  1179. REQUIRE( resa == Approx(1e-5) );
  1180. a(0) = 3.0;
  1181. a(2) = 1.0;
  1182. resa = max(a);
  1183. REQUIRE( resa == Approx(3.0) );
  1184. a(0) = -1.3;
  1185. a(1) = -2.4;
  1186. a(2) = -3.1;
  1187. a(3) = -4.4;
  1188. a(4) = -1.4;
  1189. resa = max(a);
  1190. REQUIRE( resa == Approx(-1.3) );
  1191. SpRow<double> b(5);
  1192. b(0) = -3.0;
  1193. b(2) = -1.0;
  1194. resa = max(b);
  1195. REQUIRE( resa == Approx(1e-5) );
  1196. b(0) = 3.0;
  1197. b(2) = 1.0;
  1198. resa = max(b);
  1199. REQUIRE( resa == Approx(3.0) );
  1200. b(0) = -1.3;
  1201. b(1) = -2.4;
  1202. b(2) = -3.1;
  1203. b(3) = -4.4;
  1204. b(4) = -1.4;
  1205. resa = max(b);
  1206. REQUIRE( resa == Approx(-1.3) );
  1207. SpMat<double> c(6, 5);
  1208. c(0, 0) = 1.0;
  1209. c(1, 0) = 3.0;
  1210. c(2, 0) = 4.0;
  1211. c(3, 0) = 0.6;
  1212. c(4, 0) = -1.4;
  1213. c(5, 0) = 1.2;
  1214. c(3, 2) = 1.3;
  1215. c(2, 3) = -4.0;
  1216. c(4, 3) = -1.4;
  1217. c(5, 2) = -3.4;
  1218. c(5, 3) = -4.1;
  1219. SpMat<double> res = max(c, 0);
  1220. REQUIRE( res.n_rows == 1 );
  1221. REQUIRE( res.n_cols == 5 );
  1222. REQUIRE( (double) res(0, 0) == Approx(4.0) );
  1223. REQUIRE( (double) res(0, 1) == Approx(1e-5) );
  1224. REQUIRE( (double) res(0, 2) == Approx(1.3) );
  1225. REQUIRE( (double) res(0, 3) == Approx(1e-5) );
  1226. REQUIRE( (double) res(0, 4) == Approx(1e-5) );
  1227. res = max(c, 1);
  1228. REQUIRE( res.n_rows == 6 );
  1229. REQUIRE( res.n_cols == 1 );
  1230. REQUIRE( (double) res(0, 0) == Approx(1.0) );
  1231. REQUIRE( (double) res(1, 0) == Approx(3.0) );
  1232. REQUIRE( (double) res(2, 0) == Approx(4.0) );
  1233. REQUIRE( (double) res(3, 0) == Approx(1.3) );
  1234. REQUIRE( (double) res(4, 0) == Approx(1e-5) );
  1235. REQUIRE( (double) res(5, 0) == Approx(1.2) );
  1236. }
  1237. TEST_CASE("spmat_min_cx_test")
  1238. {
  1239. SpCol<std::complex<double> > a(5);
  1240. a(0) = std::complex<double>(3.0, -2.0);
  1241. a(2) = std::complex<double>(1.0, 1.0);
  1242. std::complex<double> res = min(a);
  1243. REQUIRE( res.real() == Approx(1e-5) );
  1244. REQUIRE( res.imag() == Approx(1e-5) );
  1245. a(0) = std::complex<double>(-3.0, -2.0);
  1246. a(2) = std::complex<double>(-1.0, -1.0);
  1247. res = min(a);
  1248. REQUIRE( res.real() == Approx(1e-5) );
  1249. REQUIRE( res.imag() == Approx(1e-5) );
  1250. a(0) = std::complex<double>(1.0, 0.5);
  1251. a(1) = std::complex<double>(2.4, 1.4);
  1252. a(2) = std::complex<double>(0.5, 0.5);
  1253. a(3) = std::complex<double>(2.0, 2.0);
  1254. a(4) = std::complex<double>(1.4, -1.4);
  1255. res = min(a);
  1256. REQUIRE( res.real() == Approx(0.5) );
  1257. REQUIRE( res.imag() == Approx(0.5) );
  1258. SpRow<std::complex<double> > b(5);
  1259. b(0) = std::complex<double>(3.0, -2.0);
  1260. b(2) = std::complex<double>(1.0, 1.0);
  1261. res = min(b);
  1262. REQUIRE( res.real() == Approx(1e-5) );
  1263. REQUIRE( res.imag() == Approx(1e-5) );
  1264. b(0) = std::complex<double>(-3.0, -2.0);
  1265. b(2) = std::complex<double>(-1.0, -1.0);
  1266. res = min(b);
  1267. REQUIRE( res.real() == Approx(1e-5) );
  1268. REQUIRE( res.imag() == Approx(1e-5) );
  1269. b(0) = std::complex<double>(1.0, 0.5);
  1270. b(1) = std::complex<double>(2.4, 1.4);
  1271. b(2) = std::complex<double>(0.5, 0.5);
  1272. b(3) = std::complex<double>(2.0, 2.0);
  1273. b(4) = std::complex<double>(1.4, -1.4);
  1274. res = min(b);
  1275. REQUIRE( res.real() == Approx(0.5) );
  1276. REQUIRE( res.imag() == Approx(0.5) );
  1277. SpMat<std::complex<double> > c(4, 3);
  1278. c(0, 0) = std::complex<double>(1.0, 2.0);
  1279. c(0, 1) = std::complex<double>(0.5, 0.5);
  1280. c(0, 2) = std::complex<double>(2.0, 4.0);
  1281. c(1, 1) = std::complex<double>(-1.0, -2.0);
  1282. c(2, 1) = std::complex<double>(-3.0, -3.0);
  1283. c(3, 1) = std::complex<double>(0.25, 0.25);
  1284. SpMat<std::complex<double> > r = min(c, 0);
  1285. REQUIRE( r.n_rows == 1 );
  1286. REQUIRE( r.n_cols == 3 );
  1287. REQUIRE( ((std::complex<double>) r(0, 0)).real() == Approx(1e-5) );
  1288. REQUIRE( ((std::complex<double>) r(0, 0)).imag() == Approx(1e-5) );
  1289. REQUIRE( ((std::complex<double>) r(0, 1)).real() == Approx(0.25) );
  1290. REQUIRE( ((std::complex<double>) r(0, 1)).imag() == Approx(0.25) );
  1291. REQUIRE( ((std::complex<double>) r(0, 2)).real() == Approx(1e-5) );
  1292. REQUIRE( ((std::complex<double>) r(0, 2)).imag() == Approx(1e-5) );
  1293. r = min(c, 1);
  1294. REQUIRE( r.n_rows == 4 );
  1295. REQUIRE( r.n_cols == 1 );
  1296. REQUIRE( ((std::complex<double>) r(0, 0)).real() == Approx(0.5) );
  1297. REQUIRE( ((std::complex<double>) r(0, 0)).imag() == Approx(0.5) );
  1298. REQUIRE( ((std::complex<double>) r(1, 0)).real() == Approx(1e-5) );
  1299. REQUIRE( ((std::complex<double>) r(1, 0)).imag() == Approx(1e-5) );
  1300. REQUIRE( ((std::complex<double>) r(2, 0)).real() == Approx(1e-5) );
  1301. REQUIRE( ((std::complex<double>) r(2, 0)).imag() == Approx(1e-5) );
  1302. REQUIRE( ((std::complex<double>) r(3, 0)).real() == Approx(1e-5) );
  1303. REQUIRE( ((std::complex<double>) r(3, 0)).imag() == Approx(1e-5) );
  1304. }
  1305. TEST_CASE("spmat_max_cx_test")
  1306. {
  1307. SpCol<std::complex<double> > a(5);
  1308. a(0) = std::complex<double>(3.0, -2.0);
  1309. a(2) = std::complex<double>(1.0, 1.0);
  1310. std::complex<double> res = max(a);
  1311. REQUIRE( res.real() == Approx(3.0) );
  1312. REQUIRE( res.imag() == Approx(-2.0) );
  1313. a(0) = std::complex<double>(0);
  1314. a(2) = std::complex<double>(0);
  1315. res = max(a);
  1316. REQUIRE( res.real() == Approx(1e-5) );
  1317. REQUIRE( res.imag() == Approx(1e-5) );
  1318. a(0) = std::complex<double>(1.0, 0.5);
  1319. a(1) = std::complex<double>(2.4, 1.4);
  1320. a(2) = std::complex<double>(0.5, 0.5);
  1321. a(3) = std::complex<double>(2.0, 2.0);
  1322. a(4) = std::complex<double>(1.4, -1.4);
  1323. res = max(a);
  1324. REQUIRE( res.real() == Approx(2.0) );
  1325. REQUIRE( res.imag() == Approx(2.0) );
  1326. SpRow<std::complex<double> > b(5);
  1327. b(0) = std::complex<double>(3.0, -2.0);
  1328. b(2) = std::complex<double>(1.0, 1.0);
  1329. res = max(b);
  1330. REQUIRE( res.real() == Approx(3.0) );
  1331. REQUIRE( res.imag() == Approx(-2.0) );
  1332. b(0) = std::complex<double>(0);
  1333. b(2) = std::complex<double>(0);
  1334. res = max(b);
  1335. REQUIRE( res.real() == Approx(1e-5) );
  1336. REQUIRE( res.imag() == Approx(1e-5) );
  1337. b(0) = std::complex<double>(1.0, 0.5);
  1338. b(1) = std::complex<double>(2.4, 1.4);
  1339. b(2) = std::complex<double>(0.5, 0.5);
  1340. b(3) = std::complex<double>(2.0, 2.0);
  1341. b(4) = std::complex<double>(1.4, -1.4);
  1342. res = max(b);
  1343. REQUIRE( res.real() == Approx(2.0) );
  1344. REQUIRE( res.imag() == Approx(2.0) );
  1345. SpMat<std::complex<double> > c(4, 3);
  1346. c(0, 0) = std::complex<double>(1.0, 2.0);
  1347. c(0, 1) = std::complex<double>(0.5, 0.5);
  1348. c(1, 1) = std::complex<double>(-1.0, -2.0);
  1349. c(2, 1) = std::complex<double>(-3.0, -3.0);
  1350. c(3, 1) = std::complex<double>(0.25, 0.25);
  1351. SpMat<std::complex<double> > r = max(c, 0);
  1352. REQUIRE( r.n_rows == 1 );
  1353. REQUIRE( r.n_cols == 3 );
  1354. REQUIRE( ((std::complex<double>) r(0, 0)).real() == Approx(1.0) );
  1355. REQUIRE( ((std::complex<double>) r(0, 0)).imag() == Approx(2.0) );
  1356. REQUIRE( ((std::complex<double>) r(0, 1)).real() == Approx(-3.0) );
  1357. REQUIRE( ((std::complex<double>) r(0, 1)).imag() == Approx(-3.0) );
  1358. REQUIRE( ((std::complex<double>) r(0, 2)).real() == Approx(1e-5) );
  1359. REQUIRE( ((std::complex<double>) r(0, 2)).imag() == Approx(1e-5) );
  1360. r = max(c, 1);
  1361. REQUIRE( r.n_rows == 4 );
  1362. REQUIRE( r.n_cols == 1 );
  1363. REQUIRE( ((std::complex<double>) r(0, 0)).real() == Approx(1.0) );
  1364. REQUIRE( ((std::complex<double>) r(0, 0)).imag() == Approx(2.0) );
  1365. REQUIRE( ((std::complex<double>) r(1, 0)).real() == Approx(-1.0) );
  1366. REQUIRE( ((std::complex<double>) r(1, 0)).imag() == Approx(-2.0) );
  1367. REQUIRE( ((std::complex<double>) r(2, 0)).real() == Approx(-3.0) );
  1368. REQUIRE( ((std::complex<double>) r(2, 0)).imag() == Approx(-3.0) );
  1369. REQUIRE( ((std::complex<double>) r(3, 0)).real() == Approx(0.25) );
  1370. REQUIRE( ((std::complex<double>) r(3, 0)).imag() == Approx(0.25) );
  1371. }
  1372. TEST_CASE("spmat_complex_constructor_test")
  1373. {
  1374. // First make two sparse matrices.
  1375. SpMat<double> a(8, 10);
  1376. SpMat<double> b(8, 10);
  1377. a(0, 0) = 4;
  1378. a(4, 2) = 5;
  1379. a(5, 3) = 6;
  1380. a(6, 3) = 7;
  1381. a(1, 4) = 1;
  1382. a(5, 4) = 6;
  1383. a(7, 6) = 3;
  1384. a(0, 7) = 2;
  1385. a(3, 7) = 3;
  1386. b(0, 0) = 4;
  1387. b(4, 2) = 5;
  1388. b(7, 3) = 4;
  1389. b(1, 4) = 1;
  1390. b(3, 4) = 6;
  1391. b(5, 4) = -1;
  1392. b(6, 4) = 2;
  1393. b(7, 4) = 3;
  1394. b(6, 5) = 2;
  1395. b(6, 6) = 3;
  1396. b(3, 7) = 4;
  1397. b(6, 7) = 5;
  1398. SpMat<std::complex<double> > c(a, b);
  1399. REQUIRE( c.n_nonzero == 16 );
  1400. REQUIRE( (std::complex<double>) c(0, 0) == std::complex<double>(4, 4) );
  1401. REQUIRE( (std::complex<double>) c(4, 2) == std::complex<double>(5, 5) );
  1402. REQUIRE( (std::complex<double>) c(5, 3) == std::complex<double>(6, 0) );
  1403. REQUIRE( (std::complex<double>) c(6, 3) == std::complex<double>(7, 0) );
  1404. REQUIRE( (std::complex<double>) c(7, 3) == std::complex<double>(0, 4) );
  1405. REQUIRE( (std::complex<double>) c(1, 4) == std::complex<double>(1, 1) );
  1406. REQUIRE( (std::complex<double>) c(3, 4) == std::complex<double>(0, 6) );
  1407. REQUIRE( (std::complex<double>) c(5, 4) == std::complex<double>(6, -1) );
  1408. REQUIRE( (std::complex<double>) c(6, 4) == std::complex<double>(0, 2) );
  1409. REQUIRE( (std::complex<double>) c(7, 4) == std::complex<double>(0, 3) );
  1410. REQUIRE( (std::complex<double>) c(6, 5) == std::complex<double>(0, 2) );
  1411. REQUIRE( (std::complex<double>) c(6, 6) == std::complex<double>(0, 3) );
  1412. REQUIRE( (std::complex<double>) c(7, 6) == std::complex<double>(3, 0) );
  1413. REQUIRE( (std::complex<double>) c(0, 7) == std::complex<double>(2, 0) );
  1414. REQUIRE( (std::complex<double>) c(3, 7) == std::complex<double>(3, 4) );
  1415. REQUIRE( (std::complex<double>) c(6, 7) == std::complex<double>(0, 5) );
  1416. }
  1417. TEST_CASE("spmat_unary_operators_test")
  1418. {
  1419. SpMat<int> a(3, 3);
  1420. SpMat<int> b(3, 3);
  1421. a(0, 0) = 1;
  1422. a(1, 2) = 4;
  1423. a(2, 2) = 5;
  1424. b(0, 1) = 1;
  1425. b(1, 0) = 2;
  1426. b(1, 2) = -4;
  1427. b(2, 2) = 5;
  1428. SpMat<int> c = a + b;
  1429. REQUIRE( c.n_nonzero == 4 );
  1430. REQUIRE( (double) c(0, 0) == 1 );
  1431. REQUIRE( (double) c(1, 0) == 2 );
  1432. REQUIRE( (double) c(2, 0) == 0 );
  1433. REQUIRE( (double) c(0, 1) == 1 );
  1434. REQUIRE( (double) c(1, 1) == 0 );
  1435. REQUIRE( (double) c(2, 1) == 0 );
  1436. REQUIRE( (double) c(0, 2) == 0 );
  1437. REQUIRE( (double) c(1, 2) == 0 );
  1438. REQUIRE( (double) c(2, 2) == 10 );
  1439. c = a - b;
  1440. REQUIRE( c.n_nonzero == 4 );
  1441. REQUIRE( (double) c(0, 0) == 1 );
  1442. REQUIRE( (double) c(1, 0) == -2 );
  1443. REQUIRE( (double) c(2, 0) == 0 );
  1444. REQUIRE( (double) c(0, 1) == -1 );
  1445. REQUIRE( (double) c(1, 1) == 0 );
  1446. REQUIRE( (double) c(2, 1) == 0 );
  1447. REQUIRE( (double) c(0, 2) == 0 );
  1448. REQUIRE( (double) c(1, 2) == 8 );
  1449. REQUIRE( (double) c(2, 2) == 0 );
  1450. c = a % b;
  1451. REQUIRE( c.n_nonzero == 2 );
  1452. REQUIRE( (double) c(0, 0) == 0 );
  1453. REQUIRE( (double) c(1, 0) == 0 );
  1454. REQUIRE( (double) c(2, 0) == 0 );
  1455. REQUIRE( (double) c(0, 1) == 0 );
  1456. REQUIRE( (double) c(1, 1) == 0 );
  1457. REQUIRE( (double) c(2, 1) == 0 );
  1458. REQUIRE( (double) c(0, 2) == 0 );
  1459. REQUIRE( (double) c(1, 2) == -16 );
  1460. REQUIRE( (double) c(2, 2) == 25 );
  1461. a(0, 0) = 4;
  1462. b(0, 0) = 2;
  1463. /*
  1464. c = a / b;
  1465. REQUIRE( c.n_nonzero == 3 );
  1466. REQUIRE( (double) c(0, 0) == 2 );
  1467. REQUIRE( (double) c(1, 0) == 0 );
  1468. REQUIRE( (double) c(2, 0) == 0 );
  1469. REQUIRE( (double) c(0, 1) == 0 );
  1470. REQUIRE( (double) c(1, 1) == 0 );
  1471. REQUIRE( (double) c(2, 1) == 0 );
  1472. REQUIRE( (double) c(0, 2) == 0 );
  1473. REQUIRE( (double) c(1, 2) == -1 );
  1474. REQUIRE( (double) c(2, 2) == 1 );
  1475. */
  1476. }
  1477. TEST_CASE("spmat_unary_val_operators_test")
  1478. {
  1479. SpMat<double> a(2, 2);
  1480. a(0, 0) = 2.0;
  1481. a(1, 1) = -3.0;
  1482. SpMat<double> b = a * 3.0;
  1483. REQUIRE( b.n_nonzero == 2 );
  1484. REQUIRE( (double) b(0, 0) == Approx(6.0) );
  1485. REQUIRE( (double) b(0, 1) == Approx(1e-5) );
  1486. REQUIRE( (double) b(1, 0) == Approx(1e-5) );
  1487. REQUIRE( (double) b(1, 1) == Approx(-9.0) );
  1488. b = a / 3.0;
  1489. REQUIRE( b.n_nonzero == 2 );
  1490. REQUIRE( (double) b(0, 0) == Approx(2.0 / 3.0) );
  1491. REQUIRE( (double) b(0, 1) == Approx(1e-5) );
  1492. REQUIRE( (double) b(1, 0) == Approx(1e-5) );
  1493. REQUIRE( (double) b(1, 1) == Approx(-1.0) );
  1494. }
  1495. TEST_CASE("spmat_sparse_unary_multiplication_test")
  1496. {
  1497. SpMat<double> spaa(10, 10);
  1498. spaa(1, 5) = 0.4;
  1499. spaa(0, 4) = 0.3;
  1500. spaa(0, 8) = 1.2;
  1501. spaa(3, 0) = 1.1;
  1502. spaa(3, 1) = 1.1;
  1503. spaa(3, 2) = 1.1;
  1504. spaa(4, 4) = 0.2;
  1505. spaa(4, 9) = 0.1;
  1506. spaa(6, 2) = 4.1;
  1507. spaa(6, 8) = 4.1;
  1508. spaa(7, 5) = 1.0;
  1509. spaa(8, 9) = 0.4;
  1510. spaa(9, 4) = 0.4;
  1511. double correctResultB[10][10] =
  1512. {{ 0.00, 0.00, 0.00, 0.00, 0.06, 0.00, 0.00, 0.00, 0.00, 0.51 },
  1513. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
  1514. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
  1515. { 0.00, 0.00, 0.00, 0.00, 0.33, 0.44, 0.00, 0.00, 1.32, 0.00 },
  1516. { 0.00, 0.00, 0.00, 0.00, 0.08, 0.00, 0.00, 0.00, 0.00, 0.02 },
  1517. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
  1518. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.64 },
  1519. { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
  1520. { 0.00, 0.00, 0.00, 0.00, 0.16, 0.00, 0.00, 0.00, 0.00, 0.00 },
  1521. { 0.00, 0.00, 0.00, 0.00, 0.08, 0.00, 0.00, 0.00, 0.00, 0.04 }};
  1522. SpMat<double> spab = spaa * spaa;
  1523. for (uword i = 0; i < 10; i++)
  1524. {
  1525. for (uword j = 0; j < 10; j++)
  1526. {
  1527. REQUIRE( (double) spab(i, j) == Approx(correctResultB[i][j]) );
  1528. }
  1529. }
  1530. SpMat<double> spac(15, 15);
  1531. spac(6, 10) = 0.4;
  1532. spac(5, 9) = 0.3;
  1533. spac(5, 13) = 1.2;
  1534. spac(8, 5) = 1.1;
  1535. spac(8, 6) = 1.1;
  1536. spac(8, 7) = 1.1;
  1537. spac(9, 9) = 0.2;
  1538. spac(9, 14) = 0.1;
  1539. spac(11, 7) = 4.1;
  1540. spac(11, 13) = 4.1;
  1541. spac(12, 10) = 1.0;
  1542. spac(13, 14) = 0.4;
  1543. spac(14, 9) = 0.4;
  1544. spab = spaa * spac.submat(5, 5, 14, 14);
  1545. for (uword i = 0; i < 10; i++)
  1546. {
  1547. for (uword j = 0; j < 10; j++)
  1548. {
  1549. REQUIRE( (double) spab(i, j) == Approx(correctResultB[i][j]) );
  1550. }
  1551. }
  1552. }
  1553. TEST_CASE("spmat_unary_operator_test_2")
  1554. {
  1555. SpMat<double> a(3, 3);
  1556. a(0, 0) = 1;
  1557. a(0, 2) = 3.5;
  1558. a(1, 2) = 4.0;
  1559. a(2, 2) = -3.0;
  1560. mat b(3, 3);
  1561. b.fill(3.0);
  1562. mat c = a + b;
  1563. REQUIRE( c(0, 0) == Approx(4.0) );
  1564. REQUIRE( c(1, 0) == Approx(3.0) );
  1565. REQUIRE( c(2, 0) == Approx(3.0) );
  1566. REQUIRE( c(0, 1) == Approx(3.0) );
  1567. REQUIRE( c(1, 1) == Approx(3.0) );
  1568. REQUIRE( c(2, 1) == Approx(3.0) );
  1569. REQUIRE( c(0, 2) == Approx(6.5) );
  1570. REQUIRE( c(1, 2) == Approx(7.0) );
  1571. REQUIRE( c(2, 2) == Approx(1e-5) );
  1572. c = a - b;
  1573. REQUIRE( c(0, 0) == Approx(-2.0) );
  1574. REQUIRE( c(1, 0) == Approx(-3.0) );
  1575. REQUIRE( c(2, 0) == Approx(-3.0) );
  1576. REQUIRE( c(0, 1) == Approx(-3.0) );
  1577. REQUIRE( c(1, 1) == Approx(-3.0) );
  1578. REQUIRE( c(2, 1) == Approx(-3.0) );
  1579. REQUIRE( c(0, 2) == Approx(0.5) );
  1580. REQUIRE( c(1, 2) == Approx(1.0) );
  1581. REQUIRE( c(2, 2) == Approx(-6.0) );
  1582. SpMat<double> d = a % b;
  1583. REQUIRE( d.n_nonzero == 4 );
  1584. REQUIRE( (double) d(0, 0) == Approx(3.0) );
  1585. REQUIRE( (double) d(1, 0) == Approx(1e-5) );
  1586. REQUIRE( (double) d(2, 0) == Approx(1e-5) );
  1587. REQUIRE( (double) d(0, 1) == Approx(1e-5) );
  1588. REQUIRE( (double) d(1, 1) == Approx(1e-5) );
  1589. REQUIRE( (double) d(2, 1) == Approx(1e-5) );
  1590. REQUIRE( (double) d(0, 2) == Approx(10.5) );
  1591. REQUIRE( (double) d(1, 2) == Approx(12.0) );
  1592. REQUIRE( (double) d(2, 2) == Approx(-9.0) );
  1593. d = a / b;
  1594. REQUIRE( d.n_nonzero == 4 );
  1595. REQUIRE( (double) d(0, 0) == Approx((1.0 / 3.0)) );
  1596. REQUIRE( (double) d(1, 0) == Approx(1e-5) );
  1597. REQUIRE( (double) d(2, 0) == Approx(1e-5) );
  1598. REQUIRE( (double) d(0, 1) == Approx(1e-5) );
  1599. REQUIRE( (double) d(1, 1) == Approx(1e-5) );
  1600. REQUIRE( (double) d(2, 1) == Approx(1e-5) );
  1601. REQUIRE( (double) d(0, 2) == Approx((3.5 / 3.0)) );
  1602. REQUIRE( (double) d(1, 2) == Approx((4.0 / 3.0)) );
  1603. REQUIRE( (double) d(2, 2) == Approx(-1.0) );
  1604. c = a * b;
  1605. REQUIRE( (double) c(0, 0) == Approx(13.5) );
  1606. REQUIRE( (double) c(1, 0) == Approx(12.0) );
  1607. REQUIRE( (double) c(2, 0) == Approx(-9.0) );
  1608. REQUIRE( (double) c(0, 1) == Approx(13.5) );
  1609. REQUIRE( (double) c(1, 1) == Approx(12.0) );
  1610. REQUIRE( (double) c(2, 1) == Approx(-9.0) );
  1611. REQUIRE( (double) c(0, 2) == Approx(13.5) );
  1612. REQUIRE( (double) c(1, 2) == Approx(12.0) );
  1613. REQUIRE( (double) c(2, 2) == Approx(-9.0) );
  1614. c = b * a;
  1615. REQUIRE( (double) c(0, 0) == Approx(3.0) );
  1616. REQUIRE( (double) c(1, 0) == Approx(3.0) );
  1617. REQUIRE( (double) c(2, 0) == Approx(3.0) );
  1618. REQUIRE( (double) c(0, 1) == Approx(1e-5) );
  1619. REQUIRE( (double) c(1, 1) == Approx(1e-5) );
  1620. REQUIRE( (double) c(2, 1) == Approx(1e-5) );
  1621. REQUIRE( (double) c(0, 2) == Approx(13.5) );
  1622. REQUIRE( (double) c(1, 2) == Approx(13.5) );
  1623. REQUIRE( (double) c(2, 2) == Approx(13.5) );
  1624. }
  1625. TEST_CASE("spmat_mat_operator_tests")
  1626. {
  1627. SpMat<double> a(3, 3);
  1628. a(0, 0) = 2.0;
  1629. a(1, 2) = 3.5;
  1630. a(2, 1) = -2.0;
  1631. a(2, 2) = 4.5;
  1632. mat b(3, 3);
  1633. b.fill(2.0);
  1634. mat c(b);
  1635. c += a;
  1636. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  1637. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  1638. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  1639. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  1640. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  1641. REQUIRE( (double) c(2, 1) == Approx(1e-5) );
  1642. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  1643. REQUIRE( (double) c(1, 2) == Approx(5.5) );
  1644. REQUIRE( (double) c(2, 2) == Approx(6.5) );
  1645. c = b + a;
  1646. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  1647. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  1648. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  1649. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  1650. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  1651. REQUIRE( (double) c(2, 1) == Approx(1e-5) );
  1652. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  1653. REQUIRE( (double) c(1, 2) == Approx(5.5) );
  1654. REQUIRE( (double) c(2, 2) == Approx(6.5) );
  1655. c = b;
  1656. c -= a;
  1657. REQUIRE( (double) c(0, 0) == Approx(1e-5) );
  1658. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  1659. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  1660. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  1661. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  1662. REQUIRE( (double) c(2, 1) == Approx(4.0) );
  1663. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  1664. REQUIRE( (double) c(1, 2) == Approx(-1.5) );
  1665. REQUIRE( (double) c(2, 2) == Approx(-2.5) );
  1666. c = b - a;
  1667. REQUIRE( (double) c(0, 0) == Approx(1e-5) );
  1668. REQUIRE( (double) c(1, 0) == Approx(2.0) );
  1669. REQUIRE( (double) c(2, 0) == Approx(2.0) );
  1670. REQUIRE( (double) c(0, 1) == Approx(2.0) );
  1671. REQUIRE( (double) c(1, 1) == Approx(2.0) );
  1672. REQUIRE( (double) c(2, 1) == Approx(4.0) );
  1673. REQUIRE( (double) c(0, 2) == Approx(2.0) );
  1674. REQUIRE( (double) c(1, 2) == Approx(-1.5) );
  1675. REQUIRE( (double) c(2, 2) == Approx(-2.5) );
  1676. c = b;
  1677. c *= a;
  1678. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  1679. REQUIRE( (double) c(1, 0) == Approx(4.0) );
  1680. REQUIRE( (double) c(2, 0) == Approx(4.0) );
  1681. REQUIRE( (double) c(0, 1) == Approx(-4.0) );
  1682. REQUIRE( (double) c(1, 1) == Approx(-4.0) );
  1683. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  1684. REQUIRE( (double) c(0, 2) == Approx(16.0) );
  1685. REQUIRE( (double) c(1, 2) == Approx(16.0) );
  1686. REQUIRE( (double) c(2, 2) == Approx(16.0) );
  1687. mat e = b * a;
  1688. REQUIRE( (double) e(0, 0) == Approx(4.0) );
  1689. REQUIRE( (double) e(1, 0) == Approx(4.0) );
  1690. REQUIRE( (double) e(2, 0) == Approx(4.0) );
  1691. REQUIRE( (double) e(0, 1) == Approx(-4.0) );
  1692. REQUIRE( (double) e(1, 1) == Approx(-4.0) );
  1693. REQUIRE( (double) e(2, 1) == Approx(-4.0) );
  1694. REQUIRE( (double) e(0, 2) == Approx(16.0) );
  1695. REQUIRE( (double) e(1, 2) == Approx(16.0) );
  1696. REQUIRE( (double) e(2, 2) == Approx(16.0) );
  1697. c = b;
  1698. c %= a;
  1699. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  1700. REQUIRE( (double) c(1, 0) == Approx(1e-5) );
  1701. REQUIRE( (double) c(2, 0) == Approx(1e-5) );
  1702. REQUIRE( (double) c(0, 1) == Approx(1e-5) );
  1703. REQUIRE( (double) c(1, 1) == Approx(1e-5) );
  1704. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  1705. REQUIRE( (double) c(0, 2) == Approx(1e-5) );
  1706. REQUIRE( (double) c(1, 2) == Approx(7.0) );
  1707. REQUIRE( (double) c(2, 2) == Approx(9.0) );
  1708. SpMat<double> d = b % a;
  1709. REQUIRE( d.n_nonzero == 4 );
  1710. REQUIRE( (double) c(0, 0) == Approx(4.0) );
  1711. REQUIRE( (double) c(2, 1) == Approx(-4.0) );
  1712. REQUIRE( (double) c(1, 2) == Approx(7.0) );
  1713. REQUIRE( (double) c(2, 2) == Approx(9.0) );
  1714. c = b;
  1715. c /= a;
  1716. REQUIRE( c(0, 0) == Approx(1.0) );
  1717. REQUIRE( std::isinf(c(1, 0)) );
  1718. REQUIRE( std::isinf(c(2, 0)) );
  1719. REQUIRE( std::isinf(c(0, 1)) );
  1720. REQUIRE( std::isinf(c(1, 1)) );
  1721. REQUIRE( c(2, 1) == Approx(-1.0) );
  1722. REQUIRE( std::isinf(c(0, 2)) );
  1723. REQUIRE( c(1, 2) == Approx(2.0 / 3.5) );
  1724. REQUIRE( c(2, 2) == Approx(2.0 / 4.5) );
  1725. }
  1726. TEST_CASE("spmat_empty_hadamard")
  1727. {
  1728. SpMat<double> x(5, 5), y(5, 5), z;
  1729. z = x % y;
  1730. REQUIRE( z.n_nonzero == 0 );
  1731. REQUIRE( z.n_rows == 5 );
  1732. REQUIRE( z.n_cols == 5 );
  1733. }
  1734. TEST_CASE("spmat_sparse_dense_in_place")
  1735. {
  1736. SpMat<double> a;
  1737. a.sprandu(50, 50, 0.1);
  1738. mat b;
  1739. b.randu(50, 50);
  1740. mat d( a);
  1741. for (uword c = 0; c < 50; ++c)
  1742. {
  1743. for (uword r = 0; r < 50; ++r)
  1744. {
  1745. if ((double) a(r, c) != 0)
  1746. REQUIRE( (double) a(r, c) == Approx(d(r, c)) );
  1747. else
  1748. REQUIRE( d(r, c) == Approx(1e-5) );
  1749. }
  1750. }
  1751. SpMat<double> x;
  1752. mat y;
  1753. x = a;
  1754. y = d;
  1755. x *= b;
  1756. y *= b;
  1757. for (uword c = 0; c < 50; ++c)
  1758. {
  1759. for (uword r = 0; r < 50; ++r)
  1760. {
  1761. if ((double) a(r, c) != 0)
  1762. REQUIRE( (double) a(r, c) == Approx(d(r, c)) );
  1763. else
  1764. REQUIRE( d(r, c) == Approx(1e-5) );
  1765. }
  1766. }
  1767. x = a;
  1768. y = d;
  1769. x /= b;
  1770. y /= b;
  1771. for (uword c = 0; c < 50; ++c)
  1772. {
  1773. for (uword r = 0; r < 50; ++r)
  1774. {
  1775. if ((double) a(r, c) != 0)
  1776. REQUIRE( (double) a(r, c) == Approx(d(r, c)) );
  1777. else
  1778. REQUIRE( d(r, c) == Approx(1e-5) );
  1779. }
  1780. }
  1781. x = a;
  1782. y = d;
  1783. x %= b;
  1784. y %= b;
  1785. for (uword c = 0; c < 50; ++c)
  1786. {
  1787. for (uword r = 0; r < 50; ++r)
  1788. {
  1789. if ((double) a(r, c) != 0)
  1790. REQUIRE( (double) a(r, c) == Approx(d(r, c)) );
  1791. else
  1792. REQUIRE(d(r, c) == Approx(1e-5) );
  1793. }
  1794. }
  1795. }
  1796. TEST_CASE("spmat_sparse_dense_not_in_place")
  1797. {
  1798. SpMat<double> a;
  1799. a.sprandu(50, 50, 0.1);
  1800. mat b;
  1801. b.randu(50, 50);
  1802. mat d(a);
  1803. SpMat<double> x;
  1804. mat y;
  1805. mat z;
  1806. y = a + b;
  1807. z = d + b;
  1808. for (uword c = 0; c < 50; ++c)
  1809. {
  1810. for(uword r = 0; r < 50; ++r)
  1811. {
  1812. if ((double) y(r, c) != 0)
  1813. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1814. else
  1815. REQUIRE( z(r, c) == Approx(1e-5) );
  1816. }
  1817. }
  1818. y = a - b;
  1819. z = d - b;
  1820. for (uword c = 0; c < 50; ++c)
  1821. {
  1822. for (uword r = 0; r < 50; ++r)
  1823. {
  1824. if ((double) y(r, c) != 0)
  1825. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1826. else
  1827. REQUIRE( z(r, c) == Approx(1e-5) );
  1828. }
  1829. }
  1830. y = a * b;
  1831. z = d * b;
  1832. for (uword c = 0; c < 50; ++c)
  1833. {
  1834. for (uword r = 0; r < 50; ++r)
  1835. {
  1836. if ((double) y(r, c) != 0)
  1837. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1838. else
  1839. REQUIRE( z(r, c) == Approx(1e-5) );
  1840. }
  1841. }
  1842. y = a % b;
  1843. z = d % b;
  1844. for (uword c = 0; c < 50; ++c)
  1845. {
  1846. for (uword r = 0; r < 50; ++r)
  1847. {
  1848. if ((double) y(r, c) != 0)
  1849. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1850. else
  1851. REQUIRE( z(r, c) == Approx(1e-5) );
  1852. }
  1853. }
  1854. y = a / b;
  1855. z = d / b;
  1856. for (uword c = 0; c < 50; ++c)
  1857. {
  1858. for (uword r = 0; r < 50; ++r)
  1859. {
  1860. if ((double) y(r, c) != 0)
  1861. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1862. else
  1863. REQUIRE( z(r, c) == Approx(1e-5) );
  1864. }
  1865. }
  1866. y = b + a;
  1867. z = b + d;
  1868. for (uword c = 0; c < 50; ++c)
  1869. {
  1870. for (uword r = 0; r < 50; ++r)
  1871. {
  1872. if ((double) y(r, c) != 0)
  1873. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1874. else
  1875. REQUIRE( z(r, c) == Approx(1e-5) );
  1876. }
  1877. }
  1878. y = b - a;
  1879. z = b - d;
  1880. for (uword c = 0; c < 50; ++c)
  1881. {
  1882. for (uword r = 0; r < 50; ++r)
  1883. {
  1884. if ((double) y(r, c) != 0)
  1885. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1886. else
  1887. REQUIRE( z(r, c) == Approx(1e-5) );
  1888. }
  1889. }
  1890. y = b * a;
  1891. z = b * d;
  1892. for (uword c = 0; c < 50; ++c)
  1893. {
  1894. for (uword r = 0; r < 50; ++r)
  1895. {
  1896. if ((double) y(r, c) != 0)
  1897. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1898. else
  1899. REQUIRE( z(r, c) == Approx(1e-5) );
  1900. }
  1901. }
  1902. y = b % a;
  1903. z = b % d;
  1904. for (uword c = 0; c < 50; ++c)
  1905. {
  1906. for (uword r = 0; r < 50; ++r)
  1907. {
  1908. if ((double) y(r, c) != 0)
  1909. REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
  1910. else
  1911. REQUIRE( z(r, c) == Approx(1e-5) );
  1912. }
  1913. }
  1914. }
  1915. TEST_CASE("spmat_batch_insert_test")
  1916. {
  1917. Mat<uword> locations(2, 5);
  1918. locations(1, 0) = 1;
  1919. locations(0, 0) = 2;
  1920. locations(1, 1) = 1;
  1921. locations(0, 1) = 7;
  1922. locations(1, 2) = 4;
  1923. locations(0, 2) = 0;
  1924. locations(1, 3) = 4;
  1925. locations(0, 3) = 9;
  1926. locations(1, 4) = 5;
  1927. locations(0, 4) = 0;
  1928. Col<double> values(5);
  1929. values[0] = 1.5;
  1930. values[1] = -15.15;
  1931. values[2] = 2.2;
  1932. values[3] = 3.0;
  1933. values[4] = 5.0;
  1934. SpMat<double> m(locations, values, 10, 10, true);
  1935. REQUIRE( m.n_nonzero == 5 );
  1936. REQUIRE( m.n_rows == 10 );
  1937. REQUIRE( m.n_cols == 10 );
  1938. REQUIRE( (double) m(2, 1) == Approx(1.5) );
  1939. REQUIRE( (double) m(7, 1) == Approx(-15.15) );
  1940. REQUIRE( (double) m(0, 4) == Approx(2.2) );
  1941. REQUIRE( (double) m(9, 4) == Approx(3.0) );
  1942. REQUIRE( (double) m(0, 5) == Approx(5.0) );
  1943. REQUIRE( m.col_ptrs[11] == std::numeric_limits<uword>::max() );
  1944. // Auto size detection.
  1945. SpMat<double> n(locations, values, true);
  1946. REQUIRE( n.n_nonzero == 5 );
  1947. REQUIRE( n.n_rows == 10 );
  1948. REQUIRE( n.n_cols == 6 );
  1949. REQUIRE( (double) n(2, 1) == Approx(1.5) );
  1950. REQUIRE( (double) n(7, 1) == Approx(-15.15) );
  1951. REQUIRE( (double) n(0, 4) == Approx(2.2) );
  1952. REQUIRE( (double) n(9, 4) == Approx(3.0) );
  1953. REQUIRE( (double) n(0, 5) == Approx(5.0) );
  1954. REQUIRE( n.col_ptrs[7] == std::numeric_limits<uword>::max() );
  1955. }
  1956. TEST_CASE("spmat_batch_insert_unsorted_test")
  1957. {
  1958. Mat<uword> locations(2, 5);
  1959. locations(1, 0) = 4;
  1960. locations(0, 0) = 0;
  1961. locations(1, 1) = 1;
  1962. locations(0, 1) = 2;
  1963. locations(1, 2) = 4;
  1964. locations(0, 2) = 9;
  1965. locations(1, 3) = 5;
  1966. locations(0, 3) = 0;
  1967. locations(1, 4) = 1;
  1968. locations(0, 4) = 7;
  1969. Col<double> values(5);
  1970. values[1] = 1.5;
  1971. values[4] = -15.15;
  1972. values[0] = 2.2;
  1973. values[2] = 3.0;
  1974. values[3] = 5.0;
  1975. SpMat<double> m(locations, values, 10, 10, true);
  1976. REQUIRE( m.n_nonzero == 5 );
  1977. REQUIRE( m.n_rows == 10 );
  1978. REQUIRE( m.n_cols == 10 );
  1979. REQUIRE( (double) m(2, 1) == Approx(1.5) );
  1980. REQUIRE( (double) m(7, 1) == Approx(-15.15) );
  1981. REQUIRE( (double) m(0, 4) == Approx(2.2) );
  1982. REQUIRE( (double) m(9, 4) == Approx(3.0) );
  1983. REQUIRE( (double) m(0, 5) == Approx(5.0) );
  1984. // Auto size detection.
  1985. SpMat<double> n(locations, values, true);
  1986. REQUIRE( n.n_nonzero == 5 );
  1987. REQUIRE( n.n_rows == 10 );
  1988. REQUIRE( n.n_cols == 6 );
  1989. REQUIRE( (double) n(2, 1) == Approx(1.5) );
  1990. REQUIRE( (double) n(7, 1) == Approx(-15.15) );
  1991. REQUIRE( (double) n(0, 4) == Approx(2.2) );
  1992. REQUIRE( (double) n(9, 4) == Approx(3.0) );
  1993. REQUIRE( (double) n(0, 5) == Approx(5.0) );
  1994. }
  1995. TEST_CASE("spmat_batch_insert_empty_test")
  1996. {
  1997. Mat<uword> locations(2, 0);
  1998. Col<double> values;
  1999. SpMat<double> m(locations, values, 10, 10, false);
  2000. REQUIRE( m.n_nonzero == 0 );
  2001. REQUIRE( m.n_rows == 10 );
  2002. REQUIRE( m.n_cols == 10 );
  2003. REQUIRE( m.col_ptrs[11] == std::numeric_limits<uword>::max() );
  2004. SpMat<double> n(locations, values, false);
  2005. REQUIRE( n.n_nonzero == 0 );
  2006. REQUIRE( n.n_rows == 0 );
  2007. REQUIRE( n.n_cols == 0 );
  2008. REQUIRE( n.col_ptrs[1] == std::numeric_limits<uword>::max() );
  2009. SpMat<double> o(locations, values, 10, 10, true);
  2010. REQUIRE( o.n_nonzero == 0 );
  2011. REQUIRE( o.n_rows == 10 );
  2012. REQUIRE( o.n_cols == 10 );
  2013. REQUIRE( o.col_ptrs[11] == std::numeric_limits<uword>::max() );
  2014. SpMat<double> p(locations, values, true);
  2015. REQUIRE( p.n_nonzero == 0 );
  2016. REQUIRE( p.n_rows == 0 );
  2017. REQUIRE( p.n_cols == 0 );
  2018. REQUIRE( p.col_ptrs[1] == std::numeric_limits<uword>::max() );
  2019. }
  2020. // Make sure a matrix is the same as the other one.
  2021. template<typename T1, typename T2>
  2022. void CheckMatrices(const T1& a, const T2& b)
  2023. {
  2024. REQUIRE( a.n_rows == b.n_rows );
  2025. REQUIRE( a.n_cols == b.n_cols );
  2026. for (uword i = 0; i < a.n_elem; ++i)
  2027. REQUIRE( (double) a[i] == Approx((double) b[i]) );
  2028. }
  2029. // Test the constructor written by Dirk.
  2030. TEST_CASE("spmat_dirk_constructor_test")
  2031. {
  2032. // Come up with some values and stuff.
  2033. vec values = "4.0 2.0 1.0 3.2 1.2 3.5";
  2034. Col<uword> row_indices = "1 3 1 2 4 5";
  2035. Col<uword> col_ptrs = "0 2 2 3 4 6";
  2036. // Ok, now make a matrix.
  2037. sp_mat M(row_indices, col_ptrs, values, 6, 5);
  2038. REQUIRE( M.n_nonzero == 6 );
  2039. // Make the equivalent dense matrix.
  2040. mat D(6, 5);
  2041. D.fill(0);
  2042. D(1, 0) = 4.0;
  2043. D(3, 0) = 2.0;
  2044. D(1, 2) = 1.0;
  2045. D(2, 3) = 3.2;
  2046. D(4, 4) = 1.2;
  2047. D(5, 4) = 3.5;
  2048. // So now let's just do a bunch of operations and make sure everything is the
  2049. // same.
  2050. sp_mat dm = M * M.t();
  2051. mat dd = D * D.t();
  2052. CheckMatrices(dm, dd);
  2053. dm = M.t() * M;
  2054. dd = D.t() * D;
  2055. CheckMatrices(dm, dd);
  2056. sp_mat am = M + M;
  2057. mat ad = D + D;
  2058. CheckMatrices(am, ad);
  2059. dm = M + D;
  2060. ad = D + M;
  2061. CheckMatrices(dm, ad);
  2062. }
  2063. TEST_CASE("spmat_dirk_constructor_test2")
  2064. {
  2065. // note the zero at (1,1)
  2066. vec values = "4.0 2.0 0.0 1.0 3.2 1.2 3.5";
  2067. uvec row_indices = "1 3 1 1 2 4 5";
  2068. uvec col_ptrs = "0 2 3 4 5 7";
  2069. // Ok, now make a matrix.
  2070. sp_mat M(row_indices, col_ptrs, values, 6, 5);
  2071. REQUIRE( M.n_nonzero == 6 );
  2072. // Make the equivalent dense matrix.
  2073. mat D(6, 5);
  2074. D.fill(0);
  2075. D(1, 0) = 4.0;
  2076. D(3, 0) = 2.0;
  2077. D(1, 1) = 0.0;
  2078. D(1, 2) = 1.0;
  2079. D(2, 3) = 3.2;
  2080. D(4, 4) = 1.2;
  2081. D(5, 4) = 3.5;
  2082. // So now let's just do a bunch of operations and make sure everything is the
  2083. // same.
  2084. sp_mat dm = M * M.t();
  2085. mat dd = D * D.t();
  2086. CheckMatrices(dm, dd);
  2087. dm = M.t() * M;
  2088. dd = D.t() * D;
  2089. CheckMatrices(dm, dd);
  2090. sp_mat am = M + M;
  2091. mat ad = D + D;
  2092. CheckMatrices(am, ad);
  2093. dm = M + D;
  2094. ad = D + M;
  2095. CheckMatrices(dm, ad);
  2096. }
  2097. TEST_CASE("spmat_clear_test")
  2098. {
  2099. sp_mat x;
  2100. x.sprandu(10, 10, 0.6);
  2101. x.clear();
  2102. REQUIRE( x.n_cols == 0 );
  2103. REQUIRE( x.n_rows == 0 );
  2104. REQUIRE( x.n_nonzero == 0 );
  2105. }
  2106. TEST_CASE("spmat_batch_insert_zeroes_test")
  2107. {
  2108. Mat<uword> locations(2, 5);
  2109. locations(1, 0) = 1;
  2110. locations(0, 0) = 2;
  2111. locations(1, 1) = 1;
  2112. locations(0, 1) = 7;
  2113. locations(1, 2) = 4;
  2114. locations(0, 2) = 0;
  2115. locations(1, 3) = 4;
  2116. locations(0, 3) = 9;
  2117. locations(1, 4) = 5;
  2118. locations(0, 4) = 0;
  2119. Col<double> values(5);
  2120. values[0] = 1.5;
  2121. values[1] = -15.15;
  2122. values[2] = 2.2;
  2123. values[3] = 0.0;
  2124. values[4] = 5.0;
  2125. SpMat<double> m(locations, values, 10, 10, false, true);
  2126. REQUIRE( m.n_nonzero == 4 );
  2127. REQUIRE( m.n_rows == 10 );
  2128. REQUIRE( m.n_cols == 10 );
  2129. REQUIRE( (double) m(2, 1) == Approx(1.5) );
  2130. REQUIRE( (double) m(7, 1) == Approx(-15.15) );
  2131. REQUIRE( (double) m(0, 4) == Approx(2.2) );
  2132. REQUIRE( (double) m(9, 4) == Approx(1e-5) );
  2133. REQUIRE( (double) m(0, 5) == Approx(5.0) );
  2134. // Auto size detection.
  2135. SpMat<double> n(locations, values, false);
  2136. REQUIRE( n.n_nonzero == 4 );
  2137. REQUIRE( n.n_rows == 10 );
  2138. REQUIRE( n.n_cols == 6 );
  2139. REQUIRE( (double) n(2, 1) == Approx(1.5) );
  2140. REQUIRE( (double) n(7, 1) == Approx(-15.15) );
  2141. REQUIRE( (double) n(0, 4) == Approx(2.2) );
  2142. REQUIRE( (double) n(9, 4) == Approx(1e-5) );
  2143. REQUIRE( (double) n(0, 5) == Approx(5.0) );
  2144. }
  2145. TEST_CASE("spmat_batch_insert_unsorted_case_zeroes")
  2146. {
  2147. Mat<uword> locations(2, 5);
  2148. locations(1, 0) = 4;
  2149. locations(0, 0) = 0;
  2150. locations(1, 1) = 1;
  2151. locations(0, 1) = 2;
  2152. locations(1, 2) = 4;
  2153. locations(0, 2) = 9;
  2154. locations(1, 3) = 5;
  2155. locations(0, 3) = 0;
  2156. locations(1, 4) = 1;
  2157. locations(0, 4) = 7;
  2158. Col<double> values(5);
  2159. values[1] = 1.5;
  2160. values[4] = -15.15;
  2161. values[0] = 2.2;
  2162. values[2] = 0.0;
  2163. values[3] = 5.0;
  2164. SpMat<double> m(locations, values, 10, 10, true);
  2165. REQUIRE( m.n_nonzero == 4 );
  2166. REQUIRE( m.n_rows == 10 );
  2167. REQUIRE( m.n_cols == 10 );
  2168. REQUIRE( (double) m(2, 1) == Approx(1.5) );
  2169. REQUIRE( (double) m(7, 1) == Approx(-15.15) );
  2170. REQUIRE( (double) m(0, 4) == Approx(2.2) );
  2171. REQUIRE( (double) m(9, 4) == Approx(1e-5) );
  2172. REQUIRE( (double) m(0, 5) == Approx(5.0) );
  2173. REQUIRE( m.col_ptrs[11] == std::numeric_limits<uword>::max() );
  2174. // Auto size detection.
  2175. SpMat<double> n(locations, values, true);
  2176. REQUIRE( n.n_nonzero == 4 );
  2177. REQUIRE( n.n_rows == 10 );
  2178. REQUIRE( n.n_cols == 6 );
  2179. REQUIRE( (double) n(2, 1) == Approx(1.5) );
  2180. REQUIRE( (double) n(7, 1) == Approx(-15.15) );
  2181. REQUIRE( (double) n(0, 4) == Approx(2.2) );
  2182. REQUIRE( (double) n(9, 4) == Approx(1e-5) );
  2183. REQUIRE( (double) n(0, 5) == Approx(5.0) );
  2184. REQUIRE( n.col_ptrs[7] == std::numeric_limits<uword>::max() );
  2185. }
  2186. TEST_CASE("spmat_const_row_col_iterator_test")
  2187. {
  2188. mat X;
  2189. X.zeros(5, 5);
  2190. for (uword i = 0; i < 5; ++i)
  2191. {
  2192. X.col(i) += i;
  2193. }
  2194. for (uword i = 0; i < 5; ++i)
  2195. {
  2196. X.row(i) += 3 * i;
  2197. }
  2198. // Make sure default constructor works okay.
  2199. mat::const_row_col_iterator it;
  2200. // Make sure ++ operator, operator* and comparison operators work fine.
  2201. uword count = 0;
  2202. for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
  2203. {
  2204. // Check iterator value.
  2205. REQUIRE( *it == (count % 5) * 3 + (count / 5) );
  2206. // Check iterator position.
  2207. REQUIRE( it.row() == count % 5 );
  2208. REQUIRE( it.col() == count / 5 );
  2209. count++;
  2210. }
  2211. REQUIRE( count == 25 );
  2212. it = X.end_row_col();
  2213. do
  2214. {
  2215. --it;
  2216. count--;
  2217. // Check iterator value.
  2218. REQUIRE( *it == (count % 5) * 3 + (count / 5) );
  2219. // Check iterator position.
  2220. REQUIRE( it.row() == count % 5 );
  2221. REQUIRE( it.col() == count / 5 );
  2222. } while (it != X.begin_row_col());
  2223. REQUIRE( count == 0 );
  2224. }
  2225. TEST_CASE("spmat_row_col_iterator_test")
  2226. {
  2227. mat X;
  2228. X.zeros(5, 5);
  2229. for (uword i = 0; i < 5; ++i)
  2230. {
  2231. X.col(i) += i;
  2232. }
  2233. for (uword i = 0; i < 5; ++i)
  2234. {
  2235. X.row(i) += 3 * i;
  2236. }
  2237. // Make sure default constructor works okay.
  2238. mat::row_col_iterator it;
  2239. // Make sure ++ operator, operator* and comparison operators work fine.
  2240. uword count = 0;
  2241. for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
  2242. {
  2243. // Check iterator value.
  2244. REQUIRE( *it == (count % 5) * 3 + (count / 5) );
  2245. // Check iterator position.
  2246. REQUIRE( it.row() == count % 5 );
  2247. REQUIRE( it.col() == count / 5 );
  2248. count++;
  2249. }
  2250. REQUIRE( count == 25 );
  2251. it = X.end_row_col();
  2252. do
  2253. {
  2254. --it;
  2255. count--;
  2256. // Check iterator value.
  2257. REQUIRE( *it == (count % 5) * 3 + (count / 5) );
  2258. // Check iterator position.
  2259. REQUIRE( it.row() == count % 5 );
  2260. REQUIRE( it.col() == count / 5 );
  2261. } while (it != X.begin_row_col());
  2262. REQUIRE( count == 0 );
  2263. }
  2264. TEST_CASE("spmat_const_sprow_col_iterator_test")
  2265. {
  2266. sp_mat X(5, 5);
  2267. for (uword i = 0; i < 5; ++i)
  2268. {
  2269. X.col(i) += i;
  2270. }
  2271. for (uword i = 0; i < 5; ++i)
  2272. {
  2273. X.row(i) += 3 * i;
  2274. }
  2275. // Make sure default constructor works okay.
  2276. sp_mat::const_row_col_iterator it;
  2277. // Make sure ++ operator, operator* and comparison operators work fine.
  2278. uword count = 1;
  2279. for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
  2280. {
  2281. // Check iterator value.
  2282. REQUIRE( *it == (count % 5) * 3 + (count / 5) );
  2283. // Check iterator position.
  2284. REQUIRE( it.row() == count % 5 );
  2285. REQUIRE( it.col() == count / 5 );
  2286. count++;
  2287. }
  2288. REQUIRE( count == 25 );
  2289. it = X.end_row_col();
  2290. do
  2291. {
  2292. --it;
  2293. count--;
  2294. // Check iterator value.
  2295. REQUIRE( *it == (count % 5) * 3 + (count / 5) );
  2296. // Check iterator position.
  2297. REQUIRE( it.row() == count % 5 );
  2298. REQUIRE( it.col() == count / 5 );
  2299. } while (it != X.begin_row_col());
  2300. REQUIRE( count == 1 );
  2301. }
  2302. TEST_CASE("spmat_sprow_col_iterator_test")
  2303. {
  2304. sp_mat X(5, 5);
  2305. for (uword i = 0; i < 5; ++i)
  2306. {
  2307. X.col(i) += i;
  2308. }
  2309. for (uword i = 0; i < 5; ++i)
  2310. {
  2311. X.row(i) += 3 * i;
  2312. }
  2313. // Make sure default constructor works okay.
  2314. sp_mat::row_col_iterator it;
  2315. // Make sure ++ operator, operator* and comparison operators work fine.
  2316. uword count = 1;
  2317. for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
  2318. {
  2319. // Check iterator value.
  2320. REQUIRE( *it == (count % 5) * 3 + (count / 5) );
  2321. // Check iterator position.
  2322. REQUIRE( it.row() == count % 5 );
  2323. REQUIRE( it.col() == count / 5 );
  2324. count++;
  2325. }
  2326. REQUIRE( count == 25 );
  2327. it = X.end_row_col();
  2328. do
  2329. {
  2330. --it;
  2331. count--;
  2332. // Check iterator value.
  2333. REQUIRE( *it == (count % 5) * 3 + (count / 5) );
  2334. // Check iterator position.
  2335. REQUIRE( it.row() == count % 5 );
  2336. REQUIRE( it.col() == count / 5 );
  2337. } while (it != X.begin_row_col());
  2338. REQUIRE( count == 1 );
  2339. }
  2340. TEST_CASE("spmat_row_iterator_constructor")
  2341. {
  2342. // Create a row iterator with an exact position.
  2343. Mat<double> tmp =
  2344. { { 5.5, 0.0, 0.0 },
  2345. { 0.0, 0.0, 6.5 },
  2346. { 0.0, 7.5, 0.0 } };
  2347. SpMat<double> X(tmp);
  2348. SpMat<double>::const_row_iterator cri(X, 0, 1);
  2349. // This should end up at (1, 2) with value 6.5.
  2350. REQUIRE( cri.row() == 1 );
  2351. REQUIRE( cri.col() == 2 );
  2352. REQUIRE( (*cri) == Approx(6.5) );
  2353. cri = SpMat<double>::const_row_iterator(X, 0, 0);
  2354. // This should end up at (0, 0) with value 5.5.
  2355. REQUIRE( cri.row() == 0 );
  2356. REQUIRE( cri.col() == 0 );
  2357. REQUIRE( (*cri) == Approx(5.5) );
  2358. cri = SpMat<double>::const_row_iterator(X, 2, 1);
  2359. // This should end up at (2, 1) with value 7.5.
  2360. REQUIRE( cri.row() == 2 );
  2361. REQUIRE( cri.col() == 1 );
  2362. REQUIRE( (*cri) == Approx(7.5) );
  2363. }
  2364. // Check that sparse + scalar works.
  2365. TEST_CASE("spmat_scalar_add")
  2366. {
  2367. sp_mat m;
  2368. m.sprandu(100, 200, 0.1);
  2369. mat y = m + 3.0;
  2370. mat z = 3.0 + m;
  2371. for (uword i = 0; i < m.n_cols; ++i)
  2372. {
  2373. for (uword j = 0; j < m.n_rows; ++j)
  2374. {
  2375. REQUIRE(m(j, i) == Approx(z(j, i) - 3));
  2376. REQUIRE(m(j, i) == Approx(y(j, i) - 3));
  2377. }
  2378. }
  2379. }
  2380. // Check that sparse - scalar works.
  2381. TEST_CASE("spmat_scalar_minus")
  2382. {
  2383. sp_mat m;
  2384. m.sprandu(100, 200, 0.1);
  2385. mat y = m - 3.0;
  2386. mat z = 3.0 - m;
  2387. for (uword i = 0; i < m.n_cols; ++i)
  2388. {
  2389. for (uword j = 0; j < m.n_rows; ++j)
  2390. {
  2391. REQUIRE(m(j, i) == Approx(3 - z(j, i)));
  2392. REQUIRE(m(j, i) == Approx(y(j, i) + 3));
  2393. }
  2394. }
  2395. }
  2396. // Check that sparse / (sparse + eps) works. (and also for (sparse - eps) and (eps - sparse).
  2397. TEST_CASE("spmat_div_test")
  2398. {
  2399. sp_mat m;
  2400. m.sprandu(100, 200, 0.1);
  2401. sp_mat m2;
  2402. m2.sprandu(100, 200, 0.5); // higher probability of collision
  2403. sp_mat out = m / (m2 + 1.0);
  2404. sp_mat out2 = m / (m2 - 2.0);
  2405. sp_mat out3 = m / (2.0 - m2);
  2406. REQUIRE(out.n_rows == m.n_rows);
  2407. REQUIRE(out.n_cols == m.n_cols);
  2408. REQUIRE(out.n_nonzero == m.n_nonzero);
  2409. for (uword c = 0; c < m.n_cols; ++c)
  2410. {
  2411. for (uword r = 0; r < m.n_rows; ++r)
  2412. {
  2413. if (m(r, c) != 0.0)
  2414. {
  2415. REQUIRE((m(r, c) / (m2(r, c) + 1.0)) == Approx(out(r, c)));
  2416. REQUIRE((m(r, c) / (m2(r, c) - 2.0)) == Approx(out2(r, c)));
  2417. REQUIRE((m(r, c) / (2.0 - m2(r, c))) == Approx(out3(r, c)));
  2418. }
  2419. else
  2420. {
  2421. REQUIRE(out(r, c) == 0.0);
  2422. REQUIRE(out2(r, c) == 0.0);
  2423. REQUIRE(out3(r, c) == 0.0);
  2424. }
  2425. }
  2426. }
  2427. }
  2428. // Check that sparse % (sparse + eps) works. (and also for (sparse - eps) and (eps - sparse).
  2429. TEST_CASE("spmat_schur_test")
  2430. {
  2431. sp_mat m;
  2432. m.sprandu(100, 200, 0.1);
  2433. sp_mat m2;
  2434. m2.sprandu(100, 200, 0.5); // higher probability of collision
  2435. sp_mat out = m % (m2 + 1.0);
  2436. sp_mat out2 = m % (m2 - 2.0);
  2437. sp_mat out3 = m % (2.0 - m2);
  2438. REQUIRE(out.n_rows == m.n_rows);
  2439. REQUIRE(out.n_cols == m.n_cols);
  2440. REQUIRE(out.n_nonzero == m.n_nonzero);
  2441. for (uword c = 0; c < m.n_cols; ++c)
  2442. {
  2443. for (uword r = 0; r < m.n_rows; ++r)
  2444. {
  2445. if (m(r, c) != 0.0)
  2446. {
  2447. REQUIRE((m(r, c) * (m2(r, c) + 1.0)) == Approx(out(r, c)));
  2448. REQUIRE((m(r, c) * (m2(r, c) - 2.0)) == Approx(out2(r, c)));
  2449. REQUIRE((m(r, c) * (2.0 - m2(r, c))) == Approx(out3(r, c)));
  2450. }
  2451. else
  2452. {
  2453. REQUIRE(out(r, c) == 0.0);
  2454. REQUIRE(out2(r, c) == 0.0);
  2455. REQUIRE(out3(r, c) == 0.0);
  2456. }
  2457. }
  2458. }
  2459. }
  2460. // Make sure this compiles and works.
  2461. TEST_CASE("spmat_repeated_add_subtract")
  2462. {
  2463. sp_mat m;
  2464. m.sprandu(100, 200, 0.1);
  2465. // p: plus, m: minus, n: pre-minus
  2466. mat out_pp = m + 3 + 3;
  2467. mat out_pm = m + 3 - 3;
  2468. mat out_pn = 3 - (m + 3);
  2469. mat out_mp = m - 3 + 3;
  2470. mat out_mm = m - 3 - 3;
  2471. mat out_mn = 3 - (m - 3);
  2472. mat out_np = (3 - m) + 3;
  2473. mat out_nm = (3 - m) - 3;
  2474. mat out_nn = 3 - (3 - m);
  2475. for (uword c = 0; c < m.n_cols; ++c)
  2476. {
  2477. for (uword r = 0; r < m.n_rows; ++r)
  2478. {
  2479. REQUIRE(out_pp(r, c) == Approx(m(r, c) + 6));
  2480. REQUIRE(out_pm(r, c) == Approx(m(r, c)));
  2481. REQUIRE(out_pn(r, c) == Approx(-m(r, c)));
  2482. REQUIRE(out_mp(r, c) == Approx(m(r, c)));
  2483. REQUIRE(out_mm(r, c) == Approx(m(r, c) - 6));
  2484. REQUIRE(out_mn(r, c) == Approx(6 - m(r, c)));
  2485. REQUIRE(out_np(r, c) == Approx(6 - m(r, c)));
  2486. REQUIRE(out_nm(r, c) == Approx(-m(r, c)));
  2487. REQUIRE(out_nn(r, c) == Approx(m(r, c)));
  2488. }
  2489. }
  2490. }
  2491. // If we wrap an sp_mat() constructor around a (sparse + plus) it should force
  2492. // evaluate into a sparse matrix.
  2493. TEST_CASE("spmat_force_plus_minus_sparse")
  2494. {
  2495. // We can't test that our desired optimization is used but we can test that it
  2496. // compiles.
  2497. sp_mat m;
  2498. m.sprandu(100, 200, 0.1);
  2499. sp_mat out1(m + 1);
  2500. sp_mat out2(m - 1);
  2501. sp_mat out3(2 - m);
  2502. for (uword c = 0; c < m.n_cols; ++c)
  2503. {
  2504. for (uword r = 0; r < m.n_rows; ++r)
  2505. {
  2506. REQUIRE(out1(r, c) == Approx(m(r, c) + 1));
  2507. REQUIRE(out2(r, c) == Approx(m(r, c) - 1));
  2508. REQUIRE(out3(r, c) == Approx(2 - m(r, c)));
  2509. }
  2510. }
  2511. }
  2512. // Test elementwise max().
  2513. TEST_CASE("spmat_elementwise_max")
  2514. {
  2515. sp_mat m, n;
  2516. m.sprandu(100, 200, 0.1);
  2517. n.sprandu(100, 200, 0.2);
  2518. sp_mat out = max(m, n);
  2519. for (uword c = 0; c < m.n_cols; ++c)
  2520. {
  2521. for (uword r = 0; r < m.n_rows; ++r)
  2522. {
  2523. REQUIRE(out(r, c) == Approx(std::max((double) m(r, c), (double) n(r, c))));
  2524. }
  2525. }
  2526. }
  2527. // Test elementwise max() with a dense object.
  2528. TEST_CASE("spmat_mat_elementwise_max")
  2529. {
  2530. sp_mat m;
  2531. mat n;
  2532. m.sprandu(100, 200, 0.1);
  2533. n.randu(100, 200);
  2534. n -= 0.5;
  2535. mat out1 = max(m, n);
  2536. mat out2 = max(n, m);
  2537. for (uword c = 0; c < m.n_cols; ++c)
  2538. {
  2539. for (uword r = 0; r < m.n_rows; ++r)
  2540. {
  2541. REQUIRE(out1(r, c) == Approx(std::max((double) m(r, c), (double) n(r, c))));
  2542. REQUIRE(out2(r, c) == Approx(std::max((double) m(r, c), (double) n(r, c))));
  2543. }
  2544. }
  2545. }
  2546. // Test elementwise complex max().
  2547. TEST_CASE("spmat_elementwise_max_cx")
  2548. {
  2549. sp_cx_mat m, n;
  2550. m.sprandu(100, 200, 0.1);
  2551. n.sprandu(100, 200, 0.2);
  2552. sp_cx_mat out = arma::max(m, n);
  2553. for (uword c = 0; c < m.n_cols; ++c)
  2554. {
  2555. for (uword r = 0; r < m.n_rows; ++r)
  2556. {
  2557. if (std::abs(std::complex<double>(m(r, c))) > std::abs(std::complex<double>(n(r, c))))
  2558. REQUIRE(std::abs(std::complex<double>(out(r, c)) - std::complex<double>(m(r, c))) == Approx(0.0));
  2559. else
  2560. REQUIRE(std::abs(std::complex<double>(out(r, c)) - std::complex<double>(n(r, c))) == Approx(0.0));
  2561. }
  2562. }
  2563. }
  2564. // Test elementwise min().
  2565. TEST_CASE("spmat_elementwise_min")
  2566. {
  2567. sp_mat m, n;
  2568. m.sprandu(100, 200, 0.1);
  2569. n.sprandu(100, 200, 0.2);
  2570. sp_mat out = min(m, n);
  2571. for (uword c = 0; c < m.n_cols; ++c)
  2572. {
  2573. for (uword r = 0; r < m.n_rows; ++r)
  2574. {
  2575. REQUIRE(out(r, c) == Approx(std::min((double) m(r, c), (double) n(r, c))));
  2576. }
  2577. }
  2578. }
  2579. // Test elementwise min() with a dense object.
  2580. TEST_CASE("spmat_mat_elementwise_min")
  2581. {
  2582. sp_mat m;
  2583. mat n;
  2584. m.sprandu(100, 200, 0.1);
  2585. n.randu(100, 200);
  2586. n -= 0.5;
  2587. mat out1 = min(m, n);
  2588. mat out2 = min(n, m);
  2589. for (uword c = 0; c < m.n_cols; ++c)
  2590. {
  2591. for (uword r = 0; r < m.n_rows; ++r)
  2592. {
  2593. REQUIRE(out1(r, c) == Approx(std::min((double) m(r, c), (double) n(r, c))));
  2594. REQUIRE(out2(r, c) == Approx(std::min((double) m(r, c), (double) n(r, c))));
  2595. }
  2596. }
  2597. }
  2598. // Test elementwise complex min().
  2599. TEST_CASE("spmat_elementwise_min_cx")
  2600. {
  2601. sp_cx_mat m, n;
  2602. m.sprandu(100, 200, 0.1);
  2603. n.sprandu(100, 200, 0.2);
  2604. sp_cx_mat out = arma::min(m, n);
  2605. for (uword c = 0; c < m.n_cols; ++c)
  2606. {
  2607. for (uword r = 0; r < m.n_rows; ++r)
  2608. {
  2609. if (std::abs(std::complex<double>(m(r, c))) < std::abs(std::complex<double>(n(r, c))))
  2610. REQUIRE(std::abs(std::complex<double>(out(r, c)) - std::complex<double>(m(r, c))) == Approx(0.0));
  2611. else
  2612. REQUIRE(std::abs(std::complex<double>(out(r, c)) - std::complex<double>(n(r, c))) == Approx(0.0));
  2613. }
  2614. }
  2615. }
  2616. // Test vectorise() on a matrix.
  2617. TEST_CASE("spmat_vectorise_matrix")
  2618. {
  2619. sp_mat m;
  2620. m.sprandu(10, 10, 0.1);
  2621. sp_vec c = vectorise(m);
  2622. sp_mat d = vectorise(m);
  2623. sp_rowvec e = vectorise(m).t();
  2624. for (uword i = 0; i < c.n_elem; ++i)
  2625. {
  2626. REQUIRE(c[i] == Approx(m[i]));
  2627. REQUIRE(d[i] == Approx(m[i]));
  2628. REQUIRE(e[i] == Approx(m[i]));
  2629. }
  2630. }
  2631. // Test vectorise() as an alias.
  2632. TEST_CASE("spmat_vectorise_alias")
  2633. {
  2634. sp_mat m;
  2635. m.sprandu(10, 10, 0.1);
  2636. sp_mat n(m);
  2637. n = vectorise(n);
  2638. REQUIRE(n.n_rows == 100);
  2639. REQUIRE(n.n_cols == 1);
  2640. for (uword i = 0; i < n.n_elem; ++i)
  2641. {
  2642. REQUIRE(n[i] == Approx(m[i]));
  2643. }
  2644. }
  2645. // Test vectorise() with the dimension argument.
  2646. TEST_CASE("spmat_vectorise_dimension")
  2647. {
  2648. sp_mat m;
  2649. m.sprandu(10, 10, 0.1);
  2650. sp_mat n = m.t();
  2651. sp_vec c = vectorise(m, 0);
  2652. sp_rowvec d = vectorise(m, 1);
  2653. sp_rowvec e = vectorise(m.t(), 1);
  2654. sp_vec f = vectorise(m.t(), 0);
  2655. for (uword i = 0; i < m.n_elem; ++i)
  2656. {
  2657. REQUIRE(c[i] == Approx(m[i]));
  2658. REQUIRE(d[i] == Approx(n[i]));
  2659. REQUIRE(e[i] == Approx(m[i]));
  2660. REQUIRE(f[i] == Approx(n[i]));
  2661. }
  2662. }
  2663. // Test vectorise() with an alias and a dimension argument.
  2664. TEST_CASE("spmat_vectorise_dimension_alias")
  2665. {
  2666. sp_mat m;
  2667. m.sprandu(10, 10, 0.1);
  2668. sp_mat n(m);
  2669. m = arma::vectorise(m, 0);
  2670. REQUIRE(m.n_rows == 100);
  2671. REQUIRE(m.n_cols == 1);
  2672. for (uword i = 0; i < m.n_elem; ++i)
  2673. {
  2674. REQUIRE(m[i] == Approx(n[i]));
  2675. }
  2676. m.sprandu(10, 10, 0.1);
  2677. n = m.t();
  2678. m = arma::vectorise(m, 1);
  2679. REQUIRE(m.n_rows == 1);
  2680. REQUIRE(m.n_cols == 100);
  2681. for (uword i = 0; i < m.n_elem; ++i)
  2682. {
  2683. REQUIRE(m[i] == Approx(n[i]));
  2684. }
  2685. }