banded.hpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. //
  2. // Copyright (c) 2000-2002
  3. // Joerg Walter, Mathias Koch
  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_BANDED_
  13. #define _BOOST_UBLAS_BANDED_
  14. #include <boost/numeric/ublas/matrix.hpp>
  15. #include <boost/numeric/ublas/detail/temporary.hpp>
  16. // Iterators based on ideas of Jeremy Siek
  17. namespace boost { namespace numeric { namespace ublas {
  18. /** \brief A banded matrix of values of type \c T.
  19. *
  20. * For a \f$(mxn)\f$-dimensional banded matrix with \f$l\f$ lower and \f$u\f$ upper diagonals and
  21. * \f$0 \leq i < m\f$ and \f$0 \leq j < n\f$, if \f$i>j+l\f$ or \f$i<j-u\f$ then \f$b_{i,j}=0\f$.
  22. * The default storage for banded matrices is packed. Orientation and storage can also be specified.
  23. * Default is \c row_major and and unbounded_array. It is \b not required by the storage to initialize
  24. * elements of the matrix.
  25. *
  26. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  27. * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major
  28. * \tparam A the type of Storage array. Default is \c unbounded_array
  29. */
  30. template<class T, class L, class A>
  31. class banded_matrix:
  32. public matrix_container<banded_matrix<T, L, A> > {
  33. typedef T *pointer;
  34. typedef L layout_type;
  35. typedef banded_matrix<T, L, A> self_type;
  36. public:
  37. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  38. using matrix_container<self_type>::operator ();
  39. #endif
  40. typedef typename A::size_type size_type;
  41. typedef typename A::difference_type difference_type;
  42. typedef T value_type;
  43. typedef const T &const_reference;
  44. typedef T &reference;
  45. typedef A array_type;
  46. typedef const matrix_reference<const self_type> const_closure_type;
  47. typedef matrix_reference<self_type> closure_type;
  48. typedef vector<T, A> vector_temporary_type;
  49. typedef matrix<T, L, A> matrix_temporary_type; // general sub-matrix
  50. typedef packed_tag storage_category;
  51. typedef typename L::orientation_category orientation_category;
  52. // Construction and destruction
  53. BOOST_UBLAS_INLINE
  54. banded_matrix ():
  55. matrix_container<self_type> (),
  56. size1_ (0), size2_ (0),
  57. lower_ (0), upper_ (0), data_ (0) {}
  58. BOOST_UBLAS_INLINE
  59. banded_matrix (size_type size1, size_type size2, size_type lower = 0, size_type upper = 0):
  60. matrix_container<self_type> (),
  61. size1_ (size1), size2_ (size2),
  62. lower_ (lower), upper_ (upper), data_ ((std::max) (size1, size2) * (lower + 1 + upper)) {
  63. }
  64. BOOST_UBLAS_INLINE
  65. banded_matrix (size_type size1, size_type size2, size_type lower, size_type upper, const array_type &data):
  66. matrix_container<self_type> (),
  67. size1_ (size1), size2_ (size2),
  68. lower_ (lower), upper_ (upper), data_ (data) {}
  69. BOOST_UBLAS_INLINE
  70. banded_matrix (const banded_matrix &m):
  71. matrix_container<self_type> (),
  72. size1_ (m.size1_), size2_ (m.size2_),
  73. lower_ (m.lower_), upper_ (m.upper_), data_ (m.data_) {}
  74. template<class AE>
  75. BOOST_UBLAS_INLINE
  76. banded_matrix (const matrix_expression<AE> &ae, size_type lower = 0, size_type upper = 0):
  77. matrix_container<self_type> (),
  78. size1_ (ae ().size1 ()), size2_ (ae ().size2 ()),
  79. lower_ (lower), upper_ (upper),
  80. data_ ((std::max) (size1_, size2_) * (lower_ + 1 + upper_)) {
  81. matrix_assign<scalar_assign> (*this, ae);
  82. }
  83. // Accessors
  84. BOOST_UBLAS_INLINE
  85. size_type size1 () const {
  86. return size1_;
  87. }
  88. BOOST_UBLAS_INLINE
  89. size_type size2 () const {
  90. return size2_;
  91. }
  92. BOOST_UBLAS_INLINE
  93. size_type lower () const {
  94. return lower_;
  95. }
  96. BOOST_UBLAS_INLINE
  97. size_type upper () const {
  98. return upper_;
  99. }
  100. // Storage accessors
  101. BOOST_UBLAS_INLINE
  102. const array_type &data () const {
  103. return data_;
  104. }
  105. BOOST_UBLAS_INLINE
  106. array_type &data () {
  107. return data_;
  108. }
  109. // Resizing
  110. BOOST_UBLAS_INLINE
  111. void resize (size_type size1, size_type size2, size_type lower = 0, size_type upper = 0, bool preserve = true) {
  112. if (preserve) {
  113. self_type temporary (size1, size2, lower, upper);
  114. detail::matrix_resize_preserve<layout_type> (*this, temporary);
  115. }
  116. else {
  117. data ().resize ((std::max) (size1, size2) * (lower + 1 + upper));
  118. size1_ = size1;
  119. size2_ = size2;
  120. lower_ = lower;
  121. upper_ = upper;
  122. }
  123. }
  124. BOOST_UBLAS_INLINE
  125. void resize_packed_preserve (size_type size1, size_type size2, size_type lower = 0, size_type upper = 0) {
  126. size1_ = size1;
  127. size2_ = size2;
  128. lower_ = lower;
  129. upper_ = upper;
  130. data ().resize ((std::max) (size1, size2) * (lower + 1 + upper), value_type ());
  131. }
  132. // Element access
  133. BOOST_UBLAS_INLINE
  134. const_reference operator () (size_type i, size_type j) const {
  135. BOOST_UBLAS_CHECK (i < size1_, bad_index ());
  136. BOOST_UBLAS_CHECK (j < size2_, bad_index ());
  137. #ifdef BOOST_UBLAS_OWN_BANDED
  138. const size_type k = (std::max) (i, j);
  139. const size_type l = lower_ + j - i;
  140. if (k < (std::max) (size1_, size2_) &&
  141. l < lower_ + 1 + upper_)
  142. return data () [layout_type::element (k, (std::max) (size1_, size2_),
  143. l, lower_ + 1 + upper_)];
  144. #else
  145. const size_type k = j;
  146. const size_type l = upper_ + i - j;
  147. if (k < size2_ &&
  148. l < lower_ + 1 + upper_)
  149. return data () [layout_type::element (k, size2_,
  150. l, lower_ + 1 + upper_)];
  151. #endif
  152. return zero_;
  153. }
  154. BOOST_UBLAS_INLINE
  155. reference at_element (size_type i, size_type j) {
  156. BOOST_UBLAS_CHECK (i < size1_, bad_index ());
  157. BOOST_UBLAS_CHECK (j < size2_, bad_index ());
  158. #ifdef BOOST_UBLAS_OWN_BANDED
  159. const size_type k = (std::max) (i, j);
  160. const size_type l = lower_ + j - i;
  161. return data () [layout_type::element (k, (std::max) (size1_, size2_),
  162. l, lower_ + 1 + upper_)];
  163. #else
  164. const size_type k = j;
  165. const size_type l = upper_ + i - j;
  166. return data () [layout_type::element (k, size2_,
  167. l, lower_ + 1 + upper_)];
  168. #endif
  169. }
  170. BOOST_UBLAS_INLINE
  171. reference operator () (size_type i, size_type j) {
  172. BOOST_UBLAS_CHECK (i < size1_, bad_index ());
  173. BOOST_UBLAS_CHECK (j < size2_, bad_index ());
  174. #ifdef BOOST_UBLAS_OWN_BANDED
  175. const size_type k = (std::max) (i, j);
  176. const size_type l = lower_ + j - i;
  177. if (! (k < (std::max) (size1_, size2_) &&
  178. l < lower_ + 1 + upper_) ) {
  179. bad_index ().raise ();
  180. // NEVER reached
  181. }
  182. return data () [layout_type::element (k, (std::max) (size1_, size2_),
  183. l, lower_ + 1 + upper_)];
  184. #else
  185. const size_type k = j;
  186. const size_type l = upper_ + i - j;
  187. if (! (k < size2_ &&
  188. l < lower_ + 1 + upper_) ) {
  189. bad_index ().raise ();
  190. // NEVER reached
  191. }
  192. return data () [layout_type::element (k, size2_,
  193. l, lower_ + 1 + upper_)];
  194. #endif
  195. }
  196. // Element assignment
  197. BOOST_UBLAS_INLINE
  198. reference insert_element (size_type i, size_type j, const_reference t) {
  199. return (operator () (i, j) = t);
  200. }
  201. BOOST_UBLAS_INLINE
  202. void erase_element (size_type i, size_type j) {
  203. operator () (i, j) = value_type/*zero*/();
  204. }
  205. // Zeroing
  206. BOOST_UBLAS_INLINE
  207. void clear () {
  208. std::fill (data ().begin (), data ().end (), value_type/*zero*/());
  209. }
  210. // Assignment
  211. BOOST_UBLAS_INLINE
  212. banded_matrix &operator = (const banded_matrix &m) {
  213. size1_ = m.size1_;
  214. size2_ = m.size2_;
  215. lower_ = m.lower_;
  216. upper_ = m.upper_;
  217. data () = m.data ();
  218. return *this;
  219. }
  220. BOOST_UBLAS_INLINE
  221. banded_matrix &assign_temporary (banded_matrix &m) {
  222. swap (m);
  223. return *this;
  224. }
  225. template<class AE>
  226. BOOST_UBLAS_INLINE
  227. banded_matrix &operator = (const matrix_expression<AE> &ae) {
  228. self_type temporary (ae, lower_, upper_);
  229. return assign_temporary (temporary);
  230. }
  231. template<class AE>
  232. BOOST_UBLAS_INLINE
  233. banded_matrix &assign (const matrix_expression<AE> &ae) {
  234. matrix_assign<scalar_assign> (*this, ae);
  235. return *this;
  236. }
  237. template<class AE>
  238. BOOST_UBLAS_INLINE
  239. banded_matrix& operator += (const matrix_expression<AE> &ae) {
  240. self_type temporary (*this + ae, lower_, upper_);
  241. return assign_temporary (temporary);
  242. }
  243. template<class AE>
  244. BOOST_UBLAS_INLINE
  245. banded_matrix &plus_assign (const matrix_expression<AE> &ae) {
  246. matrix_assign<scalar_plus_assign> (*this, ae);
  247. return *this;
  248. }
  249. template<class AE>
  250. BOOST_UBLAS_INLINE
  251. banded_matrix& operator -= (const matrix_expression<AE> &ae) {
  252. self_type temporary (*this - ae, lower_, upper_);
  253. return assign_temporary (temporary);
  254. }
  255. template<class AE>
  256. BOOST_UBLAS_INLINE
  257. banded_matrix &minus_assign (const matrix_expression<AE> &ae) {
  258. matrix_assign<scalar_minus_assign> (*this, ae);
  259. return *this;
  260. }
  261. template<class AT>
  262. BOOST_UBLAS_INLINE
  263. banded_matrix& operator *= (const AT &at) {
  264. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  265. return *this;
  266. }
  267. template<class AT>
  268. BOOST_UBLAS_INLINE
  269. banded_matrix& operator /= (const AT &at) {
  270. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  271. return *this;
  272. }
  273. // Swapping
  274. BOOST_UBLAS_INLINE
  275. void swap (banded_matrix &m) {
  276. if (this != &m) {
  277. std::swap (size1_, m.size1_);
  278. std::swap (size2_, m.size2_);
  279. std::swap (lower_, m.lower_);
  280. std::swap (upper_, m.upper_);
  281. data ().swap (m.data ());
  282. }
  283. }
  284. BOOST_UBLAS_INLINE
  285. friend void swap (banded_matrix &m1, banded_matrix &m2) {
  286. m1.swap (m2);
  287. }
  288. // Iterator types
  289. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  290. typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1;
  291. typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2;
  292. typedef indexed_const_iterator1<self_type, packed_random_access_iterator_tag> const_iterator1;
  293. typedef indexed_const_iterator2<self_type, packed_random_access_iterator_tag> const_iterator2;
  294. #else
  295. class const_iterator1;
  296. class iterator1;
  297. class const_iterator2;
  298. class iterator2;
  299. #endif
  300. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  301. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  302. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  303. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  304. // Element lookup
  305. BOOST_UBLAS_INLINE
  306. const_iterator1 find1 (int rank, size_type i, size_type j) const {
  307. if (rank == 1) {
  308. size_type lower_i = (std::max) (difference_type (j - upper_), difference_type (0));
  309. i = (std::max) (i, lower_i);
  310. size_type upper_i = (std::min) (j + 1 + lower_, size1_);
  311. i = (std::min) (i, upper_i);
  312. }
  313. return const_iterator1 (*this, i, j);
  314. }
  315. BOOST_UBLAS_INLINE
  316. iterator1 find1 (int rank, size_type i, size_type j) {
  317. if (rank == 1) {
  318. size_type lower_i = (std::max) (difference_type (j - upper_), difference_type (0));
  319. i = (std::max) (i, lower_i);
  320. size_type upper_i = (std::min) (j + 1 + lower_, size1_);
  321. i = (std::min) (i, upper_i);
  322. }
  323. return iterator1 (*this, i, j);
  324. }
  325. BOOST_UBLAS_INLINE
  326. const_iterator2 find2 (int rank, size_type i, size_type j) const {
  327. if (rank == 1) {
  328. size_type lower_j = (std::max) (difference_type (i - lower_), difference_type (0));
  329. j = (std::max) (j, lower_j);
  330. size_type upper_j = (std::min) (i + 1 + upper_, size2_);
  331. j = (std::min) (j, upper_j);
  332. }
  333. return const_iterator2 (*this, i, j);
  334. }
  335. BOOST_UBLAS_INLINE
  336. iterator2 find2 (int rank, size_type i, size_type j) {
  337. if (rank == 1) {
  338. size_type lower_j = (std::max) (difference_type (i - lower_), difference_type (0));
  339. j = (std::max) (j, lower_j);
  340. size_type upper_j = (std::min) (i + 1 + upper_, size2_);
  341. j = (std::min) (j, upper_j);
  342. }
  343. return iterator2 (*this, i, j);
  344. }
  345. // Iterators simply are indices.
  346. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  347. class const_iterator1:
  348. public container_const_reference<banded_matrix>,
  349. public random_access_iterator_base<packed_random_access_iterator_tag,
  350. const_iterator1, value_type> {
  351. public:
  352. typedef typename banded_matrix::value_type value_type;
  353. typedef typename banded_matrix::difference_type difference_type;
  354. typedef typename banded_matrix::const_reference reference;
  355. typedef const typename banded_matrix::pointer pointer;
  356. typedef const_iterator2 dual_iterator_type;
  357. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  358. // Construction and destruction
  359. BOOST_UBLAS_INLINE
  360. const_iterator1 ():
  361. container_const_reference<self_type> (), it1_ (), it2_ () {}
  362. BOOST_UBLAS_INLINE
  363. const_iterator1 (const self_type &m, size_type it1, size_type it2):
  364. container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
  365. BOOST_UBLAS_INLINE
  366. const_iterator1 (const iterator1 &it):
  367. container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {}
  368. // Arithmetic
  369. BOOST_UBLAS_INLINE
  370. const_iterator1 &operator ++ () {
  371. ++ it1_;
  372. return *this;
  373. }
  374. BOOST_UBLAS_INLINE
  375. const_iterator1 &operator -- () {
  376. -- it1_;
  377. return *this;
  378. }
  379. BOOST_UBLAS_INLINE
  380. const_iterator1 &operator += (difference_type n) {
  381. it1_ += n;
  382. return *this;
  383. }
  384. BOOST_UBLAS_INLINE
  385. const_iterator1 &operator -= (difference_type n) {
  386. it1_ -= n;
  387. return *this;
  388. }
  389. BOOST_UBLAS_INLINE
  390. difference_type operator - (const const_iterator1 &it) const {
  391. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  392. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  393. return it1_ - it.it1_;
  394. }
  395. // Dereference
  396. BOOST_UBLAS_INLINE
  397. const_reference operator * () const {
  398. return (*this) () (it1_, it2_);
  399. }
  400. BOOST_UBLAS_INLINE
  401. const_reference operator [] (difference_type n) const {
  402. return *(*this + n);
  403. }
  404. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  405. BOOST_UBLAS_INLINE
  406. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  407. typename self_type::
  408. #endif
  409. const_iterator2 begin () const {
  410. return (*this) ().find2 (1, it1_, 0);
  411. }
  412. BOOST_UBLAS_INLINE
  413. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  414. typename self_type::
  415. #endif
  416. const_iterator2 end () const {
  417. return (*this) ().find2 (1, it1_, (*this) ().size2 ());
  418. }
  419. BOOST_UBLAS_INLINE
  420. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  421. typename self_type::
  422. #endif
  423. const_reverse_iterator2 rbegin () const {
  424. return const_reverse_iterator2 (end ());
  425. }
  426. BOOST_UBLAS_INLINE
  427. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  428. typename self_type::
  429. #endif
  430. const_reverse_iterator2 rend () const {
  431. return const_reverse_iterator2 (begin ());
  432. }
  433. #endif
  434. // Indices
  435. BOOST_UBLAS_INLINE
  436. size_type index1 () const {
  437. return it1_;
  438. }
  439. BOOST_UBLAS_INLINE
  440. size_type index2 () const {
  441. return it2_;
  442. }
  443. // Assignment
  444. BOOST_UBLAS_INLINE
  445. const_iterator1 &operator = (const const_iterator1 &it) {
  446. container_const_reference<self_type>::assign (&it ());
  447. it1_ = it.it1_;
  448. it2_ = it.it2_;
  449. return *this;
  450. }
  451. // Comparison
  452. BOOST_UBLAS_INLINE
  453. bool operator == (const const_iterator1 &it) const {
  454. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  455. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  456. return it1_ == it.it1_;
  457. }
  458. BOOST_UBLAS_INLINE
  459. bool operator < (const const_iterator1 &it) const {
  460. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  461. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  462. return it1_ < it.it1_;
  463. }
  464. private:
  465. size_type it1_;
  466. size_type it2_;
  467. };
  468. #endif
  469. BOOST_UBLAS_INLINE
  470. const_iterator1 begin1 () const {
  471. return find1 (0, 0, 0);
  472. }
  473. BOOST_UBLAS_INLINE
  474. const_iterator1 end1 () const {
  475. return find1 (0, size1_, 0);
  476. }
  477. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  478. class iterator1:
  479. public container_reference<banded_matrix>,
  480. public random_access_iterator_base<packed_random_access_iterator_tag,
  481. iterator1, value_type> {
  482. public:
  483. typedef typename banded_matrix::value_type value_type;
  484. typedef typename banded_matrix::difference_type difference_type;
  485. typedef typename banded_matrix::reference reference;
  486. typedef typename banded_matrix::pointer pointer;
  487. typedef iterator2 dual_iterator_type;
  488. typedef reverse_iterator2 dual_reverse_iterator_type;
  489. // Construction and destruction
  490. BOOST_UBLAS_INLINE
  491. iterator1 ():
  492. container_reference<self_type> (), it1_ (), it2_ () {}
  493. BOOST_UBLAS_INLINE
  494. iterator1 (self_type &m, size_type it1, size_type it2):
  495. container_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
  496. // Arithmetic
  497. BOOST_UBLAS_INLINE
  498. iterator1 &operator ++ () {
  499. ++ it1_;
  500. return *this;
  501. }
  502. BOOST_UBLAS_INLINE
  503. iterator1 &operator -- () {
  504. -- it1_;
  505. return *this;
  506. }
  507. BOOST_UBLAS_INLINE
  508. iterator1 &operator += (difference_type n) {
  509. it1_ += n;
  510. return *this;
  511. }
  512. BOOST_UBLAS_INLINE
  513. iterator1 &operator -= (difference_type n) {
  514. it1_ -= n;
  515. return *this;
  516. }
  517. BOOST_UBLAS_INLINE
  518. difference_type operator - (const iterator1 &it) const {
  519. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  520. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  521. return it1_ - it.it1_;
  522. }
  523. // Dereference
  524. BOOST_UBLAS_INLINE
  525. reference operator * () const {
  526. return (*this) ().at_element (it1_, it2_);
  527. }
  528. BOOST_UBLAS_INLINE
  529. reference operator [] (difference_type n) const {
  530. return *(*this + n);
  531. }
  532. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  533. BOOST_UBLAS_INLINE
  534. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  535. typename self_type::
  536. #endif
  537. iterator2 begin () const {
  538. return (*this) ().find2 (1, it1_, 0);
  539. }
  540. BOOST_UBLAS_INLINE
  541. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  542. typename self_type::
  543. #endif
  544. iterator2 end () const {
  545. return (*this) ().find2 (1, it1_, (*this) ().size2 ());
  546. }
  547. BOOST_UBLAS_INLINE
  548. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  549. typename self_type::
  550. #endif
  551. reverse_iterator2 rbegin () const {
  552. return reverse_iterator2 (end ());
  553. }
  554. BOOST_UBLAS_INLINE
  555. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  556. typename self_type::
  557. #endif
  558. reverse_iterator2 rend () const {
  559. return reverse_iterator2 (begin ());
  560. }
  561. #endif
  562. // Indices
  563. BOOST_UBLAS_INLINE
  564. size_type index1 () const {
  565. return it1_;
  566. }
  567. BOOST_UBLAS_INLINE
  568. size_type index2 () const {
  569. return it2_;
  570. }
  571. // Assignment
  572. BOOST_UBLAS_INLINE
  573. iterator1 &operator = (const iterator1 &it) {
  574. container_reference<self_type>::assign (&it ());
  575. it1_ = it.it1_;
  576. it2_ = it.it2_;
  577. return *this;
  578. }
  579. // Comparison
  580. BOOST_UBLAS_INLINE
  581. bool operator == (const iterator1 &it) const {
  582. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  583. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  584. return it1_ == it.it1_;
  585. }
  586. BOOST_UBLAS_INLINE
  587. bool operator < (const iterator1 &it) const {
  588. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  589. BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
  590. return it1_ < it.it1_;
  591. }
  592. private:
  593. size_type it1_;
  594. size_type it2_;
  595. friend class const_iterator1;
  596. };
  597. #endif
  598. BOOST_UBLAS_INLINE
  599. iterator1 begin1 () {
  600. return find1 (0, 0, 0);
  601. }
  602. BOOST_UBLAS_INLINE
  603. iterator1 end1 () {
  604. return find1 (0, size1_, 0);
  605. }
  606. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  607. class const_iterator2:
  608. public container_const_reference<banded_matrix>,
  609. public random_access_iterator_base<packed_random_access_iterator_tag,
  610. const_iterator2, value_type> {
  611. public:
  612. typedef typename banded_matrix::value_type value_type;
  613. typedef typename banded_matrix::difference_type difference_type;
  614. typedef typename banded_matrix::const_reference reference;
  615. typedef const typename banded_matrix::pointer pointer;
  616. typedef const_iterator1 dual_iterator_type;
  617. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  618. // Construction and destruction
  619. BOOST_UBLAS_INLINE
  620. const_iterator2 ():
  621. container_const_reference<self_type> (), it1_ (), it2_ () {}
  622. BOOST_UBLAS_INLINE
  623. const_iterator2 (const self_type &m, size_type it1, size_type it2):
  624. container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
  625. BOOST_UBLAS_INLINE
  626. const_iterator2 (const iterator2 &it):
  627. container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {}
  628. // Arithmetic
  629. BOOST_UBLAS_INLINE
  630. const_iterator2 &operator ++ () {
  631. ++ it2_;
  632. return *this;
  633. }
  634. BOOST_UBLAS_INLINE
  635. const_iterator2 &operator -- () {
  636. -- it2_;
  637. return *this;
  638. }
  639. BOOST_UBLAS_INLINE
  640. const_iterator2 &operator += (difference_type n) {
  641. it2_ += n;
  642. return *this;
  643. }
  644. BOOST_UBLAS_INLINE
  645. const_iterator2 &operator -= (difference_type n) {
  646. it2_ -= n;
  647. return *this;
  648. }
  649. BOOST_UBLAS_INLINE
  650. difference_type operator - (const const_iterator2 &it) const {
  651. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  652. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  653. return it2_ - it.it2_;
  654. }
  655. // Dereference
  656. BOOST_UBLAS_INLINE
  657. const_reference operator * () const {
  658. return (*this) () (it1_, it2_);
  659. }
  660. BOOST_UBLAS_INLINE
  661. const_reference operator [] (difference_type n) const {
  662. return *(*this + n);
  663. }
  664. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  665. BOOST_UBLAS_INLINE
  666. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  667. typename self_type::
  668. #endif
  669. const_iterator1 begin () const {
  670. return (*this) ().find1 (1, 0, it2_);
  671. }
  672. BOOST_UBLAS_INLINE
  673. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  674. typename self_type::
  675. #endif
  676. const_iterator1 end () const {
  677. return (*this) ().find1 (1, (*this) ().size1 (), it2_);
  678. }
  679. BOOST_UBLAS_INLINE
  680. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  681. typename self_type::
  682. #endif
  683. const_reverse_iterator1 rbegin () const {
  684. return const_reverse_iterator1 (end ());
  685. }
  686. BOOST_UBLAS_INLINE
  687. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  688. typename self_type::
  689. #endif
  690. const_reverse_iterator1 rend () const {
  691. return const_reverse_iterator1 (begin ());
  692. }
  693. #endif
  694. // Indices
  695. BOOST_UBLAS_INLINE
  696. size_type index1 () const {
  697. return it1_;
  698. }
  699. BOOST_UBLAS_INLINE
  700. size_type index2 () const {
  701. return it2_;
  702. }
  703. // Assignment
  704. BOOST_UBLAS_INLINE
  705. const_iterator2 &operator = (const const_iterator2 &it) {
  706. container_const_reference<self_type>::assign (&it ());
  707. it1_ = it.it1_;
  708. it2_ = it.it2_;
  709. return *this;
  710. }
  711. // Comparison
  712. BOOST_UBLAS_INLINE
  713. bool operator == (const const_iterator2 &it) const {
  714. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  715. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  716. return it2_ == it.it2_;
  717. }
  718. BOOST_UBLAS_INLINE
  719. bool operator < (const const_iterator2 &it) const {
  720. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  721. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  722. return it2_ < it.it2_;
  723. }
  724. private:
  725. size_type it1_;
  726. size_type it2_;
  727. };
  728. #endif
  729. BOOST_UBLAS_INLINE
  730. const_iterator2 begin2 () const {
  731. return find2 (0, 0, 0);
  732. }
  733. BOOST_UBLAS_INLINE
  734. const_iterator2 end2 () const {
  735. return find2 (0, 0, size2_);
  736. }
  737. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  738. class iterator2:
  739. public container_reference<banded_matrix>,
  740. public random_access_iterator_base<packed_random_access_iterator_tag,
  741. iterator2, value_type> {
  742. public:
  743. typedef typename banded_matrix::value_type value_type;
  744. typedef typename banded_matrix::difference_type difference_type;
  745. typedef typename banded_matrix::reference reference;
  746. typedef typename banded_matrix::pointer pointer;
  747. typedef iterator1 dual_iterator_type;
  748. typedef reverse_iterator1 dual_reverse_iterator_type;
  749. // Construction and destruction
  750. BOOST_UBLAS_INLINE
  751. iterator2 ():
  752. container_reference<self_type> (), it1_ (), it2_ () {}
  753. BOOST_UBLAS_INLINE
  754. iterator2 (self_type &m, size_type it1, size_type it2):
  755. container_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
  756. // Arithmetic
  757. BOOST_UBLAS_INLINE
  758. iterator2 &operator ++ () {
  759. ++ it2_;
  760. return *this;
  761. }
  762. BOOST_UBLAS_INLINE
  763. iterator2 &operator -- () {
  764. -- it2_;
  765. return *this;
  766. }
  767. BOOST_UBLAS_INLINE
  768. iterator2 &operator += (difference_type n) {
  769. it2_ += n;
  770. return *this;
  771. }
  772. BOOST_UBLAS_INLINE
  773. iterator2 &operator -= (difference_type n) {
  774. it2_ -= n;
  775. return *this;
  776. }
  777. BOOST_UBLAS_INLINE
  778. difference_type operator - (const iterator2 &it) const {
  779. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  780. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  781. return it2_ - it.it2_;
  782. }
  783. // Dereference
  784. BOOST_UBLAS_INLINE
  785. reference operator * () const {
  786. return (*this) ().at_element (it1_, it2_);
  787. }
  788. BOOST_UBLAS_INLINE
  789. reference operator [] (difference_type n) const {
  790. return *(*this + n);
  791. }
  792. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  793. BOOST_UBLAS_INLINE
  794. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  795. typename self_type::
  796. #endif
  797. iterator1 begin () const {
  798. return (*this) ().find1 (1, 0, it2_);
  799. }
  800. BOOST_UBLAS_INLINE
  801. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  802. typename self_type::
  803. #endif
  804. iterator1 end () const {
  805. return (*this) ().find1 (1, (*this) ().size1 (), it2_);
  806. }
  807. BOOST_UBLAS_INLINE
  808. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  809. typename self_type::
  810. #endif
  811. reverse_iterator1 rbegin () const {
  812. return reverse_iterator1 (end ());
  813. }
  814. BOOST_UBLAS_INLINE
  815. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  816. typename self_type::
  817. #endif
  818. reverse_iterator1 rend () const {
  819. return reverse_iterator1 (begin ());
  820. }
  821. #endif
  822. // Indices
  823. BOOST_UBLAS_INLINE
  824. size_type index1 () const {
  825. return it1_;
  826. }
  827. BOOST_UBLAS_INLINE
  828. size_type index2 () const {
  829. return it2_;
  830. }
  831. // Assignment
  832. BOOST_UBLAS_INLINE
  833. iterator2 &operator = (const iterator2 &it) {
  834. container_reference<self_type>::assign (&it ());
  835. it1_ = it.it1_;
  836. it2_ = it.it2_;
  837. return *this;
  838. }
  839. // Comparison
  840. BOOST_UBLAS_INLINE
  841. bool operator == (const iterator2 &it) const {
  842. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  843. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  844. return it2_ == it.it2_;
  845. }
  846. BOOST_UBLAS_INLINE
  847. bool operator < (const iterator2 &it) const {
  848. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  849. BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
  850. return it2_ < it.it2_;
  851. }
  852. private:
  853. size_type it1_;
  854. size_type it2_;
  855. friend class const_iterator2;
  856. };
  857. #endif
  858. BOOST_UBLAS_INLINE
  859. iterator2 begin2 () {
  860. return find2 (0, 0, 0);
  861. }
  862. BOOST_UBLAS_INLINE
  863. iterator2 end2 () {
  864. return find2 (0, 0, size2_);
  865. }
  866. // Reverse iterators
  867. BOOST_UBLAS_INLINE
  868. const_reverse_iterator1 rbegin1 () const {
  869. return const_reverse_iterator1 (end1 ());
  870. }
  871. BOOST_UBLAS_INLINE
  872. const_reverse_iterator1 rend1 () const {
  873. return const_reverse_iterator1 (begin1 ());
  874. }
  875. BOOST_UBLAS_INLINE
  876. reverse_iterator1 rbegin1 () {
  877. return reverse_iterator1 (end1 ());
  878. }
  879. BOOST_UBLAS_INLINE
  880. reverse_iterator1 rend1 () {
  881. return reverse_iterator1 (begin1 ());
  882. }
  883. BOOST_UBLAS_INLINE
  884. const_reverse_iterator2 rbegin2 () const {
  885. return const_reverse_iterator2 (end2 ());
  886. }
  887. BOOST_UBLAS_INLINE
  888. const_reverse_iterator2 rend2 () const {
  889. return const_reverse_iterator2 (begin2 ());
  890. }
  891. BOOST_UBLAS_INLINE
  892. reverse_iterator2 rbegin2 () {
  893. return reverse_iterator2 (end2 ());
  894. }
  895. BOOST_UBLAS_INLINE
  896. reverse_iterator2 rend2 () {
  897. return reverse_iterator2 (begin2 ());
  898. }
  899. private:
  900. size_type size1_;
  901. size_type size2_;
  902. size_type lower_;
  903. size_type upper_;
  904. array_type data_;
  905. typedef const value_type const_value_type;
  906. static const_value_type zero_;
  907. };
  908. template<class T, class L, class A>
  909. typename banded_matrix<T, L, A>::const_value_type banded_matrix<T, L, A>::zero_ = value_type/*zero*/();
  910. /** \brief A diagonal matrix of values of type \c T, which is a specialization of a banded matrix
  911. *
  912. * For a \f$(m\times m)\f$-dimensional diagonal matrix, \f$0 \leq i < m\f$ and \f$0 \leq j < m\f$,
  913. * if \f$i\neq j\f$ then \f$b_{i,j}=0\f$. The default storage for diagonal matrices is packed.
  914. * Orientation and storage can also be specified. Default is \c row major \c unbounded_array.
  915. *
  916. * As a specialization of a banded matrix, the constructor of the diagonal matrix creates
  917. * a banded matrix with 0 upper and lower diagonals around the main diagonal and the matrix is
  918. * obviously a square matrix. Operations are optimized based on these 2 assumptions. It is
  919. * \b not required by the storage to initialize elements of the matrix.
  920. *
  921. * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
  922. * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major
  923. * \tparam A the type of Storage array. Default is \c unbounded_array
  924. */
  925. template<class T, class L, class A>
  926. class diagonal_matrix:
  927. public banded_matrix<T, L, A> {
  928. public:
  929. typedef typename A::size_type size_type;
  930. typedef banded_matrix<T, L, A> matrix_type;
  931. typedef A array_type;
  932. // Construction and destruction
  933. BOOST_UBLAS_INLINE
  934. diagonal_matrix ():
  935. matrix_type () {}
  936. BOOST_UBLAS_INLINE
  937. diagonal_matrix (size_type size):
  938. matrix_type (size, size) {}
  939. BOOST_UBLAS_INLINE
  940. diagonal_matrix (size_type size, const array_type& data):
  941. matrix_type (size, size, 0, 0, data) {}
  942. BOOST_UBLAS_INLINE
  943. diagonal_matrix (size_type size1, size_type size2):
  944. matrix_type (size1, size2) {}
  945. template<class AE>
  946. BOOST_UBLAS_INLINE
  947. diagonal_matrix (const matrix_expression<AE> &ae):
  948. matrix_type (ae) {}
  949. BOOST_UBLAS_INLINE
  950. ~diagonal_matrix () {}
  951. // Assignment
  952. BOOST_UBLAS_INLINE
  953. diagonal_matrix &operator = (const diagonal_matrix &m) {
  954. matrix_type::operator = (m);
  955. return *this;
  956. }
  957. template<class AE>
  958. BOOST_UBLAS_INLINE
  959. diagonal_matrix &operator = (const matrix_expression<AE> &ae) {
  960. matrix_type::operator = (ae);
  961. return *this;
  962. }
  963. };
  964. /** \brief A banded matrix adaptator: convert a any matrix into a banded matrix expression
  965. *
  966. * For a \f$(m\times n)\f$-dimensional matrix, the \c banded_adaptor will provide a banded matrix
  967. * with \f$l\f$ lower and \f$u\f$ upper diagonals and \f$0 \leq i < m\f$ and \f$0 \leq j < n\f$,
  968. * if \f$i>j+l\f$ or \f$i<j-u\f$ then \f$b_{i,j}=0\f$.
  969. *
  970. * Storage and location are based on those of the underlying matrix. This is important because
  971. * a \c banded_adaptor does not copy the matrix data to a new place. Therefore, modifying values
  972. * in a \c banded_adaptor matrix will also modify the underlying matrix too.
  973. *
  974. * \tparam M the type of matrix used to generate a banded matrix
  975. */
  976. template<class M>
  977. class banded_adaptor:
  978. public matrix_expression<banded_adaptor<M> > {
  979. typedef banded_adaptor<M> self_type;
  980. public:
  981. #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
  982. using matrix_expression<self_type>::operator ();
  983. #endif
  984. typedef const M const_matrix_type;
  985. typedef M matrix_type;
  986. typedef typename M::size_type size_type;
  987. typedef typename M::difference_type difference_type;
  988. typedef typename M::value_type value_type;
  989. typedef typename M::const_reference const_reference;
  990. typedef typename boost::mpl::if_<boost::is_const<M>,
  991. typename M::const_reference,
  992. typename M::reference>::type reference;
  993. typedef typename boost::mpl::if_<boost::is_const<M>,
  994. typename M::const_closure_type,
  995. typename M::closure_type>::type matrix_closure_type;
  996. typedef const self_type const_closure_type;
  997. typedef self_type closure_type;
  998. // Replaced by _temporary_traits to avoid type requirements on M
  999. //typedef typename M::vector_temporary_type vector_temporary_type;
  1000. //typedef typename M::matrix_temporary_type matrix_temporary_type;
  1001. typedef typename storage_restrict_traits<typename M::storage_category,
  1002. packed_proxy_tag>::storage_category storage_category;
  1003. typedef typename M::orientation_category orientation_category;
  1004. // Construction and destruction
  1005. BOOST_UBLAS_INLINE
  1006. banded_adaptor (matrix_type &data, size_type lower = 0, size_type upper = 0):
  1007. matrix_expression<self_type> (),
  1008. data_ (data), lower_ (lower), upper_ (upper) {}
  1009. BOOST_UBLAS_INLINE
  1010. banded_adaptor (const banded_adaptor &m):
  1011. matrix_expression<self_type> (),
  1012. data_ (m.data_), lower_ (m.lower_), upper_ (m.upper_) {}
  1013. // Accessors
  1014. BOOST_UBLAS_INLINE
  1015. size_type size1 () const {
  1016. return data_.size1 ();
  1017. }
  1018. BOOST_UBLAS_INLINE
  1019. size_type size2 () const {
  1020. return data_.size2 ();
  1021. }
  1022. BOOST_UBLAS_INLINE
  1023. size_type lower () const {
  1024. return lower_;
  1025. }
  1026. BOOST_UBLAS_INLINE
  1027. size_type upper () const {
  1028. return upper_;
  1029. }
  1030. // Storage accessors
  1031. BOOST_UBLAS_INLINE
  1032. const matrix_closure_type &data () const {
  1033. return data_;
  1034. }
  1035. BOOST_UBLAS_INLINE
  1036. matrix_closure_type &data () {
  1037. return data_;
  1038. }
  1039. // Element access
  1040. #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
  1041. BOOST_UBLAS_INLINE
  1042. const_reference operator () (size_type i, size_type j) const {
  1043. BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
  1044. BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
  1045. #ifdef BOOST_UBLAS_OWN_BANDED
  1046. size_type k = (std::max) (i, j);
  1047. size_type l = lower_ + j - i;
  1048. if (k < (std::max) (size1 (), size2 ()) &&
  1049. l < lower_ + 1 + upper_)
  1050. return data () (i, j);
  1051. #else
  1052. size_type k = j;
  1053. size_type l = upper_ + i - j;
  1054. if (k < size2 () &&
  1055. l < lower_ + 1 + upper_)
  1056. return data () (i, j);
  1057. #endif
  1058. return zero_;
  1059. }
  1060. BOOST_UBLAS_INLINE
  1061. reference operator () (size_type i, size_type j) {
  1062. BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
  1063. BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
  1064. #ifdef BOOST_UBLAS_OWN_BANDED
  1065. size_type k = (std::max) (i, j);
  1066. size_type l = lower_ + j - i;
  1067. if (k < (std::max) (size1 (), size2 ()) &&
  1068. l < lower_ + 1 + upper_)
  1069. return data () (i, j);
  1070. #else
  1071. size_type k = j;
  1072. size_type l = upper_ + i - j;
  1073. if (k < size2 () &&
  1074. l < lower_ + 1 + upper_)
  1075. return data () (i, j);
  1076. #endif
  1077. #ifndef BOOST_UBLAS_REFERENCE_CONST_MEMBER
  1078. bad_index ().raise ();
  1079. #endif
  1080. return const_cast<reference>(zero_);
  1081. }
  1082. #else
  1083. BOOST_UBLAS_INLINE
  1084. reference operator () (size_type i, size_type j) const {
  1085. BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
  1086. BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
  1087. #ifdef BOOST_UBLAS_OWN_BANDED
  1088. size_type k = (std::max) (i, j);
  1089. size_type l = lower_ + j - i;
  1090. if (k < (std::max) (size1 (), size2 ()) &&
  1091. l < lower_ + 1 + upper_)
  1092. return data () (i, j);
  1093. #else
  1094. size_type k = j;
  1095. size_type l = upper_ + i - j;
  1096. if (k < size2 () &&
  1097. l < lower_ + 1 + upper_)
  1098. return data () (i, j);
  1099. #endif
  1100. #ifndef BOOST_UBLAS_REFERENCE_CONST_MEMBER
  1101. bad_index ().raise ();
  1102. #endif
  1103. return const_cast<reference>(zero_);
  1104. }
  1105. #endif
  1106. // Assignment
  1107. BOOST_UBLAS_INLINE
  1108. banded_adaptor &operator = (const banded_adaptor &m) {
  1109. matrix_assign<scalar_assign> (*this, m);
  1110. return *this;
  1111. }
  1112. BOOST_UBLAS_INLINE
  1113. banded_adaptor &assign_temporary (banded_adaptor &m) {
  1114. *this = m;
  1115. return *this;
  1116. }
  1117. template<class AE>
  1118. BOOST_UBLAS_INLINE
  1119. banded_adaptor &operator = (const matrix_expression<AE> &ae) {
  1120. matrix_assign<scalar_assign> (*this, matrix<value_type> (ae));
  1121. return *this;
  1122. }
  1123. template<class AE>
  1124. BOOST_UBLAS_INLINE
  1125. banded_adaptor &assign (const matrix_expression<AE> &ae) {
  1126. matrix_assign<scalar_assign> (*this, ae);
  1127. return *this;
  1128. }
  1129. template<class AE>
  1130. BOOST_UBLAS_INLINE
  1131. banded_adaptor& operator += (const matrix_expression<AE> &ae) {
  1132. matrix_assign<scalar_assign> (*this, matrix<value_type> (*this + ae));
  1133. return *this;
  1134. }
  1135. template<class AE>
  1136. BOOST_UBLAS_INLINE
  1137. banded_adaptor &plus_assign (const matrix_expression<AE> &ae) {
  1138. matrix_assign<scalar_plus_assign> (*this, ae);
  1139. return *this;
  1140. }
  1141. template<class AE>
  1142. BOOST_UBLAS_INLINE
  1143. banded_adaptor& operator -= (const matrix_expression<AE> &ae) {
  1144. matrix_assign<scalar_assign> (*this, matrix<value_type> (*this - ae));
  1145. return *this;
  1146. }
  1147. template<class AE>
  1148. BOOST_UBLAS_INLINE
  1149. banded_adaptor &minus_assign (const matrix_expression<AE> &ae) {
  1150. matrix_assign<scalar_minus_assign> (*this, ae);
  1151. return *this;
  1152. }
  1153. template<class AT>
  1154. BOOST_UBLAS_INLINE
  1155. banded_adaptor& operator *= (const AT &at) {
  1156. matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
  1157. return *this;
  1158. }
  1159. template<class AT>
  1160. BOOST_UBLAS_INLINE
  1161. banded_adaptor& operator /= (const AT &at) {
  1162. matrix_assign_scalar<scalar_divides_assign> (*this, at);
  1163. return *this;
  1164. }
  1165. // Closure comparison
  1166. BOOST_UBLAS_INLINE
  1167. bool same_closure (const banded_adaptor &ba) const {
  1168. return (*this).data ().same_closure (ba.data ());
  1169. }
  1170. // Swapping
  1171. BOOST_UBLAS_INLINE
  1172. void swap (banded_adaptor &m) {
  1173. if (this != &m) {
  1174. BOOST_UBLAS_CHECK (lower_ == m.lower_, bad_size ());
  1175. BOOST_UBLAS_CHECK (upper_ == m.upper_, bad_size ());
  1176. matrix_swap<scalar_swap> (*this, m);
  1177. }
  1178. }
  1179. BOOST_UBLAS_INLINE
  1180. friend void swap (banded_adaptor &m1, banded_adaptor &m2) {
  1181. m1.swap (m2);
  1182. }
  1183. // Iterator types
  1184. private:
  1185. // Use the matrix iterator
  1186. typedef typename M::const_iterator1 const_subiterator1_type;
  1187. typedef typename boost::mpl::if_<boost::is_const<M>,
  1188. typename M::const_iterator1,
  1189. typename M::iterator1>::type subiterator1_type;
  1190. typedef typename M::const_iterator2 const_subiterator2_type;
  1191. typedef typename boost::mpl::if_<boost::is_const<M>,
  1192. typename M::const_iterator2,
  1193. typename M::iterator2>::type subiterator2_type;
  1194. public:
  1195. #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1196. typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1;
  1197. typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2;
  1198. typedef indexed_const_iterator1<self_type, packed_random_access_iterator_tag> const_iterator1;
  1199. typedef indexed_const_iterator2<self_type, packed_random_access_iterator_tag> const_iterator2;
  1200. #else
  1201. class const_iterator1;
  1202. class iterator1;
  1203. class const_iterator2;
  1204. class iterator2;
  1205. #endif
  1206. typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
  1207. typedef reverse_iterator_base1<iterator1> reverse_iterator1;
  1208. typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
  1209. typedef reverse_iterator_base2<iterator2> reverse_iterator2;
  1210. // Element lookup
  1211. BOOST_UBLAS_INLINE
  1212. const_iterator1 find1 (int rank, size_type i, size_type j) const {
  1213. if (rank == 1) {
  1214. size_type lower_i = (std::max) (difference_type (j - upper_), difference_type (0));
  1215. i = (std::max) (i, lower_i);
  1216. size_type upper_i = (std::min) (j + 1 + lower_, size1 ());
  1217. i = (std::min) (i, upper_i);
  1218. }
  1219. return const_iterator1 (*this, data ().find1 (rank, i, j));
  1220. }
  1221. BOOST_UBLAS_INLINE
  1222. iterator1 find1 (int rank, size_type i, size_type j) {
  1223. if (rank == 1) {
  1224. size_type lower_i = (std::max) (difference_type (j - upper_), difference_type (0));
  1225. i = (std::max) (i, lower_i);
  1226. size_type upper_i = (std::min) (j + 1 + lower_, size1 ());
  1227. i = (std::min) (i, upper_i);
  1228. }
  1229. return iterator1 (*this, data ().find1 (rank, i, j));
  1230. }
  1231. BOOST_UBLAS_INLINE
  1232. const_iterator2 find2 (int rank, size_type i, size_type j) const {
  1233. if (rank == 1) {
  1234. size_type lower_j = (std::max) (difference_type (i - lower_), difference_type (0));
  1235. j = (std::max) (j, lower_j);
  1236. size_type upper_j = (std::min) (i + 1 + upper_, size2 ());
  1237. j = (std::min) (j, upper_j);
  1238. }
  1239. return const_iterator2 (*this, data ().find2 (rank, i, j));
  1240. }
  1241. BOOST_UBLAS_INLINE
  1242. iterator2 find2 (int rank, size_type i, size_type j) {
  1243. if (rank == 1) {
  1244. size_type lower_j = (std::max) (difference_type (i - lower_), difference_type (0));
  1245. j = (std::max) (j, lower_j);
  1246. size_type upper_j = (std::min) (i + 1 + upper_, size2 ());
  1247. j = (std::min) (j, upper_j);
  1248. }
  1249. return iterator2 (*this, data ().find2 (rank, i, j));
  1250. }
  1251. // Iterators simply are indices.
  1252. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1253. class const_iterator1:
  1254. public container_const_reference<banded_adaptor>,
  1255. public random_access_iterator_base<typename iterator_restrict_traits<
  1256. typename const_subiterator1_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
  1257. const_iterator1, value_type> {
  1258. public:
  1259. typedef typename const_subiterator1_type::value_type value_type;
  1260. typedef typename const_subiterator1_type::difference_type difference_type;
  1261. typedef typename const_subiterator1_type::reference reference;
  1262. typedef typename const_subiterator1_type::pointer pointer;
  1263. typedef const_iterator2 dual_iterator_type;
  1264. typedef const_reverse_iterator2 dual_reverse_iterator_type;
  1265. // Construction and destruction
  1266. BOOST_UBLAS_INLINE
  1267. const_iterator1 ():
  1268. container_const_reference<self_type> (), it1_ () {}
  1269. BOOST_UBLAS_INLINE
  1270. const_iterator1 (const self_type &m, const const_subiterator1_type &it1):
  1271. container_const_reference<self_type> (m), it1_ (it1) {}
  1272. BOOST_UBLAS_INLINE
  1273. const_iterator1 (const iterator1 &it):
  1274. container_const_reference<self_type> (it ()), it1_ (it.it1_) {}
  1275. // Arithmetic
  1276. BOOST_UBLAS_INLINE
  1277. const_iterator1 &operator ++ () {
  1278. ++ it1_;
  1279. return *this;
  1280. }
  1281. BOOST_UBLAS_INLINE
  1282. const_iterator1 &operator -- () {
  1283. -- it1_;
  1284. return *this;
  1285. }
  1286. BOOST_UBLAS_INLINE
  1287. const_iterator1 &operator += (difference_type n) {
  1288. it1_ += n;
  1289. return *this;
  1290. }
  1291. BOOST_UBLAS_INLINE
  1292. const_iterator1 &operator -= (difference_type n) {
  1293. it1_ -= n;
  1294. return *this;
  1295. }
  1296. BOOST_UBLAS_INLINE
  1297. difference_type operator - (const const_iterator1 &it) const {
  1298. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1299. return it1_ - it.it1_;
  1300. }
  1301. // Dereference
  1302. BOOST_UBLAS_INLINE
  1303. const_reference operator * () const {
  1304. size_type i = index1 ();
  1305. size_type j = index2 ();
  1306. BOOST_UBLAS_CHECK (i < (*this) ().size1 (), bad_index ());
  1307. BOOST_UBLAS_CHECK (j < (*this) ().size2 (), bad_index ());
  1308. #ifdef BOOST_UBLAS_OWN_BANDED
  1309. size_type k = (std::max) (i, j);
  1310. size_type l = (*this) ().lower () + j - i;
  1311. if (k < (std::max) ((*this) ().size1 (), (*this) ().size2 ()) &&
  1312. l < (*this) ().lower () + 1 + (*this) ().upper ())
  1313. return *it1_;
  1314. #else
  1315. size_type k = j;
  1316. size_type l = (*this) ().upper () + i - j;
  1317. if (k < (*this) ().size2 () &&
  1318. l < (*this) ().lower () + 1 + (*this) ().upper ())
  1319. return *it1_;
  1320. #endif
  1321. return (*this) () (i, j);
  1322. }
  1323. BOOST_UBLAS_INLINE
  1324. const_reference operator [] (difference_type n) const {
  1325. return *(*this + n);
  1326. }
  1327. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1328. BOOST_UBLAS_INLINE
  1329. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1330. typename self_type::
  1331. #endif
  1332. const_iterator2 begin () const {
  1333. return (*this) ().find2 (1, index1 (), 0);
  1334. }
  1335. BOOST_UBLAS_INLINE
  1336. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1337. typename self_type::
  1338. #endif
  1339. const_iterator2 end () const {
  1340. return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
  1341. }
  1342. BOOST_UBLAS_INLINE
  1343. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1344. typename self_type::
  1345. #endif
  1346. const_reverse_iterator2 rbegin () const {
  1347. return const_reverse_iterator2 (end ());
  1348. }
  1349. BOOST_UBLAS_INLINE
  1350. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1351. typename self_type::
  1352. #endif
  1353. const_reverse_iterator2 rend () const {
  1354. return const_reverse_iterator2 (begin ());
  1355. }
  1356. #endif
  1357. // Indices
  1358. BOOST_UBLAS_INLINE
  1359. size_type index1 () const {
  1360. return it1_.index1 ();
  1361. }
  1362. BOOST_UBLAS_INLINE
  1363. size_type index2 () const {
  1364. return it1_.index2 ();
  1365. }
  1366. // Assignment
  1367. BOOST_UBLAS_INLINE
  1368. const_iterator1 &operator = (const const_iterator1 &it) {
  1369. container_const_reference<self_type>::assign (&it ());
  1370. it1_ = it.it1_;
  1371. return *this;
  1372. }
  1373. // Comparison
  1374. BOOST_UBLAS_INLINE
  1375. bool operator == (const const_iterator1 &it) const {
  1376. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1377. return it1_ == it.it1_;
  1378. }
  1379. BOOST_UBLAS_INLINE
  1380. bool operator < (const const_iterator1 &it) const {
  1381. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1382. return it1_ < it.it1_;
  1383. }
  1384. private:
  1385. const_subiterator1_type it1_;
  1386. };
  1387. #endif
  1388. BOOST_UBLAS_INLINE
  1389. const_iterator1 begin1 () const {
  1390. return find1 (0, 0, 0);
  1391. }
  1392. BOOST_UBLAS_INLINE
  1393. const_iterator1 end1 () const {
  1394. return find1 (0, size1 (), 0);
  1395. }
  1396. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1397. class iterator1:
  1398. public container_reference<banded_adaptor>,
  1399. public random_access_iterator_base<typename iterator_restrict_traits<
  1400. typename subiterator1_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
  1401. iterator1, value_type> {
  1402. public:
  1403. typedef typename subiterator1_type::value_type value_type;
  1404. typedef typename subiterator1_type::difference_type difference_type;
  1405. typedef typename subiterator1_type::reference reference;
  1406. typedef typename subiterator1_type::pointer pointer;
  1407. typedef iterator2 dual_iterator_type;
  1408. typedef reverse_iterator2 dual_reverse_iterator_type;
  1409. // Construction and destruction
  1410. BOOST_UBLAS_INLINE
  1411. iterator1 ():
  1412. container_reference<self_type> (), it1_ () {}
  1413. BOOST_UBLAS_INLINE
  1414. iterator1 (self_type &m, const subiterator1_type &it1):
  1415. container_reference<self_type> (m), it1_ (it1) {}
  1416. // Arithmetic
  1417. BOOST_UBLAS_INLINE
  1418. iterator1 &operator ++ () {
  1419. ++ it1_;
  1420. return *this;
  1421. }
  1422. BOOST_UBLAS_INLINE
  1423. iterator1 &operator -- () {
  1424. -- it1_;
  1425. return *this;
  1426. }
  1427. BOOST_UBLAS_INLINE
  1428. iterator1 &operator += (difference_type n) {
  1429. it1_ += n;
  1430. return *this;
  1431. }
  1432. BOOST_UBLAS_INLINE
  1433. iterator1 &operator -= (difference_type n) {
  1434. it1_ -= n;
  1435. return *this;
  1436. }
  1437. BOOST_UBLAS_INLINE
  1438. difference_type operator - (const iterator1 &it) const {
  1439. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1440. return it1_ - it.it1_;
  1441. }
  1442. // Dereference
  1443. BOOST_UBLAS_INLINE
  1444. reference operator * () const {
  1445. size_type i = index1 ();
  1446. size_type j = index2 ();
  1447. BOOST_UBLAS_CHECK (i < (*this) ().size1 (), bad_index ());
  1448. BOOST_UBLAS_CHECK (j < (*this) ().size2 (), bad_index ());
  1449. #ifdef BOOST_UBLAS_OWN_BANDED
  1450. size_type k = (std::max) (i, j);
  1451. size_type l = (*this) ().lower () + j - i;
  1452. if (k < (std::max) ((*this) ().size1 (), (*this) ().size2 ()) &&
  1453. l < (*this) ().lower () + 1 + (*this) ().upper ())
  1454. return *it1_;
  1455. #else
  1456. size_type k = j;
  1457. size_type l = (*this) ().upper () + i - j;
  1458. if (k < (*this) ().size2 () &&
  1459. l < (*this) ().lower () + 1 + (*this) ().upper ())
  1460. return *it1_;
  1461. #endif
  1462. return (*this) () (i, j);
  1463. }
  1464. BOOST_UBLAS_INLINE
  1465. reference operator [] (difference_type n) const {
  1466. return *(*this + n);
  1467. }
  1468. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1469. BOOST_UBLAS_INLINE
  1470. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1471. typename self_type::
  1472. #endif
  1473. iterator2 begin () const {
  1474. return (*this) ().find2 (1, index1 (), 0);
  1475. }
  1476. BOOST_UBLAS_INLINE
  1477. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1478. typename self_type::
  1479. #endif
  1480. iterator2 end () const {
  1481. return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
  1482. }
  1483. BOOST_UBLAS_INLINE
  1484. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1485. typename self_type::
  1486. #endif
  1487. reverse_iterator2 rbegin () const {
  1488. return reverse_iterator2 (end ());
  1489. }
  1490. BOOST_UBLAS_INLINE
  1491. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1492. typename self_type::
  1493. #endif
  1494. reverse_iterator2 rend () const {
  1495. return reverse_iterator2 (begin ());
  1496. }
  1497. #endif
  1498. // Indices
  1499. BOOST_UBLAS_INLINE
  1500. size_type index1 () const {
  1501. return it1_.index1 ();
  1502. }
  1503. BOOST_UBLAS_INLINE
  1504. size_type index2 () const {
  1505. return it1_.index2 ();
  1506. }
  1507. // Assignment
  1508. BOOST_UBLAS_INLINE
  1509. iterator1 &operator = (const iterator1 &it) {
  1510. container_reference<self_type>::assign (&it ());
  1511. it1_ = it.it1_;
  1512. return *this;
  1513. }
  1514. // Comparison
  1515. BOOST_UBLAS_INLINE
  1516. bool operator == (const iterator1 &it) const {
  1517. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1518. return it1_ == it.it1_;
  1519. }
  1520. BOOST_UBLAS_INLINE
  1521. bool operator < (const iterator1 &it) const {
  1522. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1523. return it1_ < it.it1_;
  1524. }
  1525. private:
  1526. subiterator1_type it1_;
  1527. friend class const_iterator1;
  1528. };
  1529. #endif
  1530. BOOST_UBLAS_INLINE
  1531. iterator1 begin1 () {
  1532. return find1 (0, 0, 0);
  1533. }
  1534. BOOST_UBLAS_INLINE
  1535. iterator1 end1 () {
  1536. return find1 (0, size1 (), 0);
  1537. }
  1538. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1539. class const_iterator2:
  1540. public container_const_reference<banded_adaptor>,
  1541. public random_access_iterator_base<packed_random_access_iterator_tag,
  1542. const_iterator2, value_type> {
  1543. public:
  1544. typedef typename iterator_restrict_traits<typename const_subiterator2_type::iterator_category,
  1545. packed_random_access_iterator_tag>::iterator_category iterator_category;
  1546. typedef typename const_subiterator2_type::value_type value_type;
  1547. typedef typename const_subiterator2_type::difference_type difference_type;
  1548. typedef typename const_subiterator2_type::reference reference;
  1549. typedef typename const_subiterator2_type::pointer pointer;
  1550. typedef const_iterator1 dual_iterator_type;
  1551. typedef const_reverse_iterator1 dual_reverse_iterator_type;
  1552. // Construction and destruction
  1553. BOOST_UBLAS_INLINE
  1554. const_iterator2 ():
  1555. container_const_reference<self_type> (), it2_ () {}
  1556. BOOST_UBLAS_INLINE
  1557. const_iterator2 (const self_type &m, const const_subiterator2_type &it2):
  1558. container_const_reference<self_type> (m), it2_ (it2) {}
  1559. BOOST_UBLAS_INLINE
  1560. const_iterator2 (const iterator2 &it):
  1561. container_const_reference<self_type> (it ()), it2_ (it.it2_) {}
  1562. // Arithmetic
  1563. BOOST_UBLAS_INLINE
  1564. const_iterator2 &operator ++ () {
  1565. ++ it2_;
  1566. return *this;
  1567. }
  1568. BOOST_UBLAS_INLINE
  1569. const_iterator2 &operator -- () {
  1570. -- it2_;
  1571. return *this;
  1572. }
  1573. BOOST_UBLAS_INLINE
  1574. const_iterator2 &operator += (difference_type n) {
  1575. it2_ += n;
  1576. return *this;
  1577. }
  1578. BOOST_UBLAS_INLINE
  1579. const_iterator2 &operator -= (difference_type n) {
  1580. it2_ -= n;
  1581. return *this;
  1582. }
  1583. BOOST_UBLAS_INLINE
  1584. difference_type operator - (const const_iterator2 &it) const {
  1585. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1586. return it2_ - it.it2_;
  1587. }
  1588. // Dereference
  1589. BOOST_UBLAS_INLINE
  1590. const_reference operator * () const {
  1591. size_type i = index1 ();
  1592. size_type j = index2 ();
  1593. BOOST_UBLAS_CHECK (i < (*this) ().size1 (), bad_index ());
  1594. BOOST_UBLAS_CHECK (j < (*this) ().size2 (), bad_index ());
  1595. #ifdef BOOST_UBLAS_OWN_BANDED
  1596. size_type k = (std::max) (i, j);
  1597. size_type l = (*this) ().lower () + j - i;
  1598. if (k < (std::max) ((*this) ().size1 (), (*this) ().size2 ()) &&
  1599. l < (*this) ().lower () + 1 + (*this) ().upper ())
  1600. return *it2_;
  1601. #else
  1602. size_type k = j;
  1603. size_type l = (*this) ().upper () + i - j;
  1604. if (k < (*this) ().size2 () &&
  1605. l < (*this) ().lower () + 1 + (*this) ().upper ())
  1606. return *it2_;
  1607. #endif
  1608. return (*this) () (i, j);
  1609. }
  1610. BOOST_UBLAS_INLINE
  1611. const_reference operator [] (difference_type n) const {
  1612. return *(*this + n);
  1613. }
  1614. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1615. BOOST_UBLAS_INLINE
  1616. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1617. typename self_type::
  1618. #endif
  1619. const_iterator1 begin () const {
  1620. return (*this) ().find1 (1, 0, index2 ());
  1621. }
  1622. BOOST_UBLAS_INLINE
  1623. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1624. typename self_type::
  1625. #endif
  1626. const_iterator1 end () const {
  1627. return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
  1628. }
  1629. BOOST_UBLAS_INLINE
  1630. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1631. typename self_type::
  1632. #endif
  1633. const_reverse_iterator1 rbegin () const {
  1634. return const_reverse_iterator1 (end ());
  1635. }
  1636. BOOST_UBLAS_INLINE
  1637. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1638. typename self_type::
  1639. #endif
  1640. const_reverse_iterator1 rend () const {
  1641. return const_reverse_iterator1 (begin ());
  1642. }
  1643. #endif
  1644. // Indices
  1645. BOOST_UBLAS_INLINE
  1646. size_type index1 () const {
  1647. return it2_.index1 ();
  1648. }
  1649. BOOST_UBLAS_INLINE
  1650. size_type index2 () const {
  1651. return it2_.index2 ();
  1652. }
  1653. // Assignment
  1654. BOOST_UBLAS_INLINE
  1655. const_iterator2 &operator = (const const_iterator2 &it) {
  1656. container_const_reference<self_type>::assign (&it ());
  1657. it2_ = it.it2_;
  1658. return *this;
  1659. }
  1660. // Comparison
  1661. BOOST_UBLAS_INLINE
  1662. bool operator == (const const_iterator2 &it) const {
  1663. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1664. return it2_ == it.it2_;
  1665. }
  1666. BOOST_UBLAS_INLINE
  1667. bool operator < (const const_iterator2 &it) const {
  1668. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1669. return it2_ < it.it2_;
  1670. }
  1671. private:
  1672. const_subiterator2_type it2_;
  1673. };
  1674. #endif
  1675. BOOST_UBLAS_INLINE
  1676. const_iterator2 begin2 () const {
  1677. return find2 (0, 0, 0);
  1678. }
  1679. BOOST_UBLAS_INLINE
  1680. const_iterator2 end2 () const {
  1681. return find2 (0, 0, size2 ());
  1682. }
  1683. #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
  1684. class iterator2:
  1685. public container_reference<banded_adaptor>,
  1686. public random_access_iterator_base<typename iterator_restrict_traits<
  1687. typename subiterator2_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
  1688. iterator2, value_type> {
  1689. public:
  1690. typedef typename subiterator2_type::value_type value_type;
  1691. typedef typename subiterator2_type::difference_type difference_type;
  1692. typedef typename subiterator2_type::reference reference;
  1693. typedef typename subiterator2_type::pointer pointer;
  1694. typedef iterator1 dual_iterator_type;
  1695. typedef reverse_iterator1 dual_reverse_iterator_type;
  1696. // Construction and destruction
  1697. BOOST_UBLAS_INLINE
  1698. iterator2 ():
  1699. container_reference<self_type> (), it2_ () {}
  1700. BOOST_UBLAS_INLINE
  1701. iterator2 (self_type &m, const subiterator2_type &it2):
  1702. container_reference<self_type> (m), it2_ (it2) {}
  1703. // Arithmetic
  1704. BOOST_UBLAS_INLINE
  1705. iterator2 &operator ++ () {
  1706. ++ it2_;
  1707. return *this;
  1708. }
  1709. BOOST_UBLAS_INLINE
  1710. iterator2 &operator -- () {
  1711. -- it2_;
  1712. return *this;
  1713. }
  1714. BOOST_UBLAS_INLINE
  1715. iterator2 &operator += (difference_type n) {
  1716. it2_ += n;
  1717. return *this;
  1718. }
  1719. BOOST_UBLAS_INLINE
  1720. iterator2 &operator -= (difference_type n) {
  1721. it2_ -= n;
  1722. return *this;
  1723. }
  1724. BOOST_UBLAS_INLINE
  1725. difference_type operator - (const iterator2 &it) const {
  1726. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1727. return it2_ - it.it2_;
  1728. }
  1729. // Dereference
  1730. BOOST_UBLAS_INLINE
  1731. reference operator * () const {
  1732. size_type i = index1 ();
  1733. size_type j = index2 ();
  1734. BOOST_UBLAS_CHECK (i < (*this) ().size1 (), bad_index ());
  1735. BOOST_UBLAS_CHECK (j < (*this) ().size2 (), bad_index ());
  1736. #ifdef BOOST_UBLAS_OWN_BANDED
  1737. size_type k = (std::max) (i, j);
  1738. size_type l = (*this) ().lower () + j - i;
  1739. if (k < (std::max) ((*this) ().size1 (), (*this) ().size2 ()) &&
  1740. l < (*this) ().lower () + 1 + (*this) ().upper ())
  1741. return *it2_;
  1742. #else
  1743. size_type k = j;
  1744. size_type l = (*this) ().upper () + i - j;
  1745. if (k < (*this) ().size2 () &&
  1746. l < (*this) ().lower () + 1 + (*this) ().upper ())
  1747. return *it2_;
  1748. #endif
  1749. return (*this) () (i, j);
  1750. }
  1751. BOOST_UBLAS_INLINE
  1752. reference operator [] (difference_type n) const {
  1753. return *(*this + n);
  1754. }
  1755. #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
  1756. BOOST_UBLAS_INLINE
  1757. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1758. typename self_type::
  1759. #endif
  1760. iterator1 begin () const {
  1761. return (*this) ().find1 (1, 0, index2 ());
  1762. }
  1763. BOOST_UBLAS_INLINE
  1764. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1765. typename self_type::
  1766. #endif
  1767. iterator1 end () const {
  1768. return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
  1769. }
  1770. BOOST_UBLAS_INLINE
  1771. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1772. typename self_type::
  1773. #endif
  1774. reverse_iterator1 rbegin () const {
  1775. return reverse_iterator1 (end ());
  1776. }
  1777. BOOST_UBLAS_INLINE
  1778. #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
  1779. typename self_type::
  1780. #endif
  1781. reverse_iterator1 rend () const {
  1782. return reverse_iterator1 (begin ());
  1783. }
  1784. #endif
  1785. // Indices
  1786. BOOST_UBLAS_INLINE
  1787. size_type index1 () const {
  1788. return it2_.index1 ();
  1789. }
  1790. BOOST_UBLAS_INLINE
  1791. size_type index2 () const {
  1792. return it2_.index2 ();
  1793. }
  1794. // Assignment
  1795. BOOST_UBLAS_INLINE
  1796. iterator2 &operator = (const iterator2 &it) {
  1797. container_reference<self_type>::assign (&it ());
  1798. it2_ = it.it2_;
  1799. return *this;
  1800. }
  1801. // Comparison
  1802. BOOST_UBLAS_INLINE
  1803. bool operator == (const iterator2 &it) const {
  1804. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1805. return it2_ == it.it2_;
  1806. }
  1807. BOOST_UBLAS_INLINE
  1808. bool operator < (const iterator2 &it) const {
  1809. BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
  1810. return it2_ < it.it2_;
  1811. }
  1812. private:
  1813. subiterator2_type it2_;
  1814. friend class const_iterator2;
  1815. };
  1816. #endif
  1817. BOOST_UBLAS_INLINE
  1818. iterator2 begin2 () {
  1819. return find2 (0, 0, 0);
  1820. }
  1821. BOOST_UBLAS_INLINE
  1822. iterator2 end2 () {
  1823. return find2 (0, 0, size2 ());
  1824. }
  1825. // Reverse iterators
  1826. BOOST_UBLAS_INLINE
  1827. const_reverse_iterator1 rbegin1 () const {
  1828. return const_reverse_iterator1 (end1 ());
  1829. }
  1830. BOOST_UBLAS_INLINE
  1831. const_reverse_iterator1 rend1 () const {
  1832. return const_reverse_iterator1 (begin1 ());
  1833. }
  1834. BOOST_UBLAS_INLINE
  1835. reverse_iterator1 rbegin1 () {
  1836. return reverse_iterator1 (end1 ());
  1837. }
  1838. BOOST_UBLAS_INLINE
  1839. reverse_iterator1 rend1 () {
  1840. return reverse_iterator1 (begin1 ());
  1841. }
  1842. BOOST_UBLAS_INLINE
  1843. const_reverse_iterator2 rbegin2 () const {
  1844. return const_reverse_iterator2 (end2 ());
  1845. }
  1846. BOOST_UBLAS_INLINE
  1847. const_reverse_iterator2 rend2 () const {
  1848. return const_reverse_iterator2 (begin2 ());
  1849. }
  1850. BOOST_UBLAS_INLINE
  1851. reverse_iterator2 rbegin2 () {
  1852. return reverse_iterator2 (end2 ());
  1853. }
  1854. BOOST_UBLAS_INLINE
  1855. reverse_iterator2 rend2 () {
  1856. return reverse_iterator2 (begin2 ());
  1857. }
  1858. private:
  1859. matrix_closure_type data_;
  1860. size_type lower_;
  1861. size_type upper_;
  1862. typedef const value_type const_value_type;
  1863. static const_value_type zero_;
  1864. };
  1865. // Specialization for temporary_traits
  1866. template <class M>
  1867. struct vector_temporary_traits< banded_adaptor<M> >
  1868. : vector_temporary_traits< M > {} ;
  1869. template <class M>
  1870. struct vector_temporary_traits< const banded_adaptor<M> >
  1871. : vector_temporary_traits< M > {} ;
  1872. template <class M>
  1873. struct matrix_temporary_traits< banded_adaptor<M> >
  1874. : matrix_temporary_traits< M > {} ;
  1875. template <class M>
  1876. struct matrix_temporary_traits< const banded_adaptor<M> >
  1877. : matrix_temporary_traits< M > {} ;
  1878. template<class M>
  1879. typename banded_adaptor<M>::const_value_type banded_adaptor<M>::zero_ = value_type/*zero*/();
  1880. /** \brief A diagonal matrix adaptator: convert a any matrix into a diagonal matrix expression
  1881. *
  1882. * For a \f$(m\times m)\f$-dimensional matrix, the \c diagonal_adaptor will provide a diagonal matrix
  1883. * with \f$0 \leq i < m\f$ and \f$0 \leq j < m\f$, if \f$i\neq j\f$ then \f$b_{i,j}=0\f$.
  1884. *
  1885. * Storage and location are based on those of the underlying matrix. This is important because
  1886. * a \c diagonal_adaptor does not copy the matrix data to a new place. Therefore, modifying values
  1887. * in a \c diagonal_adaptor matrix will also modify the underlying matrix too.
  1888. *
  1889. * \tparam M the type of matrix used to generate the diagonal matrix
  1890. */
  1891. template<class M>
  1892. class diagonal_adaptor:
  1893. public banded_adaptor<M> {
  1894. public:
  1895. typedef M matrix_type;
  1896. typedef banded_adaptor<M> adaptor_type;
  1897. // Construction and destruction
  1898. BOOST_UBLAS_INLINE
  1899. diagonal_adaptor ():
  1900. adaptor_type () {}
  1901. BOOST_UBLAS_INLINE
  1902. diagonal_adaptor (matrix_type &data):
  1903. adaptor_type (data) {}
  1904. BOOST_UBLAS_INLINE
  1905. ~diagonal_adaptor () {}
  1906. // Assignment
  1907. BOOST_UBLAS_INLINE
  1908. diagonal_adaptor &operator = (const diagonal_adaptor &m) {
  1909. adaptor_type::operator = (m);
  1910. return *this;
  1911. }
  1912. template<class AE>
  1913. BOOST_UBLAS_INLINE
  1914. diagonal_adaptor &operator = (const matrix_expression<AE> &ae) {
  1915. adaptor_type::operator = (ae);
  1916. return *this;
  1917. }
  1918. };
  1919. }}}
  1920. #endif