From 0d1e87f26989fbccde1b6d9c7503f892d3469433 Mon Sep 17 00:00:00 2001 From: philippedevloo Date: Mon, 13 Dec 2021 16:23:13 -0300 Subject: [PATCH] Included a method to create a diagonal matrix with constant diagonal value --- Matrix/pzmatrix.cpp | 13 +++++++++++++ Matrix/pzmatrix.h | 2 ++ 2 files changed, 15 insertions(+) diff --git a/Matrix/pzmatrix.cpp b/Matrix/pzmatrix.cpp index 9f47931cf6..e436c95a5d 100644 --- a/Matrix/pzmatrix.cpp +++ b/Matrix/pzmatrix.cpp @@ -186,6 +186,19 @@ void TPZMatrix::Identity() { } } +template +void TPZMatrix::Diagonal(TVar val) { + + if ( Cols() != Rows() ) { + Error( "Diagonal (TPZMatrix<>*) must be square>" ); + } + for ( int64_t row = 0; row < Rows(); row++) { + for ( int64_t col = 0; col < Cols(); col++ ) { + (row == col)? PutVal(row,col,val):PutVal(row,col,0.); + } + } +} + template void TPZMatrix::Input(std::istream& in ) diff --git a/Matrix/pzmatrix.h b/Matrix/pzmatrix.h index b896bebb5e..8fbfb64d41 100644 --- a/Matrix/pzmatrix.h +++ b/Matrix/pzmatrix.h @@ -209,6 +209,8 @@ class TPZMatrix: public TPZBaseMatrix /** @brief Converts the matrix in an identity matrix*/ virtual void Identity(); + /** @brief Converts the matrix in a diagonal matrix*/ + virtual void Diagonal(TVar val); /** @brief It makes *T the transpose of current matrix. */ virtual void Transpose(TPZMatrix*const T) const;