00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_TRACE_INCLUDE
00013 #define MTL_TRACE_INCLUDE
00014
00015 #include <boost/numeric/linear_algebra/identity.hpp>
00016 #include <boost/numeric/mtl/utility/exception.hpp>
00017 #include <boost/numeric/mtl/utility/tag.hpp>
00018 #include <boost/numeric/mtl/utility/category.hpp>
00019 #include <boost/numeric/mtl/concept/collection.hpp>
00020
00021 namespace mtl { namespace matrix {
00022
00023 template <typename Matrix>
00024 typename Collection<Matrix>::value_type
00025 inline trace(const Matrix& matrix)
00026 {
00027 using math::zero;
00028 typedef typename Collection<Matrix>::value_type value_type;
00029
00030 MTL_THROW_IF(num_rows(matrix) != num_cols(matrix), matrix_not_square());
00031
00032
00033 if (num_rows(matrix) == 0) {
00034 value_type ref;
00035 return zero(ref);
00036 }
00037
00038 value_type value= matrix[0][0];
00039 for (unsigned i= 1; i < num_rows(matrix); i++)
00040 value+= matrix[i][i];
00041 return value;
00042 }
00043
00044
00045 }}
00046
00047 #endif // MTL_TRACE_INCLUDE