From 43370d37647e4d446e8681b754a7e3122b2d8663 Mon Sep 17 00:00:00 2001 From: Jonathan Robey Date: Tue, 6 Aug 2019 14:27:28 -0600 Subject: [PATCH] Add frobenius_norm method to BlockMatrixBase Fix to #7629 --- include/deal.II/lac/block_matrix_base.h | 30 +++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/include/deal.II/lac/block_matrix_base.h b/include/deal.II/lac/block_matrix_base.h index daee645f62..ce2649802f 100644 --- a/include/deal.II/lac/block_matrix_base.h +++ b/include/deal.II/lac/block_matrix_base.h @@ -698,6 +698,13 @@ public: value_type matrix_norm_square(const BlockVectorType &v) const; + /** + * Return the frobenius norm of the matrix, i.e. the square root of the sum + * of squares of all entries in the matrix. + */ + value_type + frobenius_norm() const; + /** * Compute the matrix scalar product $\left(u,Mv\right)$. */ @@ -2401,6 +2408,29 @@ BlockMatrixBase::matrix_norm_square(const BlockVectorType &v) const +template +typename BlockMatrixBase::value_type +BlockMatrixBase::frobenius_norm() const +{ + value_type norm_sqr = 0; + + + // For each block, get the Frobenius norm, and add the square to the + // accumulator for the full matrix + for (unsigned int row = 0; row < n_block_rows(); ++row) + { + for (unsigned int col = 0; col < n_block_cols(); ++col) + { + const value_type block_norm = block(row, col).frobenius_norm(); + norm_sqr += block_norm * block_norm; + } + } + + return std::sqrt(norm_sqr); +} + + + template template typename BlockMatrixBase::value_type -- 2.39.5