2 // Copyright (c) 2000-2009
3 // Joerg Walter, Mathias Koch, Gunter Winkler
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_FUNCTIONAL_
14 #define _BOOST_UBLAS_FUNCTIONAL_
18 #include <boost/core/ignore_unused.hpp>
20 #include <boost/numeric/ublas/traits.hpp>
21 #ifdef BOOST_UBLAS_USE_DUFF_DEVICE
22 #include <boost/numeric/ublas/detail/duff.hpp>
24 #ifdef BOOST_UBLAS_USE_SIMD
25 #include <boost/numeric/ublas/detail/raw.hpp>
27 namespace boost { namespace numeric { namespace ublas { namespace raw {
30 #ifdef BOOST_UBLAS_HAVE_BINDINGS
31 #include <boost/numeric/bindings/traits/std_vector.hpp>
32 #include <boost/numeric/bindings/traits/ublas_vector.hpp>
33 #include <boost/numeric/bindings/traits/ublas_matrix.hpp>
34 #include <boost/numeric/bindings/atlas/cblas.hpp>
37 #include <boost/numeric/ublas/detail/definitions.hpp>
41 namespace boost { namespace numeric { namespace ublas {
47 struct scalar_unary_functor {
49 typedef typename type_traits<T>::const_reference argument_type;
50 typedef typename type_traits<T>::value_type result_type;
54 struct scalar_identity:
55 public scalar_unary_functor<T> {
56 typedef typename scalar_unary_functor<T>::argument_type argument_type;
57 typedef typename scalar_unary_functor<T>::result_type result_type;
59 static BOOST_UBLAS_INLINE
60 result_type apply (argument_type t) {
66 public scalar_unary_functor<T> {
67 typedef typename scalar_unary_functor<T>::argument_type argument_type;
68 typedef typename scalar_unary_functor<T>::result_type result_type;
70 static BOOST_UBLAS_INLINE
71 result_type apply (argument_type t) {
77 public scalar_unary_functor<T> {
78 typedef typename scalar_unary_functor<T>::value_type value_type;
79 typedef typename scalar_unary_functor<T>::argument_type argument_type;
80 typedef typename scalar_unary_functor<T>::result_type result_type;
82 static BOOST_UBLAS_INLINE
83 result_type apply (argument_type t) {
84 return type_traits<value_type>::conj (t);
88 // Unary returning real
90 struct scalar_real_unary_functor {
92 typedef typename type_traits<T>::const_reference argument_type;
93 typedef typename type_traits<T>::real_type result_type;
98 public scalar_real_unary_functor<T> {
99 typedef typename scalar_real_unary_functor<T>::value_type value_type;
100 typedef typename scalar_real_unary_functor<T>::argument_type argument_type;
101 typedef typename scalar_real_unary_functor<T>::result_type result_type;
103 static BOOST_UBLAS_INLINE
104 result_type apply (argument_type t) {
105 return type_traits<value_type>::real (t);
110 public scalar_real_unary_functor<T> {
111 typedef typename scalar_real_unary_functor<T>::value_type value_type;
112 typedef typename scalar_real_unary_functor<T>::argument_type argument_type;
113 typedef typename scalar_real_unary_functor<T>::result_type result_type;
115 static BOOST_UBLAS_INLINE
116 result_type apply (argument_type t) {
117 return type_traits<value_type>::imag (t);
122 template<class T1, class T2>
123 struct scalar_binary_functor {
124 typedef typename type_traits<T1>::const_reference argument1_type;
125 typedef typename type_traits<T2>::const_reference argument2_type;
126 typedef typename promote_traits<T1, T2>::promote_type result_type;
129 template<class T1, class T2>
131 public scalar_binary_functor<T1, T2> {
132 typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
133 typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
134 typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
136 static BOOST_UBLAS_INLINE
137 result_type apply (argument1_type t1, argument2_type t2) {
141 template<class T1, class T2>
143 public scalar_binary_functor<T1, T2> {
144 typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
145 typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
146 typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
148 static BOOST_UBLAS_INLINE
149 result_type apply (argument1_type t1, argument2_type t2) {
153 template<class T1, class T2>
154 struct scalar_multiplies:
155 public scalar_binary_functor<T1, T2> {
156 typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
157 typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
158 typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
160 static BOOST_UBLAS_INLINE
161 result_type apply (argument1_type t1, argument2_type t2) {
165 template<class T1, class T2>
166 struct scalar_divides:
167 public scalar_binary_functor<T1, T2> {
168 typedef typename scalar_binary_functor<T1, T2>::argument1_type argument1_type;
169 typedef typename scalar_binary_functor<T1, T2>::argument2_type argument2_type;
170 typedef typename scalar_binary_functor<T1, T2>::result_type result_type;
172 static BOOST_UBLAS_INLINE
173 result_type apply (argument1_type t1, argument2_type t2) {
178 template<class T1, class T2>
179 struct scalar_binary_assign_functor {
180 // ISSUE Remove reference to avoid reference to reference problems
181 typedef typename type_traits<typename boost::remove_reference<T1>::type>::reference argument1_type;
182 typedef typename type_traits<T2>::const_reference argument2_type;
185 struct assign_tag {};
186 struct computed_assign_tag {};
188 template<class T1, class T2>
189 struct scalar_assign:
190 public scalar_binary_assign_functor<T1, T2> {
191 typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
192 typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
193 #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
194 static const bool computed ;
196 static const bool computed = false ;
199 static BOOST_UBLAS_INLINE
200 void apply (argument1_type t1, argument2_type t2) {
204 template<class U1, class U2>
206 typedef scalar_assign<U1, U2> other;
210 #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
211 template<class T1, class T2>
212 const bool scalar_assign<T1,T2>::computed = false;
215 template<class T1, class T2>
216 struct scalar_plus_assign:
217 public scalar_binary_assign_functor<T1, T2> {
218 typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
219 typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
220 #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
221 static const bool computed ;
223 static const bool computed = true ;
226 static BOOST_UBLAS_INLINE
227 void apply (argument1_type t1, argument2_type t2) {
231 template<class U1, class U2>
233 typedef scalar_plus_assign<U1, U2> other;
237 #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
238 template<class T1, class T2>
239 const bool scalar_plus_assign<T1,T2>::computed = true;
242 template<class T1, class T2>
243 struct scalar_minus_assign:
244 public scalar_binary_assign_functor<T1, T2> {
245 typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
246 typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
247 #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
248 static const bool computed ;
250 static const bool computed = true ;
253 static BOOST_UBLAS_INLINE
254 void apply (argument1_type t1, argument2_type t2) {
258 template<class U1, class U2>
260 typedef scalar_minus_assign<U1, U2> other;
264 #if BOOST_WORKAROUND( __IBMCPP__, <=600 )
265 template<class T1, class T2>
266 const bool scalar_minus_assign<T1,T2>::computed = true;
269 template<class T1, class T2>
270 struct scalar_multiplies_assign:
271 public scalar_binary_assign_functor<T1, T2> {
272 typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
273 typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
274 static const bool computed = true;
276 static BOOST_UBLAS_INLINE
277 void apply (argument1_type t1, argument2_type t2) {
281 template<class U1, class U2>
283 typedef scalar_multiplies_assign<U1, U2> other;
286 template<class T1, class T2>
287 struct scalar_divides_assign:
288 public scalar_binary_assign_functor<T1, T2> {
289 typedef typename scalar_binary_assign_functor<T1, T2>::argument1_type argument1_type;
290 typedef typename scalar_binary_assign_functor<T1, T2>::argument2_type argument2_type;
291 static const bool computed ;
293 static BOOST_UBLAS_INLINE
294 void apply (argument1_type t1, argument2_type t2) {
298 template<class U1, class U2>
300 typedef scalar_divides_assign<U1, U2> other;
303 template<class T1, class T2>
304 const bool scalar_divides_assign<T1,T2>::computed = true;
306 template<class T1, class T2>
307 struct scalar_binary_swap_functor {
308 typedef typename type_traits<typename boost::remove_reference<T1>::type>::reference argument1_type;
309 typedef typename type_traits<typename boost::remove_reference<T2>::type>::reference argument2_type;
312 template<class T1, class T2>
314 public scalar_binary_swap_functor<T1, T2> {
315 typedef typename scalar_binary_swap_functor<T1, T2>::argument1_type argument1_type;
316 typedef typename scalar_binary_swap_functor<T1, T2>::argument2_type argument2_type;
318 static BOOST_UBLAS_INLINE
319 void apply (argument1_type t1, argument2_type t2) {
323 template<class U1, class U2>
325 typedef scalar_swap<U1, U2> other;
331 // Unary returning scalar
333 struct vector_scalar_unary_functor {
334 typedef typename V::value_type value_type;
335 typedef typename V::value_type result_type;
340 public vector_scalar_unary_functor<V> {
341 typedef typename vector_scalar_unary_functor<V>::value_type value_type;
342 typedef typename vector_scalar_unary_functor<V>::result_type result_type;
345 static BOOST_UBLAS_INLINE
346 result_type apply (const vector_expression<E> &e) {
347 result_type t = result_type (0);
348 typedef typename E::size_type vector_size_type;
349 vector_size_type size (e ().size ());
350 for (vector_size_type i = 0; i < size; ++ i)
355 template<class D, class I>
356 static BOOST_UBLAS_INLINE
357 result_type apply (D size, I it) {
358 result_type t = result_type (0);
365 static BOOST_UBLAS_INLINE
366 result_type apply (I it, const I &it_end) {
367 result_type t = result_type (0);
374 // Unary returning real scalar
376 struct vector_scalar_real_unary_functor {
377 typedef typename V::value_type value_type;
378 typedef typename type_traits<value_type>::real_type real_type;
379 typedef real_type result_type;
383 struct vector_norm_1:
384 public vector_scalar_real_unary_functor<V> {
385 typedef typename vector_scalar_real_unary_functor<V>::value_type value_type;
386 typedef typename vector_scalar_real_unary_functor<V>::real_type real_type;
387 typedef typename vector_scalar_real_unary_functor<V>::result_type result_type;
390 static BOOST_UBLAS_INLINE
391 result_type apply (const vector_expression<E> &e) {
392 real_type t = real_type ();
393 typedef typename E::size_type vector_size_type;
394 vector_size_type size (e ().size ());
395 for (vector_size_type i = 0; i < size; ++ i) {
396 real_type u (type_traits<value_type>::type_abs (e () (i)));
402 template<class D, class I>
403 static BOOST_UBLAS_INLINE
404 result_type apply (D size, I it) {
405 real_type t = real_type ();
406 while (-- size >= 0) {
407 real_type u (type_traits<value_type>::norm_1 (*it));
415 static BOOST_UBLAS_INLINE
416 result_type apply (I it, const I &it_end) {
417 real_type t = real_type ();
418 while (it != it_end) {
419 real_type u (type_traits<value_type>::norm_1 (*it));
427 struct vector_norm_2:
428 public vector_scalar_real_unary_functor<V> {
429 typedef typename vector_scalar_real_unary_functor<V>::value_type value_type;
430 typedef typename vector_scalar_real_unary_functor<V>::real_type real_type;
431 typedef typename vector_scalar_real_unary_functor<V>::result_type result_type;
434 static BOOST_UBLAS_INLINE
435 result_type apply (const vector_expression<E> &e) {
436 #ifndef BOOST_UBLAS_SCALED_NORM
437 real_type t = real_type ();
438 typedef typename E::size_type vector_size_type;
439 vector_size_type size (e ().size ());
440 for (vector_size_type i = 0; i < size; ++ i) {
441 real_type u (type_traits<value_type>::norm_2 (e () (i)));
444 return type_traits<real_type>::type_sqrt (t);
446 real_type scale = real_type ();
447 real_type sum_squares (1);
448 size_type size (e ().size ());
449 for (size_type i = 0; i < size; ++ i) {
450 real_type u (type_traits<value_type>::norm_2 (e () (i)));
451 if ( real_type () /* zero */ == u ) continue;
453 real_type v (scale / u);
454 sum_squares = sum_squares * v * v + real_type (1);
457 real_type v (u / scale);
458 sum_squares += v * v;
461 return scale * type_traits<real_type>::type_sqrt (sum_squares);
465 template<class D, class I>
466 static BOOST_UBLAS_INLINE
467 result_type apply (D size, I it) {
468 #ifndef BOOST_UBLAS_SCALED_NORM
469 real_type t = real_type ();
470 while (-- size >= 0) {
471 real_type u (type_traits<value_type>::norm_2 (*it));
475 return type_traits<real_type>::type_sqrt (t);
477 real_type scale = real_type ();
478 real_type sum_squares (1);
479 while (-- size >= 0) {
480 real_type u (type_traits<value_type>::norm_2 (*it));
482 real_type v (scale / u);
483 sum_squares = sum_squares * v * v + real_type (1);
486 real_type v (u / scale);
487 sum_squares += v * v;
491 return scale * type_traits<real_type>::type_sqrt (sum_squares);
496 static BOOST_UBLAS_INLINE
497 result_type apply (I it, const I &it_end) {
498 #ifndef BOOST_UBLAS_SCALED_NORM
499 real_type t = real_type ();
500 while (it != it_end) {
501 real_type u (type_traits<value_type>::norm_2 (*it));
505 return type_traits<real_type>::type_sqrt (t);
507 real_type scale = real_type ();
508 real_type sum_squares (1);
509 while (it != it_end) {
510 real_type u (type_traits<value_type>::norm_2 (*it));
512 real_type v (scale / u);
513 sum_squares = sum_squares * v * v + real_type (1);
516 real_type v (u / scale);
517 sum_squares += v * v;
521 return scale * type_traits<real_type>::type_sqrt (sum_squares);
526 struct vector_norm_inf:
527 public vector_scalar_real_unary_functor<V> {
528 typedef typename vector_scalar_real_unary_functor<V>::value_type value_type;
529 typedef typename vector_scalar_real_unary_functor<V>::real_type real_type;
530 typedef typename vector_scalar_real_unary_functor<V>::result_type result_type;
533 static BOOST_UBLAS_INLINE
534 result_type apply (const vector_expression<E> &e) {
535 real_type t = real_type ();
536 typedef typename E::size_type vector_size_type;
537 vector_size_type size (e ().size ());
538 for (vector_size_type i = 0; i < size; ++ i) {
539 real_type u (type_traits<value_type>::norm_inf (e () (i)));
546 template<class D, class I>
547 static BOOST_UBLAS_INLINE
548 result_type apply (D size, I it) {
549 real_type t = real_type ();
550 while (-- size >= 0) {
551 real_type u (type_traits<value_type>::norm_inf (*it));
560 static BOOST_UBLAS_INLINE
561 result_type apply (I it, const I &it_end) {
562 real_type t = real_type ();
563 while (it != it_end) {
564 real_type u (type_traits<value_type>::norm_inf (*it));
573 // Unary returning index
575 struct vector_scalar_index_unary_functor {
576 typedef typename V::value_type value_type;
577 typedef typename type_traits<value_type>::real_type real_type;
578 typedef typename V::size_type result_type;
582 struct vector_index_norm_inf:
583 public vector_scalar_index_unary_functor<V> {
584 typedef typename vector_scalar_index_unary_functor<V>::value_type value_type;
585 typedef typename vector_scalar_index_unary_functor<V>::real_type real_type;
586 typedef typename vector_scalar_index_unary_functor<V>::result_type result_type;
589 static BOOST_UBLAS_INLINE
590 result_type apply (const vector_expression<E> &e) {
591 // ISSUE For CBLAS compatibility return 0 index in empty case
592 result_type i_norm_inf (0);
593 real_type t = real_type ();
594 typedef typename E::size_type vector_size_type;
595 vector_size_type size (e ().size ());
596 for (vector_size_type i = 0; i < size; ++ i) {
597 real_type u (type_traits<value_type>::norm_inf (e () (i)));
606 template<class D, class I>
607 static BOOST_UBLAS_INLINE
608 result_type apply (D size, I it) {
609 // ISSUE For CBLAS compatibility return 0 index in empty case
610 result_type i_norm_inf (0);
611 real_type t = real_type ();
612 while (-- size >= 0) {
613 real_type u (type_traits<value_type>::norm_inf (*it));
615 i_norm_inf = it.index ();
624 static BOOST_UBLAS_INLINE
625 result_type apply (I it, const I &it_end) {
626 // ISSUE For CBLAS compatibility return 0 index in empty case
627 result_type i_norm_inf (0);
628 real_type t = real_type ();
629 while (it != it_end) {
630 real_type u (type_traits<value_type>::norm_inf (*it));
632 i_norm_inf = it.index ();
641 // Binary returning scalar
642 template<class V1, class V2, class TV>
643 struct vector_scalar_binary_functor {
644 typedef TV value_type;
645 typedef TV result_type;
648 template<class V1, class V2, class TV>
649 struct vector_inner_prod:
650 public vector_scalar_binary_functor<V1, V2, TV> {
651 typedef typename vector_scalar_binary_functor<V1, V2, TV>::value_type value_type;
652 typedef typename vector_scalar_binary_functor<V1, V2, TV>::result_type result_type;
654 template<class C1, class C2>
655 static BOOST_UBLAS_INLINE
656 result_type apply (const vector_container<C1> &c1,
657 const vector_container<C2> &c2) {
658 #ifdef BOOST_UBLAS_USE_SIMD
660 typedef typename C1::size_type vector_size_type;
661 vector_size_type size (BOOST_UBLAS_SAME (c1 ().size (), c2 ().size ()));
662 const typename V1::value_type *data1 = data_const (c1 ());
663 const typename V1::value_type *data2 = data_const (c2 ());
664 vector_size_type s1 = stride (c1 ());
665 vector_size_type s2 = stride (c2 ());
666 result_type t = result_type (0);
667 if (s1 == 1 && s2 == 1) {
668 for (vector_size_type i = 0; i < size; ++ i)
669 t += data1 [i] * data2 [i];
670 } else if (s2 == 1) {
671 for (vector_size_type i = 0, i1 = 0; i < size; ++ i, i1 += s1)
672 t += data1 [i1] * data2 [i];
673 } else if (s1 == 1) {
674 for (vector_size_type i = 0, i2 = 0; i < size; ++ i, i2 += s2)
675 t += data1 [i] * data2 [i2];
677 for (vector_size_type i = 0, i1 = 0, i2 = 0; i < size; ++ i, i1 += s1, i2 += s2)
678 t += data1 [i1] * data2 [i2];
681 #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
682 return boost::numeric::bindings::atlas::dot (c1 (), c2 ());
684 return apply (static_cast<const vector_expression<C1> > (c1), static_cast<const vector_expression<C2> > (c2));
687 template<class E1, class E2>
688 static BOOST_UBLAS_INLINE
689 result_type apply (const vector_expression<E1> &e1,
690 const vector_expression<E2> &e2) {
691 typedef typename E1::size_type vector_size_type;
692 vector_size_type size (BOOST_UBLAS_SAME (e1 ().size (), e2 ().size ()));
693 result_type t = result_type (0);
694 #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
695 for (vector_size_type i = 0; i < size; ++ i)
696 t += e1 () (i) * e2 () (i);
698 vector_size_type i (0);
699 DD (size, 4, r, (t += e1 () (i) * e2 () (i), ++ i));
704 template<class D, class I1, class I2>
705 static BOOST_UBLAS_INLINE
706 result_type apply (D size, I1 it1, I2 it2) {
707 result_type t = result_type (0);
708 #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
710 t += *it1 * *it2, ++ it1, ++ it2;
712 DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
717 template<class I1, class I2>
718 static BOOST_UBLAS_INLINE
719 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
720 result_type t = result_type (0);
721 typedef typename I1::difference_type vector_difference_type;
722 vector_difference_type it1_size (it1_end - it1);
723 vector_difference_type it2_size (it2_end - it2);
724 vector_difference_type diff (0);
725 if (it1_size > 0 && it2_size > 0)
726 diff = it2.index () - it1.index ();
728 vector_difference_type size = (std::min) (diff, it1_size);
734 size = (std::min) (- diff, it2_size);
741 vector_difference_type size ((std::min) (it1_size, it2_size));
743 t += *it1 * *it2, ++ it1, ++ it2;
747 template<class I1, class I2>
748 static BOOST_UBLAS_INLINE
749 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, sparse_bidirectional_iterator_tag) {
750 result_type t = result_type (0);
751 if (it1 != it1_end && it2 != it2_end) {
753 if (it1.index () == it2.index ()) {
754 t += *it1 * *it2, ++ it1, ++ it2;
755 if (it1 == it1_end || it2 == it2_end)
757 } else if (it1.index () < it2.index ()) {
758 increment (it1, it1_end, it2.index () - it1.index ());
761 } else if (it1.index () > it2.index ()) {
762 increment (it2, it2_end, it1.index () - it2.index ());
774 // Binary returning vector
775 template<class M1, class M2, class TV>
776 struct matrix_vector_binary_functor {
777 typedef typename M1::size_type size_type;
778 typedef typename M1::difference_type difference_type;
779 typedef TV value_type;
780 typedef TV result_type;
783 template<class M1, class M2, class TV>
784 struct matrix_vector_prod1:
785 public matrix_vector_binary_functor<M1, M2, TV> {
786 typedef typename matrix_vector_binary_functor<M1, M2, TV>::size_type size_type;
787 typedef typename matrix_vector_binary_functor<M1, M2, TV>::difference_type difference_type;
788 typedef typename matrix_vector_binary_functor<M1, M2, TV>::value_type value_type;
789 typedef typename matrix_vector_binary_functor<M1, M2, TV>::result_type result_type;
791 template<class C1, class C2>
792 static BOOST_UBLAS_INLINE
793 result_type apply (const matrix_container<C1> &c1,
794 const vector_container<C2> &c2,
796 #ifdef BOOST_UBLAS_USE_SIMD
798 size_type size = BOOST_UBLAS_SAME (c1 ().size2 (), c2 ().size ());
799 const typename M1::value_type *data1 = data_const (c1 ()) + i * stride1 (c1 ());
800 const typename M2::value_type *data2 = data_const (c2 ());
801 size_type s1 = stride2 (c1 ());
802 size_type s2 = stride (c2 ());
803 result_type t = result_type (0);
804 if (s1 == 1 && s2 == 1) {
805 for (size_type j = 0; j < size; ++ j)
806 t += data1 [j] * data2 [j];
807 } else if (s2 == 1) {
808 for (size_type j = 0, j1 = 0; j < size; ++ j, j1 += s1)
809 t += data1 [j1] * data2 [j];
810 } else if (s1 == 1) {
811 for (size_type j = 0, j2 = 0; j < size; ++ j, j2 += s2)
812 t += data1 [j] * data2 [j2];
814 for (size_type j = 0, j1 = 0, j2 = 0; j < size; ++ j, j1 += s1, j2 += s2)
815 t += data1 [j1] * data2 [j2];
818 #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
819 return boost::numeric::bindings::atlas::dot (c1 ().row (i), c2 ());
821 return apply (static_cast<const matrix_expression<C1> > (c1), static_cast<const vector_expression<C2> > (c2, i));
824 template<class E1, class E2>
825 static BOOST_UBLAS_INLINE
826 result_type apply (const matrix_expression<E1> &e1,
827 const vector_expression<E2> &e2,
829 size_type size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size ());
830 result_type t = result_type (0);
831 #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
832 for (size_type j = 0; j < size; ++ j)
833 t += e1 () (i, j) * e2 () (j);
836 DD (size, 4, r, (t += e1 () (i, j) * e2 () (j), ++ j));
841 template<class I1, class I2>
842 static BOOST_UBLAS_INLINE
843 result_type apply (difference_type size, I1 it1, I2 it2) {
844 result_type t = result_type (0);
845 #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
847 t += *it1 * *it2, ++ it1, ++ it2;
849 DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
854 template<class I1, class I2>
855 static BOOST_UBLAS_INLINE
856 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
857 result_type t = result_type (0);
858 difference_type it1_size (it1_end - it1);
859 difference_type it2_size (it2_end - it2);
860 difference_type diff (0);
861 if (it1_size > 0 && it2_size > 0)
862 diff = it2.index () - it1.index2 ();
864 difference_type size = (std::min) (diff, it1_size);
870 size = (std::min) (- diff, it2_size);
877 difference_type size ((std::min) (it1_size, it2_size));
879 t += *it1 * *it2, ++ it1, ++ it2;
883 template<class I1, class I2>
884 static BOOST_UBLAS_INLINE
885 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
886 sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag) {
887 result_type t = result_type (0);
888 if (it1 != it1_end && it2 != it2_end) {
889 size_type it1_index = it1.index2 (), it2_index = it2.index ();
891 difference_type compare = it1_index - it2_index;
893 t += *it1 * *it2, ++ it1, ++ it2;
894 if (it1 != it1_end && it2 != it2_end) {
895 it1_index = it1.index2 ();
896 it2_index = it2.index ();
899 } else if (compare < 0) {
900 increment (it1, it1_end, - compare);
902 it1_index = it1.index2 ();
905 } else if (compare > 0) {
906 increment (it2, it2_end, compare);
908 it2_index = it2.index ();
916 // Sparse packed case
917 template<class I1, class I2>
918 static BOOST_UBLAS_INLINE
919 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &/* it2_end */,
920 sparse_bidirectional_iterator_tag, packed_random_access_iterator_tag) {
921 result_type t = result_type (0);
922 while (it1 != it1_end) {
923 t += *it1 * it2 () (it1.index2 ());
928 // Packed sparse case
929 template<class I1, class I2>
930 static BOOST_UBLAS_INLINE
931 result_type apply (I1 it1, const I1 &/* it1_end */, I2 it2, const I2 &it2_end,
932 packed_random_access_iterator_tag, sparse_bidirectional_iterator_tag) {
933 result_type t = result_type (0);
934 while (it2 != it2_end) {
935 t += it1 () (it1.index1 (), it2.index ()) * *it2;
940 // Another dispatcher
941 template<class I1, class I2>
942 static BOOST_UBLAS_INLINE
943 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
944 sparse_bidirectional_iterator_tag) {
945 typedef typename I1::iterator_category iterator1_category;
946 typedef typename I2::iterator_category iterator2_category;
947 return apply (it1, it1_end, it2, it2_end, iterator1_category (), iterator2_category ());
951 template<class M1, class M2, class TV>
952 struct matrix_vector_prod2:
953 public matrix_vector_binary_functor<M1, M2, TV> {
954 typedef typename matrix_vector_binary_functor<M1, M2, TV>::size_type size_type;
955 typedef typename matrix_vector_binary_functor<M1, M2, TV>::difference_type difference_type;
956 typedef typename matrix_vector_binary_functor<M1, M2, TV>::value_type value_type;
957 typedef typename matrix_vector_binary_functor<M1, M2, TV>::result_type result_type;
959 template<class C1, class C2>
960 static BOOST_UBLAS_INLINE
961 result_type apply (const vector_container<C1> &c1,
962 const matrix_container<C2> &c2,
964 #ifdef BOOST_UBLAS_USE_SIMD
966 size_type size = BOOST_UBLAS_SAME (c1 ().size (), c2 ().size1 ());
967 const typename M1::value_type *data1 = data_const (c1 ());
968 const typename M2::value_type *data2 = data_const (c2 ()) + i * stride2 (c2 ());
969 size_type s1 = stride (c1 ());
970 size_type s2 = stride1 (c2 ());
971 result_type t = result_type (0);
972 if (s1 == 1 && s2 == 1) {
973 for (size_type j = 0; j < size; ++ j)
974 t += data1 [j] * data2 [j];
975 } else if (s2 == 1) {
976 for (size_type j = 0, j1 = 0; j < size; ++ j, j1 += s1)
977 t += data1 [j1] * data2 [j];
978 } else if (s1 == 1) {
979 for (size_type j = 0, j2 = 0; j < size; ++ j, j2 += s2)
980 t += data1 [j] * data2 [j2];
982 for (size_type j = 0, j1 = 0, j2 = 0; j < size; ++ j, j1 += s1, j2 += s2)
983 t += data1 [j1] * data2 [j2];
986 #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
987 return boost::numeric::bindings::atlas::dot (c1 (), c2 ().column (i));
989 return apply (static_cast<const vector_expression<C1> > (c1), static_cast<const matrix_expression<C2> > (c2, i));
992 template<class E1, class E2>
993 static BOOST_UBLAS_INLINE
994 result_type apply (const vector_expression<E1> &e1,
995 const matrix_expression<E2> &e2,
997 size_type size = BOOST_UBLAS_SAME (e1 ().size (), e2 ().size1 ());
998 result_type t = result_type (0);
999 #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
1000 for (size_type j = 0; j < size; ++ j)
1001 t += e1 () (j) * e2 () (j, i);
1004 DD (size, 4, r, (t += e1 () (j) * e2 () (j, i), ++ j));
1009 template<class I1, class I2>
1010 static BOOST_UBLAS_INLINE
1011 result_type apply (difference_type size, I1 it1, I2 it2) {
1012 result_type t = result_type (0);
1013 #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
1014 while (-- size >= 0)
1015 t += *it1 * *it2, ++ it1, ++ it2;
1017 DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
1022 template<class I1, class I2>
1023 static BOOST_UBLAS_INLINE
1024 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end) {
1025 result_type t = result_type (0);
1026 difference_type it1_size (it1_end - it1);
1027 difference_type it2_size (it2_end - it2);
1028 difference_type diff (0);
1029 if (it1_size > 0 && it2_size > 0)
1030 diff = it2.index1 () - it1.index ();
1032 difference_type size = (std::min) (diff, it1_size);
1038 size = (std::min) (- diff, it2_size);
1045 difference_type size ((std::min) (it1_size, it2_size));
1046 while (-- size >= 0)
1047 t += *it1 * *it2, ++ it1, ++ it2;
1051 template<class I1, class I2>
1052 static BOOST_UBLAS_INLINE
1053 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
1054 sparse_bidirectional_iterator_tag, sparse_bidirectional_iterator_tag) {
1055 result_type t = result_type (0);
1056 if (it1 != it1_end && it2 != it2_end) {
1057 size_type it1_index = it1.index (), it2_index = it2.index1 ();
1059 difference_type compare = it1_index - it2_index;
1061 t += *it1 * *it2, ++ it1, ++ it2;
1062 if (it1 != it1_end && it2 != it2_end) {
1063 it1_index = it1.index ();
1064 it2_index = it2.index1 ();
1067 } else if (compare < 0) {
1068 increment (it1, it1_end, - compare);
1070 it1_index = it1.index ();
1073 } else if (compare > 0) {
1074 increment (it2, it2_end, compare);
1076 it2_index = it2.index1 ();
1084 // Packed sparse case
1085 template<class I1, class I2>
1086 static BOOST_UBLAS_INLINE
1087 result_type apply (I1 it1, const I1 &/* it1_end */, I2 it2, const I2 &it2_end,
1088 packed_random_access_iterator_tag, sparse_bidirectional_iterator_tag) {
1089 result_type t = result_type (0);
1090 while (it2 != it2_end) {
1091 t += it1 () (it2.index1 ()) * *it2;
1096 // Sparse packed case
1097 template<class I1, class I2>
1098 static BOOST_UBLAS_INLINE
1099 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &/* it2_end */,
1100 sparse_bidirectional_iterator_tag, packed_random_access_iterator_tag) {
1101 result_type t = result_type (0);
1102 while (it1 != it1_end) {
1103 t += *it1 * it2 () (it1.index (), it2.index2 ());
1108 // Another dispatcher
1109 template<class I1, class I2>
1110 static BOOST_UBLAS_INLINE
1111 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end,
1112 sparse_bidirectional_iterator_tag) {
1113 typedef typename I1::iterator_category iterator1_category;
1114 typedef typename I2::iterator_category iterator2_category;
1115 return apply (it1, it1_end, it2, it2_end, iterator1_category (), iterator2_category ());
1119 // Binary returning matrix
1120 template<class M1, class M2, class TV>
1121 struct matrix_matrix_binary_functor {
1122 typedef typename M1::size_type size_type;
1123 typedef typename M1::difference_type difference_type;
1124 typedef TV value_type;
1125 typedef TV result_type;
1128 template<class M1, class M2, class TV>
1129 struct matrix_matrix_prod:
1130 public matrix_matrix_binary_functor<M1, M2, TV> {
1131 typedef typename matrix_matrix_binary_functor<M1, M2, TV>::size_type size_type;
1132 typedef typename matrix_matrix_binary_functor<M1, M2, TV>::difference_type difference_type;
1133 typedef typename matrix_matrix_binary_functor<M1, M2, TV>::value_type value_type;
1134 typedef typename matrix_matrix_binary_functor<M1, M2, TV>::result_type result_type;
1136 template<class C1, class C2>
1137 static BOOST_UBLAS_INLINE
1138 result_type apply (const matrix_container<C1> &c1,
1139 const matrix_container<C2> &c2,
1140 size_type i, size_type j) {
1141 #ifdef BOOST_UBLAS_USE_SIMD
1142 using namespace raw;
1143 size_type size = BOOST_UBLAS_SAME (c1 ().size2 (), c2 ().sizc1 ());
1144 const typename M1::value_type *data1 = data_const (c1 ()) + i * stride1 (c1 ());
1145 const typename M2::value_type *data2 = data_const (c2 ()) + j * stride2 (c2 ());
1146 size_type s1 = stride2 (c1 ());
1147 size_type s2 = stride1 (c2 ());
1148 result_type t = result_type (0);
1149 if (s1 == 1 && s2 == 1) {
1150 for (size_type k = 0; k < size; ++ k)
1151 t += data1 [k] * data2 [k];
1152 } else if (s2 == 1) {
1153 for (size_type k = 0, k1 = 0; k < size; ++ k, k1 += s1)
1154 t += data1 [k1] * data2 [k];
1155 } else if (s1 == 1) {
1156 for (size_type k = 0, k2 = 0; k < size; ++ k, k2 += s2)
1157 t += data1 [k] * data2 [k2];
1159 for (size_type k = 0, k1 = 0, k2 = 0; k < size; ++ k, k1 += s1, k2 += s2)
1160 t += data1 [k1] * data2 [k2];
1163 #elif defined(BOOST_UBLAS_HAVE_BINDINGS)
1164 return boost::numeric::bindings::atlas::dot (c1 ().row (i), c2 ().column (j));
1166 boost::ignore_unused(j);
1167 return apply (static_cast<const matrix_expression<C1> > (c1), static_cast<const matrix_expression<C2> > (c2, i));
1170 template<class E1, class E2>
1171 static BOOST_UBLAS_INLINE
1172 result_type apply (const matrix_expression<E1> &e1,
1173 const matrix_expression<E2> &e2,
1174 size_type i, size_type j) {
1175 size_type size = BOOST_UBLAS_SAME (e1 ().size2 (), e2 ().size1 ());
1176 result_type t = result_type (0);
1177 #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
1178 for (size_type k = 0; k < size; ++ k)
1179 t += e1 () (i, k) * e2 () (k, j);
1182 DD (size, 4, r, (t += e1 () (i, k) * e2 () (k, j), ++ k));
1187 template<class I1, class I2>
1188 static BOOST_UBLAS_INLINE
1189 result_type apply (difference_type size, I1 it1, I2 it2) {
1190 result_type t = result_type (0);
1191 #ifndef BOOST_UBLAS_USE_DUFF_DEVICE
1192 while (-- size >= 0)
1193 t += *it1 * *it2, ++ it1, ++ it2;
1195 DD (size, 4, r, (t += *it1 * *it2, ++ it1, ++ it2));
1200 template<class I1, class I2>
1201 static BOOST_UBLAS_INLINE
1202 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, packed_random_access_iterator_tag) {
1203 result_type t = result_type (0);
1204 difference_type it1_size (it1_end - it1);
1205 difference_type it2_size (it2_end - it2);
1206 difference_type diff (0);
1207 if (it1_size > 0 && it2_size > 0)
1208 diff = it2.index1 () - it1.index2 ();
1210 difference_type size = (std::min) (diff, it1_size);
1216 size = (std::min) (- diff, it2_size);
1223 difference_type size ((std::min) (it1_size, it2_size));
1224 while (-- size >= 0)
1225 t += *it1 * *it2, ++ it1, ++ it2;
1229 template<class I1, class I2>
1230 static BOOST_UBLAS_INLINE
1231 result_type apply (I1 it1, const I1 &it1_end, I2 it2, const I2 &it2_end, sparse_bidirectional_iterator_tag) {
1232 result_type t = result_type (0);
1233 if (it1 != it1_end && it2 != it2_end) {
1234 size_type it1_index = it1.index2 (), it2_index = it2.index1 ();
1236 difference_type compare = difference_type (it1_index - it2_index);
1238 t += *it1 * *it2, ++ it1, ++ it2;
1239 if (it1 != it1_end && it2 != it2_end) {
1240 it1_index = it1.index2 ();
1241 it2_index = it2.index1 ();
1244 } else if (compare < 0) {
1245 increment (it1, it1_end, - compare);
1247 it1_index = it1.index2 ();
1250 } else if (compare > 0) {
1251 increment (it2, it2_end, compare);
1253 it2_index = it2.index1 ();
1263 // Unary returning scalar norm
1265 struct matrix_scalar_real_unary_functor {
1266 typedef typename M::value_type value_type;
1267 typedef typename type_traits<value_type>::real_type real_type;
1268 typedef real_type result_type;
1272 struct matrix_norm_1:
1273 public matrix_scalar_real_unary_functor<M> {
1274 typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
1275 typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
1276 typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
1279 static BOOST_UBLAS_INLINE
1280 result_type apply (const matrix_expression<E> &e) {
1281 real_type t = real_type ();
1282 typedef typename E::size_type matrix_size_type;
1283 matrix_size_type size2 (e ().size2 ());
1284 for (matrix_size_type j = 0; j < size2; ++ j) {
1285 real_type u = real_type ();
1286 matrix_size_type size1 (e ().size1 ());
1287 for (matrix_size_type i = 0; i < size1; ++ i) {
1288 real_type v (type_traits<value_type>::norm_1 (e () (i, j)));
1299 struct matrix_norm_frobenius:
1300 public matrix_scalar_real_unary_functor<M> {
1301 typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
1302 typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
1303 typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
1306 static BOOST_UBLAS_INLINE
1307 result_type apply (const matrix_expression<E> &e) {
1308 real_type t = real_type ();
1309 typedef typename E::size_type matrix_size_type;
1310 matrix_size_type size1 (e ().size1 ());
1311 for (matrix_size_type i = 0; i < size1; ++ i) {
1312 matrix_size_type size2 (e ().size2 ());
1313 for (matrix_size_type j = 0; j < size2; ++ j) {
1314 real_type u (type_traits<value_type>::norm_2 (e () (i, j)));
1318 return type_traits<real_type>::type_sqrt (t);
1323 struct matrix_norm_inf:
1324 public matrix_scalar_real_unary_functor<M> {
1325 typedef typename matrix_scalar_real_unary_functor<M>::value_type value_type;
1326 typedef typename matrix_scalar_real_unary_functor<M>::real_type real_type;
1327 typedef typename matrix_scalar_real_unary_functor<M>::result_type result_type;
1330 static BOOST_UBLAS_INLINE
1331 result_type apply (const matrix_expression<E> &e) {
1332 real_type t = real_type ();
1333 typedef typename E::size_type matrix_size_type;
1334 matrix_size_type size1 (e ().size1 ());
1335 for (matrix_size_type i = 0; i < size1; ++ i) {
1336 real_type u = real_type ();
1337 matrix_size_type size2 (e ().size2 ());
1338 for (matrix_size_type j = 0; j < size2; ++ j) {
1339 real_type v (type_traits<value_type>::norm_inf (e () (i, j)));
1349 // forward declaration
1350 template <class Z, class D> struct basic_column_major;
1352 // This functor defines storage layout and it's properties
1353 // matrix (i,j) -> storage [i * size_i + j]
1354 template <class Z, class D>
1355 struct basic_row_major {
1356 typedef Z size_type;
1357 typedef D difference_type;
1358 typedef row_major_tag orientation_category;
1359 typedef basic_column_major<Z,D> transposed_layout;
1363 size_type storage_size (size_type size_i, size_type size_j) {
1364 // Guard against size_type overflow
1365 BOOST_UBLAS_CHECK (size_j == 0 || size_i <= (std::numeric_limits<size_type>::max) () / size_j, bad_size ());
1366 return size_i * size_j;
1369 // Indexing conversion to storage element
1372 size_type element (size_type i, size_type size_i, size_type j, size_type size_j) {
1373 BOOST_UBLAS_CHECK (i < size_i, bad_index ());
1374 BOOST_UBLAS_CHECK (j < size_j, bad_index ());
1375 detail::ignore_unused_variable_warning(size_i);
1376 // Guard against size_type overflow
1377 BOOST_UBLAS_CHECK (i <= ((std::numeric_limits<size_type>::max) () - j) / size_j, bad_index ());
1378 return i * size_j + j;
1382 size_type address (size_type i, size_type size_i, size_type j, size_type size_j) {
1383 BOOST_UBLAS_CHECK (i <= size_i, bad_index ());
1384 BOOST_UBLAS_CHECK (j <= size_j, bad_index ());
1385 // Guard against size_type overflow - address may be size_j past end of storage
1386 BOOST_UBLAS_CHECK (size_j == 0 || i <= ((std::numeric_limits<size_type>::max) () - j) / size_j, bad_index ());
1387 detail::ignore_unused_variable_warning(size_i);
1388 return i * size_j + j;
1391 // Storage element to index conversion
1394 difference_type distance_i (difference_type k, size_type /* size_i */, size_type size_j) {
1395 return size_j != 0 ? k / size_j : 0;
1399 difference_type distance_j (difference_type k, size_type /* size_i */, size_type /* size_j */) {
1404 size_type index_i (difference_type k, size_type /* size_i */, size_type size_j) {
1405 return size_j != 0 ? k / size_j : 0;
1409 size_type index_j (difference_type k, size_type /* size_i */, size_type size_j) {
1410 return size_j != 0 ? k % size_j : 0;
1423 // Iterating storage elements
1427 void increment_i (I &it, size_type /* size_i */, size_type size_j) {
1433 void increment_i (I &it, difference_type n, size_type /* size_i */, size_type size_j) {
1439 void decrement_i (I &it, size_type /* size_i */, size_type size_j) {
1445 void decrement_i (I &it, difference_type n, size_type /* size_i */, size_type size_j) {
1451 void increment_j (I &it, size_type /* size_i */, size_type /* size_j */) {
1457 void increment_j (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
1463 void decrement_j (I &it, size_type /* size_i */, size_type /* size_j */) {
1469 void decrement_j (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
1473 // Triangular access
1476 size_type triangular_size (size_type size_i, size_type size_j) {
1477 size_type size = (std::max) (size_i, size_j);
1478 // Guard against size_type overflow - simplified
1479 BOOST_UBLAS_CHECK (size == 0 || size / 2 < (std::numeric_limits<size_type>::max) () / size /* +1/2 */, bad_size ());
1480 return ((size + 1) * size) / 2;
1484 size_type lower_element (size_type i, size_type size_i, size_type j, size_type size_j) {
1485 BOOST_UBLAS_CHECK (i < size_i, bad_index ());
1486 BOOST_UBLAS_CHECK (j < size_j, bad_index ());
1487 BOOST_UBLAS_CHECK (i >= j, bad_index ());
1488 detail::ignore_unused_variable_warning(size_i);
1489 detail::ignore_unused_variable_warning(size_j);
1490 // FIXME size_type overflow
1491 // sigma_i (i + 1) = (i + 1) * i / 2
1492 // i = 0 1 2 3, sigma = 0 1 3 6
1493 return ((i + 1) * i) / 2 + j;
1497 size_type upper_element (size_type i, size_type size_i, size_type j, size_type size_j) {
1498 BOOST_UBLAS_CHECK (i < size_i, bad_index ());
1499 BOOST_UBLAS_CHECK (j < size_j, bad_index ());
1500 BOOST_UBLAS_CHECK (i <= j, bad_index ());
1501 // FIXME size_type overflow
1502 // sigma_i (size - i) = size * i - i * (i - 1) / 2
1503 // i = 0 1 2 3, sigma = 0 4 7 9
1504 return (i * (2 * (std::max) (size_i, size_j) - i + 1)) / 2 + j - i;
1507 // Major and minor indices
1510 size_type index_M (size_type index1, size_type /* index2 */) {
1515 size_type index_m (size_type /* index1 */, size_type index2) {
1520 size_type size_M (size_type size_i, size_type /* size_j */) {
1525 size_type size_m (size_type /* size_i */, size_type size_j) {
1530 // This functor defines storage layout and it's properties
1531 // matrix (i,j) -> storage [i + j * size_i]
1532 template <class Z, class D>
1533 struct basic_column_major {
1534 typedef Z size_type;
1535 typedef D difference_type;
1536 typedef column_major_tag orientation_category;
1537 typedef basic_row_major<Z,D> transposed_layout;
1541 size_type storage_size (size_type size_i, size_type size_j) {
1542 // Guard against size_type overflow
1543 BOOST_UBLAS_CHECK (size_i == 0 || size_j <= (std::numeric_limits<size_type>::max) () / size_i, bad_size ());
1544 return size_i * size_j;
1547 // Indexing conversion to storage element
1550 size_type element (size_type i, size_type size_i, size_type j, size_type size_j) {
1551 BOOST_UBLAS_CHECK (i < size_i, bad_index ());
1552 BOOST_UBLAS_CHECK (j < size_j, bad_index ());
1553 detail::ignore_unused_variable_warning(size_j);
1554 // Guard against size_type overflow
1555 BOOST_UBLAS_CHECK (j <= ((std::numeric_limits<size_type>::max) () - i) / size_i, bad_index ());
1556 return i + j * size_i;
1560 size_type address (size_type i, size_type size_i, size_type j, size_type size_j) {
1561 BOOST_UBLAS_CHECK (i <= size_i, bad_index ());
1562 BOOST_UBLAS_CHECK (j <= size_j, bad_index ());
1563 detail::ignore_unused_variable_warning(size_j);
1564 // Guard against size_type overflow - address may be size_i past end of storage
1565 BOOST_UBLAS_CHECK (size_i == 0 || j <= ((std::numeric_limits<size_type>::max) () - i) / size_i, bad_index ());
1566 return i + j * size_i;
1569 // Storage element to index conversion
1572 difference_type distance_i (difference_type k, size_type /* size_i */, size_type /* size_j */) {
1577 difference_type distance_j (difference_type k, size_type size_i, size_type /* size_j */) {
1578 return size_i != 0 ? k / size_i : 0;
1582 size_type index_i (difference_type k, size_type size_i, size_type /* size_j */) {
1583 return size_i != 0 ? k % size_i : 0;
1587 size_type index_j (difference_type k, size_type size_i, size_type /* size_j */) {
1588 return size_i != 0 ? k / size_i : 0;
1605 void increment_i (I &it, size_type /* size_i */, size_type /* size_j */) {
1611 void increment_i (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
1617 void decrement_i (I &it, size_type /* size_i */, size_type /* size_j */) {
1623 void decrement_i (I &it, difference_type n, size_type /* size_i */, size_type /* size_j */) {
1629 void increment_j (I &it, size_type size_i, size_type /* size_j */) {
1635 void increment_j (I &it, difference_type n, size_type size_i, size_type /* size_j */) {
1641 void decrement_j (I &it, size_type size_i, size_type /* size_j */) {
1647 void decrement_j (I &it, difference_type n, size_type size_i, size_type /* size_j */) {
1651 // Triangular access
1654 size_type triangular_size (size_type size_i, size_type size_j) {
1655 size_type size = (std::max) (size_i, size_j);
1656 // Guard against size_type overflow - simplified
1657 BOOST_UBLAS_CHECK (size == 0 || size / 2 < (std::numeric_limits<size_type>::max) () / size /* +1/2 */, bad_size ());
1658 return ((size + 1) * size) / 2;
1662 size_type lower_element (size_type i, size_type size_i, size_type j, size_type size_j) {
1663 BOOST_UBLAS_CHECK (i < size_i, bad_index ());
1664 BOOST_UBLAS_CHECK (j < size_j, bad_index ());
1665 BOOST_UBLAS_CHECK (i >= j, bad_index ());
1666 // FIXME size_type overflow
1667 // sigma_j (size - j) = size * j - j * (j - 1) / 2
1668 // j = 0 1 2 3, sigma = 0 4 7 9
1669 return i - j + (j * (2 * (std::max) (size_i, size_j) - j + 1)) / 2;
1673 size_type upper_element (size_type i, size_type size_i, size_type j, size_type size_j) {
1674 BOOST_UBLAS_CHECK (i < size_i, bad_index ());
1675 BOOST_UBLAS_CHECK (j < size_j, bad_index ());
1676 BOOST_UBLAS_CHECK (i <= j, bad_index ());
1677 // FIXME size_type overflow
1678 // sigma_j (j + 1) = (j + 1) * j / 2
1679 // j = 0 1 2 3, sigma = 0 1 3 6
1680 return i + ((j + 1) * j) / 2;
1683 // Major and minor indices
1686 size_type index_M (size_type /* index1 */, size_type index2) {
1691 size_type index_m (size_type index1, size_type /* index2 */) {
1696 size_type size_M (size_type /* size_i */, size_type size_j) {
1701 size_type size_m (size_type size_i, size_type /* size_j */) {
1709 typedef Z size_type;
1714 size_type packed_size (L, size_type size_i, size_type size_j) {
1715 return L::storage_size (size_i, size_j);
1720 bool zero (size_type /* i */, size_type /* j */) {
1725 bool one (size_type /* i */, size_type /* j */) {
1730 bool other (size_type /* i */, size_type /* j */) {
1733 // FIXME: this should not be used at all
1736 size_type restrict1 (size_type i, size_type /* j */) {
1741 size_type restrict2 (size_type /* i */, size_type j) {
1746 size_type mutable_restrict1 (size_type i, size_type /* j */) {
1751 size_type mutable_restrict2 (size_type /* i */, size_type j) {
1757 template < class L >
1758 struct transposed_structure {
1759 typedef typename L::size_type size_type;
1761 template<class LAYOUT>
1764 size_type packed_size (LAYOUT l, size_type size_i, size_type size_j) {
1765 return L::packed_size(l, size_j, size_i);
1770 bool zero (size_type i, size_type j) {
1771 return L::zero(j, i);
1775 bool one (size_type i, size_type j) {
1776 return L::one(j, i);
1780 bool other (size_type i, size_type j) {
1781 return L::other(j, i);
1783 template<class LAYOUT>
1786 size_type element (LAYOUT /* l */, size_type i, size_type size_i, size_type j, size_type size_j) {
1787 return L::element(typename LAYOUT::transposed_layout(), j, size_j, i, size_i);
1792 size_type restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
1793 return L::restrict2(j, i, size2, size1);
1797 size_type restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
1798 return L::restrict1(j, i, size2, size1);
1802 size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
1803 return L::mutable_restrict2(j, i, size2, size1);
1807 size_type mutable_restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
1808 return L::mutable_restrict1(j, i, size2, size1);
1813 size_type global_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
1814 return L::global_restrict2(index2, size2, index1, size1);
1818 size_type global_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
1819 return L::global_restrict1(index2, size2, index1, size1);
1823 size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
1824 return L::global_mutable_restrict2(index2, size2, index1, size1);
1828 size_type global_mutable_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
1829 return L::global_mutable_restrict1(index2, size2, index1, size1);
1835 struct basic_lower {
1836 typedef Z size_type;
1837 typedef lower_tag triangular_type;
1842 size_type packed_size (L, size_type size_i, size_type size_j) {
1843 return L::triangular_size (size_i, size_j);
1848 bool zero (size_type i, size_type j) {
1853 bool one (size_type /* i */, size_type /* j */) {
1858 bool other (size_type i, size_type j) {
1864 size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
1865 return L::lower_element (i, size_i, j, size_j);
1868 // return nearest valid index in column j
1871 size_type restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
1872 return (std::max)(j, (std::min) (size1, i));
1874 // return nearest valid index in row i
1877 size_type restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
1878 return (std::max)(size_type(0), (std::min) (i+1, j));
1880 // return nearest valid mutable index in column j
1883 size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
1884 return (std::max)(j, (std::min) (size1, i));
1886 // return nearest valid mutable index in row i
1889 size_type mutable_restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
1890 return (std::max)(size_type(0), (std::min) (i+1, j));
1893 // return an index between the first and (1+last) filled row
1896 size_type global_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
1897 return (std::max)(size_type(0), (std::min)(size1, index1) );
1899 // return an index between the first and (1+last) filled column
1902 size_type global_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
1903 return (std::max)(size_type(0), (std::min)(size2, index2) );
1906 // return an index between the first and (1+last) filled mutable row
1909 size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
1910 return (std::max)(size_type(0), (std::min)(size1, index1) );
1912 // return an index between the first and (1+last) filled mutable column
1915 size_type global_mutable_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
1916 return (std::max)(size_type(0), (std::min)(size2, index2) );
1920 // the first row only contains a single 1. Thus it is not stored.
1922 struct basic_unit_lower : public basic_lower<Z> {
1923 typedef Z size_type;
1924 typedef unit_lower_tag triangular_type;
1929 size_type packed_size (L, size_type size_i, size_type size_j) {
1930 // Zero size strict triangles are bad at this point
1931 BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0, bad_index ());
1932 return L::triangular_size (size_i - 1, size_j - 1);
1937 bool one (size_type i, size_type j) {
1942 bool other (size_type i, size_type j) {
1948 size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
1949 // Zero size strict triangles are bad at this point
1950 BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0 && i != 0, bad_index ());
1951 return L::lower_element (i-1, size_i - 1, j, size_j - 1);
1956 size_type mutable_restrict1 (size_type i, size_type j, size_type size1, size_type /* size2 */) {
1957 return (std::max)(j+1, (std::min) (size1, i));
1961 size_type mutable_restrict2 (size_type i, size_type j, size_type /* size1 */, size_type /* size2 */) {
1962 return (std::max)(size_type(0), (std::min) (i, j));
1965 // return an index between the first and (1+last) filled mutable row
1968 size_type global_mutable_restrict1 (size_type index1, size_type size1, size_type /* index2 */, size_type /* size2 */) {
1969 return (std::max)(size_type(1), (std::min)(size1, index1) );
1971 // return an index between the first and (1+last) filled mutable column
1974 size_type global_mutable_restrict2 (size_type /* index1 */, size_type /* size1 */, size_type index2, size_type size2) {
1975 BOOST_UBLAS_CHECK( size2 >= 1 , external_logic() );
1976 return (std::max)(size_type(0), (std::min)(size2-1, index2) );
1980 // the first row only contains no element. Thus it is not stored.
1982 struct basic_strict_lower : public basic_unit_lower<Z> {
1983 typedef Z size_type;
1984 typedef strict_lower_tag triangular_type;
1989 size_type packed_size (L, size_type size_i, size_type size_j) {
1990 // Zero size strict triangles are bad at this point
1991 BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0, bad_index ());
1992 return L::triangular_size (size_i - 1, size_j - 1);
1997 bool zero (size_type i, size_type j) {
2002 bool one (size_type /*i*/, size_type /*j*/) {
2007 bool other (size_type i, size_type j) {
2013 size_type element (L, size_type i, size_type size_i, size_type j, size_type size_j) {
2014 // Zero size strict triangles are bad at this point
2015 BOOST_UBLAS_CHECK (size_i != 0 && size_j != 0 && i != 0, bad_index ());
2016 return L::lower_element (i-1, size_i - 1, j, size_j - 1);
2021 size_type restrict1 (size_type i, size_type j, size_type size1, size_type size2) {
2022 return basic_unit_lower<Z>::mutable_restrict1(i, j, size1, size2);
2026 size_type restrict2 (size_type i, size_type j, size_type size1, size_type size2) {
2027 return basic_unit_lower<Z>::mutable_restrict2(i, j, size1, size2);
2030 // return an index between the first and (1+last) filled row
2033 size_type global_restrict1 (size_type index1, size_type size1, size_type index2, size_type size2) {
2034 return basic_unit_lower<Z>::global_mutable_restrict1(index1, size1, index2, size2);
2036 // return an index between the first and (1+last) filled column
2039 size_type global_restrict2 (size_type index1, size_type size1, size_type index2, size_type size2) {
2040 return basic_unit_lower<Z>::global_mutable_restrict2(index1, size1, index2, size2);
2046 struct basic_upper : public detail::transposed_structure<basic_lower<Z> >
2048 typedef upper_tag triangular_type;
2052 struct basic_unit_upper : public detail::transposed_structure<basic_unit_lower<Z> >
2054 typedef unit_upper_tag triangular_type;
2058 struct basic_strict_upper : public detail::transposed_structure<basic_strict_lower<Z> >
2060 typedef strict_upper_tag triangular_type;