2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
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)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
13 #ifndef _BOOST_UBLAS_SYMMETRIC_
14 #define _BOOST_UBLAS_SYMMETRIC_
16 #include <boost/numeric/ublas/matrix.hpp>
17 #include <boost/numeric/ublas/triangular.hpp>
18 #include <boost/numeric/ublas/detail/temporary.hpp>
20 // Iterators based on ideas of Jeremy Siek
21 // Symmetric matrices are square. Thanks to Peter Schmitteckert for spotting this.
23 namespace boost { namespace numeric { namespace ublas {
26 bool is_symmetric (const M &m) {
27 typedef typename M::size_type size_type;
29 if (m.size1 () != m.size2 ())
31 size_type size = BOOST_UBLAS_SAME (m.size1 (), m.size2 ());
32 for (size_type i = 0; i < size; ++ i) {
33 for (size_type j = i; j < size; ++ j) {
34 if (m (i, j) != m (j, i))
41 // Array based symmetric matrix class
42 template<class T, class TRI, class L, class A>
43 class symmetric_matrix:
44 public matrix_container<symmetric_matrix<T, TRI, L, A> > {
47 typedef TRI triangular_type;
48 typedef L layout_type;
49 typedef symmetric_matrix<T, TRI, L, A> self_type;
51 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
52 using matrix_container<self_type>::operator ();
54 typedef typename A::size_type size_type;
55 typedef typename A::difference_type difference_type;
57 typedef const T &const_reference;
61 typedef const matrix_reference<const self_type> const_closure_type;
62 typedef matrix_reference<self_type> closure_type;
63 typedef vector<T, A> vector_temporary_type;
64 typedef matrix<T, L, A> matrix_temporary_type; // general sub-matrix
65 typedef packed_tag storage_category;
66 typedef typename L::orientation_category orientation_category;
68 // Construction and destruction
71 matrix_container<self_type> (),
72 size_ (0), data_ (0) {}
74 symmetric_matrix (size_type size):
75 matrix_container<self_type> (),
76 size_ (BOOST_UBLAS_SAME (size, size)), data_ (triangular_type::packed_size (layout_type (), size, size)) {
79 symmetric_matrix (size_type size1, size_type size2):
80 matrix_container<self_type> (),
81 size_ (BOOST_UBLAS_SAME (size1, size2)), data_ (triangular_type::packed_size (layout_type (), size1, size2)) {
84 symmetric_matrix (size_type size, const array_type &data):
85 matrix_container<self_type> (),
86 size_ (size), data_ (data) {}
88 symmetric_matrix (const symmetric_matrix &m):
89 matrix_container<self_type> (),
90 size_ (m.size_), data_ (m.data_) {}
93 symmetric_matrix (const matrix_expression<AE> &ae):
94 matrix_container<self_type> (),
95 size_ (BOOST_UBLAS_SAME (ae ().size1 (), ae ().size2 ())),
96 data_ (triangular_type::packed_size (layout_type (), size_, size_)) {
97 matrix_assign<scalar_assign> (*this, ae);
102 size_type size1 () const {
106 size_type size2 () const {
112 const array_type &data () const {
116 array_type &data () {
122 void resize (size_type size, bool preserve = true) {
124 self_type temporary (size, size);
125 detail::matrix_resize_preserve<layout_type, triangular_type> (*this, temporary);
128 data ().resize (triangular_type::packed_size (layout_type (), size, size));
133 void resize (size_type size1, size_type size2, bool preserve = true) {
134 resize (BOOST_UBLAS_SAME (size1, size2), preserve);
137 void resize_packed_preserve (size_type size) {
138 size_ = BOOST_UBLAS_SAME (size, size);
139 data ().resize (triangular_type::packed_size (layout_type (), size_, size_), value_type ());
144 const_reference operator () (size_type i, size_type j) const {
145 BOOST_UBLAS_CHECK (i < size_, bad_index ());
146 BOOST_UBLAS_CHECK (j < size_, bad_index ());
147 if (triangular_type::other (i, j))
148 return data () [triangular_type::element (layout_type (), i, size_, j, size_)];
150 return data () [triangular_type::element (layout_type (), j, size_, i, size_)];
153 reference at_element (size_type i, size_type j) {
154 BOOST_UBLAS_CHECK (i < size_, bad_index ());
155 BOOST_UBLAS_CHECK (j < size_, bad_index ());
156 return data () [triangular_type::element (layout_type (), i, size_, j, size_)];
159 reference operator () (size_type i, size_type j) {
160 BOOST_UBLAS_CHECK (i < size_, bad_index ());
161 BOOST_UBLAS_CHECK (j < size_, bad_index ());
162 if (triangular_type::other (i, j))
163 return data () [triangular_type::element (layout_type (), i, size_, j, size_)];
165 return data () [triangular_type::element (layout_type (), j, size_, i, size_)];
168 // Element assignment
170 reference insert_element (size_type i, size_type j, const_reference t) {
171 return (operator () (i, j) = t);
174 void erase_element (size_type i, size_type j) {
175 operator () (i, j) = value_type/*zero*/();
182 std::fill (data ().begin (), data ().end (), value_type/*zero*/());
187 symmetric_matrix &operator = (const symmetric_matrix &m) {
193 symmetric_matrix &assign_temporary (symmetric_matrix &m) {
199 symmetric_matrix &operator = (const matrix_expression<AE> &ae) {
200 self_type temporary (ae);
201 return assign_temporary (temporary);
205 symmetric_matrix &assign (const matrix_expression<AE> &ae) {
206 matrix_assign<scalar_assign> (*this, ae);
211 symmetric_matrix& operator += (const matrix_expression<AE> &ae) {
212 self_type temporary (*this + ae);
213 return assign_temporary (temporary);
217 symmetric_matrix &plus_assign (const matrix_expression<AE> &ae) {
218 matrix_assign<scalar_plus_assign> (*this, ae);
223 symmetric_matrix& operator -= (const matrix_expression<AE> &ae) {
224 self_type temporary (*this - ae);
225 return assign_temporary (temporary);
229 symmetric_matrix &minus_assign (const matrix_expression<AE> &ae) {
230 matrix_assign<scalar_minus_assign> (*this, ae);
235 symmetric_matrix& operator *= (const AT &at) {
236 matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
241 symmetric_matrix& operator /= (const AT &at) {
242 matrix_assign_scalar<scalar_divides_assign> (*this, at);
248 void swap (symmetric_matrix &m) {
250 std::swap (size_, m.size_);
251 data ().swap (m.data ());
255 friend void swap (symmetric_matrix &m1, symmetric_matrix &m2) {
260 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
261 typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1;
262 typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2;
263 typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
264 typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
266 class const_iterator1;
268 class const_iterator2;
271 typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
272 typedef reverse_iterator_base1<iterator1> reverse_iterator1;
273 typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
274 typedef reverse_iterator_base2<iterator2> reverse_iterator2;
278 const_iterator1 find1 (int /* rank */, size_type i, size_type j) const {
279 return const_iterator1 (*this, i, j);
282 iterator1 find1 (int rank, size_type i, size_type j) {
284 i = triangular_type::mutable_restrict1 (i, j, size1(), size2());
286 i = triangular_type::global_mutable_restrict1 (i, size1(), j, size2());
287 return iterator1 (*this, i, j);
290 const_iterator2 find2 (int /* rank */, size_type i, size_type j) const {
291 return const_iterator2 (*this, i, j);
294 iterator2 find2 (int rank, size_type i, size_type j) {
296 j = triangular_type::mutable_restrict2 (i, j, size1(), size2());
298 j = triangular_type::global_mutable_restrict2 (i, size1(), j, size2());
299 return iterator2 (*this, i, j);
302 // Iterators simply are indices.
304 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
305 class const_iterator1:
306 public container_const_reference<symmetric_matrix>,
307 public random_access_iterator_base<dense_random_access_iterator_tag,
308 const_iterator1, value_type> {
310 typedef typename symmetric_matrix::value_type value_type;
311 typedef typename symmetric_matrix::difference_type difference_type;
312 typedef typename symmetric_matrix::const_reference reference;
313 typedef const typename symmetric_matrix::pointer pointer;
315 typedef const_iterator2 dual_iterator_type;
316 typedef const_reverse_iterator2 dual_reverse_iterator_type;
318 // Construction and destruction
321 container_const_reference<self_type> (), it1_ (), it2_ () {}
323 const_iterator1 (const self_type &m, size_type it1, size_type it2):
324 container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
326 const_iterator1 (const iterator1 &it):
327 container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {}
331 const_iterator1 &operator ++ () {
336 const_iterator1 &operator -- () {
341 const_iterator1 &operator += (difference_type n) {
346 const_iterator1 &operator -= (difference_type n) {
351 difference_type operator - (const const_iterator1 &it) const {
352 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
353 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
354 return it1_ - it.it1_;
359 const_reference operator * () const {
360 return (*this) () (it1_, it2_);
363 const_reference operator [] (difference_type n) const {
367 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
369 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
372 const_iterator2 begin () const {
373 return (*this) ().find2 (1, it1_, 0);
376 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
379 const_iterator2 cbegin () const {
383 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
386 const_iterator2 end () const {
387 return (*this) ().find2 (1, it1_, (*this) ().size2 ());
390 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
393 const_iterator2 cend () const {
397 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
400 const_reverse_iterator2 rbegin () const {
401 return const_reverse_iterator2 (end ());
404 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
407 const_reverse_iterator2 crbegin () const {
411 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
414 const_reverse_iterator2 rend () const {
415 return const_reverse_iterator2 (begin ());
418 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
421 const_reverse_iterator2 crend () const {
428 size_type index1 () const {
432 size_type index2 () const {
438 const_iterator1 &operator = (const const_iterator1 &it) {
439 container_const_reference<self_type>::assign (&it ());
447 bool operator == (const const_iterator1 &it) const {
448 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
449 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
450 return it1_ == it.it1_;
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_;
466 const_iterator1 begin1 () const {
467 return find1 (0, 0, 0);
470 const_iterator1 cbegin1 () const {
474 const_iterator1 end1 () const {
475 return find1 (0, size_, 0);
478 const_iterator1 cend1 () const {
482 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
484 public container_reference<symmetric_matrix>,
485 public random_access_iterator_base<packed_random_access_iterator_tag,
486 iterator1, value_type> {
488 typedef typename symmetric_matrix::value_type value_type;
489 typedef typename symmetric_matrix::difference_type difference_type;
490 typedef typename symmetric_matrix::reference reference;
491 typedef typename symmetric_matrix::pointer pointer;
492 typedef iterator2 dual_iterator_type;
493 typedef reverse_iterator2 dual_reverse_iterator_type;
495 // Construction and destruction
498 container_reference<self_type> (), it1_ (), it2_ () {}
500 iterator1 (self_type &m, size_type it1, size_type it2):
501 container_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
505 iterator1 &operator ++ () {
510 iterator1 &operator -- () {
515 iterator1 &operator += (difference_type n) {
520 iterator1 &operator -= (difference_type n) {
525 difference_type operator - (const iterator1 &it) const {
526 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
527 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
528 return it1_ - it.it1_;
533 reference operator * () const {
534 return (*this) () (it1_, it2_);
537 reference operator [] (difference_type n) const {
541 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
543 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
546 iterator2 begin () const {
547 return (*this) ().find2 (1, it1_, 0);
550 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
553 iterator2 end () const {
554 return (*this) ().find2 (1, it1_, (*this) ().size2 ());
557 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
560 reverse_iterator2 rbegin () const {
561 return reverse_iterator2 (end ());
564 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
567 reverse_iterator2 rend () const {
568 return reverse_iterator2 (begin ());
574 size_type index1 () const {
578 size_type index2 () const {
584 iterator1 &operator = (const iterator1 &it) {
585 container_reference<self_type>::assign (&it ());
593 bool operator == (const iterator1 &it) const {
594 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
595 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
596 return it1_ == it.it1_;
599 bool operator < (const iterator1 &it) const {
600 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
601 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
602 return it1_ < it.it1_;
609 friend class const_iterator1;
614 iterator1 begin1 () {
615 return find1 (0, 0, 0);
619 return find1 (0, size_, 0);
622 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
623 class const_iterator2:
624 public container_const_reference<symmetric_matrix>,
625 public random_access_iterator_base<dense_random_access_iterator_tag,
626 const_iterator2, value_type> {
628 typedef typename symmetric_matrix::value_type value_type;
629 typedef typename symmetric_matrix::difference_type difference_type;
630 typedef typename symmetric_matrix::const_reference reference;
631 typedef const typename symmetric_matrix::pointer pointer;
633 typedef const_iterator1 dual_iterator_type;
634 typedef const_reverse_iterator1 dual_reverse_iterator_type;
636 // Construction and destruction
639 container_const_reference<self_type> (), it1_ (), it2_ () {}
641 const_iterator2 (const self_type &m, size_type it1, size_type it2):
642 container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
644 const_iterator2 (const iterator2 &it):
645 container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {}
649 const_iterator2 &operator ++ () {
654 const_iterator2 &operator -- () {
659 const_iterator2 &operator += (difference_type n) {
664 const_iterator2 &operator -= (difference_type n) {
669 difference_type operator - (const const_iterator2 &it) const {
670 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
671 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
672 return it2_ - it.it2_;
677 const_reference operator * () const {
678 return (*this) () (it1_, it2_);
681 const_reference operator [] (difference_type n) const {
685 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
687 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
690 const_iterator1 begin () const {
691 return (*this) ().find1 (1, 0, it2_);
694 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
697 const_iterator1 cbegin () const {
701 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
704 const_iterator1 end () const {
705 return (*this) ().find1 (1, (*this) ().size1 (), it2_);
708 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
711 const_iterator1 cend () const {
715 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
718 const_reverse_iterator1 rbegin () const {
719 return const_reverse_iterator1 (end ());
722 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
725 const_reverse_iterator1 crbegin () const {
729 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
732 const_reverse_iterator1 rend () const {
733 return const_reverse_iterator1 (begin ());
736 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
739 const_reverse_iterator1 crend () const {
746 size_type index1 () const {
750 size_type index2 () const {
756 const_iterator2 &operator = (const const_iterator2 &it) {
757 container_const_reference<self_type>::assign (&it ());
765 bool operator == (const const_iterator2 &it) const {
766 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
767 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
768 return it2_ == it.it2_;
771 bool operator < (const const_iterator2 &it) const {
772 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
773 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
774 return it2_ < it.it2_;
784 const_iterator2 begin2 () const {
785 return find2 (0, 0, 0);
788 const_iterator2 cbegin2 () const {
792 const_iterator2 end2 () const {
793 return find2 (0, 0, size_);
796 const_iterator2 cend2 () const {
800 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
802 public container_reference<symmetric_matrix>,
803 public random_access_iterator_base<packed_random_access_iterator_tag,
804 iterator2, value_type> {
806 typedef typename symmetric_matrix::value_type value_type;
807 typedef typename symmetric_matrix::difference_type difference_type;
808 typedef typename symmetric_matrix::reference reference;
809 typedef typename symmetric_matrix::pointer pointer;
811 typedef iterator1 dual_iterator_type;
812 typedef reverse_iterator1 dual_reverse_iterator_type;
814 // Construction and destruction
817 container_reference<self_type> (), it1_ (), it2_ () {}
819 iterator2 (self_type &m, size_type it1, size_type it2):
820 container_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
824 iterator2 &operator ++ () {
829 iterator2 &operator -- () {
834 iterator2 &operator += (difference_type n) {
839 iterator2 &operator -= (difference_type n) {
844 difference_type operator - (const iterator2 &it) const {
845 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
846 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
847 return it2_ - it.it2_;
852 reference operator * () const {
853 return (*this) () (it1_, it2_);
856 reference operator [] (difference_type n) const {
860 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
862 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
865 iterator1 begin () const {
866 return (*this) ().find1 (1, 0, it2_);
869 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
872 iterator1 end () const {
873 return (*this) ().find1 (1, (*this) ().size1 (), it2_);
876 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
879 reverse_iterator1 rbegin () const {
880 return reverse_iterator1 (end ());
883 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
886 reverse_iterator1 rend () const {
887 return reverse_iterator1 (begin ());
893 size_type index1 () const {
897 size_type index2 () const {
903 iterator2 &operator = (const iterator2 &it) {
904 container_reference<self_type>::assign (&it ());
912 bool operator == (const iterator2 &it) const {
913 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
914 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
915 return it2_ == it.it2_;
918 bool operator < (const iterator2 &it) const {
919 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
920 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
921 return it2_ < it.it2_;
928 friend class const_iterator2;
933 iterator2 begin2 () {
934 return find2 (0, 0, 0);
938 return find2 (0, 0, size_);
944 const_reverse_iterator1 rbegin1 () const {
945 return const_reverse_iterator1 (end1 ());
948 const_reverse_iterator1 crbegin1 () const {
952 const_reverse_iterator1 rend1 () const {
953 return const_reverse_iterator1 (begin1 ());
956 const_reverse_iterator1 crend1 () const {
961 reverse_iterator1 rbegin1 () {
962 return reverse_iterator1 (end1 ());
965 reverse_iterator1 rend1 () {
966 return reverse_iterator1 (begin1 ());
970 const_reverse_iterator2 rbegin2 () const {
971 return const_reverse_iterator2 (end2 ());
974 const_reverse_iterator2 crbegin2 () const {
978 const_reverse_iterator2 rend2 () const {
979 return const_reverse_iterator2 (begin2 ());
982 const_reverse_iterator2 crend2 () const {
986 reverse_iterator2 rbegin2 () {
987 return reverse_iterator2 (end2 ());
990 reverse_iterator2 rend2 () {
991 return reverse_iterator2 (begin2 ());
1000 // Symmetric matrix adaptor class
1001 template<class M, class TRI>
1002 class symmetric_adaptor:
1003 public matrix_expression<symmetric_adaptor<M, TRI> > {
1005 typedef symmetric_adaptor<M, TRI> self_type;
1007 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
1008 using matrix_expression<self_type>::operator ();
1010 typedef const M const_matrix_type;
1011 typedef M matrix_type;
1012 typedef TRI triangular_type;
1013 typedef typename M::size_type size_type;
1014 typedef typename M::difference_type difference_type;
1015 typedef typename M::value_type value_type;
1016 typedef typename M::const_reference const_reference;
1017 typedef typename boost::mpl::if_<boost::is_const<M>,
1018 typename M::const_reference,
1019 typename M::reference>::type reference;
1020 typedef typename boost::mpl::if_<boost::is_const<M>,
1021 typename M::const_closure_type,
1022 typename M::closure_type>::type matrix_closure_type;
1023 typedef const self_type const_closure_type;
1024 typedef self_type closure_type;
1025 // Replaced by _temporary_traits to avoid type requirements on M
1026 //typedef typename M::vector_temporary_type vector_temporary_type;
1027 //typedef typename M::matrix_temporary_type matrix_temporary_type;
1028 typedef typename storage_restrict_traits<typename M::storage_category,
1029 packed_proxy_tag>::storage_category storage_category;
1030 typedef typename M::orientation_category orientation_category;
1032 // Construction and destruction
1034 symmetric_adaptor (matrix_type &data):
1035 matrix_expression<self_type> (),
1037 BOOST_UBLAS_CHECK (data_.size1 () == data_.size2 (), bad_size ());
1040 symmetric_adaptor (const symmetric_adaptor &m):
1041 matrix_expression<self_type> (),
1043 BOOST_UBLAS_CHECK (data_.size1 () == data_.size2 (), bad_size ());
1048 size_type size1 () const {
1049 return data_.size1 ();
1052 size_type size2 () const {
1053 return data_.size2 ();
1056 // Storage accessors
1058 const matrix_closure_type &data () const {
1062 matrix_closure_type &data () {
1067 #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
1069 const_reference operator () (size_type i, size_type j) const {
1070 BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
1071 BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
1072 if (triangular_type::other (i, j))
1073 return data () (i, j);
1075 return data () (j, i);
1078 reference operator () (size_type i, size_type j) {
1079 BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
1080 BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
1081 if (triangular_type::other (i, j))
1082 return data () (i, j);
1084 return data () (j, i);
1088 reference operator () (size_type i, size_type j) const {
1089 BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
1090 BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
1091 if (triangular_type::other (i, j))
1092 return data () (i, j);
1094 return data () (j, i);
1100 symmetric_adaptor &operator = (const symmetric_adaptor &m) {
1101 matrix_assign<scalar_assign, triangular_type> (*this, m);
1105 symmetric_adaptor &assign_temporary (symmetric_adaptor &m) {
1111 symmetric_adaptor &operator = (const matrix_expression<AE> &ae) {
1112 matrix_assign<scalar_assign, triangular_type> (*this, matrix<value_type> (ae));
1117 symmetric_adaptor &assign (const matrix_expression<AE> &ae) {
1118 matrix_assign<scalar_assign, triangular_type> (*this, ae);
1123 symmetric_adaptor& operator += (const matrix_expression<AE> &ae) {
1124 matrix_assign<scalar_assign, triangular_type> (*this, matrix<value_type> (*this + ae));
1129 symmetric_adaptor &plus_assign (const matrix_expression<AE> &ae) {
1130 matrix_assign<scalar_plus_assign, triangular_type> (*this, ae);
1135 symmetric_adaptor& operator -= (const matrix_expression<AE> &ae) {
1136 matrix_assign<scalar_assign, triangular_type> (*this, matrix<value_type> (*this - ae));
1141 symmetric_adaptor &minus_assign (const matrix_expression<AE> &ae) {
1142 matrix_assign<scalar_minus_assign, triangular_type> (*this, ae);
1147 symmetric_adaptor& operator *= (const AT &at) {
1148 matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
1153 symmetric_adaptor& operator /= (const AT &at) {
1154 matrix_assign_scalar<scalar_divides_assign> (*this, at);
1158 // Closure comparison
1160 bool same_closure (const symmetric_adaptor &sa) const {
1161 return (*this).data ().same_closure (sa.data ());
1166 void swap (symmetric_adaptor &m) {
1168 matrix_swap<scalar_swap, triangular_type> (*this, m);
1171 friend void swap (symmetric_adaptor &m1, symmetric_adaptor &m2) {
1177 // Use matrix iterator
1178 typedef typename M::const_iterator1 const_subiterator1_type;
1179 typedef typename boost::mpl::if_<boost::is_const<M>,
1180 typename M::const_iterator1,
1181 typename M::iterator1>::type subiterator1_type;
1182 typedef typename M::const_iterator2 const_subiterator2_type;
1183 typedef typename boost::mpl::if_<boost::is_const<M>,
1184 typename M::const_iterator2,
1185 typename M::iterator2>::type subiterator2_type;
1188 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1189 typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1;
1190 typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2;
1191 typedef indexed_const_iterator1<self_type, dense_random_access_iterator_tag> const_iterator1;
1192 typedef indexed_const_iterator2<self_type, dense_random_access_iterator_tag> const_iterator2;
1194 class const_iterator1;
1196 class const_iterator2;
1199 typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
1200 typedef reverse_iterator_base1<iterator1> reverse_iterator1;
1201 typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
1202 typedef reverse_iterator_base2<iterator2> reverse_iterator2;
1206 const_iterator1 find1 (int rank, size_type i, size_type j) const {
1207 if (triangular_type::other (i, j)) {
1208 if (triangular_type::other (size1 (), j)) {
1209 return const_iterator1 (*this, 0, 0,
1210 data ().find1 (rank, i, j), data ().find1 (rank, size1 (), j),
1211 data ().find2 (rank, size2 (), size1 ()), data ().find2 (rank, size2 (), size1 ()));
1213 return const_iterator1 (*this, 0, 1,
1214 data ().find1 (rank, i, j), data ().find1 (rank, j, j),
1215 data ().find2 (rank, j, j), data ().find2 (rank, j, size1 ()));
1218 if (triangular_type::other (size1 (), j)) {
1219 return const_iterator1 (*this, 1, 0,
1220 data ().find1 (rank, j, j), data ().find1 (rank, size1 (), j),
1221 data ().find2 (rank, j, i), data ().find2 (rank, j, j));
1223 return const_iterator1 (*this, 1, 1,
1224 data ().find1 (rank, size1 (), size2 ()), data ().find1 (rank, size1 (), size2 ()),
1225 data ().find2 (rank, j, i), data ().find2 (rank, j, size1 ()));
1230 iterator1 find1 (int rank, size_type i, size_type j) {
1232 i = triangular_type::mutable_restrict1 (i, j, size1(), size2());
1233 return iterator1 (*this, data ().find1 (rank, i, j));
1236 const_iterator2 find2 (int rank, size_type i, size_type j) const {
1237 if (triangular_type::other (i, j)) {
1238 if (triangular_type::other (i, size2 ())) {
1239 return const_iterator2 (*this, 1, 1,
1240 data ().find1 (rank, size2 (), size1 ()), data ().find1 (rank, size2 (), size1 ()),
1241 data ().find2 (rank, i, j), data ().find2 (rank, i, size2 ()));
1243 return const_iterator2 (*this, 1, 0,
1244 data ().find1 (rank, i, i), data ().find1 (rank, size2 (), i),
1245 data ().find2 (rank, i, j), data ().find2 (rank, i, i));
1248 if (triangular_type::other (i, size2 ())) {
1249 return const_iterator2 (*this, 0, 1,
1250 data ().find1 (rank, j, i), data ().find1 (rank, i, i),
1251 data ().find2 (rank, i, i), data ().find2 (rank, i, size2 ()));
1253 return const_iterator2 (*this, 0, 0,
1254 data ().find1 (rank, j, i), data ().find1 (rank, size2 (), i),
1255 data ().find2 (rank, size1 (), size2 ()), data ().find2 (rank, size2 (), size2 ()));
1260 iterator2 find2 (int rank, size_type i, size_type j) {
1262 j = triangular_type::mutable_restrict2 (i, j, size1(), size2());
1263 return iterator2 (*this, data ().find2 (rank, i, j));
1266 // Iterators simply are indices.
1268 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1269 class const_iterator1:
1270 public container_const_reference<symmetric_adaptor>,
1271 public random_access_iterator_base<typename iterator_restrict_traits<
1272 typename const_subiterator1_type::iterator_category, dense_random_access_iterator_tag>::iterator_category,
1273 const_iterator1, value_type> {
1275 typedef typename const_subiterator1_type::value_type value_type;
1276 typedef typename const_subiterator1_type::difference_type difference_type;
1277 typedef typename const_subiterator1_type::reference reference;
1278 typedef typename const_subiterator1_type::pointer pointer;
1280 typedef const_iterator2 dual_iterator_type;
1281 typedef const_reverse_iterator2 dual_reverse_iterator_type;
1283 // Construction and destruction
1286 container_const_reference<self_type> (),
1287 begin_ (-1), end_ (-1), current_ (-1),
1288 it1_begin_ (), it1_end_ (), it1_ (),
1289 it2_begin_ (), it2_end_ (), it2_ () {}
1291 const_iterator1 (const self_type &m, int begin, int end,
1292 const const_subiterator1_type &it1_begin, const const_subiterator1_type &it1_end,
1293 const const_subiterator2_type &it2_begin, const const_subiterator2_type &it2_end):
1294 container_const_reference<self_type> (m),
1295 begin_ (begin), end_ (end), current_ (begin),
1296 it1_begin_ (it1_begin), it1_end_ (it1_end), it1_ (it1_begin_),
1297 it2_begin_ (it2_begin), it2_end_ (it2_end), it2_ (it2_begin_) {
1298 if (current_ == 0 && it1_ == it1_end_)
1300 if (current_ == 1 && it2_ == it2_end_)
1302 if ((current_ == 0 && it1_ == it1_end_) ||
1303 (current_ == 1 && it2_ == it2_end_))
1305 BOOST_UBLAS_CHECK (current_ == end_ ||
1306 (current_ == 0 && it1_ != it1_end_) ||
1307 (current_ == 1 && it2_ != it2_end_), internal_logic ());
1309 // FIXME cannot compile
1310 // iterator1 does not have these members!
1312 const_iterator1 (const iterator1 &it):
1313 container_const_reference<self_type> (it ()),
1314 begin_ (it.begin_), end_ (it.end_), current_ (it.current_),
1315 it1_begin_ (it.it1_begin_), it1_end_ (it.it1_end_), it1_ (it.it1_),
1316 it2_begin_ (it.it2_begin_), it2_end_ (it.it2_end_), it2_ (it.it2_) {
1317 BOOST_UBLAS_CHECK (current_ == end_ ||
1318 (current_ == 0 && it1_ != it1_end_) ||
1319 (current_ == 1 && it2_ != it2_end_), internal_logic ());
1324 const_iterator1 &operator ++ () {
1325 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1326 if (current_ == 0) {
1327 BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
1329 if (it1_ == it1_end_ && end_ == 1) {
1333 } else /* if (current_ == 1) */ {
1334 BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
1336 if (it2_ == it2_end_ && end_ == 0) {
1344 const_iterator1 &operator -- () {
1345 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1346 if (current_ == 0) {
1347 if (it1_ == it1_begin_ && begin_ == 1) {
1349 BOOST_UBLAS_CHECK (it2_ != it2_begin_, internal_logic ());
1355 } else /* if (current_ == 1) */ {
1356 if (it2_ == it2_begin_ && begin_ == 0) {
1358 BOOST_UBLAS_CHECK (it1_ != it1_begin_, internal_logic ());
1368 const_iterator1 &operator += (difference_type n) {
1369 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1370 if (current_ == 0) {
1371 size_type d = (std::min) (n, it1_end_ - it1_);
1374 if (n > 0 || (end_ == 1 && it1_ == it1_end_)) {
1375 BOOST_UBLAS_CHECK (end_ == 1, external_logic ());
1376 d = (std::min) (n, it2_end_ - it2_begin_);
1377 it2_ = it2_begin_ + d;
1381 } else /* if (current_ == 1) */ {
1382 size_type d = (std::min) (n, it2_end_ - it2_);
1385 if (n > 0 || (end_ == 0 && it2_ == it2_end_)) {
1386 BOOST_UBLAS_CHECK (end_ == 0, external_logic ());
1387 d = (std::min) (n, it1_end_ - it1_begin_);
1388 it1_ = it1_begin_ + d;
1393 BOOST_UBLAS_CHECK (n == 0, external_logic ());
1397 const_iterator1 &operator -= (difference_type n) {
1398 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1399 if (current_ == 0) {
1400 size_type d = (std::min) (n, it1_ - it1_begin_);
1404 BOOST_UBLAS_CHECK (end_ == 1, external_logic ());
1405 d = (std::min) (n, it2_end_ - it2_begin_);
1406 it2_ = it2_end_ - d;
1410 } else /* if (current_ == 1) */ {
1411 size_type d = (std::min) (n, it2_ - it2_begin_);
1415 BOOST_UBLAS_CHECK (end_ == 0, external_logic ());
1416 d = (std::min) (n, it1_end_ - it1_begin_);
1417 it1_ = it1_end_ - d;
1422 BOOST_UBLAS_CHECK (n == 0, external_logic ());
1426 difference_type operator - (const const_iterator1 &it) const {
1427 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1428 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1429 BOOST_UBLAS_CHECK (it.current_ == 0 || it.current_ == 1, internal_logic ());
1430 BOOST_UBLAS_CHECK (/* begin_ == it.begin_ && */ end_ == it.end_, internal_logic ());
1431 if (current_ == 0 && it.current_ == 0) {
1432 return it1_ - it.it1_;
1433 } else if (current_ == 0 && it.current_ == 1) {
1434 if (end_ == 1 && it.end_ == 1) {
1435 return (it1_ - it.it1_end_) + (it.it2_begin_ - it.it2_);
1436 } else /* if (end_ == 0 && it.end_ == 0) */ {
1437 return (it1_ - it.it1_begin_) + (it.it2_end_ - it.it2_);
1440 } else if (current_ == 1 && it.current_ == 0) {
1441 if (end_ == 1 && it.end_ == 1) {
1442 return (it2_ - it.it2_begin_) + (it.it1_end_ - it.it1_);
1443 } else /* if (end_ == 0 && it.end_ == 0) */ {
1444 return (it2_ - it.it2_end_) + (it.it1_begin_ - it.it1_);
1447 /* current_ == 1 && it.current_ == 1 */ {
1448 return it2_ - it.it2_;
1454 const_reference operator * () const {
1455 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1456 if (current_ == 0) {
1457 BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
1459 } else /* if (current_ == 1) */ {
1460 BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
1465 const_reference operator [] (difference_type n) const {
1466 return *(*this + n);
1469 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
1471 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1472 typename self_type::
1474 const_iterator2 begin () const {
1475 return (*this) ().find2 (1, index1 (), 0);
1478 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1479 typename self_type::
1481 const_iterator2 cbegin () const {
1485 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1486 typename self_type::
1488 const_iterator2 end () const {
1489 return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
1492 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1493 typename self_type::
1495 const_iterator2 cend () const {
1499 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1500 typename self_type::
1502 const_reverse_iterator2 rbegin () const {
1503 return const_reverse_iterator2 (end ());
1506 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1507 typename self_type::
1509 const_reverse_iterator2 crbegin () const {
1513 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1514 typename self_type::
1516 const_reverse_iterator2 rend () const {
1517 return const_reverse_iterator2 (begin ());
1520 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1521 typename self_type::
1523 const_reverse_iterator2 crend () const {
1530 size_type index1 () const {
1531 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1532 if (current_ == 0) {
1533 BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
1534 return it1_.index1 ();
1535 } else /* if (current_ == 1) */ {
1536 BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
1537 return it2_.index2 ();
1541 size_type index2 () const {
1542 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1543 if (current_ == 0) {
1544 BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
1545 return it1_.index2 ();
1546 } else /* if (current_ == 1) */ {
1547 BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
1548 return it2_.index1 ();
1554 const_iterator1 &operator = (const const_iterator1 &it) {
1555 container_const_reference<self_type>::assign (&it ());
1558 current_ = it.current_;
1559 it1_begin_ = it.it1_begin_;
1560 it1_end_ = it.it1_end_;
1562 it2_begin_ = it.it2_begin_;
1563 it2_end_ = it.it2_end_;
1570 bool operator == (const const_iterator1 &it) const {
1571 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1572 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1573 BOOST_UBLAS_CHECK (it.current_ == 0 || it.current_ == 1, internal_logic ());
1574 BOOST_UBLAS_CHECK (/* begin_ == it.begin_ && */ end_ == it.end_, internal_logic ());
1575 return (current_ == 0 && it.current_ == 0 && it1_ == it.it1_) ||
1576 (current_ == 1 && it.current_ == 1 && it2_ == it.it2_);
1579 bool operator < (const const_iterator1 &it) const {
1580 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1581 return it - *this > 0;
1588 const_subiterator1_type it1_begin_;
1589 const_subiterator1_type it1_end_;
1590 const_subiterator1_type it1_;
1591 const_subiterator2_type it2_begin_;
1592 const_subiterator2_type it2_end_;
1593 const_subiterator2_type it2_;
1598 const_iterator1 begin1 () const {
1599 return find1 (0, 0, 0);
1602 const_iterator1 cbegin1 () const {
1606 const_iterator1 end1 () const {
1607 return find1 (0, size1 (), 0);
1610 const_iterator1 cend1 () const {
1614 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1616 public container_reference<symmetric_adaptor>,
1617 public random_access_iterator_base<typename iterator_restrict_traits<
1618 typename subiterator1_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
1619 iterator1, value_type> {
1621 typedef typename subiterator1_type::value_type value_type;
1622 typedef typename subiterator1_type::difference_type difference_type;
1623 typedef typename subiterator1_type::reference reference;
1624 typedef typename subiterator1_type::pointer pointer;
1626 typedef iterator2 dual_iterator_type;
1627 typedef reverse_iterator2 dual_reverse_iterator_type;
1629 // Construction and destruction
1632 container_reference<self_type> (), it1_ () {}
1634 iterator1 (self_type &m, const subiterator1_type &it1):
1635 container_reference<self_type> (m), it1_ (it1) {}
1639 iterator1 &operator ++ () {
1644 iterator1 &operator -- () {
1649 iterator1 &operator += (difference_type n) {
1654 iterator1 &operator -= (difference_type n) {
1659 difference_type operator - (const iterator1 &it) const {
1660 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1661 return it1_ - it.it1_;
1666 reference operator * () const {
1670 reference operator [] (difference_type n) const {
1671 return *(*this + n);
1674 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
1676 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1677 typename self_type::
1679 iterator2 begin () const {
1680 return (*this) ().find2 (1, index1 (), 0);
1683 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1684 typename self_type::
1686 iterator2 end () const {
1687 return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
1690 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1691 typename self_type::
1693 reverse_iterator2 rbegin () const {
1694 return reverse_iterator2 (end ());
1697 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1698 typename self_type::
1700 reverse_iterator2 rend () const {
1701 return reverse_iterator2 (begin ());
1707 size_type index1 () const {
1708 return it1_.index1 ();
1711 size_type index2 () const {
1712 return it1_.index2 ();
1717 iterator1 &operator = (const iterator1 &it) {
1718 container_reference<self_type>::assign (&it ());
1725 bool operator == (const iterator1 &it) const {
1726 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1727 return it1_ == it.it1_;
1730 bool operator < (const iterator1 &it) const {
1731 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1732 return it1_ < it.it1_;
1736 subiterator1_type it1_;
1738 friend class const_iterator1;
1743 iterator1 begin1 () {
1744 return find1 (0, 0, 0);
1748 return find1 (0, size1 (), 0);
1751 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1752 class const_iterator2:
1753 public container_const_reference<symmetric_adaptor>,
1754 public random_access_iterator_base<typename iterator_restrict_traits<
1755 typename const_subiterator2_type::iterator_category, dense_random_access_iterator_tag>::iterator_category,
1756 const_iterator2, value_type> {
1758 typedef typename const_subiterator2_type::value_type value_type;
1759 typedef typename const_subiterator2_type::difference_type difference_type;
1760 typedef typename const_subiterator2_type::reference reference;
1761 typedef typename const_subiterator2_type::pointer pointer;
1763 typedef const_iterator1 dual_iterator_type;
1764 typedef const_reverse_iterator1 dual_reverse_iterator_type;
1766 // Construction and destruction
1769 container_const_reference<self_type> (),
1770 begin_ (-1), end_ (-1), current_ (-1),
1771 it1_begin_ (), it1_end_ (), it1_ (),
1772 it2_begin_ (), it2_end_ (), it2_ () {}
1774 const_iterator2 (const self_type &m, int begin, int end,
1775 const const_subiterator1_type &it1_begin, const const_subiterator1_type &it1_end,
1776 const const_subiterator2_type &it2_begin, const const_subiterator2_type &it2_end):
1777 container_const_reference<self_type> (m),
1778 begin_ (begin), end_ (end), current_ (begin),
1779 it1_begin_ (it1_begin), it1_end_ (it1_end), it1_ (it1_begin_),
1780 it2_begin_ (it2_begin), it2_end_ (it2_end), it2_ (it2_begin_) {
1781 if (current_ == 0 && it1_ == it1_end_)
1783 if (current_ == 1 && it2_ == it2_end_)
1785 if ((current_ == 0 && it1_ == it1_end_) ||
1786 (current_ == 1 && it2_ == it2_end_))
1788 BOOST_UBLAS_CHECK (current_ == end_ ||
1789 (current_ == 0 && it1_ != it1_end_) ||
1790 (current_ == 1 && it2_ != it2_end_), internal_logic ());
1792 // FIXME cannot compiler
1793 // iterator2 does not have these members!
1795 const_iterator2 (const iterator2 &it):
1796 container_const_reference<self_type> (it ()),
1797 begin_ (it.begin_), end_ (it.end_), current_ (it.current_),
1798 it1_begin_ (it.it1_begin_), it1_end_ (it.it1_end_), it1_ (it.it1_),
1799 it2_begin_ (it.it2_begin_), it2_end_ (it.it2_end_), it2_ (it.it2_) {
1800 BOOST_UBLAS_CHECK (current_ == end_ ||
1801 (current_ == 0 && it1_ != it1_end_) ||
1802 (current_ == 1 && it2_ != it2_end_), internal_logic ());
1807 const_iterator2 &operator ++ () {
1808 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1809 if (current_ == 0) {
1810 BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
1812 if (it1_ == it1_end_ && end_ == 1) {
1816 } else /* if (current_ == 1) */ {
1817 BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
1819 if (it2_ == it2_end_ && end_ == 0) {
1827 const_iterator2 &operator -- () {
1828 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1829 if (current_ == 0) {
1830 if (it1_ == it1_begin_ && begin_ == 1) {
1832 BOOST_UBLAS_CHECK (it2_ != it2_begin_, internal_logic ());
1838 } else /* if (current_ == 1) */ {
1839 if (it2_ == it2_begin_ && begin_ == 0) {
1841 BOOST_UBLAS_CHECK (it1_ != it1_begin_, internal_logic ());
1851 const_iterator2 &operator += (difference_type n) {
1852 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1853 if (current_ == 0) {
1854 size_type d = (std::min) (n, it1_end_ - it1_);
1857 if (n > 0 || (end_ == 1 && it1_ == it1_end_)) {
1858 BOOST_UBLAS_CHECK (end_ == 1, external_logic ());
1859 d = (std::min) (n, it2_end_ - it2_begin_);
1860 it2_ = it2_begin_ + d;
1864 } else /* if (current_ == 1) */ {
1865 size_type d = (std::min) (n, it2_end_ - it2_);
1868 if (n > 0 || (end_ == 0 && it2_ == it2_end_)) {
1869 BOOST_UBLAS_CHECK (end_ == 0, external_logic ());
1870 d = (std::min) (n, it1_end_ - it1_begin_);
1871 it1_ = it1_begin_ + d;
1876 BOOST_UBLAS_CHECK (n == 0, external_logic ());
1880 const_iterator2 &operator -= (difference_type n) {
1881 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1882 if (current_ == 0) {
1883 size_type d = (std::min) (n, it1_ - it1_begin_);
1887 BOOST_UBLAS_CHECK (end_ == 1, external_logic ());
1888 d = (std::min) (n, it2_end_ - it2_begin_);
1889 it2_ = it2_end_ - d;
1893 } else /* if (current_ == 1) */ {
1894 size_type d = (std::min) (n, it2_ - it2_begin_);
1898 BOOST_UBLAS_CHECK (end_ == 0, external_logic ());
1899 d = (std::min) (n, it1_end_ - it1_begin_);
1900 it1_ = it1_end_ - d;
1905 BOOST_UBLAS_CHECK (n == 0, external_logic ());
1909 difference_type operator - (const const_iterator2 &it) const {
1910 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1911 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1912 BOOST_UBLAS_CHECK (it.current_ == 0 || it.current_ == 1, internal_logic ());
1913 BOOST_UBLAS_CHECK (/* begin_ == it.begin_ && */ end_ == it.end_, internal_logic ());
1914 if (current_ == 0 && it.current_ == 0) {
1915 return it1_ - it.it1_;
1916 } else if (current_ == 0 && it.current_ == 1) {
1917 if (end_ == 1 && it.end_ == 1) {
1918 return (it1_ - it.it1_end_) + (it.it2_begin_ - it.it2_);
1919 } else /* if (end_ == 0 && it.end_ == 0) */ {
1920 return (it1_ - it.it1_begin_) + (it.it2_end_ - it.it2_);
1923 } else if (current_ == 1 && it.current_ == 0) {
1924 if (end_ == 1 && it.end_ == 1) {
1925 return (it2_ - it.it2_begin_) + (it.it1_end_ - it.it1_);
1926 } else /* if (end_ == 0 && it.end_ == 0) */ {
1927 return (it2_ - it.it2_end_) + (it.it1_begin_ - it.it1_);
1930 /* current_ == 1 && it.current_ == 1 */ {
1931 return it2_ - it.it2_;
1937 const_reference operator * () const {
1938 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
1939 if (current_ == 0) {
1940 BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
1942 } else /* if (current_ == 1) */ {
1943 BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
1948 const_reference operator [] (difference_type n) const {
1949 return *(*this + n);
1952 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
1954 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1955 typename self_type::
1957 const_iterator1 begin () const {
1958 return (*this) ().find1 (1, 0, index2 ());
1961 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1962 typename self_type::
1964 const_iterator1 cbegin () const {
1968 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1969 typename self_type::
1971 const_iterator1 end () const {
1972 return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
1975 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1976 typename self_type::
1978 const_iterator1 cend () const {
1982 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1983 typename self_type::
1985 const_reverse_iterator1 rbegin () const {
1986 return const_reverse_iterator1 (end ());
1989 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1990 typename self_type::
1992 const_reverse_iterator1 crbegin () const {
1996 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1997 typename self_type::
1999 const_reverse_iterator1 rend () const {
2000 return const_reverse_iterator1 (begin ());
2003 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
2004 typename self_type::
2006 const_reverse_iterator1 crend () const {
2013 size_type index1 () const {
2014 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
2015 if (current_ == 0) {
2016 BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
2017 return it1_.index2 ();
2018 } else /* if (current_ == 1) */ {
2019 BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
2020 return it2_.index1 ();
2024 size_type index2 () const {
2025 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
2026 if (current_ == 0) {
2027 BOOST_UBLAS_CHECK (it1_ != it1_end_, internal_logic ());
2028 return it1_.index1 ();
2029 } else /* if (current_ == 1) */ {
2030 BOOST_UBLAS_CHECK (it2_ != it2_end_, internal_logic ());
2031 return it2_.index2 ();
2037 const_iterator2 &operator = (const const_iterator2 &it) {
2038 container_const_reference<self_type>::assign (&it ());
2041 current_ = it.current_;
2042 it1_begin_ = it.it1_begin_;
2043 it1_end_ = it.it1_end_;
2045 it2_begin_ = it.it2_begin_;
2046 it2_end_ = it.it2_end_;
2053 bool operator == (const const_iterator2 &it) const {
2054 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
2055 BOOST_UBLAS_CHECK (current_ == 0 || current_ == 1, internal_logic ());
2056 BOOST_UBLAS_CHECK (it.current_ == 0 || it.current_ == 1, internal_logic ());
2057 BOOST_UBLAS_CHECK (/* begin_ == it.begin_ && */ end_ == it.end_, internal_logic ());
2058 return (current_ == 0 && it.current_ == 0 && it1_ == it.it1_) ||
2059 (current_ == 1 && it.current_ == 1 && it2_ == it.it2_);
2062 bool operator < (const const_iterator2 &it) const {
2063 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
2064 return it - *this > 0;
2071 const_subiterator1_type it1_begin_;
2072 const_subiterator1_type it1_end_;
2073 const_subiterator1_type it1_;
2074 const_subiterator2_type it2_begin_;
2075 const_subiterator2_type it2_end_;
2076 const_subiterator2_type it2_;
2081 const_iterator2 begin2 () const {
2082 return find2 (0, 0, 0);
2085 const_iterator2 cbegin2 () const {
2089 const_iterator2 end2 () const {
2090 return find2 (0, 0, size2 ());
2093 const_iterator2 cend2 () const {
2097 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
2099 public container_reference<symmetric_adaptor>,
2100 public random_access_iterator_base<typename iterator_restrict_traits<
2101 typename subiterator2_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
2102 iterator2, value_type> {
2104 typedef typename subiterator2_type::value_type value_type;
2105 typedef typename subiterator2_type::difference_type difference_type;
2106 typedef typename subiterator2_type::reference reference;
2107 typedef typename subiterator2_type::pointer pointer;
2109 typedef iterator1 dual_iterator_type;
2110 typedef reverse_iterator1 dual_reverse_iterator_type;
2112 // Construction and destruction
2115 container_reference<self_type> (), it2_ () {}
2117 iterator2 (self_type &m, const subiterator2_type &it2):
2118 container_reference<self_type> (m), it2_ (it2) {}
2122 iterator2 &operator ++ () {
2127 iterator2 &operator -- () {
2132 iterator2 &operator += (difference_type n) {
2137 iterator2 &operator -= (difference_type n) {
2142 difference_type operator - (const iterator2 &it) const {
2143 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
2144 return it2_ - it.it2_;
2149 reference operator * () const {
2153 reference operator [] (difference_type n) const {
2154 return *(*this + n);
2157 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
2159 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
2160 typename self_type::
2162 iterator1 begin () const {
2163 return (*this) ().find1 (1, 0, index2 ());
2166 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
2167 typename self_type::
2169 iterator1 end () const {
2170 return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
2173 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
2174 typename self_type::
2176 reverse_iterator1 rbegin () const {
2177 return reverse_iterator1 (end ());
2180 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
2181 typename self_type::
2183 reverse_iterator1 rend () const {
2184 return reverse_iterator1 (begin ());
2190 size_type index1 () const {
2191 return it2_.index1 ();
2194 size_type index2 () const {
2195 return it2_.index2 ();
2200 iterator2 &operator = (const iterator2 &it) {
2201 container_reference<self_type>::assign (&it ());
2208 bool operator == (const iterator2 &it) const {
2209 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
2210 return it2_ == it.it2_;
2213 bool operator < (const iterator2 &it) const {
2214 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
2215 return it2_ < it.it2_;
2219 subiterator2_type it2_;
2221 friend class const_iterator2;
2226 iterator2 begin2 () {
2227 return find2 (0, 0, 0);
2231 return find2 (0, 0, size2 ());
2234 // Reverse iterators
2237 const_reverse_iterator1 rbegin1 () const {
2238 return const_reverse_iterator1 (end1 ());
2241 const_reverse_iterator1 crbegin1 () const {
2245 const_reverse_iterator1 rend1 () const {
2246 return const_reverse_iterator1 (begin1 ());
2249 const_reverse_iterator1 crend1 () const {
2254 reverse_iterator1 rbegin1 () {
2255 return reverse_iterator1 (end1 ());
2258 reverse_iterator1 rend1 () {
2259 return reverse_iterator1 (begin1 ());
2263 const_reverse_iterator2 rbegin2 () const {
2264 return const_reverse_iterator2 (end2 ());
2267 const_reverse_iterator2 crbegin2 () const {
2271 const_reverse_iterator2 rend2 () const {
2272 return const_reverse_iterator2 (begin2 ());
2275 const_reverse_iterator2 crend2 () const {
2280 reverse_iterator2 rbegin2 () {
2281 return reverse_iterator2 (end2 ());
2284 reverse_iterator2 rend2 () {
2285 return reverse_iterator2 (begin2 ());
2289 matrix_closure_type data_;
2292 // Specialization for temporary_traits
2293 template <class M, class TRI>
2294 struct vector_temporary_traits< symmetric_adaptor<M, TRI> >
2295 : vector_temporary_traits< M > {} ;
2296 template <class M, class TRI>
2297 struct vector_temporary_traits< const symmetric_adaptor<M, TRI> >
2298 : vector_temporary_traits< M > {} ;
2300 template <class M, class TRI>
2301 struct matrix_temporary_traits< symmetric_adaptor<M, TRI> >
2302 : matrix_temporary_traits< M > {} ;
2303 template <class M, class TRI>
2304 struct matrix_temporary_traits< const symmetric_adaptor<M, TRI> >
2305 : matrix_temporary_traits< M > {} ;