123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231 |
- // Copyright 2011-2017 Ryan Curtin (http://www.ratml.org/)
- // Copyright 2017 National ICT Australia (NICTA)
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // ------------------------------------------------------------------------
- #include <armadillo>
- #include "catch.hpp"
- using namespace arma;
- // Does the matrix correctly report when it is empty?
- TEST_CASE("empty_test")
- {
- bool testPassed = true;
- sp_imat test;
- REQUIRE( test.is_empty() );
- test.set_size(3, 4);
- REQUIRE( test.is_empty() == false );
- }
- // Can we insert items into the matrix correctly?
- TEST_CASE("insertion_test")
- {
- int correctResult[3][4] =
- {{1, 0, 0, 0},
- {2, 3, 1, 0},
- {0, 9, 4, 0}};
- // Now run the same test for the Armadillo sparse matrix.
- SpMat<int> arma_test;
- arma_test.set_size(3, 4);
- // Fill the matrix (hopefully).
- arma_test(0, 0) = 1;
- arma_test(1, 0) = 2;
- arma_test(1, 1) = 3;
- arma_test(2, 1) = 9;
- arma_test(1, 2) = 1;
- arma_test(2, 2) = 4;
- for (uword i = 0; i < 3; i++)
- {
- for (uword j = 0; j < 4; j++)
- {
- REQUIRE( (int) arma_test(i, j) == correctResult[i][j] );
- }
- }
- }
- // Does sparse-sparse matrix multiplication work?
- TEST_CASE("full_sparse_sparse_matrix_multiplication_test")
- {
- // Now perform the test again for SpMat.
- SpMat<int> spa(3, 3);
- SpMat<int> spb(3, 2);
- int correctResult[3][2] =
- {{ 46, 60},
- { 40, 52},
- {121, 160}};
- spa(0, 0) = 1;
- spa(0, 1) = 10;
- spa(0, 2) = 3;
- spa(1, 0) = 3;
- spa(1, 1) = 4;
- spa(1, 2) = 5;
- spa(2, 0) = 12;
- spa(2, 1) = 13;
- spa(2, 2) = 14;
- spb(0, 0) = 1;
- spb(0, 1) = 2;
- spb(1, 0) = 3;
- spb(1, 1) = 4;
- spb(2, 0) = 5;
- spb(2, 1) = 6;
- spa *= spb;
- REQUIRE( spa.n_rows == 3 );
- REQUIRE( spa.n_cols == 2 );
- for (uword i = 0; i < 3; i++)
- {
- for (uword j = 0; j < 2; j++)
- {
- REQUIRE( (int) spa(i, j) == correctResult[i][j] );
- }
- }
- }
- TEST_CASE("sparse_sparse_matrix_multiplication_test")
- {
- SpMat<double> spaa(10, 10);
- spaa(1, 5) = 0.4;
- spaa(0, 4) = 0.3;
- spaa(0, 8) = 1.2;
- spaa(3, 0) = 1.1;
- spaa(3, 1) = 1.1;
- spaa(3, 2) = 1.1;
- spaa(4, 4) = 0.2;
- spaa(4, 9) = 0.1;
- spaa(6, 2) = 4.1;
- spaa(6, 8) = 4.1;
- spaa(7, 5) = 1.0;
- spaa(8, 9) = 0.4;
- spaa(9, 4) = 0.4;
- double correctResultB[10][10] =
- {{ 0.00, 0.00, 0.00, 0.00, 0.06, 0.00, 0.00, 0.00, 0.00, 0.51 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.33, 0.44, 0.00, 0.00, 1.32, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.08, 0.00, 0.00, 0.00, 0.00, 0.02 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.64 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.16, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.08, 0.00, 0.00, 0.00, 0.00, 0.04 }};
- spaa *= spaa;
- for (uword i = 0; i < 10; i++)
- {
- for (uword j = 0; j < 10; j++)
- {
- REQUIRE( (double) spaa(i, j) == Approx(correctResultB[i][j]) );
- }
- }
- }
- TEST_CASE("hadamard_product_test")
- {
- SpMat<int> a(4, 4), b(4, 4);
- a(1, 1) = 1;
- a(2, 1) = 1;
- a(3, 3) = 1;
- a(3, 0) = 1;
- a(0, 2) = 1;
- b(1, 1) = 1;
- b(2, 2) = 1;
- b(3, 3) = 1;
- b(3, 0) = 1;
- b(0, 3) = 1;
- b(3, 1) = 1;
- double correctResult[4][4] =
- {{ 0, 0, 0, 0 },
- { 0, 1, 0, 0 },
- { 0, 0, 0, 0 },
- { 1, 0, 0, 1 }};
- a %= b;
- for (uword i = 0; i < 4; i++)
- {
- for (uword j = 0; j < 4; j++)
- {
- REQUIRE( a(i, j) == correctResult[i][j] );
- }
- }
- SpMat<double> c, d;
- c.sprandu(30, 25, 0.1);
- d.sprandu(30, 25, 0.1);
- mat e, f;
- e = c;
- f = d;
- c %= d;
- e %= f;
- for (uword i = 0; i < 25; ++i)
- {
- for(uword j = 0; j < 30; ++j)
- {
- REQUIRE( (double) c(j, i) == Approx(e(j, i)) );
- }
- }
- }
- TEST_CASE("division_test")
- {
- SpMat<double> a(2, 2), b(2, 2);
- a(0, 1) = 0.5;
- b(0, 1) = 1.0;
- b(1, 0) = 5.0;
- a /= b;
- REQUIRE( std::isnan((double) a(0, 0)) );
- REQUIRE( (double) a(0, 1) == Approx(0.5) );
- REQUIRE( (double) a(1, 0) == Approx(1e-5) );
- REQUIRE( std::isnan((double) a(1, 1)) );
- }
- TEST_CASE("insert_delete_test")
- {
- SpMat<double> sp;
- sp.set_size(10, 10);
- // Ensure everything is empty.
- for (uword i = 0; i < 100; i++)
- {
- REQUIRE( sp(i) == 0.0 );
- }
- // Add an element.
- sp(5, 5) = 43.234;
- REQUIRE( sp.n_nonzero == 1 );
- REQUIRE( (double) sp(5, 5) == Approx(43.234) );
- // Remove the element.
- sp(5, 5) = 0.0;
- REQUIRE( sp.n_nonzero == 0 );
- }
- TEST_CASE("value_operator_test")
- {
- // Test operators that work with a single value.
- // =(double), /=(double), *=(double)
- SpMat<double> sp(3, 4);
- double correctResult[3][4] = {{1.5, 0.0, 0.0, 0.0},
- {2.1, 3.2, 0.9, 0.0},
- {0.0, 9.3, 4.0, -1.5}};
- sp(0, 0) = 1.5;
- sp(1, 0) = 2.1;
- sp(1, 1) = 3.2;
- sp(1, 2) = 0.9;
- sp(2, 1) = 9.3;
- sp(2, 2) = 4.0;
- sp(2, 3) = -1.5;
- // operator=(double)
- SpMat<double> work = sp;
- work = 5.0;
- REQUIRE( work.n_nonzero == 1 );
- REQUIRE( work.n_elem == 1 );
- REQUIRE( (double) work(0) == Approx(5.0) );
- // operator*=(double)
- work = sp;
- work *= 2;
- REQUIRE( work.n_nonzero == 7 );
- for (uword i = 0; i < 3; i++)
- {
- for (uword j = 0; j < 4; j++)
- {
- REQUIRE((double) work(i, j) == Approx(correctResult[i][j] * 2.0) );
- }
- }
- // operator/=(double)
- work = sp;
- work /= 5.5;
- REQUIRE( work.n_nonzero == 7 );
- for (uword i = 0; i < 3; i++)
- {
- for (uword j = 0; j < 4; j++)
- {
- REQUIRE((double) work(i, j) == Approx(correctResult[i][j] / 5.5) );
- }
- }
- }
- TEST_CASE("iterator_test")
- {
- SpMat<double> x(5, 5);
- x(4, 1) = 3.1;
- x(1, 2) = 4.2;
- x(1, 3) = 3.3;
- x(1, 3) = 5.5; // overwrite
- x(2, 3) = 4.5;
- x(4, 4) = 6.4;
- SpMat<double>::iterator it = x.begin();
- REQUIRE( (double) *it == Approx(3.1) );
- REQUIRE( it.row() == 4 );
- REQUIRE( it.col() == 1 );
- ++it;
- REQUIRE( (double) *it == Approx(4.2) );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 2 );
- ++it;
- REQUIRE( (double) *it == Approx(5.5) );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 3 );
- ++it;
- REQUIRE( (double) *it == Approx(4.5) );
- REQUIRE( it.row() == 2 );
- REQUIRE( it.col() == 3 );
- ++it;
- REQUIRE( (double) *it == Approx(6.4) );
- REQUIRE( it.row() == 4 );
- REQUIRE( it.col() == 4 );
- ++it;
- REQUIRE( it == x.end() );
- // Now let's go backwards.
- --it; // Get it off the end.
- REQUIRE( (double) *it == Approx(6.4) );
- REQUIRE( it.row() == 4 );
- REQUIRE( it.col() == 4 );
- --it;
- REQUIRE( (double) *it == Approx(4.5) );
- REQUIRE( it.row() == 2 );
- REQUIRE( it.col() == 3 );
- --it;
- REQUIRE( (double) *it == Approx(5.5) );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 3 );
- --it;
- REQUIRE( (double) *it == Approx(4.2) );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 2 );
- --it;
- REQUIRE( (double) *it == Approx(3.1) );
- REQUIRE( it.row() == 4 );
- REQUIRE( it.col() == 1 );
- REQUIRE( it == x.begin() );
- // Try removing an element we iterated to.
- ++it;
- ++it;
- *it = 0;
- REQUIRE( x.n_nonzero == 4 );
- }
- TEST_CASE("row_iterator_test")
- {
- SpMat<double> x(5, 5);
- x(4, 1) = 3.1;
- x(1, 2) = 4.2;
- x(1, 3) = 3.3;
- x(1, 3) = 5.5; // overwrite
- x(2, 3) = 4.5;
- x(4, 4) = 6.4;
- SpMat<double>::row_iterator it = x.begin_row();
- REQUIRE( (double) *it == Approx(4.2) );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 2 );
- ++it;
- REQUIRE( (double) *it == Approx(5.5) );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 3 );
- ++it;
- REQUIRE( (double) *it == Approx(4.5) );
- REQUIRE( it.row() == 2 );
- REQUIRE( it.col() == 3 );
- ++it;
- REQUIRE( (double) *it == Approx(3.1) );
- REQUIRE( it.row() == 4 );
- REQUIRE( it.col() == 1 );
- ++it;
- REQUIRE( (double) *it == Approx(6.4) );
- REQUIRE( it.row() == 4 );
- REQUIRE( it.col() == 4 );
- ++it;
- // REQUIRE( it == x.end_row() );
- // Now let's go backwards.
- --it; // Get it off the end.
- REQUIRE( (double) *it == Approx(6.4) );
- REQUIRE( it.row() == 4 );
- REQUIRE( it.col() == 4 );
- --it;
- REQUIRE( (double) *it == Approx(3.1) );
- REQUIRE( it.row() == 4 );
- REQUIRE( it.col() == 1 );
- --it;
- REQUIRE( (double) *it == Approx(4.5) );
- REQUIRE( it.row() == 2 );
- REQUIRE( it.col() == 3 );
- --it;
- REQUIRE( (double) *it == Approx(5.5) );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 3 );
- --it;
- REQUIRE( (double) *it == Approx(4.2) );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 2 );
- REQUIRE( it == x.begin_row() );
- // Try removing an element we iterated to.
- ++it;
- ++it;
- *it = 0;
- REQUIRE( x.n_nonzero == 4 );
- }
- TEST_CASE("basic_sp_mat_operator_test")
- {
- // +=, -=, *=, /=, %=
- SpMat<double> a(6, 5);
- a(0, 0) = 3.4;
- a(4, 1) = 4.1;
- a(5, 1) = 1.5;
- a(3, 2) = 2.6;
- a(4, 2) = 3.0;
- a(1, 3) = 9.8;
- a(4, 3) = 0.1;
- a(2, 4) = 0.2;
- a(3, 4) = 0.2;
- a(4, 4) = 0.2;
- a(5, 4) = 8.3;
- SpMat<double> b(6, 5);
- b(0, 0) = 3.4;
- b(3, 0) = 0.4;
- b(3, 1) = 0.5;
- b(4, 1) = 1.2;
- b(4, 2) = 3.0;
- b(5, 2) = 1.1;
- b(1, 3) = 0.6;
- b(3, 3) = 1.0;
- b(4, 4) = 7.3;
- b(5, 4) = 7.4;
- double addResult[6][5] = {{6.8 , 0 , 0 , 0 , 0 },
- {0 , 0 , 0 , 10.4, 0 },
- {0 , 0 , 0 , 0 , 0.2 },
- {0.4 , 0.5 , 2.6 , 1.0 , 0.2 },
- {0 , 5.3 , 6.0 , 0.1 , 7.5 },
- {0 , 1.5 , 1.1 , 0 , 15.7}};
- double subResult[6][5] = {{0 , 0 , 0 , 0 , 0 },
- {0 , 0 , 0 , 9.2 , 0 },
- {0 , 0 , 0 , 0 , 0.2 },
- {-0.4, -0.5, 2.6 , -1.0, 0.2 },
- {0 , 2.9 , 0 , 0.1 , -7.1},
- {0 , 1.5 , -1.1, 0 , 0.9 }};
- SpMat<double> out = a;
- out += b;
- REQUIRE( out.n_nonzero == 15 );
- for (uword r = 0; r < 6; r++)
- {
- for (uword c = 0; c < 5; c++)
- {
- REQUIRE( (double) out(r, c) == Approx(addResult[r][c]) );
- }
- }
- out = a;
- out -= b;
- REQUIRE( out.n_nonzero == 13 );
- for (uword r = 0; r < 6; r++)
- {
- for (uword c = 0; c < 5; c++)
- {
- REQUIRE( (double) out(r, c) == Approx(subResult[r][c]) );
- }
- }
- }
- TEST_CASE("min_max_test")
- {
- SpMat<double> a(6, 5);
- a(0, 0) = 3.4;
- a(4, 1) = 4.1;
- a(5, 1) = 1.5;
- a(3, 2) = 2.6;
- a(4, 2) = 3.0;
- a(1, 3) = 9.8;
- a(4, 3) = 0.1;
- a(2, 4) = 0.2;
- a(3, 4) = -0.2;
- a(4, 4) = 0.2;
- a(5, 4) = 8.3;
- uword index, row, col;
- REQUIRE( a.min() == Approx(-0.2) );
- REQUIRE( a.min(index) == Approx(-0.2) );
- REQUIRE( index == 27 );
- REQUIRE( a.min(row, col) == Approx(-0.2) );
- REQUIRE( row == 3 );
- REQUIRE( col == 4 );
- REQUIRE( a.max() == Approx(9.8) );
- REQUIRE( a.max(index) == Approx(9.8) );
- REQUIRE( index == 19 );
- REQUIRE( a.max(row, col) == Approx(9.8) );
- REQUIRE( row == 1 );
- REQUIRE( col == 3 );
- }
- TEST_CASE("swap_row_test")
- {
- SpMat<double> a(6, 5);
- a(0, 0) = 3.4;
- a(4, 1) = 4.1;
- a(5, 1) = 1.5;
- a(3, 2) = 2.6;
- a(4, 2) = 3.0;
- a(1, 3) = 9.8;
- a(4, 3) = 0.1;
- a(2, 4) = 0.2;
- a(3, 4) = -0.2;
- a(4, 4) = 0.2;
- a(5, 4) = 8.3;
- /**
- * [[3.4 0.0 0.0 0.0 0.0]
- * [0.0 0.0 0.0 9.8 0.0]
- * [0.0 0.0 0.0 0.0 0.2]
- * [0.0 0.0 2.6 0.0 -0.2]
- * [0.0 4.1 3.0 0.1 0.2]
- * [0.0 1.5 0.0 0.0 8.3]]
- */
- double swapOne[6][5] =
- {{ 0.0, 0.0, 2.6, 0.0, -0.2},
- { 0.0, 0.0, 0.0, 9.8, 0.0},
- { 0.0, 0.0, 0.0, 0.0, 0.2},
- { 3.4, 0.0, 0.0, 0.0, 0.0},
- { 0.0, 4.1, 3.0, 0.1, 0.2},
- { 0.0, 1.5, 0.0, 0.0, 8.3}};
- double swapTwo[6][5] =
- {{ 0.0, 0.0, 2.6, 0.0, -0.2},
- { 0.0, 0.0, 0.0, 9.8, 0.0},
- { 0.0, 0.0, 0.0, 0.0, 0.2},
- { 3.4, 0.0, 0.0, 0.0, 0.0},
- { 0.0, 1.5, 0.0, 0.0, 8.3},
- { 0.0, 4.1, 3.0, 0.1, 0.2}};
- a.swap_rows(0, 3);
- for (uword row = 0; row < a.n_rows; row++)
- {
- for (uword col = 0; col < a.n_cols; col++)
- {
- REQUIRE( (double) a(row, col) == Approx(swapOne[row][col]) );
- }
- }
- a.swap_rows(4, 5);
- for (uword row = 0; row < a.n_rows; row++)
- {
- for (uword col = 0; col < a.n_cols; col++)
- {
- REQUIRE( (double) a(row, col) == Approx(swapTwo[row][col]) );
- }
- }
- }
- TEST_CASE("swap_col_test")
- {
- SpMat<double> a(6, 5);
- a(0, 0) = 3.4;
- a(4, 1) = 4.1;
- a(5, 1) = 1.5;
- a(3, 2) = 2.6;
- a(4, 2) = 3.0;
- a(1, 3) = 9.8;
- a(4, 3) = 0.1;
- a(2, 4) = 0.2;
- a(3, 4) = -0.2;
- a(4, 4) = 0.2;
- a(5, 4) = 8.3;
- mat b(6, 5);
- b.zeros(6, 5);
- b(0, 0) = 3.4;
- b(4, 1) = 4.1;
- b(5, 1) = 1.5;
- b(3, 2) = 2.6;
- b(4, 2) = 3.0;
- b(1, 3) = 9.8;
- b(4, 3) = 0.1;
- b(2, 4) = 0.2;
- b(3, 4) = -0.2;
- b(4, 4) = 0.2;
- b(5, 4) = 8.3;
- /**
- * [[3.4 0.0 0.0 0.0 0.0]
- * [0.0 0.0 0.0 9.8 0.0]
- * [0.0 0.0 0.0 0.0 0.2]
- * [0.0 0.0 2.6 0.0 -0.2]
- * [0.0 4.1 3.0 0.1 0.2]
- * [0.0 1.5 0.0 0.0 8.3]]
- */
- a.swap_cols(2, 3);
- b.swap_cols(2, 3);
- for (uword row = 0; row < a.n_rows; row++)
- {
- for (uword col = 0; col < a.n_cols; col++)
- {
- REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
- }
- }
- a.swap_cols(0, 4);
- b.swap_cols(0, 4);
- for (uword row = 0; row < a.n_rows; row++)
- {
- for (uword col = 0; col < a.n_cols; col++)
- {
- REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
- }
- }
- a.swap_cols(1, 4);
- b.swap_cols(1, 4);
- for (uword row = 0; row < a.n_rows; row++)
- {
- for (uword col = 0; col < a.n_cols; col++)
- {
- REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
- }
- }
- }
- TEST_CASE("shed_col_test")
- {
- SpMat<int> a(2, 2);
- a(0, 0) = 1;
- a(1, 1) = 1;
- /**
- * [[1 0]
- * [0 1]]
- *
- * becomes
- *
- * [[0]
- * [1]]
- */
- a.shed_col(0);
- REQUIRE( a.n_cols == 1 );
- REQUIRE( a.n_rows == 2 );
- REQUIRE( a.n_elem == 2 );
- REQUIRE( a.n_nonzero == 1 );
- REQUIRE( a(0, 0) == 0 );
- REQUIRE( a(1, 0) == 1 );
- }
- TEST_CASE("shed_cols_test")
- {
- SpMat<int> a(3, 3);
- a(0, 0) = 1;
- a(1, 1) = 1;
- a(2, 2) = 1;
- SpMat<int> b(3, 3);
- b(0, 0) = 1;
- b(1, 1) = 1;
- b(2, 2) = 1;
- SpMat<int> c(3, 3);
- c(0, 0) = 1;
- c(1, 1) = 1;
- c(2, 2) = 1;
- /**
- * [[1 0 0]
- * [0 1 0]
- * [0 0 1]]
- *
- * becomes
- *
- * [[0]
- * [0]
- * [1]]
- */
- a.shed_cols(0, 1);
- REQUIRE( a.n_cols == 1 );
- REQUIRE( a.n_rows == 3 );
- REQUIRE( a.n_elem == 3 );
- REQUIRE( a.n_nonzero == 1 );
- REQUIRE( a(0, 0) == 0 );
- REQUIRE( a(1, 0) == 0 );
- REQUIRE( a(2, 0) == 1 );
- b.shed_cols(1, 2);
- REQUIRE( b.n_cols == 1 );
- REQUIRE( b.n_rows == 3 );
- REQUIRE( b.n_elem == 3 );
- REQUIRE( b.n_nonzero == 1 );
- REQUIRE( b(0, 0) == 1 );
- REQUIRE( b(1, 0) == 0 );
- REQUIRE( b(2, 0) == 0 );
- c.shed_cols(0, 0);
- c.shed_cols(1, 1);
- REQUIRE( c.n_cols == 1 );
- REQUIRE( c.n_rows == 3 );
- REQUIRE( c.n_elem == 3 );
- REQUIRE( c.n_nonzero == 1 );
- REQUIRE( c(0, 0) == 0 );
- REQUIRE( c(1, 0) == 1 );
- REQUIRE( c(2, 0) == 0 );
- }
- TEST_CASE("shed_row_test")
- {
- SpMat<int> a(3, 3);
- a(0, 0) = 1;
- a(1, 1) = 1;
- a(2, 2) = 1;
- Mat<int> b(3, 3);
- b.zeros(3, 3);
- b(0, 0) = 1;
- b(1, 1) = 1;
- b(2, 2) = 1;
- /**
- * [[1 0 0]
- * [0 1 0]
- * [0 0 1]]
- *
- * becomes
- *
- * [[1 0 0]
- * [0 1 0]]
- */
- a.shed_row(2);
- b.shed_row(2);
- REQUIRE( a.n_cols == 3 );
- REQUIRE( a.n_rows == 2 );
- REQUIRE( a.n_elem == 6 );
- REQUIRE( a.n_nonzero == 2 );
- for (uword row = 0; row < a.n_rows; row++)
- {
- for (uword col = 0; col < a.n_cols; col++)
- {
- REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
- }
- }
- }
- TEST_CASE("shed_rows_test")
- {
- SpMat<int> a(5, 5);
- a(0, 0) = 1;
- a(1, 1) = 1;
- a(2, 2) = 1;
- a(3, 3) = 1;
- a(4, 4) = 1;
- Mat<int> b(5, 5);
- b.zeros(5, 5);
- b(0, 0) = 1;
- b(1, 1) = 1;
- b(2, 2) = 1;
- b(3, 3) = 1;
- b(4, 4) = 1;
- SpMat<int> c = a;
- Mat<int> d = b;
- /**
- * [[1 0 0 0 0]
- * [0 1 0 0 0]
- * [0 0 1 0 0]
- * [0 0 0 1 0]
- * [0 0 0 0 1]]
- *
- * becomes
- *
- * [[1 0 0 0 0]
- * [0 1 0 0 0]]
- */
- a.shed_rows(2,4);
- b.shed_rows(2,4);
- REQUIRE( a.n_cols == 5 );
- REQUIRE( a.n_rows == 2 );
- REQUIRE( a.n_elem == 10 );
- REQUIRE( a.n_nonzero == 2 );
- for (uword row = 0; row < a.n_rows; row++)
- {
- for (uword col = 0; col < a.n_cols; col++)
- {
- REQUIRE( (double) a(row, col) == Approx(b(row, col)) );
- }
- }
- c.shed_rows(0, 2);
- d.shed_rows(0, 2);
- REQUIRE( c.n_cols == 5 );
- REQUIRE( c.n_rows == 2 );
- REQUIRE( c.n_elem == 10 );
- REQUIRE( c.n_nonzero == 2 );
- for (uword row = 0; row < c.n_rows; ++row)
- {
- for (uword col = 0; col < c.n_cols; ++col)
- {
- REQUIRE( (int) c(row, col) == d(row, col) );
- }
- }
- }
- TEST_CASE("sp_mat_reshape_columnwise_test")
- {
- // Input matrix:
- // [[0 2 0]
- // [1 3 0]
- // [0 0 5]
- // [0 4 6]]
- //
- // Output matrix:
- // [[0 0 0 0]
- // [1 2 4 5]
- // [0 3 0 6]]
- SpMat<unsigned int> ref(4, 3);
- ref(1, 0) = 1;
- ref(0, 1) = 2;
- ref(1, 1) = 3;
- ref(3, 1) = 4;
- ref(2, 2) = 5;
- ref(3, 2) = 6;
- // Now reshape.
- ref.reshape(3, 4);
- // Check everything.
- REQUIRE( ref.n_cols == 4 );
- REQUIRE( ref.n_rows == 3 );
- REQUIRE( (unsigned int) ref(0, 0) == 0 );
- REQUIRE( (unsigned int) ref(1, 0) == 1 );
- REQUIRE( (unsigned int) ref(2, 0) == 0 );
- REQUIRE( (unsigned int) ref(0, 1) == 0 );
- REQUIRE( (unsigned int) ref(1, 1) == 2 );
- REQUIRE( (unsigned int) ref(2, 1) == 3 );
- REQUIRE( (unsigned int) ref(0, 2) == 0 );
- REQUIRE( (unsigned int) ref(1, 2) == 4 );
- REQUIRE( (unsigned int) ref(2, 2) == 0 );
- REQUIRE( (unsigned int) ref(0, 3) == 0 );
- REQUIRE( (unsigned int) ref(1, 3) == 5 );
- REQUIRE( (unsigned int) ref(2, 3) == 6 );
- }
- // TEST_CASE("sp_mat_reshape_rowwise_test")
- // {
- // // Input matrix:
- // // [[0 2 0]
- // // [1 3 0]
- // // [0 0 5]
- // // [0 4 6]]
- // //
- // // Output matrix:
- // // [[0 2 0 1]
- // // [3 0 0 0]
- // // [5 0 4 6]]
- // SpMat<unsigned int> ref(4, 3);
- // ref(1, 0) = 1;
- // ref(0, 1) = 2;
- // ref(1, 1) = 3;
- // ref(3, 1) = 4;
- // ref(2, 2) = 5;
- // ref(3, 2) = 6;
- //
- // // Now reshape.
- // ref.reshape(3, 4, 1 /* row-wise */);
- //
- // // Check everything.
- // REQUIRE( ref.n_cols == 4 );
- // REQUIRE( ref.n_rows == 3 );
- //
- // REQUIRE( (unsigned int) ref(0, 0) == 0 );
- // REQUIRE( (unsigned int) ref(1, 0) == 3 );
- // REQUIRE( (unsigned int) ref(2, 0) == 5 );
- // REQUIRE( (unsigned int) ref(0, 1) == 2 );
- // REQUIRE( (unsigned int) ref(1, 1) == 0 );
- // REQUIRE( (unsigned int) ref(2, 1) == 0 );
- // REQUIRE( (unsigned int) ref(0, 2) == 0 );
- // REQUIRE( (unsigned int) ref(1, 2) == 0 );
- // REQUIRE( (unsigned int) ref(2, 2) == 4 );
- // REQUIRE( (unsigned int) ref(0, 3) == 1 );
- // REQUIRE( (unsigned int) ref(1, 3) == 0 );
- // REQUIRE( (unsigned int) ref(2, 3) == 6 );
- // }
- TEST_CASE("sp_mat_zeros_tests")
- {
- SpMat<double> m(4, 3);
- m(1, 0) = 1;
- m(0, 1) = 2;
- m(1, 1) = 3;
- m(3, 1) = 4;
- m(2, 2) = 5;
- m(3, 2) = 6;
- // Now zero it out.
- SpMat<double> d = m;
- d.zeros();
- REQUIRE( d.values[0] == 0 );
- REQUIRE( d.row_indices[0] == 0);
- REQUIRE( d.col_ptrs[0] == 0 );
- REQUIRE( d.col_ptrs[1] == 0 );
- REQUIRE( d.col_ptrs[2] == 0 );
- REQUIRE( d.col_ptrs[3] == 0 );
- REQUIRE( d.n_cols == 3 );
- REQUIRE( d.n_rows == 4 );
- REQUIRE( d.n_elem == 12 );
- REQUIRE( d.n_nonzero == 0 );
- // Now zero it out again.
- d = m;
- d.zeros(10);
- REQUIRE( d.values[0] == 0 );
- REQUIRE( d.row_indices[0] == 0);
- REQUIRE( d.col_ptrs[0] == 0 );
- REQUIRE( d.col_ptrs[1] == 0 );
- REQUIRE( d.n_cols == 1 );
- REQUIRE( d.n_rows == 10 );
- REQUIRE( d.n_elem == 10 );
- REQUIRE( d.n_nonzero == 0 );
- // Now zero it out again.
- d = m;
- d.zeros(5, 5);
- REQUIRE( d.values[0] == 0 );
- REQUIRE( d.row_indices[0] == 0);
- REQUIRE( d.col_ptrs[0] == 0 );
- REQUIRE( d.col_ptrs[1] == 0 );
- REQUIRE( d.col_ptrs[2] == 0 );
- REQUIRE( d.col_ptrs[3] == 0 );
- REQUIRE( d.col_ptrs[4] == 0 );
- REQUIRE( d.col_ptrs[5] == 0 );
- REQUIRE( d.n_cols == 5 );
- REQUIRE( d.n_rows == 5 );
- REQUIRE( d.n_elem == 25 );
- REQUIRE( d.n_nonzero == 0 );
- }
- /**
- * Check that eye() works.
- */
- TEST_CASE("sp_mat_eye_test")
- {
- SpMat<double> e = eye<SpMat<double> >(5, 5);
- REQUIRE( e.n_elem == 25 );
- REQUIRE( e.n_rows == 5 );
- REQUIRE( e.n_cols == 5 );
- REQUIRE( e.n_nonzero == 5 );
- for (uword i = 0; i < 5; i++)
- {
- for (uword j = 0; j < 5; j++)
- {
- if (i == j)
- REQUIRE( (double) e(i, j) == Approx(1.0) );
- else
- REQUIRE( (double) e(i, j) == Approx(1e-5) );
- }
- }
- // Just check that these compile and run.
- e = eye<SpMat<double> >(5, 5);
- e *= eye<SpMat<double> >(5, 5);
- e %= eye<SpMat<double> >(5, 5);
- e /= eye<SpMat<double> >(5, 5);
- }
- /**
- * Check that pow works.
- *
- TEST_CASE("sp_mat_pow_test")
- {
- SpMat<double> a(3, 3);
- a(0, 2) = 4.3;
- a(1, 1) = -5.5;
- a(2, 2) = -6.3;
- a += pow(a, 2);
- REQUIRE( (double) a(0, 0) == 0 );
- REQUIRE( (double) a(1, 0) == 0 );
- REQUIRE( (double) a(2, 0) == 0 );
- REQUIRE( (double) a(0, 1) == 0 );
- REQUIRE( (double) a(1, 1) == Approx(24.75) );
- REQUIRE( (double) a(2, 1) == 0 );
- REQUIRE( (double) a(0, 2) == Approx(22.79) );
- REQUIRE( (double) a(1, 2) == 0 );
- REQUIRE( (double) a(2, 2) == Approx(33.39) );
- a = pow(a, 2);
- a *= pow(a, 2);
- a %= pow(a, 2);
- a /= pow(a, 2);
- }
- */
- // I hate myself.
- #undef TEST_OPERATOR
- #define TEST_OPERATOR(EOP_TEST, EOP) \
- TEST_CASE(EOP_TEST) \
- {\
- SpMat<double> a(3, 3);\
- a(0, 2) = 4.3;\
- a(1, 1) = -5.5;\
- a(2, 2) = -6.3;\
- a(1, 0) = 0.001;\
- Mat<double> b(3, 3);\
- b.zeros();\
- b(0, 2) = 4.3;\
- b(1, 1) = -5.5;\
- b(2, 2) = -6.3;\
- b(1, 0) = 0.001;\
- \
- SpMat<double> c = EOP(a);\
- Mat<double> d = EOP(b);\
- \
- if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
- REQUIRE( c(0, 0) == d(0, 0) );\
- if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
- REQUIRE( c(1, 0) == d(1, 0) );\
- if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
- REQUIRE( c(2, 0) == d(2, 0) );\
- if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
- REQUIRE( c(0, 1) == d(0, 1) );\
- if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
- REQUIRE( c(1, 1) == d(1, 1) );\
- if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
- REQUIRE( c(2, 1) == d(2, 1) );\
- if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
- REQUIRE( c(0, 2) == d(0, 2) );\
- if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
- REQUIRE( c(1, 2) == d(1, 2) );\
- if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
- REQUIRE( c(2, 2) == d(2, 2) );\
- \
- c -= EOP(a);\
- d -= EOP(b);\
- \
- if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
- REQUIRE( c(0, 0) == d(0, 0) );\
- if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
- REQUIRE( c(1, 0) == d(1, 0) );\
- if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
- REQUIRE( c(2, 0) == d(2, 0) );\
- if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
- REQUIRE( c(0, 1) == d(0, 1) );\
- if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
- REQUIRE( c(1, 1) == d(1, 1) );\
- if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
- REQUIRE( c(2, 1) == d(2, 1) );\
- if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
- REQUIRE( c(0, 2) == d(0, 2) );\
- if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
- REQUIRE( c(1, 2) == d(1, 2) );\
- if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
- REQUIRE( c(2, 2) == d(2, 2) );\
- \
- c %= EOP(a);\
- d %= EOP(b);\
- \
- if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
- REQUIRE( c(0, 0) == d(0, 0) );\
- if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
- REQUIRE( c(1, 0) == d(1, 0) );\
- if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
- REQUIRE( c(2, 0) == d(2, 0) );\
- if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
- REQUIRE( c(0, 1) == d(0, 1) );\
- if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
- REQUIRE( c(1, 1) == d(1, 1) );\
- if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
- REQUIRE( c(2, 1) == d(2, 1) );\
- if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
- REQUIRE( c(0, 2) == d(0, 2) );\
- if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
- REQUIRE( c(1, 2) == d(1, 2) );\
- if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
- REQUIRE( c(2, 2) == d(2, 2) );\
- \
- c *= EOP(a);\
- d *= EOP(b);\
- \
- if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
- REQUIRE( c(0, 0) == d(0, 0) );\
- if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
- REQUIRE( c(1, 0) == d(1, 0) );\
- if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
- REQUIRE( c(2, 0) == d(2, 0) );\
- if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
- REQUIRE( c(0, 1) == d(0, 1) );\
- if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
- REQUIRE( c(1, 1) == d(1, 1) );\
- if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
- REQUIRE( c(2, 1) == d(2, 1) );\
- if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
- REQUIRE( c(0, 2) == d(0, 2) );\
- if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
- REQUIRE( c(1, 2) == d(1, 2) );\
- if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
- REQUIRE( c(2, 2) == d(2, 2) );\
- \
- c /= EOP(a);\
- d /= EOP(b);\
- \
- if (c(0, 0) == c(0, 0) && d(0, 0) == d(0, 0))\
- REQUIRE( c(0, 0) == d(0, 0) );\
- if (c(1, 0) == c(1, 0) && d(1, 0) == d(1, 0))\
- REQUIRE( c(1, 0) == d(1, 0) );\
- if (c(2, 0) == c(2, 0) && d(2, 0) == d(2, 0))\
- REQUIRE( c(2, 0) == d(2, 0) );\
- if (c(0, 1) == c(0, 1) && d(0, 1) == d(0, 1))\
- REQUIRE( c(0, 1) == d(0, 1) );\
- if (c(1, 1) == c(1, 1) && d(1, 1) == d(1, 1))\
- REQUIRE( c(1, 1) == d(1, 1) );\
- if (c(2, 1) == c(2, 1) && d(2, 1) == d(2, 1))\
- REQUIRE( c(2, 1) == d(2, 1) );\
- if (c(0, 2) == c(0, 2) && d(0, 2) == d(0, 2))\
- REQUIRE( c(0, 2) == d(0, 2) );\
- if (c(1, 2) == c(1, 2) && d(1, 2) == d(1, 2))\
- REQUIRE( c(1, 2) == d(1, 2) );\
- if (c(2, 2) == c(2, 2) && d(2, 2) == d(2, 2))\
- REQUIRE( c(2, 2) == d(2, 2) );\
- }
- // Now run all the operators...
- TEST_OPERATOR("sp_mat_abs_test", abs)
- //TEST_OPERATOR("sp_mat_eps_test", eps);
- //TEST_OPERATOR(expTest, exp);
- //TEST_OPERATOR(exp2Test, exp2);
- //TEST_OPERATOR(exp10Test, exp10);
- //TEST_OPERATOR(trunc_expTest, trunc_exp);
- //TEST_OPERATOR(logTest, log);
- //TEST_OPERATOR(log2Test, log2);
- //TEST_OPERATOR(log10Test, log10);
- //TEST_OPERATOR(trunc_logTest, trunc_log);
- TEST_OPERATOR("sp_mat_sqrt_test", sqrt)
- TEST_OPERATOR("sp_mat_square_test", square)
- TEST_OPERATOR("sp_mat_floor_test", floor)
- TEST_OPERATOR("sp_mat_ceil_test", ceil)
- //TEST_OPERATOR(cosTest, cos);
- //TEST_OPERATOR(acosTest, acos);
- //TEST_OPERATOR(coshTest, cosh);
- //TEST_OPERATOR(acoshTest, acosh);
- //TEST_OPERATOR(sinTest, sin);
- //TEST_OPERATOR(asinTest, asin);
- //TEST_OPERATOR(sinhTest, sinh);
- //TEST_OPERATOR(asinhTest, asinh);
- //TEST_OPERATOR(tanTest, tan);
- //TEST_OPERATOR(tanhTest, tanh);
- //TEST_OPERATOR(atanTest, atan);
- //TEST_OPERATOR(atanhTest, atanh);
- /*
- TEST_CASE("spmat_diskio_tests")
- {
- std::string file_names[] = {"raw_ascii.txt",
- "raw_binary.bin",
- "arma_ascii.csv",
- "csv_ascii.csv",
- "arma_binary.bin",
- "pgm_binary.bin",
- "coord_ascii.txt"};
- diskio dio;
- SpMat<int> m(4, 3);
- m(0, 0) = 1;
- m(3, 0) = 2;
- m(0, 2) = 3;
- m(3, 2) = 4;
- m(2, 1) = 5;
- m(1, 2) = 6;
- // Save the matrix.
- REQUIRE( dio.save_raw_ascii(m, file_names[0]) );
- // REQUIRE( dio.save_raw_binary(m, file_names[1]) );
- // REQUIRE( dio.save_arma_ascii(m, file_names[2]) );
- // REQUIRE( dio.save_csv_ascii(m, file_names[3]) );
- REQUIRE( dio.save_arma_binary(m, file_names[4]) );
- // REQUIRE( dio.save_pgm_binary(m, file_names[5]) );
- REQUIRE( dio.save_coord_ascii(m, file_names[6]) );
- // Load the files.
- SpMat<int> lm[7];
- std::string err;
- REQUIRE( dio.load_raw_ascii(lm[0], file_names[0], err) );
- // REQUIRE( dio.load_raw_binary(lm[1], file_names[1], err) );
- // REQUIRE( dio.load_arma_ascii(lm[2], file_names[2], err) );
- // REQUIRE( dio.load_csv_ascii(lm[3], file_names[3], err) );
- REQUIRE( dio.load_arma_binary(lm[4], file_names[4], err) );
- // REQUIRE( dio.load_pgm_binary(lm[5], file_names[5], err) );
- REQUIRE( dio.load_coord_ascii(lm[6], file_names[6], err) );
- // Now make sure all the matrices are identical.
- for (uword i = 0; i < 7; i++)
- {
- for (uword r = 0; r < 4; r++)
- {
- for (uword c = 0; c < 3; c++)
- {
- REQUIRE( m(r, c) == lm[i](r, c) );
- }
- }
- }
- for (uword i = 0; i < 7; ++i)
- {
- remove(file_names[i].c_str());
- }
- }
- */
- TEST_CASE("min_test")
- {
- SpCol<double> a(5);
- a(0) = 3.0;
- a(2) = 1.0;
- double res = min(a);
- REQUIRE( res == Approx(1e-5) );
- a(0) = -3.0;
- a(2) = -1.0;
- res = min(a);
- REQUIRE( res == Approx(-3.0) );
- a(0) = 1.3;
- a(1) = 2.4;
- a(2) = 3.1;
- a(3) = 4.4;
- a(4) = 1.4;
- res = min(a);
- REQUIRE( res == Approx(1.3) );
- SpRow<double> b(5);
- b(0) = 3.0;
- b(2) = 1.0;
- res = min(b);
- REQUIRE( res == Approx(1e-5) );
- b(0) = -3.0;
- b(2) = -1.0;
- res = min(b);
- REQUIRE( res == Approx(-3.0) );
- b(0) = 1.3;
- b(1) = 2.4;
- b(2) = 3.1;
- b(3) = 4.4;
- b(4) = 1.4;
- res = min(b);
- REQUIRE( res == Approx(1.3) );
- SpMat<double> c(6, 5);
- c(0, 0) = 1.0;
- c(1, 0) = 3.0;
- c(2, 0) = 4.0;
- c(3, 0) = 0.6;
- c(4, 0) = 1.4;
- c(5, 0) = 1.2;
- c(3, 2) = 1.3;
- c(2, 3) = -4.0;
- c(4, 3) = -1.4;
- c(5, 2) = -3.4;
- c(5, 3) = -4.1;
- SpMat<double> r = min(c, 0);
- REQUIRE( r.n_rows == 1 );
- REQUIRE( r.n_cols == 5 );
- REQUIRE( (double) r(0, 0) == Approx(0.6) );
- REQUIRE( (double) r(0, 1) == Approx(1e-5) );
- REQUIRE( (double) r(0, 2) == Approx(-3.4) );
- REQUIRE( (double) r(0, 3) == Approx(-4.1) );
- REQUIRE( (double) r(0, 4) == Approx(1e-5) );
- r = min(c, 1);
- REQUIRE( r.n_rows == 6 );
- REQUIRE( r.n_cols == 1 );
- REQUIRE( (double) r(0, 0) == Approx(1e-5) );
- REQUIRE( (double) r(1, 0) == Approx(1e-5) );
- REQUIRE( (double) r(2, 0) == Approx(-4.0) );
- REQUIRE( (double) r(3, 0) == Approx(1e-5) );
- REQUIRE( (double) r(4, 0) == Approx(-1.4) );
- REQUIRE( (double) r(5, 0) == Approx(-4.1) );
- }
- TEST_CASE("max_test")
- {
- SpCol<double> a(5);
- a(0) = -3.0;
- a(2) = -1.0;
- double resa = max(a);
- REQUIRE( resa == Approx(1e-5) );
- a(0) = 3.0;
- a(2) = 1.0;
- resa = max(a);
- REQUIRE( resa == Approx(3.0) );
- a(0) = -1.3;
- a(1) = -2.4;
- a(2) = -3.1;
- a(3) = -4.4;
- a(4) = -1.4;
- resa = max(a);
- REQUIRE( resa == Approx(-1.3) );
- SpRow<double> b(5);
- b(0) = -3.0;
- b(2) = -1.0;
- resa = max(b);
- REQUIRE( resa == Approx(1e-5) );
- b(0) = 3.0;
- b(2) = 1.0;
- resa = max(b);
- REQUIRE( resa == Approx(3.0) );
- b(0) = -1.3;
- b(1) = -2.4;
- b(2) = -3.1;
- b(3) = -4.4;
- b(4) = -1.4;
- resa = max(b);
- REQUIRE( resa == Approx(-1.3) );
- SpMat<double> c(6, 5);
- c(0, 0) = 1.0;
- c(1, 0) = 3.0;
- c(2, 0) = 4.0;
- c(3, 0) = 0.6;
- c(4, 0) = -1.4;
- c(5, 0) = 1.2;
- c(3, 2) = 1.3;
- c(2, 3) = -4.0;
- c(4, 3) = -1.4;
- c(5, 2) = -3.4;
- c(5, 3) = -4.1;
- SpMat<double> res = max(c, 0);
- REQUIRE( res.n_rows == 1 );
- REQUIRE( res.n_cols == 5 );
- REQUIRE( (double) res(0, 0) == Approx(4.0) );
- REQUIRE( (double) res(0, 1) == Approx(1e-5) );
- REQUIRE( (double) res(0, 2) == Approx(1.3) );
- REQUIRE( (double) res(0, 3) == Approx(1e-5) );
- REQUIRE( (double) res(0, 4) == Approx(1e-5) );
- res = max(c, 1);
- REQUIRE( res.n_rows == 6 );
- REQUIRE( res.n_cols == 1 );
- REQUIRE( (double) res(0, 0) == Approx(1.0) );
- REQUIRE( (double) res(1, 0) == Approx(3.0) );
- REQUIRE( (double) res(2, 0) == Approx(4.0) );
- REQUIRE( (double) res(3, 0) == Approx(1.3) );
- REQUIRE( (double) res(4, 0) == Approx(1e-5) );
- REQUIRE( (double) res(5, 0) == Approx(1.2) );
- }
- TEST_CASE("spmat_min_cx_test")
- {
- SpCol<std::complex<double> > a(5);
- a(0) = std::complex<double>(3.0, -2.0);
- a(2) = std::complex<double>(1.0, 1.0);
- std::complex<double> res = min(a);
- REQUIRE( res.real() == Approx(1e-5) );
- REQUIRE( res.imag() == Approx(1e-5) );
- a(0) = std::complex<double>(-3.0, -2.0);
- a(2) = std::complex<double>(-1.0, -1.0);
- res = min(a);
- REQUIRE( res.real() == Approx(1e-5) );
- REQUIRE( res.imag() == Approx(1e-5) );
- a(0) = std::complex<double>(1.0, 0.5);
- a(1) = std::complex<double>(2.4, 1.4);
- a(2) = std::complex<double>(0.5, 0.5);
- a(3) = std::complex<double>(2.0, 2.0);
- a(4) = std::complex<double>(1.4, -1.4);
- res = min(a);
- REQUIRE( res.real() == Approx(0.5) );
- REQUIRE( res.imag() == Approx(0.5) );
- SpRow<std::complex<double> > b(5);
- b(0) = std::complex<double>(3.0, -2.0);
- b(2) = std::complex<double>(1.0, 1.0);
- res = min(b);
- REQUIRE( res.real() == Approx(1e-5) );
- REQUIRE( res.imag() == Approx(1e-5) );
- b(0) = std::complex<double>(-3.0, -2.0);
- b(2) = std::complex<double>(-1.0, -1.0);
- res = min(b);
- REQUIRE( res.real() == Approx(1e-5) );
- REQUIRE( res.imag() == Approx(1e-5) );
- b(0) = std::complex<double>(1.0, 0.5);
- b(1) = std::complex<double>(2.4, 1.4);
- b(2) = std::complex<double>(0.5, 0.5);
- b(3) = std::complex<double>(2.0, 2.0);
- b(4) = std::complex<double>(1.4, -1.4);
- res = min(b);
- REQUIRE( res.real() == Approx(0.5) );
- REQUIRE( res.imag() == Approx(0.5) );
- SpMat<std::complex<double> > c(4, 3);
- c(0, 0) = std::complex<double>(1.0, 2.0);
- c(0, 1) = std::complex<double>(0.5, 0.5);
- c(0, 2) = std::complex<double>(2.0, 4.0);
- c(1, 1) = std::complex<double>(-1.0, -2.0);
- c(2, 1) = std::complex<double>(-3.0, -3.0);
- c(3, 1) = std::complex<double>(0.25, 0.25);
- SpMat<std::complex<double> > r = min(c, 0);
- REQUIRE( r.n_rows == 1 );
- REQUIRE( r.n_cols == 3 );
- REQUIRE( ((std::complex<double>) r(0, 0)).real() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(0, 0)).imag() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(0, 1)).real() == Approx(0.25) );
- REQUIRE( ((std::complex<double>) r(0, 1)).imag() == Approx(0.25) );
- REQUIRE( ((std::complex<double>) r(0, 2)).real() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(0, 2)).imag() == Approx(1e-5) );
- r = min(c, 1);
- REQUIRE( r.n_rows == 4 );
- REQUIRE( r.n_cols == 1 );
- REQUIRE( ((std::complex<double>) r(0, 0)).real() == Approx(0.5) );
- REQUIRE( ((std::complex<double>) r(0, 0)).imag() == Approx(0.5) );
- REQUIRE( ((std::complex<double>) r(1, 0)).real() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(1, 0)).imag() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(2, 0)).real() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(2, 0)).imag() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(3, 0)).real() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(3, 0)).imag() == Approx(1e-5) );
- }
- TEST_CASE("spmat_max_cx_test")
- {
- SpCol<std::complex<double> > a(5);
- a(0) = std::complex<double>(3.0, -2.0);
- a(2) = std::complex<double>(1.0, 1.0);
- std::complex<double> res = max(a);
- REQUIRE( res.real() == Approx(3.0) );
- REQUIRE( res.imag() == Approx(-2.0) );
- a(0) = std::complex<double>(0);
- a(2) = std::complex<double>(0);
- res = max(a);
- REQUIRE( res.real() == Approx(1e-5) );
- REQUIRE( res.imag() == Approx(1e-5) );
- a(0) = std::complex<double>(1.0, 0.5);
- a(1) = std::complex<double>(2.4, 1.4);
- a(2) = std::complex<double>(0.5, 0.5);
- a(3) = std::complex<double>(2.0, 2.0);
- a(4) = std::complex<double>(1.4, -1.4);
- res = max(a);
- REQUIRE( res.real() == Approx(2.0) );
- REQUIRE( res.imag() == Approx(2.0) );
- SpRow<std::complex<double> > b(5);
- b(0) = std::complex<double>(3.0, -2.0);
- b(2) = std::complex<double>(1.0, 1.0);
- res = max(b);
- REQUIRE( res.real() == Approx(3.0) );
- REQUIRE( res.imag() == Approx(-2.0) );
- b(0) = std::complex<double>(0);
- b(2) = std::complex<double>(0);
- res = max(b);
- REQUIRE( res.real() == Approx(1e-5) );
- REQUIRE( res.imag() == Approx(1e-5) );
- b(0) = std::complex<double>(1.0, 0.5);
- b(1) = std::complex<double>(2.4, 1.4);
- b(2) = std::complex<double>(0.5, 0.5);
- b(3) = std::complex<double>(2.0, 2.0);
- b(4) = std::complex<double>(1.4, -1.4);
- res = max(b);
- REQUIRE( res.real() == Approx(2.0) );
- REQUIRE( res.imag() == Approx(2.0) );
- SpMat<std::complex<double> > c(4, 3);
- c(0, 0) = std::complex<double>(1.0, 2.0);
- c(0, 1) = std::complex<double>(0.5, 0.5);
- c(1, 1) = std::complex<double>(-1.0, -2.0);
- c(2, 1) = std::complex<double>(-3.0, -3.0);
- c(3, 1) = std::complex<double>(0.25, 0.25);
- SpMat<std::complex<double> > r = max(c, 0);
- REQUIRE( r.n_rows == 1 );
- REQUIRE( r.n_cols == 3 );
- REQUIRE( ((std::complex<double>) r(0, 0)).real() == Approx(1.0) );
- REQUIRE( ((std::complex<double>) r(0, 0)).imag() == Approx(2.0) );
- REQUIRE( ((std::complex<double>) r(0, 1)).real() == Approx(-3.0) );
- REQUIRE( ((std::complex<double>) r(0, 1)).imag() == Approx(-3.0) );
- REQUIRE( ((std::complex<double>) r(0, 2)).real() == Approx(1e-5) );
- REQUIRE( ((std::complex<double>) r(0, 2)).imag() == Approx(1e-5) );
- r = max(c, 1);
- REQUIRE( r.n_rows == 4 );
- REQUIRE( r.n_cols == 1 );
- REQUIRE( ((std::complex<double>) r(0, 0)).real() == Approx(1.0) );
- REQUIRE( ((std::complex<double>) r(0, 0)).imag() == Approx(2.0) );
- REQUIRE( ((std::complex<double>) r(1, 0)).real() == Approx(-1.0) );
- REQUIRE( ((std::complex<double>) r(1, 0)).imag() == Approx(-2.0) );
- REQUIRE( ((std::complex<double>) r(2, 0)).real() == Approx(-3.0) );
- REQUIRE( ((std::complex<double>) r(2, 0)).imag() == Approx(-3.0) );
- REQUIRE( ((std::complex<double>) r(3, 0)).real() == Approx(0.25) );
- REQUIRE( ((std::complex<double>) r(3, 0)).imag() == Approx(0.25) );
- }
- TEST_CASE("spmat_complex_constructor_test")
- {
- // First make two sparse matrices.
- SpMat<double> a(8, 10);
- SpMat<double> b(8, 10);
- a(0, 0) = 4;
- a(4, 2) = 5;
- a(5, 3) = 6;
- a(6, 3) = 7;
- a(1, 4) = 1;
- a(5, 4) = 6;
- a(7, 6) = 3;
- a(0, 7) = 2;
- a(3, 7) = 3;
- b(0, 0) = 4;
- b(4, 2) = 5;
- b(7, 3) = 4;
- b(1, 4) = 1;
- b(3, 4) = 6;
- b(5, 4) = -1;
- b(6, 4) = 2;
- b(7, 4) = 3;
- b(6, 5) = 2;
- b(6, 6) = 3;
- b(3, 7) = 4;
- b(6, 7) = 5;
- SpMat<std::complex<double> > c(a, b);
- REQUIRE( c.n_nonzero == 16 );
- REQUIRE( (std::complex<double>) c(0, 0) == std::complex<double>(4, 4) );
- REQUIRE( (std::complex<double>) c(4, 2) == std::complex<double>(5, 5) );
- REQUIRE( (std::complex<double>) c(5, 3) == std::complex<double>(6, 0) );
- REQUIRE( (std::complex<double>) c(6, 3) == std::complex<double>(7, 0) );
- REQUIRE( (std::complex<double>) c(7, 3) == std::complex<double>(0, 4) );
- REQUIRE( (std::complex<double>) c(1, 4) == std::complex<double>(1, 1) );
- REQUIRE( (std::complex<double>) c(3, 4) == std::complex<double>(0, 6) );
- REQUIRE( (std::complex<double>) c(5, 4) == std::complex<double>(6, -1) );
- REQUIRE( (std::complex<double>) c(6, 4) == std::complex<double>(0, 2) );
- REQUIRE( (std::complex<double>) c(7, 4) == std::complex<double>(0, 3) );
- REQUIRE( (std::complex<double>) c(6, 5) == std::complex<double>(0, 2) );
- REQUIRE( (std::complex<double>) c(6, 6) == std::complex<double>(0, 3) );
- REQUIRE( (std::complex<double>) c(7, 6) == std::complex<double>(3, 0) );
- REQUIRE( (std::complex<double>) c(0, 7) == std::complex<double>(2, 0) );
- REQUIRE( (std::complex<double>) c(3, 7) == std::complex<double>(3, 4) );
- REQUIRE( (std::complex<double>) c(6, 7) == std::complex<double>(0, 5) );
- }
- TEST_CASE("spmat_unary_operators_test")
- {
- SpMat<int> a(3, 3);
- SpMat<int> b(3, 3);
- a(0, 0) = 1;
- a(1, 2) = 4;
- a(2, 2) = 5;
- b(0, 1) = 1;
- b(1, 0) = 2;
- b(1, 2) = -4;
- b(2, 2) = 5;
- SpMat<int> c = a + b;
- REQUIRE( c.n_nonzero == 4 );
- REQUIRE( (double) c(0, 0) == 1 );
- REQUIRE( (double) c(1, 0) == 2 );
- REQUIRE( (double) c(2, 0) == 0 );
- REQUIRE( (double) c(0, 1) == 1 );
- REQUIRE( (double) c(1, 1) == 0 );
- REQUIRE( (double) c(2, 1) == 0 );
- REQUIRE( (double) c(0, 2) == 0 );
- REQUIRE( (double) c(1, 2) == 0 );
- REQUIRE( (double) c(2, 2) == 10 );
- c = a - b;
- REQUIRE( c.n_nonzero == 4 );
- REQUIRE( (double) c(0, 0) == 1 );
- REQUIRE( (double) c(1, 0) == -2 );
- REQUIRE( (double) c(2, 0) == 0 );
- REQUIRE( (double) c(0, 1) == -1 );
- REQUIRE( (double) c(1, 1) == 0 );
- REQUIRE( (double) c(2, 1) == 0 );
- REQUIRE( (double) c(0, 2) == 0 );
- REQUIRE( (double) c(1, 2) == 8 );
- REQUIRE( (double) c(2, 2) == 0 );
- c = a % b;
- REQUIRE( c.n_nonzero == 2 );
- REQUIRE( (double) c(0, 0) == 0 );
- REQUIRE( (double) c(1, 0) == 0 );
- REQUIRE( (double) c(2, 0) == 0 );
- REQUIRE( (double) c(0, 1) == 0 );
- REQUIRE( (double) c(1, 1) == 0 );
- REQUIRE( (double) c(2, 1) == 0 );
- REQUIRE( (double) c(0, 2) == 0 );
- REQUIRE( (double) c(1, 2) == -16 );
- REQUIRE( (double) c(2, 2) == 25 );
- a(0, 0) = 4;
- b(0, 0) = 2;
- /*
- c = a / b;
- REQUIRE( c.n_nonzero == 3 );
- REQUIRE( (double) c(0, 0) == 2 );
- REQUIRE( (double) c(1, 0) == 0 );
- REQUIRE( (double) c(2, 0) == 0 );
- REQUIRE( (double) c(0, 1) == 0 );
- REQUIRE( (double) c(1, 1) == 0 );
- REQUIRE( (double) c(2, 1) == 0 );
- REQUIRE( (double) c(0, 2) == 0 );
- REQUIRE( (double) c(1, 2) == -1 );
- REQUIRE( (double) c(2, 2) == 1 );
- */
- }
- TEST_CASE("spmat_unary_val_operators_test")
- {
- SpMat<double> a(2, 2);
- a(0, 0) = 2.0;
- a(1, 1) = -3.0;
- SpMat<double> b = a * 3.0;
- REQUIRE( b.n_nonzero == 2 );
- REQUIRE( (double) b(0, 0) == Approx(6.0) );
- REQUIRE( (double) b(0, 1) == Approx(1e-5) );
- REQUIRE( (double) b(1, 0) == Approx(1e-5) );
- REQUIRE( (double) b(1, 1) == Approx(-9.0) );
- b = a / 3.0;
- REQUIRE( b.n_nonzero == 2 );
- REQUIRE( (double) b(0, 0) == Approx(2.0 / 3.0) );
- REQUIRE( (double) b(0, 1) == Approx(1e-5) );
- REQUIRE( (double) b(1, 0) == Approx(1e-5) );
- REQUIRE( (double) b(1, 1) == Approx(-1.0) );
- }
- TEST_CASE("spmat_sparse_unary_multiplication_test")
- {
- SpMat<double> spaa(10, 10);
- spaa(1, 5) = 0.4;
- spaa(0, 4) = 0.3;
- spaa(0, 8) = 1.2;
- spaa(3, 0) = 1.1;
- spaa(3, 1) = 1.1;
- spaa(3, 2) = 1.1;
- spaa(4, 4) = 0.2;
- spaa(4, 9) = 0.1;
- spaa(6, 2) = 4.1;
- spaa(6, 8) = 4.1;
- spaa(7, 5) = 1.0;
- spaa(8, 9) = 0.4;
- spaa(9, 4) = 0.4;
- double correctResultB[10][10] =
- {{ 0.00, 0.00, 0.00, 0.00, 0.06, 0.00, 0.00, 0.00, 0.00, 0.51 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.33, 0.44, 0.00, 0.00, 1.32, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.08, 0.00, 0.00, 0.00, 0.00, 0.02 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.64 },
- { 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.16, 0.00, 0.00, 0.00, 0.00, 0.00 },
- { 0.00, 0.00, 0.00, 0.00, 0.08, 0.00, 0.00, 0.00, 0.00, 0.04 }};
- SpMat<double> spab = spaa * spaa;
- for (uword i = 0; i < 10; i++)
- {
- for (uword j = 0; j < 10; j++)
- {
- REQUIRE( (double) spab(i, j) == Approx(correctResultB[i][j]) );
- }
- }
- SpMat<double> spac(15, 15);
- spac(6, 10) = 0.4;
- spac(5, 9) = 0.3;
- spac(5, 13) = 1.2;
- spac(8, 5) = 1.1;
- spac(8, 6) = 1.1;
- spac(8, 7) = 1.1;
- spac(9, 9) = 0.2;
- spac(9, 14) = 0.1;
- spac(11, 7) = 4.1;
- spac(11, 13) = 4.1;
- spac(12, 10) = 1.0;
- spac(13, 14) = 0.4;
- spac(14, 9) = 0.4;
- spab = spaa * spac.submat(5, 5, 14, 14);
- for (uword i = 0; i < 10; i++)
- {
- for (uword j = 0; j < 10; j++)
- {
- REQUIRE( (double) spab(i, j) == Approx(correctResultB[i][j]) );
- }
- }
- }
- TEST_CASE("spmat_unary_operator_test_2")
- {
- SpMat<double> a(3, 3);
- a(0, 0) = 1;
- a(0, 2) = 3.5;
- a(1, 2) = 4.0;
- a(2, 2) = -3.0;
- mat b(3, 3);
- b.fill(3.0);
- mat c = a + b;
- REQUIRE( c(0, 0) == Approx(4.0) );
- REQUIRE( c(1, 0) == Approx(3.0) );
- REQUIRE( c(2, 0) == Approx(3.0) );
- REQUIRE( c(0, 1) == Approx(3.0) );
- REQUIRE( c(1, 1) == Approx(3.0) );
- REQUIRE( c(2, 1) == Approx(3.0) );
- REQUIRE( c(0, 2) == Approx(6.5) );
- REQUIRE( c(1, 2) == Approx(7.0) );
- REQUIRE( c(2, 2) == Approx(1e-5) );
- c = a - b;
- REQUIRE( c(0, 0) == Approx(-2.0) );
- REQUIRE( c(1, 0) == Approx(-3.0) );
- REQUIRE( c(2, 0) == Approx(-3.0) );
- REQUIRE( c(0, 1) == Approx(-3.0) );
- REQUIRE( c(1, 1) == Approx(-3.0) );
- REQUIRE( c(2, 1) == Approx(-3.0) );
- REQUIRE( c(0, 2) == Approx(0.5) );
- REQUIRE( c(1, 2) == Approx(1.0) );
- REQUIRE( c(2, 2) == Approx(-6.0) );
- SpMat<double> d = a % b;
- REQUIRE( d.n_nonzero == 4 );
- REQUIRE( (double) d(0, 0) == Approx(3.0) );
- REQUIRE( (double) d(1, 0) == Approx(1e-5) );
- REQUIRE( (double) d(2, 0) == Approx(1e-5) );
- REQUIRE( (double) d(0, 1) == Approx(1e-5) );
- REQUIRE( (double) d(1, 1) == Approx(1e-5) );
- REQUIRE( (double) d(2, 1) == Approx(1e-5) );
- REQUIRE( (double) d(0, 2) == Approx(10.5) );
- REQUIRE( (double) d(1, 2) == Approx(12.0) );
- REQUIRE( (double) d(2, 2) == Approx(-9.0) );
- d = a / b;
- REQUIRE( d.n_nonzero == 4 );
- REQUIRE( (double) d(0, 0) == Approx((1.0 / 3.0)) );
- REQUIRE( (double) d(1, 0) == Approx(1e-5) );
- REQUIRE( (double) d(2, 0) == Approx(1e-5) );
- REQUIRE( (double) d(0, 1) == Approx(1e-5) );
- REQUIRE( (double) d(1, 1) == Approx(1e-5) );
- REQUIRE( (double) d(2, 1) == Approx(1e-5) );
- REQUIRE( (double) d(0, 2) == Approx((3.5 / 3.0)) );
- REQUIRE( (double) d(1, 2) == Approx((4.0 / 3.0)) );
- REQUIRE( (double) d(2, 2) == Approx(-1.0) );
- c = a * b;
- REQUIRE( (double) c(0, 0) == Approx(13.5) );
- REQUIRE( (double) c(1, 0) == Approx(12.0) );
- REQUIRE( (double) c(2, 0) == Approx(-9.0) );
- REQUIRE( (double) c(0, 1) == Approx(13.5) );
- REQUIRE( (double) c(1, 1) == Approx(12.0) );
- REQUIRE( (double) c(2, 1) == Approx(-9.0) );
- REQUIRE( (double) c(0, 2) == Approx(13.5) );
- REQUIRE( (double) c(1, 2) == Approx(12.0) );
- REQUIRE( (double) c(2, 2) == Approx(-9.0) );
- c = b * a;
- REQUIRE( (double) c(0, 0) == Approx(3.0) );
- REQUIRE( (double) c(1, 0) == Approx(3.0) );
- REQUIRE( (double) c(2, 0) == Approx(3.0) );
- REQUIRE( (double) c(0, 1) == Approx(1e-5) );
- REQUIRE( (double) c(1, 1) == Approx(1e-5) );
- REQUIRE( (double) c(2, 1) == Approx(1e-5) );
- REQUIRE( (double) c(0, 2) == Approx(13.5) );
- REQUIRE( (double) c(1, 2) == Approx(13.5) );
- REQUIRE( (double) c(2, 2) == Approx(13.5) );
- }
- TEST_CASE("spmat_mat_operator_tests")
- {
- SpMat<double> a(3, 3);
- a(0, 0) = 2.0;
- a(1, 2) = 3.5;
- a(2, 1) = -2.0;
- a(2, 2) = 4.5;
- mat b(3, 3);
- b.fill(2.0);
- mat c(b);
- c += a;
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 0) == Approx(2.0) );
- REQUIRE( (double) c(2, 0) == Approx(2.0) );
- REQUIRE( (double) c(0, 1) == Approx(2.0) );
- REQUIRE( (double) c(1, 1) == Approx(2.0) );
- REQUIRE( (double) c(2, 1) == Approx(1e-5) );
- REQUIRE( (double) c(0, 2) == Approx(2.0) );
- REQUIRE( (double) c(1, 2) == Approx(5.5) );
- REQUIRE( (double) c(2, 2) == Approx(6.5) );
- c = b + a;
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 0) == Approx(2.0) );
- REQUIRE( (double) c(2, 0) == Approx(2.0) );
- REQUIRE( (double) c(0, 1) == Approx(2.0) );
- REQUIRE( (double) c(1, 1) == Approx(2.0) );
- REQUIRE( (double) c(2, 1) == Approx(1e-5) );
- REQUIRE( (double) c(0, 2) == Approx(2.0) );
- REQUIRE( (double) c(1, 2) == Approx(5.5) );
- REQUIRE( (double) c(2, 2) == Approx(6.5) );
- c = b;
- c -= a;
- REQUIRE( (double) c(0, 0) == Approx(1e-5) );
- REQUIRE( (double) c(1, 0) == Approx(2.0) );
- REQUIRE( (double) c(2, 0) == Approx(2.0) );
- REQUIRE( (double) c(0, 1) == Approx(2.0) );
- REQUIRE( (double) c(1, 1) == Approx(2.0) );
- REQUIRE( (double) c(2, 1) == Approx(4.0) );
- REQUIRE( (double) c(0, 2) == Approx(2.0) );
- REQUIRE( (double) c(1, 2) == Approx(-1.5) );
- REQUIRE( (double) c(2, 2) == Approx(-2.5) );
- c = b - a;
- REQUIRE( (double) c(0, 0) == Approx(1e-5) );
- REQUIRE( (double) c(1, 0) == Approx(2.0) );
- REQUIRE( (double) c(2, 0) == Approx(2.0) );
- REQUIRE( (double) c(0, 1) == Approx(2.0) );
- REQUIRE( (double) c(1, 1) == Approx(2.0) );
- REQUIRE( (double) c(2, 1) == Approx(4.0) );
- REQUIRE( (double) c(0, 2) == Approx(2.0) );
- REQUIRE( (double) c(1, 2) == Approx(-1.5) );
- REQUIRE( (double) c(2, 2) == Approx(-2.5) );
- c = b;
- c *= a;
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 0) == Approx(4.0) );
- REQUIRE( (double) c(2, 0) == Approx(4.0) );
- REQUIRE( (double) c(0, 1) == Approx(-4.0) );
- REQUIRE( (double) c(1, 1) == Approx(-4.0) );
- REQUIRE( (double) c(2, 1) == Approx(-4.0) );
- REQUIRE( (double) c(0, 2) == Approx(16.0) );
- REQUIRE( (double) c(1, 2) == Approx(16.0) );
- REQUIRE( (double) c(2, 2) == Approx(16.0) );
- mat e = b * a;
- REQUIRE( (double) e(0, 0) == Approx(4.0) );
- REQUIRE( (double) e(1, 0) == Approx(4.0) );
- REQUIRE( (double) e(2, 0) == Approx(4.0) );
- REQUIRE( (double) e(0, 1) == Approx(-4.0) );
- REQUIRE( (double) e(1, 1) == Approx(-4.0) );
- REQUIRE( (double) e(2, 1) == Approx(-4.0) );
- REQUIRE( (double) e(0, 2) == Approx(16.0) );
- REQUIRE( (double) e(1, 2) == Approx(16.0) );
- REQUIRE( (double) e(2, 2) == Approx(16.0) );
- c = b;
- c %= a;
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 0) == Approx(1e-5) );
- REQUIRE( (double) c(2, 0) == Approx(1e-5) );
- REQUIRE( (double) c(0, 1) == Approx(1e-5) );
- REQUIRE( (double) c(1, 1) == Approx(1e-5) );
- REQUIRE( (double) c(2, 1) == Approx(-4.0) );
- REQUIRE( (double) c(0, 2) == Approx(1e-5) );
- REQUIRE( (double) c(1, 2) == Approx(7.0) );
- REQUIRE( (double) c(2, 2) == Approx(9.0) );
- SpMat<double> d = b % a;
- REQUIRE( d.n_nonzero == 4 );
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(2, 1) == Approx(-4.0) );
- REQUIRE( (double) c(1, 2) == Approx(7.0) );
- REQUIRE( (double) c(2, 2) == Approx(9.0) );
- c = b;
- c /= a;
- REQUIRE( c(0, 0) == Approx(1.0) );
- REQUIRE( std::isinf(c(1, 0)) );
- REQUIRE( std::isinf(c(2, 0)) );
- REQUIRE( std::isinf(c(0, 1)) );
- REQUIRE( std::isinf(c(1, 1)) );
- REQUIRE( c(2, 1) == Approx(-1.0) );
- REQUIRE( std::isinf(c(0, 2)) );
- REQUIRE( c(1, 2) == Approx(2.0 / 3.5) );
- REQUIRE( c(2, 2) == Approx(2.0 / 4.5) );
- }
- TEST_CASE("spmat_empty_hadamard")
- {
- SpMat<double> x(5, 5), y(5, 5), z;
- z = x % y;
- REQUIRE( z.n_nonzero == 0 );
- REQUIRE( z.n_rows == 5 );
- REQUIRE( z.n_cols == 5 );
- }
- TEST_CASE("spmat_sparse_dense_in_place")
- {
- SpMat<double> a;
- a.sprandu(50, 50, 0.1);
- mat b;
- b.randu(50, 50);
- mat d( a);
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) a(r, c) != 0)
- REQUIRE( (double) a(r, c) == Approx(d(r, c)) );
- else
- REQUIRE( d(r, c) == Approx(1e-5) );
- }
- }
- SpMat<double> x;
- mat y;
- x = a;
- y = d;
- x *= b;
- y *= b;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) a(r, c) != 0)
- REQUIRE( (double) a(r, c) == Approx(d(r, c)) );
- else
- REQUIRE( d(r, c) == Approx(1e-5) );
- }
- }
- x = a;
- y = d;
- x /= b;
- y /= b;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) a(r, c) != 0)
- REQUIRE( (double) a(r, c) == Approx(d(r, c)) );
- else
- REQUIRE( d(r, c) == Approx(1e-5) );
- }
- }
- x = a;
- y = d;
- x %= b;
- y %= b;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) a(r, c) != 0)
- REQUIRE( (double) a(r, c) == Approx(d(r, c)) );
- else
- REQUIRE(d(r, c) == Approx(1e-5) );
- }
- }
- }
- TEST_CASE("spmat_sparse_dense_not_in_place")
- {
- SpMat<double> a;
- a.sprandu(50, 50, 0.1);
- mat b;
- b.randu(50, 50);
- mat d(a);
- SpMat<double> x;
- mat y;
- mat z;
- y = a + b;
- z = d + b;
- for (uword c = 0; c < 50; ++c)
- {
- for(uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- y = a - b;
- z = d - b;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- y = a * b;
- z = d * b;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- y = a % b;
- z = d % b;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- y = a / b;
- z = d / b;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- y = b + a;
- z = b + d;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- y = b - a;
- z = b - d;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- y = b * a;
- z = b * d;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- y = b % a;
- z = b % d;
- for (uword c = 0; c < 50; ++c)
- {
- for (uword r = 0; r < 50; ++r)
- {
- if ((double) y(r, c) != 0)
- REQUIRE( (double) y(r, c) == Approx(z(r, c)) );
- else
- REQUIRE( z(r, c) == Approx(1e-5) );
- }
- }
- }
- TEST_CASE("spmat_batch_insert_test")
- {
- Mat<uword> locations(2, 5);
- locations(1, 0) = 1;
- locations(0, 0) = 2;
- locations(1, 1) = 1;
- locations(0, 1) = 7;
- locations(1, 2) = 4;
- locations(0, 2) = 0;
- locations(1, 3) = 4;
- locations(0, 3) = 9;
- locations(1, 4) = 5;
- locations(0, 4) = 0;
- Col<double> values(5);
- values[0] = 1.5;
- values[1] = -15.15;
- values[2] = 2.2;
- values[3] = 3.0;
- values[4] = 5.0;
- SpMat<double> m(locations, values, 10, 10, true);
- REQUIRE( m.n_nonzero == 5 );
- REQUIRE( m.n_rows == 10 );
- REQUIRE( m.n_cols == 10 );
- REQUIRE( (double) m(2, 1) == Approx(1.5) );
- REQUIRE( (double) m(7, 1) == Approx(-15.15) );
- REQUIRE( (double) m(0, 4) == Approx(2.2) );
- REQUIRE( (double) m(9, 4) == Approx(3.0) );
- REQUIRE( (double) m(0, 5) == Approx(5.0) );
- REQUIRE( m.col_ptrs[11] == std::numeric_limits<uword>::max() );
- // Auto size detection.
- SpMat<double> n(locations, values, true);
- REQUIRE( n.n_nonzero == 5 );
- REQUIRE( n.n_rows == 10 );
- REQUIRE( n.n_cols == 6 );
- REQUIRE( (double) n(2, 1) == Approx(1.5) );
- REQUIRE( (double) n(7, 1) == Approx(-15.15) );
- REQUIRE( (double) n(0, 4) == Approx(2.2) );
- REQUIRE( (double) n(9, 4) == Approx(3.0) );
- REQUIRE( (double) n(0, 5) == Approx(5.0) );
- REQUIRE( n.col_ptrs[7] == std::numeric_limits<uword>::max() );
- }
- TEST_CASE("spmat_batch_insert_unsorted_test")
- {
- Mat<uword> locations(2, 5);
- locations(1, 0) = 4;
- locations(0, 0) = 0;
- locations(1, 1) = 1;
- locations(0, 1) = 2;
- locations(1, 2) = 4;
- locations(0, 2) = 9;
- locations(1, 3) = 5;
- locations(0, 3) = 0;
- locations(1, 4) = 1;
- locations(0, 4) = 7;
- Col<double> values(5);
- values[1] = 1.5;
- values[4] = -15.15;
- values[0] = 2.2;
- values[2] = 3.0;
- values[3] = 5.0;
- SpMat<double> m(locations, values, 10, 10, true);
- REQUIRE( m.n_nonzero == 5 );
- REQUIRE( m.n_rows == 10 );
- REQUIRE( m.n_cols == 10 );
- REQUIRE( (double) m(2, 1) == Approx(1.5) );
- REQUIRE( (double) m(7, 1) == Approx(-15.15) );
- REQUIRE( (double) m(0, 4) == Approx(2.2) );
- REQUIRE( (double) m(9, 4) == Approx(3.0) );
- REQUIRE( (double) m(0, 5) == Approx(5.0) );
- // Auto size detection.
- SpMat<double> n(locations, values, true);
- REQUIRE( n.n_nonzero == 5 );
- REQUIRE( n.n_rows == 10 );
- REQUIRE( n.n_cols == 6 );
- REQUIRE( (double) n(2, 1) == Approx(1.5) );
- REQUIRE( (double) n(7, 1) == Approx(-15.15) );
- REQUIRE( (double) n(0, 4) == Approx(2.2) );
- REQUIRE( (double) n(9, 4) == Approx(3.0) );
- REQUIRE( (double) n(0, 5) == Approx(5.0) );
- }
- TEST_CASE("spmat_batch_insert_empty_test")
- {
- Mat<uword> locations(2, 0);
- Col<double> values;
- SpMat<double> m(locations, values, 10, 10, false);
- REQUIRE( m.n_nonzero == 0 );
- REQUIRE( m.n_rows == 10 );
- REQUIRE( m.n_cols == 10 );
- REQUIRE( m.col_ptrs[11] == std::numeric_limits<uword>::max() );
- SpMat<double> n(locations, values, false);
- REQUIRE( n.n_nonzero == 0 );
- REQUIRE( n.n_rows == 0 );
- REQUIRE( n.n_cols == 0 );
- REQUIRE( n.col_ptrs[1] == std::numeric_limits<uword>::max() );
- SpMat<double> o(locations, values, 10, 10, true);
- REQUIRE( o.n_nonzero == 0 );
- REQUIRE( o.n_rows == 10 );
- REQUIRE( o.n_cols == 10 );
- REQUIRE( o.col_ptrs[11] == std::numeric_limits<uword>::max() );
- SpMat<double> p(locations, values, true);
- REQUIRE( p.n_nonzero == 0 );
- REQUIRE( p.n_rows == 0 );
- REQUIRE( p.n_cols == 0 );
- REQUIRE( p.col_ptrs[1] == std::numeric_limits<uword>::max() );
- }
- // Make sure a matrix is the same as the other one.
- template<typename T1, typename T2>
- void CheckMatrices(const T1& a, const T2& b)
- {
- REQUIRE( a.n_rows == b.n_rows );
- REQUIRE( a.n_cols == b.n_cols );
- for (uword i = 0; i < a.n_elem; ++i)
- REQUIRE( (double) a[i] == Approx((double) b[i]) );
- }
- // Test the constructor written by Dirk.
- TEST_CASE("spmat_dirk_constructor_test")
- {
- // Come up with some values and stuff.
- vec values = "4.0 2.0 1.0 3.2 1.2 3.5";
- Col<uword> row_indices = "1 3 1 2 4 5";
- Col<uword> col_ptrs = "0 2 2 3 4 6";
- // Ok, now make a matrix.
- sp_mat M(row_indices, col_ptrs, values, 6, 5);
-
- REQUIRE( M.n_nonzero == 6 );
- // Make the equivalent dense matrix.
- mat D(6, 5);
- D.fill(0);
- D(1, 0) = 4.0;
- D(3, 0) = 2.0;
- D(1, 2) = 1.0;
- D(2, 3) = 3.2;
- D(4, 4) = 1.2;
- D(5, 4) = 3.5;
- // So now let's just do a bunch of operations and make sure everything is the
- // same.
- sp_mat dm = M * M.t();
- mat dd = D * D.t();
- CheckMatrices(dm, dd);
- dm = M.t() * M;
- dd = D.t() * D;
- CheckMatrices(dm, dd);
- sp_mat am = M + M;
- mat ad = D + D;
- CheckMatrices(am, ad);
- dm = M + D;
- ad = D + M;
- CheckMatrices(dm, ad);
- }
- TEST_CASE("spmat_dirk_constructor_test2")
- {
- // note the zero at (1,1)
- vec values = "4.0 2.0 0.0 1.0 3.2 1.2 3.5";
- uvec row_indices = "1 3 1 1 2 4 5";
- uvec col_ptrs = "0 2 3 4 5 7";
-
- // Ok, now make a matrix.
- sp_mat M(row_indices, col_ptrs, values, 6, 5);
-
- REQUIRE( M.n_nonzero == 6 );
- // Make the equivalent dense matrix.
- mat D(6, 5);
- D.fill(0);
- D(1, 0) = 4.0;
- D(3, 0) = 2.0;
- D(1, 1) = 0.0;
- D(1, 2) = 1.0;
- D(2, 3) = 3.2;
- D(4, 4) = 1.2;
- D(5, 4) = 3.5;
- // So now let's just do a bunch of operations and make sure everything is the
- // same.
- sp_mat dm = M * M.t();
- mat dd = D * D.t();
- CheckMatrices(dm, dd);
- dm = M.t() * M;
- dd = D.t() * D;
- CheckMatrices(dm, dd);
- sp_mat am = M + M;
- mat ad = D + D;
- CheckMatrices(am, ad);
- dm = M + D;
- ad = D + M;
- CheckMatrices(dm, ad);
- }
- TEST_CASE("spmat_clear_test")
- {
- sp_mat x;
- x.sprandu(10, 10, 0.6);
- x.clear();
- REQUIRE( x.n_cols == 0 );
- REQUIRE( x.n_rows == 0 );
- REQUIRE( x.n_nonzero == 0 );
- }
- TEST_CASE("spmat_batch_insert_zeroes_test")
- {
- Mat<uword> locations(2, 5);
- locations(1, 0) = 1;
- locations(0, 0) = 2;
- locations(1, 1) = 1;
- locations(0, 1) = 7;
- locations(1, 2) = 4;
- locations(0, 2) = 0;
- locations(1, 3) = 4;
- locations(0, 3) = 9;
- locations(1, 4) = 5;
- locations(0, 4) = 0;
- Col<double> values(5);
- values[0] = 1.5;
- values[1] = -15.15;
- values[2] = 2.2;
- values[3] = 0.0;
- values[4] = 5.0;
- SpMat<double> m(locations, values, 10, 10, false, true);
- REQUIRE( m.n_nonzero == 4 );
- REQUIRE( m.n_rows == 10 );
- REQUIRE( m.n_cols == 10 );
- REQUIRE( (double) m(2, 1) == Approx(1.5) );
- REQUIRE( (double) m(7, 1) == Approx(-15.15) );
- REQUIRE( (double) m(0, 4) == Approx(2.2) );
- REQUIRE( (double) m(9, 4) == Approx(1e-5) );
- REQUIRE( (double) m(0, 5) == Approx(5.0) );
- // Auto size detection.
- SpMat<double> n(locations, values, false);
- REQUIRE( n.n_nonzero == 4 );
- REQUIRE( n.n_rows == 10 );
- REQUIRE( n.n_cols == 6 );
- REQUIRE( (double) n(2, 1) == Approx(1.5) );
- REQUIRE( (double) n(7, 1) == Approx(-15.15) );
- REQUIRE( (double) n(0, 4) == Approx(2.2) );
- REQUIRE( (double) n(9, 4) == Approx(1e-5) );
- REQUIRE( (double) n(0, 5) == Approx(5.0) );
- }
- TEST_CASE("spmat_batch_insert_unsorted_case_zeroes")
- {
- Mat<uword> locations(2, 5);
- locations(1, 0) = 4;
- locations(0, 0) = 0;
- locations(1, 1) = 1;
- locations(0, 1) = 2;
- locations(1, 2) = 4;
- locations(0, 2) = 9;
- locations(1, 3) = 5;
- locations(0, 3) = 0;
- locations(1, 4) = 1;
- locations(0, 4) = 7;
- Col<double> values(5);
- values[1] = 1.5;
- values[4] = -15.15;
- values[0] = 2.2;
- values[2] = 0.0;
- values[3] = 5.0;
- SpMat<double> m(locations, values, 10, 10, true);
- REQUIRE( m.n_nonzero == 4 );
- REQUIRE( m.n_rows == 10 );
- REQUIRE( m.n_cols == 10 );
- REQUIRE( (double) m(2, 1) == Approx(1.5) );
- REQUIRE( (double) m(7, 1) == Approx(-15.15) );
- REQUIRE( (double) m(0, 4) == Approx(2.2) );
- REQUIRE( (double) m(9, 4) == Approx(1e-5) );
- REQUIRE( (double) m(0, 5) == Approx(5.0) );
- REQUIRE( m.col_ptrs[11] == std::numeric_limits<uword>::max() );
- // Auto size detection.
- SpMat<double> n(locations, values, true);
- REQUIRE( n.n_nonzero == 4 );
- REQUIRE( n.n_rows == 10 );
- REQUIRE( n.n_cols == 6 );
- REQUIRE( (double) n(2, 1) == Approx(1.5) );
- REQUIRE( (double) n(7, 1) == Approx(-15.15) );
- REQUIRE( (double) n(0, 4) == Approx(2.2) );
- REQUIRE( (double) n(9, 4) == Approx(1e-5) );
- REQUIRE( (double) n(0, 5) == Approx(5.0) );
- REQUIRE( n.col_ptrs[7] == std::numeric_limits<uword>::max() );
- }
- TEST_CASE("spmat_const_row_col_iterator_test")
- {
- mat X;
- X.zeros(5, 5);
- for (uword i = 0; i < 5; ++i)
- {
- X.col(i) += i;
- }
- for (uword i = 0; i < 5; ++i)
- {
- X.row(i) += 3 * i;
- }
- // Make sure default constructor works okay.
- mat::const_row_col_iterator it;
- // Make sure ++ operator, operator* and comparison operators work fine.
- uword count = 0;
- for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
- {
- // Check iterator value.
- REQUIRE( *it == (count % 5) * 3 + (count / 5) );
- // Check iterator position.
- REQUIRE( it.row() == count % 5 );
- REQUIRE( it.col() == count / 5 );
- count++;
- }
- REQUIRE( count == 25 );
- it = X.end_row_col();
- do
- {
- --it;
- count--;
- // Check iterator value.
- REQUIRE( *it == (count % 5) * 3 + (count / 5) );
- // Check iterator position.
- REQUIRE( it.row() == count % 5 );
- REQUIRE( it.col() == count / 5 );
- } while (it != X.begin_row_col());
- REQUIRE( count == 0 );
- }
- TEST_CASE("spmat_row_col_iterator_test")
- {
- mat X;
- X.zeros(5, 5);
- for (uword i = 0; i < 5; ++i)
- {
- X.col(i) += i;
- }
- for (uword i = 0; i < 5; ++i)
- {
- X.row(i) += 3 * i;
- }
- // Make sure default constructor works okay.
- mat::row_col_iterator it;
- // Make sure ++ operator, operator* and comparison operators work fine.
- uword count = 0;
- for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
- {
- // Check iterator value.
- REQUIRE( *it == (count % 5) * 3 + (count / 5) );
- // Check iterator position.
- REQUIRE( it.row() == count % 5 );
- REQUIRE( it.col() == count / 5 );
- count++;
- }
- REQUIRE( count == 25 );
- it = X.end_row_col();
- do
- {
- --it;
- count--;
- // Check iterator value.
- REQUIRE( *it == (count % 5) * 3 + (count / 5) );
- // Check iterator position.
- REQUIRE( it.row() == count % 5 );
- REQUIRE( it.col() == count / 5 );
- } while (it != X.begin_row_col());
- REQUIRE( count == 0 );
- }
- TEST_CASE("spmat_const_sprow_col_iterator_test")
- {
- sp_mat X(5, 5);
- for (uword i = 0; i < 5; ++i)
- {
- X.col(i) += i;
- }
- for (uword i = 0; i < 5; ++i)
- {
- X.row(i) += 3 * i;
- }
- // Make sure default constructor works okay.
- sp_mat::const_row_col_iterator it;
- // Make sure ++ operator, operator* and comparison operators work fine.
- uword count = 1;
- for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
- {
- // Check iterator value.
- REQUIRE( *it == (count % 5) * 3 + (count / 5) );
- // Check iterator position.
- REQUIRE( it.row() == count % 5 );
- REQUIRE( it.col() == count / 5 );
- count++;
- }
- REQUIRE( count == 25 );
- it = X.end_row_col();
- do
- {
- --it;
- count--;
- // Check iterator value.
- REQUIRE( *it == (count % 5) * 3 + (count / 5) );
- // Check iterator position.
- REQUIRE( it.row() == count % 5 );
- REQUIRE( it.col() == count / 5 );
- } while (it != X.begin_row_col());
- REQUIRE( count == 1 );
- }
- TEST_CASE("spmat_sprow_col_iterator_test")
- {
- sp_mat X(5, 5);
- for (uword i = 0; i < 5; ++i)
- {
- X.col(i) += i;
- }
- for (uword i = 0; i < 5; ++i)
- {
- X.row(i) += 3 * i;
- }
- // Make sure default constructor works okay.
- sp_mat::row_col_iterator it;
- // Make sure ++ operator, operator* and comparison operators work fine.
- uword count = 1;
- for (it = X.begin_row_col(); it != X.end_row_col(); ++it)
- {
- // Check iterator value.
- REQUIRE( *it == (count % 5) * 3 + (count / 5) );
- // Check iterator position.
- REQUIRE( it.row() == count % 5 );
- REQUIRE( it.col() == count / 5 );
- count++;
- }
- REQUIRE( count == 25 );
- it = X.end_row_col();
- do
- {
- --it;
- count--;
- // Check iterator value.
- REQUIRE( *it == (count % 5) * 3 + (count / 5) );
- // Check iterator position.
- REQUIRE( it.row() == count % 5 );
- REQUIRE( it.col() == count / 5 );
- } while (it != X.begin_row_col());
- REQUIRE( count == 1 );
- }
- TEST_CASE("spmat_row_iterator_constructor")
- {
- // Create a row iterator with an exact position.
- Mat<double> tmp =
- { { 5.5, 0.0, 0.0 },
- { 0.0, 0.0, 6.5 },
- { 0.0, 7.5, 0.0 } };
- SpMat<double> X(tmp);
- SpMat<double>::const_row_iterator cri(X, 0, 1);
- // This should end up at (1, 2) with value 6.5.
- REQUIRE( cri.row() == 1 );
- REQUIRE( cri.col() == 2 );
- REQUIRE( (*cri) == Approx(6.5) );
- cri = SpMat<double>::const_row_iterator(X, 0, 0);
- // This should end up at (0, 0) with value 5.5.
- REQUIRE( cri.row() == 0 );
- REQUIRE( cri.col() == 0 );
- REQUIRE( (*cri) == Approx(5.5) );
- cri = SpMat<double>::const_row_iterator(X, 2, 1);
- // This should end up at (2, 1) with value 7.5.
- REQUIRE( cri.row() == 2 );
- REQUIRE( cri.col() == 1 );
- REQUIRE( (*cri) == Approx(7.5) );
- }
- // Check that sparse + scalar works.
- TEST_CASE("spmat_scalar_add")
- {
- sp_mat m;
- m.sprandu(100, 200, 0.1);
- mat y = m + 3.0;
- mat z = 3.0 + m;
- for (uword i = 0; i < m.n_cols; ++i)
- {
- for (uword j = 0; j < m.n_rows; ++j)
- {
- REQUIRE(m(j, i) == Approx(z(j, i) - 3));
- REQUIRE(m(j, i) == Approx(y(j, i) - 3));
- }
- }
- }
- // Check that sparse - scalar works.
- TEST_CASE("spmat_scalar_minus")
- {
- sp_mat m;
- m.sprandu(100, 200, 0.1);
- mat y = m - 3.0;
- mat z = 3.0 - m;
- for (uword i = 0; i < m.n_cols; ++i)
- {
- for (uword j = 0; j < m.n_rows; ++j)
- {
- REQUIRE(m(j, i) == Approx(3 - z(j, i)));
- REQUIRE(m(j, i) == Approx(y(j, i) + 3));
- }
- }
- }
- // Check that sparse / (sparse + eps) works. (and also for (sparse - eps) and (eps - sparse).
- TEST_CASE("spmat_div_test")
- {
- sp_mat m;
- m.sprandu(100, 200, 0.1);
- sp_mat m2;
- m2.sprandu(100, 200, 0.5); // higher probability of collision
- sp_mat out = m / (m2 + 1.0);
- sp_mat out2 = m / (m2 - 2.0);
- sp_mat out3 = m / (2.0 - m2);
- REQUIRE(out.n_rows == m.n_rows);
- REQUIRE(out.n_cols == m.n_cols);
- REQUIRE(out.n_nonzero == m.n_nonzero);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- if (m(r, c) != 0.0)
- {
- REQUIRE((m(r, c) / (m2(r, c) + 1.0)) == Approx(out(r, c)));
- REQUIRE((m(r, c) / (m2(r, c) - 2.0)) == Approx(out2(r, c)));
- REQUIRE((m(r, c) / (2.0 - m2(r, c))) == Approx(out3(r, c)));
- }
- else
- {
- REQUIRE(out(r, c) == 0.0);
- REQUIRE(out2(r, c) == 0.0);
- REQUIRE(out3(r, c) == 0.0);
- }
- }
- }
- }
- // Check that sparse % (sparse + eps) works. (and also for (sparse - eps) and (eps - sparse).
- TEST_CASE("spmat_schur_test")
- {
- sp_mat m;
- m.sprandu(100, 200, 0.1);
- sp_mat m2;
- m2.sprandu(100, 200, 0.5); // higher probability of collision
- sp_mat out = m % (m2 + 1.0);
- sp_mat out2 = m % (m2 - 2.0);
- sp_mat out3 = m % (2.0 - m2);
- REQUIRE(out.n_rows == m.n_rows);
- REQUIRE(out.n_cols == m.n_cols);
- REQUIRE(out.n_nonzero == m.n_nonzero);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- if (m(r, c) != 0.0)
- {
- REQUIRE((m(r, c) * (m2(r, c) + 1.0)) == Approx(out(r, c)));
- REQUIRE((m(r, c) * (m2(r, c) - 2.0)) == Approx(out2(r, c)));
- REQUIRE((m(r, c) * (2.0 - m2(r, c))) == Approx(out3(r, c)));
- }
- else
- {
- REQUIRE(out(r, c) == 0.0);
- REQUIRE(out2(r, c) == 0.0);
- REQUIRE(out3(r, c) == 0.0);
- }
- }
- }
- }
- // Make sure this compiles and works.
- TEST_CASE("spmat_repeated_add_subtract")
- {
- sp_mat m;
- m.sprandu(100, 200, 0.1);
- // p: plus, m: minus, n: pre-minus
- mat out_pp = m + 3 + 3;
- mat out_pm = m + 3 - 3;
- mat out_pn = 3 - (m + 3);
- mat out_mp = m - 3 + 3;
- mat out_mm = m - 3 - 3;
- mat out_mn = 3 - (m - 3);
- mat out_np = (3 - m) + 3;
- mat out_nm = (3 - m) - 3;
- mat out_nn = 3 - (3 - m);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- REQUIRE(out_pp(r, c) == Approx(m(r, c) + 6));
- REQUIRE(out_pm(r, c) == Approx(m(r, c)));
- REQUIRE(out_pn(r, c) == Approx(-m(r, c)));
- REQUIRE(out_mp(r, c) == Approx(m(r, c)));
- REQUIRE(out_mm(r, c) == Approx(m(r, c) - 6));
- REQUIRE(out_mn(r, c) == Approx(6 - m(r, c)));
- REQUIRE(out_np(r, c) == Approx(6 - m(r, c)));
- REQUIRE(out_nm(r, c) == Approx(-m(r, c)));
- REQUIRE(out_nn(r, c) == Approx(m(r, c)));
- }
- }
- }
- // If we wrap an sp_mat() constructor around a (sparse + plus) it should force
- // evaluate into a sparse matrix.
- TEST_CASE("spmat_force_plus_minus_sparse")
- {
- // We can't test that our desired optimization is used but we can test that it
- // compiles.
- sp_mat m;
- m.sprandu(100, 200, 0.1);
- sp_mat out1(m + 1);
- sp_mat out2(m - 1);
- sp_mat out3(2 - m);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- REQUIRE(out1(r, c) == Approx(m(r, c) + 1));
- REQUIRE(out2(r, c) == Approx(m(r, c) - 1));
- REQUIRE(out3(r, c) == Approx(2 - m(r, c)));
- }
- }
- }
- // Test elementwise max().
- TEST_CASE("spmat_elementwise_max")
- {
- sp_mat m, n;
- m.sprandu(100, 200, 0.1);
- n.sprandu(100, 200, 0.2);
- sp_mat out = max(m, n);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- REQUIRE(out(r, c) == Approx(std::max((double) m(r, c), (double) n(r, c))));
- }
- }
- }
- // Test elementwise max() with a dense object.
- TEST_CASE("spmat_mat_elementwise_max")
- {
- sp_mat m;
- mat n;
- m.sprandu(100, 200, 0.1);
- n.randu(100, 200);
- n -= 0.5;
- mat out1 = max(m, n);
- mat out2 = max(n, m);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- REQUIRE(out1(r, c) == Approx(std::max((double) m(r, c), (double) n(r, c))));
- REQUIRE(out2(r, c) == Approx(std::max((double) m(r, c), (double) n(r, c))));
- }
- }
- }
- // Test elementwise complex max().
- TEST_CASE("spmat_elementwise_max_cx")
- {
- sp_cx_mat m, n;
- m.sprandu(100, 200, 0.1);
- n.sprandu(100, 200, 0.2);
- sp_cx_mat out = arma::max(m, n);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- if (std::abs(std::complex<double>(m(r, c))) > std::abs(std::complex<double>(n(r, c))))
- REQUIRE(std::abs(std::complex<double>(out(r, c)) - std::complex<double>(m(r, c))) == Approx(0.0));
- else
- REQUIRE(std::abs(std::complex<double>(out(r, c)) - std::complex<double>(n(r, c))) == Approx(0.0));
- }
- }
- }
- // Test elementwise min().
- TEST_CASE("spmat_elementwise_min")
- {
- sp_mat m, n;
- m.sprandu(100, 200, 0.1);
- n.sprandu(100, 200, 0.2);
- sp_mat out = min(m, n);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- REQUIRE(out(r, c) == Approx(std::min((double) m(r, c), (double) n(r, c))));
- }
- }
- }
- // Test elementwise min() with a dense object.
- TEST_CASE("spmat_mat_elementwise_min")
- {
- sp_mat m;
- mat n;
- m.sprandu(100, 200, 0.1);
- n.randu(100, 200);
- n -= 0.5;
- mat out1 = min(m, n);
- mat out2 = min(n, m);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- REQUIRE(out1(r, c) == Approx(std::min((double) m(r, c), (double) n(r, c))));
- REQUIRE(out2(r, c) == Approx(std::min((double) m(r, c), (double) n(r, c))));
- }
- }
- }
- // Test elementwise complex min().
- TEST_CASE("spmat_elementwise_min_cx")
- {
- sp_cx_mat m, n;
- m.sprandu(100, 200, 0.1);
- n.sprandu(100, 200, 0.2);
- sp_cx_mat out = arma::min(m, n);
- for (uword c = 0; c < m.n_cols; ++c)
- {
- for (uword r = 0; r < m.n_rows; ++r)
- {
- if (std::abs(std::complex<double>(m(r, c))) < std::abs(std::complex<double>(n(r, c))))
- REQUIRE(std::abs(std::complex<double>(out(r, c)) - std::complex<double>(m(r, c))) == Approx(0.0));
- else
- REQUIRE(std::abs(std::complex<double>(out(r, c)) - std::complex<double>(n(r, c))) == Approx(0.0));
- }
- }
- }
- // Test vectorise() on a matrix.
- TEST_CASE("spmat_vectorise_matrix")
- {
- sp_mat m;
- m.sprandu(10, 10, 0.1);
- sp_vec c = vectorise(m);
- sp_mat d = vectorise(m);
- sp_rowvec e = vectorise(m).t();
- for (uword i = 0; i < c.n_elem; ++i)
- {
- REQUIRE(c[i] == Approx(m[i]));
- REQUIRE(d[i] == Approx(m[i]));
- REQUIRE(e[i] == Approx(m[i]));
- }
- }
- // Test vectorise() as an alias.
- TEST_CASE("spmat_vectorise_alias")
- {
- sp_mat m;
- m.sprandu(10, 10, 0.1);
- sp_mat n(m);
- n = vectorise(n);
- REQUIRE(n.n_rows == 100);
- REQUIRE(n.n_cols == 1);
- for (uword i = 0; i < n.n_elem; ++i)
- {
- REQUIRE(n[i] == Approx(m[i]));
- }
- }
- // Test vectorise() with the dimension argument.
- TEST_CASE("spmat_vectorise_dimension")
- {
- sp_mat m;
- m.sprandu(10, 10, 0.1);
- sp_mat n = m.t();
- sp_vec c = vectorise(m, 0);
- sp_rowvec d = vectorise(m, 1);
- sp_rowvec e = vectorise(m.t(), 1);
- sp_vec f = vectorise(m.t(), 0);
- for (uword i = 0; i < m.n_elem; ++i)
- {
- REQUIRE(c[i] == Approx(m[i]));
- REQUIRE(d[i] == Approx(n[i]));
- REQUIRE(e[i] == Approx(m[i]));
- REQUIRE(f[i] == Approx(n[i]));
- }
- }
- // Test vectorise() with an alias and a dimension argument.
- TEST_CASE("spmat_vectorise_dimension_alias")
- {
- sp_mat m;
- m.sprandu(10, 10, 0.1);
- sp_mat n(m);
- m = arma::vectorise(m, 0);
- REQUIRE(m.n_rows == 100);
- REQUIRE(m.n_cols == 1);
- for (uword i = 0; i < m.n_elem; ++i)
- {
- REQUIRE(m[i] == Approx(n[i]));
- }
- m.sprandu(10, 10, 0.1);
- n = m.t();
- m = arma::vectorise(m, 1);
- REQUIRE(m.n_rows == 1);
- REQUIRE(m.n_cols == 100);
- for (uword i = 0; i < m.n_elem; ++i)
- {
- REQUIRE(m[i] == Approx(n[i]));
- }
- }
|