00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_MAX_OF_SUMS_INCLUDE
00013 #define MTL_MAX_OF_SUMS_INCLUDE
00014
00015 #include <boost/numeric/mtl/concept/magnitude.hpp>
00016 #include <boost/numeric/mtl/concept/collection.hpp>
00017 #include <boost/numeric/linear_algebra/operators.hpp>
00018 #include <boost/numeric/linear_algebra/identity.hpp>
00019 #include <boost/numeric/mtl/utility/tag.hpp>
00020 #include <boost/numeric/mtl/utility/range_generator.hpp>
00021 #include <boost/numeric/mtl/vector/dense_vector.hpp>
00022
00023 #include <numeric>
00024 #include <cmath>
00025
00026
00027 namespace mtl { namespace impl {
00028
00029
00030 template <typename Matrix, typename MinorIndex>
00031 typename RealMagnitude<typename Collection<Matrix>::value_type>::type
00032 inline max_of_sums(const Matrix& matrix, bool aligned, MinorIndex minor_index, unsigned dim2)
00033 {
00034 using std::max; using std::abs; using math::zero;
00035
00036 typedef typename Collection<Matrix>::value_type value_type;
00037 typedef typename RealMagnitude<value_type>::type real_type;
00038 real_type ref, my_zero= zero(ref);
00039
00040
00041 if (num_rows(matrix) == 0 || num_cols(matrix) == 0)
00042 return my_zero;
00043
00044 typedef typename traits::range_generator<tag::major, Matrix>::type cursor_type;
00045 typedef typename traits::range_generator<tag::nz, cursor_type>::type icursor_type;
00046 typename traits::const_value<Matrix>::type value(matrix);
00047
00048 if (aligned) {
00049 real_type maxv= my_zero;
00050 for (cursor_type cursor = begin<tag::major>(matrix), cend = end<tag::major>(matrix); cursor != cend; ++cursor) {
00051 real_type sum= my_zero;
00052 for (icursor_type icursor = begin<tag::nz>(cursor), icend = end<tag::nz>(cursor); icursor != icend; ++icursor)
00053 sum+= abs(value(*icursor));
00054 maxv= max(maxv, sum);
00055 }
00056 return maxv;
00057 }
00058
00059
00060 vector::dense_vector<real_type> sums(dim2, my_zero);
00061 for (cursor_type cursor = begin<tag::major>(matrix), cend = end<tag::major>(matrix); cursor != cend; ++cursor)
00062 for (icursor_type icursor = begin<tag::nz>(cursor), icend = end<tag::nz>(cursor); icursor != icend; ++icursor)
00063 sums[minor_index(*icursor)]+= abs(value(*icursor));
00064
00065 return std::accumulate(sums.begin(), sums.end(), my_zero, math::max<real_type>());
00066 }
00067
00068
00069 }}
00070
00071 #endif // MTL_MAX_OF_SUMS_INCLUDE