matrix.hpp 211 KB

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