matrix.hpp 210 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988
  1. //
  2. // Copyright (c) 2000-2010
  3. // Joerg Walter, Mathias Koch, Gunter Winkler, David Bellot
  4. // Copyright (c) 2014, Athanasios Iliopoulos
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // The authors gratefully acknowledge the support of
  11. // GeNeSys mbH & Co. KG in producing this work.
  12. //
  13. #ifndef _BOOST_UBLAS_MATRIX_
  14. #define _BOOST_UBLAS_MATRIX_
  15. #include <boost/config.hpp>
  16. #include <boost/numeric/ublas/vector.hpp>
  17. #include <boost/numeric/ublas/matrix_expression.hpp>
  18. #include <boost/numeric/ublas/detail/matrix_assign.hpp>
  19. #include <boost/serialization/collection_size_type.hpp>
  20. #include <boost/serialization/array.hpp>
  21. #include <boost/serialization/nvp.hpp>
  22. // Iterators based on ideas of Jeremy Siek
  23. namespace boost { namespace numeric {
  24. /** \brief main namespace of uBLAS.
  25. *
  26. * Use this namespace for all operations with uBLAS. It can also be abbreviated with
  27. * \code namespace ublas = boost::numeric::ublas; \endcode
  28. *
  29. * A common practice is to bring this namespace into the current scope with
  30. * \code using namespace boost::numeric::ublas; \endcode.
  31. *
  32. * However, be warned that using the ublas namespace and the std::vector at the same time can lead to the compiler to confusion.
  33. * The solution is simply to prefix each ublas vector like \c boost::numeric::ublas::vector<T>. If you think it's too long to
  34. * write, you can define a new namespace like \c namespace ublas = boost::numeric::ublas and then just declare your vectors
  35. * with \c ublas::vector<T>. STL vectors will be declared as vector<T>. No need to prefix with \c std::
  36. */
  37. namespace ublas {
  38. namespace detail {
  39. using namespace boost::numeric::ublas;
  40. // Matrix resizing algorithm
  41. template <class L, class M>
  42. BOOST_UBLAS_INLINE
  43. void matrix_resize_preserve (M& m, M& temporary) {
  44. typedef L layout_type;
  45. typedef typename M::size_type size_type;
  46. const size_type msize1 (m.size1 ()); // original size
  47. const size_type msize2 (m.size2 ());
  48. const size_type size1 (temporary.size1 ()); // new size is specified by temporary
  49. const size_type size2 (temporary.size2 ());
  50. // Common elements to preserve
  51. const size_type size1_min = (std::min) (size1, msize1);
  52. const size_type size2_min = (std::min) (size2, msize2);
  53. // Order for major and minor sizes
  54. const size_type major_size = layout_type::size_M (size1_min, size2_min);
  55. const size_type minor_size = layout_type::size_m (size1_min, size2_min);
  56. // Indexing copy over major
  57. for (size_type major = 0; major != major_size; ++major) {
  58. for (size_type minor = 0; minor != minor_size; ++minor) {
  59. // find indexes - use invertability of element_ functions
  60. const size_type i1 = layout_type::index_M(major, minor);
  61. const size_type i2 = layout_type::index_m(major, minor);
  62. temporary.data () [layout_type::element (i1, size1, i2, size2)] =
  63. m.data() [layout_type::element (i1, msize1, i2, msize2)];
  64. }
  65. }
  66. m.assign_temporary (temporary);
  67. }
  68. }
  69. /** \brief A dense matrix of values of type \c T.
  70. *
  71. * For a \f$(m \times n)\f$-dimensional matrix and \f$ 0 \leq i < m, 0 \leq j < n\f$, every element \f$ m_{i,j} \f$ is mapped to
  72. * the \f$(i.n + j)\f$-th element of the container for row major orientation or the \f$ (i + j.m) \f$-th element of
  73. * the container for column major orientation. In a dense matrix all elements are represented in memory in a
  74. * contiguous chunk of memory by definition.
  75. *
  76. * Orientation and storage can also be specified, otherwise a \c row_major and \c unbounded_array are used. It is \b not
  77. * required by the storage to initialize elements of the matrix.
  78. *
  79. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  80. * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major
  81. * \tparam A the type of Storage array. Default is \c unbounded_array
  82. */
  83. template<class T, class L, class A>
  84. class matrix:
  85. public matrix_container<matrix<T, L, A> > {
  86. typedef T *pointer;
  87. typedef L layout_type;
  88. typedef matrix<T, L, A> self_type;
  89. public:
  90. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  91. using matrix_container<self_type>::operator ();
  92. #endif
  93. typedef typename A::size_type size_type;
  94. typedef typename A::difference_type difference_type;
  95. typedef T value_type;
  96. typedef const T &const_reference;
  97. typedef T &reference;
  98. typedef A array_type;
  99. typedef const matrix_reference<const self_type> const_closure_type;
  100. typedef matrix_reference<self_type> closure_type;
  101. typedef vector<T, A> vector_temporary_type;
  102. typedef self_type matrix_temporary_type;
  103. typedef dense_tag storage_category;
  104. // This could be better for performance,
  105. // typedef typename unknown_orientation_tag orientation_category;
  106. // but others depend on the orientation information...
  107. typedef typename L::orientation_category orientation_category;
  108. // Construction and destruction
  109. /// Default dense matrix constructor. Make a dense matrix of size (0,0)
  110. BOOST_UBLAS_INLINE
  111. matrix ():
  112. matrix_container<self_type> (),
  113. size1_ (0), size2_ (0), data_ () {}
  114. /** Dense matrix constructor with defined size
  115. * \param size1 number of rows
  116. * \param size2 number of columns
  117. */
  118. BOOST_UBLAS_INLINE
  119. matrix (size_type size1, size_type size2):
  120. matrix_container<self_type> (),
  121. size1_ (size1), size2_ (size2), data_ (layout_type::storage_size (size1, size2)) {
  122. }
  123. /** Dense matrix constructor with defined size a initial value for all the matrix elements
  124. * \param size1 number of rows
  125. * \param size2 number of columns
  126. * \param init initial value assigned to all elements
  127. */
  128. matrix (size_type size1, size_type size2, const value_type &init):
  129. matrix_container<self_type> (),
  130. size1_ (size1), size2_ (size2), data_ (layout_type::storage_size (size1, size2), init) {
  131. }
  132. /** Dense matrix constructor with defined size and an initial data array
  133. * \param size1 number of rows
  134. * \param size2 number of columns
  135. * \param data array to copy into the matrix. Must have the same dimension as the matrix
  136. */
  137. BOOST_UBLAS_INLINE
  138. matrix (size_type size1, size_type size2, const array_type &data):
  139. matrix_container<self_type> (),
  140. size1_ (size1), size2_ (size2), data_ (data) {}
  141. /** Copy-constructor of a dense matrix
  142. * \param m is a dense matrix
  143. */
  144. BOOST_UBLAS_INLINE
  145. matrix (const matrix &m):
  146. matrix_container<self_type> (),
  147. size1_ (m.size1_), size2_ (m.size2_), data_ (m.data_) {}
  148. /** Copy-constructor of a dense matrix from a matrix expression
  149. * \param ae is a matrix expression
  150. */
  151. template<class AE>
  152. BOOST_UBLAS_INLINE
  153. matrix (const matrix_expression<AE> &ae):
  154. matrix_container<self_type> (),
  155. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), data_ (layout_type::storage_size (size1_, size2_)) {
  156. matrix_assign<scalar_assign> (*this, ae);
  157. }
  158. // Accessors
  159. /** Return the number of rows of the matrix
  160. * You can also use the free size<>() function in operation/size.hpp as size<1>(m) where m is a matrix
  161. */
  162. BOOST_UBLAS_INLINE
  163. size_type size1 () const {
  164. return size1_;
  165. }
  166. /** Return the number of colums of the matrix
  167. * You can also use the free size<>() function in operation/size.hpp as size<2>(m) where m is a matrix
  168. */
  169. BOOST_UBLAS_INLINE
  170. size_type size2 () const {
  171. return size2_;
  172. }
  173. // Storage accessors
  174. /** Return a constant reference to the internal storage of a dense matrix, i.e. the raw data
  175. * It's type depends on the type used by the matrix to store its data
  176. */
  177. BOOST_UBLAS_INLINE
  178. const array_type &data () const {
  179. return data_;
  180. }
  181. /** Return a reference to the internal storage of a dense matrix, i.e. the raw data
  182. * It's type depends on the type used by the matrix to store its data
  183. */
  184. BOOST_UBLAS_INLINE
  185. array_type &data () {
  186. return data_;
  187. }
  188. // Resizing
  189. /** Resize a matrix to new dimensions
  190. * If data are preserved, then if the size if bigger at least on one dimension, extra values are filled with zeros.
  191. * If data are not preserved, then nothing has to be assumed regarding the content of the matrix after resizing.
  192. * \param size1 the new number of rows
  193. * \param size2 the new number of colums
  194. * \param preserve a boolean to say if one wants the data to be preserved during the resizing. Default is true.
  195. */
  196. BOOST_UBLAS_INLINE
  197. void resize (size_type size1, size_type size2, bool preserve = true) {
  198. if (preserve) {
  199. self_type temporary (size1, size2);
  200. detail::matrix_resize_preserve<layout_type> (*this, temporary);
  201. }
  202. else {
  203. data ().resize (layout_type::storage_size (size1, size2));
  204. size1_ = size1;
  205. size2_ = size2;
  206. }
  207. }
  208. // Element access
  209. /** Access a matrix element. Here we return a const reference
  210. * \param i the first coordinate of the element. By default it's the row
  211. * \param j the second coordinate of the element. By default it's the column
  212. * \return a const reference to the element
  213. */
  214. BOOST_UBLAS_INLINE
  215. const_reference operator () (size_type i, size_type j) const {
  216. return data () [layout_type::element (i, size1_, j, size2_)];
  217. }
  218. /** Access a matrix element. Here we return a reference
  219. * \param i the first coordinate of the element. By default it's the row
  220. * \param j the second coordinate of the element. By default it's the column
  221. * \return a reference to the element
  222. */
  223. BOOST_UBLAS_INLINE
  224. reference at_element (size_type i, size_type j) {
  225. return data () [layout_type::element (i, size1_, j, size2_)];
  226. }
  227. /** Access a matrix element. Here we return a reference
  228. * \param i the first coordinate of the element. By default it's the row
  229. * \param j the second coordinate of the element. By default it's the column
  230. * \return a reference to the element
  231. */
  232. BOOST_UBLAS_INLINE
  233. reference operator () (size_type i, size_type j) {
  234. return at_element (i, j);
  235. }
  236. // Element assignment
  237. /** Change the value of a matrix element. Return back a reference to it
  238. * \param i the first coordinate of the element. By default it's the row
  239. * \param j the second coordinate of the element. By default it's the column
  240. * \param t the new value of the element
  241. * \return a reference to the newly changed element
  242. */
  243. BOOST_UBLAS_INLINE
  244. reference insert_element (size_type i, size_type j, const_reference t) {
  245. return (at_element (i, j) = t);
  246. }
  247. /** Erase the element
  248. * For most types (int, double, etc...) it means setting 0 (zero) the element at zero in fact.
  249. * For user-defined types, it could be another value if you decided it. Your type in that case must
  250. * contain a default null value.
  251. * \param i the first coordinate of the element. By default it's the row
  252. * \param j the second coordinate of the element. By default it's the column
  253. */
  254. void erase_element (size_type i, size_type j) {
  255. at_element (i, j) = value_type/*zero*/();
  256. }
  257. // Zeroing
  258. /** Erase all elements in the matrix
  259. * For most types (int, double, etc...) it means writing 0 (zero) everywhere.
  260. * For user-defined types, it could be another value if you decided it. Your type in that case must
  261. * contain a default null value.
  262. */
  263. BOOST_UBLAS_INLINE
  264. void clear () {
  265. std::fill (data ().begin (), data ().end (), value_type/*zero*/());
  266. }
  267. // Assignment
  268. #ifdef BOOST_UBLAS_MOVE_SEMANTICS
  269. /*! @note "pass by value" the key idea to enable move semantics */
  270. BOOST_UBLAS_INLINE
  271. matrix &operator = (matrix m) {
  272. assign_temporary(m);
  273. return *this;
  274. }
  275. #else
  276. BOOST_UBLAS_INLINE
  277. matrix &operator = (const matrix &m) {
  278. size1_ = m.size1_;
  279. size2_ = m.size2_;
  280. data () = m.data ();
  281. return *this;
  282. }
  283. #endif
  284. template<class C> // Container assignment without temporary
  285. BOOST_UBLAS_INLINE
  286. matrix &operator = (const matrix_container<C> &m) {
  287. resize (m ().size1 (), m ().size2 (), false);
  288. assign (m);
  289. return *this;
  290. }
  291. BOOST_UBLAS_INLINE
  292. matrix &assign_temporary (matrix &m) {
  293. swap (m);
  294. return *this;
  295. }
  296. template<class AE>
  297. BOOST_UBLAS_INLINE
  298. matrix &operator = (const matrix_expression<AE> &ae) {
  299. self_type temporary (ae);
  300. return assign_temporary (temporary);
  301. }
  302. template<class AE>
  303. BOOST_UBLAS_INLINE
  304. matrix &assign (const matrix_expression<AE> &ae) {
  305. matrix_assign<scalar_assign> (*this, ae);
  306. return *this;
  307. }
  308. template<class AE>
  309. BOOST_UBLAS_INLINE
  310. matrix& operator += (const matrix_expression<AE> &ae) {
  311. self_type temporary (*this + ae);
  312. return assign_temporary (temporary);
  313. }
  314. template<class C> // Container assignment without temporary
  315. BOOST_UBLAS_INLINE
  316. matrix &operator += (const matrix_container<C> &m) {
  317. plus_assign (m);
  318. return *this;
  319. }
  320. template<class AE>
  321. BOOST_UBLAS_INLINE
  322. matrix &plus_assign (const matrix_expression<AE> &ae) {
  323. matrix_assign<scalar_plus_assign> (*this, ae);
  324. return *this;
  325. }
  326. template<class AE>
  327. BOOST_UBLAS_INLINE
  328. matrix& operator -= (const matrix_expression<AE> &ae) {
  329. self_type temporary (*this - ae);
  330. return assign_temporary (temporary);
  331. }
  332. template<class C> // Container assignment without temporary
  333. BOOST_UBLAS_INLINE
  334. matrix &operator -= (const matrix_container<C> &m) {
  335. minus_assign (m);
  336. return *this;
  337. }
  338. template<class AE>
  339. BOOST_UBLAS_INLINE
  340. matrix &minus_assign (const matrix_expression<AE> &ae) {
  341. matrix_assign<scalar_minus_assign> (*this, ae);
  342. return *this;
  343. }
  344. template<class AT>
  345. BOOST_UBLAS_INLINE
  346. matrix& operator *= (const AT &at) {
  347. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  348. return *this;
  349. }
  350. template<class AT>
  351. BOOST_UBLAS_INLINE
  352. matrix& operator /= (const AT &at) {
  353. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  354. return *this;
  355. }
  356. // Swapping
  357. BOOST_UBLAS_INLINE
  358. void swap (matrix &m) {
  359. if (this != &m) {
  360. std::swap (size1_, m.size1_);
  361. std::swap (size2_, m.size2_);
  362. data ().swap (m.data ());
  363. }
  364. }
  365. BOOST_UBLAS_INLINE
  366. friend void swap (matrix &m1, matrix &m2) {
  367. m1.swap (m2);
  368. }
  369. // Iterator types
  370. private:
  371. // Use the storage array iterator
  372. typedef typename A::const_iterator const_subiterator_type;
  373. typedef typename A::iterator subiterator_type;
  374. public:
  375. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  376. typedef indexed_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  377. typedef indexed_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  378. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  379. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  380. #else
  381. class const_iterator1;
  382. class iterator1;
  383. class const_iterator2;
  384. class iterator2;
  385. #endif
  386. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  387. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  388. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  389. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  390. // Element lookup
  391. BOOST_UBLAS_INLINE
  392. const_iterator1 find1 (int /* rank */, size_type i, size_type j) const {
  393. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  394. return const_iterator1 (*this, i, j);
  395. #else
  396. return const_iterator1 (*this, data ().begin () + layout_type::address (i, size1_, j, size2_));
  397. #endif
  398. }
  399. BOOST_UBLAS_INLINE
  400. iterator1 find1 (int /* rank */, size_type i, size_type j) {
  401. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  402. return iterator1 (*this, i, j);
  403. #else
  404. return iterator1 (*this, data ().begin () + layout_type::address (i, size1_, j, size2_));
  405. #endif
  406. }
  407. BOOST_UBLAS_INLINE
  408. const_iterator2 find2 (int /* rank */, size_type i, size_type j) const {
  409. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  410. return const_iterator2 (*this, i, j);
  411. #else
  412. return const_iterator2 (*this, data ().begin () + layout_type::address (i, size1_, j, size2_));
  413. #endif
  414. }
  415. BOOST_UBLAS_INLINE
  416. iterator2 find2 (int /* rank */, size_type i, size_type j) {
  417. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  418. return iterator2 (*this, i, j);
  419. #else
  420. return iterator2 (*this, data ().begin () + layout_type::address (i, size1_, j, size2_));
  421. #endif
  422. }
  423. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  424. class const_iterator1:
  425. public container_const_reference<matrix>,
  426. public random_access_iterator_base<dense_random_access_iterator_tag,
  427. const_iterator1, value_type> {
  428. public:
  429. typedef typename matrix::value_type value_type;
  430. typedef typename matrix::difference_type difference_type;
  431. typedef typename matrix::const_reference reference;
  432. typedef const typename matrix::pointer pointer;
  433. typedef const_iterator2 dual_iterator_type;
  434. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  435. // Construction and destruction
  436. BOOST_UBLAS_INLINE
  437. const_iterator1 ():
  438. container_const_reference<self_type> (), it_ () {}
  439. BOOST_UBLAS_INLINE
  440. const_iterator1 (const self_type &m, const const_subiterator_type &it):
  441. container_const_reference<self_type> (m), it_ (it) {}
  442. BOOST_UBLAS_INLINE
  443. const_iterator1 (const iterator1 &it):
  444. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  445. // Arithmetic
  446. BOOST_UBLAS_INLINE
  447. const_iterator1 &operator ++ () {
  448. layout_type::increment_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  449. return *this;
  450. }
  451. BOOST_UBLAS_INLINE
  452. const_iterator1 &operator -- () {
  453. layout_type::decrement_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  454. return *this;
  455. }
  456. BOOST_UBLAS_INLINE
  457. const_iterator1 &operator += (difference_type n) {
  458. layout_type::increment_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  459. return *this;
  460. }
  461. BOOST_UBLAS_INLINE
  462. const_iterator1 &operator -= (difference_type n) {
  463. layout_type::decrement_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  464. return *this;
  465. }
  466. BOOST_UBLAS_INLINE
  467. difference_type operator - (const const_iterator1 &it) const {
  468. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  469. return layout_type::distance_i (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  470. }
  471. // Dereference
  472. BOOST_UBLAS_INLINE
  473. const_reference operator * () const {
  474. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  475. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  476. return *it_;
  477. }
  478. BOOST_UBLAS_INLINE
  479. const_reference operator [] (difference_type n) const {
  480. return *(*this + n);
  481. }
  482. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  483. BOOST_UBLAS_INLINE
  484. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  485. typename self_type::
  486. #endif
  487. const_iterator2 begin () const {
  488. const self_type &m = (*this) ();
  489. return m.find2 (1, index1 (), 0);
  490. }
  491. BOOST_UBLAS_INLINE
  492. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  493. typename self_type::
  494. #endif
  495. const_iterator2 cbegin () const {
  496. return begin ();
  497. }
  498. BOOST_UBLAS_INLINE
  499. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  500. typename self_type::
  501. #endif
  502. const_iterator2 end () const {
  503. const self_type &m = (*this) ();
  504. return m.find2 (1, index1 (), m.size2 ());
  505. }
  506. BOOST_UBLAS_INLINE
  507. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  508. typename self_type::
  509. #endif
  510. const_iterator2 cend () const {
  511. return end ();
  512. }
  513. BOOST_UBLAS_INLINE
  514. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  515. typename self_type::
  516. #endif
  517. const_reverse_iterator2 rbegin () const {
  518. return const_reverse_iterator2 (end ());
  519. }
  520. BOOST_UBLAS_INLINE
  521. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  522. typename self_type::
  523. #endif
  524. const_reverse_iterator2 crbegin () const {
  525. return rbegin ();
  526. }
  527. BOOST_UBLAS_INLINE
  528. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  529. typename self_type::
  530. #endif
  531. const_reverse_iterator2 rend () const {
  532. return const_reverse_iterator2 (begin ());
  533. }
  534. BOOST_UBLAS_INLINE
  535. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  536. typename self_type::
  537. #endif
  538. const_reverse_iterator2 crend () const {
  539. return rend ();
  540. }
  541. #endif
  542. // Indices
  543. BOOST_UBLAS_INLINE
  544. size_type index1 () const {
  545. const self_type &m = (*this) ();
  546. return layout_type::index_i (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  547. }
  548. BOOST_UBLAS_INLINE
  549. size_type index2 () const {
  550. const self_type &m = (*this) ();
  551. return layout_type::index_j (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  552. }
  553. // Assignment
  554. BOOST_UBLAS_INLINE
  555. const_iterator1 &operator = (const const_iterator1 &it) {
  556. container_const_reference<self_type>::assign (&it ());
  557. it_ = it.it_;
  558. return *this;
  559. }
  560. // Comparison
  561. BOOST_UBLAS_INLINE
  562. bool operator == (const const_iterator1 &it) const {
  563. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  564. return it_ == it.it_;
  565. }
  566. BOOST_UBLAS_INLINE
  567. bool operator < (const const_iterator1 &it) const {
  568. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  569. return it_ < it.it_;
  570. }
  571. private:
  572. const_subiterator_type it_;
  573. friend class iterator1;
  574. };
  575. #endif
  576. BOOST_UBLAS_INLINE
  577. const_iterator1 begin1 () const {
  578. return find1 (0, 0, 0);
  579. }
  580. BOOST_UBLAS_INLINE
  581. const_iterator1 cbegin1 () const {
  582. return begin1 ();
  583. }
  584. BOOST_UBLAS_INLINE
  585. const_iterator1 end1 () const {
  586. return find1 (0, size1_, 0);
  587. }
  588. BOOST_UBLAS_INLINE
  589. const_iterator1 cend1 () const {
  590. return end1 ();
  591. }
  592. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  593. class iterator1:
  594. public container_reference<matrix>,
  595. public random_access_iterator_base<dense_random_access_iterator_tag,
  596. iterator1, value_type> {
  597. public:
  598. typedef typename matrix::value_type value_type;
  599. typedef typename matrix::difference_type difference_type;
  600. typedef typename matrix::reference reference;
  601. typedef typename matrix::pointer pointer;
  602. typedef iterator2 dual_iterator_type;
  603. typedef reverse_iterator2 dual_reverse_iterator_type;
  604. // Construction and destruction
  605. BOOST_UBLAS_INLINE
  606. iterator1 ():
  607. container_reference<self_type> (), it_ () {}
  608. BOOST_UBLAS_INLINE
  609. iterator1 (self_type &m, const subiterator_type &it):
  610. container_reference<self_type> (m), it_ (it) {}
  611. // Arithmetic
  612. BOOST_UBLAS_INLINE
  613. iterator1 &operator ++ () {
  614. layout_type::increment_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  615. return *this;
  616. }
  617. BOOST_UBLAS_INLINE
  618. iterator1 &operator -- () {
  619. layout_type::decrement_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  620. return *this;
  621. }
  622. BOOST_UBLAS_INLINE
  623. iterator1 &operator += (difference_type n) {
  624. layout_type::increment_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  625. return *this;
  626. }
  627. BOOST_UBLAS_INLINE
  628. iterator1 &operator -= (difference_type n) {
  629. layout_type::decrement_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  630. return *this;
  631. }
  632. BOOST_UBLAS_INLINE
  633. difference_type operator - (const iterator1 &it) const {
  634. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  635. return layout_type::distance_i (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  636. }
  637. // Dereference
  638. BOOST_UBLAS_INLINE
  639. reference operator * () const {
  640. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  641. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  642. return *it_;
  643. }
  644. BOOST_UBLAS_INLINE
  645. reference operator [] (difference_type n) const {
  646. return *(*this + n);
  647. }
  648. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  649. BOOST_UBLAS_INLINE
  650. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  651. typename self_type::
  652. #endif
  653. iterator2 begin () const {
  654. self_type &m = (*this) ();
  655. return m.find2 (1, index1 (), 0);
  656. }
  657. BOOST_UBLAS_INLINE
  658. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  659. typename self_type::
  660. #endif
  661. iterator2 end () const {
  662. self_type &m = (*this) ();
  663. return m.find2 (1, index1 (), m.size2 ());
  664. }
  665. BOOST_UBLAS_INLINE
  666. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  667. typename self_type::
  668. #endif
  669. reverse_iterator2 rbegin () const {
  670. return reverse_iterator2 (end ());
  671. }
  672. BOOST_UBLAS_INLINE
  673. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  674. typename self_type::
  675. #endif
  676. reverse_iterator2 rend () const {
  677. return reverse_iterator2 (begin ());
  678. }
  679. #endif
  680. // Indices
  681. BOOST_UBLAS_INLINE
  682. size_type index1 () const {
  683. self_type &m = (*this) ();
  684. return layout_type::index_i (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  685. }
  686. BOOST_UBLAS_INLINE
  687. size_type index2 () const {
  688. self_type &m = (*this) ();
  689. return layout_type::index_j (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  690. }
  691. // Assignment
  692. BOOST_UBLAS_INLINE
  693. iterator1 &operator = (const iterator1 &it) {
  694. container_reference<self_type>::assign (&it ());
  695. it_ = it.it_;
  696. return *this;
  697. }
  698. // Comparison
  699. BOOST_UBLAS_INLINE
  700. bool operator == (const iterator1 &it) const {
  701. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  702. return it_ == it.it_;
  703. }
  704. BOOST_UBLAS_INLINE
  705. bool operator < (const iterator1 &it) const {
  706. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  707. return it_ < it.it_;
  708. }
  709. private:
  710. subiterator_type it_;
  711. friend class const_iterator1;
  712. };
  713. #endif
  714. BOOST_UBLAS_INLINE
  715. iterator1 begin1 () {
  716. return find1 (0, 0, 0);
  717. }
  718. BOOST_UBLAS_INLINE
  719. iterator1 end1 () {
  720. return find1 (0, size1_, 0);
  721. }
  722. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  723. class const_iterator2:
  724. public container_const_reference<matrix>,
  725. public random_access_iterator_base<dense_random_access_iterator_tag,
  726. const_iterator2, value_type> {
  727. public:
  728. typedef typename matrix::value_type value_type;
  729. typedef typename matrix::difference_type difference_type;
  730. typedef typename matrix::const_reference reference;
  731. typedef const typename matrix::pointer pointer;
  732. typedef const_iterator1 dual_iterator_type;
  733. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  734. // Construction and destruction
  735. BOOST_UBLAS_INLINE
  736. const_iterator2 ():
  737. container_const_reference<self_type> (), it_ () {}
  738. BOOST_UBLAS_INLINE
  739. const_iterator2 (const self_type &m, const const_subiterator_type &it):
  740. container_const_reference<self_type> (m), it_ (it) {}
  741. BOOST_UBLAS_INLINE
  742. const_iterator2 (const iterator2 &it):
  743. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  744. // Arithmetic
  745. BOOST_UBLAS_INLINE
  746. const_iterator2 &operator ++ () {
  747. layout_type::increment_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  748. return *this;
  749. }
  750. BOOST_UBLAS_INLINE
  751. const_iterator2 &operator -- () {
  752. layout_type::decrement_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  753. return *this;
  754. }
  755. BOOST_UBLAS_INLINE
  756. const_iterator2 &operator += (difference_type n) {
  757. layout_type::increment_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  758. return *this;
  759. }
  760. BOOST_UBLAS_INLINE
  761. const_iterator2 &operator -= (difference_type n) {
  762. layout_type::decrement_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  763. return *this;
  764. }
  765. BOOST_UBLAS_INLINE
  766. difference_type operator - (const const_iterator2 &it) const {
  767. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  768. return layout_type::distance_j (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  769. }
  770. // Dereference
  771. BOOST_UBLAS_INLINE
  772. const_reference operator * () const {
  773. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  774. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  775. return *it_;
  776. }
  777. BOOST_UBLAS_INLINE
  778. const_reference operator [] (difference_type n) const {
  779. return *(*this + n);
  780. }
  781. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  782. BOOST_UBLAS_INLINE
  783. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  784. typename self_type::
  785. #endif
  786. const_iterator1 begin () const {
  787. const self_type &m = (*this) ();
  788. return m.find1 (1, 0, index2 ());
  789. }
  790. BOOST_UBLAS_INLINE
  791. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  792. typename self_type::
  793. #endif
  794. const_iterator1 cbegin () const {
  795. return begin ();
  796. }
  797. BOOST_UBLAS_INLINE
  798. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  799. typename self_type::
  800. #endif
  801. const_iterator1 end () const {
  802. const self_type &m = (*this) ();
  803. return m.find1 (1, m.size1 (), index2 ());
  804. }
  805. BOOST_UBLAS_INLINE
  806. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  807. typename self_type::
  808. #endif
  809. const_iterator1 cend () const {
  810. return end ();
  811. }
  812. BOOST_UBLAS_INLINE
  813. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  814. typename self_type::
  815. #endif
  816. const_reverse_iterator1 rbegin () const {
  817. return const_reverse_iterator1 (end ());
  818. }
  819. BOOST_UBLAS_INLINE
  820. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  821. typename self_type::
  822. #endif
  823. const_reverse_iterator1 crbegin () const {
  824. return rbegin ();
  825. }
  826. BOOST_UBLAS_INLINE
  827. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  828. typename self_type::
  829. #endif
  830. const_reverse_iterator1 rend () const {
  831. return const_reverse_iterator1 (begin ());
  832. }
  833. BOOST_UBLAS_INLINE
  834. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  835. typename self_type::
  836. #endif
  837. const_reverse_iterator1 crend () const {
  838. return rend ();
  839. }
  840. #endif
  841. // Indices
  842. BOOST_UBLAS_INLINE
  843. size_type index1 () const {
  844. const self_type &m = (*this) ();
  845. return layout_type::index_i (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  846. }
  847. BOOST_UBLAS_INLINE
  848. size_type index2 () const {
  849. const self_type &m = (*this) ();
  850. return layout_type::index_j (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  851. }
  852. // Assignment
  853. BOOST_UBLAS_INLINE
  854. const_iterator2 &operator = (const const_iterator2 &it) {
  855. container_const_reference<self_type>::assign (&it ());
  856. it_ = it.it_;
  857. return *this;
  858. }
  859. // Comparison
  860. BOOST_UBLAS_INLINE
  861. bool operator == (const const_iterator2 &it) const {
  862. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  863. return it_ == it.it_;
  864. }
  865. BOOST_UBLAS_INLINE
  866. bool operator < (const const_iterator2 &it) const {
  867. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  868. return it_ < it.it_;
  869. }
  870. private:
  871. const_subiterator_type it_;
  872. friend class iterator2;
  873. };
  874. #endif
  875. BOOST_UBLAS_INLINE
  876. const_iterator2 begin2 () const {
  877. return find2 (0, 0, 0);
  878. }
  879. BOOST_UBLAS_INLINE
  880. const_iterator2 cbegin2 () const {
  881. return begin2 ();
  882. }
  883. BOOST_UBLAS_INLINE
  884. const_iterator2 end2 () const {
  885. return find2 (0, 0, size2_);
  886. }
  887. BOOST_UBLAS_INLINE
  888. const_iterator2 cend2 () const {
  889. return end2 ();
  890. }
  891. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  892. class iterator2:
  893. public container_reference<matrix>,
  894. public random_access_iterator_base<dense_random_access_iterator_tag,
  895. iterator2, value_type> {
  896. public:
  897. typedef typename matrix::value_type value_type;
  898. typedef typename matrix::difference_type difference_type;
  899. typedef typename matrix::reference reference;
  900. typedef typename matrix::pointer pointer;
  901. typedef iterator1 dual_iterator_type;
  902. typedef reverse_iterator1 dual_reverse_iterator_type;
  903. // Construction and destruction
  904. BOOST_UBLAS_INLINE
  905. iterator2 ():
  906. container_reference<self_type> (), it_ () {}
  907. BOOST_UBLAS_INLINE
  908. iterator2 (self_type &m, const subiterator_type &it):
  909. container_reference<self_type> (m), it_ (it) {}
  910. // Arithmetic
  911. BOOST_UBLAS_INLINE
  912. iterator2 &operator ++ () {
  913. layout_type::increment_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  914. return *this;
  915. }
  916. BOOST_UBLAS_INLINE
  917. iterator2 &operator -- () {
  918. layout_type::decrement_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  919. return *this;
  920. }
  921. BOOST_UBLAS_INLINE
  922. iterator2 &operator += (difference_type n) {
  923. layout_type::increment_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  924. return *this;
  925. }
  926. BOOST_UBLAS_INLINE
  927. iterator2 &operator -= (difference_type n) {
  928. layout_type::decrement_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  929. return *this;
  930. }
  931. BOOST_UBLAS_INLINE
  932. difference_type operator - (const iterator2 &it) const {
  933. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  934. return layout_type::distance_j (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  935. }
  936. // Dereference
  937. BOOST_UBLAS_INLINE
  938. reference operator * () const {
  939. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  940. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  941. return *it_;
  942. }
  943. BOOST_UBLAS_INLINE
  944. reference operator [] (difference_type n) const {
  945. return *(*this + n);
  946. }
  947. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  948. BOOST_UBLAS_INLINE
  949. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  950. typename self_type::
  951. #endif
  952. iterator1 begin () const {
  953. self_type &m = (*this) ();
  954. return m.find1 (1, 0, index2 ());
  955. }
  956. BOOST_UBLAS_INLINE
  957. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  958. typename self_type::
  959. #endif
  960. iterator1 end () const {
  961. self_type &m = (*this) ();
  962. return m.find1 (1, m.size1 (), index2 ());
  963. }
  964. BOOST_UBLAS_INLINE
  965. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  966. typename self_type::
  967. #endif
  968. reverse_iterator1 rbegin () const {
  969. return reverse_iterator1 (end ());
  970. }
  971. BOOST_UBLAS_INLINE
  972. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  973. typename self_type::
  974. #endif
  975. reverse_iterator1 rend () const {
  976. return reverse_iterator1 (begin ());
  977. }
  978. #endif
  979. // Indices
  980. BOOST_UBLAS_INLINE
  981. size_type index1 () const {
  982. self_type &m = (*this) ();
  983. return layout_type::index_i (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  984. }
  985. BOOST_UBLAS_INLINE
  986. size_type index2 () const {
  987. self_type &m = (*this) ();
  988. return layout_type::index_j (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  989. }
  990. // Assignment
  991. BOOST_UBLAS_INLINE
  992. iterator2 &operator = (const iterator2 &it) {
  993. container_reference<self_type>::assign (&it ());
  994. it_ = it.it_;
  995. return *this;
  996. }
  997. // Comparison
  998. BOOST_UBLAS_INLINE
  999. bool operator == (const iterator2 &it) const {
  1000. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1001. return it_ == it.it_;
  1002. }
  1003. BOOST_UBLAS_INLINE
  1004. bool operator < (const iterator2 &it) const {
  1005. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1006. return it_ < it.it_;
  1007. }
  1008. private:
  1009. subiterator_type it_;
  1010. friend class const_iterator2;
  1011. };
  1012. #endif
  1013. BOOST_UBLAS_INLINE
  1014. iterator2 begin2 () {
  1015. return find2 (0, 0, 0);
  1016. }
  1017. BOOST_UBLAS_INLINE
  1018. iterator2 end2 () {
  1019. return find2 (0, 0, size2_);
  1020. }
  1021. // Reverse iterators
  1022. BOOST_UBLAS_INLINE
  1023. const_reverse_iterator1 rbegin1 () const {
  1024. return const_reverse_iterator1 (end1 ());
  1025. }
  1026. BOOST_UBLAS_INLINE
  1027. const_reverse_iterator1 crbegin1 () const {
  1028. return rbegin1 ();
  1029. }
  1030. BOOST_UBLAS_INLINE
  1031. const_reverse_iterator1 rend1 () const {
  1032. return const_reverse_iterator1 (begin1 ());
  1033. }
  1034. BOOST_UBLAS_INLINE
  1035. const_reverse_iterator1 crend1 () const {
  1036. return rend1 ();
  1037. }
  1038. BOOST_UBLAS_INLINE
  1039. reverse_iterator1 rbegin1 () {
  1040. return reverse_iterator1 (end1 ());
  1041. }
  1042. BOOST_UBLAS_INLINE
  1043. reverse_iterator1 rend1 () {
  1044. return reverse_iterator1 (begin1 ());
  1045. }
  1046. BOOST_UBLAS_INLINE
  1047. const_reverse_iterator2 rbegin2 () const {
  1048. return const_reverse_iterator2 (end2 ());
  1049. }
  1050. BOOST_UBLAS_INLINE
  1051. const_reverse_iterator2 crbegin2 () const {
  1052. return rbegin2 ();
  1053. }
  1054. BOOST_UBLAS_INLINE
  1055. const_reverse_iterator2 rend2 () const {
  1056. return const_reverse_iterator2 (begin2 ());
  1057. }
  1058. BOOST_UBLAS_INLINE
  1059. const_reverse_iterator2 crend2 () const {
  1060. return rend2 ();
  1061. }
  1062. BOOST_UBLAS_INLINE
  1063. reverse_iterator2 rbegin2 () {
  1064. return reverse_iterator2 (end2 ());
  1065. }
  1066. BOOST_UBLAS_INLINE
  1067. reverse_iterator2 rend2 () {
  1068. return reverse_iterator2 (begin2 ());
  1069. }
  1070. // Serialization
  1071. template<class Archive>
  1072. void serialize(Archive & ar, const unsigned int /* file_version */){
  1073. // we need to copy to a collection_size_type to get a portable
  1074. // and efficient serialization
  1075. serialization::collection_size_type s1 (size1_);
  1076. serialization::collection_size_type s2 (size2_);
  1077. // serialize the sizes
  1078. ar & serialization::make_nvp("size1",s1)
  1079. & serialization::make_nvp("size2",s2);
  1080. // copy the values back if loading
  1081. if (Archive::is_loading::value) {
  1082. size1_ = s1;
  1083. size2_ = s2;
  1084. }
  1085. ar & serialization::make_nvp("data",data_);
  1086. }
  1087. private:
  1088. size_type size1_;
  1089. size_type size2_;
  1090. array_type data_;
  1091. };
  1092. #ifdef BOOST_UBLAS_CPP_GE_2011
  1093. /** \brief A fixed size dense matrix of values of type \c T. Equivalent to a c-style 2 dimensional array.
  1094. *
  1095. * For a \f$(m \times n)\f$-dimensional fixed_matrix and \f$ 0 \leq i < m, 0 \leq j < n\f$, every element \f$ m_{i,j} \f$ is mapped to
  1096. * the \f$(i.n + j)\f$-th element of the container for row major orientation or the \f$ (i + j.m) \f$-th element of
  1097. * the container for column major orientation. In a dense matrix all elements are represented in memory in a
  1098. * contiguous chunk of memory by definition.
  1099. *
  1100. * Orientation and storage can also be specified, otherwise \c row_major and \c std::array are used. It is \b not
  1101. * required by the storage container to initialize elements of the matrix.
  1102. *
  1103. * \tparam T the type of object stored in the matrix (like double, float, std::complex<double>, etc...)
  1104. * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major
  1105. * \tparam A the type of Storage array. Default is \c std::array<T, M*N>
  1106. */
  1107. template<class T, std::size_t M, std::size_t N, class L, class A>
  1108. class fixed_matrix:
  1109. public matrix_container<fixed_matrix<T, M, N, L, A> > {
  1110. typedef T *pointer;
  1111. typedef L layout_type;
  1112. typedef fixed_matrix<T, M, N, L, A> self_type;
  1113. public:
  1114. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  1115. using matrix_container<self_type>::operator ();
  1116. #endif
  1117. typedef typename A::size_type size_type;
  1118. typedef typename A::difference_type difference_type;
  1119. typedef T value_type;
  1120. typedef const T &const_reference;
  1121. typedef T &reference;
  1122. typedef A array_type;
  1123. typedef const matrix_reference<const self_type> const_closure_type;
  1124. typedef matrix_reference<self_type> closure_type;
  1125. typedef vector<T, A> vector_temporary_type;
  1126. typedef self_type matrix_temporary_type;
  1127. typedef dense_tag storage_category;
  1128. // This could be better for performance,
  1129. // typedef typename unknown_orientation_tag orientation_category;
  1130. // but others depend on the orientation information...
  1131. typedef typename L::orientation_category orientation_category;
  1132. // Construction and destruction
  1133. /// Default dense fixed_matrix constructor. Make a dense fixed_matrix of size M x N
  1134. BOOST_UBLAS_INLINE
  1135. fixed_matrix ():
  1136. matrix_container<self_type> (),
  1137. data_ () {}
  1138. /// \brief Construct a fixed_matrix from a list of values
  1139. /// The list may be included in curly braces. Typical syntax is choices are :
  1140. /// fixed_matrix<double, 2,2> v = { 1, 2, 3, 4 } or fixed_matrix<double,4> v( {1, 2, 3, 4} ) or fixed_matrix<double,2,2> v( 1, 2, 3, 4 )
  1141. template <typename... Types>
  1142. fixed_matrix(value_type v0, Types... vrest) :
  1143. matrix_container<self_type> (),
  1144. data_{ { v0, vrest... } } {}
  1145. /** Dense fixed_matrix constructor with defined initial value for all the matrix elements
  1146. * \param init initial value assigned to all elements
  1147. */
  1148. fixed_matrix (const value_type &init):
  1149. matrix_container<self_type> (),
  1150. data_ ( ) {
  1151. data_.fill(init);
  1152. }
  1153. /** Dense matrix constructor with defined initial data array
  1154. * \param data array to copy into the matrix. Must have the same dimension as the matrix
  1155. */
  1156. BOOST_UBLAS_INLINE
  1157. fixed_matrix (const array_type &data):
  1158. matrix_container<self_type> (),
  1159. data_ (data) {}
  1160. /** Copy-constructor of a dense fixed_matrix
  1161. * \param m is a dense fixed_matrix
  1162. */
  1163. BOOST_UBLAS_INLINE
  1164. fixed_matrix (const fixed_matrix &m):
  1165. matrix_container<self_type> (),
  1166. data_ (m.data_) {}
  1167. /** Copy-constructor of a dense matrix from a matrix expression
  1168. * \param ae is a matrix expression
  1169. */
  1170. template<class AE>
  1171. BOOST_UBLAS_INLINE
  1172. fixed_matrix (const matrix_expression<AE> &ae):
  1173. matrix_container<self_type> (),
  1174. data_ () {
  1175. matrix_assign<scalar_assign> (*this, ae);
  1176. }
  1177. // Accessors
  1178. /** Return the number of rows of the fixed_matrix
  1179. * You can also use the free size<>() function in operation/size.hpp as size<1>(m) where m is a fixed_matrix
  1180. */
  1181. BOOST_UBLAS_INLINE
  1182. BOOST_CONSTEXPR size_type size1 () const {
  1183. return M;
  1184. }
  1185. /** Return the number of colums of the fixed_matrix
  1186. * You can also use the free size<>() function in operation/size.hpp as size<2>(m) where m is a fixed_matrix
  1187. */
  1188. BOOST_UBLAS_INLINE
  1189. BOOST_CONSTEXPR size_type size2 () const {
  1190. return N;
  1191. }
  1192. // Storage accessors
  1193. /** Return a constant reference to the internal storage of a dense matrix, i.e. the raw data
  1194. * It's type depends on the type used by the matrix to store its data
  1195. */
  1196. BOOST_UBLAS_INLINE
  1197. const array_type &data () const {
  1198. return data_;
  1199. }
  1200. /** Return a reference to the internal storage of a dense fixed_matrix, i.e. the raw data
  1201. * It's type depends on the type used by the fixed_matrix to store its data
  1202. */
  1203. BOOST_UBLAS_INLINE
  1204. array_type &data () {
  1205. return data_;
  1206. }
  1207. // Element access
  1208. /** Access a fixed_matrix element. Here we return a const reference
  1209. * \param i the first coordinate of the element. By default it's the row
  1210. * \param j the second coordinate of the element. By default it's the column
  1211. * \return a const reference to the element
  1212. */
  1213. BOOST_UBLAS_INLINE
  1214. const_reference operator () (size_type i, size_type j) const {
  1215. return data () [layout_type::element (i, M, j, N)]; // Fixme: add static lookup for element(...) i.e.: element<M, N>(i,j)
  1216. }
  1217. /** Access a fixed_matrix element. Here we return a reference
  1218. * \param i the first coordinate of the element. By default it's the row
  1219. * \param j the second coordinate of the element. By default it's the column
  1220. * \return a reference to the element
  1221. */
  1222. BOOST_UBLAS_INLINE
  1223. reference at_element (size_type i, size_type j) {
  1224. return data () [layout_type::element (i, M, j, N)];
  1225. }
  1226. /** Access a fixed_matrix element. Here we return a reference
  1227. * \param i the first coordinate of the element. By default it's the row
  1228. * \param j the second coordinate of the element. By default it's the column
  1229. * \return a reference to the element
  1230. */
  1231. BOOST_UBLAS_INLINE
  1232. reference operator () (size_type i, size_type j) {
  1233. return at_element (i, j);
  1234. }
  1235. // Element assignment
  1236. /** Change the value of a fixed_matrix element. Return back a reference to it
  1237. * \param i the first coordinate of the element. By default it's the row
  1238. * \param j the second coordinate of the element. By default it's the column
  1239. * \param t the new value of the element
  1240. * \return a reference to the newly changed element
  1241. */
  1242. BOOST_UBLAS_INLINE
  1243. reference insert_element (size_type i, size_type j, const_reference t) {
  1244. return (at_element (i, j) = t);
  1245. }
  1246. /** Erase the element
  1247. * For most types (int, double, etc...) it means setting 0 (zero) the element at zero in fact.
  1248. * For user-defined types, it could be another value if you decided it. Your type in that case must
  1249. * contain a default null value.
  1250. * \param i the first coordinate of the element. By default it's the row
  1251. * \param j the second coordinate of the element. By default it's the column
  1252. */
  1253. void erase_element (size_type i, size_type j) {
  1254. at_element (i, j) = value_type/*zero*/();
  1255. }
  1256. // Zeroing
  1257. /** Erase all elements in the fixed_matrix
  1258. * For most types (int, double, etc...) it means writing 0 (zero) everywhere.
  1259. * For user-defined types, it could be another value if you decided it. Your type in that case must
  1260. * contain a default null value.
  1261. */
  1262. BOOST_UBLAS_INLINE
  1263. void clear () {
  1264. std::fill (data ().begin (), data ().end (), value_type/*zero*/());
  1265. }
  1266. // Assignment
  1267. #ifdef BOOST_UBLAS_MOVE_SEMANTICS
  1268. /*! @note "pass by value" the key idea to enable move semantics */
  1269. BOOST_UBLAS_INLINE
  1270. fixed_matrix &operator = (matrix m) {
  1271. assign_temporary(m);
  1272. return *this;
  1273. }
  1274. #else
  1275. BOOST_UBLAS_INLINE
  1276. fixed_matrix &operator = (const fixed_matrix &m) {
  1277. data () = m.data ();
  1278. return *this;
  1279. }
  1280. #endif
  1281. template<class C> // Container assignment without temporary
  1282. BOOST_UBLAS_INLINE
  1283. fixed_matrix &operator = (const matrix_container<C> &m) {
  1284. resize (m ().size1 (), m ().size2 (), false);
  1285. assign (m);
  1286. return *this;
  1287. }
  1288. BOOST_UBLAS_INLINE
  1289. fixed_matrix &assign_temporary (fixed_matrix &m) {
  1290. swap (m);
  1291. return *this;
  1292. }
  1293. template<class AE>
  1294. BOOST_UBLAS_INLINE
  1295. fixed_matrix &operator = (const matrix_expression<AE> &ae) {
  1296. self_type temporary (ae);
  1297. return assign_temporary (temporary);
  1298. }
  1299. template<class AE>
  1300. BOOST_UBLAS_INLINE
  1301. fixed_matrix &assign (const matrix_expression<AE> &ae) {
  1302. matrix_assign<scalar_assign> (*this, ae);
  1303. return *this;
  1304. }
  1305. template<class AE>
  1306. BOOST_UBLAS_INLINE
  1307. fixed_matrix& operator += (const matrix_expression<AE> &ae) {
  1308. self_type temporary (*this + ae);
  1309. return assign_temporary (temporary);
  1310. }
  1311. template<class C> // Container assignment without temporary
  1312. BOOST_UBLAS_INLINE
  1313. fixed_matrix &operator += (const matrix_container<C> &m) {
  1314. plus_assign (m);
  1315. return *this;
  1316. }
  1317. template<class AE>
  1318. BOOST_UBLAS_INLINE
  1319. fixed_matrix &plus_assign (const matrix_expression<AE> &ae) {
  1320. matrix_assign<scalar_plus_assign> (*this, ae);
  1321. return *this;
  1322. }
  1323. template<class AE>
  1324. BOOST_UBLAS_INLINE
  1325. fixed_matrix& operator -= (const matrix_expression<AE> &ae) {
  1326. self_type temporary (*this - ae);
  1327. return assign_temporary (temporary);
  1328. }
  1329. template<class C> // Container assignment without temporary
  1330. BOOST_UBLAS_INLINE
  1331. fixed_matrix &operator -= (const matrix_container<C> &m) {
  1332. minus_assign (m);
  1333. return *this;
  1334. }
  1335. template<class AE>
  1336. BOOST_UBLAS_INLINE
  1337. fixed_matrix &minus_assign (const matrix_expression<AE> &ae) {
  1338. matrix_assign<scalar_minus_assign> (*this, ae);
  1339. return *this;
  1340. }
  1341. template<class AT>
  1342. BOOST_UBLAS_INLINE
  1343. fixed_matrix& operator *= (const AT &at) {
  1344. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  1345. return *this;
  1346. }
  1347. template<class AT>
  1348. BOOST_UBLAS_INLINE
  1349. fixed_matrix& operator /= (const AT &at) {
  1350. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  1351. return *this;
  1352. }
  1353. // Swapping
  1354. BOOST_UBLAS_INLINE
  1355. void swap (fixed_matrix &m) {
  1356. if (this != &m) {
  1357. data ().swap (m.data ());
  1358. }
  1359. }
  1360. BOOST_UBLAS_INLINE
  1361. friend void swap (fixed_matrix &m1, fixed_matrix &m2) {
  1362. m1.swap (m2);
  1363. }
  1364. // Iterator types
  1365. private:
  1366. // Use the storage array iterator
  1367. typedef typename A::const_iterator const_subiterator_type;
  1368. typedef typename A::iterator subiterator_type;
  1369. public:
  1370. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1371. typedef indexed_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  1372. typedef indexed_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  1373. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  1374. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  1375. #else
  1376. class const_iterator1;
  1377. class iterator1;
  1378. class const_iterator2;
  1379. class iterator2;
  1380. #endif
  1381. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  1382. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  1383. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  1384. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  1385. // Element lookup
  1386. BOOST_UBLAS_INLINE
  1387. const_iterator1 find1 (int /* rank */, size_type i, size_type j) const {
  1388. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1389. return const_iterator1 (*this, i, j);
  1390. #else
  1391. return const_iterator1 (*this, data ().begin () + layout_type::address (i, M, j, N));
  1392. #endif
  1393. }
  1394. BOOST_UBLAS_INLINE
  1395. iterator1 find1 (int /* rank */, size_type i, size_type j) {
  1396. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1397. return iterator1 (*this, i, j);
  1398. #else
  1399. return iterator1 (*this, data ().begin () + layout_type::address (i, M, j, N));
  1400. #endif
  1401. }
  1402. BOOST_UBLAS_INLINE
  1403. const_iterator2 find2 (int /* rank */, size_type i, size_type j) const {
  1404. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1405. return const_iterator2 (*this, i, j);
  1406. #else
  1407. return const_iterator2 (*this, data ().begin () + layout_type::address (i, M, j, N));
  1408. #endif
  1409. }
  1410. BOOST_UBLAS_INLINE
  1411. iterator2 find2 (int /* rank */, size_type i, size_type j) {
  1412. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1413. return iterator2 (*this, i, j);
  1414. #else
  1415. return iterator2 (*this, data ().begin () + layout_type::address (i, M, j, N));
  1416. #endif
  1417. }
  1418. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1419. class const_iterator1:
  1420. public container_const_reference<fixed_matrix>,
  1421. public random_access_iterator_base<dense_random_access_iterator_tag,
  1422. const_iterator1, value_type> {
  1423. public:
  1424. typedef typename fixed_matrix::value_type value_type;
  1425. typedef typename fixed_matrix::difference_type difference_type;
  1426. typedef typename fixed_matrix::const_reference reference;
  1427. typedef const typename fixed_matrix::pointer pointer;
  1428. typedef const_iterator2 dual_iterator_type;
  1429. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  1430. // Construction and destruction
  1431. BOOST_UBLAS_INLINE
  1432. const_iterator1 ():
  1433. container_const_reference<self_type> (), it_ () {}
  1434. BOOST_UBLAS_INLINE
  1435. const_iterator1 (const self_type &m, const const_subiterator_type &it):
  1436. container_const_reference<self_type> (m), it_ (it) {}
  1437. BOOST_UBLAS_INLINE
  1438. const_iterator1 (const iterator1 &it):
  1439. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  1440. // Arithmetic
  1441. BOOST_UBLAS_INLINE
  1442. const_iterator1 &operator ++ () {
  1443. layout_type::increment_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  1444. return *this;
  1445. }
  1446. BOOST_UBLAS_INLINE
  1447. const_iterator1 &operator -- () {
  1448. layout_type::decrement_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  1449. return *this;
  1450. }
  1451. BOOST_UBLAS_INLINE
  1452. const_iterator1 &operator += (difference_type n) {
  1453. layout_type::increment_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  1454. return *this;
  1455. }
  1456. BOOST_UBLAS_INLINE
  1457. const_iterator1 &operator -= (difference_type n) {
  1458. layout_type::decrement_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  1459. return *this;
  1460. }
  1461. BOOST_UBLAS_INLINE
  1462. difference_type operator - (const const_iterator1 &it) const {
  1463. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1464. return layout_type::distance_i (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  1465. }
  1466. // Dereference
  1467. BOOST_UBLAS_INLINE
  1468. const_reference operator * () const {
  1469. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1470. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1471. return *it_;
  1472. }
  1473. BOOST_UBLAS_INLINE
  1474. const_reference operator [] (difference_type n) const {
  1475. return *(*this + n);
  1476. }
  1477. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1478. BOOST_UBLAS_INLINE
  1479. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1480. typename self_type::
  1481. #endif
  1482. const_iterator2 begin () const {
  1483. const self_type &m = (*this) ();
  1484. return m.find2 (1, index1 (), 0);
  1485. }
  1486. BOOST_UBLAS_INLINE
  1487. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1488. typename self_type::
  1489. #endif
  1490. const_iterator2 cbegin () const {
  1491. return begin ();
  1492. }
  1493. BOOST_UBLAS_INLINE
  1494. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1495. typename self_type::
  1496. #endif
  1497. const_iterator2 end () const {
  1498. const self_type &m = (*this) ();
  1499. return m.find2 (1, index1 (), m.size2 ());
  1500. }
  1501. BOOST_UBLAS_INLINE
  1502. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1503. typename self_type::
  1504. #endif
  1505. const_iterator2 cend () const {
  1506. return end ();
  1507. }
  1508. BOOST_UBLAS_INLINE
  1509. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1510. typename self_type::
  1511. #endif
  1512. const_reverse_iterator2 rbegin () const {
  1513. return const_reverse_iterator2 (end ());
  1514. }
  1515. BOOST_UBLAS_INLINE
  1516. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1517. typename self_type::
  1518. #endif
  1519. const_reverse_iterator2 crbegin () const {
  1520. return rbegin ();
  1521. }
  1522. BOOST_UBLAS_INLINE
  1523. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1524. typename self_type::
  1525. #endif
  1526. const_reverse_iterator2 rend () const {
  1527. return const_reverse_iterator2 (begin ());
  1528. }
  1529. BOOST_UBLAS_INLINE
  1530. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1531. typename self_type::
  1532. #endif
  1533. const_reverse_iterator2 crend () const {
  1534. return rend ();
  1535. }
  1536. #endif
  1537. // Indices
  1538. BOOST_UBLAS_INLINE
  1539. size_type index1 () const {
  1540. const self_type &m = (*this) ();
  1541. return layout_type::index_i (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  1542. }
  1543. BOOST_UBLAS_INLINE
  1544. size_type index2 () const {
  1545. const self_type &m = (*this) ();
  1546. return layout_type::index_j (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  1547. }
  1548. // Assignment
  1549. BOOST_UBLAS_INLINE
  1550. const_iterator1 &operator = (const const_iterator1 &it) {
  1551. container_const_reference<self_type>::assign (&it ());
  1552. it_ = it.it_;
  1553. return *this;
  1554. }
  1555. // Comparison
  1556. BOOST_UBLAS_INLINE
  1557. bool operator == (const const_iterator1 &it) const {
  1558. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1559. return it_ == it.it_;
  1560. }
  1561. BOOST_UBLAS_INLINE
  1562. bool operator < (const const_iterator1 &it) const {
  1563. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1564. return it_ < it.it_;
  1565. }
  1566. private:
  1567. const_subiterator_type it_;
  1568. friend class iterator1;
  1569. };
  1570. #endif
  1571. BOOST_UBLAS_INLINE
  1572. const_iterator1 begin1 () const {
  1573. return find1 (0, 0, 0);
  1574. }
  1575. BOOST_UBLAS_INLINE
  1576. const_iterator1 cbegin1 () const {
  1577. return begin1 ();
  1578. }
  1579. BOOST_UBLAS_INLINE
  1580. const_iterator1 end1 () const {
  1581. return find1 (0, M, 0);
  1582. }
  1583. BOOST_UBLAS_INLINE
  1584. const_iterator1 cend1 () const {
  1585. return end1 ();
  1586. }
  1587. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1588. class iterator1:
  1589. public container_reference<fixed_matrix>,
  1590. public random_access_iterator_base<dense_random_access_iterator_tag,
  1591. iterator1, value_type> {
  1592. public:
  1593. typedef typename fixed_matrix::value_type value_type;
  1594. typedef typename fixed_matrix::difference_type difference_type;
  1595. typedef typename fixed_matrix::reference reference;
  1596. typedef typename fixed_matrix::pointer pointer;
  1597. typedef iterator2 dual_iterator_type;
  1598. typedef reverse_iterator2 dual_reverse_iterator_type;
  1599. // Construction and destruction
  1600. BOOST_UBLAS_INLINE
  1601. iterator1 ():
  1602. container_reference<self_type> (), it_ () {}
  1603. BOOST_UBLAS_INLINE
  1604. iterator1 (self_type &m, const subiterator_type &it):
  1605. container_reference<self_type> (m), it_ (it) {}
  1606. // Arithmetic
  1607. BOOST_UBLAS_INLINE
  1608. iterator1 &operator ++ () {
  1609. layout_type::increment_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  1610. return *this;
  1611. }
  1612. BOOST_UBLAS_INLINE
  1613. iterator1 &operator -- () {
  1614. layout_type::decrement_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  1615. return *this;
  1616. }
  1617. BOOST_UBLAS_INLINE
  1618. iterator1 &operator += (difference_type n) {
  1619. layout_type::increment_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  1620. return *this;
  1621. }
  1622. BOOST_UBLAS_INLINE
  1623. iterator1 &operator -= (difference_type n) {
  1624. layout_type::decrement_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  1625. return *this;
  1626. }
  1627. BOOST_UBLAS_INLINE
  1628. difference_type operator - (const iterator1 &it) const {
  1629. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1630. return layout_type::distance_i (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  1631. }
  1632. // Dereference
  1633. BOOST_UBLAS_INLINE
  1634. reference operator * () const {
  1635. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1636. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1637. return *it_;
  1638. }
  1639. BOOST_UBLAS_INLINE
  1640. reference operator [] (difference_type n) const {
  1641. return *(*this + n);
  1642. }
  1643. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1644. BOOST_UBLAS_INLINE
  1645. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1646. typename self_type::
  1647. #endif
  1648. iterator2 begin () const {
  1649. self_type &m = (*this) ();
  1650. return m.find2 (1, index1 (), 0);
  1651. }
  1652. BOOST_UBLAS_INLINE
  1653. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1654. typename self_type::
  1655. #endif
  1656. iterator2 end () const {
  1657. self_type &m = (*this) ();
  1658. return m.find2 (1, index1 (), m.size2 ());
  1659. }
  1660. BOOST_UBLAS_INLINE
  1661. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1662. typename self_type::
  1663. #endif
  1664. reverse_iterator2 rbegin () const {
  1665. return reverse_iterator2 (end ());
  1666. }
  1667. BOOST_UBLAS_INLINE
  1668. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1669. typename self_type::
  1670. #endif
  1671. reverse_iterator2 rend () const {
  1672. return reverse_iterator2 (begin ());
  1673. }
  1674. #endif
  1675. // Indices
  1676. BOOST_UBLAS_INLINE
  1677. size_type index1 () const {
  1678. self_type &m = (*this) ();
  1679. return layout_type::index_i (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  1680. }
  1681. BOOST_UBLAS_INLINE
  1682. size_type index2 () const {
  1683. self_type &m = (*this) ();
  1684. return layout_type::index_j (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  1685. }
  1686. // Assignment
  1687. BOOST_UBLAS_INLINE
  1688. iterator1 &operator = (const iterator1 &it) {
  1689. container_reference<self_type>::assign (&it ());
  1690. it_ = it.it_;
  1691. return *this;
  1692. }
  1693. // Comparison
  1694. BOOST_UBLAS_INLINE
  1695. bool operator == (const iterator1 &it) const {
  1696. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1697. return it_ == it.it_;
  1698. }
  1699. BOOST_UBLAS_INLINE
  1700. bool operator < (const iterator1 &it) const {
  1701. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1702. return it_ < it.it_;
  1703. }
  1704. private:
  1705. subiterator_type it_;
  1706. friend class const_iterator1;
  1707. };
  1708. #endif
  1709. BOOST_UBLAS_INLINE
  1710. iterator1 begin1 () {
  1711. return find1 (0, 0, 0);
  1712. }
  1713. BOOST_UBLAS_INLINE
  1714. iterator1 end1 () {
  1715. return find1 (0, M, 0);
  1716. }
  1717. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1718. class const_iterator2:
  1719. public container_const_reference<fixed_matrix>,
  1720. public random_access_iterator_base<dense_random_access_iterator_tag,
  1721. const_iterator2, value_type> {
  1722. public:
  1723. typedef typename fixed_matrix::value_type value_type;
  1724. typedef typename fixed_matrix::difference_type difference_type;
  1725. typedef typename fixed_matrix::const_reference reference;
  1726. typedef const typename fixed_matrix::pointer pointer;
  1727. typedef const_iterator1 dual_iterator_type;
  1728. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  1729. // Construction and destruction
  1730. BOOST_UBLAS_INLINE
  1731. const_iterator2 ():
  1732. container_const_reference<self_type> (), it_ () {}
  1733. BOOST_UBLAS_INLINE
  1734. const_iterator2 (const self_type &m, const const_subiterator_type &it):
  1735. container_const_reference<self_type> (m), it_ (it) {}
  1736. BOOST_UBLAS_INLINE
  1737. const_iterator2 (const iterator2 &it):
  1738. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  1739. // Arithmetic
  1740. BOOST_UBLAS_INLINE
  1741. const_iterator2 &operator ++ () {
  1742. layout_type::increment_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  1743. return *this;
  1744. }
  1745. BOOST_UBLAS_INLINE
  1746. const_iterator2 &operator -- () {
  1747. layout_type::decrement_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  1748. return *this;
  1749. }
  1750. BOOST_UBLAS_INLINE
  1751. const_iterator2 &operator += (difference_type n) {
  1752. layout_type::increment_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  1753. return *this;
  1754. }
  1755. BOOST_UBLAS_INLINE
  1756. const_iterator2 &operator -= (difference_type n) {
  1757. layout_type::decrement_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  1758. return *this;
  1759. }
  1760. BOOST_UBLAS_INLINE
  1761. difference_type operator - (const const_iterator2 &it) const {
  1762. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1763. return layout_type::distance_j (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  1764. }
  1765. // Dereference
  1766. BOOST_UBLAS_INLINE
  1767. const_reference operator * () const {
  1768. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1769. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1770. return *it_;
  1771. }
  1772. BOOST_UBLAS_INLINE
  1773. const_reference operator [] (difference_type n) const {
  1774. return *(*this + n);
  1775. }
  1776. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1777. BOOST_UBLAS_INLINE
  1778. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1779. typename self_type::
  1780. #endif
  1781. const_iterator1 begin () const {
  1782. const self_type &m = (*this) ();
  1783. return m.find1 (1, 0, index2 ());
  1784. }
  1785. BOOST_UBLAS_INLINE
  1786. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1787. typename self_type::
  1788. #endif
  1789. const_iterator1 cbegin () const {
  1790. return begin ();
  1791. }
  1792. BOOST_UBLAS_INLINE
  1793. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1794. typename self_type::
  1795. #endif
  1796. const_iterator1 end () const {
  1797. const self_type &m = (*this) ();
  1798. return m.find1 (1, m.size1 (), index2 ());
  1799. }
  1800. BOOST_UBLAS_INLINE
  1801. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1802. typename self_type::
  1803. #endif
  1804. const_iterator1 cend () const {
  1805. return end ();
  1806. }
  1807. BOOST_UBLAS_INLINE
  1808. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1809. typename self_type::
  1810. #endif
  1811. const_reverse_iterator1 rbegin () const {
  1812. return const_reverse_iterator1 (end ());
  1813. }
  1814. BOOST_UBLAS_INLINE
  1815. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1816. typename self_type::
  1817. #endif
  1818. const_reverse_iterator1 crbegin () const {
  1819. return rbegin ();
  1820. }
  1821. BOOST_UBLAS_INLINE
  1822. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1823. typename self_type::
  1824. #endif
  1825. const_reverse_iterator1 rend () const {
  1826. return const_reverse_iterator1 (begin ());
  1827. }
  1828. BOOST_UBLAS_INLINE
  1829. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1830. typename self_type::
  1831. #endif
  1832. const_reverse_iterator1 crend () const {
  1833. return rend ();
  1834. }
  1835. #endif
  1836. // Indices
  1837. BOOST_UBLAS_INLINE
  1838. size_type index1 () const {
  1839. const self_type &m = (*this) ();
  1840. return layout_type::index_i (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  1841. }
  1842. BOOST_UBLAS_INLINE
  1843. size_type index2 () const {
  1844. const self_type &m = (*this) ();
  1845. return layout_type::index_j (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  1846. }
  1847. // Assignment
  1848. BOOST_UBLAS_INLINE
  1849. const_iterator2 &operator = (const const_iterator2 &it) {
  1850. container_const_reference<self_type>::assign (&it ());
  1851. it_ = it.it_;
  1852. return *this;
  1853. }
  1854. // Comparison
  1855. BOOST_UBLAS_INLINE
  1856. bool operator == (const const_iterator2 &it) const {
  1857. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1858. return it_ == it.it_;
  1859. }
  1860. BOOST_UBLAS_INLINE
  1861. bool operator < (const const_iterator2 &it) const {
  1862. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1863. return it_ < it.it_;
  1864. }
  1865. private:
  1866. const_subiterator_type it_;
  1867. friend class iterator2;
  1868. };
  1869. #endif
  1870. BOOST_UBLAS_INLINE
  1871. const_iterator2 begin2 () const {
  1872. return find2 (0, 0, 0);
  1873. }
  1874. BOOST_UBLAS_INLINE
  1875. const_iterator2 cbegin2 () const {
  1876. return begin2 ();
  1877. }
  1878. BOOST_UBLAS_INLINE
  1879. const_iterator2 end2 () const {
  1880. return find2 (0, 0, N);
  1881. }
  1882. BOOST_UBLAS_INLINE
  1883. const_iterator2 cend2 () const {
  1884. return end2 ();
  1885. }
  1886. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1887. class iterator2:
  1888. public container_reference<fixed_matrix>,
  1889. public random_access_iterator_base<dense_random_access_iterator_tag,
  1890. iterator2, value_type> {
  1891. public:
  1892. typedef typename fixed_matrix::value_type value_type;
  1893. typedef typename fixed_matrix::difference_type difference_type;
  1894. typedef typename fixed_matrix::reference reference;
  1895. typedef typename fixed_matrix::pointer pointer;
  1896. typedef iterator1 dual_iterator_type;
  1897. typedef reverse_iterator1 dual_reverse_iterator_type;
  1898. // Construction and destruction
  1899. BOOST_UBLAS_INLINE
  1900. iterator2 ():
  1901. container_reference<self_type> (), it_ () {}
  1902. BOOST_UBLAS_INLINE
  1903. iterator2 (self_type &m, const subiterator_type &it):
  1904. container_reference<self_type> (m), it_ (it) {}
  1905. // Arithmetic
  1906. BOOST_UBLAS_INLINE
  1907. iterator2 &operator ++ () {
  1908. layout_type::increment_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  1909. return *this;
  1910. }
  1911. BOOST_UBLAS_INLINE
  1912. iterator2 &operator -- () {
  1913. layout_type::decrement_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  1914. return *this;
  1915. }
  1916. BOOST_UBLAS_INLINE
  1917. iterator2 &operator += (difference_type n) {
  1918. layout_type::increment_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  1919. return *this;
  1920. }
  1921. BOOST_UBLAS_INLINE
  1922. iterator2 &operator -= (difference_type n) {
  1923. layout_type::decrement_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  1924. return *this;
  1925. }
  1926. BOOST_UBLAS_INLINE
  1927. difference_type operator - (const iterator2 &it) const {
  1928. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1929. return layout_type::distance_j (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  1930. }
  1931. // Dereference
  1932. BOOST_UBLAS_INLINE
  1933. reference operator * () const {
  1934. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1935. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1936. return *it_;
  1937. }
  1938. BOOST_UBLAS_INLINE
  1939. reference operator [] (difference_type n) const {
  1940. return *(*this + n);
  1941. }
  1942. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1943. BOOST_UBLAS_INLINE
  1944. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1945. typename self_type::
  1946. #endif
  1947. iterator1 begin () const {
  1948. self_type &m = (*this) ();
  1949. return m.find1 (1, 0, index2 ());
  1950. }
  1951. BOOST_UBLAS_INLINE
  1952. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1953. typename self_type::
  1954. #endif
  1955. iterator1 end () const {
  1956. self_type &m = (*this) ();
  1957. return m.find1 (1, m.size1 (), index2 ());
  1958. }
  1959. BOOST_UBLAS_INLINE
  1960. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1961. typename self_type::
  1962. #endif
  1963. reverse_iterator1 rbegin () const {
  1964. return reverse_iterator1 (end ());
  1965. }
  1966. BOOST_UBLAS_INLINE
  1967. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1968. typename self_type::
  1969. #endif
  1970. reverse_iterator1 rend () const {
  1971. return reverse_iterator1 (begin ());
  1972. }
  1973. #endif
  1974. // Indices
  1975. BOOST_UBLAS_INLINE
  1976. size_type index1 () const {
  1977. self_type &m = (*this) ();
  1978. return layout_type::index_i (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  1979. }
  1980. BOOST_UBLAS_INLINE
  1981. size_type index2 () const {
  1982. self_type &m = (*this) ();
  1983. return layout_type::index_j (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  1984. }
  1985. // Assignment
  1986. BOOST_UBLAS_INLINE
  1987. iterator2 &operator = (const iterator2 &it) {
  1988. container_reference<self_type>::assign (&it ());
  1989. it_ = it.it_;
  1990. return *this;
  1991. }
  1992. // Comparison
  1993. BOOST_UBLAS_INLINE
  1994. bool operator == (const iterator2 &it) const {
  1995. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1996. return it_ == it.it_;
  1997. }
  1998. BOOST_UBLAS_INLINE
  1999. bool operator < (const iterator2 &it) const {
  2000. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2001. return it_ < it.it_;
  2002. }
  2003. private:
  2004. subiterator_type it_;
  2005. friend class const_iterator2;
  2006. };
  2007. #endif
  2008. BOOST_UBLAS_INLINE
  2009. iterator2 begin2 () {
  2010. return find2 (0, 0, 0);
  2011. }
  2012. BOOST_UBLAS_INLINE
  2013. iterator2 end2 () {
  2014. return find2 (0, 0, N);
  2015. }
  2016. // Reverse iterators
  2017. BOOST_UBLAS_INLINE
  2018. const_reverse_iterator1 rbegin1 () const {
  2019. return const_reverse_iterator1 (end1 ());
  2020. }
  2021. BOOST_UBLAS_INLINE
  2022. const_reverse_iterator1 crbegin1 () const {
  2023. return rbegin1 ();
  2024. }
  2025. BOOST_UBLAS_INLINE
  2026. const_reverse_iterator1 rend1 () const {
  2027. return const_reverse_iterator1 (begin1 ());
  2028. }
  2029. BOOST_UBLAS_INLINE
  2030. const_reverse_iterator1 crend1 () const {
  2031. return rend1 ();
  2032. }
  2033. BOOST_UBLAS_INLINE
  2034. reverse_iterator1 rbegin1 () {
  2035. return reverse_iterator1 (end1 ());
  2036. }
  2037. BOOST_UBLAS_INLINE
  2038. reverse_iterator1 rend1 () {
  2039. return reverse_iterator1 (begin1 ());
  2040. }
  2041. BOOST_UBLAS_INLINE
  2042. const_reverse_iterator2 rbegin2 () const {
  2043. return const_reverse_iterator2 (end2 ());
  2044. }
  2045. BOOST_UBLAS_INLINE
  2046. const_reverse_iterator2 crbegin2 () const {
  2047. return rbegin2 ();
  2048. }
  2049. BOOST_UBLAS_INLINE
  2050. const_reverse_iterator2 rend2 () const {
  2051. return const_reverse_iterator2 (begin2 ());
  2052. }
  2053. BOOST_UBLAS_INLINE
  2054. const_reverse_iterator2 crend2 () const {
  2055. return rend2 ();
  2056. }
  2057. BOOST_UBLAS_INLINE
  2058. reverse_iterator2 rbegin2 () {
  2059. return reverse_iterator2 (end2 ());
  2060. }
  2061. BOOST_UBLAS_INLINE
  2062. reverse_iterator2 rend2 () {
  2063. return reverse_iterator2 (begin2 ());
  2064. }
  2065. // Serialization
  2066. template<class Archive>
  2067. void serialize(Archive & ar, const unsigned int /* file_version */){
  2068. ar & serialization::make_nvp("data",data_);
  2069. }
  2070. private:
  2071. array_type data_;
  2072. };
  2073. #endif // BOOST_UBLAS_CPP_GE_2011
  2074. /** \brief A dense matrix of values of type \c T with a variable size bounded to a maximum of \f$M\f$ by \f$N\f$.
  2075. *
  2076. * For a \f$(m \times n)\f$-dimensional matrix and \f$ 0 \leq i < m, 0 \leq j < n\f$, every element \f$m_{i,j}\f$ is mapped
  2077. * to the \f$(i.n + j)\f$-th element of the container for row major orientation or the \f$(i + j.m)\f$-th element
  2078. * of the container for column major orientation. Finally in a dense matrix all elements are represented in memory
  2079. * in a contiguous chunk of memory.
  2080. *
  2081. * Orientation can be specified. Default is \c row_major
  2082. * The default constructor creates the matrix with size \f$M\f$ by \f$N\f$. Elements are constructed by the storage
  2083. * type \c bounded_array, which need not initialise their value.
  2084. *
  2085. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  2086. * \tparam M maximum and default number of rows (if not specified at construction)
  2087. * \tparam N maximum and default number of columns (if not specified at construction)
  2088. * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major
  2089. */
  2090. template<class T, std::size_t M, std::size_t N, class L>
  2091. class bounded_matrix:
  2092. public matrix<T, L, bounded_array<T, M * N> > {
  2093. typedef matrix<T, L, bounded_array<T, M * N> > matrix_type;
  2094. public:
  2095. typedef typename matrix_type::size_type size_type;
  2096. static const size_type max_size1 = M;
  2097. static const size_type max_size2 = N;
  2098. // Construction and destruction
  2099. BOOST_UBLAS_INLINE
  2100. bounded_matrix ():
  2101. matrix_type (M, N) {}
  2102. BOOST_UBLAS_INLINE
  2103. bounded_matrix (size_type size1, size_type size2):
  2104. matrix_type (size1, size2) {}
  2105. BOOST_UBLAS_INLINE
  2106. bounded_matrix (const bounded_matrix &m):
  2107. matrix_type (m) {}
  2108. template<class A2> // Allow matrix<T, L, bounded_array<M,N> > construction
  2109. BOOST_UBLAS_INLINE
  2110. bounded_matrix (const matrix<T, L, A2> &m):
  2111. matrix_type (m) {}
  2112. template<class AE>
  2113. BOOST_UBLAS_INLINE
  2114. bounded_matrix (const matrix_expression<AE> &ae):
  2115. matrix_type (ae) {}
  2116. BOOST_UBLAS_INLINE
  2117. ~bounded_matrix () {}
  2118. // Assignment
  2119. #ifdef BOOST_UBLAS_MOVE_SEMANTICS
  2120. /*! @note "pass by value" the key idea to enable move semantics */
  2121. BOOST_UBLAS_INLINE
  2122. bounded_matrix &operator = (bounded_matrix m) {
  2123. matrix_type::operator = (m);
  2124. return *this;
  2125. }
  2126. #else
  2127. BOOST_UBLAS_INLINE
  2128. bounded_matrix &operator = (const bounded_matrix &m) {
  2129. matrix_type::operator = (m);
  2130. return *this;
  2131. }
  2132. #endif
  2133. template<class L2, class A2> // Generic matrix assignment
  2134. BOOST_UBLAS_INLINE
  2135. bounded_matrix &operator = (const matrix<T, L2, A2> &m) {
  2136. matrix_type::operator = (m);
  2137. return *this;
  2138. }
  2139. template<class C> // Container assignment without temporary
  2140. BOOST_UBLAS_INLINE
  2141. bounded_matrix &operator = (const matrix_container<C> &m) {
  2142. matrix_type::operator = (m);
  2143. return *this;
  2144. }
  2145. template<class AE>
  2146. BOOST_UBLAS_INLINE
  2147. bounded_matrix &operator = (const matrix_expression<AE> &ae) {
  2148. matrix_type::operator = (ae);
  2149. return *this;
  2150. }
  2151. };
  2152. /** \brief A dense matrix of values of type \c T stored as a vector of vectors.
  2153. *
  2154. * Rows or columns are not stored into contiguous chunks of memory but data inside rows (or columns) are.
  2155. * Orientation and storage can also be specified, otherwise a row major and unbounded arrays are used.
  2156. * The data is stored as a vector of vectors, meaning that rows or columns might not be stored into contiguous chunks
  2157. * of memory. Orientation and storage can also be specified, otherwise a row major and unbounded arrays are used.
  2158. * The storage type defaults to \c unbounded_array<unbounded_array<T>> and orientation is \c row_major. It is \b not
  2159. * required by the storage to initialize elements of the matrix. For a \f$(m \times n)\f$-dimensional matrix and
  2160. * \f$ 0 \leq i < m, 0 \leq j < n\f$, every element \f$m_{i,j}\f$ is mapped to the \f$(i.n + j)\f$-th element of the
  2161. * container for row major orientation or the \f$(i + j.m)\f$-th element of the container for column major orientation.
  2162. *
  2163. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  2164. * \tparam L the storage organization. It can be either \c row_major or \c column_major. By default it is \c row_major
  2165. * \tparam A the type of Storage array. By default, it is an \unbounded_array<unbounder_array<T>>
  2166. */
  2167. template<class T, class L, class A>
  2168. class vector_of_vector:
  2169. public matrix_container<vector_of_vector<T, L, A> > {
  2170. typedef T *pointer;
  2171. typedef L layout_type;
  2172. typedef vector_of_vector<T, L, A> self_type;
  2173. public:
  2174. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  2175. using matrix_container<self_type>::operator ();
  2176. #endif
  2177. typedef typename A::size_type size_type;
  2178. typedef typename A::difference_type difference_type;
  2179. typedef T value_type;
  2180. typedef const T &const_reference;
  2181. typedef T &reference;
  2182. typedef A array_type;
  2183. typedef const matrix_reference<const self_type> const_closure_type;
  2184. typedef matrix_reference<self_type> closure_type;
  2185. typedef vector<T, typename A::value_type> vector_temporary_type;
  2186. typedef self_type matrix_temporary_type;
  2187. typedef dense_tag storage_category;
  2188. // This could be better for performance,
  2189. // typedef typename unknown_orientation_tag orientation_category;
  2190. // but others depend on the orientation information...
  2191. typedef typename L::orientation_category orientation_category;
  2192. // Construction and destruction
  2193. BOOST_UBLAS_INLINE
  2194. vector_of_vector ():
  2195. matrix_container<self_type> (),
  2196. size1_ (0), size2_ (0), data_ (1) {}
  2197. BOOST_UBLAS_INLINE
  2198. vector_of_vector (size_type size1, size_type size2):
  2199. matrix_container<self_type> (),
  2200. size1_ (size1), size2_ (size2), data_ (1) {
  2201. resize (size1, size2, true);
  2202. }
  2203. BOOST_UBLAS_INLINE
  2204. vector_of_vector (const vector_of_vector &m):
  2205. matrix_container<self_type> (),
  2206. size1_ (m.size1_), size2_ (m.size2_), data_ (m.data_) {}
  2207. template<class AE>
  2208. BOOST_UBLAS_INLINE
  2209. vector_of_vector (const matrix_expression<AE> &ae):
  2210. matrix_container<self_type> (),
  2211. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), data_ (layout_type::size_M (size1_, size2_) + 1) {
  2212. for (size_type k = 0; k < layout_type::size_M (size1_, size2_); ++ k)
  2213. data ()[k].resize (layout_type::size_m (size1_, size2_));
  2214. matrix_assign<scalar_assign> (*this, ae);
  2215. }
  2216. // Accessors
  2217. BOOST_UBLAS_INLINE
  2218. size_type size1 () const {
  2219. return size1_;
  2220. }
  2221. BOOST_UBLAS_INLINE
  2222. size_type size2 () const {
  2223. return size2_;
  2224. }
  2225. // Storage accessors
  2226. BOOST_UBLAS_INLINE
  2227. const array_type &data () const {
  2228. return data_;
  2229. }
  2230. BOOST_UBLAS_INLINE
  2231. array_type &data () {
  2232. return data_;
  2233. }
  2234. // Resizing
  2235. BOOST_UBLAS_INLINE
  2236. void resize (size_type size1, size_type size2, bool preserve = true) {
  2237. size1_ = size1;
  2238. size2_ = size2;
  2239. if (preserve)
  2240. data ().resize (layout_type::size_M (size1, size2) + 1, typename array_type::value_type ());
  2241. else
  2242. data ().resize (layout_type::size_M (size1, size2) + 1);
  2243. for (size_type k = 0; k < layout_type::size_M (size1, size2); ++ k) {
  2244. if (preserve)
  2245. data () [k].resize (layout_type::size_m (size1, size2), value_type ());
  2246. else
  2247. data () [k].resize (layout_type::size_m (size1, size2));
  2248. }
  2249. }
  2250. // Element access
  2251. BOOST_UBLAS_INLINE
  2252. const_reference operator () (size_type i, size_type j) const {
  2253. return data () [layout_type::index_M (i, j)] [layout_type::index_m (i, j)];
  2254. }
  2255. BOOST_UBLAS_INLINE
  2256. reference at_element (size_type i, size_type j) {
  2257. return data () [layout_type::index_M (i, j)] [layout_type::index_m (i, j)];
  2258. }
  2259. BOOST_UBLAS_INLINE
  2260. reference operator () (size_type i, size_type j) {
  2261. return at_element (i, j);
  2262. }
  2263. // Element assignment
  2264. BOOST_UBLAS_INLINE
  2265. reference insert_element (size_type i, size_type j, const_reference t) {
  2266. return (at_element (i, j) = t);
  2267. }
  2268. BOOST_UBLAS_INLINE
  2269. void erase_element (size_type i, size_type j) {
  2270. at_element (i, j) = value_type/*zero*/();
  2271. }
  2272. // Zeroing
  2273. BOOST_UBLAS_INLINE
  2274. void clear () {
  2275. for (size_type k = 0; k < layout_type::size_M (size1_, size2_); ++ k)
  2276. std::fill (data () [k].begin (), data () [k].end (), value_type/*zero*/());
  2277. }
  2278. // Assignment
  2279. BOOST_UBLAS_INLINE
  2280. vector_of_vector &operator = (const vector_of_vector &m) {
  2281. size1_ = m.size1_;
  2282. size2_ = m.size2_;
  2283. data () = m.data ();
  2284. return *this;
  2285. }
  2286. BOOST_UBLAS_INLINE
  2287. vector_of_vector &assign_temporary (vector_of_vector &m) {
  2288. swap (m);
  2289. return *this;
  2290. }
  2291. template<class AE>
  2292. BOOST_UBLAS_INLINE
  2293. vector_of_vector &operator = (const matrix_expression<AE> &ae) {
  2294. self_type temporary (ae);
  2295. return assign_temporary (temporary);
  2296. }
  2297. template<class C> // Container assignment without temporary
  2298. BOOST_UBLAS_INLINE
  2299. vector_of_vector &operator = (const matrix_container<C> &m) {
  2300. resize (m ().size1 (), m ().size2 (), false);
  2301. assign (m);
  2302. return *this;
  2303. }
  2304. template<class AE>
  2305. BOOST_UBLAS_INLINE
  2306. vector_of_vector &assign (const matrix_expression<AE> &ae) {
  2307. matrix_assign<scalar_assign> (*this, ae);
  2308. return *this;
  2309. }
  2310. template<class AE>
  2311. BOOST_UBLAS_INLINE
  2312. vector_of_vector& operator += (const matrix_expression<AE> &ae) {
  2313. self_type temporary (*this + ae);
  2314. return assign_temporary (temporary);
  2315. }
  2316. template<class C> // Container assignment without temporary
  2317. BOOST_UBLAS_INLINE
  2318. vector_of_vector &operator += (const matrix_container<C> &m) {
  2319. plus_assign (m);
  2320. return *this;
  2321. }
  2322. template<class AE>
  2323. BOOST_UBLAS_INLINE
  2324. vector_of_vector &plus_assign (const matrix_expression<AE> &ae) {
  2325. matrix_assign<scalar_plus_assign> (*this, ae);
  2326. return *this;
  2327. }
  2328. template<class AE>
  2329. BOOST_UBLAS_INLINE
  2330. vector_of_vector& operator -= (const matrix_expression<AE> &ae) {
  2331. self_type temporary (*this - ae);
  2332. return assign_temporary (temporary);
  2333. }
  2334. template<class C> // Container assignment without temporary
  2335. BOOST_UBLAS_INLINE
  2336. vector_of_vector &operator -= (const matrix_container<C> &m) {
  2337. minus_assign (m);
  2338. return *this;
  2339. }
  2340. template<class AE>
  2341. BOOST_UBLAS_INLINE
  2342. vector_of_vector &minus_assign (const matrix_expression<AE> &ae) {
  2343. matrix_assign<scalar_minus_assign> (*this, ae);
  2344. return *this;
  2345. }
  2346. template<class AT>
  2347. BOOST_UBLAS_INLINE
  2348. vector_of_vector& operator *= (const AT &at) {
  2349. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  2350. return *this;
  2351. }
  2352. template<class AT>
  2353. BOOST_UBLAS_INLINE
  2354. vector_of_vector& operator /= (const AT &at) {
  2355. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  2356. return *this;
  2357. }
  2358. // Swapping
  2359. BOOST_UBLAS_INLINE
  2360. void swap (vector_of_vector &m) {
  2361. if (this != &m) {
  2362. std::swap (size1_, m.size1_);
  2363. std::swap (size2_, m.size2_);
  2364. data ().swap (m.data ());
  2365. }
  2366. }
  2367. BOOST_UBLAS_INLINE
  2368. friend void swap (vector_of_vector &m1, vector_of_vector &m2) {
  2369. m1.swap (m2);
  2370. }
  2371. // Iterator types
  2372. private:
  2373. // Use the vector iterator
  2374. typedef typename A::value_type::const_iterator const_subiterator_type;
  2375. typedef typename A::value_type::iterator subiterator_type;
  2376. public:
  2377. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2378. typedef indexed_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  2379. typedef indexed_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  2380. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  2381. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  2382. #else
  2383. class const_iterator1;
  2384. class iterator1;
  2385. class const_iterator2;
  2386. class iterator2;
  2387. #endif
  2388. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  2389. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  2390. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  2391. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  2392. // Element lookup
  2393. BOOST_UBLAS_INLINE
  2394. const_iterator1 find1 (int /*rank*/, size_type i, size_type j) const {
  2395. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2396. return const_iterator1 (*this, i, j);
  2397. #else
  2398. return const_iterator1 (*this, i, j, data () [layout_type::index_M (i, j)].begin () + layout_type::index_m (i, j));
  2399. #endif
  2400. }
  2401. BOOST_UBLAS_INLINE
  2402. iterator1 find1 (int /*rank*/, size_type i, size_type j) {
  2403. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2404. return iterator1 (*this, i, j);
  2405. #else
  2406. return iterator1 (*this, i, j, data () [layout_type::index_M (i, j)].begin () + layout_type::index_m (i, j));
  2407. #endif
  2408. }
  2409. BOOST_UBLAS_INLINE
  2410. const_iterator2 find2 (int /*rank*/, size_type i, size_type j) const {
  2411. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2412. return const_iterator2 (*this, i, j);
  2413. #else
  2414. return const_iterator2 (*this, i, j, data () [layout_type::index_M (i, j)].begin () + layout_type::index_m (i, j));
  2415. #endif
  2416. }
  2417. BOOST_UBLAS_INLINE
  2418. iterator2 find2 (int /*rank*/, size_type i, size_type j) {
  2419. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2420. return iterator2 (*this, i, j);
  2421. #else
  2422. return iterator2 (*this, i, j, data () [layout_type::index_M (i, j)].begin () + layout_type::index_m (i, j));
  2423. #endif
  2424. }
  2425. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2426. class const_iterator1:
  2427. public container_const_reference<vector_of_vector>,
  2428. public random_access_iterator_base<dense_random_access_iterator_tag,
  2429. const_iterator1, value_type> {
  2430. public:
  2431. typedef typename vector_of_vector::value_type value_type;
  2432. typedef typename vector_of_vector::difference_type difference_type;
  2433. typedef typename vector_of_vector::const_reference reference;
  2434. typedef const typename vector_of_vector::pointer pointer;
  2435. typedef const_iterator2 dual_iterator_type;
  2436. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  2437. // Construction and destruction
  2438. BOOST_UBLAS_INLINE
  2439. const_iterator1 ():
  2440. container_const_reference<self_type> (), i_ (), j_ (), it_ () {}
  2441. BOOST_UBLAS_INLINE
  2442. const_iterator1 (const self_type &m, size_type i, size_type j, const const_subiterator_type &it):
  2443. container_const_reference<self_type> (m), i_ (i), j_ (j), it_ (it) {}
  2444. BOOST_UBLAS_INLINE
  2445. const_iterator1 (const iterator1 &it):
  2446. container_const_reference<self_type> (it ()), i_ (it.i_), j_ (it.j_), it_ (it.it_) {}
  2447. // Arithmetic
  2448. BOOST_UBLAS_INLINE
  2449. const_iterator1 &operator ++ () {
  2450. ++ i_;
  2451. const self_type &m = (*this) ();
  2452. if (layout_type::fast_i ())
  2453. ++ it_;
  2454. else
  2455. it_ = m.find1 (1, i_, j_).it_;
  2456. return *this;
  2457. }
  2458. BOOST_UBLAS_INLINE
  2459. const_iterator1 &operator -- () {
  2460. -- i_;
  2461. const self_type &m = (*this) ();
  2462. if (layout_type::fast_i ())
  2463. -- it_;
  2464. else
  2465. it_ = m.find1 (1, i_, j_).it_;
  2466. return *this;
  2467. }
  2468. BOOST_UBLAS_INLINE
  2469. const_iterator1 &operator += (difference_type n) {
  2470. i_ += n;
  2471. const self_type &m = (*this) ();
  2472. it_ = m.find1 (1, i_, j_).it_;
  2473. return *this;
  2474. }
  2475. BOOST_UBLAS_INLINE
  2476. const_iterator1 &operator -= (difference_type n) {
  2477. i_ -= n;
  2478. const self_type &m = (*this) ();
  2479. it_ = m.find1 (1, i_, j_).it_;
  2480. return *this;
  2481. }
  2482. BOOST_UBLAS_INLINE
  2483. difference_type operator - (const const_iterator1 &it) const {
  2484. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2485. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  2486. return index1 () - it.index1 ();
  2487. }
  2488. // Dereference
  2489. BOOST_UBLAS_INLINE
  2490. const_reference operator * () const {
  2491. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  2492. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  2493. return *it_;
  2494. }
  2495. BOOST_UBLAS_INLINE
  2496. const_reference operator [] (difference_type n) const {
  2497. return *(*this + n);
  2498. }
  2499. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2500. BOOST_UBLAS_INLINE
  2501. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2502. typename self_type::
  2503. #endif
  2504. const_iterator2 begin () const {
  2505. const self_type &m = (*this) ();
  2506. return m.find2 (1, index1 (), 0);
  2507. }
  2508. BOOST_UBLAS_INLINE
  2509. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2510. typename self_type::
  2511. #endif
  2512. const_iterator2 cbegin () const {
  2513. return begin ();
  2514. }
  2515. BOOST_UBLAS_INLINE
  2516. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2517. typename self_type::
  2518. #endif
  2519. const_iterator2 end () const {
  2520. const self_type &m = (*this) ();
  2521. return m.find2 (1, index1 (), m.size2 ());
  2522. }
  2523. BOOST_UBLAS_INLINE
  2524. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2525. typename self_type::
  2526. #endif
  2527. const_iterator2 cend () const {
  2528. return end ();
  2529. }
  2530. BOOST_UBLAS_INLINE
  2531. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2532. typename self_type::
  2533. #endif
  2534. const_reverse_iterator2 rbegin () const {
  2535. return const_reverse_iterator2 (end ());
  2536. }
  2537. BOOST_UBLAS_INLINE
  2538. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2539. typename self_type::
  2540. #endif
  2541. const_reverse_iterator2 crbegin () const {
  2542. return rbegin ();
  2543. }
  2544. BOOST_UBLAS_INLINE
  2545. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2546. typename self_type::
  2547. #endif
  2548. const_reverse_iterator2 rend () const {
  2549. return const_reverse_iterator2 (begin ());
  2550. }
  2551. BOOST_UBLAS_INLINE
  2552. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2553. typename self_type::
  2554. #endif
  2555. const_reverse_iterator2 crend () const {
  2556. return rend ();
  2557. }
  2558. #endif
  2559. // Indices
  2560. BOOST_UBLAS_INLINE
  2561. size_type index1 () const {
  2562. return i_;
  2563. }
  2564. BOOST_UBLAS_INLINE
  2565. size_type index2 () const {
  2566. return j_;
  2567. }
  2568. // Assignment
  2569. BOOST_UBLAS_INLINE
  2570. const_iterator1 &operator = (const const_iterator1 &it) {
  2571. container_const_reference<self_type>::assign (&it ());
  2572. it_ = it.it_;
  2573. return *this;
  2574. }
  2575. // Comparison
  2576. BOOST_UBLAS_INLINE
  2577. bool operator == (const const_iterator1 &it) const {
  2578. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2579. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  2580. return it_ == it.it_;
  2581. }
  2582. BOOST_UBLAS_INLINE
  2583. bool operator < (const const_iterator1 &it) const {
  2584. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2585. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  2586. return it_ < it.it_;
  2587. }
  2588. private:
  2589. size_type i_;
  2590. size_type j_;
  2591. const_subiterator_type it_;
  2592. friend class iterator1;
  2593. };
  2594. #endif
  2595. BOOST_UBLAS_INLINE
  2596. const_iterator1 begin1 () const {
  2597. return find1 (0, 0, 0);
  2598. }
  2599. BOOST_UBLAS_INLINE
  2600. const_iterator1 cbegin1 () const {
  2601. return begin1 ();
  2602. }
  2603. BOOST_UBLAS_INLINE
  2604. const_iterator1 end1 () const {
  2605. return find1 (0, size1_, 0);
  2606. }
  2607. BOOST_UBLAS_INLINE
  2608. const_iterator1 cend1 () const {
  2609. return end1 ();
  2610. }
  2611. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2612. class iterator1:
  2613. public container_reference<vector_of_vector>,
  2614. public random_access_iterator_base<dense_random_access_iterator_tag,
  2615. iterator1, value_type> {
  2616. public:
  2617. typedef typename vector_of_vector::value_type value_type;
  2618. typedef typename vector_of_vector::difference_type difference_type;
  2619. typedef typename vector_of_vector::reference reference;
  2620. typedef typename vector_of_vector::pointer pointer;
  2621. typedef iterator2 dual_iterator_type;
  2622. typedef reverse_iterator2 dual_reverse_iterator_type;
  2623. // Construction and destruction
  2624. BOOST_UBLAS_INLINE
  2625. iterator1 ():
  2626. container_reference<self_type> (), i_ (), j_ (), it_ () {}
  2627. BOOST_UBLAS_INLINE
  2628. iterator1 (self_type &m, size_type i, size_type j, const subiterator_type &it):
  2629. container_reference<self_type> (m), i_ (i), j_ (j), it_ (it) {}
  2630. // Arithmetic
  2631. BOOST_UBLAS_INLINE
  2632. iterator1 &operator ++ () {
  2633. ++ i_;
  2634. self_type &m = (*this) ();
  2635. if (layout_type::fast_i ())
  2636. ++ it_;
  2637. else
  2638. it_ = m.find1 (1, i_, j_).it_;
  2639. return *this;
  2640. }
  2641. BOOST_UBLAS_INLINE
  2642. iterator1 &operator -- () {
  2643. -- i_;
  2644. self_type &m = (*this) ();
  2645. if (layout_type::fast_i ())
  2646. -- it_;
  2647. else
  2648. it_ = m.find1 (1, i_, j_).it_;
  2649. return *this;
  2650. }
  2651. BOOST_UBLAS_INLINE
  2652. iterator1 &operator += (difference_type n) {
  2653. i_ += n;
  2654. self_type &m = (*this) ();
  2655. it_ = m.find1 (1, i_, j_).it_;
  2656. return *this;
  2657. }
  2658. BOOST_UBLAS_INLINE
  2659. iterator1 &operator -= (difference_type n) {
  2660. i_ -= n;
  2661. self_type &m = (*this) ();
  2662. it_ = m.find1 (1, i_, j_).it_;
  2663. return *this;
  2664. }
  2665. BOOST_UBLAS_INLINE
  2666. difference_type operator - (const iterator1 &it) const {
  2667. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2668. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  2669. return index1 () - it.index1 ();
  2670. }
  2671. // Dereference
  2672. BOOST_UBLAS_INLINE
  2673. reference operator * () const {
  2674. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  2675. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  2676. return *it_;
  2677. }
  2678. BOOST_UBLAS_INLINE
  2679. reference operator [] (difference_type n) const {
  2680. return *(*this + n);
  2681. }
  2682. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2683. BOOST_UBLAS_INLINE
  2684. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2685. typename self_type::
  2686. #endif
  2687. iterator2 begin () const {
  2688. self_type &m = (*this) ();
  2689. return m.find2 (1, index1 (), 0);
  2690. }
  2691. BOOST_UBLAS_INLINE
  2692. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2693. typename self_type::
  2694. #endif
  2695. iterator2 end () const {
  2696. self_type &m = (*this) ();
  2697. return m.find2 (1, index1 (), m.size2 ());
  2698. }
  2699. BOOST_UBLAS_INLINE
  2700. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2701. typename self_type::
  2702. #endif
  2703. reverse_iterator2 rbegin () const {
  2704. return reverse_iterator2 (end ());
  2705. }
  2706. BOOST_UBLAS_INLINE
  2707. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2708. typename self_type::
  2709. #endif
  2710. reverse_iterator2 rend () const {
  2711. return reverse_iterator2 (begin ());
  2712. }
  2713. #endif
  2714. // Indices
  2715. BOOST_UBLAS_INLINE
  2716. size_type index1 () const {
  2717. return i_;
  2718. }
  2719. BOOST_UBLAS_INLINE
  2720. size_type index2 () const {
  2721. return j_;
  2722. }
  2723. // Assignment
  2724. BOOST_UBLAS_INLINE
  2725. iterator1 &operator = (const iterator1 &it) {
  2726. container_reference<self_type>::assign (&it ());
  2727. it_ = it.it_;
  2728. return *this;
  2729. }
  2730. // Comparison
  2731. BOOST_UBLAS_INLINE
  2732. bool operator == (const iterator1 &it) const {
  2733. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2734. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  2735. return it_ == it.it_;
  2736. }
  2737. BOOST_UBLAS_INLINE
  2738. bool operator < (const iterator1 &it) const {
  2739. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2740. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  2741. return it_ < it.it_;
  2742. }
  2743. private:
  2744. size_type i_;
  2745. size_type j_;
  2746. subiterator_type it_;
  2747. friend class const_iterator1;
  2748. };
  2749. #endif
  2750. BOOST_UBLAS_INLINE
  2751. iterator1 begin1 () {
  2752. return find1 (0, 0, 0);
  2753. }
  2754. BOOST_UBLAS_INLINE
  2755. iterator1 end1 () {
  2756. return find1 (0, size1_, 0);
  2757. }
  2758. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2759. class const_iterator2:
  2760. public container_const_reference<vector_of_vector>,
  2761. public random_access_iterator_base<dense_random_access_iterator_tag,
  2762. const_iterator2, value_type> {
  2763. public:
  2764. typedef typename vector_of_vector::value_type value_type;
  2765. typedef typename vector_of_vector::difference_type difference_type;
  2766. typedef typename vector_of_vector::const_reference reference;
  2767. typedef const typename vector_of_vector::pointer pointer;
  2768. typedef const_iterator1 dual_iterator_type;
  2769. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  2770. // Construction and destruction
  2771. BOOST_UBLAS_INLINE
  2772. const_iterator2 ():
  2773. container_const_reference<self_type> (), i_ (), j_ (), it_ () {}
  2774. BOOST_UBLAS_INLINE
  2775. const_iterator2 (const self_type &m, size_type i, size_type j, const const_subiterator_type &it):
  2776. container_const_reference<self_type> (m), i_ (i), j_ (j), it_ (it) {}
  2777. BOOST_UBLAS_INLINE
  2778. const_iterator2 (const iterator2 &it):
  2779. container_const_reference<self_type> (it ()), i_ (it.i_), j_ (it.j_), it_ (it.it_) {}
  2780. // Arithmetic
  2781. BOOST_UBLAS_INLINE
  2782. const_iterator2 &operator ++ () {
  2783. ++ j_;
  2784. const self_type &m = (*this) ();
  2785. if (layout_type::fast_j ())
  2786. ++ it_;
  2787. else
  2788. it_ = m.find2 (1, i_, j_).it_;
  2789. return *this;
  2790. }
  2791. BOOST_UBLAS_INLINE
  2792. const_iterator2 &operator -- () {
  2793. -- j_;
  2794. const self_type &m = (*this) ();
  2795. if (layout_type::fast_j ())
  2796. -- it_;
  2797. else
  2798. it_ = m.find2 (1, i_, j_).it_;
  2799. return *this;
  2800. }
  2801. BOOST_UBLAS_INLINE
  2802. const_iterator2 &operator += (difference_type n) {
  2803. j_ += n;
  2804. const self_type &m = (*this) ();
  2805. it_ = m.find2 (1, i_, j_).it_;
  2806. return *this;
  2807. }
  2808. BOOST_UBLAS_INLINE
  2809. const_iterator2 &operator -= (difference_type n) {
  2810. j_ -= n;
  2811. const self_type &m = (*this) ();
  2812. it_ = m.find2 (1, i_, j_).it_;
  2813. return *this;
  2814. }
  2815. BOOST_UBLAS_INLINE
  2816. difference_type operator - (const const_iterator2 &it) const {
  2817. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2818. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  2819. return index2 () - it.index2 ();
  2820. }
  2821. // Dereference
  2822. BOOST_UBLAS_INLINE
  2823. const_reference operator * () const {
  2824. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  2825. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  2826. return *it_;
  2827. }
  2828. BOOST_UBLAS_INLINE
  2829. const_reference operator [] (difference_type n) const {
  2830. return *(*this + n);
  2831. }
  2832. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2833. BOOST_UBLAS_INLINE
  2834. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2835. typename self_type::
  2836. #endif
  2837. const_iterator1 begin () const {
  2838. const self_type &m = (*this) ();
  2839. return m.find1 (1, 0, index2 ());
  2840. }
  2841. BOOST_UBLAS_INLINE
  2842. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2843. typename self_type::
  2844. #endif
  2845. const_iterator1 cbegin () const {
  2846. return begin ();
  2847. }
  2848. BOOST_UBLAS_INLINE
  2849. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2850. typename self_type::
  2851. #endif
  2852. const_iterator1 end () const {
  2853. const self_type &m = (*this) ();
  2854. return m.find1 (1, m.size1 (), index2 ());
  2855. }
  2856. BOOST_UBLAS_INLINE
  2857. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2858. typename self_type::
  2859. #endif
  2860. const_iterator1 cend () const {
  2861. return end ();
  2862. }
  2863. BOOST_UBLAS_INLINE
  2864. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2865. typename self_type::
  2866. #endif
  2867. const_reverse_iterator1 rbegin () const {
  2868. return const_reverse_iterator1 (end ());
  2869. }
  2870. BOOST_UBLAS_INLINE
  2871. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2872. typename self_type::
  2873. #endif
  2874. const_reverse_iterator1 crbegin () const {
  2875. return rbegin ();
  2876. }
  2877. BOOST_UBLAS_INLINE
  2878. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2879. typename self_type::
  2880. #endif
  2881. const_reverse_iterator1 rend () const {
  2882. return const_reverse_iterator1 (begin ());
  2883. }
  2884. BOOST_UBLAS_INLINE
  2885. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2886. typename self_type::
  2887. #endif
  2888. const_reverse_iterator2 crend () const {
  2889. return rend ();
  2890. }
  2891. #endif
  2892. // Indices
  2893. BOOST_UBLAS_INLINE
  2894. size_type index1 () const {
  2895. return i_;
  2896. }
  2897. BOOST_UBLAS_INLINE
  2898. size_type index2 () const {
  2899. return j_;
  2900. }
  2901. // Assignment
  2902. BOOST_UBLAS_INLINE
  2903. const_iterator2 &operator = (const const_iterator2 &it) {
  2904. container_const_reference<self_type>::assign (&it ());
  2905. it_ = it.it_;
  2906. return *this;
  2907. }
  2908. // Comparison
  2909. BOOST_UBLAS_INLINE
  2910. bool operator == (const const_iterator2 &it) const {
  2911. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2912. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  2913. return it_ == it.it_;
  2914. }
  2915. BOOST_UBLAS_INLINE
  2916. bool operator < (const const_iterator2 &it) const {
  2917. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2918. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  2919. return it_ < it.it_;
  2920. }
  2921. private:
  2922. size_type i_;
  2923. size_type j_;
  2924. const_subiterator_type it_;
  2925. friend class iterator2;
  2926. };
  2927. #endif
  2928. BOOST_UBLAS_INLINE
  2929. const_iterator2 begin2 () const {
  2930. return find2 (0, 0, 0);
  2931. }
  2932. BOOST_UBLAS_INLINE
  2933. const_iterator2 cbegin2 () const {
  2934. return begin2 ();
  2935. }
  2936. BOOST_UBLAS_INLINE
  2937. const_iterator2 end2 () const {
  2938. return find2 (0, 0, size2_);
  2939. }
  2940. BOOST_UBLAS_INLINE
  2941. const_iterator2 cend2 () const {
  2942. return end2 ();
  2943. }
  2944. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2945. class iterator2:
  2946. public container_reference<vector_of_vector>,
  2947. public random_access_iterator_base<dense_random_access_iterator_tag,
  2948. iterator2, value_type> {
  2949. public:
  2950. typedef typename vector_of_vector::value_type value_type;
  2951. typedef typename vector_of_vector::difference_type difference_type;
  2952. typedef typename vector_of_vector::reference reference;
  2953. typedef typename vector_of_vector::pointer pointer;
  2954. typedef iterator1 dual_iterator_type;
  2955. typedef reverse_iterator1 dual_reverse_iterator_type;
  2956. // Construction and destruction
  2957. BOOST_UBLAS_INLINE
  2958. iterator2 ():
  2959. container_reference<self_type> (), i_ (), j_ (), it_ () {}
  2960. BOOST_UBLAS_INLINE
  2961. iterator2 (self_type &m, size_type i, size_type j, const subiterator_type &it):
  2962. container_reference<self_type> (m), i_ (i), j_ (j), it_ (it) {}
  2963. // Arithmetic
  2964. BOOST_UBLAS_INLINE
  2965. iterator2 &operator ++ () {
  2966. ++ j_;
  2967. self_type &m = (*this) ();
  2968. if (layout_type::fast_j ())
  2969. ++ it_;
  2970. else
  2971. it_ = m.find2 (1, i_, j_).it_;
  2972. return *this;
  2973. }
  2974. BOOST_UBLAS_INLINE
  2975. iterator2 &operator -- () {
  2976. -- j_;
  2977. self_type &m = (*this) ();
  2978. if (layout_type::fast_j ())
  2979. -- it_;
  2980. else
  2981. it_ = m.find2 (1, i_, j_).it_;
  2982. return *this;
  2983. }
  2984. BOOST_UBLAS_INLINE
  2985. iterator2 &operator += (difference_type n) {
  2986. j_ += n;
  2987. self_type &m = (*this) ();
  2988. it_ = m.find2 (1, i_, j_).it_;
  2989. return *this;
  2990. }
  2991. BOOST_UBLAS_INLINE
  2992. iterator2 &operator -= (difference_type n) {
  2993. j_ -= n;
  2994. self_type &m = (*this) ();
  2995. it_ = m.find2 (1, i_, j_).it_;
  2996. return *this;
  2997. }
  2998. BOOST_UBLAS_INLINE
  2999. difference_type operator - (const iterator2 &it) const {
  3000. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3001. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  3002. return index2 () - it.index2 ();
  3003. }
  3004. // Dereference
  3005. BOOST_UBLAS_INLINE
  3006. reference operator * () const {
  3007. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3008. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3009. return *it_;
  3010. }
  3011. BOOST_UBLAS_INLINE
  3012. reference operator [] (difference_type n) const {
  3013. return *(*this + n);
  3014. }
  3015. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3016. BOOST_UBLAS_INLINE
  3017. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3018. typename self_type::
  3019. #endif
  3020. iterator1 begin () const {
  3021. self_type &m = (*this) ();
  3022. return m.find1 (1, 0, index2 ());
  3023. }
  3024. BOOST_UBLAS_INLINE
  3025. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3026. typename self_type::
  3027. #endif
  3028. iterator1 end () const {
  3029. self_type &m = (*this) ();
  3030. return m.find1 (1, m.size1 (), index2 ());
  3031. }
  3032. BOOST_UBLAS_INLINE
  3033. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3034. typename self_type::
  3035. #endif
  3036. reverse_iterator1 rbegin () const {
  3037. return reverse_iterator1 (end ());
  3038. }
  3039. BOOST_UBLAS_INLINE
  3040. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3041. typename self_type::
  3042. #endif
  3043. reverse_iterator1 rend () const {
  3044. return reverse_iterator1 (begin ());
  3045. }
  3046. #endif
  3047. // Indices
  3048. BOOST_UBLAS_INLINE
  3049. size_type index1 () const {
  3050. return i_;
  3051. }
  3052. BOOST_UBLAS_INLINE
  3053. size_type index2 () const {
  3054. return j_;
  3055. }
  3056. // Assignment
  3057. BOOST_UBLAS_INLINE
  3058. iterator2 &operator = (const iterator2 &it) {
  3059. container_reference<self_type>::assign (&it ());
  3060. it_ = it.it_;
  3061. return *this;
  3062. }
  3063. // Comparison
  3064. BOOST_UBLAS_INLINE
  3065. bool operator == (const iterator2 &it) const {
  3066. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3067. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  3068. return it_ == it.it_;
  3069. }
  3070. BOOST_UBLAS_INLINE
  3071. bool operator < (const iterator2 &it) const {
  3072. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3073. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  3074. return it_ < it.it_;
  3075. }
  3076. private:
  3077. size_type i_;
  3078. size_type j_;
  3079. subiterator_type it_;
  3080. friend class const_iterator2;
  3081. };
  3082. #endif
  3083. BOOST_UBLAS_INLINE
  3084. iterator2 begin2 () {
  3085. return find2 (0, 0, 0);
  3086. }
  3087. BOOST_UBLAS_INLINE
  3088. iterator2 end2 () {
  3089. return find2 (0, 0, size2_);
  3090. }
  3091. // Reverse iterators
  3092. BOOST_UBLAS_INLINE
  3093. const_reverse_iterator1 rbegin1 () const {
  3094. return const_reverse_iterator1 (end1 ());
  3095. }
  3096. BOOST_UBLAS_INLINE
  3097. const_reverse_iterator1 crbegin1 () const {
  3098. return rbegin1 ();
  3099. }
  3100. BOOST_UBLAS_INLINE
  3101. const_reverse_iterator1 rend1 () const {
  3102. return const_reverse_iterator1 (begin1 ());
  3103. }
  3104. BOOST_UBLAS_INLINE
  3105. const_reverse_iterator1 crend1 () const {
  3106. return rend1 ();
  3107. }
  3108. BOOST_UBLAS_INLINE
  3109. reverse_iterator1 rbegin1 () {
  3110. return reverse_iterator1 (end1 ());
  3111. }
  3112. BOOST_UBLAS_INLINE
  3113. reverse_iterator1 rend1 () {
  3114. return reverse_iterator1 (begin1 ());
  3115. }
  3116. BOOST_UBLAS_INLINE
  3117. const_reverse_iterator2 rbegin2 () const {
  3118. return const_reverse_iterator2 (end2 ());
  3119. }
  3120. BOOST_UBLAS_INLINE
  3121. const_reverse_iterator2 crbegin2 () const {
  3122. return rbegin2 ();
  3123. }
  3124. BOOST_UBLAS_INLINE
  3125. const_reverse_iterator2 rend2 () const {
  3126. return const_reverse_iterator2 (begin2 ());
  3127. }
  3128. BOOST_UBLAS_INLINE
  3129. const_reverse_iterator2 crend2 () const {
  3130. return rend2 ();
  3131. }
  3132. BOOST_UBLAS_INLINE
  3133. reverse_iterator2 rbegin2 () {
  3134. return reverse_iterator2 (end2 ());
  3135. }
  3136. BOOST_UBLAS_INLINE
  3137. reverse_iterator2 rend2 () {
  3138. return reverse_iterator2 (begin2 ());
  3139. }
  3140. // Serialization
  3141. template<class Archive>
  3142. void serialize(Archive & ar, const unsigned int /* file_version */){
  3143. // we need to copy to a collection_size_type to get a portable
  3144. // and efficient serialization
  3145. serialization::collection_size_type s1 (size1_);
  3146. serialization::collection_size_type s2 (size2_);
  3147. // serialize the sizes
  3148. ar & serialization::make_nvp("size1",s1)
  3149. & serialization::make_nvp("size2",s2);
  3150. // copy the values back if loading
  3151. if (Archive::is_loading::value) {
  3152. size1_ = s1;
  3153. size2_ = s2;
  3154. }
  3155. ar & serialization::make_nvp("data",data_);
  3156. }
  3157. private:
  3158. size_type size1_;
  3159. size_type size2_;
  3160. array_type data_;
  3161. };
  3162. /** \brief A matrix with all values of type \c T equal to zero
  3163. *
  3164. * Changing values does not affect the matrix, however assigning it to a normal matrix will put zero
  3165. * everywhere in the target matrix. All accesses are constant time, due to the trivial value.
  3166. *
  3167. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  3168. * \tparam ALLOC an allocator for storing the zero element. By default, a standar allocator is used.
  3169. */
  3170. template<class T, class ALLOC>
  3171. class zero_matrix:
  3172. public matrix_container<zero_matrix<T, ALLOC> > {
  3173. typedef const T *const_pointer;
  3174. typedef zero_matrix<T, ALLOC> self_type;
  3175. public:
  3176. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  3177. using matrix_container<self_type>::operator ();
  3178. #endif
  3179. typedef typename ALLOC::size_type size_type;
  3180. typedef typename ALLOC::difference_type difference_type;
  3181. typedef T value_type;
  3182. typedef const T &const_reference;
  3183. typedef T &reference;
  3184. typedef const matrix_reference<const self_type> const_closure_type;
  3185. typedef matrix_reference<self_type> closure_type;
  3186. typedef sparse_tag storage_category;
  3187. typedef unknown_orientation_tag orientation_category;
  3188. // Construction and destruction
  3189. BOOST_UBLAS_INLINE
  3190. zero_matrix ():
  3191. matrix_container<self_type> (),
  3192. size1_ (0), size2_ (0) {}
  3193. BOOST_UBLAS_INLINE
  3194. zero_matrix (size_type size):
  3195. matrix_container<self_type> (),
  3196. size1_ (size), size2_ (size) {}
  3197. BOOST_UBLAS_INLINE
  3198. zero_matrix (size_type size1, size_type size2):
  3199. matrix_container<self_type> (),
  3200. size1_ (size1), size2_ (size2) {}
  3201. BOOST_UBLAS_INLINE
  3202. zero_matrix (const zero_matrix &m):
  3203. matrix_container<self_type> (),
  3204. size1_ (m.size1_), size2_ (m.size2_) {}
  3205. // Accessors
  3206. BOOST_UBLAS_INLINE
  3207. size_type size1 () const {
  3208. return size1_;
  3209. }
  3210. BOOST_UBLAS_INLINE
  3211. size_type size2 () const {
  3212. return size2_;
  3213. }
  3214. // Resizing
  3215. BOOST_UBLAS_INLINE
  3216. void resize (size_type size, bool /*preserve*/ = true) {
  3217. size1_ = size;
  3218. size2_ = size;
  3219. }
  3220. BOOST_UBLAS_INLINE
  3221. void resize (size_type size1, size_type size2, bool /*preserve*/ = true) {
  3222. size1_ = size1;
  3223. size2_ = size2;
  3224. }
  3225. // Element access
  3226. BOOST_UBLAS_INLINE
  3227. const_reference operator () (size_type /* i */, size_type /* j */) const {
  3228. return zero_;
  3229. }
  3230. // Assignment
  3231. BOOST_UBLAS_INLINE
  3232. zero_matrix &operator = (const zero_matrix &m) {
  3233. size1_ = m.size1_;
  3234. size2_ = m.size2_;
  3235. return *this;
  3236. }
  3237. BOOST_UBLAS_INLINE
  3238. zero_matrix &assign_temporary (zero_matrix &m) {
  3239. swap (m);
  3240. return *this;
  3241. }
  3242. // Swapping
  3243. BOOST_UBLAS_INLINE
  3244. void swap (zero_matrix &m) {
  3245. if (this != &m) {
  3246. std::swap (size1_, m.size1_);
  3247. std::swap (size2_, m.size2_);
  3248. }
  3249. }
  3250. BOOST_UBLAS_INLINE
  3251. friend void swap (zero_matrix &m1, zero_matrix &m2) {
  3252. m1.swap (m2);
  3253. }
  3254. // Iterator types
  3255. public:
  3256. class const_iterator1;
  3257. class const_iterator2;
  3258. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  3259. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  3260. // Element lookup
  3261. BOOST_UBLAS_INLINE
  3262. const_iterator1 find1 (int /*rank*/, size_type /*i*/, size_type /*j*/) const {
  3263. return const_iterator1 (*this);
  3264. }
  3265. BOOST_UBLAS_INLINE
  3266. const_iterator2 find2 (int /*rank*/, size_type /*i*/, size_type /*j*/) const {
  3267. return const_iterator2 (*this);
  3268. }
  3269. class const_iterator1:
  3270. public container_const_reference<zero_matrix>,
  3271. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  3272. const_iterator1, value_type> {
  3273. public:
  3274. typedef typename zero_matrix::value_type value_type;
  3275. typedef typename zero_matrix::difference_type difference_type;
  3276. typedef typename zero_matrix::const_reference reference;
  3277. typedef typename zero_matrix::const_pointer pointer;
  3278. typedef const_iterator2 dual_iterator_type;
  3279. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  3280. // Construction and destruction
  3281. BOOST_UBLAS_INLINE
  3282. const_iterator1 ():
  3283. container_const_reference<self_type> () {}
  3284. BOOST_UBLAS_INLINE
  3285. const_iterator1 (const self_type &m):
  3286. container_const_reference<self_type> (m) {}
  3287. // Arithmetic
  3288. BOOST_UBLAS_INLINE
  3289. const_iterator1 &operator ++ () {
  3290. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3291. return *this;
  3292. }
  3293. BOOST_UBLAS_INLINE
  3294. const_iterator1 &operator -- () {
  3295. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3296. return *this;
  3297. }
  3298. // Dereference
  3299. BOOST_UBLAS_INLINE
  3300. const_reference operator * () const {
  3301. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3302. return zero_; // arbitary return value
  3303. }
  3304. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3305. BOOST_UBLAS_INLINE
  3306. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3307. typename self_type::
  3308. #endif
  3309. const_iterator2 begin () const {
  3310. return const_iterator2 ((*this) ());
  3311. }
  3312. BOOST_UBLAS_INLINE
  3313. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3314. typename self_type::
  3315. #endif
  3316. const_iterator2 cbegin () const {
  3317. return begin ();
  3318. }
  3319. BOOST_UBLAS_INLINE
  3320. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3321. typename self_type::
  3322. #endif
  3323. const_iterator2 end () const {
  3324. return const_iterator2 ((*this) ());
  3325. }
  3326. BOOST_UBLAS_INLINE
  3327. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3328. typename self_type::
  3329. #endif
  3330. const_iterator2 cend () const {
  3331. return end ();
  3332. }
  3333. BOOST_UBLAS_INLINE
  3334. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3335. typename self_type::
  3336. #endif
  3337. const_reverse_iterator2 rbegin () const {
  3338. return const_reverse_iterator2 (end ());
  3339. }
  3340. BOOST_UBLAS_INLINE
  3341. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3342. typename self_type::
  3343. #endif
  3344. const_reverse_iterator2 crbegin () const {
  3345. return rbegin ();
  3346. }
  3347. BOOST_UBLAS_INLINE
  3348. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3349. typename self_type::
  3350. #endif
  3351. const_reverse_iterator2 rend () const {
  3352. return const_reverse_iterator2 (begin ());
  3353. }
  3354. BOOST_UBLAS_INLINE
  3355. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3356. typename self_type::
  3357. #endif
  3358. const_reverse_iterator2 crend () const {
  3359. return rend ();
  3360. }
  3361. #endif
  3362. // Indices
  3363. BOOST_UBLAS_INLINE
  3364. size_type index1 () const {
  3365. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3366. return 0; // arbitary return value
  3367. }
  3368. BOOST_UBLAS_INLINE
  3369. size_type index2 () const {
  3370. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3371. return 0; // arbitary return value
  3372. }
  3373. // Assignment
  3374. BOOST_UBLAS_INLINE
  3375. const_iterator1 &operator = (const const_iterator1 &it) {
  3376. container_const_reference<self_type>::assign (&it ());
  3377. return *this;
  3378. }
  3379. // Comparison
  3380. BOOST_UBLAS_INLINE
  3381. bool operator == (const const_iterator1 &it) const {
  3382. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3383. detail::ignore_unused_variable_warning(it);
  3384. return true;
  3385. }
  3386. };
  3387. typedef const_iterator1 iterator1;
  3388. BOOST_UBLAS_INLINE
  3389. const_iterator1 begin1 () const {
  3390. return const_iterator1 (*this);
  3391. }
  3392. BOOST_UBLAS_INLINE
  3393. const_iterator1 cbegin1 () const {
  3394. return begin1 ();
  3395. }
  3396. BOOST_UBLAS_INLINE
  3397. const_iterator1 end1 () const {
  3398. return const_iterator1 (*this);
  3399. }
  3400. BOOST_UBLAS_INLINE
  3401. const_iterator1 cend1 () const {
  3402. return end1 ();
  3403. }
  3404. class const_iterator2:
  3405. public container_const_reference<zero_matrix>,
  3406. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  3407. const_iterator2, value_type> {
  3408. public:
  3409. typedef typename zero_matrix::value_type value_type;
  3410. typedef typename zero_matrix::difference_type difference_type;
  3411. typedef typename zero_matrix::const_reference reference;
  3412. typedef typename zero_matrix::const_pointer pointer;
  3413. typedef const_iterator1 dual_iterator_type;
  3414. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  3415. // Construction and destruction
  3416. BOOST_UBLAS_INLINE
  3417. const_iterator2 ():
  3418. container_const_reference<self_type> () {}
  3419. BOOST_UBLAS_INLINE
  3420. const_iterator2 (const self_type &m):
  3421. container_const_reference<self_type> (m) {}
  3422. // Arithmetic
  3423. BOOST_UBLAS_INLINE
  3424. const_iterator2 &operator ++ () {
  3425. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3426. return *this;
  3427. }
  3428. BOOST_UBLAS_INLINE
  3429. const_iterator2 &operator -- () {
  3430. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3431. return *this;
  3432. }
  3433. // Dereference
  3434. BOOST_UBLAS_INLINE
  3435. const_reference operator * () const {
  3436. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3437. return zero_; // arbitary return value
  3438. }
  3439. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3440. BOOST_UBLAS_INLINE
  3441. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3442. typename self_type::
  3443. #endif
  3444. const_iterator1 begin () const {
  3445. return const_iterator1 ((*this) ());
  3446. }
  3447. BOOST_UBLAS_INLINE
  3448. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3449. typename self_type::
  3450. #endif
  3451. const_iterator1 cbegin () const {
  3452. return begin ();
  3453. }
  3454. BOOST_UBLAS_INLINE
  3455. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3456. typename self_type::
  3457. #endif
  3458. const_iterator1 end () const {
  3459. return const_iterator1 ((*this) ());
  3460. }
  3461. BOOST_UBLAS_INLINE
  3462. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3463. typename self_type::
  3464. #endif
  3465. const_iterator1 cend () const {
  3466. return end ();
  3467. }
  3468. BOOST_UBLAS_INLINE
  3469. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3470. typename self_type::
  3471. #endif
  3472. const_reverse_iterator1 rbegin () const {
  3473. return const_reverse_iterator1 (end ());
  3474. }
  3475. BOOST_UBLAS_INLINE
  3476. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3477. typename self_type::
  3478. #endif
  3479. const_reverse_iterator1 crbegin () const {
  3480. return rbegin ();
  3481. }
  3482. BOOST_UBLAS_INLINE
  3483. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3484. typename self_type::
  3485. #endif
  3486. const_reverse_iterator1 rend () const {
  3487. return const_reverse_iterator1 (begin ());
  3488. }
  3489. BOOST_UBLAS_INLINE
  3490. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3491. typename self_type::
  3492. #endif
  3493. const_reverse_iterator1 crend () const {
  3494. return rend ();
  3495. }
  3496. #endif
  3497. // Indices
  3498. BOOST_UBLAS_INLINE
  3499. size_type index1 () const {
  3500. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3501. return 0; // arbitary return value
  3502. }
  3503. BOOST_UBLAS_INLINE
  3504. size_type index2 () const {
  3505. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  3506. return 0; // arbitary return value
  3507. }
  3508. // Assignment
  3509. BOOST_UBLAS_INLINE
  3510. const_iterator2 &operator = (const const_iterator2 &it) {
  3511. container_const_reference<self_type>::assign (&it ());
  3512. return *this;
  3513. }
  3514. // Comparison
  3515. BOOST_UBLAS_INLINE
  3516. bool operator == (const const_iterator2 &it) const {
  3517. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3518. detail::ignore_unused_variable_warning(it);
  3519. return true;
  3520. }
  3521. };
  3522. typedef const_iterator2 iterator2;
  3523. BOOST_UBLAS_INLINE
  3524. const_iterator2 begin2 () const {
  3525. return find2 (0, 0, 0);
  3526. }
  3527. BOOST_UBLAS_INLINE
  3528. const_iterator2 cbegin2 () const {
  3529. return begin2 ();
  3530. }
  3531. BOOST_UBLAS_INLINE
  3532. const_iterator2 end2 () const {
  3533. return find2 (0, 0, size2_);
  3534. }
  3535. BOOST_UBLAS_INLINE
  3536. const_iterator2 cend2 () const {
  3537. return end2 ();
  3538. }
  3539. // Reverse iterators
  3540. BOOST_UBLAS_INLINE
  3541. const_reverse_iterator1 rbegin1 () const {
  3542. return const_reverse_iterator1 (end1 ());
  3543. }
  3544. BOOST_UBLAS_INLINE
  3545. const_reverse_iterator1 crbegin1 () const {
  3546. return rbegin1 ();
  3547. }
  3548. BOOST_UBLAS_INLINE
  3549. const_reverse_iterator1 rend1 () const {
  3550. return const_reverse_iterator1 (begin1 ());
  3551. }
  3552. BOOST_UBLAS_INLINE
  3553. const_reverse_iterator1 crend1 () const {
  3554. return rend1 ();
  3555. }
  3556. BOOST_UBLAS_INLINE
  3557. const_reverse_iterator2 rbegin2 () const {
  3558. return const_reverse_iterator2 (end2 ());
  3559. }
  3560. BOOST_UBLAS_INLINE
  3561. const_reverse_iterator2 crbegin2 () const {
  3562. return rbegin2 ();
  3563. }
  3564. BOOST_UBLAS_INLINE
  3565. const_reverse_iterator2 rend2 () const {
  3566. return const_reverse_iterator2 (begin2 ());
  3567. }
  3568. BOOST_UBLAS_INLINE
  3569. const_reverse_iterator2 crend2 () const {
  3570. return rend2 ();
  3571. }
  3572. // Serialization
  3573. template<class Archive>
  3574. void serialize(Archive & ar, const unsigned int /* file_version */){
  3575. // we need to copy to a collection_size_type to get a portable
  3576. // and efficient serialization
  3577. serialization::collection_size_type s1 (size1_);
  3578. serialization::collection_size_type s2 (size2_);
  3579. // serialize the sizes
  3580. ar & serialization::make_nvp("size1",s1)
  3581. & serialization::make_nvp("size2",s2);
  3582. // copy the values back if loading
  3583. if (Archive::is_loading::value) {
  3584. size1_ = s1;
  3585. size2_ = s2;
  3586. }
  3587. }
  3588. private:
  3589. size_type size1_;
  3590. size_type size2_;
  3591. static const value_type zero_;
  3592. };
  3593. template<class T, class ALLOC>
  3594. const typename zero_matrix<T, ALLOC>::value_type zero_matrix<T, ALLOC>::zero_ = T(/*zero*/);
  3595. /** \brief An identity matrix with values of type \c T
  3596. *
  3597. * Elements or cordinates \f$(i,i)\f$ are equal to 1 (one) and all others to 0 (zero).
  3598. * Changing values does not affect the matrix, however assigning it to a normal matrix will
  3599. * make the matrix equal to an identity matrix. All accesses are constant du to the trivial values.
  3600. *
  3601. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  3602. * \tparam ALLOC an allocator for storing the zeros and one elements. By default, a standar allocator is used.
  3603. */
  3604. template<class T, class ALLOC>
  3605. class identity_matrix:
  3606. public matrix_container<identity_matrix<T, ALLOC> > {
  3607. typedef const T *const_pointer;
  3608. typedef identity_matrix<T, ALLOC> self_type;
  3609. public:
  3610. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  3611. using matrix_container<self_type>::operator ();
  3612. #endif
  3613. typedef typename ALLOC::size_type size_type;
  3614. typedef typename ALLOC::difference_type difference_type;
  3615. typedef T value_type;
  3616. typedef const T &const_reference;
  3617. typedef T &reference;
  3618. typedef const matrix_reference<const self_type> const_closure_type;
  3619. typedef matrix_reference<self_type> closure_type;
  3620. typedef sparse_tag storage_category;
  3621. typedef unknown_orientation_tag orientation_category;
  3622. // Construction and destruction
  3623. BOOST_UBLAS_INLINE
  3624. identity_matrix ():
  3625. matrix_container<self_type> (),
  3626. size1_ (0), size2_ (0), size_common_ (0) {}
  3627. BOOST_UBLAS_INLINE
  3628. identity_matrix (size_type size):
  3629. matrix_container<self_type> (),
  3630. size1_ (size), size2_ (size), size_common_ ((std::min) (size1_, size2_)) {}
  3631. BOOST_UBLAS_INLINE
  3632. identity_matrix (size_type size1, size_type size2):
  3633. matrix_container<self_type> (),
  3634. size1_ (size1), size2_ (size2), size_common_ ((std::min) (size1_, size2_)) {}
  3635. BOOST_UBLAS_INLINE
  3636. identity_matrix (const identity_matrix &m):
  3637. matrix_container<self_type> (),
  3638. size1_ (m.size1_), size2_ (m.size2_), size_common_ ((std::min) (size1_, size2_)) {}
  3639. // Accessors
  3640. BOOST_UBLAS_INLINE
  3641. size_type size1 () const {
  3642. return size1_;
  3643. }
  3644. BOOST_UBLAS_INLINE
  3645. size_type size2 () const {
  3646. return size2_;
  3647. }
  3648. // Resizing
  3649. BOOST_UBLAS_INLINE
  3650. void resize (size_type size, bool /*preserve*/ = true) {
  3651. size1_ = size;
  3652. size2_ = size;
  3653. size_common_ = ((std::min)(size1_, size2_));
  3654. }
  3655. BOOST_UBLAS_INLINE
  3656. void resize (size_type size1, size_type size2, bool /*preserve*/ = true) {
  3657. size1_ = size1;
  3658. size2_ = size2;
  3659. size_common_ = ((std::min)(size1_, size2_));
  3660. }
  3661. // Element access
  3662. BOOST_UBLAS_INLINE
  3663. const_reference operator () (size_type i, size_type j) const {
  3664. if (i == j)
  3665. return one_;
  3666. else
  3667. return zero_;
  3668. }
  3669. // Assignment
  3670. BOOST_UBLAS_INLINE
  3671. identity_matrix &operator = (const identity_matrix &m) {
  3672. size1_ = m.size1_;
  3673. size2_ = m.size2_;
  3674. size_common_ = m.size_common_;
  3675. return *this;
  3676. }
  3677. BOOST_UBLAS_INLINE
  3678. identity_matrix &assign_temporary (identity_matrix &m) {
  3679. swap (m);
  3680. return *this;
  3681. }
  3682. // Swapping
  3683. BOOST_UBLAS_INLINE
  3684. void swap (identity_matrix &m) {
  3685. if (this != &m) {
  3686. std::swap (size1_, m.size1_);
  3687. std::swap (size2_, m.size2_);
  3688. std::swap (size_common_, m.size_common_);
  3689. }
  3690. }
  3691. BOOST_UBLAS_INLINE
  3692. friend void swap (identity_matrix &m1, identity_matrix &m2) {
  3693. m1.swap (m2);
  3694. }
  3695. // Iterator types
  3696. private:
  3697. // Use an index
  3698. typedef size_type const_subiterator_type;
  3699. public:
  3700. class const_iterator1;
  3701. class const_iterator2;
  3702. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  3703. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  3704. // Element lookup
  3705. BOOST_UBLAS_INLINE
  3706. const_iterator1 find1 (int rank, size_type i, size_type j) const {
  3707. if (rank == 1) {
  3708. i = (std::max) (i, j);
  3709. i = (std::min) (i, j + 1);
  3710. }
  3711. return const_iterator1 (*this, i);
  3712. }
  3713. BOOST_UBLAS_INLINE
  3714. const_iterator2 find2 (int rank, size_type i, size_type j) const {
  3715. if (rank == 1) {
  3716. j = (std::max) (j, i);
  3717. j = (std::min) (j, i + 1);
  3718. }
  3719. return const_iterator2 (*this, j);
  3720. }
  3721. class const_iterator1:
  3722. public container_const_reference<identity_matrix>,
  3723. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  3724. const_iterator1, value_type> {
  3725. public:
  3726. typedef typename identity_matrix::value_type value_type;
  3727. typedef typename identity_matrix::difference_type difference_type;
  3728. typedef typename identity_matrix::const_reference reference;
  3729. typedef typename identity_matrix::const_pointer pointer;
  3730. typedef const_iterator2 dual_iterator_type;
  3731. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  3732. // Construction and destruction
  3733. BOOST_UBLAS_INLINE
  3734. const_iterator1 ():
  3735. container_const_reference<self_type> (), it_ () {}
  3736. BOOST_UBLAS_INLINE
  3737. const_iterator1 (const self_type &m, const const_subiterator_type &it):
  3738. container_const_reference<self_type> (m), it_ (it) {}
  3739. // Arithmetic
  3740. BOOST_UBLAS_INLINE
  3741. const_iterator1 &operator ++ () {
  3742. BOOST_UBLAS_CHECK (it_ < (*this) ().size1 (), bad_index ());
  3743. ++it_;
  3744. return *this;
  3745. }
  3746. BOOST_UBLAS_INLINE
  3747. const_iterator1 &operator -- () {
  3748. BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
  3749. --it_;
  3750. return *this;
  3751. }
  3752. // Dereference
  3753. BOOST_UBLAS_INLINE
  3754. const_reference operator * () const {
  3755. return one_;
  3756. }
  3757. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3758. BOOST_UBLAS_INLINE
  3759. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3760. typename self_type::
  3761. #endif
  3762. const_iterator2 begin () const {
  3763. return const_iterator2 ((*this) (), it_);
  3764. }
  3765. BOOST_UBLAS_INLINE
  3766. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3767. typename self_type::
  3768. #endif
  3769. const_iterator2 cbegin () const {
  3770. return begin ();
  3771. }
  3772. BOOST_UBLAS_INLINE
  3773. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3774. typename self_type::
  3775. #endif
  3776. const_iterator2 end () const {
  3777. return const_iterator2 ((*this) (), it_ + 1);
  3778. }
  3779. BOOST_UBLAS_INLINE
  3780. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3781. typename self_type::
  3782. #endif
  3783. const_iterator2 cend () const {
  3784. return end ();
  3785. }
  3786. BOOST_UBLAS_INLINE
  3787. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3788. typename self_type::
  3789. #endif
  3790. const_reverse_iterator2 rbegin () const {
  3791. return const_reverse_iterator2 (end ());
  3792. }
  3793. BOOST_UBLAS_INLINE
  3794. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3795. typename self_type::
  3796. #endif
  3797. const_reverse_iterator2 crbegin () const {
  3798. return rbegin ();
  3799. }
  3800. BOOST_UBLAS_INLINE
  3801. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3802. typename self_type::
  3803. #endif
  3804. const_reverse_iterator2 rend () const {
  3805. return const_reverse_iterator2 (begin ());
  3806. }
  3807. BOOST_UBLAS_INLINE
  3808. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3809. typename self_type::
  3810. #endif
  3811. const_reverse_iterator2 crend () const {
  3812. return rend ();
  3813. }
  3814. #endif
  3815. // Indices
  3816. BOOST_UBLAS_INLINE
  3817. size_type index1 () const {
  3818. return it_;
  3819. }
  3820. BOOST_UBLAS_INLINE
  3821. size_type index2 () const {
  3822. return it_;
  3823. }
  3824. // Assignment
  3825. BOOST_UBLAS_INLINE
  3826. const_iterator1 &operator = (const const_iterator1 &it) {
  3827. container_const_reference<self_type>::assign (&it ());
  3828. it_ = it.it_;
  3829. return *this;
  3830. }
  3831. // Comparison
  3832. BOOST_UBLAS_INLINE
  3833. bool operator == (const const_iterator1 &it) const {
  3834. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3835. return it_ == it.it_;
  3836. }
  3837. private:
  3838. const_subiterator_type it_;
  3839. };
  3840. typedef const_iterator1 iterator1;
  3841. BOOST_UBLAS_INLINE
  3842. const_iterator1 begin1 () const {
  3843. return const_iterator1 (*this, 0);
  3844. }
  3845. BOOST_UBLAS_INLINE
  3846. const_iterator1 cbegin1 () const {
  3847. return begin1 ();
  3848. }
  3849. BOOST_UBLAS_INLINE
  3850. const_iterator1 end1 () const {
  3851. return const_iterator1 (*this, size_common_);
  3852. }
  3853. BOOST_UBLAS_INLINE
  3854. const_iterator1 cend1 () const {
  3855. return end1 ();
  3856. }
  3857. class const_iterator2:
  3858. public container_const_reference<identity_matrix>,
  3859. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  3860. const_iterator2, value_type> {
  3861. public:
  3862. typedef typename identity_matrix::value_type value_type;
  3863. typedef typename identity_matrix::difference_type difference_type;
  3864. typedef typename identity_matrix::const_reference reference;
  3865. typedef typename identity_matrix::const_pointer pointer;
  3866. typedef const_iterator1 dual_iterator_type;
  3867. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  3868. // Construction and destruction
  3869. BOOST_UBLAS_INLINE
  3870. const_iterator2 ():
  3871. container_const_reference<self_type> (), it_ () {}
  3872. BOOST_UBLAS_INLINE
  3873. const_iterator2 (const self_type &m, const const_subiterator_type &it):
  3874. container_const_reference<self_type> (m), it_ (it) {}
  3875. // Arithmetic
  3876. BOOST_UBLAS_INLINE
  3877. const_iterator2 &operator ++ () {
  3878. BOOST_UBLAS_CHECK (it_ < (*this) ().size_common_, bad_index ());
  3879. ++it_;
  3880. return *this;
  3881. }
  3882. BOOST_UBLAS_INLINE
  3883. const_iterator2 &operator -- () {
  3884. BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
  3885. --it_;
  3886. return *this;
  3887. }
  3888. // Dereference
  3889. BOOST_UBLAS_INLINE
  3890. const_reference operator * () const {
  3891. return one_;
  3892. }
  3893. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3894. BOOST_UBLAS_INLINE
  3895. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3896. typename self_type::
  3897. #endif
  3898. const_iterator1 begin () const {
  3899. return const_iterator1 ((*this) (), it_);
  3900. }
  3901. BOOST_UBLAS_INLINE
  3902. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3903. typename self_type::
  3904. #endif
  3905. const_iterator1 cbegin () const {
  3906. return begin ();
  3907. }
  3908. BOOST_UBLAS_INLINE
  3909. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3910. typename self_type::
  3911. #endif
  3912. const_iterator1 end () const {
  3913. return const_iterator1 ((*this) (), it_ + 1);
  3914. }
  3915. BOOST_UBLAS_INLINE
  3916. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3917. typename self_type::
  3918. #endif
  3919. const_iterator1 cend () const {
  3920. return end ();
  3921. }
  3922. BOOST_UBLAS_INLINE
  3923. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3924. typename self_type::
  3925. #endif
  3926. const_reverse_iterator1 rbegin () const {
  3927. return const_reverse_iterator1 (end ());
  3928. }
  3929. BOOST_UBLAS_INLINE
  3930. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3931. typename self_type::
  3932. #endif
  3933. const_reverse_iterator1 crbegin () const {
  3934. return rbegin ();
  3935. }
  3936. BOOST_UBLAS_INLINE
  3937. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3938. typename self_type::
  3939. #endif
  3940. const_reverse_iterator1 rend () const {
  3941. return const_reverse_iterator1 (begin ());
  3942. }
  3943. BOOST_UBLAS_INLINE
  3944. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3945. typename self_type::
  3946. #endif
  3947. const_reverse_iterator1 crend () const {
  3948. return rend ();
  3949. }
  3950. #endif
  3951. // Indices
  3952. BOOST_UBLAS_INLINE
  3953. size_type index1 () const {
  3954. return it_;
  3955. }
  3956. BOOST_UBLAS_INLINE
  3957. size_type index2 () const {
  3958. return it_;
  3959. }
  3960. // Assignment
  3961. BOOST_UBLAS_INLINE
  3962. const_iterator2 &operator = (const const_iterator2 &it) {
  3963. container_const_reference<self_type>::assign (&it ());
  3964. it_ = it.it_;
  3965. return *this;
  3966. }
  3967. // Comparison
  3968. BOOST_UBLAS_INLINE
  3969. bool operator == (const const_iterator2 &it) const {
  3970. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3971. return it_ == it.it_;
  3972. }
  3973. private:
  3974. const_subiterator_type it_;
  3975. };
  3976. typedef const_iterator2 iterator2;
  3977. BOOST_UBLAS_INLINE
  3978. const_iterator2 begin2 () const {
  3979. return const_iterator2 (*this, 0);
  3980. }
  3981. BOOST_UBLAS_INLINE
  3982. const_iterator2 cbegin2 () const {
  3983. return begin2 ();
  3984. }
  3985. BOOST_UBLAS_INLINE
  3986. const_iterator2 end2 () const {
  3987. return const_iterator2 (*this, size_common_);
  3988. }
  3989. BOOST_UBLAS_INLINE
  3990. const_iterator2 cend2 () const {
  3991. return end2 ();
  3992. }
  3993. // Reverse iterators
  3994. BOOST_UBLAS_INLINE
  3995. const_reverse_iterator1 rbegin1 () const {
  3996. return const_reverse_iterator1 (end1 ());
  3997. }
  3998. BOOST_UBLAS_INLINE
  3999. const_reverse_iterator1 crbegin1 () const {
  4000. return rbegin1 ();
  4001. }
  4002. BOOST_UBLAS_INLINE
  4003. const_reverse_iterator1 rend1 () const {
  4004. return const_reverse_iterator1 (begin1 ());
  4005. }
  4006. BOOST_UBLAS_INLINE
  4007. const_reverse_iterator1 crend1 () const {
  4008. return rend1 ();
  4009. }
  4010. BOOST_UBLAS_INLINE
  4011. const_reverse_iterator2 rbegin2 () const {
  4012. return const_reverse_iterator2 (end2 ());
  4013. }
  4014. BOOST_UBLAS_INLINE
  4015. const_reverse_iterator2 crbegin2 () const {
  4016. return rbegin2 ();
  4017. }
  4018. BOOST_UBLAS_INLINE
  4019. const_reverse_iterator2 rend2 () const {
  4020. return const_reverse_iterator2 (begin2 ());
  4021. }
  4022. BOOST_UBLAS_INLINE
  4023. const_reverse_iterator2 crend2 () const {
  4024. return rend2 ();
  4025. }
  4026. // Serialization
  4027. template<class Archive>
  4028. void serialize(Archive & ar, const unsigned int /* file_version */){
  4029. // we need to copy to a collection_size_type to get a portable
  4030. // and efficient serialization
  4031. serialization::collection_size_type s1 (size1_);
  4032. serialization::collection_size_type s2 (size2_);
  4033. // serialize the sizes
  4034. ar & serialization::make_nvp("size1",s1)
  4035. & serialization::make_nvp("size2",s2);
  4036. // copy the values back if loading
  4037. if (Archive::is_loading::value) {
  4038. size1_ = s1;
  4039. size2_ = s2;
  4040. size_common_ = ((std::min)(size1_, size2_));
  4041. }
  4042. }
  4043. private:
  4044. size_type size1_;
  4045. size_type size2_;
  4046. size_type size_common_;
  4047. static const value_type zero_;
  4048. static const value_type one_;
  4049. };
  4050. template<class T, class ALLOC>
  4051. const typename identity_matrix<T, ALLOC>::value_type identity_matrix<T, ALLOC>::zero_ = T(/*zero*/);
  4052. template<class T, class ALLOC>
  4053. const typename identity_matrix<T, ALLOC>::value_type identity_matrix<T, ALLOC>::one_ (1); // ISSUE: need 'one'-traits here
  4054. /** \brief A matrix with all values of type \c T equal to the same value
  4055. *
  4056. * Changing one value has the effect of changing all the values. Assigning it to a normal matrix will copy
  4057. * the same value everywhere in this matrix. All accesses are constant time, due to the trivial value.
  4058. *
  4059. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  4060. * \tparam ALLOC an allocator for storing the unique value. By default, a standar allocator is used.
  4061. */
  4062. template<class T, class ALLOC>
  4063. class scalar_matrix:
  4064. public matrix_container<scalar_matrix<T, ALLOC> > {
  4065. typedef const T *const_pointer;
  4066. typedef scalar_matrix<T, ALLOC> self_type;
  4067. public:
  4068. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  4069. using matrix_container<self_type>::operator ();
  4070. #endif
  4071. typedef std::size_t size_type;
  4072. typedef std::ptrdiff_t difference_type;
  4073. typedef T value_type;
  4074. typedef const T &const_reference;
  4075. typedef T &reference;
  4076. typedef const matrix_reference<const self_type> const_closure_type;
  4077. typedef matrix_reference<self_type> closure_type;
  4078. typedef dense_tag storage_category;
  4079. typedef unknown_orientation_tag orientation_category;
  4080. // Construction and destruction
  4081. BOOST_UBLAS_INLINE
  4082. scalar_matrix ():
  4083. matrix_container<self_type> (),
  4084. size1_ (0), size2_ (0), value_ () {}
  4085. BOOST_UBLAS_INLINE
  4086. scalar_matrix (size_type size1, size_type size2, const value_type &value = value_type(1)):
  4087. matrix_container<self_type> (),
  4088. size1_ (size1), size2_ (size2), value_ (value) {}
  4089. BOOST_UBLAS_INLINE
  4090. scalar_matrix (const scalar_matrix &m):
  4091. matrix_container<self_type> (),
  4092. size1_ (m.size1_), size2_ (m.size2_), value_ (m.value_) {}
  4093. // Accessors
  4094. BOOST_UBLAS_INLINE
  4095. size_type size1 () const {
  4096. return size1_;
  4097. }
  4098. BOOST_UBLAS_INLINE
  4099. size_type size2 () const {
  4100. return size2_;
  4101. }
  4102. // Resizing
  4103. BOOST_UBLAS_INLINE
  4104. void resize (size_type size1, size_type size2, bool /*preserve*/ = true) {
  4105. size1_ = size1;
  4106. size2_ = size2;
  4107. }
  4108. // Element access
  4109. BOOST_UBLAS_INLINE
  4110. const_reference operator () (size_type /*i*/, size_type /*j*/) const {
  4111. return value_;
  4112. }
  4113. // Assignment
  4114. BOOST_UBLAS_INLINE
  4115. scalar_matrix &operator = (const scalar_matrix &m) {
  4116. size1_ = m.size1_;
  4117. size2_ = m.size2_;
  4118. value_ = m.value_;
  4119. return *this;
  4120. }
  4121. BOOST_UBLAS_INLINE
  4122. scalar_matrix &assign_temporary (scalar_matrix &m) {
  4123. swap (m);
  4124. return *this;
  4125. }
  4126. // Swapping
  4127. BOOST_UBLAS_INLINE
  4128. void swap (scalar_matrix &m) {
  4129. if (this != &m) {
  4130. std::swap (size1_, m.size1_);
  4131. std::swap (size2_, m.size2_);
  4132. std::swap (value_, m.value_);
  4133. }
  4134. }
  4135. BOOST_UBLAS_INLINE
  4136. friend void swap (scalar_matrix &m1, scalar_matrix &m2) {
  4137. m1.swap (m2);
  4138. }
  4139. // Iterator types
  4140. private:
  4141. // Use an index
  4142. typedef size_type const_subiterator_type;
  4143. public:
  4144. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4145. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  4146. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  4147. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  4148. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  4149. #else
  4150. class const_iterator1;
  4151. class const_iterator2;
  4152. #endif
  4153. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  4154. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  4155. // Element lookup
  4156. BOOST_UBLAS_INLINE
  4157. const_iterator1 find1 (int /*rank*/, size_type i, size_type j) const {
  4158. return const_iterator1 (*this, i, j);
  4159. }
  4160. BOOST_UBLAS_INLINE
  4161. const_iterator2 find2 (int /*rank*/, size_type i, size_type j) const {
  4162. return const_iterator2 (*this, i, j);
  4163. }
  4164. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4165. class const_iterator1:
  4166. public container_const_reference<scalar_matrix>,
  4167. public random_access_iterator_base<dense_random_access_iterator_tag,
  4168. const_iterator1, value_type> {
  4169. public:
  4170. typedef typename scalar_matrix::value_type value_type;
  4171. typedef typename scalar_matrix::difference_type difference_type;
  4172. typedef typename scalar_matrix::const_reference reference;
  4173. typedef typename scalar_matrix::const_pointer pointer;
  4174. typedef const_iterator2 dual_iterator_type;
  4175. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  4176. // Construction and destruction
  4177. BOOST_UBLAS_INLINE
  4178. const_iterator1 ():
  4179. container_const_reference<scalar_matrix> (), it1_ (), it2_ () {}
  4180. BOOST_UBLAS_INLINE
  4181. const_iterator1 (const scalar_matrix &m, const const_subiterator_type &it1, const const_subiterator_type &it2):
  4182. container_const_reference<scalar_matrix> (m), it1_ (it1), it2_ (it2) {}
  4183. // Arithmetic
  4184. BOOST_UBLAS_INLINE
  4185. const_iterator1 &operator ++ () {
  4186. ++ it1_;
  4187. return *this;
  4188. }
  4189. BOOST_UBLAS_INLINE
  4190. const_iterator1 &operator -- () {
  4191. -- it1_;
  4192. return *this;
  4193. }
  4194. BOOST_UBLAS_INLINE
  4195. const_iterator1 &operator += (difference_type n) {
  4196. it1_ += n;
  4197. return *this;
  4198. }
  4199. BOOST_UBLAS_INLINE
  4200. const_iterator1 &operator -= (difference_type n) {
  4201. it1_ -= n;
  4202. return *this;
  4203. }
  4204. BOOST_UBLAS_INLINE
  4205. difference_type operator - (const const_iterator1 &it) const {
  4206. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4207. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  4208. return it1_ - it.it1_;
  4209. }
  4210. // Dereference
  4211. BOOST_UBLAS_INLINE
  4212. const_reference operator * () const {
  4213. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  4214. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  4215. return (*this) () (index1 (), index2 ());
  4216. }
  4217. BOOST_UBLAS_INLINE
  4218. const_reference operator [] (difference_type n) const {
  4219. return *(*this + n);
  4220. }
  4221. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  4222. BOOST_UBLAS_INLINE
  4223. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4224. typename self_type::
  4225. #endif
  4226. const_iterator2 begin () const {
  4227. const scalar_matrix &m = (*this) ();
  4228. return m.find2 (1, index1 (), 0);
  4229. }
  4230. BOOST_UBLAS_INLINE
  4231. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4232. typename self_type::
  4233. #endif
  4234. const_iterator2 cbegin () const {
  4235. return begin ();
  4236. }
  4237. BOOST_UBLAS_INLINE
  4238. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4239. typename self_type::
  4240. #endif
  4241. const_iterator2 end () const {
  4242. const scalar_matrix &m = (*this) ();
  4243. return m.find2 (1, index1 (), m.size2 ());
  4244. }
  4245. BOOST_UBLAS_INLINE
  4246. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4247. typename self_type::
  4248. #endif
  4249. const_iterator2 cend () const {
  4250. return end ();
  4251. }
  4252. BOOST_UBLAS_INLINE
  4253. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4254. typename self_type::
  4255. #endif
  4256. const_reverse_iterator2 rbegin () const {
  4257. return const_reverse_iterator2 (end ());
  4258. }
  4259. BOOST_UBLAS_INLINE
  4260. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4261. typename self_type::
  4262. #endif
  4263. const_reverse_iterator2 crbegin () const {
  4264. return rbegin ();
  4265. }
  4266. BOOST_UBLAS_INLINE
  4267. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4268. typename self_type::
  4269. #endif
  4270. const_reverse_iterator2 rend () const {
  4271. return const_reverse_iterator2 (begin ());
  4272. }
  4273. BOOST_UBLAS_INLINE
  4274. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4275. typename self_type::
  4276. #endif
  4277. const_reverse_iterator2 crend () const {
  4278. return rend ();
  4279. }
  4280. #endif
  4281. // Indices
  4282. BOOST_UBLAS_INLINE
  4283. size_type index1 () const {
  4284. return it1_;
  4285. }
  4286. BOOST_UBLAS_INLINE
  4287. size_type index2 () const {
  4288. return it2_;
  4289. }
  4290. // Assignment
  4291. BOOST_UBLAS_INLINE
  4292. const_iterator1 &operator = (const const_iterator1 &it) {
  4293. container_const_reference<scalar_matrix>::assign (&it ());
  4294. it1_ = it.it1_;
  4295. it2_ = it.it2_;
  4296. return *this;
  4297. }
  4298. // Comparison
  4299. BOOST_UBLAS_INLINE
  4300. bool operator == (const const_iterator1 &it) const {
  4301. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4302. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  4303. return it1_ == it.it1_;
  4304. }
  4305. BOOST_UBLAS_INLINE
  4306. bool operator < (const const_iterator1 &it) const {
  4307. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4308. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  4309. return it1_ < it.it1_;
  4310. }
  4311. private:
  4312. const_subiterator_type it1_;
  4313. const_subiterator_type it2_;
  4314. };
  4315. typedef const_iterator1 iterator1;
  4316. #endif
  4317. BOOST_UBLAS_INLINE
  4318. const_iterator1 begin1 () const {
  4319. return find1 (0, 0, 0);
  4320. }
  4321. BOOST_UBLAS_INLINE
  4322. const_iterator1 cbegin1 () const {
  4323. return begin1 ();
  4324. }
  4325. BOOST_UBLAS_INLINE
  4326. const_iterator1 end1 () const {
  4327. return find1 (0, size1_, 0);
  4328. }
  4329. BOOST_UBLAS_INLINE
  4330. const_iterator1 cend1 () const {
  4331. return end1 ();
  4332. }
  4333. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4334. class const_iterator2:
  4335. public container_const_reference<scalar_matrix>,
  4336. public random_access_iterator_base<dense_random_access_iterator_tag,
  4337. const_iterator2, value_type> {
  4338. public:
  4339. typedef typename scalar_matrix::value_type value_type;
  4340. typedef typename scalar_matrix::difference_type difference_type;
  4341. typedef typename scalar_matrix::const_reference reference;
  4342. typedef typename scalar_matrix::const_pointer pointer;
  4343. typedef const_iterator1 dual_iterator_type;
  4344. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  4345. // Construction and destruction
  4346. BOOST_UBLAS_INLINE
  4347. const_iterator2 ():
  4348. container_const_reference<scalar_matrix> (), it1_ (), it2_ () {}
  4349. BOOST_UBLAS_INLINE
  4350. const_iterator2 (const scalar_matrix &m, const const_subiterator_type &it1, const const_subiterator_type &it2):
  4351. container_const_reference<scalar_matrix> (m), it1_ (it1), it2_ (it2) {}
  4352. // Arithmetic
  4353. BOOST_UBLAS_INLINE
  4354. const_iterator2 &operator ++ () {
  4355. ++ it2_;
  4356. return *this;
  4357. }
  4358. BOOST_UBLAS_INLINE
  4359. const_iterator2 &operator -- () {
  4360. -- it2_;
  4361. return *this;
  4362. }
  4363. BOOST_UBLAS_INLINE
  4364. const_iterator2 &operator += (difference_type n) {
  4365. it2_ += n;
  4366. return *this;
  4367. }
  4368. BOOST_UBLAS_INLINE
  4369. const_iterator2 &operator -= (difference_type n) {
  4370. it2_ -= n;
  4371. return *this;
  4372. }
  4373. BOOST_UBLAS_INLINE
  4374. difference_type operator - (const const_iterator2 &it) const {
  4375. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4376. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  4377. return it2_ - it.it2_;
  4378. }
  4379. // Dereference
  4380. BOOST_UBLAS_INLINE
  4381. const_reference operator * () const {
  4382. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  4383. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  4384. return (*this) () (index1 (), index2 ());
  4385. }
  4386. BOOST_UBLAS_INLINE
  4387. const_reference operator [] (difference_type n) const {
  4388. return *(*this + n);
  4389. }
  4390. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  4391. BOOST_UBLAS_INLINE
  4392. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4393. typename self_type::
  4394. #endif
  4395. const_iterator1 begin () const {
  4396. const scalar_matrix &m = (*this) ();
  4397. return m.find1 (1, 0, index2 ());
  4398. }
  4399. BOOST_UBLAS_INLINE
  4400. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4401. typename self_type::
  4402. #endif
  4403. const_iterator1 cbegin () const {
  4404. return begin ();
  4405. }
  4406. BOOST_UBLAS_INLINE
  4407. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4408. typename self_type::
  4409. #endif
  4410. const_iterator1 end () const {
  4411. const scalar_matrix &m = (*this) ();
  4412. return m.find1 (1, m.size1 (), index2 ());
  4413. }
  4414. BOOST_UBLAS_INLINE
  4415. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4416. typename self_type::
  4417. #endif
  4418. const_iterator1 cend () const {
  4419. return end ();
  4420. }
  4421. BOOST_UBLAS_INLINE
  4422. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4423. typename self_type::
  4424. #endif
  4425. const_reverse_iterator1 rbegin () const {
  4426. return const_reverse_iterator1 (end ());
  4427. }
  4428. BOOST_UBLAS_INLINE
  4429. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4430. typename self_type::
  4431. #endif
  4432. const_reverse_iterator1 crbegin () const {
  4433. return rbegin ();
  4434. }
  4435. BOOST_UBLAS_INLINE
  4436. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4437. typename self_type::
  4438. #endif
  4439. const_reverse_iterator1 rend () const {
  4440. return const_reverse_iterator1 (begin ());
  4441. }
  4442. BOOST_UBLAS_INLINE
  4443. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4444. typename self_type::
  4445. #endif
  4446. const_reverse_iterator1 crend () const {
  4447. return rend ();
  4448. }
  4449. #endif
  4450. // Indices
  4451. BOOST_UBLAS_INLINE
  4452. size_type index1 () const {
  4453. return it1_;
  4454. }
  4455. BOOST_UBLAS_INLINE
  4456. size_type index2 () const {
  4457. return it2_;
  4458. }
  4459. // Assignment
  4460. BOOST_UBLAS_INLINE
  4461. const_iterator2 &operator = (const const_iterator2 &it) {
  4462. container_const_reference<scalar_matrix>::assign (&it ());
  4463. it1_ = it.it1_;
  4464. it2_ = it.it2_;
  4465. return *this;
  4466. }
  4467. // Comparison
  4468. BOOST_UBLAS_INLINE
  4469. bool operator == (const const_iterator2 &it) const {
  4470. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4471. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  4472. return it2_ == it.it2_;
  4473. }
  4474. BOOST_UBLAS_INLINE
  4475. bool operator < (const const_iterator2 &it) const {
  4476. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4477. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  4478. return it2_ < it.it2_;
  4479. }
  4480. private:
  4481. const_subiterator_type it1_;
  4482. const_subiterator_type it2_;
  4483. };
  4484. typedef const_iterator2 iterator2;
  4485. #endif
  4486. BOOST_UBLAS_INLINE
  4487. const_iterator2 begin2 () const {
  4488. return find2 (0, 0, 0);
  4489. }
  4490. BOOST_UBLAS_INLINE
  4491. const_iterator2 cbegin2 () const {
  4492. return begin2 ();
  4493. }
  4494. BOOST_UBLAS_INLINE
  4495. const_iterator2 end2 () const {
  4496. return find2 (0, 0, size2_);
  4497. }
  4498. BOOST_UBLAS_INLINE
  4499. const_iterator2 cend2 () const {
  4500. return end2 ();
  4501. }
  4502. // Reverse iterators
  4503. BOOST_UBLAS_INLINE
  4504. const_reverse_iterator1 rbegin1 () const {
  4505. return const_reverse_iterator1 (end1 ());
  4506. }
  4507. BOOST_UBLAS_INLINE
  4508. const_reverse_iterator1 crbegin1 () const {
  4509. return rbegin1 ();
  4510. }
  4511. BOOST_UBLAS_INLINE
  4512. const_reverse_iterator1 rend1 () const {
  4513. return const_reverse_iterator1 (begin1 ());
  4514. }
  4515. BOOST_UBLAS_INLINE
  4516. const_reverse_iterator1 crend1 () const {
  4517. return rend1 ();
  4518. }
  4519. BOOST_UBLAS_INLINE
  4520. const_reverse_iterator2 rbegin2 () const {
  4521. return const_reverse_iterator2 (end2 ());
  4522. }
  4523. BOOST_UBLAS_INLINE
  4524. const_reverse_iterator2 crbegin2 () const {
  4525. return rbegin2 ();
  4526. }
  4527. BOOST_UBLAS_INLINE
  4528. const_reverse_iterator2 rend2 () const {
  4529. return const_reverse_iterator2 (begin2 ());
  4530. }
  4531. BOOST_UBLAS_INLINE
  4532. const_reverse_iterator2 crend2 () const {
  4533. return rend2 ();
  4534. }
  4535. // Serialization
  4536. template<class Archive>
  4537. void serialize(Archive & ar, const unsigned int /* file_version */){
  4538. // we need to copy to a collection_size_type to get a portable
  4539. // and efficient serialization
  4540. serialization::collection_size_type s1 (size1_);
  4541. serialization::collection_size_type s2 (size2_);
  4542. // serialize the sizes
  4543. ar & serialization::make_nvp("size1",s1)
  4544. & serialization::make_nvp("size2",s2);
  4545. // copy the values back if loading
  4546. if (Archive::is_loading::value) {
  4547. size1_ = s1;
  4548. size2_ = s2;
  4549. }
  4550. ar & serialization::make_nvp("value", value_);
  4551. }
  4552. private:
  4553. size_type size1_;
  4554. size_type size2_;
  4555. value_type value_;
  4556. };
  4557. /** \brief An array based matrix class which size is defined at type specification or object instanciation
  4558. *
  4559. * This matrix is directly based on a predefined C-style arry of data, thus providing the fastest
  4560. * implementation possible. The constraint is that dimensions of the matrix must be specified at
  4561. * the instanciation or the type specification.
  4562. *
  4563. * For instance, \code typedef c_matrix<double,4,4> my_4by4_matrix \endcode
  4564. * defines a 4 by 4 double-precision matrix. You can also instantiate it directly with
  4565. * \code c_matrix<int,8,5> my_fast_matrix \endcode. This will make a 8 by 5 integer matrix. The
  4566. * price to pay for this speed is that you cannot resize it to a size larger than the one defined
  4567. * in the template parameters. In the previous example, a size of 4 by 5 or 3 by 2 is acceptable,
  4568. * but a new size of 9 by 5 or even 10 by 10 will raise a bad_size() exception.
  4569. *
  4570. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  4571. * \tparam N the default maximum number of rows
  4572. * \tparam M the default maximum number of columns
  4573. */
  4574. template<class T, std::size_t N, std::size_t M>
  4575. class c_matrix:
  4576. public matrix_container<c_matrix<T, N, M> > {
  4577. typedef c_matrix<T, N, M> self_type;
  4578. public:
  4579. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  4580. using matrix_container<self_type>::operator ();
  4581. #endif
  4582. typedef std::size_t size_type;
  4583. typedef std::ptrdiff_t difference_type;
  4584. typedef T value_type;
  4585. typedef const T &const_reference;
  4586. typedef T &reference;
  4587. typedef const T *const_pointer;
  4588. typedef T *pointer;
  4589. typedef const matrix_reference<const self_type> const_closure_type;
  4590. typedef matrix_reference<self_type> closure_type;
  4591. typedef c_vector<T, N * M> vector_temporary_type; // vector able to store all elements of c_matrix
  4592. typedef self_type matrix_temporary_type;
  4593. typedef dense_tag storage_category;
  4594. // This could be better for performance,
  4595. // typedef typename unknown_orientation_tag orientation_category;
  4596. // but others depend on the orientation information...
  4597. typedef row_major_tag orientation_category;
  4598. // Construction and destruction
  4599. BOOST_UBLAS_INLINE
  4600. c_matrix ():
  4601. size1_ (N), size2_ (M) /* , data_ () */ {
  4602. }
  4603. BOOST_UBLAS_INLINE
  4604. c_matrix (size_type size1, size_type size2):
  4605. size1_ (size1), size2_ (size2) /* , data_ () */ {
  4606. if (size1_ > N || size2_ > M)
  4607. bad_size ().raise ();
  4608. }
  4609. BOOST_UBLAS_INLINE
  4610. c_matrix (const c_matrix &m):
  4611. size1_ (m.size1_), size2_ (m.size2_) /* , data_ () */ {
  4612. if (size1_ > N || size2_ > M)
  4613. bad_size ().raise ();
  4614. assign(m);
  4615. }
  4616. template<class AE>
  4617. BOOST_UBLAS_INLINE
  4618. c_matrix (const matrix_expression<AE> &ae):
  4619. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()) /* , data_ () */ {
  4620. if (size1_ > N || size2_ > M)
  4621. bad_size ().raise ();
  4622. matrix_assign<scalar_assign> (*this, ae);
  4623. }
  4624. // Accessors
  4625. BOOST_UBLAS_INLINE
  4626. size_type size1 () const {
  4627. return size1_;
  4628. }
  4629. BOOST_UBLAS_INLINE
  4630. size_type size2 () const {
  4631. return size2_;
  4632. }
  4633. BOOST_UBLAS_INLINE
  4634. const_pointer data () const {
  4635. return reinterpret_cast<const_pointer> (data_);
  4636. }
  4637. BOOST_UBLAS_INLINE
  4638. pointer data () {
  4639. return reinterpret_cast<pointer> (data_);
  4640. }
  4641. // Resizing
  4642. BOOST_UBLAS_INLINE
  4643. void resize (size_type size1, size_type size2, bool preserve = true) {
  4644. if (size1 > N || size2 > M)
  4645. bad_size ().raise ();
  4646. if (preserve) {
  4647. self_type temporary (size1, size2);
  4648. // Common elements to preserve
  4649. const size_type size1_min = (std::min) (size1, size1_);
  4650. const size_type size2_min = (std::min) (size2, size2_);
  4651. for (size_type i = 0; i != size1_min; ++i) { // indexing copy over major
  4652. for (size_type j = 0; j != size2_min; ++j) {
  4653. temporary.data_[i][j] = data_[i][j];
  4654. }
  4655. }
  4656. assign_temporary (temporary);
  4657. }
  4658. else {
  4659. size1_ = size1;
  4660. size2_ = size2;
  4661. }
  4662. }
  4663. // Element access
  4664. BOOST_UBLAS_INLINE
  4665. const_reference operator () (size_type i, size_type j) const {
  4666. BOOST_UBLAS_CHECK (i < size1_, bad_index ());
  4667. BOOST_UBLAS_CHECK (j < size2_, bad_index ());
  4668. return data_ [i] [j];
  4669. }
  4670. BOOST_UBLAS_INLINE
  4671. reference at_element (size_type i, size_type j) {
  4672. BOOST_UBLAS_CHECK (i < size1_, bad_index ());
  4673. BOOST_UBLAS_CHECK (j < size2_, bad_index ());
  4674. return data_ [i] [j];
  4675. }
  4676. BOOST_UBLAS_INLINE
  4677. reference operator () (size_type i, size_type j) {
  4678. return at_element (i, j);
  4679. }
  4680. // Element assignment
  4681. BOOST_UBLAS_INLINE
  4682. reference insert_element (size_type i, size_type j, const_reference t) {
  4683. return (at_element (i, j) = t);
  4684. }
  4685. // Zeroing
  4686. BOOST_UBLAS_INLINE
  4687. void clear () {
  4688. for (size_type i = 0; i < size1_; ++ i)
  4689. std::fill (data_ [i], data_ [i] + size2_, value_type/*zero*/());
  4690. }
  4691. // Assignment
  4692. #ifdef BOOST_UBLAS_MOVE_SEMANTICS
  4693. /*! @note "pass by value" the key idea to enable move semantics */
  4694. BOOST_UBLAS_INLINE
  4695. c_matrix &operator = (c_matrix m) {
  4696. assign_temporary(m);
  4697. return *this;
  4698. }
  4699. #else
  4700. BOOST_UBLAS_INLINE
  4701. c_matrix &operator = (const c_matrix &m) {
  4702. size1_ = m.size1_;
  4703. size2_ = m.size2_;
  4704. for (size_type i = 0; i < m.size1_; ++ i)
  4705. std::copy (m.data_ [i], m.data_ [i] + m.size2_, data_ [i]);
  4706. return *this;
  4707. }
  4708. #endif
  4709. template<class C> // Container assignment without temporary
  4710. BOOST_UBLAS_INLINE
  4711. c_matrix &operator = (const matrix_container<C> &m) {
  4712. resize (m ().size1 (), m ().size2 (), false);
  4713. assign (m);
  4714. return *this;
  4715. }
  4716. BOOST_UBLAS_INLINE
  4717. c_matrix &assign_temporary (c_matrix &m) {
  4718. swap (m);
  4719. return *this;
  4720. }
  4721. template<class AE>
  4722. BOOST_UBLAS_INLINE
  4723. c_matrix &operator = (const matrix_expression<AE> &ae) {
  4724. self_type temporary (ae);
  4725. return assign_temporary (temporary);
  4726. }
  4727. template<class AE>
  4728. BOOST_UBLAS_INLINE
  4729. c_matrix &assign (const matrix_expression<AE> &ae) {
  4730. matrix_assign<scalar_assign> (*this, ae);
  4731. return *this;
  4732. }
  4733. template<class AE>
  4734. BOOST_UBLAS_INLINE
  4735. c_matrix& operator += (const matrix_expression<AE> &ae) {
  4736. self_type temporary (*this + ae);
  4737. return assign_temporary (temporary);
  4738. }
  4739. template<class C> // Container assignment without temporary
  4740. BOOST_UBLAS_INLINE
  4741. c_matrix &operator += (const matrix_container<C> &m) {
  4742. plus_assign (m);
  4743. return *this;
  4744. }
  4745. template<class AE>
  4746. BOOST_UBLAS_INLINE
  4747. c_matrix &plus_assign (const matrix_expression<AE> &ae) {
  4748. matrix_assign<scalar_plus_assign> (*this, ae);
  4749. return *this;
  4750. }
  4751. template<class AE>
  4752. BOOST_UBLAS_INLINE
  4753. c_matrix& operator -= (const matrix_expression<AE> &ae) {
  4754. self_type temporary (*this - ae);
  4755. return assign_temporary (temporary);
  4756. }
  4757. template<class C> // Container assignment without temporary
  4758. BOOST_UBLAS_INLINE
  4759. c_matrix &operator -= (const matrix_container<C> &m) {
  4760. minus_assign (m);
  4761. return *this;
  4762. }
  4763. template<class AE>
  4764. BOOST_UBLAS_INLINE
  4765. c_matrix &minus_assign (const matrix_expression<AE> &ae) {
  4766. matrix_assign<scalar_minus_assign> (*this, ae);
  4767. return *this;
  4768. }
  4769. template<class AT>
  4770. BOOST_UBLAS_INLINE
  4771. c_matrix& operator *= (const AT &at) {
  4772. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  4773. return *this;
  4774. }
  4775. template<class AT>
  4776. BOOST_UBLAS_INLINE
  4777. c_matrix& operator /= (const AT &at) {
  4778. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  4779. return *this;
  4780. }
  4781. // Swapping
  4782. BOOST_UBLAS_INLINE
  4783. void swap (c_matrix &m) {
  4784. if (this != &m) {
  4785. BOOST_UBLAS_CHECK (size1_ == m.size1_, bad_size ());
  4786. BOOST_UBLAS_CHECK (size2_ == m.size2_, bad_size ());
  4787. std::swap (size1_, m.size1_);
  4788. std::swap (size2_, m.size2_);
  4789. for (size_type i = 0; i < size1_; ++ i)
  4790. std::swap_ranges (data_ [i], data_ [i] + size2_, m.data_ [i]);
  4791. }
  4792. }
  4793. BOOST_UBLAS_INLINE
  4794. friend void swap (c_matrix &m1, c_matrix &m2) {
  4795. m1.swap (m2);
  4796. }
  4797. // Iterator types
  4798. private:
  4799. // Use pointers for iterator
  4800. typedef const_pointer const_subiterator_type;
  4801. typedef pointer subiterator_type;
  4802. public:
  4803. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4804. typedef indexed_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  4805. typedef indexed_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  4806. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  4807. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  4808. #else
  4809. class const_iterator1;
  4810. class iterator1;
  4811. class const_iterator2;
  4812. class iterator2;
  4813. #endif
  4814. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  4815. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  4816. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  4817. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  4818. // Element lookup
  4819. BOOST_UBLAS_INLINE
  4820. const_iterator1 find1 (int /*rank*/, size_type i, size_type j) const {
  4821. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4822. return const_iterator1 (*this, i, j);
  4823. #else
  4824. return const_iterator1 (*this, &data_ [i] [j]);
  4825. #endif
  4826. }
  4827. BOOST_UBLAS_INLINE
  4828. iterator1 find1 (int /*rank*/, size_type i, size_type j) {
  4829. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4830. return iterator1 (*this, i, j);
  4831. #else
  4832. return iterator1 (*this, &data_ [i] [j]);
  4833. #endif
  4834. }
  4835. BOOST_UBLAS_INLINE
  4836. const_iterator2 find2 (int /*rank*/, size_type i, size_type j) const {
  4837. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4838. return const_iterator2 (*this, i, j);
  4839. #else
  4840. return const_iterator2 (*this, &data_ [i] [j]);
  4841. #endif
  4842. }
  4843. BOOST_UBLAS_INLINE
  4844. iterator2 find2 (int /*rank*/, size_type i, size_type j) {
  4845. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4846. return iterator2 (*this, i, j);
  4847. #else
  4848. return iterator2 (*this, &data_ [i] [j]);
  4849. #endif
  4850. }
  4851. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  4852. class const_iterator1:
  4853. public container_const_reference<c_matrix>,
  4854. public random_access_iterator_base<dense_random_access_iterator_tag,
  4855. const_iterator1, value_type> {
  4856. public:
  4857. typedef typename c_matrix::difference_type difference_type;
  4858. typedef typename c_matrix::value_type value_type;
  4859. typedef typename c_matrix::const_reference reference;
  4860. typedef typename c_matrix::const_pointer pointer;
  4861. typedef const_iterator2 dual_iterator_type;
  4862. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  4863. // Construction and destruction
  4864. BOOST_UBLAS_INLINE
  4865. const_iterator1 ():
  4866. container_const_reference<self_type> (), it_ () {}
  4867. BOOST_UBLAS_INLINE
  4868. const_iterator1 (const self_type &m, const const_subiterator_type &it):
  4869. container_const_reference<self_type> (m), it_ (it) {}
  4870. BOOST_UBLAS_INLINE
  4871. const_iterator1 (const iterator1 &it):
  4872. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  4873. // Arithmetic
  4874. BOOST_UBLAS_INLINE
  4875. const_iterator1 &operator ++ () {
  4876. it_ += M;
  4877. return *this;
  4878. }
  4879. BOOST_UBLAS_INLINE
  4880. const_iterator1 &operator -- () {
  4881. it_ -= M;
  4882. return *this;
  4883. }
  4884. BOOST_UBLAS_INLINE
  4885. const_iterator1 &operator += (difference_type n) {
  4886. it_ += n * M;
  4887. return *this;
  4888. }
  4889. BOOST_UBLAS_INLINE
  4890. const_iterator1 &operator -= (difference_type n) {
  4891. it_ -= n * M;
  4892. return *this;
  4893. }
  4894. BOOST_UBLAS_INLINE
  4895. difference_type operator - (const const_iterator1 &it) const {
  4896. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4897. return (it_ - it.it_) / M;
  4898. }
  4899. // Dereference
  4900. BOOST_UBLAS_INLINE
  4901. const_reference operator * () const {
  4902. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  4903. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  4904. return *it_;
  4905. }
  4906. BOOST_UBLAS_INLINE
  4907. const_reference operator [] (difference_type n) const {
  4908. return *(*this + n);
  4909. }
  4910. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  4911. BOOST_UBLAS_INLINE
  4912. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4913. typename self_type::
  4914. #endif
  4915. const_iterator2 begin () const {
  4916. const self_type &m = (*this) ();
  4917. return m.find2 (1, index1 (), 0);
  4918. }
  4919. BOOST_UBLAS_INLINE
  4920. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4921. typename self_type::
  4922. #endif
  4923. const_iterator2 cbegin () const {
  4924. return begin ();
  4925. }
  4926. BOOST_UBLAS_INLINE
  4927. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4928. typename self_type::
  4929. #endif
  4930. const_iterator2 end () const {
  4931. const self_type &m = (*this) ();
  4932. return m.find2 (1, index1 (), m.size2 ());
  4933. }
  4934. BOOST_UBLAS_INLINE
  4935. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4936. typename self_type::
  4937. #endif
  4938. const_iterator2 cend () const {
  4939. return end ();
  4940. }
  4941. BOOST_UBLAS_INLINE
  4942. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4943. typename self_type::
  4944. #endif
  4945. const_reverse_iterator2 rbegin () const {
  4946. return const_reverse_iterator2 (end ());
  4947. }
  4948. BOOST_UBLAS_INLINE
  4949. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4950. typename self_type::
  4951. #endif
  4952. const_reverse_iterator2 crbegin () const {
  4953. return rbegin ();
  4954. }
  4955. BOOST_UBLAS_INLINE
  4956. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4957. typename self_type::
  4958. #endif
  4959. const_reverse_iterator2 rend () const {
  4960. return const_reverse_iterator2 (begin ());
  4961. }
  4962. BOOST_UBLAS_INLINE
  4963. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  4964. typename self_type::
  4965. #endif
  4966. const_reverse_iterator2 crend () const {
  4967. return rend ();
  4968. }
  4969. #endif
  4970. // Indices
  4971. BOOST_UBLAS_INLINE
  4972. size_type index1 () const {
  4973. const self_type &m = (*this) ();
  4974. return (it_ - m.begin1 ().it_) / M;
  4975. }
  4976. BOOST_UBLAS_INLINE
  4977. size_type index2 () const {
  4978. const self_type &m = (*this) ();
  4979. return (it_ - m.begin1 ().it_) % M;
  4980. }
  4981. // Assignment
  4982. BOOST_UBLAS_INLINE
  4983. const_iterator1 &operator = (const const_iterator1 &it) {
  4984. container_const_reference<self_type>::assign (&it ());
  4985. it_ = it.it_;
  4986. return *this;
  4987. }
  4988. // Comparison
  4989. BOOST_UBLAS_INLINE
  4990. bool operator == (const const_iterator1 &it) const {
  4991. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4992. return it_ == it.it_;
  4993. }
  4994. BOOST_UBLAS_INLINE
  4995. bool operator < (const const_iterator1 &it) const {
  4996. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  4997. return it_ < it.it_;
  4998. }
  4999. private:
  5000. const_subiterator_type it_;
  5001. friend class iterator1;
  5002. };
  5003. #endif
  5004. BOOST_UBLAS_INLINE
  5005. const_iterator1 begin1 () const {
  5006. return find1 (0, 0, 0);
  5007. }
  5008. BOOST_UBLAS_INLINE
  5009. const_iterator1 cbegin1 () const {
  5010. return begin1 ();
  5011. }
  5012. BOOST_UBLAS_INLINE
  5013. const_iterator1 end1 () const {
  5014. return find1 (0, size1_, 0);
  5015. }
  5016. BOOST_UBLAS_INLINE
  5017. const_iterator1 cend1 () const {
  5018. return end1 ();
  5019. }
  5020. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  5021. class iterator1:
  5022. public container_reference<c_matrix>,
  5023. public random_access_iterator_base<dense_random_access_iterator_tag,
  5024. iterator1, value_type> {
  5025. public:
  5026. typedef typename c_matrix::difference_type difference_type;
  5027. typedef typename c_matrix::value_type value_type;
  5028. typedef typename c_matrix::reference reference;
  5029. typedef typename c_matrix::pointer pointer;
  5030. typedef iterator2 dual_iterator_type;
  5031. typedef reverse_iterator2 dual_reverse_iterator_type;
  5032. // Construction and destruction
  5033. BOOST_UBLAS_INLINE
  5034. iterator1 ():
  5035. container_reference<self_type> (), it_ () {}
  5036. BOOST_UBLAS_INLINE
  5037. iterator1 (self_type &m, const subiterator_type &it):
  5038. container_reference<self_type> (m), it_ (it) {}
  5039. // Arithmetic
  5040. BOOST_UBLAS_INLINE
  5041. iterator1 &operator ++ () {
  5042. it_ += M;
  5043. return *this;
  5044. }
  5045. BOOST_UBLAS_INLINE
  5046. iterator1 &operator -- () {
  5047. it_ -= M;
  5048. return *this;
  5049. }
  5050. BOOST_UBLAS_INLINE
  5051. iterator1 &operator += (difference_type n) {
  5052. it_ += n * M;
  5053. return *this;
  5054. }
  5055. BOOST_UBLAS_INLINE
  5056. iterator1 &operator -= (difference_type n) {
  5057. it_ -= n * M;
  5058. return *this;
  5059. }
  5060. BOOST_UBLAS_INLINE
  5061. difference_type operator - (const iterator1 &it) const {
  5062. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5063. return (it_ - it.it_) / M;
  5064. }
  5065. // Dereference
  5066. BOOST_UBLAS_INLINE
  5067. reference operator * () const {
  5068. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  5069. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  5070. return *it_;
  5071. }
  5072. BOOST_UBLAS_INLINE
  5073. reference operator [] (difference_type n) const {
  5074. return *(*this + n);
  5075. }
  5076. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  5077. BOOST_UBLAS_INLINE
  5078. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5079. typename self_type::
  5080. #endif
  5081. iterator2 begin () const {
  5082. self_type &m = (*this) ();
  5083. return m.find2 (1, index1 (), 0);
  5084. }
  5085. BOOST_UBLAS_INLINE
  5086. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5087. typename self_type::
  5088. #endif
  5089. iterator2 end () const {
  5090. self_type &m = (*this) ();
  5091. return m.find2 (1, index1 (), m.size2 ());
  5092. }
  5093. BOOST_UBLAS_INLINE
  5094. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5095. typename self_type::
  5096. #endif
  5097. reverse_iterator2 rbegin () const {
  5098. return reverse_iterator2 (end ());
  5099. }
  5100. BOOST_UBLAS_INLINE
  5101. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5102. typename self_type::
  5103. #endif
  5104. reverse_iterator2 rend () const {
  5105. return reverse_iterator2 (begin ());
  5106. }
  5107. #endif
  5108. // Indices
  5109. BOOST_UBLAS_INLINE
  5110. size_type index1 () const {
  5111. const self_type &m = (*this) ();
  5112. return (it_ - m.begin1 ().it_) / M;
  5113. }
  5114. BOOST_UBLAS_INLINE
  5115. size_type index2 () const {
  5116. const self_type &m = (*this) ();
  5117. return (it_ - m.begin1 ().it_) % M;
  5118. }
  5119. // Assignment
  5120. BOOST_UBLAS_INLINE
  5121. iterator1 &operator = (const iterator1 &it) {
  5122. container_reference<self_type>::assign (&it ());
  5123. it_ = it.it_;
  5124. return *this;
  5125. }
  5126. // Comparison
  5127. BOOST_UBLAS_INLINE
  5128. bool operator == (const iterator1 &it) const {
  5129. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5130. return it_ == it.it_;
  5131. }
  5132. BOOST_UBLAS_INLINE
  5133. bool operator < (const iterator1 &it) const {
  5134. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5135. return it_ < it.it_;
  5136. }
  5137. private:
  5138. subiterator_type it_;
  5139. friend class const_iterator1;
  5140. };
  5141. #endif
  5142. BOOST_UBLAS_INLINE
  5143. iterator1 begin1 () {
  5144. return find1 (0, 0, 0);
  5145. }
  5146. BOOST_UBLAS_INLINE
  5147. iterator1 end1 () {
  5148. return find1 (0, size1_, 0);
  5149. }
  5150. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  5151. class const_iterator2:
  5152. public container_const_reference<c_matrix>,
  5153. public random_access_iterator_base<dense_random_access_iterator_tag,
  5154. const_iterator2, value_type> {
  5155. public:
  5156. typedef typename c_matrix::difference_type difference_type;
  5157. typedef typename c_matrix::value_type value_type;
  5158. typedef typename c_matrix::const_reference reference;
  5159. typedef typename c_matrix::const_reference pointer;
  5160. typedef const_iterator1 dual_iterator_type;
  5161. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  5162. // Construction and destruction
  5163. BOOST_UBLAS_INLINE
  5164. const_iterator2 ():
  5165. container_const_reference<self_type> (), it_ () {}
  5166. BOOST_UBLAS_INLINE
  5167. const_iterator2 (const self_type &m, const const_subiterator_type &it):
  5168. container_const_reference<self_type> (m), it_ (it) {}
  5169. BOOST_UBLAS_INLINE
  5170. const_iterator2 (const iterator2 &it):
  5171. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  5172. // Arithmetic
  5173. BOOST_UBLAS_INLINE
  5174. const_iterator2 &operator ++ () {
  5175. ++ it_;
  5176. return *this;
  5177. }
  5178. BOOST_UBLAS_INLINE
  5179. const_iterator2 &operator -- () {
  5180. -- it_;
  5181. return *this;
  5182. }
  5183. BOOST_UBLAS_INLINE
  5184. const_iterator2 &operator += (difference_type n) {
  5185. it_ += n;
  5186. return *this;
  5187. }
  5188. BOOST_UBLAS_INLINE
  5189. const_iterator2 &operator -= (difference_type n) {
  5190. it_ -= n;
  5191. return *this;
  5192. }
  5193. BOOST_UBLAS_INLINE
  5194. difference_type operator - (const const_iterator2 &it) const {
  5195. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5196. return it_ - it.it_;
  5197. }
  5198. // Dereference
  5199. BOOST_UBLAS_INLINE
  5200. const_reference operator * () const {
  5201. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  5202. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  5203. return *it_;
  5204. }
  5205. BOOST_UBLAS_INLINE
  5206. const_reference operator [] (difference_type n) const {
  5207. return *(*this + n);
  5208. }
  5209. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  5210. BOOST_UBLAS_INLINE
  5211. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5212. typename self_type::
  5213. #endif
  5214. const_iterator1 begin () const {
  5215. const self_type &m = (*this) ();
  5216. return m.find1 (1, 0, index2 ());
  5217. }
  5218. BOOST_UBLAS_INLINE
  5219. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5220. typename self_type::
  5221. #endif
  5222. const_iterator1 cbegin () const {
  5223. return begin ();
  5224. }
  5225. BOOST_UBLAS_INLINE
  5226. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5227. typename self_type::
  5228. #endif
  5229. const_iterator1 end () const {
  5230. const self_type &m = (*this) ();
  5231. return m.find1 (1, m.size1 (), index2 ());
  5232. }
  5233. BOOST_UBLAS_INLINE
  5234. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5235. typename self_type::
  5236. #endif
  5237. const_iterator1 cend () const {
  5238. return end ();
  5239. }
  5240. BOOST_UBLAS_INLINE
  5241. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5242. typename self_type::
  5243. #endif
  5244. const_reverse_iterator1 rbegin () const {
  5245. return const_reverse_iterator1 (end ());
  5246. }
  5247. BOOST_UBLAS_INLINE
  5248. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5249. typename self_type::
  5250. #endif
  5251. const_reverse_iterator1 crbegin () const {
  5252. return rbegin ();
  5253. }
  5254. BOOST_UBLAS_INLINE
  5255. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5256. typename self_type::
  5257. #endif
  5258. const_reverse_iterator1 rend () const {
  5259. return const_reverse_iterator1 (begin ());
  5260. }
  5261. BOOST_UBLAS_INLINE
  5262. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5263. typename self_type::
  5264. #endif
  5265. const_reverse_iterator1 crend () const {
  5266. return rend ();
  5267. }
  5268. #endif
  5269. // Indices
  5270. BOOST_UBLAS_INLINE
  5271. size_type index1 () const {
  5272. const self_type &m = (*this) ();
  5273. return (it_ - m.begin2 ().it_) / M;
  5274. }
  5275. BOOST_UBLAS_INLINE
  5276. size_type index2 () const {
  5277. const self_type &m = (*this) ();
  5278. return (it_ - m.begin2 ().it_) % M;
  5279. }
  5280. // Assignment
  5281. BOOST_UBLAS_INLINE
  5282. const_iterator2 &operator = (const const_iterator2 &it) {
  5283. container_const_reference<self_type>::assign (&it ());
  5284. it_ = it.it_;
  5285. return *this;
  5286. }
  5287. // Comparison
  5288. BOOST_UBLAS_INLINE
  5289. bool operator == (const const_iterator2 &it) const {
  5290. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5291. return it_ == it.it_;
  5292. }
  5293. BOOST_UBLAS_INLINE
  5294. bool operator < (const const_iterator2 &it) const {
  5295. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5296. return it_ < it.it_;
  5297. }
  5298. private:
  5299. const_subiterator_type it_;
  5300. friend class iterator2;
  5301. };
  5302. #endif
  5303. BOOST_UBLAS_INLINE
  5304. const_iterator2 begin2 () const {
  5305. return find2 (0, 0, 0);
  5306. }
  5307. BOOST_UBLAS_INLINE
  5308. const_iterator2 cbegin2 () const {
  5309. return begin2 ();
  5310. }
  5311. BOOST_UBLAS_INLINE
  5312. const_iterator2 end2 () const {
  5313. return find2 (0, 0, size2_);
  5314. }
  5315. BOOST_UBLAS_INLINE
  5316. const_iterator2 cend2 () const {
  5317. return end2 ();
  5318. }
  5319. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  5320. class iterator2:
  5321. public container_reference<c_matrix>,
  5322. public random_access_iterator_base<dense_random_access_iterator_tag,
  5323. iterator2, value_type> {
  5324. public:
  5325. typedef typename c_matrix::difference_type difference_type;
  5326. typedef typename c_matrix::value_type value_type;
  5327. typedef typename c_matrix::reference reference;
  5328. typedef typename c_matrix::pointer pointer;
  5329. typedef iterator1 dual_iterator_type;
  5330. typedef reverse_iterator1 dual_reverse_iterator_type;
  5331. // Construction and destruction
  5332. BOOST_UBLAS_INLINE
  5333. iterator2 ():
  5334. container_reference<self_type> (), it_ () {}
  5335. BOOST_UBLAS_INLINE
  5336. iterator2 (self_type &m, const subiterator_type &it):
  5337. container_reference<self_type> (m), it_ (it) {}
  5338. // Arithmetic
  5339. BOOST_UBLAS_INLINE
  5340. iterator2 &operator ++ () {
  5341. ++ it_;
  5342. return *this;
  5343. }
  5344. BOOST_UBLAS_INLINE
  5345. iterator2 &operator -- () {
  5346. -- it_;
  5347. return *this;
  5348. }
  5349. BOOST_UBLAS_INLINE
  5350. iterator2 &operator += (difference_type n) {
  5351. it_ += n;
  5352. return *this;
  5353. }
  5354. BOOST_UBLAS_INLINE
  5355. iterator2 &operator -= (difference_type n) {
  5356. it_ -= n;
  5357. return *this;
  5358. }
  5359. BOOST_UBLAS_INLINE
  5360. difference_type operator - (const iterator2 &it) const {
  5361. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5362. return it_ - it.it_;
  5363. }
  5364. // Dereference
  5365. BOOST_UBLAS_INLINE
  5366. reference operator * () const {
  5367. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  5368. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  5369. return *it_;
  5370. }
  5371. BOOST_UBLAS_INLINE
  5372. reference operator [] (difference_type n) const {
  5373. return *(*this + n);
  5374. }
  5375. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  5376. BOOST_UBLAS_INLINE
  5377. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5378. typename self_type::
  5379. #endif
  5380. iterator1 begin () const {
  5381. self_type &m = (*this) ();
  5382. return m.find1 (1, 0, index2 ());
  5383. }
  5384. BOOST_UBLAS_INLINE
  5385. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5386. typename self_type::
  5387. #endif
  5388. iterator1 end () const {
  5389. self_type &m = (*this) ();
  5390. return m.find1 (1, m.size1 (), index2 ());
  5391. }
  5392. BOOST_UBLAS_INLINE
  5393. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5394. typename self_type::
  5395. #endif
  5396. reverse_iterator1 rbegin () const {
  5397. return reverse_iterator1 (end ());
  5398. }
  5399. BOOST_UBLAS_INLINE
  5400. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  5401. typename self_type::
  5402. #endif
  5403. reverse_iterator1 rend () const {
  5404. return reverse_iterator1 (begin ());
  5405. }
  5406. #endif
  5407. // Indices
  5408. BOOST_UBLAS_INLINE
  5409. size_type index1 () const {
  5410. const self_type &m = (*this) ();
  5411. return (it_ - m.begin2 ().it_) / M;
  5412. }
  5413. BOOST_UBLAS_INLINE
  5414. size_type index2 () const {
  5415. const self_type &m = (*this) ();
  5416. return (it_ - m.begin2 ().it_) % M;
  5417. }
  5418. // Assignment
  5419. BOOST_UBLAS_INLINE
  5420. iterator2 &operator = (const iterator2 &it) {
  5421. container_reference<self_type>::assign (&it ());
  5422. it_ = it.it_;
  5423. return *this;
  5424. }
  5425. // Comparison
  5426. BOOST_UBLAS_INLINE
  5427. bool operator == (const iterator2 &it) const {
  5428. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5429. return it_ == it.it_;
  5430. }
  5431. BOOST_UBLAS_INLINE
  5432. bool operator < (const iterator2 &it) const {
  5433. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  5434. return it_ < it.it_;
  5435. }
  5436. private:
  5437. subiterator_type it_;
  5438. friend class const_iterator2;
  5439. };
  5440. #endif
  5441. BOOST_UBLAS_INLINE
  5442. iterator2 begin2 () {
  5443. return find2 (0, 0, 0);
  5444. }
  5445. BOOST_UBLAS_INLINE
  5446. iterator2 end2 () {
  5447. return find2 (0, 0, size2_);
  5448. }
  5449. // Reverse iterators
  5450. BOOST_UBLAS_INLINE
  5451. const_reverse_iterator1 rbegin1 () const {
  5452. return const_reverse_iterator1 (end1 ());
  5453. }
  5454. BOOST_UBLAS_INLINE
  5455. const_reverse_iterator1 crbegin1 () const {
  5456. return rbegin1 ();
  5457. }
  5458. BOOST_UBLAS_INLINE
  5459. const_reverse_iterator1 rend1 () const {
  5460. return const_reverse_iterator1 (begin1 ());
  5461. }
  5462. BOOST_UBLAS_INLINE
  5463. const_reverse_iterator1 crend1 () const {
  5464. return rend1 ();
  5465. }
  5466. BOOST_UBLAS_INLINE
  5467. reverse_iterator1 rbegin1 () {
  5468. return reverse_iterator1 (end1 ());
  5469. }
  5470. BOOST_UBLAS_INLINE
  5471. reverse_iterator1 rend1 () {
  5472. return reverse_iterator1 (begin1 ());
  5473. }
  5474. BOOST_UBLAS_INLINE
  5475. const_reverse_iterator2 rbegin2 () const {
  5476. return const_reverse_iterator2 (end2 ());
  5477. }
  5478. BOOST_UBLAS_INLINE
  5479. const_reverse_iterator2 crbegin2 () const {
  5480. return rbegin2 ();
  5481. }
  5482. BOOST_UBLAS_INLINE
  5483. const_reverse_iterator2 rend2 () const {
  5484. return const_reverse_iterator2 (begin2 ());
  5485. }
  5486. BOOST_UBLAS_INLINE
  5487. const_reverse_iterator2 crend2 () const {
  5488. return rend2 ();
  5489. }
  5490. BOOST_UBLAS_INLINE
  5491. reverse_iterator2 rbegin2 () {
  5492. return reverse_iterator2 (end2 ());
  5493. }
  5494. BOOST_UBLAS_INLINE
  5495. reverse_iterator2 rend2 () {
  5496. return reverse_iterator2 (begin2 ());
  5497. }
  5498. // Serialization
  5499. template<class Archive>
  5500. void serialize(Archive & ar, const unsigned int /* file_version */){
  5501. // we need to copy to a collection_size_type to get a portable
  5502. // and efficient serialization
  5503. serialization::collection_size_type s1 (size1_);
  5504. serialization::collection_size_type s2 (size2_);
  5505. // serialize the sizes
  5506. ar & serialization::make_nvp("size1",s1)
  5507. & serialization::make_nvp("size2",s2);
  5508. // copy the values back if loading
  5509. if (Archive::is_loading::value) {
  5510. size1_ = s1;
  5511. size2_ = s2;
  5512. }
  5513. // could probably use make_array( &(data[0][0]), N*M )
  5514. ar & serialization::make_array(data_, N);
  5515. }
  5516. private:
  5517. size_type size1_;
  5518. size_type size2_;
  5519. value_type data_ [N] [M];
  5520. };
  5521. }}}
  5522. #endif