12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538 |
- // 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;
- TEST_CASE("sp_subview_tests")
- {
- Mat<double> ref(4,4);
- ref.eye(4,4);
- SpMat<double> X(4,4);
- X.eye(4,4);
- /*
- * [[1,0,0,0] [[2,0,0,0]
- * [0,1,0,0] -> [0,2,0,0]
- * [0,0,1,0] [0,0,2,0]
- * [0,0,0,1]] [0,0,0,1]]
- */
- ref.submat(0, 0, 2, 2) *= 2;
- X.submat(0, 0, 2, 2) *= 2;
- for (uword i = 0; i < 4; i++)
- {
- for (uword j = 0; j < 4; j++)
- {
- REQUIRE( (double) ref(i, j) == Approx((double) X(i, j)) );
- }
- }
- /*
- * [[2,0,0,0] [[2,0,0,0]
- * [0,2,0,0] -> [0,1,0,0]
- * [0,0,2,0] [0,0,1,0]
- * [0,0,0,1]] [0,0,0,.5]]
- */
- ref.submat(1, 1, 3, 3) /= 2;
- X.submat(1, 1, 3, 3) /= 2;
- for (uword i = 0; i < 4; i++)
- {
- for (uword j = 0; j < 4; j++)
- {
- REQUIRE( (double) ref(i, j) == Approx((double) X(i, j)) );
- }
- }
- span s(1, 2);
- ref.submat(s, s) += 10;
- X.submat(s, s) += 10;
- for (uword i = 0; i < 4; i++)
- {
- for (uword j = 0; j < 4; j++)
- {
- REQUIRE( (double) ref(i, j) == Approx((double) X(i, j)) );
- }
- }
- }
- TEST_CASE("sp_subview_const_test")
- {
- Mat<double> ref(4, 4);
- ref.eye(4, 4);
- SpMat<double> X(4, 4);
- X.eye(4, 4);
- const SpSubview<double> subX = X.submat(span(0, 2), span::all);
- /*
- X.print("x");
- for (size_t i = 0; i < 3; ++i)
- {
- for (size_t j = 0; j < 4; ++j)
- {
- printf("%f ", subX(i, j));
- }
- printf("\n");
- }
- */
- }
- TEST_CASE("sp_subview_multiplication_test")
- {
- // Ensure matrix multiplication with subviews works correctly.
- SpMat<double> a(2, 5);
- SpMat<double> b(5, 2);
- a(1, 3) = 1;
- a(0, 0) = 2;
- a(1, 2) = 1.5;
- b(4, 1) = 3;
- b(3, 0) = 2;
- b(1, 0) = 1;
- b(0, 0) = 0.6;
- b *= a;
- REQUIRE( b.n_cols == 5 );
- REQUIRE( b.n_rows == 5 );
- REQUIRE( b.n_elem == 25 );
- REQUIRE( b.n_nonzero == 5 );
- REQUIRE( (double) b(0, 0) == Approx(1.2) );
- REQUIRE( (double) b(0, 1) == Approx(1e-5) );
- REQUIRE( (double) b(0, 2) == Approx(1e-5) );
- REQUIRE( (double) b(0, 3) == Approx(1e-5) );
- REQUIRE( (double) b(0, 4) == Approx(1e-5) );
- REQUIRE( (double) b(1, 0) == Approx(2.0) );
- REQUIRE( (double) b(1, 1) == Approx(1e-5) );
- REQUIRE( (double) b(1, 2) == Approx(1e-5) );
- REQUIRE( (double) b(1, 3) == Approx(1e-5) );
- REQUIRE( (double) b(1, 4) == Approx(1e-5) );
- REQUIRE( (double) b(2, 0) == Approx(1e-5) );
- REQUIRE( (double) b(2, 1) == Approx(1e-5) );
- REQUIRE( (double) b(2, 2) == Approx(1e-5) );
- REQUIRE( (double) b(2, 3) == Approx(1e-5) );
- REQUIRE( (double) b(2, 4) == Approx(1e-5) );
- REQUIRE( (double) b(3, 0) == Approx(4.0) );
- REQUIRE( (double) b(3, 1) == Approx(1e-5) );
- REQUIRE( (double) b(3, 2) == Approx(1e-5) );
- REQUIRE( (double) b(3, 3) == Approx(1e-5) );
- REQUIRE( (double) b(3, 4) == Approx(1e-5) );
- REQUIRE( (double) b(4, 0) == Approx(1e-5) );
- REQUIRE( (double) b(4, 1) == Approx(1e-5) );
- REQUIRE( (double) b(4, 2) == Approx(4.5) );
- REQUIRE( (double) b(4, 3) == Approx(3.0) );
- REQUIRE( (double) b(4, 4) == Approx(1e-5) );
- }
- TEST_CASE("sp_subview_multiplication_test_2")
- {
- // Ensure matrix multiplication with subviews works correctly.
- SpMat<double> a(4, 5);
- SpMat<double> b(5, 2);
- a(2, 3) = 1;
- a(3, 1) = 1.4;
- a(0, 0) = 2;
- a(1, 0) = 2;
- a(2, 2) = 1.5;
- b(4, 1) = 3;
- b(3, 0) = 2;
- b(1, 0) = 1;
- b(0, 0) = 0.6;
- b *= a.rows(1, 2);
- REQUIRE( b.n_cols == 5 );
- REQUIRE( b.n_rows == 5 );
- REQUIRE( b.n_elem == 25 );
- REQUIRE( b.n_nonzero == 5 );
- REQUIRE( (double) b(0, 0) == Approx(1.2) );
- REQUIRE( (double) b(0, 1) == Approx(1e-5) );
- REQUIRE( (double) b(0, 2) == Approx(1e-5) );
- REQUIRE( (double) b(0, 3) == Approx(1e-5) );
- REQUIRE( (double) b(0, 4) == Approx(1e-5) );
- REQUIRE( (double) b(1, 0) == Approx(2.0) );
- REQUIRE( (double) b(1, 1) == Approx(1e-5) );
- REQUIRE( (double) b(1, 2) == Approx(1e-5) );
- REQUIRE( (double) b(1, 3) == Approx(1e-5) );
- REQUIRE( (double) b(1, 4) == Approx(1e-5) );
- REQUIRE( (double) b(2, 0) == Approx(1e-5) );
- REQUIRE( (double) b(2, 1) == Approx(1e-5) );
- REQUIRE( (double) b(2, 2) == Approx(1e-5) );
- REQUIRE( (double) b(2, 3) == Approx(1e-5) );
- REQUIRE( (double) b(2, 4) == Approx(1e-5) );
- REQUIRE( (double) b(3, 0) == Approx(4.0) );
- REQUIRE( (double) b(3, 1) == Approx(1e-5) );
- REQUIRE( (double) b(3, 2) == Approx(1e-5) );
- REQUIRE( (double) b(3, 3) == Approx(1e-5) );
- REQUIRE( (double) b(3, 4) == Approx(1e-5) );
- REQUIRE( (double) b(4, 0) == Approx(1e-5) );
- REQUIRE( (double) b(4, 1) == Approx(1e-5) );
- REQUIRE( (double) b(4, 2) == Approx(4.5) );
- REQUIRE( (double) b(4, 3) == Approx(3.0) );
- REQUIRE( (double) b(4, 4) == Approx(1e-5) );
- }
- TEST_CASE("sp_subview_unary_operators_test")
- {
- SpMat<int> a(3, 3);
- SpMat<int> b(5, 5);
- a(0, 0) = 1;
- a(1, 2) = 4;
- a(2, 2) = 5;
- b(2, 3) = 1;
- b(3, 2) = 2;
- b(3, 4) = -4;
- b(4, 4) = 5;
- SpMat<int> c = a + b.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- 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(2, 2) = 2;
- /*
- c = a / b.submat(2, 2, 4, 4);
- 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("sp_subview_mat_operator_tests")
- {
- SpMat<double> a(6, 10);
- a(2, 2) = 2.0;
- a(3, 4) = 3.5;
- a(4, 3) = -2.0;
- a(4, 4) = 4.5;
- a(5, 1) = 3.2;
- a(0, 1) = 1.3;
- a(1, 1) = -4.0;
- a(5, 3) = 5.3;
- mat b(3, 3);
- b.fill(2.0);
- mat c(b);
- c += a.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- 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.submat(2, 2, 4, 4);
- REQUIRE( d.n_nonzero == 4 );
- REQUIRE( (double) d(0, 0) == Approx(4.0) );
- REQUIRE( (double) d(2, 1) == Approx(-4.0) );
- REQUIRE( (double) d(1, 2) == Approx(7.0) );
- REQUIRE( (double) d(2, 2) == Approx(9.0) );
- c = b;
- c /= a.submat(2, 2, 4, 4);
- 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("sp_subview_base_test")
- {
- SpMat<double> a(6, 10);
- a(2, 2) = 2.0;
- a(3, 4) = 3.5;
- a(4, 3) = -2.0;
- a(4, 4) = 4.5;
- a(5, 1) = 3.2;
- a(0, 1) = 1.3;
- a(1, 1) = -4.0;
- a(5, 3) = 5.3;
- mat b(3, 3);
- b.fill(2.0);
- SpMat<double> c = a;
- c.submat(2, 2, 4, 4) = b;
- REQUIRE( c.n_nonzero == 13 );
- REQUIRE( (double) c(2, 2) == Approx(2.0) );
- REQUIRE( (double) c(3, 2) == Approx(2.0) );
- REQUIRE( (double) c(4, 2) == Approx(2.0) );
- REQUIRE( (double) c(2, 3) == Approx(2.0) );
- REQUIRE( (double) c(3, 3) == Approx(2.0) );
- REQUIRE( (double) c(4, 3) == Approx(2.0) );
- REQUIRE( (double) c(2, 4) == Approx(2.0) );
- REQUIRE( (double) c(3, 4) == Approx(2.0) );
- REQUIRE( (double) c(4, 4) == Approx(2.0) );
- c = a;
- c.submat(2, 2, 4, 4) += b;
- REQUIRE( c.n_nonzero == 12 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(3, 2) == Approx(2.0) );
- REQUIRE( (double) c(4, 2) == Approx(2.0) );
- REQUIRE( (double) c(2, 3) == Approx(2.0) );
- REQUIRE( (double) c(3, 3) == Approx(2.0) );
- REQUIRE( (double) c(4, 3) == Approx(1e-5) );
- REQUIRE( (double) c(2, 4) == Approx(2.0) );
- REQUIRE( (double) c(3, 4) == Approx(5.5) );
- REQUIRE( (double) c(4, 4) == Approx(6.5) );
- Mat<double> d = a.submat(2, 2, 4, 4) + b;
- c = a.submat(2, 2, 4, 4) + b;
- REQUIRE( c.n_nonzero == 8 );
- 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 = a;
- c.submat(2, 2, 4, 4) -= b;
- REQUIRE( c.n_nonzero == 12 );
- REQUIRE( (double) c(2, 2) == Approx(1e-5) );
- REQUIRE( (double) c(3, 2) == Approx(-2.0) );
- REQUIRE( (double) c(4, 2) == Approx(-2.0) );
- REQUIRE( (double) c(2, 3) == Approx(-2.0) );
- REQUIRE( (double) c(3, 3) == Approx(-2.0) );
- REQUIRE( (double) c(4, 3) == Approx(-4.0) );
- REQUIRE( (double) c(2, 4) == Approx(-2.0) );
- REQUIRE( (double) c(3, 4) == Approx(1.5) );
- REQUIRE( (double) c(4, 4) == Approx(2.5) );
- c = a.submat(2, 2, 4, 4) - b;
- REQUIRE( c.n_nonzero == 8 );
- 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 = a;
- c.submat(2, 2, 4, 4) *= b;
- REQUIRE( c.n_nonzero == 13 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(3, 2) == Approx(7.0) );
- REQUIRE( (double) c(4, 2) == Approx(5.0) );
- REQUIRE( (double) c(2, 3) == Approx(4.0) );
- REQUIRE( (double) c(3, 3) == Approx(7.0) );
- REQUIRE( (double) c(4, 3) == Approx(5.0) );
- REQUIRE( (double) c(2, 4) == Approx(4.0) );
- REQUIRE( (double) c(3, 4) == Approx(7.0) );
- REQUIRE( (double) c(4, 4) == Approx(5.0) );
- c = a.submat(2, 2, 4, 4) * b;
- REQUIRE( c.n_nonzero == 9 );
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 0) == Approx(7.0) );
- REQUIRE( (double) c(2, 0) == Approx(5.0) );
- REQUIRE( (double) c(0, 1) == Approx(4.0) );
- REQUIRE( (double) c(1, 1) == Approx(7.0) );
- REQUIRE( (double) c(2, 1) == Approx(5.0) );
- REQUIRE( (double) c(0, 2) == Approx(4.0) );
- REQUIRE( (double) c(1, 2) == Approx(7.0) );
- REQUIRE( (double) c(2, 2) == Approx(5.0) );
- c = a.submat(2, 2, 4, 4) % b;
- REQUIRE( c.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 = a;
- c.submat(2, 2, 4, 4) %= b;
- REQUIRE( c.n_nonzero == 8 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(4, 3) == Approx(-4.0) );
- REQUIRE( (double) c(3, 4) == Approx(7.0) );
- REQUIRE( (double) c(4, 4) == Approx(9.0) );
- c = a.submat(2, 2, 4, 4) / b;
- REQUIRE( c.n_nonzero == 4 );
- REQUIRE( (double) c(0, 0) == Approx(1.0) );
- REQUIRE( (double) c(2, 1) == Approx(-1.0) );
- REQUIRE( (double) c(1, 2) == Approx(3.5 / 2.0) );
- REQUIRE( (double) c(2, 2) == Approx(4.5 / 2.0) );
- c = a;
- c.submat(2, 2, 4, 4) /= b;
- REQUIRE( c.n_nonzero == 8 );
- REQUIRE( (double) c(2, 2) == Approx(1.0) );
- REQUIRE( (double) c(4, 3) == Approx(-1.0) );
- REQUIRE( (double) c(3, 4) == Approx(3.5 / 2.0) );
- REQUIRE( (double) c(4, 4) == Approx(4.5 / 2.0) );
- }
- TEST_CASE("sp_subview_sp_mat_test")
- {
- SpMat<double> a(6, 10);
- a(2, 2) = 2.0;
- a(3, 4) = 3.5;
- a(4, 3) = -2.0;
- a(4, 4) = 4.5;
- a(5, 1) = 3.2;
- a(0, 1) = 1.3;
- a(1, 1) = -4.0;
- a(5, 3) = 5.3;
- SpMat<double> b(3, 3);
- b(0, 0) = 2.0;
- b(1, 2) = 1.5;
- b(2, 1) = 2.0;
- SpMat<double> c = a;
- c.submat(2, 2, 4, 4) = b;
- REQUIRE( c.n_nonzero == 7 );
- REQUIRE( (double) c(2, 2) == Approx(2.0) );
- REQUIRE( (double) c(3, 4) == Approx(1.5) );
- REQUIRE( (double) c(4, 3) == Approx(2.0) );
- c = a;
- c.submat(2, 2, 4, 4) += b;
- REQUIRE( c.n_nonzero == 7 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(3, 4) == Approx(5.0) );
- REQUIRE( (double) c(4, 4) == Approx(4.5) );
- c = a.submat(2, 2, 4, 4) + b;
- REQUIRE( c.n_nonzero == 3 );
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 2) == Approx(5.0) );
- REQUIRE( (double) c(2, 2) == Approx(4.5) );
- c = a;
- c.submat(2, 2, 4, 4) -= b;
- REQUIRE( c.n_nonzero == 7 );
- REQUIRE( (double) c(2, 2) == Approx(1e-5) );
- REQUIRE( (double) c(3, 2) == Approx(1e-5) );
- REQUIRE( (double) c(4, 2) == Approx(1e-5) );
- REQUIRE( (double) c(2, 3) == Approx(1e-5) );
- REQUIRE( (double) c(3, 3) == Approx(1e-5) );
- REQUIRE( (double) c(4, 3) == Approx(-4.0) );
- REQUIRE( (double) c(2, 4) == Approx(1e-5) );
- REQUIRE( (double) c(3, 4) == Approx(2.0) );
- REQUIRE( (double) c(4, 4) == Approx(4.5) );
- c = a.submat(2, 2, 4, 4) - b;
- REQUIRE( c.n_nonzero == 3 );
- REQUIRE( (double) c(2, 1) == Approx(-4.0) );
- REQUIRE( (double) c(1, 2) == Approx(2.0) );
- REQUIRE( (double) c(2, 2) == Approx(4.5) );
- c = a;
- c.submat(2, 2, 4, 4) *= b;
- REQUIRE( c.n_nonzero == 8 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(3, 3) == Approx(7.0) );
- REQUIRE( (double) c(4, 3) == Approx(9.0) );
- REQUIRE( (double) c(4, 4) == Approx(-3.0) );
- c = a.submat(2, 2, 4, 4) * b;
- REQUIRE( c.n_nonzero == 4 );
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 1) == Approx(7.0) );
- REQUIRE( (double) c(2, 1) == Approx(9.0) );
- REQUIRE( (double) c(2, 2) == Approx(-3.0) );
- c = a.submat(2, 2, 4, 4) % b;
- REQUIRE( c.n_nonzero == 3 );
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(2, 1) == Approx(-4.0) );
- REQUIRE( (double) c(1, 2) == Approx(5.25) );
- REQUIRE( (double) c(2, 2) == Approx(1e-5) );
- c = a;
- c.submat(2, 2, 4, 4) %= b;
- REQUIRE( c.n_nonzero == 7 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(4, 3) == Approx(-4.0) );
- REQUIRE( (double) c(3, 4) == Approx(5.25) );
- REQUIRE( (double) c(4, 4) == Approx(1e-5) );
- // c = a.submat(2, 2, 4, 4) / b;
- // REQUIRE( c.n_nonzero == 9 );
- // REQUIRE( (double) c(0, 0) == Approx(1.0) );
- // REQUIRE( (double) c(1, 0) != (double) c(1, 0) );
- // REQUIRE( (double) c(2, 0) != (double) c(2, 0) );
- // REQUIRE( (double) c(0, 1) != (double) c(0, 1) );
- // REQUIRE( (double) c(1, 1) != (double) c(1, 1) );
- // REQUIRE( (double) c(2, 1) == Approx(-1.0) );
- // REQUIRE( (double) c(0, 2) != (double) c(0, 2) );
- // REQUIRE( (double) c(1, 2) == Approx((3.5 / 1.5)) );
- // REQUIRE( std::isinf((double) c(2, 2)) );
- c = a;
- c.submat(2, 2, 4, 4) /= b;
- REQUIRE( c.n_nonzero == 13 );
- REQUIRE( (double) c(2, 2) == Approx(1.0) );
- REQUIRE( (double) c(3, 2) != (double) c(3, 2));
- REQUIRE( (double) c(4, 2) != (double) c(4, 2));
- REQUIRE( (double) c(2, 3) != (double) c(2, 3));
- REQUIRE( (double) c(3, 3) != (double) c(3, 3));
- REQUIRE( (double) c(4, 3) == Approx(-1.0) );
- REQUIRE( (double) c(2, 4) != (double) c(2, 4) );
- REQUIRE( (double) c(3, 4) == Approx((3.5 / 1.5)) );
- REQUIRE( std::isinf((double) c(4, 4)) );
- }
- TEST_CASE("sp_subview_sp_subview_tests")
- {
- SpMat<double> a(6, 10);
- a(2, 2) = 2.0;
- a(3, 4) = 3.5;
- a(4, 3) = -2.0;
- a(4, 4) = 4.5;
- a(5, 1) = 3.2;
- a(0, 1) = 1.3;
- a(1, 1) = -4.0;
- a(5, 3) = 5.3;
- SpMat<double> b(5, 5);
- b(0, 0) = 1.0;
- b(0, 1) = 1.0;
- b(0, 2) = 1.0;
- b(0, 3) = 1.0;
- b(0, 4) = 1.0;
- b(1, 0) = 1.0;
- b(2, 0) = 1.0;
- b(3, 0) = 1.0;
- b(4, 0) = 1.0;
- b(4, 1) = 1.0;
- b(4, 2) = 1.0;
- b(4, 3) = 1.0;
- b(4, 4) = 1.0;
- b(3, 4) = 1.0;
- b(2, 4) = 1.0;
- b(1, 4) = 1.0;
- b(1, 1) = 2.0;
- b(2, 3) = 1.5;
- b(3, 2) = 2.0;
- SpMat<double> c = a;
- c.submat(2, 2, 4, 4) = b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 7 );
- REQUIRE( (double) c(2, 2) == Approx(2.0) );
- REQUIRE( (double) c(3, 4) == Approx(1.5) );
- REQUIRE( (double) c(4, 3) == Approx(2.0) );
- c = a;
- c.submat(2, 2, 4, 4) += b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 7 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(3, 4) == Approx(5.0) );
- REQUIRE( (double) c(4, 4) == Approx(4.5) );
- c = a.submat(2, 2, 4, 4) + b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 3 );
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 2) == Approx(5.0) );
- REQUIRE( (double) c(2, 2) == Approx(4.5) );
- c = a;
- c.submat(2, 2, 4, 4) -= b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 7 );
- REQUIRE( (double) c(2, 2) == Approx(1e-5) );
- REQUIRE( (double) c(3, 2) == Approx(1e-5) );
- REQUIRE( (double) c(4, 2) == Approx(1e-5) );
- REQUIRE( (double) c(2, 3) == Approx(1e-5) );
- REQUIRE( (double) c(3, 3) == Approx(1e-5) );
- REQUIRE( (double) c(4, 3) == Approx(-4.0) );
- REQUIRE( (double) c(2, 4) == Approx(1e-5) );
- REQUIRE( (double) c(3, 4) == Approx(2.0) );
- REQUIRE( (double) c(4, 4) == Approx(4.5) );
- c = a.submat(2, 2, 4, 4) - b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 3 );
- REQUIRE( (double) c(2, 1) == Approx(-4.0) );
- REQUIRE( (double) c(1, 2) == Approx(2.0) );
- REQUIRE( (double) c(2, 2) == Approx(4.5) );
- c = a;
- c.submat(2, 2, 4, 4) *= b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 8 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(3, 3) == Approx(7.0) );
- REQUIRE( (double) c(4, 3) == Approx(9.0) );
- REQUIRE( (double) c(4, 4) == Approx(-3.0) );
- c = a.submat(2, 2, 4, 4) * b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 4 );
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(1, 1) == Approx(7.0) );
- REQUIRE( (double) c(2, 1) == Approx(9.0) );
- REQUIRE( (double) c(2, 2) == Approx(-3.0) );
- c = a.submat(2, 2, 4, 4) % b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 3 );
- REQUIRE( (double) c(0, 0) == Approx(4.0) );
- REQUIRE( (double) c(2, 1) == Approx(-4.0) );
- REQUIRE( (double) c(1, 2) == Approx(5.25) );
- REQUIRE( (double) c(2, 2) == Approx(1e-5) );
- c = a;
- c.submat(2, 2, 4, 4) %= b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 7 );
- REQUIRE( (double) c(2, 2) == Approx(4.0) );
- REQUIRE( (double) c(4, 3) == Approx(-4.0) );
- REQUIRE( (double) c(3, 4) == Approx(5.25) );
- REQUIRE( (double) c(4, 4) == Approx(1e-5) );
- // c = a.submat(2, 2, 4, 4) / b.submat(1, 1, 3, 3);
- // REQUIRE( c.n_nonzero == 9 );
- // REQUIRE( (double) c(0, 0) == Approx(1.0) );
- // REQUIRE( (double) c(1, 0) != (double) c(1, 0) );
- // REQUIRE( (double) c(2, 0) != (double) c(2, 0) );
- // REQUIRE( (double) c(0, 1) != (double) c(0, 1) );
- // REQUIRE( (double) c(1, 1) != (double) c(1, 1) );
- // REQUIRE( (double) c(2, 1) == Approx(-1.0) );
- // REQUIRE( (double) c(0, 2) != (double) c(0, 2) );
- // REQUIRE( (double) c(1, 2) == Approx((3.5 / 1.5)) );
- // REQUIRE( std::isinf((double) c(2, 2)) );
- c = a;
- c.submat(2, 2, 4, 4) /= b.submat(1, 1, 3, 3);
- REQUIRE( c.n_nonzero == 13 );
- REQUIRE( (double) c(2, 2) == Approx(1.0) );
- REQUIRE( (double) c(3, 2) != (double) c(3, 2) );
- REQUIRE( (double) c(4, 2) != (double) c(4, 2) );
- REQUIRE( (double) c(2, 3) != (double) c(2, 3) );
- REQUIRE( (double) c(3, 3) != (double) c(3, 3) );
- REQUIRE( (double) c(4, 3) == Approx(-1.0) );
- REQUIRE( (double) c(2, 4) != (double) c(2, 4) );
- REQUIRE( (double) c(3, 4) == Approx((3.5 / 1.5)) );
- REQUIRE( std::isinf((double) c(4, 4)) );
- }
- TEST_CASE("sp_subview_iterators_test")
- {
- SpMat<double> b(5, 5);
- b(0, 0) = 1.0;
- b(0, 1) = 1.0;
- b(0, 2) = 1.0;
- b(0, 3) = 1.0;
- b(0, 4) = 1.0;
- b(1, 0) = 1.0;
- b(2, 0) = 1.0;
- b(3, 0) = 1.0;
- b(4, 0) = 1.0;
- b(4, 1) = 1.0;
- b(4, 2) = 1.0;
- b(4, 3) = 1.0;
- b(4, 4) = 1.0;
- b(3, 4) = 1.0;
- b(2, 4) = 1.0;
- b(1, 4) = 1.0;
- b(1, 1) = 2.0;
- b(2, 3) = 1.5;
- b(3, 2) = 2.0;
- // [[1.0 1.0 1.0 1.0 1.0]
- // [1.0 2.0 0.0 0.0 1.0]
- // [1.0 0.0 0.0 1.5 1.0]
- // [1.0 0.0 2.0 0.0 1.0]
- // [1.0 1.0 1.0 1.0 1.0]]
- SpSubview<double> s = b.submat(1, 1, 3, 3);
- SpSubview<double>::iterator it = s.begin();
- REQUIRE( it.pos() == 0 );
- REQUIRE( it.skip_pos == 6 );
- REQUIRE( it.row() == 0 );
- REQUIRE( it.col() == 0 );
- REQUIRE( (double) (*it) == Approx(2.0) );
- ++it;
- REQUIRE( it.pos() == 1 );
- REQUIRE( it.skip_pos == 8 );
- REQUIRE( it.row() == 2 );
- REQUIRE( it.col() == 1 );
- REQUIRE( (double) (*it) == Approx(2.0) );
- ++it;
- REQUIRE( it.pos() == 2 );
- REQUIRE( it.skip_pos == 10 );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 2 );
- REQUIRE( (double) (*it) == Approx(1.5) );
- *it = 4.3;
- REQUIRE( (double) (*it) == Approx(4.3) );
- ++it;
- REQUIRE( it.pos() == s.n_nonzero );
- --it;
- REQUIRE( it.pos() == 2 );
- REQUIRE( it.skip_pos == 10 );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 2 );
- REQUIRE( (double) (*it) == Approx(4.3) );
- --it;
- REQUIRE( it.pos() == 1 );
- REQUIRE( it.skip_pos == 8 );
- REQUIRE( it.row() == 2 );
- REQUIRE( it.col() == 1 );
- REQUIRE( (double) (*it) == Approx(2.0) );
- --it;
- REQUIRE( it.pos() == 0 );
- REQUIRE( it.skip_pos == 6 );
- REQUIRE( it.row() == 0 );
- REQUIRE( it.col() == 0 );
- REQUIRE( (double) (*it) == Approx(2.0) );
- SpMat<double> c(5, 5);
- c(1, 1) = 2.0;
- c(2, 3) = 1.5;
- c(3, 2) = 2.0;
- SpSubview<double> ss = c.submat(1, 1, 3, 3);
- SpSubview<double>::iterator sit = ss.begin();
- REQUIRE( sit.pos() == 0 );
- REQUIRE( sit.skip_pos == 0 );
- REQUIRE( sit.row() == 0 );
- REQUIRE( sit.col() == 0 );
- REQUIRE( (double) (*sit) == Approx(2.0) );
- ++sit;
- REQUIRE( sit.pos() == 1 );
- REQUIRE( sit.skip_pos == 0 );
- REQUIRE( sit.row() == 2 );
- REQUIRE( sit.col() == 1 );
- REQUIRE( (double) (*sit) == Approx(2.0) );
- ++sit;
- REQUIRE( sit.pos() == 2 );
- REQUIRE( sit.skip_pos == 0 );
- REQUIRE( sit.row() == 1 );
- REQUIRE( sit.col() == 2 );
- REQUIRE( (double) (*sit) == Approx(1.5) );
- *sit = 4.2;
- REQUIRE( (double) (*sit) == Approx(4.2) );
- ++sit;
- REQUIRE( sit.pos() == ss.n_nonzero );
- --sit;
- REQUIRE( sit.pos() == 2 );
- REQUIRE( sit.skip_pos == 0 );
- REQUIRE( sit.row() == 1 );
- REQUIRE( sit.col() == 2 );
- REQUIRE( (double) (*sit) == Approx(4.2) );
- --sit;
- REQUIRE( sit.pos() == 1 );
- REQUIRE( sit.skip_pos == 0 );
- REQUIRE( sit.row() == 2 );
- REQUIRE( sit.col() == 1 );
- REQUIRE( (double) (*sit) == Approx(2.0) );
- --sit;
- REQUIRE( sit.pos() == 0 );
- REQUIRE( sit.skip_pos == 0 );
- REQUIRE( sit.row() == 0 );
- REQUIRE( sit.col() == 0 );
- REQUIRE( (double) (*sit) == Approx(2.0) );
- }
- TEST_CASE("sp_subview_row_iterators_test")
- {
- SpMat<double> b(5, 5);
- b(0, 0) = 1.0;
- b(0, 1) = 1.0;
- b(0, 2) = 1.0;
- b(0, 3) = 1.0;
- b(0, 4) = 1.0;
- b(1, 0) = 1.0;
- b(2, 0) = 1.0;
- b(3, 0) = 1.0;
- b(4, 0) = 1.0;
- b(4, 1) = 1.0;
- b(4, 2) = 1.0;
- b(4, 3) = 1.0;
- b(4, 4) = 1.0;
- b(3, 4) = 1.0;
- b(2, 4) = 1.0;
- b(1, 4) = 1.0;
- b(1, 1) = 2.0;
- b(2, 3) = 1.5;
- b(3, 2) = 2.0;
- // [[1.0 1.0 1.0 1.0 1.0]
- // [1.0 2.0 0.0 0.0 1.0]
- // [1.0 0.0 0.0 1.5 1.0]
- // [1.0 0.0 2.0 0.0 1.0]
- // [1.0 1.0 1.0 1.0 1.0]]
- SpSubview<double> s = b.submat(1, 1, 3, 3);
- SpSubview<double>::row_iterator it = s.begin_row();
- REQUIRE( it.pos() == 0 );
- REQUIRE( it.row() == 0 );
- REQUIRE( it.col() == 0 );
- REQUIRE( it.actual_pos == 6 );
- REQUIRE( (double) (*it) == Approx(2.0) );
- ++it;
- REQUIRE( it.pos() == 1 );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 2 );
- REQUIRE( it.actual_pos == 12 );
- REQUIRE( (double) (*it) == Approx(1.5) );
- ++it;
- REQUIRE( it.pos() == 2 );
- REQUIRE( it.row() == 2 );
- REQUIRE( it.col() == 1 );
- REQUIRE( it.actual_pos == 9 );
- REQUIRE( (double) (*it) == Approx(2.0) );
- ++it;
- REQUIRE( it.pos() == s.n_nonzero );
- --it;
- REQUIRE( it.pos() == 2 );
- REQUIRE( it.row() == 2 );
- REQUIRE( it.col() == 1 );
- REQUIRE( it.actual_pos == 9 );
- REQUIRE( (double) (*it) == Approx(2.0) );
- (*it) = 4.0;
- REQUIRE( (double) (*it) == Approx(4.0) );
- --it;
- REQUIRE( it.pos() == 1 );
- REQUIRE( it.row() == 1 );
- REQUIRE( it.col() == 2 );
- REQUIRE( it.actual_pos == 12 );
- REQUIRE( (double) (*it) == Approx(1.5) );
- --it;
- REQUIRE( it.pos() == 0 );
- REQUIRE( it.row() == 0 );
- REQUIRE( it.col() == 0 );
- REQUIRE( it.actual_pos == 6 );
- REQUIRE( (double) (*it) == Approx(2.0) );
- // now a different matrix
- SpMat<double> c(5, 5);
- c(1, 1) = 2.0;
- c(2, 3) = 1.5;
- c(3, 2) = 2.0;
- SpSubview<double> ss = c.submat(0, 0, 3, 3);
- SpSubview<double>::row_iterator sit = ss.begin_row();
- REQUIRE( sit.pos() == 0 );
- REQUIRE( sit.row() == 1 );
- REQUIRE( sit.col() == 1 );
- REQUIRE( (double) (*sit) == Approx(2.0) );
- ++sit;
- REQUIRE( sit.pos() == 1 );
- REQUIRE( sit.row() == 2 );
- REQUIRE( sit.col() == 3 );
- REQUIRE( (double) (*sit) == Approx(1.5) );
- ++sit;
- REQUIRE( sit.pos() == 2 );
- REQUIRE( sit.row() == 3 );
- REQUIRE( sit.col() == 2 );
- REQUIRE( (double) (*sit) == Approx(2.0) );
- ++sit;
- REQUIRE( sit.pos() == ss.n_nonzero );
- --sit;
- REQUIRE( sit.pos() == 2 );
- REQUIRE( sit.row() == 3 );
- REQUIRE( sit.col() == 2 );
- REQUIRE( (double) (*sit) == Approx(2.0) );
- (*sit) = 4.0;
- REQUIRE( (double) (*sit) == Approx(4.0) );
- --sit;
- REQUIRE( sit.pos() == 1 );
- REQUIRE( sit.row() == 2 );
- REQUIRE( sit.col() == 3 );
- REQUIRE( (double) (*sit) == Approx(1.5) );
- --sit;
- REQUIRE( sit.pos() == 0 );
- REQUIRE( sit.row() == 1 );
- REQUIRE( sit.col() == 1 );
- REQUIRE( (double) (*sit) == Approx(2.0) );
- }
- TEST_CASE("sp_subview_sp_base_add_subtract_modulo")
- {
- SpMat<double> m;
- m.sprandu(100, 100, 0.1);
- SpMat<double> n;
- n.sprandu(50, 50, 0.1);
- Mat<double> x(m);
- Mat<double> y(n);
- m.submat(25, 25, 74, 74) += n;
- x.submat(25, 25, 74, 74) += y;
- for (uword c = 0; c < 100; ++c)
- {
- for (uword r = 0; r < 100; ++r)
- {
- REQUIRE( (double) m(r, c) == Approx(x(r, c)) );
- }
- }
- m.sprandu(100, 100, 0.1);
- n.sprandu(50, 50, 0.1);
- x = m;
- y = n;
- m.submat(25, 25, 74, 74) -= n;
- x.submat(25, 25, 74, 74) -= y;
- for (uword c = 0; c < 100; ++c)
- {
- for (uword r = 0; r < 100; ++r)
- {
- REQUIRE( (double) m(r, c) == Approx(x(r, c)) );
- }
- }
- m.sprandu(100, 100, 0.1);
- n.sprandu(50, 50, 0.1);
- x = m;
- y = n;
- m.submat(25, 25, 74, 74) %= n;
- x.submat(25, 25, 74, 74) %= y;
- for( uword c = 0; c < 100; ++c)
- {
- for( uword r = 0; r < 100; ++r)
- {
- REQUIRE( (double) m(r, c) == Approx(x(r, c)) );
- }
- }
- }
- TEST_CASE("sp_subview_hadamard")
- {
- SpMat<double> x;
- x.sprandu(100, 100, 0.1);
- Mat<double> d(x);
- SpMat<double> y;
- y.sprandu(200, 200, 0.1);
- Mat<double> dy(y);
- x %= y.submat(50, 50, 149, 149);
- d %= dy.submat(50, 50, 149, 149);
- for (uword c = 0; c < 100; ++c)
- {
- for (uword r = 0; r < 100; ++r)
- {
- REQUIRE( (double) x(r, c) == Approx(d(r, c)) );
- }
- }
- }
- TEST_CASE("sp_subview_subviews_test")
- {
- SpMat<double> m(20, 20);
- m.sprandu(20, 20, 0.3);
- // Get a subview.
- SpSubview<double> s = m.submat(1, 1, 10, 10); // 10x10
- const SpSubview<double> c = m.submat(1, 1, 10, 10);
- SpSubview<double> t = s.row(1);
- const SpSubview<double> d = c.row(1);
- REQUIRE( t.n_rows == 1 );
- REQUIRE( t.n_cols == 10 );
- REQUIRE( d.n_rows == 1 );
- REQUIRE( d.n_cols == 10 );
- REQUIRE( t.aux_row1 == 2 );
- REQUIRE( t.aux_col1 == 1 );
- for (uword i = 0; i < 10; ++i)
- {
- REQUIRE( (double) t[i] == (double) m(2, i + 1) );
- REQUIRE( d[i] == (double) m(2, i + 1) );
- }
- SpSubview<double> t1 = s.col(2);
- const SpSubview<double> d1 = c.col(2);
- REQUIRE( t1.n_rows == 10 );
- REQUIRE( t1.n_cols == 1 );
- REQUIRE( d1.n_rows == 10 );
- REQUIRE( d1.n_cols == 1 );
- for (uword i = 0; i < 10; ++i)
- {
- REQUIRE( (double) t1[i] == (double) m(i + 1, 3) );
- REQUIRE( d1[i] == (double) m(i + 1, 3) );
- }
- SpSubview<double> t2 = s.rows(3, 5);
- const SpSubview<double> d2 = c.rows(3, 5);
- REQUIRE( t2.n_rows == 3 );
- REQUIRE( t2.n_cols == 10 );
- REQUIRE( d2.n_rows == 3 );
- REQUIRE( d2.n_cols == 10 );
- for (uword j = 0; j < 3; ++j)
- {
- for (uword i = 0; i < 10; ++i)
- {
- REQUIRE( (double) t2(j, i) == (double) m(4 + j, i + 1) );
- REQUIRE( d2(j, i) == (double) m(4 + j, i + 1) );
- }
- }
- SpSubview<double> t3 = s.cols(4, 6);
- const SpSubview<double> d3 = c.cols(4, 6);
- REQUIRE( t3.n_rows == 10 );
- REQUIRE( t3.n_cols == 3 );
- REQUIRE( d3.n_rows == 10 );
- REQUIRE( d3.n_cols == 3 );
- for (uword j = 0; j < 3; ++j)
- {
- for (uword i = 0; i < 10; ++i)
- {
- REQUIRE( (double) t3(i, j) == (double) m(i + 1, 5 + j) );
- REQUIRE( d3(i, j) == (double) m(i + 1, 5 + j) );
- }
- }
- SpSubview<double> t4 = s.submat(1, 1, 6, 6);
- const SpSubview<double> d4 = c.submat(1, 1, 6, 6);
- REQUIRE( t4.n_rows == 6 );
- REQUIRE( t4.n_cols == 6 );
- REQUIRE( d4.n_rows == 6 );
- REQUIRE( d4.n_cols == 6 );
- for (uword j = 0; j < 6; ++j)
- {
- for (uword i = 0; i < 6; ++i)
- {
- REQUIRE( (double) t4(i, j) == (double) m(i + 2, 2 + j) );
- REQUIRE( d4(i, j) == (double) m(i + 2, 2 + j) );
- }
- }
- SpSubview<double> t5 = s.submat(span(2, 8), span(2, 5));
- const SpSubview<double> d5 = c.submat(span(2, 8), span(2, 5));
- REQUIRE( t5.n_rows == 7 );
- REQUIRE( t5.n_cols == 4 );
- REQUIRE( d5.n_rows == 7 );
- REQUIRE( d5.n_cols == 4 );
- for (uword j = 0; j < 4; ++j)
- {
- for (uword i = 0; i < 7; ++i)
- {
- REQUIRE( (double) t5(i, j) == (double) m(i + 3, 3 + j) );
- REQUIRE( d5(i, j) == (double) m(i + 3, 3 + j) );
- }
- }
- SpSubview<double> t6 = s(4, span(1, 5));
- const SpSubview<double> d6 = c(4, span(1, 5));
- REQUIRE( t6.n_rows == 1 );
- REQUIRE( t6.n_cols == 5 );
- REQUIRE( d6.n_rows == 1 );
- REQUIRE( d6.n_cols == 5 );
- for (uword i = 0; i < 5; ++i)
- {
- REQUIRE( (double) t6(i) == (double) m(5, 2 + i) );
- REQUIRE( d6(i) == (double) m(5, 2 + i) );
- }
- SpSubview<double> t7 = s(span(1, 5), 4);
- const SpSubview<double> d7 = c(span(1, 5), 4);
- REQUIRE( t7.n_rows == 5 );
- REQUIRE( t7.n_cols == 1 );
- REQUIRE( d7.n_rows == 5 );
- REQUIRE( d7.n_cols == 1 );
- for (uword i = 0; i < 5; ++i)
- {
- REQUIRE( (double) t7(i) == (double) m(2 + i, 5) );
- REQUIRE( d7(i) == (double) m(2 + i, 5) );
- }
- SpSubview<double> t8 = s(span(1, 9), span(7, 8));
- const SpSubview<double> d8 = c(span(1, 9), span(7, 8));
- REQUIRE( t8.n_rows == 9 );
- REQUIRE( t8.n_cols == 2 );
- REQUIRE( d8.n_rows == 9 );
- REQUIRE( d8.n_cols == 2 );
- for (uword j = 0; j < 2; ++j)
- {
- for (uword i = 0; i < 9; ++i)
- {
- REQUIRE( (double) t8(i, j) == (double) m(i + 2, 8 + j) );
- REQUIRE( d8(i, j) == (double) m(i + 2, 8 + j) );
- }
- }
- }
- TEST_CASE("sp_subview_assignment_sp_base")
- {
- mat d(51, 51);
- d.fill(7.0); // Why not?
- mat dd(d);
- sp_mat e;
- e.sprandu(50, 50, 0.3);
- mat ed(e); // Dense copy.
- d.submat(0, 0, 49, 49) = e;
- dd.submat(0, 0, 49, 49) = ed;
- for (uword i = 0; i < d.n_elem; ++i)
- {
- REQUIRE( d[i] == Approx(dd[i]) );
- }
- }
- TEST_CASE("sp_subview_addition_sp_base")
- {
- mat d(51, 51);
- d.fill(7.0); // Why not?
- mat dd(d);
- sp_mat e;
- e.sprandu(50, 50, 0.3);
- mat ed(e); // Dense copy.
- d.submat(0, 0, 49, 49) += e;
- dd.submat(0, 0, 49, 49) += ed;
- for (uword i = 0; i < d.n_elem; ++i)
- {
- REQUIRE( d[i] == Approx(dd[i]) );
- }
- }
- TEST_CASE("sp_subview_subtraction_sp_base")
- {
- mat d(51, 51);
- d.fill(7.0); // Why not?
- mat dd(d);
- sp_mat e;
- e.sprandu(50, 50, 0.3);
- mat ed(e); // Dense copy.
- d.submat(0, 0, 49, 49) -= e;
- dd.submat(0, 0, 49, 49) -= ed;
- for (uword i = 0; i < d.n_elem; ++i)
- {
- REQUIRE( d[i] == Approx(dd[i]) );
- }
- }
- TEST_CASE("sp_subview_schur_sp_base")
- {
- mat d(51, 51);
- d.fill(7.0); // Why not?
- mat dd(d);
- sp_mat e;
- e.sprandu(50, 50, 0.3);
- mat ed(e); // Dense copy.
- d.submat(0, 0, 49, 49) %= e;
- dd.submat(0, 0, 49, 49) %= ed;
- for (uword i = 0; i < d.n_elem; ++i)
- {
- REQUIRE( d[i] == Approx(dd[i]) );
- }
- }
- TEST_CASE("sp_subview_division_sp_base")
- {
- mat d(51, 51);
- d.fill(7.0); // Why not?
- mat dd(d);
- sp_mat e;
- e.sprandu(50, 50, 0.3);
- mat ed(e); // Dense copy.
- d.submat(0, 0, 49, 49) /= e;
- dd.submat(0, 0, 49, 49) /= ed;
- for (uword i = 0; i < d.n_elem; ++i)
- {
- if (std::isinf(d[i]))
- REQUIRE( std::isinf(dd[i]) );
- else
- REQUIRE( d[i] == Approx(dd[i]) );
- }
- }
- TEST_CASE("sp_subview_row_iterator_constructor")
- {
- // Create a row iterator with an exact position.
- Mat<double> tmp =
- { { 1.1, 1.2, 1.3, 1.4, 1.5 },
- { 2.1, 5.5, 0.0, 0.0, 3.1 },
- { 2.2, 0.0, 0.0, 6.5, 3.2 },
- { 2.3, 0.0, 7.5, 0.0, 3.3 },
- { 2.4, 4.2, 4.3, 4.4, 3.4 } };
- SpMat<double> X(tmp);
- SpSubview<double> sv = X.submat(1, 1, 3, 3);
- // Now create an iterator to an exact position.
- SpSubview<double>::const_row_iterator cri(sv, 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 = SpSubview<double>::const_row_iterator(sv, 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 = SpSubview<double>::const_row_iterator(sv, 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) );
- }
- TEST_CASE("sp_subview_row_iterator_test_1")
- {
- Mat<double> tmp =
- { { 1.1, 1.2, 1.3, 1.4, 1.5 },
- { 2.1, 5.5, 0.0, 0.0, 3.1 },
- { 2.2, 0.0, 0.0, 6.5, 3.2 },
- { 2.3, 0.0, 7.5, 0.0, 3.3 },
- { 2.4, 4.2, 4.3, 4.4, 3.4 } };
- SpMat<double> X(tmp);
- SpSubview<double> sv = X.submat(1, 1, 3, 3);
- SpSubview<double>::row_iterator it = sv.begin_row();
- SpSubview<double>::row_iterator it2 = sv.begin_row();
- ++it;
- --it;
- REQUIRE( it.row() == it2.row() );
- REQUIRE( it.col() == it2.col() );
- REQUIRE( (*it) == Approx(*it2) );
- REQUIRE( it.pos() == it2.pos() );
- }
- TEST_CASE("sp_subview_row_iterator_test_2")
- {
- // Make sure the loop terminates.
- Mat<double> tmp =
- { { 1.1, 1.2, 1.3, 1.4, 1.5 },
- { 2.1, 5.5, 0.0, 0.0, 3.1 },
- { 2.2, 0.0, 0.0, 6.5, 3.2 },
- { 2.3, 0.0, 7.5, 0.0, 3.3 },
- { 2.4, 4.2, 4.3, 4.4, 3.4 } };
- SpMat<double> X(tmp);
- SpSubview<double> sv = X.submat(1, 1, 3, 3);
- SpSubview<double>::row_iterator it = sv.end_row(sv.n_rows - 1); // points to past-the-end
- SpSubview<double>::row_iterator it_end = sv.begin_row();
- do {
- --it;
- } while(it != it_end);
- REQUIRE( true );
- }
|