From 054865be9b2d6219f09c71060060111f477dd3fd Mon Sep 17 00:00:00 2001 From: Rene Milk <rene.milk@uni-muenster.de> Date: Thu, 26 Aug 2010 21:30:29 +0200 Subject: [PATCH] trace function --- stuff/matrix.hh | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/stuff/matrix.hh b/stuff/matrix.hh index 76a677df4..1271cc8ab 100644 --- a/stuff/matrix.hh +++ b/stuff/matrix.hh @@ -45,6 +45,28 @@ public: } }; +template <class FieldMatrixType> +class MatrixDiagonal : public FieldVector<typename FieldMatrixType::field_type, FieldMatrixType::rows> +{ +public: + MatrixDiagonal(const FieldMatrixType& matrix) + { + CompileTimeChecker<FieldMatrixType::rows == FieldMatrixType::cols> matrix_must_be_square; + for (size_t i = 0; i < FieldMatrixType::rows; i++) + (*this)[i] = matrix[i][i]; + } +}; + +template <class FieldMatrixType> +typename FieldMatrixType::field_type matrixTrace(const FieldMatrixType& matrix) +{ + MatrixDiagonal<FieldMatrixType> diag(matrix); + typename FieldMatrixType::field_type trace = typename FieldMatrixType::field_type(0); + for (size_t i = 0; i < FieldMatrixType::rows; i++) + trace += diag[i]; + return trace; +} + } // namespace Dune -- GitLab