MathTL
|
00001 // -*- c++ -*- 00002 00003 // +--------------------------------------------------------------------+ 00004 // | This file is part of MathTL - the Mathematical Template Library | 00005 // | | 00006 // | Copyright (c) 2002-2009 | 00007 // | Thorsten Raasch, Manuel Werner | 00008 // +--------------------------------------------------------------------+ 00009 00010 #ifndef _MATHTL_MATRIX_NORMS_H 00011 #define _MATHTL_MATRIX_NORMS_H 00012 00013 #include <algorithm> 00014 00015 namespace MathTL 00016 { 00017 /* 00018 computation of different norms for generic MATRIX classes 00019 00020 You can use template specialization to speed up the norm computation 00021 for specific MATRIX classes. 00022 */ 00023 00028 template <class MATRIX> 00029 const double row_sum_norm(const MATRIX& M) 00030 { 00031 double r(0.0); 00032 00033 for (typename MATRIX::size_type i(0); i < M.row_dimension(); i++) 00034 { 00035 double rowsum(0.0); 00036 for (typename MATRIX::size_type j(0); j < M.column_dimension(); j++) 00037 rowsum += fabs(M.get_entry(i,j)); 00038 r = std::max(r, rowsum); 00039 } 00040 00041 return r; 00042 } 00043 00048 template <class MATRIX> 00049 const double column_sum_norm(const MATRIX& M) 00050 { 00051 double r(0.0); 00052 00053 for (typename MATRIX::size_type j(0); j < M.column_dimension(); j++) 00054 { 00055 double colsum(0.0); 00056 for (typename MATRIX::size_type i(0); i < M.column_dimension(); i++) 00057 colsum += fabs(M.get_entry(i,j)); 00058 r = std::max(r, colsum); 00059 } 00060 00061 return r; 00062 } 00063 00069 template <class MATRIX> 00070 const double frobenius_norm(const MATRIX& M) 00071 { 00072 double r(0.0); 00073 00074 for (typename MATRIX::size_type j(0); j < M.column_dimension(); j++) 00075 for (typename MATRIX::size_type i(0); i < M.column_dimension(); i++) 00076 r += M.get_entry(i,j)*M.get_entry(i,j); 00077 00078 return sqrt(r); 00079 } 00080 00084 template <class MATRIX> 00085 const double maximum_norm(const MATRIX& M) 00086 { 00087 double r(0.0); 00088 00089 for (typename MATRIX::size_type i(0); i < M.row_dimension(); i++) 00090 for (typename MATRIX::size_type j(0); j < M.column_dimension(); j++) 00091 r = std::max(r, fabs(M.get_entry(i,j))); 00092 00093 return r; 00094 } 00095 00096 } 00097 00098 #endif