matrix.hpp 211 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013
  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 boost::allocator_size_type<ALLOC>::type size_type;
  3203. typedef typename boost::allocator_difference_type<ALLOC>::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 boost::allocator_size_type<ALLOC>::type size_type;
  3637. typedef typename boost::allocator_difference_type<ALLOC>::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