00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_MATRIX_MAX_POS
00013 #define MTL_MATRIX_MAX_POS
00014
00015 #include <cmath>
00016 #include <utility>
00017 #include <boost/numeric/linear_algebra/identity.hpp>
00018 #include <boost/numeric/mtl/concept/collection.hpp>
00019 #include <boost/numeric/mtl/utility/range_generator.hpp>
00020 #include <boost/numeric/mtl/utility/tag.hpp>
00021 #include <boost/numeric/mtl/utility/property_map.hpp>
00022
00023 namespace mtl {
00024
00025 namespace matrix {
00027 template <typename Matrix>
00028 std::pair<typename Collection<Matrix>::size_type, typename Collection<Matrix>::size_type>
00029 inline max_pos(const Matrix& A)
00030 {
00031 namespace traits = mtl::traits;
00032 typedef typename Collection<Matrix>::value_type value_type;
00033 typedef typename Collection<Matrix>::size_type size_type;
00034
00035 value_type max(A[0][0]);
00036 size_type r= 0, c= 0;
00037
00038 typename traits::row<Matrix>::type row(A);
00039 typename traits::col<Matrix>::type col(A);
00040 typename traits::const_value<Matrix>::type value(A);
00041 typedef typename traits::range_generator<tag::major, Matrix>::type cursor_type;
00042
00043 for (cursor_type cursor = begin<tag::major>(A), cend = end<tag::major>(A); cursor != cend; ++cursor) {
00044 typedef typename traits::range_generator<tag::nz, cursor_type>::type icursor_type;
00045 for (icursor_type icursor = begin<tag::nz>(cursor), icend = end<tag::nz>(cursor); icursor != icend; ++icursor)
00046 if (value(*icursor) > max) {
00047 max= value(*icursor);
00048 r= row(*icursor);
00049 c= col(*icursor);
00050 }
00051 }
00052
00053 return std::make_pair(r, c);
00054 }
00055
00056 }
00057
00058 namespace vector {
00060 template <typename Vector>
00061 typename Collection<Vector>::size_type
00062 inline max_pos(const Vector& v)
00063 {
00064 typedef typename Collection<Vector>::size_type size_type;
00065 typedef typename Collection<Vector>::value_type value_type;
00066
00067 size_type i= 0;
00068
00069 size_type max_col= size(v);
00070 value_type max(v[0]);
00071
00072 for(size_type j= 1;i < max_col; j++)
00073 if(v[j] > max) {
00074 max = v[j];
00075 i= j;
00076 }
00077 return i;
00078 }
00079
00080 }
00081
00082 }
00083
00084 #endif // MTL_MATRIX_MAX_POS
00085