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_TRIANGULAR_
14 #define _BOOST_UBLAS_TRIANGULAR_
16 #include <boost/numeric/ublas/matrix.hpp>
17 #include <boost/numeric/ublas/detail/temporary.hpp>
18 #include <boost/type_traits/remove_const.hpp>
20 // Iterators based on ideas of Jeremy Siek
22 namespace boost { namespace numeric { namespace ublas {
25 using namespace boost::numeric::ublas;
27 // Matrix resizing algorithm
28 template <class L, class T, class M>
30 void matrix_resize_preserve (M& m, M& temporary) {
31 typedef L layout_type;
32 typedef T triangular_type;
33 typedef typename M::size_type size_type;
34 const size_type msize1 (m.size1 ()); // original size
35 const size_type msize2 (m.size2 ());
36 const size_type size1 (temporary.size1 ()); // new size is specified by temporary
37 const size_type size2 (temporary.size2 ());
38 // Common elements to preserve
39 const size_type size1_min = (std::min) (size1, msize1);
40 const size_type size2_min = (std::min) (size2, msize2);
41 // Order for major and minor sizes
42 const size_type major_size = layout_type::size_M (size1_min, size2_min);
43 const size_type minor_size = layout_type::size_m (size1_min, size2_min);
44 // Indexing copy over major
45 for (size_type major = 0; major != major_size; ++major) {
46 for (size_type minor = 0; minor != minor_size; ++minor) {
47 // find indexes - use invertability of element_ functions
48 const size_type i1 = layout_type::index_M(major, minor);
49 const size_type i2 = layout_type::index_m(major, minor);
50 if ( triangular_type::other(i1,i2) ) {
51 temporary.data () [triangular_type::element (layout_type (), i1, size1, i2, size2)] =
52 m.data() [triangular_type::element (layout_type (), i1, msize1, i2, msize2)];
56 m.assign_temporary (temporary);
60 /** \brief A triangular matrix of values of type \c T.
62 * For a \f$(n \times n )\f$-dimensional lower triangular matrix and if \f$0 \leq i < n\f$, \f$0 \leq j < n\f$ and \f$i>j\f$ holds,
63 * \f$m_{i,j}=0\f$. Furthermore if \f$m_{i,i}=1\f$, the matrix is called unit lower triangular.
65 * For a \f$(n \times n )\f$-dimensional upper triangular matrix and if \f$0 \leq i < n\f$, \f$0 \leq j < n\f$ and \f$i<j\f$ holds,
66 * \f$m_{i,j}=0\f$. Furthermore if \f$m_{i,i}=1\f$, the matrix is called unit upper triangular.
68 * The default storage for triangular matrices is packed. Orientation and storage can also be specified.
69 * Default is \c row_major and and unbounded_array. It is \b not required by the storage to initialize
70 * elements of the matrix.
72 * \tparam T the type of object stored in the matrix (like double, float, complex, etc...)
73 * \tparam TRI the type of the triangular matrix. It can either be \c lower or \c upper. Default is \c lower
74 * \tparam L the storage organization. It can be either \c row_major or \c column_major. Default is \c row_major
75 * \tparam A the type of Storage array. Default is \c unbounded_array
77 template<class T, class TRI, class L, class A>
78 class triangular_matrix:
79 public matrix_container<triangular_matrix<T, TRI, L, A> > {
82 typedef TRI triangular_type;
83 typedef L layout_type;
84 typedef triangular_matrix<T, TRI, L, A> self_type;
86 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
87 using matrix_container<self_type>::operator ();
89 typedef typename A::size_type size_type;
90 typedef typename A::difference_type difference_type;
92 typedef const T &const_reference;
96 typedef const matrix_reference<const self_type> const_closure_type;
97 typedef matrix_reference<self_type> closure_type;
98 typedef vector<T, A> vector_temporary_type;
99 typedef matrix<T, L, A> matrix_temporary_type; // general sub-matrix
100 typedef packed_tag storage_category;
101 typedef typename L::orientation_category orientation_category;
103 // Construction and destruction
105 triangular_matrix ():
106 matrix_container<self_type> (),
107 size1_ (0), size2_ (0), data_ (0) {}
109 triangular_matrix (size_type size1, size_type size2):
110 matrix_container<self_type> (),
111 size1_ (size1), size2_ (size2), data_ (triangular_type::packed_size (layout_type (), size1, size2)) {
114 triangular_matrix (size_type size1, size_type size2, const array_type &data):
115 matrix_container<self_type> (),
116 size1_ (size1), size2_ (size2), data_ (data) {}
118 triangular_matrix (const triangular_matrix &m):
119 matrix_container<self_type> (),
120 size1_ (m.size1_), size2_ (m.size2_), data_ (m.data_) {}
123 triangular_matrix (const matrix_expression<AE> &ae):
124 matrix_container<self_type> (),
125 size1_ (ae ().size1 ()), size2_ (ae ().size2 ()),
126 data_ (triangular_type::packed_size (layout_type (), size1_, size2_)) {
127 matrix_assign<scalar_assign> (*this, ae);
132 size_type size1 () const {
136 size_type size2 () const {
142 const array_type &data () const {
146 array_type &data () {
152 void resize (size_type size1, size_type size2, bool preserve = true) {
154 self_type temporary (size1, size2);
155 detail::matrix_resize_preserve<layout_type, triangular_type> (*this, temporary);
158 data ().resize (triangular_type::packed_size (layout_type (), size1, size2));
164 void resize_packed_preserve (size_type size1, size_type size2) {
167 data ().resize (triangular_type::packed_size (layout_type (), size1_, size2_), value_type ());
172 const_reference operator () (size_type i, size_type j) const {
173 BOOST_UBLAS_CHECK (i < size1_, bad_index ());
174 BOOST_UBLAS_CHECK (j < size2_, bad_index ());
175 if (triangular_type::other (i, j))
176 return data () [triangular_type::element (layout_type (), i, size1_, j, size2_)];
177 else if (triangular_type::one (i, j))
183 reference at_element (size_type i, size_type j) {
184 BOOST_UBLAS_CHECK (i < size1_, bad_index ());
185 BOOST_UBLAS_CHECK (j < size2_, bad_index ());
186 return data () [triangular_type::element (layout_type (), i, size1_, j, size2_)];
189 reference operator () (size_type i, size_type j) {
190 BOOST_UBLAS_CHECK (i < size1_, bad_index ());
191 BOOST_UBLAS_CHECK (j < size2_, bad_index ());
192 if (!triangular_type::other (i, j)) {
193 bad_index ().raise ();
196 return data () [triangular_type::element (layout_type (), i, size1_, j, size2_)];
199 // Element assignment
201 reference insert_element (size_type i, size_type j, const_reference t) {
202 return (operator () (i, j) = t);
205 void erase_element (size_type i, size_type j) {
206 operator () (i, j) = value_type/*zero*/();
213 std::fill (data ().begin (), data ().end (), value_type/*zero*/());
218 triangular_matrix &operator = (const triangular_matrix &m) {
225 triangular_matrix &assign_temporary (triangular_matrix &m) {
231 triangular_matrix &operator = (const matrix_expression<AE> &ae) {
232 self_type temporary (ae);
233 return assign_temporary (temporary);
237 triangular_matrix &assign (const matrix_expression<AE> &ae) {
238 matrix_assign<scalar_assign> (*this, ae);
243 triangular_matrix& operator += (const matrix_expression<AE> &ae) {
244 self_type temporary (*this + ae);
245 return assign_temporary (temporary);
249 triangular_matrix &plus_assign (const matrix_expression<AE> &ae) {
250 matrix_assign<scalar_plus_assign> (*this, ae);
255 triangular_matrix& operator -= (const matrix_expression<AE> &ae) {
256 self_type temporary (*this - ae);
257 return assign_temporary (temporary);
261 triangular_matrix &minus_assign (const matrix_expression<AE> &ae) {
262 matrix_assign<scalar_minus_assign> (*this, ae);
267 triangular_matrix& operator *= (const AT &at) {
268 matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
273 triangular_matrix& operator /= (const AT &at) {
274 matrix_assign_scalar<scalar_divides_assign> (*this, at);
280 void swap (triangular_matrix &m) {
282 // BOOST_UBLAS_CHECK (size2_ == m.size2_, bad_size ());
283 std::swap (size1_, m.size1_);
284 std::swap (size2_, m.size2_);
285 data ().swap (m.data ());
289 friend void swap (triangular_matrix &m1, triangular_matrix &m2) {
294 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
295 typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1;
296 typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2;
297 typedef indexed_const_iterator1<self_type, packed_random_access_iterator_tag> const_iterator1;
298 typedef indexed_const_iterator2<self_type, packed_random_access_iterator_tag> const_iterator2;
300 class const_iterator1;
302 class const_iterator2;
305 typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
306 typedef reverse_iterator_base1<iterator1> reverse_iterator1;
307 typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
308 typedef reverse_iterator_base2<iterator2> reverse_iterator2;
312 const_iterator1 find1 (int rank, size_type i, size_type j) const {
314 i = triangular_type::restrict1 (i, j, size1_, size2_);
316 i = triangular_type::global_restrict1 (i, size1_, j, size2_);
317 return const_iterator1 (*this, i, j);
320 iterator1 find1 (int rank, size_type i, size_type j) {
322 i = triangular_type::mutable_restrict1 (i, j, size1_, size2_);
324 i = triangular_type::global_mutable_restrict1 (i, size1_, j, size2_);
325 return iterator1 (*this, i, j);
328 const_iterator2 find2 (int rank, size_type i, size_type j) const {
330 j = triangular_type::restrict2 (i, j, size1_, size2_);
332 j = triangular_type::global_restrict2 (i, size1_, j, size2_);
333 return const_iterator2 (*this, i, j);
336 iterator2 find2 (int rank, size_type i, size_type j) {
338 j = triangular_type::mutable_restrict2 (i, j, size1_, size2_);
340 j = triangular_type::global_mutable_restrict2 (i, size1_, j, size2_);
341 return iterator2 (*this, i, j);
344 // Iterators simply are indices.
346 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
347 class const_iterator1:
348 public container_const_reference<triangular_matrix>,
349 public random_access_iterator_base<packed_random_access_iterator_tag,
350 const_iterator1, value_type> {
352 typedef typename triangular_matrix::value_type value_type;
353 typedef typename triangular_matrix::difference_type difference_type;
354 typedef typename triangular_matrix::const_reference reference;
355 typedef const typename triangular_matrix::pointer pointer;
357 typedef const_iterator2 dual_iterator_type;
358 typedef const_reverse_iterator2 dual_reverse_iterator_type;
360 // Construction and destruction
363 container_const_reference<self_type> (), it1_ (), it2_ () {}
365 const_iterator1 (const self_type &m, size_type it1, size_type it2):
366 container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
368 const_iterator1 (const iterator1 &it):
369 container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {}
373 const_iterator1 &operator ++ () {
378 const_iterator1 &operator -- () {
383 const_iterator1 &operator += (difference_type n) {
388 const_iterator1 &operator -= (difference_type n) {
393 difference_type operator - (const const_iterator1 &it) const {
394 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
395 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
396 return it1_ - it.it1_;
401 const_reference operator * () const {
402 return (*this) () (it1_, it2_);
405 const_reference operator [] (difference_type n) const {
409 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
411 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
414 const_iterator2 begin () const {
415 return (*this) ().find2 (1, it1_, 0);
418 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
421 const_iterator2 cbegin () const {
425 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
428 const_iterator2 end () const {
429 return (*this) ().find2 (1, it1_, (*this) ().size2 ());
432 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
435 const_iterator2 cend () const {
439 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
442 const_reverse_iterator2 rbegin () const {
443 return const_reverse_iterator2 (end ());
446 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
449 const_reverse_iterator2 crbegin () const {
453 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
456 const_reverse_iterator2 rend () const {
457 return const_reverse_iterator2 (begin ());
460 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
463 const_reverse_iterator2 crend () const {
470 size_type index1 () const {
474 size_type index2 () const {
480 const_iterator1 &operator = (const const_iterator1 &it) {
481 container_const_reference<self_type>::assign (&it ());
489 bool operator == (const const_iterator1 &it) const {
490 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
491 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
492 return it1_ == it.it1_;
495 bool operator < (const const_iterator1 &it) const {
496 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
497 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
498 return it1_ < it.it1_;
508 const_iterator1 begin1 () const {
509 return find1 (0, 0, 0);
512 const_iterator1 cbegin1 () const {
516 const_iterator1 end1 () const {
517 return find1 (0, size1_, 0);
520 const_iterator1 cend1 () const {
524 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
526 public container_reference<triangular_matrix>,
527 public random_access_iterator_base<packed_random_access_iterator_tag,
528 iterator1, value_type> {
530 typedef typename triangular_matrix::value_type value_type;
531 typedef typename triangular_matrix::difference_type difference_type;
532 typedef typename triangular_matrix::reference reference;
533 typedef typename triangular_matrix::pointer pointer;
535 typedef iterator2 dual_iterator_type;
536 typedef reverse_iterator2 dual_reverse_iterator_type;
538 // Construction and destruction
541 container_reference<self_type> (), it1_ (), it2_ () {}
543 iterator1 (self_type &m, size_type it1, size_type it2):
544 container_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
548 iterator1 &operator ++ () {
553 iterator1 &operator -- () {
558 iterator1 &operator += (difference_type n) {
563 iterator1 &operator -= (difference_type n) {
568 difference_type operator - (const iterator1 &it) const {
569 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
570 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
571 return it1_ - it.it1_;
576 reference operator * () const {
577 return (*this) () (it1_, it2_);
580 reference operator [] (difference_type n) const {
584 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
586 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
589 iterator2 begin () const {
590 return (*this) ().find2 (1, it1_, 0);
593 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
596 iterator2 end () const {
597 return (*this) ().find2 (1, it1_, (*this) ().size2 ());
600 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
603 reverse_iterator2 rbegin () const {
604 return reverse_iterator2 (end ());
607 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
610 reverse_iterator2 rend () const {
611 return reverse_iterator2 (begin ());
617 size_type index1 () const {
621 size_type index2 () const {
627 iterator1 &operator = (const iterator1 &it) {
628 container_reference<self_type>::assign (&it ());
636 bool operator == (const iterator1 &it) const {
637 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
638 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
639 return it1_ == it.it1_;
642 bool operator < (const iterator1 &it) const {
643 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
644 BOOST_UBLAS_CHECK (it2_ == it.it2_, external_logic ());
645 return it1_ < it.it1_;
652 friend class const_iterator1;
657 iterator1 begin1 () {
658 return find1 (0, 0, 0);
662 return find1 (0, size1_, 0);
665 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
666 class const_iterator2:
667 public container_const_reference<triangular_matrix>,
668 public random_access_iterator_base<packed_random_access_iterator_tag,
669 const_iterator2, value_type> {
671 typedef typename triangular_matrix::value_type value_type;
672 typedef typename triangular_matrix::difference_type difference_type;
673 typedef typename triangular_matrix::const_reference reference;
674 typedef const typename triangular_matrix::pointer pointer;
676 typedef const_iterator1 dual_iterator_type;
677 typedef const_reverse_iterator1 dual_reverse_iterator_type;
679 // Construction and destruction
682 container_const_reference<self_type> (), it1_ (), it2_ () {}
684 const_iterator2 (const self_type &m, size_type it1, size_type it2):
685 container_const_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
687 const_iterator2 (const iterator2 &it):
688 container_const_reference<self_type> (it ()), it1_ (it.it1_), it2_ (it.it2_) {}
692 const_iterator2 &operator ++ () {
697 const_iterator2 &operator -- () {
702 const_iterator2 &operator += (difference_type n) {
707 const_iterator2 &operator -= (difference_type n) {
712 difference_type operator - (const const_iterator2 &it) const {
713 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
714 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
715 return it2_ - it.it2_;
720 const_reference operator * () const {
721 return (*this) () (it1_, it2_);
724 const_reference operator [] (difference_type n) const {
728 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
730 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
733 const_iterator1 begin () const {
734 return (*this) ().find1 (1, 0, it2_);
737 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
740 const_iterator1 cbegin () const {
744 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
747 const_iterator1 end () const {
748 return (*this) ().find1 (1, (*this) ().size1 (), it2_);
751 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
754 const_iterator1 cend () const {
758 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
761 const_reverse_iterator1 rbegin () const {
762 return const_reverse_iterator1 (end ());
765 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
768 const_reverse_iterator1 crbegin () const {
773 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
776 const_reverse_iterator1 rend () const {
777 return const_reverse_iterator1 (begin ());
780 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
783 const_reverse_iterator1 crend () const {
790 size_type index1 () const {
794 size_type index2 () const {
800 const_iterator2 &operator = (const const_iterator2 &it) {
801 container_const_reference<self_type>::assign (&it ());
809 bool operator == (const const_iterator2 &it) const {
810 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
811 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
812 return it2_ == it.it2_;
815 bool operator < (const const_iterator2 &it) const {
816 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
817 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
818 return it2_ < it.it2_;
828 const_iterator2 begin2 () const {
829 return find2 (0, 0, 0);
832 const_iterator2 cbegin2 () const {
836 const_iterator2 end2 () const {
837 return find2 (0, 0, size2_);
840 const_iterator2 cend2 () const {
844 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
846 public container_reference<triangular_matrix>,
847 public random_access_iterator_base<packed_random_access_iterator_tag,
848 iterator2, value_type> {
850 typedef typename triangular_matrix::value_type value_type;
851 typedef typename triangular_matrix::difference_type difference_type;
852 typedef typename triangular_matrix::reference reference;
853 typedef typename triangular_matrix::pointer pointer;
855 typedef iterator1 dual_iterator_type;
856 typedef reverse_iterator1 dual_reverse_iterator_type;
858 // Construction and destruction
861 container_reference<self_type> (), it1_ (), it2_ () {}
863 iterator2 (self_type &m, size_type it1, size_type it2):
864 container_reference<self_type> (m), it1_ (it1), it2_ (it2) {}
868 iterator2 &operator ++ () {
873 iterator2 &operator -- () {
878 iterator2 &operator += (difference_type n) {
883 iterator2 &operator -= (difference_type n) {
888 difference_type operator - (const iterator2 &it) const {
889 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
890 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
891 return it2_ - it.it2_;
896 reference operator * () const {
897 return (*this) () (it1_, it2_);
900 reference operator [] (difference_type n) const {
904 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
906 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
909 iterator1 begin () const {
910 return (*this) ().find1 (1, 0, it2_);
913 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
916 iterator1 end () const {
917 return (*this) ().find1 (1, (*this) ().size1 (), it2_);
920 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
923 reverse_iterator1 rbegin () const {
924 return reverse_iterator1 (end ());
927 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
930 reverse_iterator1 rend () const {
931 return reverse_iterator1 (begin ());
937 size_type index1 () const {
941 size_type index2 () const {
947 iterator2 &operator = (const iterator2 &it) {
948 container_reference<self_type>::assign (&it ());
956 bool operator == (const iterator2 &it) const {
957 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
958 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
959 return it2_ == it.it2_;
962 bool operator < (const iterator2 &it) const {
963 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
964 BOOST_UBLAS_CHECK (it1_ == it.it1_, external_logic ());
965 return it2_ < it.it2_;
972 friend class const_iterator2;
977 iterator2 begin2 () {
978 return find2 (0, 0, 0);
982 return find2 (0, 0, size2_);
988 const_reverse_iterator1 rbegin1 () const {
989 return const_reverse_iterator1 (end1 ());
992 const_reverse_iterator1 crbegin1 () const {
996 const_reverse_iterator1 rend1 () const {
997 return const_reverse_iterator1 (begin1 ());
1000 const_reverse_iterator1 crend1 () const {
1005 reverse_iterator1 rbegin1 () {
1006 return reverse_iterator1 (end1 ());
1009 reverse_iterator1 rend1 () {
1010 return reverse_iterator1 (begin1 ());
1014 const_reverse_iterator2 rbegin2 () const {
1015 return const_reverse_iterator2 (end2 ());
1018 const_reverse_iterator2 crbegin2 () const {
1022 const_reverse_iterator2 rend2 () const {
1023 return const_reverse_iterator2 (begin2 ());
1026 const_reverse_iterator2 crend2 () const {
1031 reverse_iterator2 rbegin2 () {
1032 return reverse_iterator2 (end2 ());
1035 reverse_iterator2 rend2 () {
1036 return reverse_iterator2 (begin2 ());
1043 static const value_type zero_;
1044 static const value_type one_;
1047 template<class T, class TRI, class L, class A>
1048 const typename triangular_matrix<T, TRI, L, A>::value_type triangular_matrix<T, TRI, L, A>::zero_ = value_type/*zero*/();
1049 template<class T, class TRI, class L, class A>
1050 const typename triangular_matrix<T, TRI, L, A>::value_type triangular_matrix<T, TRI, L, A>::one_ (1);
1053 // Triangular matrix adaptor class
1054 template<class M, class TRI>
1055 class triangular_adaptor:
1056 public matrix_expression<triangular_adaptor<M, TRI> > {
1058 typedef triangular_adaptor<M, TRI> self_type;
1061 #ifdef BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS
1062 using matrix_expression<self_type>::operator ();
1064 typedef const M const_matrix_type;
1065 typedef M matrix_type;
1066 typedef TRI triangular_type;
1067 typedef typename M::size_type size_type;
1068 typedef typename M::difference_type difference_type;
1069 typedef typename M::value_type value_type;
1070 typedef typename M::const_reference const_reference;
1071 typedef typename boost::mpl::if_<boost::is_const<M>,
1072 typename M::const_reference,
1073 typename M::reference>::type reference;
1074 typedef typename boost::mpl::if_<boost::is_const<M>,
1075 typename M::const_closure_type,
1076 typename M::closure_type>::type matrix_closure_type;
1077 typedef const self_type const_closure_type;
1078 typedef self_type closure_type;
1079 // Replaced by _temporary_traits to avoid type requirements on M
1080 //typedef typename M::vector_temporary_type vector_temporary_type;
1081 //typedef typename M::matrix_temporary_type matrix_temporary_type;
1082 typedef typename storage_restrict_traits<typename M::storage_category,
1083 packed_proxy_tag>::storage_category storage_category;
1084 typedef typename M::orientation_category orientation_category;
1086 // Construction and destruction
1088 triangular_adaptor (matrix_type &data):
1089 matrix_expression<self_type> (),
1092 triangular_adaptor (const triangular_adaptor &m):
1093 matrix_expression<self_type> (),
1098 size_type size1 () const {
1099 return data_.size1 ();
1102 size_type size2 () const {
1103 return data_.size2 ();
1106 // Storage accessors
1108 const matrix_closure_type &data () const {
1112 matrix_closure_type &data () {
1117 #ifndef BOOST_UBLAS_PROXY_CONST_MEMBER
1119 const_reference operator () (size_type i, size_type j) const {
1120 BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
1121 BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
1122 if (triangular_type::other (i, j))
1123 return data () (i, j);
1124 else if (triangular_type::one (i, j))
1130 reference operator () (size_type i, size_type j) {
1131 BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
1132 BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
1133 if (!triangular_type::other (i, j)) {
1134 bad_index ().raise ();
1137 return data () (i, j);
1141 reference operator () (size_type i, size_type j) const {
1142 BOOST_UBLAS_CHECK (i < size1 (), bad_index ());
1143 BOOST_UBLAS_CHECK (j < size2 (), bad_index ());
1144 if (!triangular_type::other (i, j)) {
1145 bad_index ().raise ();
1148 return data () (i, j);
1154 triangular_adaptor &operator = (const triangular_adaptor &m) {
1155 matrix_assign<scalar_assign> (*this, m);
1159 triangular_adaptor &assign_temporary (triangular_adaptor &m) {
1165 triangular_adaptor &operator = (const matrix_expression<AE> &ae) {
1166 matrix_assign<scalar_assign> (*this, matrix<value_type> (ae));
1171 triangular_adaptor &assign (const matrix_expression<AE> &ae) {
1172 matrix_assign<scalar_assign> (*this, ae);
1177 triangular_adaptor& operator += (const matrix_expression<AE> &ae) {
1178 matrix_assign<scalar_assign> (*this, matrix<value_type> (*this + ae));
1183 triangular_adaptor &plus_assign (const matrix_expression<AE> &ae) {
1184 matrix_assign<scalar_plus_assign> (*this, ae);
1189 triangular_adaptor& operator -= (const matrix_expression<AE> &ae) {
1190 matrix_assign<scalar_assign> (*this, matrix<value_type> (*this - ae));
1195 triangular_adaptor &minus_assign (const matrix_expression<AE> &ae) {
1196 matrix_assign<scalar_minus_assign> (*this, ae);
1201 triangular_adaptor& operator *= (const AT &at) {
1202 matrix_assign_scalar<scalar_multiplies_assign> (*this, at);
1207 triangular_adaptor& operator /= (const AT &at) {
1208 matrix_assign_scalar<scalar_divides_assign> (*this, at);
1212 // Closure comparison
1214 bool same_closure (const triangular_adaptor &ta) const {
1215 return (*this).data ().same_closure (ta.data ());
1220 void swap (triangular_adaptor &m) {
1222 matrix_swap<scalar_swap> (*this, m);
1225 friend void swap (triangular_adaptor &m1, triangular_adaptor &m2) {
1231 typedef typename M::const_iterator1 const_subiterator1_type;
1232 typedef typename boost::mpl::if_<boost::is_const<M>,
1233 typename M::const_iterator1,
1234 typename M::iterator1>::type subiterator1_type;
1235 typedef typename M::const_iterator2 const_subiterator2_type;
1236 typedef typename boost::mpl::if_<boost::is_const<M>,
1237 typename M::const_iterator2,
1238 typename M::iterator2>::type subiterator2_type;
1241 #ifdef BOOST_UBLAS_USE_INDEXED_ITERATOR
1242 typedef indexed_iterator1<self_type, packed_random_access_iterator_tag> iterator1;
1243 typedef indexed_iterator2<self_type, packed_random_access_iterator_tag> iterator2;
1244 typedef indexed_const_iterator1<self_type, packed_random_access_iterator_tag> const_iterator1;
1245 typedef indexed_const_iterator2<self_type, packed_random_access_iterator_tag> const_iterator2;
1247 class const_iterator1;
1249 class const_iterator2;
1252 typedef reverse_iterator_base1<const_iterator1> const_reverse_iterator1;
1253 typedef reverse_iterator_base1<iterator1> reverse_iterator1;
1254 typedef reverse_iterator_base2<const_iterator2> const_reverse_iterator2;
1255 typedef reverse_iterator_base2<iterator2> reverse_iterator2;
1259 const_iterator1 find1 (int rank, size_type i, size_type j) const {
1261 i = triangular_type::restrict1 (i, j, size1(), size2());
1263 i = triangular_type::global_restrict1 (i, size1(), j, size2());
1264 return const_iterator1 (*this, data ().find1 (rank, i, j));
1267 iterator1 find1 (int rank, size_type i, size_type j) {
1269 i = triangular_type::mutable_restrict1 (i, j, size1(), size2());
1271 i = triangular_type::global_mutable_restrict1 (i, size1(), j, size2());
1272 return iterator1 (*this, data ().find1 (rank, i, j));
1275 const_iterator2 find2 (int rank, size_type i, size_type j) const {
1277 j = triangular_type::restrict2 (i, j, size1(), size2());
1279 j = triangular_type::global_restrict2 (i, size1(), j, size2());
1280 return const_iterator2 (*this, data ().find2 (rank, i, j));
1283 iterator2 find2 (int rank, size_type i, size_type j) {
1285 j = triangular_type::mutable_restrict2 (i, j, size1(), size2());
1287 j = triangular_type::global_mutable_restrict2 (i, size1(), j, size2());
1288 return iterator2 (*this, data ().find2 (rank, i, j));
1291 // Iterators simply are indices.
1293 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1294 class const_iterator1:
1295 public container_const_reference<triangular_adaptor>,
1296 public random_access_iterator_base<typename iterator_restrict_traits<
1297 typename const_subiterator1_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
1298 const_iterator1, value_type> {
1300 typedef typename const_subiterator1_type::value_type value_type;
1301 typedef typename const_subiterator1_type::difference_type difference_type;
1302 typedef typename const_subiterator1_type::reference reference;
1303 typedef typename const_subiterator1_type::pointer pointer;
1305 typedef const_iterator2 dual_iterator_type;
1306 typedef const_reverse_iterator2 dual_reverse_iterator_type;
1308 // Construction and destruction
1311 container_const_reference<self_type> (), it1_ () {}
1313 const_iterator1 (const self_type &m, const const_subiterator1_type &it1):
1314 container_const_reference<self_type> (m), it1_ (it1) {}
1316 const_iterator1 (const iterator1 &it):
1317 container_const_reference<self_type> (it ()), it1_ (it.it1_) {}
1321 const_iterator1 &operator ++ () {
1326 const_iterator1 &operator -- () {
1331 const_iterator1 &operator += (difference_type n) {
1336 const_iterator1 &operator -= (difference_type n) {
1341 difference_type operator - (const const_iterator1 &it) const {
1342 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1343 return it1_ - it.it1_;
1348 const_reference operator * () const {
1349 size_type i = index1 ();
1350 size_type j = index2 ();
1351 BOOST_UBLAS_CHECK (i < (*this) ().size1 (), bad_index ());
1352 BOOST_UBLAS_CHECK (j < (*this) ().size2 (), bad_index ());
1353 if (triangular_type::other (i, j))
1356 return (*this) () (i, j);
1359 const_reference operator [] (difference_type n) const {
1360 return *(*this + n);
1363 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
1365 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1366 typename self_type::
1368 const_iterator2 begin () const {
1369 return (*this) ().find2 (1, index1 (), 0);
1372 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1373 typename self_type::
1375 const_iterator2 cbegin () const {
1379 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1380 typename self_type::
1382 const_iterator2 end () const {
1383 return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
1386 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1387 typename self_type::
1389 const_iterator2 cend () const {
1394 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1395 typename self_type::
1397 const_reverse_iterator2 rbegin () const {
1398 return const_reverse_iterator2 (end ());
1401 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1402 typename self_type::
1404 const_reverse_iterator2 crbegin () const {
1408 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1409 typename self_type::
1411 const_reverse_iterator2 rend () const {
1412 return const_reverse_iterator2 (begin ());
1415 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1416 typename self_type::
1418 const_reverse_iterator2 crend () const {
1425 size_type index1 () const {
1426 return it1_.index1 ();
1429 size_type index2 () const {
1430 return it1_.index2 ();
1435 const_iterator1 &operator = (const const_iterator1 &it) {
1436 container_const_reference<self_type>::assign (&it ());
1443 bool operator == (const const_iterator1 &it) const {
1444 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1445 return it1_ == it.it1_;
1448 bool operator < (const const_iterator1 &it) const {
1449 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1450 return it1_ < it.it1_;
1454 const_subiterator1_type it1_;
1459 const_iterator1 begin1 () const {
1460 return find1 (0, 0, 0);
1463 const_iterator1 cbegin1 () const {
1467 const_iterator1 end1 () const {
1468 return find1 (0, size1 (), 0);
1471 const_iterator1 cend1 () const {
1475 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1477 public container_reference<triangular_adaptor>,
1478 public random_access_iterator_base<typename iterator_restrict_traits<
1479 typename subiterator1_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
1480 iterator1, value_type> {
1482 typedef typename subiterator1_type::value_type value_type;
1483 typedef typename subiterator1_type::difference_type difference_type;
1484 typedef typename subiterator1_type::reference reference;
1485 typedef typename subiterator1_type::pointer pointer;
1487 typedef iterator2 dual_iterator_type;
1488 typedef reverse_iterator2 dual_reverse_iterator_type;
1490 // Construction and destruction
1493 container_reference<self_type> (), it1_ () {}
1495 iterator1 (self_type &m, const subiterator1_type &it1):
1496 container_reference<self_type> (m), it1_ (it1) {}
1500 iterator1 &operator ++ () {
1505 iterator1 &operator -- () {
1510 iterator1 &operator += (difference_type n) {
1515 iterator1 &operator -= (difference_type n) {
1520 difference_type operator - (const iterator1 &it) const {
1521 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1522 return it1_ - it.it1_;
1527 reference operator * () const {
1528 size_type i = index1 ();
1529 size_type j = index2 ();
1530 BOOST_UBLAS_CHECK (i < (*this) ().size1 (), bad_index ());
1531 BOOST_UBLAS_CHECK (j < (*this) ().size2 (), bad_index ());
1532 if (triangular_type::other (i, j))
1535 return (*this) () (i, j);
1538 reference operator [] (difference_type n) const {
1539 return *(*this + n);
1542 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
1544 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1545 typename self_type::
1547 iterator2 begin () const {
1548 return (*this) ().find2 (1, index1 (), 0);
1551 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1552 typename self_type::
1554 iterator2 end () const {
1555 return (*this) ().find2 (1, index1 (), (*this) ().size2 ());
1558 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1559 typename self_type::
1561 reverse_iterator2 rbegin () const {
1562 return reverse_iterator2 (end ());
1565 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1566 typename self_type::
1568 reverse_iterator2 rend () const {
1569 return reverse_iterator2 (begin ());
1575 size_type index1 () const {
1576 return it1_.index1 ();
1579 size_type index2 () const {
1580 return it1_.index2 ();
1585 iterator1 &operator = (const iterator1 &it) {
1586 container_reference<self_type>::assign (&it ());
1593 bool operator == (const iterator1 &it) const {
1594 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1595 return it1_ == it.it1_;
1598 bool operator < (const iterator1 &it) const {
1599 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1600 return it1_ < it.it1_;
1604 subiterator1_type it1_;
1606 friend class const_iterator1;
1611 iterator1 begin1 () {
1612 return find1 (0, 0, 0);
1616 return find1 (0, size1 (), 0);
1619 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1620 class const_iterator2:
1621 public container_const_reference<triangular_adaptor>,
1622 public random_access_iterator_base<typename iterator_restrict_traits<
1623 typename const_subiterator1_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
1624 const_iterator2, value_type> {
1626 typedef typename const_subiterator2_type::value_type value_type;
1627 typedef typename const_subiterator2_type::difference_type difference_type;
1628 typedef typename const_subiterator2_type::reference reference;
1629 typedef typename const_subiterator2_type::pointer pointer;
1631 typedef const_iterator1 dual_iterator_type;
1632 typedef const_reverse_iterator1 dual_reverse_iterator_type;
1634 // Construction and destruction
1637 container_const_reference<self_type> (), it2_ () {}
1639 const_iterator2 (const self_type &m, const const_subiterator2_type &it2):
1640 container_const_reference<self_type> (m), it2_ (it2) {}
1642 const_iterator2 (const iterator2 &it):
1643 container_const_reference<self_type> (it ()), it2_ (it.it2_) {}
1647 const_iterator2 &operator ++ () {
1652 const_iterator2 &operator -- () {
1657 const_iterator2 &operator += (difference_type n) {
1662 const_iterator2 &operator -= (difference_type n) {
1667 difference_type operator - (const const_iterator2 &it) const {
1668 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1669 return it2_ - it.it2_;
1674 const_reference operator * () const {
1675 size_type i = index1 ();
1676 size_type j = index2 ();
1677 BOOST_UBLAS_CHECK (i < (*this) ().size1 (), bad_index ());
1678 BOOST_UBLAS_CHECK (j < (*this) ().size2 (), bad_index ());
1679 if (triangular_type::other (i, j))
1682 return (*this) () (i, j);
1685 const_reference operator [] (difference_type n) const {
1686 return *(*this + n);
1689 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
1691 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1692 typename self_type::
1694 const_iterator1 begin () const {
1695 return (*this) ().find1 (1, 0, index2 ());
1698 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1699 typename self_type::
1701 const_iterator1 cbegin () const {
1705 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1706 typename self_type::
1708 const_iterator1 end () const {
1709 return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
1712 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1713 typename self_type::
1715 const_iterator1 cend () const {
1719 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1720 typename self_type::
1722 const_reverse_iterator1 rbegin () const {
1723 return const_reverse_iterator1 (end ());
1726 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1727 typename self_type::
1729 const_reverse_iterator1 crbegin () const {
1733 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1734 typename self_type::
1736 const_reverse_iterator1 rend () const {
1737 return const_reverse_iterator1 (begin ());
1740 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1741 typename self_type::
1743 const_reverse_iterator1 crend () const {
1750 size_type index1 () const {
1751 return it2_.index1 ();
1754 size_type index2 () const {
1755 return it2_.index2 ();
1760 const_iterator2 &operator = (const const_iterator2 &it) {
1761 container_const_reference<self_type>::assign (&it ());
1768 bool operator == (const const_iterator2 &it) const {
1769 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1770 return it2_ == it.it2_;
1773 bool operator < (const const_iterator2 &it) const {
1774 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1775 return it2_ < it.it2_;
1779 const_subiterator2_type it2_;
1784 const_iterator2 begin2 () const {
1785 return find2 (0, 0, 0);
1788 const_iterator2 cbegin2 () const {
1792 const_iterator2 end2 () const {
1793 return find2 (0, 0, size2 ());
1796 const_iterator2 cend2 () const {
1800 #ifndef BOOST_UBLAS_USE_INDEXED_ITERATOR
1802 public container_reference<triangular_adaptor>,
1803 public random_access_iterator_base<typename iterator_restrict_traits<
1804 typename subiterator1_type::iterator_category, packed_random_access_iterator_tag>::iterator_category,
1805 iterator2, value_type> {
1807 typedef typename subiterator2_type::value_type value_type;
1808 typedef typename subiterator2_type::difference_type difference_type;
1809 typedef typename subiterator2_type::reference reference;
1810 typedef typename subiterator2_type::pointer pointer;
1812 typedef iterator1 dual_iterator_type;
1813 typedef reverse_iterator1 dual_reverse_iterator_type;
1815 // Construction and destruction
1818 container_reference<self_type> (), it2_ () {}
1820 iterator2 (self_type &m, const subiterator2_type &it2):
1821 container_reference<self_type> (m), it2_ (it2) {}
1825 iterator2 &operator ++ () {
1830 iterator2 &operator -- () {
1835 iterator2 &operator += (difference_type n) {
1840 iterator2 &operator -= (difference_type n) {
1845 difference_type operator - (const iterator2 &it) const {
1846 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1847 return it2_ - it.it2_;
1852 reference operator * () const {
1853 size_type i = index1 ();
1854 size_type j = index2 ();
1855 BOOST_UBLAS_CHECK (i < (*this) ().size1 (), bad_index ());
1856 BOOST_UBLAS_CHECK (j < (*this) ().size2 (), bad_index ());
1857 if (triangular_type::other (i, j))
1860 return (*this) () (i, j);
1863 reference operator [] (difference_type n) const {
1864 return *(*this + n);
1867 #ifndef BOOST_UBLAS_NO_NESTED_CLASS_RELATION
1869 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1870 typename self_type::
1872 iterator1 begin () const {
1873 return (*this) ().find1 (1, 0, index2 ());
1876 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1877 typename self_type::
1879 iterator1 end () const {
1880 return (*this) ().find1 (1, (*this) ().size1 (), index2 ());
1883 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1884 typename self_type::
1886 reverse_iterator1 rbegin () const {
1887 return reverse_iterator1 (end ());
1890 #ifdef BOOST_UBLAS_MSVC_NESTED_CLASS_RELATION
1891 typename self_type::
1893 reverse_iterator1 rend () const {
1894 return reverse_iterator1 (begin ());
1900 size_type index1 () const {
1901 return it2_.index1 ();
1904 size_type index2 () const {
1905 return it2_.index2 ();
1910 iterator2 &operator = (const iterator2 &it) {
1911 container_reference<self_type>::assign (&it ());
1918 bool operator == (const iterator2 &it) const {
1919 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1920 return it2_ == it.it2_;
1923 bool operator < (const iterator2 &it) const {
1924 BOOST_UBLAS_CHECK (&(*this) () == &it (), external_logic ());
1925 return it2_ < it.it2_;
1929 subiterator2_type it2_;
1931 friend class const_iterator2;
1936 iterator2 begin2 () {
1937 return find2 (0, 0, 0);
1941 return find2 (0, 0, size2 ());
1944 // Reverse iterators
1947 const_reverse_iterator1 rbegin1 () const {
1948 return const_reverse_iterator1 (end1 ());
1951 const_reverse_iterator1 crbegin1 () const {
1955 const_reverse_iterator1 rend1 () const {
1956 return const_reverse_iterator1 (begin1 ());
1959 const_reverse_iterator1 crend1 () const {
1964 reverse_iterator1 rbegin1 () {
1965 return reverse_iterator1 (end1 ());
1968 reverse_iterator1 rend1 () {
1969 return reverse_iterator1 (begin1 ());
1973 const_reverse_iterator2 rbegin2 () const {
1974 return const_reverse_iterator2 (end2 ());
1977 const_reverse_iterator2 crbegin2 () const {
1981 const_reverse_iterator2 rend2 () const {
1982 return const_reverse_iterator2 (begin2 ());
1985 const_reverse_iterator2 crend2 () const {
1990 reverse_iterator2 rbegin2 () {
1991 return reverse_iterator2 (end2 ());
1994 reverse_iterator2 rend2 () {
1995 return reverse_iterator2 (begin2 ());
1999 matrix_closure_type data_;
2000 static const value_type zero_;
2001 static const value_type one_;
2004 template<class M, class TRI>
2005 const typename triangular_adaptor<M, TRI>::value_type triangular_adaptor<M, TRI>::zero_ = value_type/*zero*/();
2006 template<class M, class TRI>
2007 const typename triangular_adaptor<M, TRI>::value_type triangular_adaptor<M, TRI>::one_ (1);
2009 template <class M, class TRI>
2010 struct vector_temporary_traits< triangular_adaptor<M, TRI> >
2011 : vector_temporary_traits< typename boost::remove_const<M>::type > {} ;
2012 template <class M, class TRI>
2013 struct vector_temporary_traits< const triangular_adaptor<M, TRI> >
2014 : vector_temporary_traits< typename boost::remove_const<M>::type > {} ;
2016 template <class M, class TRI>
2017 struct matrix_temporary_traits< triangular_adaptor<M, TRI> >
2018 : matrix_temporary_traits< typename boost::remove_const<M>::type > {};
2019 template <class M, class TRI>
2020 struct matrix_temporary_traits< const triangular_adaptor<M, TRI> >
2021 : matrix_temporary_traits< typename boost::remove_const<M>::type > {};
2024 template<class E1, class E2>
2025 struct matrix_vector_solve_traits {
2026 typedef typename promote_traits<typename E1::value_type, typename E2::value_type>::promote_type promote_type;
2027 typedef vector<promote_type> result_type;
2031 // n * (n - 1) / 2 + n = n * (n + 1) / 2 multiplications,
2032 // n * (n - 1) / 2 additions
2034 // Dense (proxy) case
2035 template<class E1, class E2>
2037 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2038 lower_tag, column_major_tag, dense_proxy_tag) {
2039 typedef typename E2::size_type size_type;
2040 typedef typename E2::value_type value_type;
2042 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2043 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2044 size_type size = e2 ().size ();
2045 for (size_type n = 0; n < size; ++ n) {
2046 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2047 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2049 if (e1 () (n, n) == value_type/*zero*/())
2050 singular ().raise ();
2052 value_type t = e2 () (n) /= e1 () (n, n);
2053 if (t != value_type/*zero*/()) {
2054 for (size_type m = n + 1; m < size; ++ m)
2055 e2 () (m) -= e1 () (m, n) * t;
2059 // Packed (proxy) case
2060 template<class E1, class E2>
2062 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2063 lower_tag, column_major_tag, packed_proxy_tag) {
2064 typedef typename E2::size_type size_type;
2065 typedef typename E2::difference_type difference_type;
2066 typedef typename E2::value_type value_type;
2068 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2069 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2070 size_type size = e2 ().size ();
2071 for (size_type n = 0; n < size; ++ n) {
2072 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2073 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2075 if (e1 () (n, n) == value_type/*zero*/())
2076 singular ().raise ();
2078 value_type t = e2 () (n) /= e1 () (n, n);
2079 if (t != value_type/*zero*/()) {
2080 typename E1::const_iterator1 it1e1 (e1 ().find1 (1, n + 1, n));
2081 typename E1::const_iterator1 it1e1_end (e1 ().find1 (1, e1 ().size1 (), n));
2082 difference_type m (it1e1_end - it1e1);
2084 e2 () (it1e1.index1 ()) -= *it1e1 * t, ++ it1e1;
2088 // Sparse (proxy) case
2089 template<class E1, class E2>
2091 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2092 lower_tag, column_major_tag, unknown_storage_tag) {
2093 typedef typename E2::size_type size_type;
2094 typedef typename E2::value_type value_type;
2096 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2097 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2098 size_type size = e2 ().size ();
2099 for (size_type n = 0; n < size; ++ n) {
2100 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2101 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2103 if (e1 () (n, n) == value_type/*zero*/())
2104 singular ().raise ();
2106 value_type t = e2 () (n) /= e1 () (n, n);
2107 if (t != value_type/*zero*/()) {
2108 typename E1::const_iterator1 it1e1 (e1 ().find1 (1, n + 1, n));
2109 typename E1::const_iterator1 it1e1_end (e1 ().find1 (1, e1 ().size1 (), n));
2110 while (it1e1 != it1e1_end)
2111 e2 () (it1e1.index1 ()) -= *it1e1 * t, ++ it1e1;
2116 // Dense (proxy) case
2117 template<class E1, class E2>
2119 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2120 lower_tag, row_major_tag, dense_proxy_tag) {
2121 typedef typename E2::size_type size_type;
2122 typedef typename E2::value_type value_type;
2124 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2125 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2126 size_type size = e2 ().size ();
2127 for (size_type n = 0; n < size; ++ n) {
2128 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2129 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2131 if (e1 () (n, n) == value_type/*zero*/())
2132 singular ().raise ();
2134 value_type t = e2 () (n) /= e1 () (n, n);
2135 if (t != value_type/*zero*/()) {
2136 for (size_type m = n + 1; m < size; ++ m)
2137 e2 () (m) -= e1 () (m, n) * t;
2141 // Packed (proxy) case
2142 template<class E1, class E2>
2144 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2145 lower_tag, row_major_tag, packed_proxy_tag) {
2146 typedef typename E2::size_type size_type;
2147 typedef typename E2::value_type value_type;
2149 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2150 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2151 size_type size = e2 ().size ();
2152 for (size_type n = 0; n < size; ++ n) {
2153 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2154 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2156 if (e1 () (n, n) == value_type/*zero*/())
2157 singular ().raise ();
2159 value_type t = e2 () (n);
2160 typename E1::const_iterator2 it2e1 (e1 ().find2 (1, n, 0));
2161 typename E1::const_iterator2 it2e1_end (e1 ().find2 (1, n, n));
2162 while (it2e1 != it2e1_end) {
2163 t -= *it2e1 * e2 () (it2e1.index2());
2166 e2() (n) = t / e1 () (n, n);
2169 // Sparse (proxy) case
2170 template<class E1, class E2>
2172 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2173 lower_tag, row_major_tag, unknown_storage_tag) {
2174 typedef typename E2::size_type size_type;
2175 typedef typename E2::value_type value_type;
2177 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2178 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2179 size_type size = e2 ().size ();
2180 for (size_type n = 0; n < size; ++ n) {
2181 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2182 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2184 if (e1 () (n, n) == value_type/*zero*/())
2185 singular ().raise ();
2187 value_type t = e2 () (n);
2188 typename E1::const_iterator2 it2e1 (e1 ().find2 (1, n, 0));
2189 typename E1::const_iterator2 it2e1_end (e1 ().find2 (1, n, n));
2190 while (it2e1 != it2e1_end) {
2191 t -= *it2e1 * e2 () (it2e1.index2());
2194 e2() (n) = t / e1 () (n, n);
2199 template<class E1, class E2>
2201 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2202 lower_tag, column_major_tag) {
2203 typedef typename E1::storage_category storage_category;
2204 inplace_solve (e1, e2,
2205 lower_tag (), column_major_tag (), storage_category ());
2207 template<class E1, class E2>
2209 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2210 lower_tag, row_major_tag) {
2211 typedef typename E1::storage_category storage_category;
2212 inplace_solve (e1, e2,
2213 lower_tag (), row_major_tag (), storage_category ());
2216 template<class E1, class E2>
2218 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2220 typedef typename E1::orientation_category orientation_category;
2221 inplace_solve (e1, e2,
2222 lower_tag (), orientation_category ());
2224 template<class E1, class E2>
2226 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2228 typedef typename E1::orientation_category orientation_category;
2229 inplace_solve (triangular_adaptor<const E1, unit_lower> (e1 ()), e2,
2230 unit_lower_tag (), orientation_category ());
2233 // Dense (proxy) case
2234 template<class E1, class E2>
2236 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2237 upper_tag, column_major_tag, dense_proxy_tag) {
2238 typedef typename E2::size_type size_type;
2239 typedef typename E2::difference_type difference_type;
2240 typedef typename E2::value_type value_type;
2242 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2243 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2244 size_type size = e2 ().size ();
2245 for (difference_type n = size - 1; n >= 0; -- n) {
2246 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2247 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2249 if (e1 () (n, n) == value_type/*zero*/())
2250 singular ().raise ();
2252 value_type t = e2 () (n) /= e1 () (n, n);
2253 if (t != value_type/*zero*/()) {
2254 for (difference_type m = n - 1; m >= 0; -- m)
2255 e2 () (m) -= e1 () (m, n) * t;
2259 // Packed (proxy) case
2260 template<class E1, class E2>
2262 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2263 upper_tag, column_major_tag, packed_proxy_tag) {
2264 typedef typename E2::size_type size_type;
2265 typedef typename E2::difference_type difference_type;
2266 typedef typename E2::value_type value_type;
2268 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2269 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2270 size_type size = e2 ().size ();
2271 for (difference_type n = size - 1; n >= 0; -- n) {
2272 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2273 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2275 if (e1 () (n, n) == value_type/*zero*/())
2276 singular ().raise ();
2278 value_type t = e2 () (n) /= e1 () (n, n);
2279 if (t != value_type/*zero*/()) {
2280 typename E1::const_reverse_iterator1 it1e1 (e1 ().find1 (1, n, n));
2281 typename E1::const_reverse_iterator1 it1e1_rend (e1 ().find1 (1, 0, n));
2282 while (it1e1 != it1e1_rend) {
2283 e2 () (it1e1.index1 ()) -= *it1e1 * t, ++ it1e1;
2288 // Sparse (proxy) case
2289 template<class E1, class E2>
2291 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2292 upper_tag, column_major_tag, unknown_storage_tag) {
2293 typedef typename E2::size_type size_type;
2294 typedef typename E2::difference_type difference_type;
2295 typedef typename E2::value_type value_type;
2297 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2298 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2299 size_type size = e2 ().size ();
2300 for (difference_type n = size - 1; n >= 0; -- n) {
2301 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2302 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2304 if (e1 () (n, n) == value_type/*zero*/())
2305 singular ().raise ();
2307 value_type t = e2 () (n) /= e1 () (n, n);
2308 if (t != value_type/*zero*/()) {
2309 typename E1::const_reverse_iterator1 it1e1 (e1 ().find1 (1, n, n));
2310 typename E1::const_reverse_iterator1 it1e1_rend (e1 ().find1 (1, 0, n));
2311 while (it1e1 != it1e1_rend) {
2312 e2 () (it1e1.index1 ()) -= *it1e1 * t, ++ it1e1;
2318 // Dense (proxy) case
2319 template<class E1, class E2>
2321 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2322 upper_tag, row_major_tag, dense_proxy_tag) {
2323 typedef typename E2::size_type size_type;
2324 typedef typename E2::difference_type difference_type;
2325 typedef typename E2::value_type value_type;
2327 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2328 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2329 size_type size = e1 ().size1 ();
2330 for (difference_type n = size-1; n >=0; -- n) {
2331 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2332 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2334 if (e1 () (n, n) == value_type/*zero*/())
2335 singular ().raise ();
2337 value_type t = e2 () (n);
2338 for (difference_type m = n + 1; m < static_cast<difference_type>(e1 ().size2()); ++ m) {
2339 t -= e1 () (n, m) * e2 () (m);
2341 e2() (n) = t / e1 () (n, n);
2344 // Packed (proxy) case
2345 template<class E1, class E2>
2347 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2348 upper_tag, row_major_tag, packed_proxy_tag) {
2349 typedef typename E2::size_type size_type;
2350 typedef typename E2::difference_type difference_type;
2351 typedef typename E2::value_type value_type;
2353 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2354 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2355 size_type size = e1 ().size1 ();
2356 for (difference_type n = size-1; n >=0; -- n) {
2357 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2358 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2360 if (e1 () (n, n) == value_type/*zero*/())
2361 singular ().raise ();
2363 value_type t = e2 () (n);
2364 typename E1::const_iterator2 it2e1 (e1 ().find2 (1, n, n+1));
2365 typename E1::const_iterator2 it2e1_end (e1 ().find2 (1, n, e1 ().size2 ()));
2366 while (it2e1 != it2e1_end) {
2367 t -= *it2e1 * e2 () (it2e1.index2());
2370 e2() (n) = t / e1 () (n, n);
2374 // Sparse (proxy) case
2375 template<class E1, class E2>
2377 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2378 upper_tag, row_major_tag, unknown_storage_tag) {
2379 typedef typename E2::size_type size_type;
2380 typedef typename E2::difference_type difference_type;
2381 typedef typename E2::value_type value_type;
2383 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2384 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size (), bad_size ());
2385 size_type size = e1 ().size1 ();
2386 for (difference_type n = size-1; n >=0; -- n) {
2387 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2388 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2390 if (e1 () (n, n) == value_type/*zero*/())
2391 singular ().raise ();
2393 value_type t = e2 () (n);
2394 typename E1::const_iterator2 it2e1 (e1 ().find2 (1, n, n+1));
2395 typename E1::const_iterator2 it2e1_end (e1 ().find2 (1, n, e1 ().size2 ()));
2396 while (it2e1 != it2e1_end) {
2397 t -= *it2e1 * e2 () (it2e1.index2());
2400 e2() (n) = t / e1 () (n, n);
2406 template<class E1, class E2>
2408 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2409 upper_tag, column_major_tag) {
2410 typedef typename E1::storage_category storage_category;
2411 inplace_solve (e1, e2,
2412 upper_tag (), column_major_tag (), storage_category ());
2414 template<class E1, class E2>
2416 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2417 upper_tag, row_major_tag) {
2418 typedef typename E1::storage_category storage_category;
2419 inplace_solve (e1, e2,
2420 upper_tag (), row_major_tag (), storage_category ());
2423 template<class E1, class E2>
2425 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2427 typedef typename E1::orientation_category orientation_category;
2428 inplace_solve (e1, e2,
2429 upper_tag (), orientation_category ());
2431 template<class E1, class E2>
2433 void inplace_solve (const matrix_expression<E1> &e1, vector_expression<E2> &e2,
2435 typedef typename E1::orientation_category orientation_category;
2436 inplace_solve (triangular_adaptor<const E1, unit_upper> (e1 ()), e2,
2437 unit_upper_tag (), orientation_category ());
2440 template<class E1, class E2, class C>
2442 typename matrix_vector_solve_traits<E1, E2>::result_type
2443 solve (const matrix_expression<E1> &e1,
2444 const vector_expression<E2> &e2,
2446 typename matrix_vector_solve_traits<E1, E2>::result_type r (e2);
2447 inplace_solve (e1, r, C ());
2453 template<class E1, class E2>
2455 void inplace_solve (vector_expression<E1> &e1, const matrix_expression<E2> &e2,
2456 lower_tag, row_major_tag) {
2457 typedef typename E2::storage_category storage_category;
2458 inplace_solve (trans(e2), e1,
2459 upper_tag (), column_major_tag (), storage_category ());
2461 template<class E1, class E2>
2463 void inplace_solve (vector_expression<E1> &e1, const matrix_expression<E2> &e2,
2464 lower_tag, column_major_tag) {
2465 typedef typename E2::storage_category storage_category;
2466 inplace_solve (trans (e2), e1,
2467 upper_tag (), row_major_tag (), storage_category ());
2470 template<class E1, class E2>
2472 void inplace_solve (vector_expression<E1> &e1, const matrix_expression<E2> &e2,
2474 typedef typename E2::orientation_category orientation_category;
2475 inplace_solve (e1, e2,
2476 lower_tag (), orientation_category ());
2478 template<class E1, class E2>
2480 void inplace_solve (vector_expression<E1> &e1, const matrix_expression<E2> &e2,
2482 typedef typename E2::orientation_category orientation_category;
2483 inplace_solve (e1, triangular_adaptor<const E2, unit_lower> (e2 ()),
2484 unit_lower_tag (), orientation_category ());
2489 template<class E1, class E2>
2491 void inplace_solve (vector_expression<E1> &e1, const matrix_expression<E2> &e2,
2492 upper_tag, row_major_tag) {
2493 typedef typename E2::storage_category storage_category;
2494 inplace_solve (trans(e2), e1,
2495 lower_tag (), column_major_tag (), storage_category ());
2497 template<class E1, class E2>
2499 void inplace_solve (vector_expression<E1> &e1, const matrix_expression<E2> &e2,
2500 upper_tag, column_major_tag) {
2501 typedef typename E2::storage_category storage_category;
2502 inplace_solve (trans (e2), e1,
2503 lower_tag (), row_major_tag (), storage_category ());
2506 template<class E1, class E2>
2508 void inplace_solve (vector_expression<E1> &e1, const matrix_expression<E2> &e2,
2510 typedef typename E2::orientation_category orientation_category;
2511 inplace_solve (e1, e2,
2512 upper_tag (), orientation_category ());
2514 template<class E1, class E2>
2516 void inplace_solve (vector_expression<E1> &e1, const matrix_expression<E2> &e2,
2518 typedef typename E2::orientation_category orientation_category;
2519 inplace_solve (e1, triangular_adaptor<const E2, unit_upper> (e2 ()),
2520 unit_upper_tag (), orientation_category ());
2523 template<class E1, class E2, class C>
2525 typename matrix_vector_solve_traits<E1, E2>::result_type
2526 solve (const vector_expression<E1> &e1,
2527 const matrix_expression<E2> &e2,
2529 typename matrix_vector_solve_traits<E1, E2>::result_type r (e1);
2530 inplace_solve (r, e2, C ());
2534 template<class E1, class E2>
2535 struct matrix_matrix_solve_traits {
2536 typedef typename promote_traits<typename E1::value_type, typename E2::value_type>::promote_type promote_type;
2537 typedef matrix<promote_type> result_type;
2541 // k * n * (n - 1) / 2 + k * n = k * n * (n + 1) / 2 multiplications,
2542 // k * n * (n - 1) / 2 additions
2544 // Dense (proxy) case
2545 template<class E1, class E2>
2547 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2548 lower_tag, dense_proxy_tag) {
2549 typedef typename E2::size_type size_type;
2550 typedef typename E2::value_type value_type;
2552 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2553 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size1 (), bad_size ());
2554 size_type size1 = e2 ().size1 ();
2555 size_type size2 = e2 ().size2 ();
2556 for (size_type n = 0; n < size1; ++ n) {
2557 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2558 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2560 if (e1 () (n, n) == value_type/*zero*/())
2561 singular ().raise ();
2563 for (size_type l = 0; l < size2; ++ l) {
2564 value_type t = e2 () (n, l) /= e1 () (n, n);
2565 if (t != value_type/*zero*/()) {
2566 for (size_type m = n + 1; m < size1; ++ m)
2567 e2 () (m, l) -= e1 () (m, n) * t;
2572 // Packed (proxy) case
2573 template<class E1, class E2>
2575 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2576 lower_tag, packed_proxy_tag) {
2577 typedef typename E2::size_type size_type;
2578 typedef typename E2::difference_type difference_type;
2579 typedef typename E2::value_type value_type;
2581 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2582 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size1 (), bad_size ());
2583 size_type size1 = e2 ().size1 ();
2584 size_type size2 = e2 ().size2 ();
2585 for (size_type n = 0; n < size1; ++ n) {
2586 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2587 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2589 if (e1 () (n, n) == value_type/*zero*/())
2590 singular ().raise ();
2592 for (size_type l = 0; l < size2; ++ l) {
2593 value_type t = e2 () (n, l) /= e1 () (n, n);
2594 if (t != value_type/*zero*/()) {
2595 typename E1::const_iterator1 it1e1 (e1 ().find1 (1, n + 1, n));
2596 typename E1::const_iterator1 it1e1_end (e1 ().find1 (1, e1 ().size1 (), n));
2597 difference_type m (it1e1_end - it1e1);
2599 e2 () (it1e1.index1 (), l) -= *it1e1 * t, ++ it1e1;
2604 // Sparse (proxy) case
2605 template<class E1, class E2>
2607 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2608 lower_tag, unknown_storage_tag) {
2609 typedef typename E2::size_type size_type;
2610 typedef typename E2::value_type value_type;
2612 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2613 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size1 (), bad_size ());
2614 size_type size1 = e2 ().size1 ();
2615 size_type size2 = e2 ().size2 ();
2616 for (size_type n = 0; n < size1; ++ n) {
2617 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2618 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2620 if (e1 () (n, n) == value_type/*zero*/())
2621 singular ().raise ();
2623 for (size_type l = 0; l < size2; ++ l) {
2624 value_type t = e2 () (n, l) /= e1 () (n, n);
2625 if (t != value_type/*zero*/()) {
2626 typename E1::const_iterator1 it1e1 (e1 ().find1 (1, n + 1, n));
2627 typename E1::const_iterator1 it1e1_end (e1 ().find1 (1, e1 ().size1 (), n));
2628 while (it1e1 != it1e1_end)
2629 e2 () (it1e1.index1 (), l) -= *it1e1 * t, ++ it1e1;
2635 template<class E1, class E2>
2637 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2639 typedef typename E1::storage_category dispatch_category;
2640 inplace_solve (e1, e2,
2641 lower_tag (), dispatch_category ());
2643 template<class E1, class E2>
2645 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2647 typedef typename E1::storage_category dispatch_category;
2648 inplace_solve (triangular_adaptor<const E1, unit_lower> (e1 ()), e2,
2649 unit_lower_tag (), dispatch_category ());
2652 // Dense (proxy) case
2653 template<class E1, class E2>
2655 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2656 upper_tag, dense_proxy_tag) {
2657 typedef typename E2::size_type size_type;
2658 typedef typename E2::difference_type difference_type;
2659 typedef typename E2::value_type value_type;
2661 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2662 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size1 (), bad_size ());
2663 size_type size1 = e2 ().size1 ();
2664 size_type size2 = e2 ().size2 ();
2665 for (difference_type n = size1 - 1; n >= 0; -- n) {
2666 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2667 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2669 if (e1 () (n, n) == value_type/*zero*/())
2670 singular ().raise ();
2672 for (difference_type l = size2 - 1; l >= 0; -- l) {
2673 value_type t = e2 () (n, l) /= e1 () (n, n);
2674 if (t != value_type/*zero*/()) {
2675 for (difference_type m = n - 1; m >= 0; -- m)
2676 e2 () (m, l) -= e1 () (m, n) * t;
2681 // Packed (proxy) case
2682 template<class E1, class E2>
2684 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2685 upper_tag, packed_proxy_tag) {
2686 typedef typename E2::size_type size_type;
2687 typedef typename E2::difference_type difference_type;
2688 typedef typename E2::value_type value_type;
2690 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2691 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size1 (), bad_size ());
2692 size_type size1 = e2 ().size1 ();
2693 size_type size2 = e2 ().size2 ();
2694 for (difference_type n = size1 - 1; n >= 0; -- n) {
2695 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2696 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2698 if (e1 () (n, n) == value_type/*zero*/())
2699 singular ().raise ();
2701 for (difference_type l = size2 - 1; l >= 0; -- l) {
2702 value_type t = e2 () (n, l) /= e1 () (n, n);
2703 if (t != value_type/*zero*/()) {
2704 typename E1::const_reverse_iterator1 it1e1 (e1 ().find1 (1, n, n));
2705 typename E1::const_reverse_iterator1 it1e1_rend (e1 ().find1 (1, 0, n));
2706 difference_type m (it1e1_rend - it1e1);
2708 e2 () (it1e1.index1 (), l) -= *it1e1 * t, ++ it1e1;
2713 // Sparse (proxy) case
2714 template<class E1, class E2>
2716 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2717 upper_tag, unknown_storage_tag) {
2718 typedef typename E2::size_type size_type;
2719 typedef typename E2::difference_type difference_type;
2720 typedef typename E2::value_type value_type;
2722 BOOST_UBLAS_CHECK (e1 ().size1 () == e1 ().size2 (), bad_size ());
2723 BOOST_UBLAS_CHECK (e1 ().size2 () == e2 ().size1 (), bad_size ());
2724 size_type size1 = e2 ().size1 ();
2725 size_type size2 = e2 ().size2 ();
2726 for (difference_type n = size1 - 1; n >= 0; -- n) {
2727 #ifndef BOOST_UBLAS_SINGULAR_CHECK
2728 BOOST_UBLAS_CHECK (e1 () (n, n) != value_type/*zero*/(), singular ());
2730 if (e1 () (n, n) == value_type/*zero*/())
2731 singular ().raise ();
2733 for (difference_type l = size2 - 1; l >= 0; -- l) {
2734 value_type t = e2 () (n, l) /= e1 () (n, n);
2735 if (t != value_type/*zero*/()) {
2736 typename E1::const_reverse_iterator1 it1e1 (e1 ().find1 (1, n, n));
2737 typename E1::const_reverse_iterator1 it1e1_rend (e1 ().find1 (1, 0, n));
2738 while (it1e1 != it1e1_rend)
2739 e2 () (it1e1.index1 (), l) -= *it1e1 * t, ++ it1e1;
2745 template<class E1, class E2>
2747 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2749 typedef typename E1::storage_category dispatch_category;
2750 inplace_solve (e1, e2,
2751 upper_tag (), dispatch_category ());
2753 template<class E1, class E2>
2755 void inplace_solve (const matrix_expression<E1> &e1, matrix_expression<E2> &e2,
2757 typedef typename E1::storage_category dispatch_category;
2758 inplace_solve (triangular_adaptor<const E1, unit_upper> (e1 ()), e2,
2759 unit_upper_tag (), dispatch_category ());
2762 template<class E1, class E2, class C>
2764 typename matrix_matrix_solve_traits<E1, E2>::result_type
2765 solve (const matrix_expression<E1> &e1,
2766 const matrix_expression<E2> &e2,
2768 typename matrix_matrix_solve_traits<E1, E2>::result_type r (e2);
2769 inplace_solve (e1, r, C ());