00001 // Software License for MTL 00002 // 00003 // Copyright (c) 2007 The Trustees of Indiana University. 00004 // 2008 Dresden University of Technology and the Trustees of Indiana University. 00005 // All rights reserved. 00006 // Authors: Peter Gottschling and Andrew Lumsdaine 00007 // 00008 // This file is part of the Matrix Template Library 00009 // 00010 // See also license.mtl.txt in the distribution. 00011 00012 #ifndef MTL_MATRIX_MAX_ABS_POS_INCLUDE 00013 #define MTL_MATRIX_MAX_ABS_POS_INCLUDE 00014 00015 #include <utility> 00016 #include <cmath> 00017 #include <boost/numeric/linear_algebra/identity.hpp> 00018 #include <boost/numeric/mtl/concept/collection.hpp> 00019 #include <boost/numeric/mtl/concept/magnitude.hpp> 00020 #include <boost/numeric/mtl/utility/range_generator.hpp> 00021 #include <boost/numeric/mtl/utility/tag.hpp> 00022 00023 namespace mtl { 00024 00025 namespace matrix { 00027 template <typename Matrix> 00028 typename mtl::traits::enable_if_matrix<Matrix, std::pair<typename Collection<Matrix>::size_type, typename Collection<Matrix>::size_type> >::type 00029 inline max_abs_pos(const Matrix& A) 00030 { 00031 namespace traits = mtl::traits; 00032 using std::abs; 00033 typedef typename Collection<Matrix>::value_type value_type; 00034 typedef typename Collection<Matrix>::size_type size_type; 00035 00036 typename RealMagnitude<value_type>::type max(abs(A[0][0])); 00037 size_type r= 0, c= 0; 00038 00039 typename traits::row<Matrix>::type row(A); 00040 typename traits::col<Matrix>::type col(A); 00041 typename traits::const_value<Matrix>::type value(A); 00042 typedef typename traits::range_generator<tag::major, Matrix>::type cursor_type; 00043 00044 for (cursor_type cursor = begin<tag::major>(A), cend = end<tag::major>(A); cursor != cend; ++cursor) { 00045 typedef typename traits::range_generator<tag::nz, cursor_type>::type icursor_type; 00046 for (icursor_type icursor = begin<tag::nz>(cursor), icend = end<tag::nz>(cursor); icursor != icend; ++icursor) 00047 if (abs(value(*icursor)) > max) { 00048 max= abs(value(*icursor)); 00049 r= row(*icursor); 00050 c= col(*icursor); 00051 } 00052 } 00053 00054 return std::make_pair(r, c); 00055 } 00056 00057 } // namespace matrix 00058 00059 namespace vector { 00061 template <typename Vector> 00062 typename mtl::traits::enable_if_vector<Vector, typename Collection<Vector>::size_type>::type 00063 inline max_abs_pos(const Vector& v) 00064 { 00065 using std::abs; 00066 typedef typename Collection<Vector>::size_type size_type; 00067 typedef typename Collection<Vector>::value_type value_type; 00068 00069 size_type i= 0; 00070 size_type max_col= size(v); 00071 value_type max(abs(v[0])); 00072 00073 for(size_type j= 1; j < max_col; j++) 00074 if(abs(v[j]) > max) { 00075 max = v[j]; 00076 i= j; 00077 } 00078 return i; 00079 } 00080 00081 } // namespace vector 00082 00083 } // namespace mtl 00084 00085 #endif // MTL_MATRIX_MAX_ABS_POS_INCLUDE 00086