matrix.hpp 156 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390
  1. //
  2. // Copyright (c) 2000-2010
  3. // Joerg Walter, Mathias Koch, Gunter Winkler, David Bellot
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // The authors gratefully acknowledge the support of
  10. // GeNeSys mbH & Co. KG in producing this work.
  11. //
  12. #ifndef _BOOST_UBLAS_MATRIX_
  13. #define _BOOST_UBLAS_MATRIX_
  14. #include <boost/numeric/ublas/vector.hpp>
  15. #include <boost/numeric/ublas/matrix_expression.hpp>
  16. #include <boost/numeric/ublas/detail/matrix_assign.hpp>
  17. #include <boost/serialization/collection_size_type.hpp>
  18. #include <boost/serialization/array.hpp>
  19. #include <boost/serialization/nvp.hpp>
  20. // Iterators based on ideas of Jeremy Siek
  21. namespace boost { namespace numeric {
  22. /** \brief main namespace of uBLAS.
  23. *
  24. * Use this namespace for all operations with uBLAS. It can also be abbreviated with
  25. * \code namespace ublas = boost::numeric::ublas; \endcode
  26. *
  27. * A common practice is to bring this namespace into the current scope with
  28. * \code using namespace boost::numeric::ublas; \endcode.
  29. *
  30. * However, be warned that using the ublas namespace and the std::vector at the same time can lead to the compiler to confusion.
  31. * The solution is simply to prefix each ublas vector like \c boost::numeric::ublas::vector<T>. If you think it's too long to
  32. * write, you can define a new namespace like \c namespace ublas = boost::numeric::ublas and then just declare your vectors
  33. * with \c ublas::vector<T>. STL vectors will be declared as vector<T>. No need to prefix with \c std::
  34. */
  35. namespace ublas {
  36. namespace detail {
  37. using namespace boost::numeric::ublas;
  38. // Matrix resizing algorithm
  39. template <class L, class M>
  40. BOOST_UBLAS_INLINE
  41. void matrix_resize_preserve (M& m, M& temporary) {
  42. typedef L layout_type;
  43. typedef typename M::size_type size_type;
  44. const size_type msize1 (m.size1 ()); // original size
  45. const size_type msize2 (m.size2 ());
  46. const size_type size1 (temporary.size1 ()); // new size is specified by temporary
  47. const size_type size2 (temporary.size2 ());
  48. // Common elements to preserve
  49. const size_type size1_min = (std::min) (size1, msize1);
  50. const size_type size2_min = (std::min) (size2, msize2);
  51. // Order for major and minor sizes
  52. const size_type major_size = layout_type::size_M (size1_min, size2_min);
  53. const size_type minor_size = layout_type::size_m (size1_min, size2_min);
  54. // Indexing copy over major
  55. for (size_type major = 0; major != major_size; ++major) {
  56. for (size_type minor = 0; minor != minor_size; ++minor) {
  57. // find indexes - use invertability of element_ functions
  58. const size_type i1 = layout_type::index_M(major, minor);
  59. const size_type i2 = layout_type::index_m(major, minor);
  60. temporary.data () [layout_type::element (i1, size1, i2, size2)] =
  61. m.data() [layout_type::element (i1, msize1, i2, msize2)];
  62. }
  63. }
  64. m.assign_temporary (temporary);
  65. }
  66. }
  67. /** \brief A dense matrix of values of type \c T.
  68. *
  69. * 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
  70. * 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
  71. * the container for column major orientation. In a dense matrix all elements are represented in memory in a
  72. * contiguous chunk of memory by definition.
  73. *
  74. * Orientation and storage can also be specified, otherwise a \c row_major and \c unbounded_array are used. It is \b not
  75. * required by the storage to initialize elements of the matrix.
  76. *
  77. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  78. * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major
  79. * \tparam A the type of Storage array. Default is \c unbounded_array
  80. */
  81. template<class T, class L, class A>
  82. class matrix:
  83. public matrix_container<matrix<T, L, A> > {
  84. typedef T *pointer;
  85. typedef L layout_type;
  86. typedef matrix<T, L, A> self_type;
  87. public:
  88. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  89. using matrix_container<self_type>::operator ();
  90. #endif
  91. typedef typename A::size_type size_type;
  92. typedef typename A::difference_type difference_type;
  93. typedef T value_type;
  94. typedef const T &const_reference;
  95. typedef T &reference;
  96. typedef A array_type;
  97. typedef const matrix_reference<const self_type> const_closure_type;
  98. typedef matrix_reference<self_type> closure_type;
  99. typedef vector<T, A> vector_temporary_type;
  100. typedef self_type matrix_temporary_type;
  101. typedef dense_tag storage_category;
  102. // This could be better for performance,
  103. // typedef typename unknown_orientation_tag orientation_category;
  104. // but others depend on the orientation information...
  105. typedef typename L::orientation_category orientation_category;
  106. // Construction and destruction
  107. /// Default dense matrix constructor. Make a dense matrix of size (0,0)
  108. BOOST_UBLAS_INLINE
  109. matrix ():
  110. matrix_container<self_type> (),
  111. size1_ (0), size2_ (0), data_ () {}
  112. /** Dense matrix constructor with defined size
  113. * \param size1 number of rows
  114. * \param size2 number of columns
  115. */
  116. BOOST_UBLAS_INLINE
  117. matrix (size_type size1, size_type size2):
  118. matrix_container<self_type> (),
  119. size1_ (size1), size2_ (size2), data_ (layout_type::storage_size (size1, size2)) {
  120. }
  121. /** Dense matrix constructor with defined size a initial value for all the matrix elements
  122. * \param size1 number of rows
  123. * \param size2 number of columns
  124. * \param init initial value assigned to all elements
  125. */
  126. matrix (size_type size1, size_type size2, const value_type &init):
  127. matrix_container<self_type> (),
  128. size1_ (size1), size2_ (size2), data_ (layout_type::storage_size (size1, size2), init) {
  129. }
  130. /** Dense matrix constructor with defined size and an initial data array
  131. * \param size1 number of rows
  132. * \param size2 number of columns
  133. * \param data array to copy into the matrix. Must have the same dimension as the matrix
  134. */
  135. BOOST_UBLAS_INLINE
  136. matrix (size_type size1, size_type size2, const array_type &data):
  137. matrix_container<self_type> (),
  138. size1_ (size1), size2_ (size2), data_ (data) {}
  139. /** Copy-constructor of a dense matrix
  140. * \param m is a dense matrix
  141. */
  142. BOOST_UBLAS_INLINE
  143. matrix (const matrix &m):
  144. matrix_container<self_type> (),
  145. size1_ (m.size1_), size2_ (m.size2_), data_ (m.data_) {}
  146. /** Copy-constructor of a dense matrix from a matrix expression
  147. * \param ae is a matrix expression
  148. */
  149. template<class AE>
  150. BOOST_UBLAS_INLINE
  151. matrix (const matrix_expression<AE> &ae):
  152. matrix_container<self_type> (),
  153. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), data_ (layout_type::storage_size (size1_, size2_)) {
  154. matrix_assign<scalar_assign> (*this, ae);
  155. }
  156. // Accessors
  157. /** Return the number of rows of the matrix
  158. * You can also use the free size<>() function in operation/size.hpp as size<1>(m) where m is a matrix
  159. */
  160. BOOST_UBLAS_INLINE
  161. size_type size1 () const {
  162. return size1_;
  163. }
  164. /** Return the number of colums of the matrix
  165. * You can also use the free size<>() function in operation/size.hpp as size<2>(m) where m is a matrix
  166. */
  167. BOOST_UBLAS_INLINE
  168. size_type size2 () const {
  169. return size2_;
  170. }
  171. // Storage accessors
  172. /** Return a constant reference to the internal storage of a dense matrix, i.e. the raw data
  173. * It's type depends on the type used by the matrix to store its data
  174. */
  175. BOOST_UBLAS_INLINE
  176. const array_type &data () const {
  177. return data_;
  178. }
  179. /** Return a reference to the internal storage of a dense matrix, i.e. the raw data
  180. * It's type depends on the type used by the matrix to store its data
  181. */
  182. BOOST_UBLAS_INLINE
  183. array_type &data () {
  184. return data_;
  185. }
  186. // Resizing
  187. /** Resize a matrix to new dimensions
  188. * If data are preserved, then if the size if bigger at least on one dimension, extra values are filled with zeros.
  189. * If data are not preserved, then nothing has to be assumed regarding the content of the matrix after resizing.
  190. * \param size1 the new number of rows
  191. * \param size2 the new number of colums
  192. * \param preserve a boolean to say if one wants the data to be preserved during the resizing. Default is true.
  193. */
  194. BOOST_UBLAS_INLINE
  195. void resize (size_type size1, size_type size2, bool preserve = true) {
  196. if (preserve) {
  197. self_type temporary (size1, size2);
  198. detail::matrix_resize_preserve<layout_type> (*this, temporary);
  199. }
  200. else {
  201. data ().resize (layout_type::storage_size (size1, size2));
  202. size1_ = size1;
  203. size2_ = size2;
  204. }
  205. }
  206. // Element access
  207. /** Access a matrix element. Here we return a const reference
  208. * \param i the first coordinate of the element. By default it's the row
  209. * \param j the second coordinate of the element. By default it's the column
  210. * \return a const reference to the element
  211. */
  212. BOOST_UBLAS_INLINE
  213. const_reference operator () (size_type i, size_type j) const {
  214. return data () [layout_type::element (i, size1_, j, size2_)];
  215. }
  216. /** Access a matrix element. Here we return a reference
  217. * \param i the first coordinate of the element. By default it's the row
  218. * \param j the second coordinate of the element. By default it's the column
  219. * \return a reference to the element
  220. */
  221. BOOST_UBLAS_INLINE
  222. reference at_element (size_type i, size_type j) {
  223. return data () [layout_type::element (i, size1_, j, size2_)];
  224. }
  225. /** Access a matrix element. Here we return a reference
  226. * \param i the first coordinate of the element. By default it's the row
  227. * \param j the second coordinate of the element. By default it's the column
  228. * \return a reference to the element
  229. */
  230. BOOST_UBLAS_INLINE
  231. reference operator () (size_type i, size_type j) {
  232. return at_element (i, j);
  233. }
  234. // Element assignment
  235. /** Change the value of a matrix element. Return back a reference to it
  236. * \param i the first coordinate of the element. By default it's the row
  237. * \param j the second coordinate of the element. By default it's the column
  238. * \param t the new value of the element
  239. * \return a reference to the newly changed element
  240. */
  241. BOOST_UBLAS_INLINE
  242. reference insert_element (size_type i, size_type j, const_reference t) {
  243. return (at_element (i, j) = t);
  244. }
  245. /** Erase the element
  246. * For most types (int, double, etc...) it means setting 0 (zero) the element at zero in fact.
  247. * For user-defined types, it could be another value if you decided it. Your type in that case must
  248. * contain a default null value.
  249. * \param i the first coordinate of the element. By default it's the row
  250. * \param j the second coordinate of the element. By default it's the column
  251. */
  252. void erase_element (size_type i, size_type j) {
  253. at_element (i, j) = value_type/*zero*/();
  254. }
  255. // Zeroing
  256. /** Erase all elements in the matrix
  257. * For most types (int, double, etc...) it means writing 0 (zero) everywhere.
  258. * For user-defined types, it could be another value if you decided it. Your type in that case must
  259. * contain a default null value.
  260. */
  261. BOOST_UBLAS_INLINE
  262. void clear () {
  263. std::fill (data ().begin (), data ().end (), value_type/*zero*/());
  264. }
  265. // Assignment
  266. #ifdef BOOST_UBLAS_MOVE_SEMANTICS
  267. /*! @note "pass by value" the key idea to enable move semantics */
  268. BOOST_UBLAS_INLINE
  269. matrix &operator = (matrix m) {
  270. assign_temporary(m);
  271. return *this;
  272. }
  273. #else
  274. BOOST_UBLAS_INLINE
  275. matrix &operator = (const matrix &m) {
  276. size1_ = m.size1_;
  277. size2_ = m.size2_;
  278. data () = m.data ();
  279. return *this;
  280. }
  281. #endif
  282. template<class C> // Container assignment without temporary
  283. BOOST_UBLAS_INLINE
  284. matrix &operator = (const matrix_container<C> &m) {
  285. resize (m ().size1 (), m ().size2 (), false);
  286. assign (m);
  287. return *this;
  288. }
  289. BOOST_UBLAS_INLINE
  290. matrix &assign_temporary (matrix &m) {
  291. swap (m);
  292. return *this;
  293. }
  294. template<class AE>
  295. BOOST_UBLAS_INLINE
  296. matrix &operator = (const matrix_expression<AE> &ae) {
  297. self_type temporary (ae);
  298. return assign_temporary (temporary);
  299. }
  300. template<class AE>
  301. BOOST_UBLAS_INLINE
  302. matrix &assign (const matrix_expression<AE> &ae) {
  303. matrix_assign<scalar_assign> (*this, ae);
  304. return *this;
  305. }
  306. template<class AE>
  307. BOOST_UBLAS_INLINE
  308. matrix& operator += (const matrix_expression<AE> &ae) {
  309. self_type temporary (*this + ae);
  310. return assign_temporary (temporary);
  311. }
  312. template<class C> // Container assignment without temporary
  313. BOOST_UBLAS_INLINE
  314. matrix &operator += (const matrix_container<C> &m) {
  315. plus_assign (m);
  316. return *this;
  317. }
  318. template<class AE>
  319. BOOST_UBLAS_INLINE
  320. matrix &plus_assign (const matrix_expression<AE> &ae) {
  321. matrix_assign<scalar_plus_assign> (*this, ae);
  322. return *this;
  323. }
  324. template<class AE>
  325. BOOST_UBLAS_INLINE
  326. matrix& operator -= (const matrix_expression<AE> &ae) {
  327. self_type temporary (*this - ae);
  328. return assign_temporary (temporary);
  329. }
  330. template<class C> // Container assignment without temporary
  331. BOOST_UBLAS_INLINE
  332. matrix &operator -= (const matrix_container<C> &m) {
  333. minus_assign (m);
  334. return *this;
  335. }
  336. template<class AE>
  337. BOOST_UBLAS_INLINE
  338. matrix &minus_assign (const matrix_expression<AE> &ae) {
  339. matrix_assign<scalar_minus_assign> (*this, ae);
  340. return *this;
  341. }
  342. template<class AT>
  343. BOOST_UBLAS_INLINE
  344. matrix& operator *= (const AT &at) {
  345. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  346. return *this;
  347. }
  348. template<class AT>
  349. BOOST_UBLAS_INLINE
  350. matrix& operator /= (const AT &at) {
  351. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  352. return *this;
  353. }
  354. // Swapping
  355. BOOST_UBLAS_INLINE
  356. void swap (matrix &m) {
  357. if (this != &m) {
  358. std::swap (size1_, m.size1_);
  359. std::swap (size2_, m.size2_);
  360. data ().swap (m.data ());
  361. }
  362. }
  363. BOOST_UBLAS_INLINE
  364. friend void swap (matrix &m1, matrix &m2) {
  365. m1.swap (m2);
  366. }
  367. // Iterator types
  368. private:
  369. // Use the storage array iterator
  370. typedef typename A::const_iterator const_subiterator_type;
  371. typedef typename A::iterator subiterator_type;
  372. public:
  373. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  374. typedef indexed_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  375. typedef indexed_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  376. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  377. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  378. #else
  379. class const_iterator1;
  380. class iterator1;
  381. class const_iterator2;
  382. class iterator2;
  383. #endif
  384. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  385. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  386. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  387. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  388. // Element lookup
  389. BOOST_UBLAS_INLINE
  390. const_iterator1 find1 (int /* rank */, size_type i, size_type j) const {
  391. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  392. return const_iterator1 (*this, i, j);
  393. #else
  394. return const_iterator1 (*this, data ().begin () + layout_type::address (i, size1_, j, size2_));
  395. #endif
  396. }
  397. BOOST_UBLAS_INLINE
  398. iterator1 find1 (int /* rank */, size_type i, size_type j) {
  399. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  400. return iterator1 (*this, i, j);
  401. #else
  402. return iterator1 (*this, data ().begin () + layout_type::address (i, size1_, j, size2_));
  403. #endif
  404. }
  405. BOOST_UBLAS_INLINE
  406. const_iterator2 find2 (int /* rank */, size_type i, size_type j) const {
  407. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  408. return const_iterator2 (*this, i, j);
  409. #else
  410. return const_iterator2 (*this, data ().begin () + layout_type::address (i, size1_, j, size2_));
  411. #endif
  412. }
  413. BOOST_UBLAS_INLINE
  414. iterator2 find2 (int /* rank */, size_type i, size_type j) {
  415. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  416. return iterator2 (*this, i, j);
  417. #else
  418. return iterator2 (*this, data ().begin () + layout_type::address (i, size1_, j, size2_));
  419. #endif
  420. }
  421. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  422. class const_iterator1:
  423. public container_const_reference<matrix>,
  424. public random_access_iterator_base<dense_random_access_iterator_tag,
  425. const_iterator1, value_type> {
  426. public:
  427. typedef typename matrix::value_type value_type;
  428. typedef typename matrix::difference_type difference_type;
  429. typedef typename matrix::const_reference reference;
  430. typedef const typename matrix::pointer pointer;
  431. typedef const_iterator2 dual_iterator_type;
  432. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  433. // Construction and destruction
  434. BOOST_UBLAS_INLINE
  435. const_iterator1 ():
  436. container_const_reference<self_type> (), it_ () {}
  437. BOOST_UBLAS_INLINE
  438. const_iterator1 (const self_type &m, const const_subiterator_type &it):
  439. container_const_reference<self_type> (m), it_ (it) {}
  440. BOOST_UBLAS_INLINE
  441. const_iterator1 (const iterator1 &it):
  442. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  443. // Arithmetic
  444. BOOST_UBLAS_INLINE
  445. const_iterator1 &operator ++ () {
  446. layout_type::increment_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  447. return *this;
  448. }
  449. BOOST_UBLAS_INLINE
  450. const_iterator1 &operator -- () {
  451. layout_type::decrement_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  452. return *this;
  453. }
  454. BOOST_UBLAS_INLINE
  455. const_iterator1 &operator += (difference_type n) {
  456. layout_type::increment_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  457. return *this;
  458. }
  459. BOOST_UBLAS_INLINE
  460. const_iterator1 &operator -= (difference_type n) {
  461. layout_type::decrement_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  462. return *this;
  463. }
  464. BOOST_UBLAS_INLINE
  465. difference_type operator - (const const_iterator1 &it) const {
  466. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  467. return layout_type::distance_i (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  468. }
  469. // Dereference
  470. BOOST_UBLAS_INLINE
  471. const_reference operator * () const {
  472. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  473. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  474. return *it_;
  475. }
  476. BOOST_UBLAS_INLINE
  477. const_reference operator [] (difference_type n) const {
  478. return *(*this + n);
  479. }
  480. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  481. BOOST_UBLAS_INLINE
  482. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  483. typename self_type::
  484. #endif
  485. const_iterator2 begin () const {
  486. const self_type &m = (*this) ();
  487. return m.find2 (1, index1 (), 0);
  488. }
  489. BOOST_UBLAS_INLINE
  490. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  491. typename self_type::
  492. #endif
  493. const_iterator2 end () const {
  494. const self_type &m = (*this) ();
  495. return m.find2 (1, index1 (), m.size2 ());
  496. }
  497. BOOST_UBLAS_INLINE
  498. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  499. typename self_type::
  500. #endif
  501. const_reverse_iterator2 rbegin () const {
  502. return const_reverse_iterator2 (end ());
  503. }
  504. BOOST_UBLAS_INLINE
  505. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  506. typename self_type::
  507. #endif
  508. const_reverse_iterator2 rend () const {
  509. return const_reverse_iterator2 (begin ());
  510. }
  511. #endif
  512. // Indices
  513. BOOST_UBLAS_INLINE
  514. size_type index1 () const {
  515. const self_type &m = (*this) ();
  516. return layout_type::index_i (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  517. }
  518. BOOST_UBLAS_INLINE
  519. size_type index2 () const {
  520. const self_type &m = (*this) ();
  521. return layout_type::index_j (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  522. }
  523. // Assignment
  524. BOOST_UBLAS_INLINE
  525. const_iterator1 &operator = (const const_iterator1 &it) {
  526. container_const_reference<self_type>::assign (&it ());
  527. it_ = it.it_;
  528. return *this;
  529. }
  530. // Comparison
  531. BOOST_UBLAS_INLINE
  532. bool operator == (const const_iterator1 &it) const {
  533. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  534. return it_ == it.it_;
  535. }
  536. BOOST_UBLAS_INLINE
  537. bool operator < (const const_iterator1 &it) const {
  538. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  539. return it_ < it.it_;
  540. }
  541. private:
  542. const_subiterator_type it_;
  543. friend class iterator1;
  544. };
  545. #endif
  546. BOOST_UBLAS_INLINE
  547. const_iterator1 begin1 () const {
  548. return find1 (0, 0, 0);
  549. }
  550. BOOST_UBLAS_INLINE
  551. const_iterator1 end1 () const {
  552. return find1 (0, size1_, 0);
  553. }
  554. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  555. class iterator1:
  556. public container_reference<matrix>,
  557. public random_access_iterator_base<dense_random_access_iterator_tag,
  558. iterator1, value_type> {
  559. public:
  560. typedef typename matrix::value_type value_type;
  561. typedef typename matrix::difference_type difference_type;
  562. typedef typename matrix::reference reference;
  563. typedef typename matrix::pointer pointer;
  564. typedef iterator2 dual_iterator_type;
  565. typedef reverse_iterator2 dual_reverse_iterator_type;
  566. // Construction and destruction
  567. BOOST_UBLAS_INLINE
  568. iterator1 ():
  569. container_reference<self_type> (), it_ () {}
  570. BOOST_UBLAS_INLINE
  571. iterator1 (self_type &m, const subiterator_type &it):
  572. container_reference<self_type> (m), it_ (it) {}
  573. // Arithmetic
  574. BOOST_UBLAS_INLINE
  575. iterator1 &operator ++ () {
  576. layout_type::increment_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  577. return *this;
  578. }
  579. BOOST_UBLAS_INLINE
  580. iterator1 &operator -- () {
  581. layout_type::decrement_i (it_, (*this) ().size1 (), (*this) ().size2 ());
  582. return *this;
  583. }
  584. BOOST_UBLAS_INLINE
  585. iterator1 &operator += (difference_type n) {
  586. layout_type::increment_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  587. return *this;
  588. }
  589. BOOST_UBLAS_INLINE
  590. iterator1 &operator -= (difference_type n) {
  591. layout_type::decrement_i (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  592. return *this;
  593. }
  594. BOOST_UBLAS_INLINE
  595. difference_type operator - (const iterator1 &it) const {
  596. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  597. return layout_type::distance_i (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  598. }
  599. // Dereference
  600. BOOST_UBLAS_INLINE
  601. reference operator * () const {
  602. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  603. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  604. return *it_;
  605. }
  606. BOOST_UBLAS_INLINE
  607. reference operator [] (difference_type n) const {
  608. return *(*this + n);
  609. }
  610. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  611. BOOST_UBLAS_INLINE
  612. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  613. typename self_type::
  614. #endif
  615. iterator2 begin () const {
  616. self_type &m = (*this) ();
  617. return m.find2 (1, index1 (), 0);
  618. }
  619. BOOST_UBLAS_INLINE
  620. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  621. typename self_type::
  622. #endif
  623. iterator2 end () const {
  624. self_type &m = (*this) ();
  625. return m.find2 (1, index1 (), m.size2 ());
  626. }
  627. BOOST_UBLAS_INLINE
  628. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  629. typename self_type::
  630. #endif
  631. reverse_iterator2 rbegin () const {
  632. return reverse_iterator2 (end ());
  633. }
  634. BOOST_UBLAS_INLINE
  635. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  636. typename self_type::
  637. #endif
  638. reverse_iterator2 rend () const {
  639. return reverse_iterator2 (begin ());
  640. }
  641. #endif
  642. // Indices
  643. BOOST_UBLAS_INLINE
  644. size_type index1 () const {
  645. self_type &m = (*this) ();
  646. return layout_type::index_i (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  647. }
  648. BOOST_UBLAS_INLINE
  649. size_type index2 () const {
  650. self_type &m = (*this) ();
  651. return layout_type::index_j (it_ - m.begin1 ().it_, m.size1 (), m.size2 ());
  652. }
  653. // Assignment
  654. BOOST_UBLAS_INLINE
  655. iterator1 &operator = (const iterator1 &it) {
  656. container_reference<self_type>::assign (&it ());
  657. it_ = it.it_;
  658. return *this;
  659. }
  660. // Comparison
  661. BOOST_UBLAS_INLINE
  662. bool operator == (const iterator1 &it) const {
  663. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  664. return it_ == it.it_;
  665. }
  666. BOOST_UBLAS_INLINE
  667. bool operator < (const iterator1 &it) const {
  668. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  669. return it_ < it.it_;
  670. }
  671. private:
  672. subiterator_type it_;
  673. friend class const_iterator1;
  674. };
  675. #endif
  676. BOOST_UBLAS_INLINE
  677. iterator1 begin1 () {
  678. return find1 (0, 0, 0);
  679. }
  680. BOOST_UBLAS_INLINE
  681. iterator1 end1 () {
  682. return find1 (0, size1_, 0);
  683. }
  684. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  685. class const_iterator2:
  686. public container_const_reference<matrix>,
  687. public random_access_iterator_base<dense_random_access_iterator_tag,
  688. const_iterator2, value_type> {
  689. public:
  690. typedef typename matrix::value_type value_type;
  691. typedef typename matrix::difference_type difference_type;
  692. typedef typename matrix::const_reference reference;
  693. typedef const typename matrix::pointer pointer;
  694. typedef const_iterator1 dual_iterator_type;
  695. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  696. // Construction and destruction
  697. BOOST_UBLAS_INLINE
  698. const_iterator2 ():
  699. container_const_reference<self_type> (), it_ () {}
  700. BOOST_UBLAS_INLINE
  701. const_iterator2 (const self_type &m, const const_subiterator_type &it):
  702. container_const_reference<self_type> (m), it_ (it) {}
  703. BOOST_UBLAS_INLINE
  704. const_iterator2 (const iterator2 &it):
  705. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  706. // Arithmetic
  707. BOOST_UBLAS_INLINE
  708. const_iterator2 &operator ++ () {
  709. layout_type::increment_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  710. return *this;
  711. }
  712. BOOST_UBLAS_INLINE
  713. const_iterator2 &operator -- () {
  714. layout_type::decrement_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  715. return *this;
  716. }
  717. BOOST_UBLAS_INLINE
  718. const_iterator2 &operator += (difference_type n) {
  719. layout_type::increment_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  720. return *this;
  721. }
  722. BOOST_UBLAS_INLINE
  723. const_iterator2 &operator -= (difference_type n) {
  724. layout_type::decrement_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  725. return *this;
  726. }
  727. BOOST_UBLAS_INLINE
  728. difference_type operator - (const const_iterator2 &it) const {
  729. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  730. return layout_type::distance_j (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  731. }
  732. // Dereference
  733. BOOST_UBLAS_INLINE
  734. const_reference operator * () const {
  735. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  736. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  737. return *it_;
  738. }
  739. BOOST_UBLAS_INLINE
  740. const_reference operator [] (difference_type n) const {
  741. return *(*this + n);
  742. }
  743. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  744. BOOST_UBLAS_INLINE
  745. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  746. typename self_type::
  747. #endif
  748. const_iterator1 begin () const {
  749. const self_type &m = (*this) ();
  750. return m.find1 (1, 0, index2 ());
  751. }
  752. BOOST_UBLAS_INLINE
  753. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  754. typename self_type::
  755. #endif
  756. const_iterator1 end () const {
  757. const self_type &m = (*this) ();
  758. return m.find1 (1, m.size1 (), index2 ());
  759. }
  760. BOOST_UBLAS_INLINE
  761. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  762. typename self_type::
  763. #endif
  764. const_reverse_iterator1 rbegin () const {
  765. return const_reverse_iterator1 (end ());
  766. }
  767. BOOST_UBLAS_INLINE
  768. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  769. typename self_type::
  770. #endif
  771. const_reverse_iterator1 rend () const {
  772. return const_reverse_iterator1 (begin ());
  773. }
  774. #endif
  775. // Indices
  776. BOOST_UBLAS_INLINE
  777. size_type index1 () const {
  778. const self_type &m = (*this) ();
  779. return layout_type::index_i (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  780. }
  781. BOOST_UBLAS_INLINE
  782. size_type index2 () const {
  783. const self_type &m = (*this) ();
  784. return layout_type::index_j (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  785. }
  786. // Assignment
  787. BOOST_UBLAS_INLINE
  788. const_iterator2 &operator = (const const_iterator2 &it) {
  789. container_const_reference<self_type>::assign (&it ());
  790. it_ = it.it_;
  791. return *this;
  792. }
  793. // Comparison
  794. BOOST_UBLAS_INLINE
  795. bool operator == (const const_iterator2 &it) const {
  796. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  797. return it_ == it.it_;
  798. }
  799. BOOST_UBLAS_INLINE
  800. bool operator < (const const_iterator2 &it) const {
  801. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  802. return it_ < it.it_;
  803. }
  804. private:
  805. const_subiterator_type it_;
  806. friend class iterator2;
  807. };
  808. #endif
  809. BOOST_UBLAS_INLINE
  810. const_iterator2 begin2 () const {
  811. return find2 (0, 0, 0);
  812. }
  813. BOOST_UBLAS_INLINE
  814. const_iterator2 end2 () const {
  815. return find2 (0, 0, size2_);
  816. }
  817. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  818. class iterator2:
  819. public container_reference<matrix>,
  820. public random_access_iterator_base<dense_random_access_iterator_tag,
  821. iterator2, value_type> {
  822. public:
  823. typedef typename matrix::value_type value_type;
  824. typedef typename matrix::difference_type difference_type;
  825. typedef typename matrix::reference reference;
  826. typedef typename matrix::pointer pointer;
  827. typedef iterator1 dual_iterator_type;
  828. typedef reverse_iterator1 dual_reverse_iterator_type;
  829. // Construction and destruction
  830. BOOST_UBLAS_INLINE
  831. iterator2 ():
  832. container_reference<self_type> (), it_ () {}
  833. BOOST_UBLAS_INLINE
  834. iterator2 (self_type &m, const subiterator_type &it):
  835. container_reference<self_type> (m), it_ (it) {}
  836. // Arithmetic
  837. BOOST_UBLAS_INLINE
  838. iterator2 &operator ++ () {
  839. layout_type::increment_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  840. return *this;
  841. }
  842. BOOST_UBLAS_INLINE
  843. iterator2 &operator -- () {
  844. layout_type::decrement_j (it_, (*this) ().size1 (), (*this) ().size2 ());
  845. return *this;
  846. }
  847. BOOST_UBLAS_INLINE
  848. iterator2 &operator += (difference_type n) {
  849. layout_type::increment_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  850. return *this;
  851. }
  852. BOOST_UBLAS_INLINE
  853. iterator2 &operator -= (difference_type n) {
  854. layout_type::decrement_j (it_, n, (*this) ().size1 (), (*this) ().size2 ());
  855. return *this;
  856. }
  857. BOOST_UBLAS_INLINE
  858. difference_type operator - (const iterator2 &it) const {
  859. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  860. return layout_type::distance_j (it_ - it.it_, (*this) ().size1 (), (*this) ().size2 ());
  861. }
  862. // Dereference
  863. BOOST_UBLAS_INLINE
  864. reference operator * () const {
  865. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  866. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  867. return *it_;
  868. }
  869. BOOST_UBLAS_INLINE
  870. reference operator [] (difference_type n) const {
  871. return *(*this + n);
  872. }
  873. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  874. BOOST_UBLAS_INLINE
  875. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  876. typename self_type::
  877. #endif
  878. iterator1 begin () const {
  879. self_type &m = (*this) ();
  880. return m.find1 (1, 0, index2 ());
  881. }
  882. BOOST_UBLAS_INLINE
  883. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  884. typename self_type::
  885. #endif
  886. iterator1 end () const {
  887. self_type &m = (*this) ();
  888. return m.find1 (1, m.size1 (), index2 ());
  889. }
  890. BOOST_UBLAS_INLINE
  891. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  892. typename self_type::
  893. #endif
  894. reverse_iterator1 rbegin () const {
  895. return reverse_iterator1 (end ());
  896. }
  897. BOOST_UBLAS_INLINE
  898. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  899. typename self_type::
  900. #endif
  901. reverse_iterator1 rend () const {
  902. return reverse_iterator1 (begin ());
  903. }
  904. #endif
  905. // Indices
  906. BOOST_UBLAS_INLINE
  907. size_type index1 () const {
  908. self_type &m = (*this) ();
  909. return layout_type::index_i (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  910. }
  911. BOOST_UBLAS_INLINE
  912. size_type index2 () const {
  913. self_type &m = (*this) ();
  914. return layout_type::index_j (it_ - m.begin2 ().it_, m.size1 (), m.size2 ());
  915. }
  916. // Assignment
  917. BOOST_UBLAS_INLINE
  918. iterator2 &operator = (const iterator2 &it) {
  919. container_reference<self_type>::assign (&it ());
  920. it_ = it.it_;
  921. return *this;
  922. }
  923. // Comparison
  924. BOOST_UBLAS_INLINE
  925. bool operator == (const iterator2 &it) const {
  926. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  927. return it_ == it.it_;
  928. }
  929. BOOST_UBLAS_INLINE
  930. bool operator < (const iterator2 &it) const {
  931. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  932. return it_ < it.it_;
  933. }
  934. private:
  935. subiterator_type it_;
  936. friend class const_iterator2;
  937. };
  938. #endif
  939. BOOST_UBLAS_INLINE
  940. iterator2 begin2 () {
  941. return find2 (0, 0, 0);
  942. }
  943. BOOST_UBLAS_INLINE
  944. iterator2 end2 () {
  945. return find2 (0, 0, size2_);
  946. }
  947. // Reverse iterators
  948. BOOST_UBLAS_INLINE
  949. const_reverse_iterator1 rbegin1 () const {
  950. return const_reverse_iterator1 (end1 ());
  951. }
  952. BOOST_UBLAS_INLINE
  953. const_reverse_iterator1 rend1 () const {
  954. return const_reverse_iterator1 (begin1 ());
  955. }
  956. BOOST_UBLAS_INLINE
  957. reverse_iterator1 rbegin1 () {
  958. return reverse_iterator1 (end1 ());
  959. }
  960. BOOST_UBLAS_INLINE
  961. reverse_iterator1 rend1 () {
  962. return reverse_iterator1 (begin1 ());
  963. }
  964. BOOST_UBLAS_INLINE
  965. const_reverse_iterator2 rbegin2 () const {
  966. return const_reverse_iterator2 (end2 ());
  967. }
  968. BOOST_UBLAS_INLINE
  969. const_reverse_iterator2 rend2 () const {
  970. return const_reverse_iterator2 (begin2 ());
  971. }
  972. BOOST_UBLAS_INLINE
  973. reverse_iterator2 rbegin2 () {
  974. return reverse_iterator2 (end2 ());
  975. }
  976. BOOST_UBLAS_INLINE
  977. reverse_iterator2 rend2 () {
  978. return reverse_iterator2 (begin2 ());
  979. }
  980. // Serialization
  981. template<class Archive>
  982. void serialize(Archive & ar, const unsigned int /* file_version */){
  983. // we need to copy to a collection_size_type to get a portable
  984. // and efficient serialization
  985. serialization::collection_size_type s1 (size1_);
  986. serialization::collection_size_type s2 (size2_);
  987. // serialize the sizes
  988. ar & serialization::make_nvp("size1",s1)
  989. & serialization::make_nvp("size2",s2);
  990. // copy the values back if loading
  991. if (Archive::is_loading::value) {
  992. size1_ = s1;
  993. size2_ = s2;
  994. }
  995. ar & serialization::make_nvp("data",data_);
  996. }
  997. private:
  998. size_type size1_;
  999. size_type size2_;
  1000. array_type data_;
  1001. };
  1002. /** \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$.
  1003. *
  1004. * 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
  1005. * 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
  1006. * of the container for column major orientation. Finally in a dense matrix all elements are represented in memory
  1007. * in a contiguous chunk of memory.
  1008. *
  1009. * Orientation can be specified. Default is \c row_major
  1010. * The default constructor creates the matrix with size \f$M\f$ by \f$N\f$. Elements are constructed by the storage
  1011. * type \c bounded_array, which need not initialise their value.
  1012. *
  1013. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  1014. * \tparam M maximum and default number of rows (if not specified at construction)
  1015. * \tparam N maximum and default number of columns (if not specified at construction)
  1016. * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major
  1017. */
  1018. template<class T, std::size_t M, std::size_t N, class L>
  1019. class bounded_matrix:
  1020. public matrix<T, L, bounded_array<T, M * N> > {
  1021. typedef matrix<T, L, bounded_array<T, M * N> > matrix_type;
  1022. public:
  1023. typedef typename matrix_type::size_type size_type;
  1024. static const size_type max_size1 = M;
  1025. static const size_type max_size2 = N;
  1026. // Construction and destruction
  1027. BOOST_UBLAS_INLINE
  1028. bounded_matrix ():
  1029. matrix_type (M, N) {}
  1030. BOOST_UBLAS_INLINE
  1031. bounded_matrix (size_type size1, size_type size2):
  1032. matrix_type (size1, size2) {}
  1033. BOOST_UBLAS_INLINE
  1034. bounded_matrix (const bounded_matrix &m):
  1035. matrix_type (m) {}
  1036. template<class A2> // Allow matrix<T, L, bounded_array<M,N> > construction
  1037. BOOST_UBLAS_INLINE
  1038. bounded_matrix (const matrix<T, L, A2> &m):
  1039. matrix_type (m) {}
  1040. template<class AE>
  1041. BOOST_UBLAS_INLINE
  1042. bounded_matrix (const matrix_expression<AE> &ae):
  1043. matrix_type (ae) {}
  1044. BOOST_UBLAS_INLINE
  1045. ~bounded_matrix () {}
  1046. // Assignment
  1047. #ifdef BOOST_UBLAS_MOVE_SEMANTICS
  1048. /*! @note "pass by value" the key idea to enable move semantics */
  1049. BOOST_UBLAS_INLINE
  1050. bounded_matrix &operator = (bounded_matrix m) {
  1051. matrix_type::operator = (m);
  1052. return *this;
  1053. }
  1054. #else
  1055. BOOST_UBLAS_INLINE
  1056. bounded_matrix &operator = (const bounded_matrix &m) {
  1057. matrix_type::operator = (m);
  1058. return *this;
  1059. }
  1060. #endif
  1061. template<class L2, class A2> // Generic matrix assignment
  1062. BOOST_UBLAS_INLINE
  1063. bounded_matrix &operator = (const matrix<T, L2, A2> &m) {
  1064. matrix_type::operator = (m);
  1065. return *this;
  1066. }
  1067. template<class C> // Container assignment without temporary
  1068. BOOST_UBLAS_INLINE
  1069. bounded_matrix &operator = (const matrix_container<C> &m) {
  1070. matrix_type::operator = (m);
  1071. return *this;
  1072. }
  1073. template<class AE>
  1074. BOOST_UBLAS_INLINE
  1075. bounded_matrix &operator = (const matrix_expression<AE> &ae) {
  1076. matrix_type::operator = (ae);
  1077. return *this;
  1078. }
  1079. };
  1080. /** \brief A dense matrix of values of type \c T stored as a vector of vectors.
  1081. *
  1082. * Rows or columns are not stored into contiguous chunks of memory but data inside rows (or columns) are.
  1083. * Orientation and storage can also be specified, otherwise a row major and unbounded arrays are used.
  1084. * The data is stored as a vector of vectors, meaning that rows or columns might not be stored into contiguous chunks
  1085. * of memory. Orientation and storage can also be specified, otherwise a row major and unbounded arrays are used.
  1086. * The storage type defaults to \c unbounded_array<unbounded_array<T>> and orientation is \c row_major. It is \b not
  1087. * required by the storage to initialize elements of the matrix. For a \f$(m \times n)\f$-dimensional matrix and
  1088. * \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
  1089. * container for row major orientation or the \f$(i + j.m)\f$-th element of the container for column major orientation.
  1090. *
  1091. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  1092. * \tparam L the storage organization. It can be either \c row_major or \c column_major. By default it is \c row_major
  1093. * \tparam A the type of Storage array. By default, it is an \unbounded_array<unbounder_array<T>>
  1094. */
  1095. template<class T, class L, class A>
  1096. class vector_of_vector:
  1097. public matrix_container<vector_of_vector<T, L, A> > {
  1098. typedef T *pointer;
  1099. typedef L layout_type;
  1100. typedef vector_of_vector<T, L, A> self_type;
  1101. public:
  1102. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  1103. using matrix_container<self_type>::operator ();
  1104. #endif
  1105. typedef typename A::size_type size_type;
  1106. typedef typename A::difference_type difference_type;
  1107. typedef T value_type;
  1108. typedef const T &const_reference;
  1109. typedef T &reference;
  1110. typedef A array_type;
  1111. typedef const matrix_reference<const self_type> const_closure_type;
  1112. typedef matrix_reference<self_type> closure_type;
  1113. typedef vector<T, typename A::value_type> vector_temporary_type;
  1114. typedef self_type matrix_temporary_type;
  1115. typedef dense_tag storage_category;
  1116. // This could be better for performance,
  1117. // typedef typename unknown_orientation_tag orientation_category;
  1118. // but others depend on the orientation information...
  1119. typedef typename L::orientation_category orientation_category;
  1120. // Construction and destruction
  1121. BOOST_UBLAS_INLINE
  1122. vector_of_vector ():
  1123. matrix_container<self_type> (),
  1124. size1_ (0), size2_ (0), data_ (1) {}
  1125. BOOST_UBLAS_INLINE
  1126. vector_of_vector (size_type size1, size_type size2):
  1127. matrix_container<self_type> (),
  1128. size1_ (size1), size2_ (size2), data_ (1) {
  1129. resize (size1, size2, true);
  1130. }
  1131. BOOST_UBLAS_INLINE
  1132. vector_of_vector (const vector_of_vector &m):
  1133. matrix_container<self_type> (),
  1134. size1_ (m.size1_), size2_ (m.size2_), data_ (m.data_) {}
  1135. template<class AE>
  1136. BOOST_UBLAS_INLINE
  1137. vector_of_vector (const matrix_expression<AE> &ae):
  1138. matrix_container<self_type> (),
  1139. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()), data_ (layout_type::size_M (size1_, size2_) + 1) {
  1140. for (size_type k = 0; k < layout_type::size_M (size1_, size2_); ++ k)
  1141. data ()[k].resize (layout_type::size_m (size1_, size2_));
  1142. matrix_assign<scalar_assign> (*this, ae);
  1143. }
  1144. // Accessors
  1145. BOOST_UBLAS_INLINE
  1146. size_type size1 () const {
  1147. return size1_;
  1148. }
  1149. BOOST_UBLAS_INLINE
  1150. size_type size2 () const {
  1151. return size2_;
  1152. }
  1153. // Storage accessors
  1154. BOOST_UBLAS_INLINE
  1155. const array_type &data () const {
  1156. return data_;
  1157. }
  1158. BOOST_UBLAS_INLINE
  1159. array_type &data () {
  1160. return data_;
  1161. }
  1162. // Resizing
  1163. BOOST_UBLAS_INLINE
  1164. void resize (size_type size1, size_type size2, bool preserve = true) {
  1165. size1_ = size1;
  1166. size2_ = size2;
  1167. if (preserve)
  1168. data ().resize (layout_type::size_M (size1, size2) + 1, typename array_type::value_type ());
  1169. else
  1170. data ().resize (layout_type::size_M (size1, size2) + 1);
  1171. for (size_type k = 0; k < layout_type::size_M (size1, size2); ++ k) {
  1172. if (preserve)
  1173. data () [k].resize (layout_type::size_m (size1, size2), value_type ());
  1174. else
  1175. data () [k].resize (layout_type::size_m (size1, size2));
  1176. }
  1177. }
  1178. // Element access
  1179. BOOST_UBLAS_INLINE
  1180. const_reference operator () (size_type i, size_type j) const {
  1181. return data () [layout_type::index_M (i, j)] [layout_type::index_m (i, j)];
  1182. }
  1183. BOOST_UBLAS_INLINE
  1184. reference at_element (size_type i, size_type j) {
  1185. return data () [layout_type::index_M (i, j)] [layout_type::index_m (i, j)];
  1186. }
  1187. BOOST_UBLAS_INLINE
  1188. reference operator () (size_type i, size_type j) {
  1189. return at_element (i, j);
  1190. }
  1191. // Element assignment
  1192. BOOST_UBLAS_INLINE
  1193. reference insert_element (size_type i, size_type j, const_reference t) {
  1194. return (at_element (i, j) = t);
  1195. }
  1196. BOOST_UBLAS_INLINE
  1197. void erase_element (size_type i, size_type j) {
  1198. at_element (i, j) = value_type/*zero*/();
  1199. }
  1200. // Zeroing
  1201. BOOST_UBLAS_INLINE
  1202. void clear () {
  1203. for (size_type k = 0; k < layout_type::size_M (size1_, size2_); ++ k)
  1204. std::fill (data () [k].begin (), data () [k].end (), value_type/*zero*/());
  1205. }
  1206. // Assignment
  1207. BOOST_UBLAS_INLINE
  1208. vector_of_vector &operator = (const vector_of_vector &m) {
  1209. size1_ = m.size1_;
  1210. size2_ = m.size2_;
  1211. data () = m.data ();
  1212. return *this;
  1213. }
  1214. BOOST_UBLAS_INLINE
  1215. vector_of_vector &assign_temporary (vector_of_vector &m) {
  1216. swap (m);
  1217. return *this;
  1218. }
  1219. template<class AE>
  1220. BOOST_UBLAS_INLINE
  1221. vector_of_vector &operator = (const matrix_expression<AE> &ae) {
  1222. self_type temporary (ae);
  1223. return assign_temporary (temporary);
  1224. }
  1225. template<class C> // Container assignment without temporary
  1226. BOOST_UBLAS_INLINE
  1227. vector_of_vector &operator = (const matrix_container<C> &m) {
  1228. resize (m ().size1 (), m ().size2 (), false);
  1229. assign (m);
  1230. return *this;
  1231. }
  1232. template<class AE>
  1233. BOOST_UBLAS_INLINE
  1234. vector_of_vector &assign (const matrix_expression<AE> &ae) {
  1235. matrix_assign<scalar_assign> (*this, ae);
  1236. return *this;
  1237. }
  1238. template<class AE>
  1239. BOOST_UBLAS_INLINE
  1240. vector_of_vector& operator += (const matrix_expression<AE> &ae) {
  1241. self_type temporary (*this + ae);
  1242. return assign_temporary (temporary);
  1243. }
  1244. template<class C> // Container assignment without temporary
  1245. BOOST_UBLAS_INLINE
  1246. vector_of_vector &operator += (const matrix_container<C> &m) {
  1247. plus_assign (m);
  1248. return *this;
  1249. }
  1250. template<class AE>
  1251. BOOST_UBLAS_INLINE
  1252. vector_of_vector &plus_assign (const matrix_expression<AE> &ae) {
  1253. matrix_assign<scalar_plus_assign> (*this, ae);
  1254. return *this;
  1255. }
  1256. template<class AE>
  1257. BOOST_UBLAS_INLINE
  1258. vector_of_vector& operator -= (const matrix_expression<AE> &ae) {
  1259. self_type temporary (*this - ae);
  1260. return assign_temporary (temporary);
  1261. }
  1262. template<class C> // Container assignment without temporary
  1263. BOOST_UBLAS_INLINE
  1264. vector_of_vector &operator -= (const matrix_container<C> &m) {
  1265. minus_assign (m);
  1266. return *this;
  1267. }
  1268. template<class AE>
  1269. BOOST_UBLAS_INLINE
  1270. vector_of_vector &minus_assign (const matrix_expression<AE> &ae) {
  1271. matrix_assign<scalar_minus_assign> (*this, ae);
  1272. return *this;
  1273. }
  1274. template<class AT>
  1275. BOOST_UBLAS_INLINE
  1276. vector_of_vector& operator *= (const AT &at) {
  1277. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  1278. return *this;
  1279. }
  1280. template<class AT>
  1281. BOOST_UBLAS_INLINE
  1282. vector_of_vector& operator /= (const AT &at) {
  1283. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  1284. return *this;
  1285. }
  1286. // Swapping
  1287. BOOST_UBLAS_INLINE
  1288. void swap (vector_of_vector &m) {
  1289. if (this != &m) {
  1290. std::swap (size1_, m.size1_);
  1291. std::swap (size2_, m.size2_);
  1292. data ().swap (m.data ());
  1293. }
  1294. }
  1295. BOOST_UBLAS_INLINE
  1296. friend void swap (vector_of_vector &m1, vector_of_vector &m2) {
  1297. m1.swap (m2);
  1298. }
  1299. // Iterator types
  1300. private:
  1301. // Use the vector iterator
  1302. typedef typename A::value_type::const_iterator const_subiterator_type;
  1303. typedef typename A::value_type::iterator subiterator_type;
  1304. public:
  1305. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1306. typedef indexed_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  1307. typedef indexed_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  1308. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  1309. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  1310. #else
  1311. class const_iterator1;
  1312. class iterator1;
  1313. class const_iterator2;
  1314. class iterator2;
  1315. #endif
  1316. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  1317. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  1318. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  1319. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  1320. // Element lookup
  1321. BOOST_UBLAS_INLINE
  1322. const_iterator1 find1 (int /*rank*/, size_type i, size_type j) const {
  1323. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1324. return const_iterator1 (*this, i, j);
  1325. #else
  1326. return const_iterator1 (*this, i, j, data () [layout_type::index_M (i, j)].begin () + layout_type::index_m (i, j));
  1327. #endif
  1328. }
  1329. BOOST_UBLAS_INLINE
  1330. iterator1 find1 (int /*rank*/, size_type i, size_type j) {
  1331. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1332. return iterator1 (*this, i, j);
  1333. #else
  1334. return iterator1 (*this, i, j, data () [layout_type::index_M (i, j)].begin () + layout_type::index_m (i, j));
  1335. #endif
  1336. }
  1337. BOOST_UBLAS_INLINE
  1338. const_iterator2 find2 (int /*rank*/, size_type i, size_type j) const {
  1339. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1340. return const_iterator2 (*this, i, j);
  1341. #else
  1342. return const_iterator2 (*this, i, j, data () [layout_type::index_M (i, j)].begin () + layout_type::index_m (i, j));
  1343. #endif
  1344. }
  1345. BOOST_UBLAS_INLINE
  1346. iterator2 find2 (int /*rank*/, size_type i, size_type j) {
  1347. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1348. return iterator2 (*this, i, j);
  1349. #else
  1350. return iterator2 (*this, i, j, data () [layout_type::index_M (i, j)].begin () + layout_type::index_m (i, j));
  1351. #endif
  1352. }
  1353. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1354. class const_iterator1:
  1355. public container_const_reference<vector_of_vector>,
  1356. public random_access_iterator_base<dense_random_access_iterator_tag,
  1357. const_iterator1, value_type> {
  1358. public:
  1359. typedef typename vector_of_vector::value_type value_type;
  1360. typedef typename vector_of_vector::difference_type difference_type;
  1361. typedef typename vector_of_vector::const_reference reference;
  1362. typedef const typename vector_of_vector::pointer pointer;
  1363. typedef const_iterator2 dual_iterator_type;
  1364. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  1365. // Construction and destruction
  1366. BOOST_UBLAS_INLINE
  1367. const_iterator1 ():
  1368. container_const_reference<self_type> (), i_ (), j_ (), it_ () {}
  1369. BOOST_UBLAS_INLINE
  1370. const_iterator1 (const self_type &m, size_type i, size_type j, const const_subiterator_type &it):
  1371. container_const_reference<self_type> (m), i_ (i), j_ (j), it_ (it) {}
  1372. BOOST_UBLAS_INLINE
  1373. const_iterator1 (const iterator1 &it):
  1374. container_const_reference<self_type> (it ()), i_ (it.i_), j_ (it.j_), it_ (it.it_) {}
  1375. // Arithmetic
  1376. BOOST_UBLAS_INLINE
  1377. const_iterator1 &operator ++ () {
  1378. ++ i_;
  1379. const self_type &m = (*this) ();
  1380. if (layout_type::fast_i ())
  1381. ++ it_;
  1382. else
  1383. it_ = m.find1 (1, i_, j_).it_;
  1384. return *this;
  1385. }
  1386. BOOST_UBLAS_INLINE
  1387. const_iterator1 &operator -- () {
  1388. -- i_;
  1389. const self_type &m = (*this) ();
  1390. if (layout_type::fast_i ())
  1391. -- it_;
  1392. else
  1393. it_ = m.find1 (1, i_, j_).it_;
  1394. return *this;
  1395. }
  1396. BOOST_UBLAS_INLINE
  1397. const_iterator1 &operator += (difference_type n) {
  1398. i_ += n;
  1399. const self_type &m = (*this) ();
  1400. it_ = m.find1 (1, i_, j_).it_;
  1401. return *this;
  1402. }
  1403. BOOST_UBLAS_INLINE
  1404. const_iterator1 &operator -= (difference_type n) {
  1405. i_ -= n;
  1406. const self_type &m = (*this) ();
  1407. it_ = m.find1 (1, i_, j_).it_;
  1408. return *this;
  1409. }
  1410. BOOST_UBLAS_INLINE
  1411. difference_type operator - (const const_iterator1 &it) const {
  1412. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1413. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  1414. return index1 () - it.index1 ();
  1415. }
  1416. // Dereference
  1417. BOOST_UBLAS_INLINE
  1418. const_reference operator * () const {
  1419. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1420. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1421. return *it_;
  1422. }
  1423. BOOST_UBLAS_INLINE
  1424. const_reference operator [] (difference_type n) const {
  1425. return *(*this + n);
  1426. }
  1427. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1428. BOOST_UBLAS_INLINE
  1429. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1430. typename self_type::
  1431. #endif
  1432. const_iterator2 begin () const {
  1433. const self_type &m = (*this) ();
  1434. return m.find2 (1, index1 (), 0);
  1435. }
  1436. BOOST_UBLAS_INLINE
  1437. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1438. typename self_type::
  1439. #endif
  1440. const_iterator2 end () const {
  1441. const self_type &m = (*this) ();
  1442. return m.find2 (1, index1 (), m.size2 ());
  1443. }
  1444. BOOST_UBLAS_INLINE
  1445. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1446. typename self_type::
  1447. #endif
  1448. const_reverse_iterator2 rbegin () const {
  1449. return const_reverse_iterator2 (end ());
  1450. }
  1451. BOOST_UBLAS_INLINE
  1452. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1453. typename self_type::
  1454. #endif
  1455. const_reverse_iterator2 rend () const {
  1456. return const_reverse_iterator2 (begin ());
  1457. }
  1458. #endif
  1459. // Indices
  1460. BOOST_UBLAS_INLINE
  1461. size_type index1 () const {
  1462. return i_;
  1463. }
  1464. BOOST_UBLAS_INLINE
  1465. size_type index2 () const {
  1466. return j_;
  1467. }
  1468. // Assignment
  1469. BOOST_UBLAS_INLINE
  1470. const_iterator1 &operator = (const const_iterator1 &it) {
  1471. container_const_reference<self_type>::assign (&it ());
  1472. it_ = it.it_;
  1473. return *this;
  1474. }
  1475. // Comparison
  1476. BOOST_UBLAS_INLINE
  1477. bool operator == (const const_iterator1 &it) const {
  1478. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1479. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  1480. return it_ == it.it_;
  1481. }
  1482. BOOST_UBLAS_INLINE
  1483. bool operator < (const const_iterator1 &it) const {
  1484. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1485. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  1486. return it_ < it.it_;
  1487. }
  1488. private:
  1489. size_type i_;
  1490. size_type j_;
  1491. const_subiterator_type it_;
  1492. friend class iterator1;
  1493. };
  1494. #endif
  1495. BOOST_UBLAS_INLINE
  1496. const_iterator1 begin1 () const {
  1497. return find1 (0, 0, 0);
  1498. }
  1499. BOOST_UBLAS_INLINE
  1500. const_iterator1 end1 () const {
  1501. return find1 (0, size1_, 0);
  1502. }
  1503. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1504. class iterator1:
  1505. public container_reference<vector_of_vector>,
  1506. public random_access_iterator_base<dense_random_access_iterator_tag,
  1507. iterator1, value_type> {
  1508. public:
  1509. typedef typename vector_of_vector::value_type value_type;
  1510. typedef typename vector_of_vector::difference_type difference_type;
  1511. typedef typename vector_of_vector::reference reference;
  1512. typedef typename vector_of_vector::pointer pointer;
  1513. typedef iterator2 dual_iterator_type;
  1514. typedef reverse_iterator2 dual_reverse_iterator_type;
  1515. // Construction and destruction
  1516. BOOST_UBLAS_INLINE
  1517. iterator1 ():
  1518. container_reference<self_type> (), i_ (), j_ (), it_ () {}
  1519. BOOST_UBLAS_INLINE
  1520. iterator1 (self_type &m, size_type i, size_type j, const subiterator_type &it):
  1521. container_reference<self_type> (m), i_ (i), j_ (j), it_ (it) {}
  1522. // Arithmetic
  1523. BOOST_UBLAS_INLINE
  1524. iterator1 &operator ++ () {
  1525. ++ i_;
  1526. self_type &m = (*this) ();
  1527. if (layout_type::fast_i ())
  1528. ++ it_;
  1529. else
  1530. it_ = m.find1 (1, i_, j_).it_;
  1531. return *this;
  1532. }
  1533. BOOST_UBLAS_INLINE
  1534. iterator1 &operator -- () {
  1535. -- i_;
  1536. self_type &m = (*this) ();
  1537. if (layout_type::fast_i ())
  1538. -- it_;
  1539. else
  1540. it_ = m.find1 (1, i_, j_).it_;
  1541. return *this;
  1542. }
  1543. BOOST_UBLAS_INLINE
  1544. iterator1 &operator += (difference_type n) {
  1545. i_ += n;
  1546. self_type &m = (*this) ();
  1547. it_ = m.find1 (1, i_, j_).it_;
  1548. return *this;
  1549. }
  1550. BOOST_UBLAS_INLINE
  1551. iterator1 &operator -= (difference_type n) {
  1552. i_ -= n;
  1553. self_type &m = (*this) ();
  1554. it_ = m.find1 (1, i_, j_).it_;
  1555. return *this;
  1556. }
  1557. BOOST_UBLAS_INLINE
  1558. difference_type operator - (const iterator1 &it) const {
  1559. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1560. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  1561. return index1 () - it.index1 ();
  1562. }
  1563. // Dereference
  1564. BOOST_UBLAS_INLINE
  1565. reference operator * () const {
  1566. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1567. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1568. return *it_;
  1569. }
  1570. BOOST_UBLAS_INLINE
  1571. reference operator [] (difference_type n) const {
  1572. return *(*this + n);
  1573. }
  1574. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1575. BOOST_UBLAS_INLINE
  1576. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1577. typename self_type::
  1578. #endif
  1579. iterator2 begin () const {
  1580. self_type &m = (*this) ();
  1581. return m.find2 (1, index1 (), 0);
  1582. }
  1583. BOOST_UBLAS_INLINE
  1584. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1585. typename self_type::
  1586. #endif
  1587. iterator2 end () const {
  1588. self_type &m = (*this) ();
  1589. return m.find2 (1, index1 (), m.size2 ());
  1590. }
  1591. BOOST_UBLAS_INLINE
  1592. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1593. typename self_type::
  1594. #endif
  1595. reverse_iterator2 rbegin () const {
  1596. return reverse_iterator2 (end ());
  1597. }
  1598. BOOST_UBLAS_INLINE
  1599. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1600. typename self_type::
  1601. #endif
  1602. reverse_iterator2 rend () const {
  1603. return reverse_iterator2 (begin ());
  1604. }
  1605. #endif
  1606. // Indices
  1607. BOOST_UBLAS_INLINE
  1608. size_type index1 () const {
  1609. return i_;
  1610. }
  1611. BOOST_UBLAS_INLINE
  1612. size_type index2 () const {
  1613. return j_;
  1614. }
  1615. // Assignment
  1616. BOOST_UBLAS_INLINE
  1617. iterator1 &operator = (const iterator1 &it) {
  1618. container_reference<self_type>::assign (&it ());
  1619. it_ = it.it_;
  1620. return *this;
  1621. }
  1622. // Comparison
  1623. BOOST_UBLAS_INLINE
  1624. bool operator == (const iterator1 &it) const {
  1625. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1626. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  1627. return it_ == it.it_;
  1628. }
  1629. BOOST_UBLAS_INLINE
  1630. bool operator < (const iterator1 &it) const {
  1631. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1632. BOOST_UBLAS_CHECK (index2 () == it.index2 (), bad_index ());
  1633. return it_ < it.it_;
  1634. }
  1635. private:
  1636. size_type i_;
  1637. size_type j_;
  1638. subiterator_type it_;
  1639. friend class const_iterator1;
  1640. };
  1641. #endif
  1642. BOOST_UBLAS_INLINE
  1643. iterator1 begin1 () {
  1644. return find1 (0, 0, 0);
  1645. }
  1646. BOOST_UBLAS_INLINE
  1647. iterator1 end1 () {
  1648. return find1 (0, size1_, 0);
  1649. }
  1650. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1651. class const_iterator2:
  1652. public container_const_reference<vector_of_vector>,
  1653. public random_access_iterator_base<dense_random_access_iterator_tag,
  1654. const_iterator2, value_type> {
  1655. public:
  1656. typedef typename vector_of_vector::value_type value_type;
  1657. typedef typename vector_of_vector::difference_type difference_type;
  1658. typedef typename vector_of_vector::const_reference reference;
  1659. typedef const typename vector_of_vector::pointer pointer;
  1660. typedef const_iterator1 dual_iterator_type;
  1661. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  1662. // Construction and destruction
  1663. BOOST_UBLAS_INLINE
  1664. const_iterator2 ():
  1665. container_const_reference<self_type> (), i_ (), j_ (), it_ () {}
  1666. BOOST_UBLAS_INLINE
  1667. const_iterator2 (const self_type &m, size_type i, size_type j, const const_subiterator_type &it):
  1668. container_const_reference<self_type> (m), i_ (i), j_ (j), it_ (it) {}
  1669. BOOST_UBLAS_INLINE
  1670. const_iterator2 (const iterator2 &it):
  1671. container_const_reference<self_type> (it ()), i_ (it.i_), j_ (it.j_), it_ (it.it_) {}
  1672. // Arithmetic
  1673. BOOST_UBLAS_INLINE
  1674. const_iterator2 &operator ++ () {
  1675. ++ j_;
  1676. const self_type &m = (*this) ();
  1677. if (layout_type::fast_j ())
  1678. ++ it_;
  1679. else
  1680. it_ = m.find2 (1, i_, j_).it_;
  1681. return *this;
  1682. }
  1683. BOOST_UBLAS_INLINE
  1684. const_iterator2 &operator -- () {
  1685. -- j_;
  1686. const self_type &m = (*this) ();
  1687. if (layout_type::fast_j ())
  1688. -- it_;
  1689. else
  1690. it_ = m.find2 (1, i_, j_).it_;
  1691. return *this;
  1692. }
  1693. BOOST_UBLAS_INLINE
  1694. const_iterator2 &operator += (difference_type n) {
  1695. j_ += n;
  1696. const self_type &m = (*this) ();
  1697. it_ = m.find2 (1, i_, j_).it_;
  1698. return *this;
  1699. }
  1700. BOOST_UBLAS_INLINE
  1701. const_iterator2 &operator -= (difference_type n) {
  1702. j_ -= n;
  1703. const self_type &m = (*this) ();
  1704. it_ = m.find2 (1, i_, j_).it_;
  1705. return *this;
  1706. }
  1707. BOOST_UBLAS_INLINE
  1708. difference_type operator - (const const_iterator2 &it) const {
  1709. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1710. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  1711. return index2 () - it.index2 ();
  1712. }
  1713. // Dereference
  1714. BOOST_UBLAS_INLINE
  1715. const_reference operator * () const {
  1716. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1717. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1718. return *it_;
  1719. }
  1720. BOOST_UBLAS_INLINE
  1721. const_reference operator [] (difference_type n) const {
  1722. return *(*this + n);
  1723. }
  1724. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1725. BOOST_UBLAS_INLINE
  1726. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1727. typename self_type::
  1728. #endif
  1729. const_iterator1 begin () const {
  1730. const self_type &m = (*this) ();
  1731. return m.find1 (1, 0, index2 ());
  1732. }
  1733. BOOST_UBLAS_INLINE
  1734. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1735. typename self_type::
  1736. #endif
  1737. const_iterator1 end () const {
  1738. const self_type &m = (*this) ();
  1739. return m.find1 (1, m.size1 (), index2 ());
  1740. }
  1741. BOOST_UBLAS_INLINE
  1742. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1743. typename self_type::
  1744. #endif
  1745. const_reverse_iterator1 rbegin () const {
  1746. return const_reverse_iterator1 (end ());
  1747. }
  1748. BOOST_UBLAS_INLINE
  1749. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1750. typename self_type::
  1751. #endif
  1752. const_reverse_iterator1 rend () const {
  1753. return const_reverse_iterator1 (begin ());
  1754. }
  1755. #endif
  1756. // Indices
  1757. BOOST_UBLAS_INLINE
  1758. size_type index1 () const {
  1759. return i_;
  1760. }
  1761. BOOST_UBLAS_INLINE
  1762. size_type index2 () const {
  1763. return j_;
  1764. }
  1765. // Assignment
  1766. BOOST_UBLAS_INLINE
  1767. const_iterator2 &operator = (const const_iterator2 &it) {
  1768. container_const_reference<self_type>::assign (&it ());
  1769. it_ = it.it_;
  1770. return *this;
  1771. }
  1772. // Comparison
  1773. BOOST_UBLAS_INLINE
  1774. bool operator == (const const_iterator2 &it) const {
  1775. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1776. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  1777. return it_ == it.it_;
  1778. }
  1779. BOOST_UBLAS_INLINE
  1780. bool operator < (const const_iterator2 &it) const {
  1781. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1782. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  1783. return it_ < it.it_;
  1784. }
  1785. private:
  1786. size_type i_;
  1787. size_type j_;
  1788. const_subiterator_type it_;
  1789. friend class iterator2;
  1790. };
  1791. #endif
  1792. BOOST_UBLAS_INLINE
  1793. const_iterator2 begin2 () const {
  1794. return find2 (0, 0, 0);
  1795. }
  1796. BOOST_UBLAS_INLINE
  1797. const_iterator2 end2 () const {
  1798. return find2 (0, 0, size2_);
  1799. }
  1800. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1801. class iterator2:
  1802. public container_reference<vector_of_vector>,
  1803. public random_access_iterator_base<dense_random_access_iterator_tag,
  1804. iterator2, value_type> {
  1805. public:
  1806. typedef typename vector_of_vector::value_type value_type;
  1807. typedef typename vector_of_vector::difference_type difference_type;
  1808. typedef typename vector_of_vector::reference reference;
  1809. typedef typename vector_of_vector::pointer pointer;
  1810. typedef iterator1 dual_iterator_type;
  1811. typedef reverse_iterator1 dual_reverse_iterator_type;
  1812. // Construction and destruction
  1813. BOOST_UBLAS_INLINE
  1814. iterator2 ():
  1815. container_reference<self_type> (), i_ (), j_ (), it_ () {}
  1816. BOOST_UBLAS_INLINE
  1817. iterator2 (self_type &m, size_type i, size_type j, const subiterator_type &it):
  1818. container_reference<self_type> (m), i_ (i), j_ (j), it_ (it) {}
  1819. // Arithmetic
  1820. BOOST_UBLAS_INLINE
  1821. iterator2 &operator ++ () {
  1822. ++ j_;
  1823. self_type &m = (*this) ();
  1824. if (layout_type::fast_j ())
  1825. ++ it_;
  1826. else
  1827. it_ = m.find2 (1, i_, j_).it_;
  1828. return *this;
  1829. }
  1830. BOOST_UBLAS_INLINE
  1831. iterator2 &operator -- () {
  1832. -- j_;
  1833. self_type &m = (*this) ();
  1834. if (layout_type::fast_j ())
  1835. -- it_;
  1836. else
  1837. it_ = m.find2 (1, i_, j_).it_;
  1838. return *this;
  1839. }
  1840. BOOST_UBLAS_INLINE
  1841. iterator2 &operator += (difference_type n) {
  1842. j_ += n;
  1843. self_type &m = (*this) ();
  1844. it_ = m.find2 (1, i_, j_).it_;
  1845. return *this;
  1846. }
  1847. BOOST_UBLAS_INLINE
  1848. iterator2 &operator -= (difference_type n) {
  1849. j_ -= n;
  1850. self_type &m = (*this) ();
  1851. it_ = m.find2 (1, i_, j_).it_;
  1852. return *this;
  1853. }
  1854. BOOST_UBLAS_INLINE
  1855. difference_type operator - (const iterator2 &it) const {
  1856. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1857. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  1858. return index2 () - it.index2 ();
  1859. }
  1860. // Dereference
  1861. BOOST_UBLAS_INLINE
  1862. reference operator * () const {
  1863. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  1864. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  1865. return *it_;
  1866. }
  1867. BOOST_UBLAS_INLINE
  1868. reference operator [] (difference_type n) const {
  1869. return *(*this + n);
  1870. }
  1871. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1872. BOOST_UBLAS_INLINE
  1873. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1874. typename self_type::
  1875. #endif
  1876. iterator1 begin () const {
  1877. self_type &m = (*this) ();
  1878. return m.find1 (1, 0, index2 ());
  1879. }
  1880. BOOST_UBLAS_INLINE
  1881. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1882. typename self_type::
  1883. #endif
  1884. iterator1 end () const {
  1885. self_type &m = (*this) ();
  1886. return m.find1 (1, m.size1 (), index2 ());
  1887. }
  1888. BOOST_UBLAS_INLINE
  1889. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1890. typename self_type::
  1891. #endif
  1892. reverse_iterator1 rbegin () const {
  1893. return reverse_iterator1 (end ());
  1894. }
  1895. BOOST_UBLAS_INLINE
  1896. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1897. typename self_type::
  1898. #endif
  1899. reverse_iterator1 rend () const {
  1900. return reverse_iterator1 (begin ());
  1901. }
  1902. #endif
  1903. // Indices
  1904. BOOST_UBLAS_INLINE
  1905. size_type index1 () const {
  1906. return i_;
  1907. }
  1908. BOOST_UBLAS_INLINE
  1909. size_type index2 () const {
  1910. return j_;
  1911. }
  1912. // Assignment
  1913. BOOST_UBLAS_INLINE
  1914. iterator2 &operator = (const iterator2 &it) {
  1915. container_reference<self_type>::assign (&it ());
  1916. it_ = it.it_;
  1917. return *this;
  1918. }
  1919. // Comparison
  1920. BOOST_UBLAS_INLINE
  1921. bool operator == (const iterator2 &it) const {
  1922. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1923. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  1924. return it_ == it.it_;
  1925. }
  1926. BOOST_UBLAS_INLINE
  1927. bool operator < (const iterator2 &it) const {
  1928. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1929. BOOST_UBLAS_CHECK (index1 () == it.index1 (), bad_index ());
  1930. return it_ < it.it_;
  1931. }
  1932. private:
  1933. size_type i_;
  1934. size_type j_;
  1935. subiterator_type it_;
  1936. friend class const_iterator2;
  1937. };
  1938. #endif
  1939. BOOST_UBLAS_INLINE
  1940. iterator2 begin2 () {
  1941. return find2 (0, 0, 0);
  1942. }
  1943. BOOST_UBLAS_INLINE
  1944. iterator2 end2 () {
  1945. return find2 (0, 0, size2_);
  1946. }
  1947. // Reverse iterators
  1948. BOOST_UBLAS_INLINE
  1949. const_reverse_iterator1 rbegin1 () const {
  1950. return const_reverse_iterator1 (end1 ());
  1951. }
  1952. BOOST_UBLAS_INLINE
  1953. const_reverse_iterator1 rend1 () const {
  1954. return const_reverse_iterator1 (begin1 ());
  1955. }
  1956. BOOST_UBLAS_INLINE
  1957. reverse_iterator1 rbegin1 () {
  1958. return reverse_iterator1 (end1 ());
  1959. }
  1960. BOOST_UBLAS_INLINE
  1961. reverse_iterator1 rend1 () {
  1962. return reverse_iterator1 (begin1 ());
  1963. }
  1964. BOOST_UBLAS_INLINE
  1965. const_reverse_iterator2 rbegin2 () const {
  1966. return const_reverse_iterator2 (end2 ());
  1967. }
  1968. BOOST_UBLAS_INLINE
  1969. const_reverse_iterator2 rend2 () const {
  1970. return const_reverse_iterator2 (begin2 ());
  1971. }
  1972. BOOST_UBLAS_INLINE
  1973. reverse_iterator2 rbegin2 () {
  1974. return reverse_iterator2 (end2 ());
  1975. }
  1976. BOOST_UBLAS_INLINE
  1977. reverse_iterator2 rend2 () {
  1978. return reverse_iterator2 (begin2 ());
  1979. }
  1980. // Serialization
  1981. template<class Archive>
  1982. void serialize(Archive & ar, const unsigned int /* file_version */){
  1983. // we need to copy to a collection_size_type to get a portable
  1984. // and efficient serialization
  1985. serialization::collection_size_type s1 (size1_);
  1986. serialization::collection_size_type s2 (size2_);
  1987. // serialize the sizes
  1988. ar & serialization::make_nvp("size1",s1)
  1989. & serialization::make_nvp("size2",s2);
  1990. // copy the values back if loading
  1991. if (Archive::is_loading::value) {
  1992. size1_ = s1;
  1993. size2_ = s2;
  1994. }
  1995. ar & serialization::make_nvp("data",data_);
  1996. }
  1997. private:
  1998. size_type size1_;
  1999. size_type size2_;
  2000. array_type data_;
  2001. };
  2002. /** \brief A matrix with all values of type \c T equal to zero
  2003. *
  2004. * Changing values does not affect the matrix, however assigning it to a normal matrix will put zero
  2005. * everywhere in the target matrix. All accesses are constant time, due to the trivial value.
  2006. *
  2007. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  2008. * \tparam ALLOC an allocator for storing the zero element. By default, a standar allocator is used.
  2009. */
  2010. template<class T, class ALLOC>
  2011. class zero_matrix:
  2012. public matrix_container<zero_matrix<T, ALLOC> > {
  2013. typedef const T *const_pointer;
  2014. typedef zero_matrix<T, ALLOC> self_type;
  2015. public:
  2016. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  2017. using matrix_container<self_type>::operator ();
  2018. #endif
  2019. typedef typename ALLOC::size_type size_type;
  2020. typedef typename ALLOC::difference_type difference_type;
  2021. typedef T value_type;
  2022. typedef const T &const_reference;
  2023. typedef T &reference;
  2024. typedef const matrix_reference<const self_type> const_closure_type;
  2025. typedef matrix_reference<self_type> closure_type;
  2026. typedef sparse_tag storage_category;
  2027. typedef unknown_orientation_tag orientation_category;
  2028. // Construction and destruction
  2029. BOOST_UBLAS_INLINE
  2030. zero_matrix ():
  2031. matrix_container<self_type> (),
  2032. size1_ (0), size2_ (0) {}
  2033. BOOST_UBLAS_INLINE
  2034. zero_matrix (size_type size):
  2035. matrix_container<self_type> (),
  2036. size1_ (size), size2_ (size) {}
  2037. BOOST_UBLAS_INLINE
  2038. zero_matrix (size_type size1, size_type size2):
  2039. matrix_container<self_type> (),
  2040. size1_ (size1), size2_ (size2) {}
  2041. BOOST_UBLAS_INLINE
  2042. zero_matrix (const zero_matrix &m):
  2043. matrix_container<self_type> (),
  2044. size1_ (m.size1_), size2_ (m.size2_) {}
  2045. // Accessors
  2046. BOOST_UBLAS_INLINE
  2047. size_type size1 () const {
  2048. return size1_;
  2049. }
  2050. BOOST_UBLAS_INLINE
  2051. size_type size2 () const {
  2052. return size2_;
  2053. }
  2054. // Resizing
  2055. BOOST_UBLAS_INLINE
  2056. void resize (size_type size, bool preserve = true) {
  2057. size1_ = size;
  2058. size2_ = size;
  2059. }
  2060. BOOST_UBLAS_INLINE
  2061. void resize (size_type size1, size_type size2, bool /*preserve*/ = true) {
  2062. size1_ = size1;
  2063. size2_ = size2;
  2064. }
  2065. // Element access
  2066. BOOST_UBLAS_INLINE
  2067. const_reference operator () (size_type /* i */, size_type /* j */) const {
  2068. return zero_;
  2069. }
  2070. // Assignment
  2071. BOOST_UBLAS_INLINE
  2072. zero_matrix &operator = (const zero_matrix &m) {
  2073. size1_ = m.size1_;
  2074. size2_ = m.size2_;
  2075. return *this;
  2076. }
  2077. BOOST_UBLAS_INLINE
  2078. zero_matrix &assign_temporary (zero_matrix &m) {
  2079. swap (m);
  2080. return *this;
  2081. }
  2082. // Swapping
  2083. BOOST_UBLAS_INLINE
  2084. void swap (zero_matrix &m) {
  2085. if (this != &m) {
  2086. std::swap (size1_, m.size1_);
  2087. std::swap (size2_, m.size2_);
  2088. }
  2089. }
  2090. BOOST_UBLAS_INLINE
  2091. friend void swap (zero_matrix &m1, zero_matrix &m2) {
  2092. m1.swap (m2);
  2093. }
  2094. // Iterator types
  2095. public:
  2096. class const_iterator1;
  2097. class const_iterator2;
  2098. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  2099. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  2100. // Element lookup
  2101. BOOST_UBLAS_INLINE
  2102. const_iterator1 find1 (int /*rank*/, size_type /*i*/, size_type /*j*/) const {
  2103. return const_iterator1 (*this);
  2104. }
  2105. BOOST_UBLAS_INLINE
  2106. const_iterator2 find2 (int /*rank*/, size_type /*i*/, size_type /*j*/) const {
  2107. return const_iterator2 (*this);
  2108. }
  2109. class const_iterator1:
  2110. public container_const_reference<zero_matrix>,
  2111. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  2112. const_iterator1, value_type> {
  2113. public:
  2114. typedef typename zero_matrix::value_type value_type;
  2115. typedef typename zero_matrix::difference_type difference_type;
  2116. typedef typename zero_matrix::const_reference reference;
  2117. typedef typename zero_matrix::const_pointer pointer;
  2118. typedef const_iterator2 dual_iterator_type;
  2119. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  2120. // Construction and destruction
  2121. BOOST_UBLAS_INLINE
  2122. const_iterator1 ():
  2123. container_const_reference<self_type> () {}
  2124. BOOST_UBLAS_INLINE
  2125. const_iterator1 (const self_type &m):
  2126. container_const_reference<self_type> (m) {}
  2127. // Arithmetic
  2128. BOOST_UBLAS_INLINE
  2129. const_iterator1 &operator ++ () {
  2130. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2131. return *this;
  2132. }
  2133. BOOST_UBLAS_INLINE
  2134. const_iterator1 &operator -- () {
  2135. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2136. return *this;
  2137. }
  2138. // Dereference
  2139. BOOST_UBLAS_INLINE
  2140. const_reference operator * () const {
  2141. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2142. return zero_; // arbitary return value
  2143. }
  2144. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2145. BOOST_UBLAS_INLINE
  2146. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2147. typename self_type::
  2148. #endif
  2149. const_iterator2 begin () const {
  2150. return const_iterator2 ((*this) ());
  2151. }
  2152. BOOST_UBLAS_INLINE
  2153. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2154. typename self_type::
  2155. #endif
  2156. const_iterator2 end () const {
  2157. return const_iterator2 ((*this) ());
  2158. }
  2159. BOOST_UBLAS_INLINE
  2160. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2161. typename self_type::
  2162. #endif
  2163. const_reverse_iterator2 rbegin () const {
  2164. return const_reverse_iterator2 (end ());
  2165. }
  2166. BOOST_UBLAS_INLINE
  2167. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2168. typename self_type::
  2169. #endif
  2170. const_reverse_iterator2 rend () const {
  2171. return const_reverse_iterator2 (begin ());
  2172. }
  2173. #endif
  2174. // Indices
  2175. BOOST_UBLAS_INLINE
  2176. size_type index1 () const {
  2177. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2178. return 0; // arbitary return value
  2179. }
  2180. BOOST_UBLAS_INLINE
  2181. size_type index2 () const {
  2182. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2183. return 0; // arbitary return value
  2184. }
  2185. // Assignment
  2186. BOOST_UBLAS_INLINE
  2187. const_iterator1 &operator = (const const_iterator1 &it) {
  2188. container_const_reference<self_type>::assign (&it ());
  2189. return *this;
  2190. }
  2191. // Comparison
  2192. BOOST_UBLAS_INLINE
  2193. bool operator == (const const_iterator1 &it) const {
  2194. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2195. detail::ignore_unused_variable_warning(it);
  2196. return true;
  2197. }
  2198. };
  2199. typedef const_iterator1 iterator1;
  2200. BOOST_UBLAS_INLINE
  2201. const_iterator1 begin1 () const {
  2202. return const_iterator1 (*this);
  2203. }
  2204. BOOST_UBLAS_INLINE
  2205. const_iterator1 end1 () const {
  2206. return const_iterator1 (*this);
  2207. }
  2208. class const_iterator2:
  2209. public container_const_reference<zero_matrix>,
  2210. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  2211. const_iterator2, value_type> {
  2212. public:
  2213. typedef typename zero_matrix::value_type value_type;
  2214. typedef typename zero_matrix::difference_type difference_type;
  2215. typedef typename zero_matrix::const_reference reference;
  2216. typedef typename zero_matrix::const_pointer pointer;
  2217. typedef const_iterator1 dual_iterator_type;
  2218. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  2219. // Construction and destruction
  2220. BOOST_UBLAS_INLINE
  2221. const_iterator2 ():
  2222. container_const_reference<self_type> () {}
  2223. BOOST_UBLAS_INLINE
  2224. const_iterator2 (const self_type &m):
  2225. container_const_reference<self_type> (m) {}
  2226. // Arithmetic
  2227. BOOST_UBLAS_INLINE
  2228. const_iterator2 &operator ++ () {
  2229. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2230. return *this;
  2231. }
  2232. BOOST_UBLAS_INLINE
  2233. const_iterator2 &operator -- () {
  2234. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2235. return *this;
  2236. }
  2237. // Dereference
  2238. BOOST_UBLAS_INLINE
  2239. const_reference operator * () const {
  2240. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2241. return zero_; // arbitary return value
  2242. }
  2243. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2244. BOOST_UBLAS_INLINE
  2245. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2246. typename self_type::
  2247. #endif
  2248. const_iterator1 begin () const {
  2249. return const_iterator1 ((*this) ());
  2250. }
  2251. BOOST_UBLAS_INLINE
  2252. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2253. typename self_type::
  2254. #endif
  2255. const_iterator1 end () const {
  2256. return const_iterator1 ((*this) ());
  2257. }
  2258. BOOST_UBLAS_INLINE
  2259. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2260. typename self_type::
  2261. #endif
  2262. const_reverse_iterator1 rbegin () const {
  2263. return const_reverse_iterator1 (end ());
  2264. }
  2265. BOOST_UBLAS_INLINE
  2266. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2267. typename self_type::
  2268. #endif
  2269. const_reverse_iterator1 rend () const {
  2270. return const_reverse_iterator1 (begin ());
  2271. }
  2272. #endif
  2273. // Indices
  2274. BOOST_UBLAS_INLINE
  2275. size_type index1 () const {
  2276. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2277. return 0; // arbitary return value
  2278. }
  2279. BOOST_UBLAS_INLINE
  2280. size_type index2 () const {
  2281. BOOST_UBLAS_CHECK_FALSE (bad_index ());
  2282. return 0; // arbitary return value
  2283. }
  2284. // Assignment
  2285. BOOST_UBLAS_INLINE
  2286. const_iterator2 &operator = (const const_iterator2 &it) {
  2287. container_const_reference<self_type>::assign (&it ());
  2288. return *this;
  2289. }
  2290. // Comparison
  2291. BOOST_UBLAS_INLINE
  2292. bool operator == (const const_iterator2 &it) const {
  2293. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2294. detail::ignore_unused_variable_warning(it);
  2295. return true;
  2296. }
  2297. };
  2298. typedef const_iterator2 iterator2;
  2299. BOOST_UBLAS_INLINE
  2300. const_iterator2 begin2 () const {
  2301. return find2 (0, 0, 0);
  2302. }
  2303. BOOST_UBLAS_INLINE
  2304. const_iterator2 end2 () const {
  2305. return find2 (0, 0, size2_);
  2306. }
  2307. // Reverse iterators
  2308. BOOST_UBLAS_INLINE
  2309. const_reverse_iterator1 rbegin1 () const {
  2310. return const_reverse_iterator1 (end1 ());
  2311. }
  2312. BOOST_UBLAS_INLINE
  2313. const_reverse_iterator1 rend1 () const {
  2314. return const_reverse_iterator1 (begin1 ());
  2315. }
  2316. BOOST_UBLAS_INLINE
  2317. const_reverse_iterator2 rbegin2 () const {
  2318. return const_reverse_iterator2 (end2 ());
  2319. }
  2320. BOOST_UBLAS_INLINE
  2321. const_reverse_iterator2 rend2 () const {
  2322. return const_reverse_iterator2 (begin2 ());
  2323. }
  2324. // Serialization
  2325. template<class Archive>
  2326. void serialize(Archive & ar, const unsigned int /* file_version */){
  2327. // we need to copy to a collection_size_type to get a portable
  2328. // and efficient serialization
  2329. serialization::collection_size_type s1 (size1_);
  2330. serialization::collection_size_type s2 (size2_);
  2331. // serialize the sizes
  2332. ar & serialization::make_nvp("size1",s1)
  2333. & serialization::make_nvp("size2",s2);
  2334. // copy the values back if loading
  2335. if (Archive::is_loading::value) {
  2336. size1_ = s1;
  2337. size2_ = s2;
  2338. }
  2339. }
  2340. private:
  2341. size_type size1_;
  2342. size_type size2_;
  2343. static const value_type zero_;
  2344. };
  2345. template<class T, class ALLOC>
  2346. const typename zero_matrix<T, ALLOC>::value_type zero_matrix<T, ALLOC>::zero_ = T(/*zero*/);
  2347. /** \brief An identity matrix with values of type \c T
  2348. *
  2349. * Elements or cordinates \f$(i,i)\f$ are equal to 1 (one) and all others to 0 (zero).
  2350. * Changing values does not affect the matrix, however assigning it to a normal matrix will
  2351. * make the matrix equal to an identity matrix. All accesses are constant du to the trivial values.
  2352. *
  2353. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  2354. * \tparam ALLOC an allocator for storing the zeros and one elements. By default, a standar allocator is used.
  2355. */
  2356. template<class T, class ALLOC>
  2357. class identity_matrix:
  2358. public matrix_container<identity_matrix<T, ALLOC> > {
  2359. typedef const T *const_pointer;
  2360. typedef identity_matrix<T, ALLOC> self_type;
  2361. public:
  2362. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  2363. using matrix_container<self_type>::operator ();
  2364. #endif
  2365. typedef typename ALLOC::size_type size_type;
  2366. typedef typename ALLOC::difference_type difference_type;
  2367. typedef T value_type;
  2368. typedef const T &const_reference;
  2369. typedef T &reference;
  2370. typedef const matrix_reference<const self_type> const_closure_type;
  2371. typedef matrix_reference<self_type> closure_type;
  2372. typedef sparse_tag storage_category;
  2373. typedef unknown_orientation_tag orientation_category;
  2374. // Construction and destruction
  2375. BOOST_UBLAS_INLINE
  2376. identity_matrix ():
  2377. matrix_container<self_type> (),
  2378. size1_ (0), size2_ (0), size_common_ (0) {}
  2379. BOOST_UBLAS_INLINE
  2380. identity_matrix (size_type size):
  2381. matrix_container<self_type> (),
  2382. size1_ (size), size2_ (size), size_common_ ((std::min) (size1_, size2_)) {}
  2383. BOOST_UBLAS_INLINE
  2384. identity_matrix (size_type size1, size_type size2):
  2385. matrix_container<self_type> (),
  2386. size1_ (size1), size2_ (size2), size_common_ ((std::min) (size1_, size2_)) {}
  2387. BOOST_UBLAS_INLINE
  2388. identity_matrix (const identity_matrix &m):
  2389. matrix_container<self_type> (),
  2390. size1_ (m.size1_), size2_ (m.size2_), size_common_ ((std::min) (size1_, size2_)) {}
  2391. // Accessors
  2392. BOOST_UBLAS_INLINE
  2393. size_type size1 () const {
  2394. return size1_;
  2395. }
  2396. BOOST_UBLAS_INLINE
  2397. size_type size2 () const {
  2398. return size2_;
  2399. }
  2400. // Resizing
  2401. BOOST_UBLAS_INLINE
  2402. void resize (size_type size, bool preserve = true) {
  2403. size1_ = size;
  2404. size2_ = size;
  2405. size_common_ = ((std::min)(size1_, size2_));
  2406. }
  2407. BOOST_UBLAS_INLINE
  2408. void resize (size_type size1, size_type size2, bool /*preserve*/ = true) {
  2409. size1_ = size1;
  2410. size2_ = size2;
  2411. size_common_ = ((std::min)(size1_, size2_));
  2412. }
  2413. // Element access
  2414. BOOST_UBLAS_INLINE
  2415. const_reference operator () (size_type i, size_type j) const {
  2416. if (i == j)
  2417. return one_;
  2418. else
  2419. return zero_;
  2420. }
  2421. // Assignment
  2422. BOOST_UBLAS_INLINE
  2423. identity_matrix &operator = (const identity_matrix &m) {
  2424. size1_ = m.size1_;
  2425. size2_ = m.size2_;
  2426. size_common_ = m.size_common_;
  2427. return *this;
  2428. }
  2429. BOOST_UBLAS_INLINE
  2430. identity_matrix &assign_temporary (identity_matrix &m) {
  2431. swap (m);
  2432. return *this;
  2433. }
  2434. // Swapping
  2435. BOOST_UBLAS_INLINE
  2436. void swap (identity_matrix &m) {
  2437. if (this != &m) {
  2438. std::swap (size1_, m.size1_);
  2439. std::swap (size2_, m.size2_);
  2440. std::swap (size_common_, m.size_common_);
  2441. }
  2442. }
  2443. BOOST_UBLAS_INLINE
  2444. friend void swap (identity_matrix &m1, identity_matrix &m2) {
  2445. m1.swap (m2);
  2446. }
  2447. // Iterator types
  2448. private:
  2449. // Use an index
  2450. typedef size_type const_subiterator_type;
  2451. public:
  2452. class const_iterator1;
  2453. class const_iterator2;
  2454. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  2455. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  2456. // Element lookup
  2457. BOOST_UBLAS_INLINE
  2458. const_iterator1 find1 (int rank, size_type i, size_type j) const {
  2459. if (rank == 1) {
  2460. i = (std::max) (i, j);
  2461. i = (std::min) (i, j + 1);
  2462. }
  2463. return const_iterator1 (*this, i);
  2464. }
  2465. BOOST_UBLAS_INLINE
  2466. const_iterator2 find2 (int rank, size_type i, size_type j) const {
  2467. if (rank == 1) {
  2468. j = (std::max) (j, i);
  2469. j = (std::min) (j, i + 1);
  2470. }
  2471. return const_iterator2 (*this, j);
  2472. }
  2473. class const_iterator1:
  2474. public container_const_reference<identity_matrix>,
  2475. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  2476. const_iterator1, value_type> {
  2477. public:
  2478. typedef typename identity_matrix::value_type value_type;
  2479. typedef typename identity_matrix::difference_type difference_type;
  2480. typedef typename identity_matrix::const_reference reference;
  2481. typedef typename identity_matrix::const_pointer pointer;
  2482. typedef const_iterator2 dual_iterator_type;
  2483. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  2484. // Construction and destruction
  2485. BOOST_UBLAS_INLINE
  2486. const_iterator1 ():
  2487. container_const_reference<self_type> (), it_ () {}
  2488. BOOST_UBLAS_INLINE
  2489. const_iterator1 (const self_type &m, const const_subiterator_type &it):
  2490. container_const_reference<self_type> (m), it_ (it) {}
  2491. // Arithmetic
  2492. BOOST_UBLAS_INLINE
  2493. const_iterator1 &operator ++ () {
  2494. BOOST_UBLAS_CHECK (it_ < (*this) ().size1 (), bad_index ());
  2495. ++it_;
  2496. return *this;
  2497. }
  2498. BOOST_UBLAS_INLINE
  2499. const_iterator1 &operator -- () {
  2500. BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
  2501. --it_;
  2502. return *this;
  2503. }
  2504. // Dereference
  2505. BOOST_UBLAS_INLINE
  2506. const_reference operator * () const {
  2507. return one_;
  2508. }
  2509. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2510. BOOST_UBLAS_INLINE
  2511. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2512. typename self_type::
  2513. #endif
  2514. const_iterator2 begin () const {
  2515. return const_iterator2 ((*this) (), it_);
  2516. }
  2517. BOOST_UBLAS_INLINE
  2518. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2519. typename self_type::
  2520. #endif
  2521. const_iterator2 end () const {
  2522. return const_iterator2 ((*this) (), it_ + 1);
  2523. }
  2524. BOOST_UBLAS_INLINE
  2525. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2526. typename self_type::
  2527. #endif
  2528. const_reverse_iterator2 rbegin () const {
  2529. return const_reverse_iterator2 (end ());
  2530. }
  2531. BOOST_UBLAS_INLINE
  2532. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2533. typename self_type::
  2534. #endif
  2535. const_reverse_iterator2 rend () const {
  2536. return const_reverse_iterator2 (begin ());
  2537. }
  2538. #endif
  2539. // Indices
  2540. BOOST_UBLAS_INLINE
  2541. size_type index1 () const {
  2542. return it_;
  2543. }
  2544. BOOST_UBLAS_INLINE
  2545. size_type index2 () const {
  2546. return it_;
  2547. }
  2548. // Assignment
  2549. BOOST_UBLAS_INLINE
  2550. const_iterator1 &operator = (const const_iterator1 &it) {
  2551. container_const_reference<self_type>::assign (&it ());
  2552. it_ = it.it_;
  2553. return *this;
  2554. }
  2555. // Comparison
  2556. BOOST_UBLAS_INLINE
  2557. bool operator == (const const_iterator1 &it) const {
  2558. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2559. return it_ == it.it_;
  2560. }
  2561. private:
  2562. const_subiterator_type it_;
  2563. };
  2564. typedef const_iterator1 iterator1;
  2565. BOOST_UBLAS_INLINE
  2566. const_iterator1 begin1 () const {
  2567. return const_iterator1 (*this, 0);
  2568. }
  2569. BOOST_UBLAS_INLINE
  2570. const_iterator1 end1 () const {
  2571. return const_iterator1 (*this, size_common_);
  2572. }
  2573. class const_iterator2:
  2574. public container_const_reference<identity_matrix>,
  2575. public bidirectional_iterator_base<sparse_bidirectional_iterator_tag,
  2576. const_iterator2, value_type> {
  2577. public:
  2578. typedef typename identity_matrix::value_type value_type;
  2579. typedef typename identity_matrix::difference_type difference_type;
  2580. typedef typename identity_matrix::const_reference reference;
  2581. typedef typename identity_matrix::const_pointer pointer;
  2582. typedef const_iterator1 dual_iterator_type;
  2583. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  2584. // Construction and destruction
  2585. BOOST_UBLAS_INLINE
  2586. const_iterator2 ():
  2587. container_const_reference<self_type> (), it_ () {}
  2588. BOOST_UBLAS_INLINE
  2589. const_iterator2 (const self_type &m, const const_subiterator_type &it):
  2590. container_const_reference<self_type> (m), it_ (it) {}
  2591. // Arithmetic
  2592. BOOST_UBLAS_INLINE
  2593. const_iterator2 &operator ++ () {
  2594. BOOST_UBLAS_CHECK (it_ < (*this) ().size_common_, bad_index ());
  2595. ++it_;
  2596. return *this;
  2597. }
  2598. BOOST_UBLAS_INLINE
  2599. const_iterator2 &operator -- () {
  2600. BOOST_UBLAS_CHECK (it_ > 0, bad_index ());
  2601. --it_;
  2602. return *this;
  2603. }
  2604. // Dereference
  2605. BOOST_UBLAS_INLINE
  2606. const_reference operator * () const {
  2607. return one_;
  2608. }
  2609. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2610. BOOST_UBLAS_INLINE
  2611. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2612. typename self_type::
  2613. #endif
  2614. const_iterator1 begin () const {
  2615. return const_iterator1 ((*this) (), it_);
  2616. }
  2617. BOOST_UBLAS_INLINE
  2618. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2619. typename self_type::
  2620. #endif
  2621. const_iterator1 end () const {
  2622. return const_iterator1 ((*this) (), it_ + 1);
  2623. }
  2624. BOOST_UBLAS_INLINE
  2625. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2626. typename self_type::
  2627. #endif
  2628. const_reverse_iterator1 rbegin () const {
  2629. return const_reverse_iterator1 (end ());
  2630. }
  2631. BOOST_UBLAS_INLINE
  2632. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2633. typename self_type::
  2634. #endif
  2635. const_reverse_iterator1 rend () const {
  2636. return const_reverse_iterator1 (begin ());
  2637. }
  2638. #endif
  2639. // Indices
  2640. BOOST_UBLAS_INLINE
  2641. size_type index1 () const {
  2642. return it_;
  2643. }
  2644. BOOST_UBLAS_INLINE
  2645. size_type index2 () const {
  2646. return it_;
  2647. }
  2648. // Assignment
  2649. BOOST_UBLAS_INLINE
  2650. const_iterator2 &operator = (const const_iterator2 &it) {
  2651. container_const_reference<self_type>::assign (&it ());
  2652. it_ = it.it_;
  2653. return *this;
  2654. }
  2655. // Comparison
  2656. BOOST_UBLAS_INLINE
  2657. bool operator == (const const_iterator2 &it) const {
  2658. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2659. return it_ == it.it_;
  2660. }
  2661. private:
  2662. const_subiterator_type it_;
  2663. };
  2664. typedef const_iterator2 iterator2;
  2665. BOOST_UBLAS_INLINE
  2666. const_iterator2 begin2 () const {
  2667. return const_iterator2 (*this, 0);
  2668. }
  2669. BOOST_UBLAS_INLINE
  2670. const_iterator2 end2 () const {
  2671. return const_iterator2 (*this, size_common_);
  2672. }
  2673. // Reverse iterators
  2674. BOOST_UBLAS_INLINE
  2675. const_reverse_iterator1 rbegin1 () const {
  2676. return const_reverse_iterator1 (end1 ());
  2677. }
  2678. BOOST_UBLAS_INLINE
  2679. const_reverse_iterator1 rend1 () const {
  2680. return const_reverse_iterator1 (begin1 ());
  2681. }
  2682. BOOST_UBLAS_INLINE
  2683. const_reverse_iterator2 rbegin2 () const {
  2684. return const_reverse_iterator2 (end2 ());
  2685. }
  2686. BOOST_UBLAS_INLINE
  2687. const_reverse_iterator2 rend2 () const {
  2688. return const_reverse_iterator2 (begin2 ());
  2689. }
  2690. // Serialization
  2691. template<class Archive>
  2692. void serialize(Archive & ar, const unsigned int /* file_version */){
  2693. // we need to copy to a collection_size_type to get a portable
  2694. // and efficient serialization
  2695. serialization::collection_size_type s1 (size1_);
  2696. serialization::collection_size_type s2 (size2_);
  2697. // serialize the sizes
  2698. ar & serialization::make_nvp("size1",s1)
  2699. & serialization::make_nvp("size2",s2);
  2700. // copy the values back if loading
  2701. if (Archive::is_loading::value) {
  2702. size1_ = s1;
  2703. size2_ = s2;
  2704. size_common_ = ((std::min)(size1_, size2_));
  2705. }
  2706. }
  2707. private:
  2708. size_type size1_;
  2709. size_type size2_;
  2710. size_type size_common_;
  2711. static const value_type zero_;
  2712. static const value_type one_;
  2713. };
  2714. template<class T, class ALLOC>
  2715. const typename identity_matrix<T, ALLOC>::value_type identity_matrix<T, ALLOC>::zero_ = T(/*zero*/);
  2716. template<class T, class ALLOC>
  2717. const typename identity_matrix<T, ALLOC>::value_type identity_matrix<T, ALLOC>::one_ (1); // ISSUE: need 'one'-traits here
  2718. /** \brief A matrix with all values of type \c T equal to the same value
  2719. *
  2720. * Changing one value has the effect of changing all the values. Assigning it to a normal matrix will copy
  2721. * the same value everywhere in this matrix. All accesses are constant time, due to the trivial value.
  2722. *
  2723. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  2724. * \tparam ALLOC an allocator for storing the unique value. By default, a standar allocator is used.
  2725. */
  2726. template<class T, class ALLOC>
  2727. class scalar_matrix:
  2728. public matrix_container<scalar_matrix<T, ALLOC> > {
  2729. typedef const T *const_pointer;
  2730. typedef scalar_matrix<T, ALLOC> self_type;
  2731. public:
  2732. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  2733. using matrix_container<self_type>::operator ();
  2734. #endif
  2735. typedef std::size_t size_type;
  2736. typedef std::ptrdiff_t difference_type;
  2737. typedef T value_type;
  2738. typedef const T &const_reference;
  2739. typedef T &reference;
  2740. typedef const matrix_reference<const self_type> const_closure_type;
  2741. typedef matrix_reference<self_type> closure_type;
  2742. typedef dense_tag storage_category;
  2743. typedef unknown_orientation_tag orientation_category;
  2744. // Construction and destruction
  2745. BOOST_UBLAS_INLINE
  2746. scalar_matrix ():
  2747. matrix_container<self_type> (),
  2748. size1_ (0), size2_ (0), value_ () {}
  2749. BOOST_UBLAS_INLINE
  2750. scalar_matrix (size_type size1, size_type size2, const value_type &value = value_type(1)):
  2751. matrix_container<self_type> (),
  2752. size1_ (size1), size2_ (size2), value_ (value) {}
  2753. BOOST_UBLAS_INLINE
  2754. scalar_matrix (const scalar_matrix &m):
  2755. matrix_container<self_type> (),
  2756. size1_ (m.size1_), size2_ (m.size2_), value_ (m.value_) {}
  2757. // Accessors
  2758. BOOST_UBLAS_INLINE
  2759. size_type size1 () const {
  2760. return size1_;
  2761. }
  2762. BOOST_UBLAS_INLINE
  2763. size_type size2 () const {
  2764. return size2_;
  2765. }
  2766. // Resizing
  2767. BOOST_UBLAS_INLINE
  2768. void resize (size_type size1, size_type size2, bool /*preserve*/ = true) {
  2769. size1_ = size1;
  2770. size2_ = size2;
  2771. }
  2772. // Element access
  2773. BOOST_UBLAS_INLINE
  2774. const_reference operator () (size_type /*i*/, size_type /*j*/) const {
  2775. return value_;
  2776. }
  2777. // Assignment
  2778. BOOST_UBLAS_INLINE
  2779. scalar_matrix &operator = (const scalar_matrix &m) {
  2780. size1_ = m.size1_;
  2781. size2_ = m.size2_;
  2782. value_ = m.value_;
  2783. return *this;
  2784. }
  2785. BOOST_UBLAS_INLINE
  2786. scalar_matrix &assign_temporary (scalar_matrix &m) {
  2787. swap (m);
  2788. return *this;
  2789. }
  2790. // Swapping
  2791. BOOST_UBLAS_INLINE
  2792. void swap (scalar_matrix &m) {
  2793. if (this != &m) {
  2794. std::swap (size1_, m.size1_);
  2795. std::swap (size2_, m.size2_);
  2796. std::swap (value_, m.value_);
  2797. }
  2798. }
  2799. BOOST_UBLAS_INLINE
  2800. friend void swap (scalar_matrix &m1, scalar_matrix &m2) {
  2801. m1.swap (m2);
  2802. }
  2803. // Iterator types
  2804. private:
  2805. // Use an index
  2806. typedef size_type const_subiterator_type;
  2807. public:
  2808. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2809. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  2810. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  2811. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  2812. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  2813. #else
  2814. class const_iterator1;
  2815. class const_iterator2;
  2816. #endif
  2817. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  2818. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  2819. // Element lookup
  2820. BOOST_UBLAS_INLINE
  2821. const_iterator1 find1 (int /*rank*/, size_type i, size_type j) const {
  2822. return const_iterator1 (*this, i, j);
  2823. }
  2824. BOOST_UBLAS_INLINE
  2825. const_iterator2 find2 (int /*rank*/, size_type i, size_type j) const {
  2826. return const_iterator2 (*this, i, j);
  2827. }
  2828. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2829. class const_iterator1:
  2830. public container_const_reference<scalar_matrix>,
  2831. public random_access_iterator_base<dense_random_access_iterator_tag,
  2832. const_iterator1, value_type> {
  2833. public:
  2834. typedef typename scalar_matrix::value_type value_type;
  2835. typedef typename scalar_matrix::difference_type difference_type;
  2836. typedef typename scalar_matrix::const_reference reference;
  2837. typedef typename scalar_matrix::const_pointer pointer;
  2838. typedef const_iterator2 dual_iterator_type;
  2839. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  2840. // Construction and destruction
  2841. BOOST_UBLAS_INLINE
  2842. const_iterator1 ():
  2843. container_const_reference<scalar_matrix> (), it1_ (), it2_ () {}
  2844. BOOST_UBLAS_INLINE
  2845. const_iterator1 (const scalar_matrix &m, const const_subiterator_type &it1, const const_subiterator_type &it2):
  2846. container_const_reference<scalar_matrix> (m), it1_ (it1), it2_ (it2) {}
  2847. // Arithmetic
  2848. BOOST_UBLAS_INLINE
  2849. const_iterator1 &operator ++ () {
  2850. ++ it1_;
  2851. return *this;
  2852. }
  2853. BOOST_UBLAS_INLINE
  2854. const_iterator1 &operator -- () {
  2855. -- it1_;
  2856. return *this;
  2857. }
  2858. BOOST_UBLAS_INLINE
  2859. const_iterator1 &operator += (difference_type n) {
  2860. it1_ += n;
  2861. return *this;
  2862. }
  2863. BOOST_UBLAS_INLINE
  2864. const_iterator1 &operator -= (difference_type n) {
  2865. it1_ -= n;
  2866. return *this;
  2867. }
  2868. BOOST_UBLAS_INLINE
  2869. difference_type operator - (const const_iterator1 &it) const {
  2870. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2871. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  2872. return it1_ - it.it1_;
  2873. }
  2874. // Dereference
  2875. BOOST_UBLAS_INLINE
  2876. const_reference operator * () const {
  2877. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  2878. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  2879. return (*this) () (index1 (), index2 ());
  2880. }
  2881. BOOST_UBLAS_INLINE
  2882. const_reference operator [] (difference_type n) const {
  2883. return *(*this + n);
  2884. }
  2885. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  2886. BOOST_UBLAS_INLINE
  2887. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2888. typename self_type::
  2889. #endif
  2890. const_iterator2 begin () const {
  2891. const scalar_matrix &m = (*this) ();
  2892. return m.find2 (1, index1 (), 0);
  2893. }
  2894. BOOST_UBLAS_INLINE
  2895. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2896. typename self_type::
  2897. #endif
  2898. const_iterator2 end () const {
  2899. const scalar_matrix &m = (*this) ();
  2900. return m.find2 (1, index1 (), m.size2 ());
  2901. }
  2902. BOOST_UBLAS_INLINE
  2903. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2904. typename self_type::
  2905. #endif
  2906. const_reverse_iterator2 rbegin () const {
  2907. return const_reverse_iterator2 (end ());
  2908. }
  2909. BOOST_UBLAS_INLINE
  2910. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  2911. typename self_type::
  2912. #endif
  2913. const_reverse_iterator2 rend () const {
  2914. return const_reverse_iterator2 (begin ());
  2915. }
  2916. #endif
  2917. // Indices
  2918. BOOST_UBLAS_INLINE
  2919. size_type index1 () const {
  2920. return it1_;
  2921. }
  2922. BOOST_UBLAS_INLINE
  2923. size_type index2 () const {
  2924. return it2_;
  2925. }
  2926. // Assignment
  2927. BOOST_UBLAS_INLINE
  2928. const_iterator1 &operator = (const const_iterator1 &it) {
  2929. container_const_reference<scalar_matrix>::assign (&it ());
  2930. it1_ = it.it1_;
  2931. it2_ = it.it2_;
  2932. return *this;
  2933. }
  2934. // Comparison
  2935. BOOST_UBLAS_INLINE
  2936. bool operator == (const const_iterator1 &it) const {
  2937. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2938. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  2939. return it1_ == it.it1_;
  2940. }
  2941. BOOST_UBLAS_INLINE
  2942. bool operator < (const const_iterator1 &it) const {
  2943. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  2944. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  2945. return it1_ < it.it1_;
  2946. }
  2947. private:
  2948. const_subiterator_type it1_;
  2949. const_subiterator_type it2_;
  2950. };
  2951. typedef const_iterator1 iterator1;
  2952. #endif
  2953. BOOST_UBLAS_INLINE
  2954. const_iterator1 begin1 () const {
  2955. return find1 (0, 0, 0);
  2956. }
  2957. BOOST_UBLAS_INLINE
  2958. const_iterator1 end1 () const {
  2959. return find1 (0, size1_, 0);
  2960. }
  2961. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  2962. class const_iterator2:
  2963. public container_const_reference<scalar_matrix>,
  2964. public random_access_iterator_base<dense_random_access_iterator_tag,
  2965. const_iterator2, value_type> {
  2966. public:
  2967. typedef typename scalar_matrix::value_type value_type;
  2968. typedef typename scalar_matrix::difference_type difference_type;
  2969. typedef typename scalar_matrix::const_reference reference;
  2970. typedef typename scalar_matrix::const_pointer pointer;
  2971. typedef const_iterator1 dual_iterator_type;
  2972. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  2973. // Construction and destruction
  2974. BOOST_UBLAS_INLINE
  2975. const_iterator2 ():
  2976. container_const_reference<scalar_matrix> (), it1_ (), it2_ () {}
  2977. BOOST_UBLAS_INLINE
  2978. const_iterator2 (const scalar_matrix &m, const const_subiterator_type &it1, const const_subiterator_type &it2):
  2979. container_const_reference<scalar_matrix> (m), it1_ (it1), it2_ (it2) {}
  2980. // Arithmetic
  2981. BOOST_UBLAS_INLINE
  2982. const_iterator2 &operator ++ () {
  2983. ++ it2_;
  2984. return *this;
  2985. }
  2986. BOOST_UBLAS_INLINE
  2987. const_iterator2 &operator -- () {
  2988. -- it2_;
  2989. return *this;
  2990. }
  2991. BOOST_UBLAS_INLINE
  2992. const_iterator2 &operator += (difference_type n) {
  2993. it2_ += n;
  2994. return *this;
  2995. }
  2996. BOOST_UBLAS_INLINE
  2997. const_iterator2 &operator -= (difference_type n) {
  2998. it2_ -= n;
  2999. return *this;
  3000. }
  3001. BOOST_UBLAS_INLINE
  3002. difference_type operator - (const const_iterator2 &it) const {
  3003. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3004. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  3005. return it2_ - it.it2_;
  3006. }
  3007. // Dereference
  3008. BOOST_UBLAS_INLINE
  3009. const_reference operator * () const {
  3010. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3011. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3012. return (*this) () (index1 (), index2 ());
  3013. }
  3014. BOOST_UBLAS_INLINE
  3015. const_reference operator [] (difference_type n) const {
  3016. return *(*this + n);
  3017. }
  3018. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3019. BOOST_UBLAS_INLINE
  3020. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3021. typename self_type::
  3022. #endif
  3023. const_iterator1 begin () const {
  3024. const scalar_matrix &m = (*this) ();
  3025. return m.find1 (1, 0, index2 ());
  3026. }
  3027. BOOST_UBLAS_INLINE
  3028. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3029. typename self_type::
  3030. #endif
  3031. const_iterator1 end () const {
  3032. const scalar_matrix &m = (*this) ();
  3033. return m.find1 (1, m.size1 (), index2 ());
  3034. }
  3035. BOOST_UBLAS_INLINE
  3036. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3037. typename self_type::
  3038. #endif
  3039. const_reverse_iterator1 rbegin () const {
  3040. return const_reverse_iterator1 (end ());
  3041. }
  3042. BOOST_UBLAS_INLINE
  3043. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3044. typename self_type::
  3045. #endif
  3046. const_reverse_iterator1 rend () const {
  3047. return const_reverse_iterator1 (begin ());
  3048. }
  3049. #endif
  3050. // Indices
  3051. BOOST_UBLAS_INLINE
  3052. size_type index1 () const {
  3053. return it1_;
  3054. }
  3055. BOOST_UBLAS_INLINE
  3056. size_type index2 () const {
  3057. return it2_;
  3058. }
  3059. // Assignment
  3060. BOOST_UBLAS_INLINE
  3061. const_iterator2 &operator = (const const_iterator2 &it) {
  3062. container_const_reference<scalar_matrix>::assign (&it ());
  3063. it1_ = it.it1_;
  3064. it2_ = it.it2_;
  3065. return *this;
  3066. }
  3067. // Comparison
  3068. BOOST_UBLAS_INLINE
  3069. bool operator == (const const_iterator2 &it) const {
  3070. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3071. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  3072. return it2_ == it.it2_;
  3073. }
  3074. BOOST_UBLAS_INLINE
  3075. bool operator < (const const_iterator2 &it) const {
  3076. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3077. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  3078. return it2_ < it.it2_;
  3079. }
  3080. private:
  3081. const_subiterator_type it1_;
  3082. const_subiterator_type it2_;
  3083. };
  3084. typedef const_iterator2 iterator2;
  3085. #endif
  3086. BOOST_UBLAS_INLINE
  3087. const_iterator2 begin2 () const {
  3088. return find2 (0, 0, 0);
  3089. }
  3090. BOOST_UBLAS_INLINE
  3091. const_iterator2 end2 () const {
  3092. return find2 (0, 0, size2_);
  3093. }
  3094. // Reverse iterators
  3095. BOOST_UBLAS_INLINE
  3096. const_reverse_iterator1 rbegin1 () const {
  3097. return const_reverse_iterator1 (end1 ());
  3098. }
  3099. BOOST_UBLAS_INLINE
  3100. const_reverse_iterator1 rend1 () const {
  3101. return const_reverse_iterator1 (begin1 ());
  3102. }
  3103. BOOST_UBLAS_INLINE
  3104. const_reverse_iterator2 rbegin2 () const {
  3105. return const_reverse_iterator2 (end2 ());
  3106. }
  3107. BOOST_UBLAS_INLINE
  3108. const_reverse_iterator2 rend2 () const {
  3109. return const_reverse_iterator2 (begin2 ());
  3110. }
  3111. // Serialization
  3112. template<class Archive>
  3113. void serialize(Archive & ar, const unsigned int /* file_version */){
  3114. // we need to copy to a collection_size_type to get a portable
  3115. // and efficient serialization
  3116. serialization::collection_size_type s1 (size1_);
  3117. serialization::collection_size_type s2 (size2_);
  3118. // serialize the sizes
  3119. ar & serialization::make_nvp("size1",s1)
  3120. & serialization::make_nvp("size2",s2);
  3121. // copy the values back if loading
  3122. if (Archive::is_loading::value) {
  3123. size1_ = s1;
  3124. size2_ = s2;
  3125. }
  3126. ar & serialization::make_nvp("value", value_);
  3127. }
  3128. private:
  3129. size_type size1_;
  3130. size_type size2_;
  3131. value_type value_;
  3132. };
  3133. /** \brief An array based matrix class which size is defined at type specification or object instanciation
  3134. *
  3135. * This matrix is directly based on a predefined C-style arry of data, thus providing the fastest
  3136. * implementation possible. The constraint is that dimensions of the matrix must be specified at
  3137. * the instanciation or the type specification.
  3138. *
  3139. * For instance, \code typedef c_matrix<double,4,4> my_4by4_matrix \endcode
  3140. * defines a 4 by 4 double-precision matrix. You can also instantiate it directly with
  3141. * \code c_matrix<int,8,5> my_fast_matrix \endcode. This will make a 8 by 5 integer matrix. The
  3142. * price to pay for this speed is that you cannot resize it to a size larger than the one defined
  3143. * in the template parameters. In the previous example, a size of 4 by 5 or 3 by 2 is acceptable,
  3144. * but a new size of 9 by 5 or even 10 by 10 will raise a bad_size() exception.
  3145. *
  3146. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  3147. * \tparam N the default maximum number of rows
  3148. * \tparam M the default maximum number of columns
  3149. */
  3150. template<class T, std::size_t N, std::size_t M>
  3151. class c_matrix:
  3152. public matrix_container<c_matrix<T, N, M> > {
  3153. typedef c_matrix<T, N, M> self_type;
  3154. public:
  3155. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  3156. using matrix_container<self_type>::operator ();
  3157. #endif
  3158. typedef std::size_t size_type;
  3159. typedef std::ptrdiff_t difference_type;
  3160. typedef T value_type;
  3161. typedef const T &const_reference;
  3162. typedef T &reference;
  3163. typedef const T *const_pointer;
  3164. typedef T *pointer;
  3165. typedef const matrix_reference<const self_type> const_closure_type;
  3166. typedef matrix_reference<self_type> closure_type;
  3167. typedef c_vector<T, N * M> vector_temporary_type; // vector able to store all elements of c_matrix
  3168. typedef self_type matrix_temporary_type;
  3169. typedef dense_tag storage_category;
  3170. // This could be better for performance,
  3171. // typedef typename unknown_orientation_tag orientation_category;
  3172. // but others depend on the orientation information...
  3173. typedef row_major_tag orientation_category;
  3174. // Construction and destruction
  3175. BOOST_UBLAS_INLINE
  3176. c_matrix ():
  3177. size1_ (N), size2_ (M) /* , data_ () */ {
  3178. }
  3179. BOOST_UBLAS_INLINE
  3180. c_matrix (size_type size1, size_type size2):
  3181. size1_ (size1), size2_ (size2) /* , data_ () */ {
  3182. if (size1_ > N || size2_ > M)
  3183. bad_size ().raise ();
  3184. }
  3185. BOOST_UBLAS_INLINE
  3186. c_matrix (const c_matrix &m):
  3187. size1_ (m.size1_), size2_ (m.size2_) /* , data_ () */ {
  3188. if (size1_ > N || size2_ > M)
  3189. bad_size ().raise ();
  3190. assign(m);
  3191. }
  3192. template<class AE>
  3193. BOOST_UBLAS_INLINE
  3194. c_matrix (const matrix_expression<AE> &ae):
  3195. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()) /* , data_ () */ {
  3196. if (size1_ > N || size2_ > M)
  3197. bad_size ().raise ();
  3198. matrix_assign<scalar_assign> (*this, ae);
  3199. }
  3200. // Accessors
  3201. BOOST_UBLAS_INLINE
  3202. size_type size1 () const {
  3203. return size1_;
  3204. }
  3205. BOOST_UBLAS_INLINE
  3206. size_type size2 () const {
  3207. return size2_;
  3208. }
  3209. BOOST_UBLAS_INLINE
  3210. const_pointer data () const {
  3211. return reinterpret_cast<const_pointer> (data_);
  3212. }
  3213. BOOST_UBLAS_INLINE
  3214. pointer data () {
  3215. return reinterpret_cast<pointer> (data_);
  3216. }
  3217. // Resizing
  3218. BOOST_UBLAS_INLINE
  3219. void resize (size_type size1, size_type size2, bool preserve = true) {
  3220. if (size1 > N || size2 > M)
  3221. bad_size ().raise ();
  3222. if (preserve) {
  3223. self_type temporary (size1, size2);
  3224. // Common elements to preserve
  3225. const size_type size1_min = (std::min) (size1, size1_);
  3226. const size_type size2_min = (std::min) (size2, size2_);
  3227. for (size_type i = 0; i != size1_min; ++i) { // indexing copy over major
  3228. for (size_type j = 0; j != size2_min; ++j) {
  3229. temporary.data_[i][j] = data_[i][j];
  3230. }
  3231. }
  3232. assign_temporary (temporary);
  3233. }
  3234. else {
  3235. size1_ = size1;
  3236. size2_ = size2;
  3237. }
  3238. }
  3239. // Element access
  3240. BOOST_UBLAS_INLINE
  3241. const_reference operator () (size_type i, size_type j) const {
  3242. BOOST_UBLAS_CHECK (i < size1_, bad_index ());
  3243. BOOST_UBLAS_CHECK (j < size2_, bad_index ());
  3244. return data_ [i] [j];
  3245. }
  3246. BOOST_UBLAS_INLINE
  3247. reference at_element (size_type i, size_type j) {
  3248. BOOST_UBLAS_CHECK (i < size1_, bad_index ());
  3249. BOOST_UBLAS_CHECK (j < size2_, bad_index ());
  3250. return data_ [i] [j];
  3251. }
  3252. BOOST_UBLAS_INLINE
  3253. reference operator () (size_type i, size_type j) {
  3254. return at_element (i, j);
  3255. }
  3256. // Element assignment
  3257. BOOST_UBLAS_INLINE
  3258. reference insert_element (size_type i, size_type j, const_reference t) {
  3259. return (at_element (i, j) = t);
  3260. }
  3261. // Zeroing
  3262. BOOST_UBLAS_INLINE
  3263. void clear () {
  3264. for (size_type i = 0; i < size1_; ++ i)
  3265. std::fill (data_ [i], data_ [i] + size2_, value_type/*zero*/());
  3266. }
  3267. // Assignment
  3268. #ifdef BOOST_UBLAS_MOVE_SEMANTICS
  3269. /*! @note "pass by value" the key idea to enable move semantics */
  3270. BOOST_UBLAS_INLINE
  3271. c_matrix &operator = (c_matrix m) {
  3272. assign_temporary(m);
  3273. return *this;
  3274. }
  3275. #else
  3276. BOOST_UBLAS_INLINE
  3277. c_matrix &operator = (const c_matrix &m) {
  3278. size1_ = m.size1_;
  3279. size2_ = m.size2_;
  3280. for (size_type i = 0; i < m.size1_; ++ i)
  3281. std::copy (m.data_ [i], m.data_ [i] + m.size2_, data_ [i]);
  3282. return *this;
  3283. }
  3284. #endif
  3285. template<class C> // Container assignment without temporary
  3286. BOOST_UBLAS_INLINE
  3287. c_matrix &operator = (const matrix_container<C> &m) {
  3288. resize (m ().size1 (), m ().size2 (), false);
  3289. assign (m);
  3290. return *this;
  3291. }
  3292. BOOST_UBLAS_INLINE
  3293. c_matrix &assign_temporary (c_matrix &m) {
  3294. swap (m);
  3295. return *this;
  3296. }
  3297. template<class AE>
  3298. BOOST_UBLAS_INLINE
  3299. c_matrix &operator = (const matrix_expression<AE> &ae) {
  3300. self_type temporary (ae);
  3301. return assign_temporary (temporary);
  3302. }
  3303. template<class AE>
  3304. BOOST_UBLAS_INLINE
  3305. c_matrix &assign (const matrix_expression<AE> &ae) {
  3306. matrix_assign<scalar_assign> (*this, ae);
  3307. return *this;
  3308. }
  3309. template<class AE>
  3310. BOOST_UBLAS_INLINE
  3311. c_matrix& operator += (const matrix_expression<AE> &ae) {
  3312. self_type temporary (*this + ae);
  3313. return assign_temporary (temporary);
  3314. }
  3315. template<class C> // Container assignment without temporary
  3316. BOOST_UBLAS_INLINE
  3317. c_matrix &operator += (const matrix_container<C> &m) {
  3318. plus_assign (m);
  3319. return *this;
  3320. }
  3321. template<class AE>
  3322. BOOST_UBLAS_INLINE
  3323. c_matrix &plus_assign (const matrix_expression<AE> &ae) {
  3324. matrix_assign<scalar_plus_assign> (*this, ae);
  3325. return *this;
  3326. }
  3327. template<class AE>
  3328. BOOST_UBLAS_INLINE
  3329. c_matrix& operator -= (const matrix_expression<AE> &ae) {
  3330. self_type temporary (*this - ae);
  3331. return assign_temporary (temporary);
  3332. }
  3333. template<class C> // Container assignment without temporary
  3334. BOOST_UBLAS_INLINE
  3335. c_matrix &operator -= (const matrix_container<C> &m) {
  3336. minus_assign (m);
  3337. return *this;
  3338. }
  3339. template<class AE>
  3340. BOOST_UBLAS_INLINE
  3341. c_matrix &minus_assign (const matrix_expression<AE> &ae) {
  3342. matrix_assign<scalar_minus_assign> (*this, ae);
  3343. return *this;
  3344. }
  3345. template<class AT>
  3346. BOOST_UBLAS_INLINE
  3347. c_matrix& operator *= (const AT &at) {
  3348. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  3349. return *this;
  3350. }
  3351. template<class AT>
  3352. BOOST_UBLAS_INLINE
  3353. c_matrix& operator /= (const AT &at) {
  3354. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  3355. return *this;
  3356. }
  3357. // Swapping
  3358. BOOST_UBLAS_INLINE
  3359. void swap (c_matrix &m) {
  3360. if (this != &m) {
  3361. BOOST_UBLAS_CHECK (size1_ == m.size1_, bad_size ());
  3362. BOOST_UBLAS_CHECK (size2_ == m.size2_, bad_size ());
  3363. std::swap (size1_, m.size1_);
  3364. std::swap (size2_, m.size2_);
  3365. for (size_type i = 0; i < size1_; ++ i)
  3366. std::swap_ranges (data_ [i], data_ [i] + size2_, m.data_ [i]);
  3367. }
  3368. }
  3369. BOOST_UBLAS_INLINE
  3370. friend void swap (c_matrix &m1, c_matrix &m2) {
  3371. m1.swap (m2);
  3372. }
  3373. // Iterator types
  3374. private:
  3375. // Use pointers for iterator
  3376. typedef const_pointer const_subiterator_type;
  3377. typedef pointer subiterator_type;
  3378. public:
  3379. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3380. typedef indexed_iterator1<self_type, dense_random_access_iterator_tag> iterator1;
  3381. typedef indexed_iterator2<self_type, dense_random_access_iterator_tag> iterator2;
  3382. typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
  3383. typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
  3384. #else
  3385. class const_iterator1;
  3386. class iterator1;
  3387. class const_iterator2;
  3388. class iterator2;
  3389. #endif
  3390. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  3391. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  3392. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  3393. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  3394. // Element lookup
  3395. BOOST_UBLAS_INLINE
  3396. const_iterator1 find1 (int rank, size_type i, size_type j) const {
  3397. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3398. return const_iterator1 (*this, i, j);
  3399. #else
  3400. return const_iterator1 (*this, &data_ [i] [j]);
  3401. #endif
  3402. }
  3403. BOOST_UBLAS_INLINE
  3404. iterator1 find1 (int rank, size_type i, size_type j) {
  3405. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3406. return iterator1 (*this, i, j);
  3407. #else
  3408. return iterator1 (*this, &data_ [i] [j]);
  3409. #endif
  3410. }
  3411. BOOST_UBLAS_INLINE
  3412. const_iterator2 find2 (int rank, size_type i, size_type j) const {
  3413. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3414. return const_iterator2 (*this, i, j);
  3415. #else
  3416. return const_iterator2 (*this, &data_ [i] [j]);
  3417. #endif
  3418. }
  3419. BOOST_UBLAS_INLINE
  3420. iterator2 find2 (int rank, size_type i, size_type j) {
  3421. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3422. return iterator2 (*this, i, j);
  3423. #else
  3424. return iterator2 (*this, &data_ [i] [j]);
  3425. #endif
  3426. }
  3427. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3428. class const_iterator1:
  3429. public container_const_reference<c_matrix>,
  3430. public random_access_iterator_base<dense_random_access_iterator_tag,
  3431. const_iterator1, value_type> {
  3432. public:
  3433. typedef typename c_matrix::difference_type difference_type;
  3434. typedef typename c_matrix::value_type value_type;
  3435. typedef typename c_matrix::const_reference reference;
  3436. typedef typename c_matrix::const_pointer pointer;
  3437. typedef const_iterator2 dual_iterator_type;
  3438. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  3439. // Construction and destruction
  3440. BOOST_UBLAS_INLINE
  3441. const_iterator1 ():
  3442. container_const_reference<self_type> (), it_ () {}
  3443. BOOST_UBLAS_INLINE
  3444. const_iterator1 (const self_type &m, const const_subiterator_type &it):
  3445. container_const_reference<self_type> (m), it_ (it) {}
  3446. BOOST_UBLAS_INLINE
  3447. const_iterator1 (const iterator1 &it):
  3448. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  3449. // Arithmetic
  3450. BOOST_UBLAS_INLINE
  3451. const_iterator1 &operator ++ () {
  3452. it_ += M;
  3453. return *this;
  3454. }
  3455. BOOST_UBLAS_INLINE
  3456. const_iterator1 &operator -- () {
  3457. it_ -= M;
  3458. return *this;
  3459. }
  3460. BOOST_UBLAS_INLINE
  3461. const_iterator1 &operator += (difference_type n) {
  3462. it_ += n * M;
  3463. return *this;
  3464. }
  3465. BOOST_UBLAS_INLINE
  3466. const_iterator1 &operator -= (difference_type n) {
  3467. it_ -= n * M;
  3468. return *this;
  3469. }
  3470. BOOST_UBLAS_INLINE
  3471. difference_type operator - (const const_iterator1 &it) const {
  3472. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3473. return (it_ - it.it_) / M;
  3474. }
  3475. // Dereference
  3476. BOOST_UBLAS_INLINE
  3477. const_reference operator * () const {
  3478. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3479. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3480. return *it_;
  3481. }
  3482. BOOST_UBLAS_INLINE
  3483. const_reference operator [] (difference_type n) const {
  3484. return *(*this + n);
  3485. }
  3486. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3487. BOOST_UBLAS_INLINE
  3488. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3489. typename self_type::
  3490. #endif
  3491. const_iterator2 begin () const {
  3492. const self_type &m = (*this) ();
  3493. return m.find2 (1, index1 (), 0);
  3494. }
  3495. BOOST_UBLAS_INLINE
  3496. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3497. typename self_type::
  3498. #endif
  3499. const_iterator2 end () const {
  3500. const self_type &m = (*this) ();
  3501. return m.find2 (1, index1 (), m.size2 ());
  3502. }
  3503. BOOST_UBLAS_INLINE
  3504. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3505. typename self_type::
  3506. #endif
  3507. const_reverse_iterator2 rbegin () const {
  3508. return const_reverse_iterator2 (end ());
  3509. }
  3510. BOOST_UBLAS_INLINE
  3511. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3512. typename self_type::
  3513. #endif
  3514. const_reverse_iterator2 rend () const {
  3515. return const_reverse_iterator2 (begin ());
  3516. }
  3517. #endif
  3518. // Indices
  3519. BOOST_UBLAS_INLINE
  3520. size_type index1 () const {
  3521. const self_type &m = (*this) ();
  3522. return (it_ - m.begin1 ().it_) / M;
  3523. }
  3524. BOOST_UBLAS_INLINE
  3525. size_type index2 () const {
  3526. const self_type &m = (*this) ();
  3527. return (it_ - m.begin1 ().it_) % M;
  3528. }
  3529. // Assignment
  3530. BOOST_UBLAS_INLINE
  3531. const_iterator1 &operator = (const const_iterator1 &it) {
  3532. container_const_reference<self_type>::assign (&it ());
  3533. it_ = it.it_;
  3534. return *this;
  3535. }
  3536. // Comparison
  3537. BOOST_UBLAS_INLINE
  3538. bool operator == (const const_iterator1 &it) const {
  3539. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3540. return it_ == it.it_;
  3541. }
  3542. BOOST_UBLAS_INLINE
  3543. bool operator < (const const_iterator1 &it) const {
  3544. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3545. return it_ < it.it_;
  3546. }
  3547. private:
  3548. const_subiterator_type it_;
  3549. friend class iterator1;
  3550. };
  3551. #endif
  3552. BOOST_UBLAS_INLINE
  3553. const_iterator1 begin1 () const {
  3554. return find1 (0, 0, 0);
  3555. }
  3556. BOOST_UBLAS_INLINE
  3557. const_iterator1 end1 () const {
  3558. return find1 (0, size1_, 0);
  3559. }
  3560. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3561. class iterator1:
  3562. public container_reference<c_matrix>,
  3563. public random_access_iterator_base<dense_random_access_iterator_tag,
  3564. iterator1, value_type> {
  3565. public:
  3566. typedef typename c_matrix::difference_type difference_type;
  3567. typedef typename c_matrix::value_type value_type;
  3568. typedef typename c_matrix::reference reference;
  3569. typedef typename c_matrix::pointer pointer;
  3570. typedef iterator2 dual_iterator_type;
  3571. typedef reverse_iterator2 dual_reverse_iterator_type;
  3572. // Construction and destruction
  3573. BOOST_UBLAS_INLINE
  3574. iterator1 ():
  3575. container_reference<self_type> (), it_ () {}
  3576. BOOST_UBLAS_INLINE
  3577. iterator1 (self_type &m, const subiterator_type &it):
  3578. container_reference<self_type> (m), it_ (it) {}
  3579. // Arithmetic
  3580. BOOST_UBLAS_INLINE
  3581. iterator1 &operator ++ () {
  3582. it_ += M;
  3583. return *this;
  3584. }
  3585. BOOST_UBLAS_INLINE
  3586. iterator1 &operator -- () {
  3587. it_ -= M;
  3588. return *this;
  3589. }
  3590. BOOST_UBLAS_INLINE
  3591. iterator1 &operator += (difference_type n) {
  3592. it_ += n * M;
  3593. return *this;
  3594. }
  3595. BOOST_UBLAS_INLINE
  3596. iterator1 &operator -= (difference_type n) {
  3597. it_ -= n * M;
  3598. return *this;
  3599. }
  3600. BOOST_UBLAS_INLINE
  3601. difference_type operator - (const iterator1 &it) const {
  3602. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3603. return (it_ - it.it_) / M;
  3604. }
  3605. // Dereference
  3606. BOOST_UBLAS_INLINE
  3607. reference operator * () const {
  3608. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3609. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3610. return *it_;
  3611. }
  3612. BOOST_UBLAS_INLINE
  3613. reference operator [] (difference_type n) const {
  3614. return *(*this + n);
  3615. }
  3616. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3617. BOOST_UBLAS_INLINE
  3618. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3619. typename self_type::
  3620. #endif
  3621. iterator2 begin () const {
  3622. self_type &m = (*this) ();
  3623. return m.find2 (1, index1 (), 0);
  3624. }
  3625. BOOST_UBLAS_INLINE
  3626. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3627. typename self_type::
  3628. #endif
  3629. iterator2 end () const {
  3630. self_type &m = (*this) ();
  3631. return m.find2 (1, index1 (), m.size2 ());
  3632. }
  3633. BOOST_UBLAS_INLINE
  3634. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3635. typename self_type::
  3636. #endif
  3637. reverse_iterator2 rbegin () const {
  3638. return reverse_iterator2 (end ());
  3639. }
  3640. BOOST_UBLAS_INLINE
  3641. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3642. typename self_type::
  3643. #endif
  3644. reverse_iterator2 rend () const {
  3645. return reverse_iterator2 (begin ());
  3646. }
  3647. #endif
  3648. // Indices
  3649. BOOST_UBLAS_INLINE
  3650. size_type index1 () const {
  3651. const self_type &m = (*this) ();
  3652. return (it_ - m.begin1 ().it_) / M;
  3653. }
  3654. BOOST_UBLAS_INLINE
  3655. size_type index2 () const {
  3656. const self_type &m = (*this) ();
  3657. return (it_ - m.begin1 ().it_) % M;
  3658. }
  3659. // Assignment
  3660. BOOST_UBLAS_INLINE
  3661. iterator1 &operator = (const iterator1 &it) {
  3662. container_reference<self_type>::assign (&it ());
  3663. it_ = it.it_;
  3664. return *this;
  3665. }
  3666. // Comparison
  3667. BOOST_UBLAS_INLINE
  3668. bool operator == (const iterator1 &it) const {
  3669. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3670. return it_ == it.it_;
  3671. }
  3672. BOOST_UBLAS_INLINE
  3673. bool operator < (const iterator1 &it) const {
  3674. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3675. return it_ < it.it_;
  3676. }
  3677. private:
  3678. subiterator_type it_;
  3679. friend class const_iterator1;
  3680. };
  3681. #endif
  3682. BOOST_UBLAS_INLINE
  3683. iterator1 begin1 () {
  3684. return find1 (0, 0, 0);
  3685. }
  3686. BOOST_UBLAS_INLINE
  3687. iterator1 end1 () {
  3688. return find1 (0, size1_, 0);
  3689. }
  3690. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3691. class const_iterator2:
  3692. public container_const_reference<c_matrix>,
  3693. public random_access_iterator_base<dense_random_access_iterator_tag,
  3694. const_iterator2, value_type> {
  3695. public:
  3696. typedef typename c_matrix::difference_type difference_type;
  3697. typedef typename c_matrix::value_type value_type;
  3698. typedef typename c_matrix::const_reference reference;
  3699. typedef typename c_matrix::const_reference pointer;
  3700. typedef const_iterator1 dual_iterator_type;
  3701. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  3702. // Construction and destruction
  3703. BOOST_UBLAS_INLINE
  3704. const_iterator2 ():
  3705. container_const_reference<self_type> (), it_ () {}
  3706. BOOST_UBLAS_INLINE
  3707. const_iterator2 (const self_type &m, const const_subiterator_type &it):
  3708. container_const_reference<self_type> (m), it_ (it) {}
  3709. BOOST_UBLAS_INLINE
  3710. const_iterator2 (const iterator2 &it):
  3711. container_const_reference<self_type> (it ()), it_ (it.it_) {}
  3712. // Arithmetic
  3713. BOOST_UBLAS_INLINE
  3714. const_iterator2 &operator ++ () {
  3715. ++ it_;
  3716. return *this;
  3717. }
  3718. BOOST_UBLAS_INLINE
  3719. const_iterator2 &operator -- () {
  3720. -- it_;
  3721. return *this;
  3722. }
  3723. BOOST_UBLAS_INLINE
  3724. const_iterator2 &operator += (difference_type n) {
  3725. it_ += n;
  3726. return *this;
  3727. }
  3728. BOOST_UBLAS_INLINE
  3729. const_iterator2 &operator -= (difference_type n) {
  3730. it_ -= n;
  3731. return *this;
  3732. }
  3733. BOOST_UBLAS_INLINE
  3734. difference_type operator - (const const_iterator2 &it) const {
  3735. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3736. return it_ - it.it_;
  3737. }
  3738. // Dereference
  3739. BOOST_UBLAS_INLINE
  3740. const_reference operator * () const {
  3741. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3742. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3743. return *it_;
  3744. }
  3745. BOOST_UBLAS_INLINE
  3746. const_reference operator [] (difference_type n) const {
  3747. return *(*this + n);
  3748. }
  3749. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3750. BOOST_UBLAS_INLINE
  3751. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3752. typename self_type::
  3753. #endif
  3754. const_iterator1 begin () const {
  3755. const self_type &m = (*this) ();
  3756. return m.find1 (1, 0, index2 ());
  3757. }
  3758. BOOST_UBLAS_INLINE
  3759. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3760. typename self_type::
  3761. #endif
  3762. const_iterator1 end () const {
  3763. const self_type &m = (*this) ();
  3764. return m.find1 (1, m.size1 (), index2 ());
  3765. }
  3766. BOOST_UBLAS_INLINE
  3767. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3768. typename self_type::
  3769. #endif
  3770. const_reverse_iterator1 rbegin () const {
  3771. return const_reverse_iterator1 (end ());
  3772. }
  3773. BOOST_UBLAS_INLINE
  3774. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3775. typename self_type::
  3776. #endif
  3777. const_reverse_iterator1 rend () const {
  3778. return const_reverse_iterator1 (begin ());
  3779. }
  3780. #endif
  3781. // Indices
  3782. BOOST_UBLAS_INLINE
  3783. size_type index1 () const {
  3784. const self_type &m = (*this) ();
  3785. return (it_ - m.begin2 ().it_) / M;
  3786. }
  3787. BOOST_UBLAS_INLINE
  3788. size_type index2 () const {
  3789. const self_type &m = (*this) ();
  3790. return (it_ - m.begin2 ().it_) % M;
  3791. }
  3792. // Assignment
  3793. BOOST_UBLAS_INLINE
  3794. const_iterator2 &operator = (const const_iterator2 &it) {
  3795. container_const_reference<self_type>::assign (&it ());
  3796. it_ = it.it_;
  3797. return *this;
  3798. }
  3799. // Comparison
  3800. BOOST_UBLAS_INLINE
  3801. bool operator == (const const_iterator2 &it) const {
  3802. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3803. return it_ == it.it_;
  3804. }
  3805. BOOST_UBLAS_INLINE
  3806. bool operator < (const const_iterator2 &it) const {
  3807. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3808. return it_ < it.it_;
  3809. }
  3810. private:
  3811. const_subiterator_type it_;
  3812. friend class iterator2;
  3813. };
  3814. #endif
  3815. BOOST_UBLAS_INLINE
  3816. const_iterator2 begin2 () const {
  3817. return find2 (0, 0, 0);
  3818. }
  3819. BOOST_UBLAS_INLINE
  3820. const_iterator2 end2 () const {
  3821. return find2 (0, 0, size2_);
  3822. }
  3823. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  3824. class iterator2:
  3825. public container_reference<c_matrix>,
  3826. public random_access_iterator_base<dense_random_access_iterator_tag,
  3827. iterator2, value_type> {
  3828. public:
  3829. typedef typename c_matrix::difference_type difference_type;
  3830. typedef typename c_matrix::value_type value_type;
  3831. typedef typename c_matrix::reference reference;
  3832. typedef typename c_matrix::pointer pointer;
  3833. typedef iterator1 dual_iterator_type;
  3834. typedef reverse_iterator1 dual_reverse_iterator_type;
  3835. // Construction and destruction
  3836. BOOST_UBLAS_INLINE
  3837. iterator2 ():
  3838. container_reference<self_type> (), it_ () {}
  3839. BOOST_UBLAS_INLINE
  3840. iterator2 (self_type &m, const subiterator_type &it):
  3841. container_reference<self_type> (m), it_ (it) {}
  3842. // Arithmetic
  3843. BOOST_UBLAS_INLINE
  3844. iterator2 &operator ++ () {
  3845. ++ it_;
  3846. return *this;
  3847. }
  3848. BOOST_UBLAS_INLINE
  3849. iterator2 &operator -- () {
  3850. -- it_;
  3851. return *this;
  3852. }
  3853. BOOST_UBLAS_INLINE
  3854. iterator2 &operator += (difference_type n) {
  3855. it_ += n;
  3856. return *this;
  3857. }
  3858. BOOST_UBLAS_INLINE
  3859. iterator2 &operator -= (difference_type n) {
  3860. it_ -= n;
  3861. return *this;
  3862. }
  3863. BOOST_UBLAS_INLINE
  3864. difference_type operator - (const iterator2 &it) const {
  3865. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3866. return it_ - it.it_;
  3867. }
  3868. // Dereference
  3869. BOOST_UBLAS_INLINE
  3870. reference operator * () const {
  3871. BOOST_UBLAS_CHECK (index1 () < (*this) ().size1 (), bad_index ());
  3872. BOOST_UBLAS_CHECK (index2 () < (*this) ().size2 (), bad_index ());
  3873. return *it_;
  3874. }
  3875. BOOST_UBLAS_INLINE
  3876. reference operator [] (difference_type n) const {
  3877. return *(*this + n);
  3878. }
  3879. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  3880. BOOST_UBLAS_INLINE
  3881. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3882. typename self_type::
  3883. #endif
  3884. iterator1 begin () const {
  3885. self_type &m = (*this) ();
  3886. return m.find1 (1, 0, index2 ());
  3887. }
  3888. BOOST_UBLAS_INLINE
  3889. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3890. typename self_type::
  3891. #endif
  3892. iterator1 end () const {
  3893. self_type &m = (*this) ();
  3894. return m.find1 (1, m.size1 (), index2 ());
  3895. }
  3896. BOOST_UBLAS_INLINE
  3897. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3898. typename self_type::
  3899. #endif
  3900. reverse_iterator1 rbegin () const {
  3901. return reverse_iterator1 (end ());
  3902. }
  3903. BOOST_UBLAS_INLINE
  3904. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  3905. typename self_type::
  3906. #endif
  3907. reverse_iterator1 rend () const {
  3908. return reverse_iterator1 (begin ());
  3909. }
  3910. #endif
  3911. // Indices
  3912. BOOST_UBLAS_INLINE
  3913. size_type index1 () const {
  3914. const self_type &m = (*this) ();
  3915. return (it_ - m.begin2 ().it_) / M;
  3916. }
  3917. BOOST_UBLAS_INLINE
  3918. size_type index2 () const {
  3919. const self_type &m = (*this) ();
  3920. return (it_ - m.begin2 ().it_) % M;
  3921. }
  3922. // Assignment
  3923. BOOST_UBLAS_INLINE
  3924. iterator2 &operator = (const iterator2 &it) {
  3925. container_reference<self_type>::assign (&it ());
  3926. it_ = it.it_;
  3927. return *this;
  3928. }
  3929. // Comparison
  3930. BOOST_UBLAS_INLINE
  3931. bool operator == (const iterator2 &it) const {
  3932. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3933. return it_ == it.it_;
  3934. }
  3935. BOOST_UBLAS_INLINE
  3936. bool operator < (const iterator2 &it) const {
  3937. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  3938. return it_ < it.it_;
  3939. }
  3940. private:
  3941. subiterator_type it_;
  3942. friend class const_iterator2;
  3943. };
  3944. #endif
  3945. BOOST_UBLAS_INLINE
  3946. iterator2 begin2 () {
  3947. return find2 (0, 0, 0);
  3948. }
  3949. BOOST_UBLAS_INLINE
  3950. iterator2 end2 () {
  3951. return find2 (0, 0, size2_);
  3952. }
  3953. // Reverse iterators
  3954. BOOST_UBLAS_INLINE
  3955. const_reverse_iterator1 rbegin1 () const {
  3956. return const_reverse_iterator1 (end1 ());
  3957. }
  3958. BOOST_UBLAS_INLINE
  3959. const_reverse_iterator1 rend1 () const {
  3960. return const_reverse_iterator1 (begin1 ());
  3961. }
  3962. BOOST_UBLAS_INLINE
  3963. reverse_iterator1 rbegin1 () {
  3964. return reverse_iterator1 (end1 ());
  3965. }
  3966. BOOST_UBLAS_INLINE
  3967. reverse_iterator1 rend1 () {
  3968. return reverse_iterator1 (begin1 ());
  3969. }
  3970. BOOST_UBLAS_INLINE
  3971. const_reverse_iterator2 rbegin2 () const {
  3972. return const_reverse_iterator2 (end2 ());
  3973. }
  3974. BOOST_UBLAS_INLINE
  3975. const_reverse_iterator2 rend2 () const {
  3976. return const_reverse_iterator2 (begin2 ());
  3977. }
  3978. BOOST_UBLAS_INLINE
  3979. reverse_iterator2 rbegin2 () {
  3980. return reverse_iterator2 (end2 ());
  3981. }
  3982. BOOST_UBLAS_INLINE
  3983. reverse_iterator2 rend2 () {
  3984. return reverse_iterator2 (begin2 ());
  3985. }
  3986. // Serialization
  3987. template<class Archive>
  3988. void serialize(Archive & ar, const unsigned int /* file_version */){
  3989. // we need to copy to a collection_size_type to get a portable
  3990. // and efficient serialization
  3991. serialization::collection_size_type s1 (size1_);
  3992. serialization::collection_size_type s2 (size2_);
  3993. // serialize the sizes
  3994. ar & serialization::make_nvp("size1",s1)
  3995. & serialization::make_nvp("size2",s2);
  3996. // copy the values back if loading
  3997. if (Archive::is_loading::value) {
  3998. size1_ = s1;
  3999. size2_ = s2;
  4000. }
  4001. // could probably use make_array( &(data[0][0]), N*M )
  4002. ar & serialization::make_array(data_, N);
  4003. }
  4004. private:
  4005. size_type size1_;
  4006. size_type size2_;
  4007. value_type data_ [N] [M];
  4008. };
  4009. }}}
  4010. #endif