Skip to content

Commit

Permalink
Add symmetrize routine
Browse files Browse the repository at this point in the history
  • Loading branch information
william-dawson committed Jun 19, 2024
1 parent d215bee commit f365108
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions Source/C/PSMatrix_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ double MatrixNorm_ps_wrp(const int *ih_this);
double MeasureAsymmetry_ps_wrp(const int *ih_this);
void MatrixTrace_ps_wrp(const int *ih_this, double *trace_val);
int IsIdentity_ps_wrp(const int *ih_this);
void SymmetrizeMatrix_ps_wrp(int *ih_this);

#endif
5 changes: 5 additions & 0 deletions Source/CPlusPlus/PSMatrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ double Matrix_ps::MeasureAsymmetry() const {
return MeasureAsymmetry_ps_wrp(ih_this);
}

//////////////////////////////////////////////////////////////////////////////
void Matrix_ps::Symmetrize() {
SymmetrizeMatrix_ps_wrp(ih_this);
}

//////////////////////////////////////////////////////////////////////////////
Matrix_ps::~Matrix_ps() { DestructMatrix_ps_wrp(ih_this); }
} // namespace NTPoly
2 changes: 2 additions & 0 deletions Source/CPlusPlus/PSMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ class Matrix_ps {
double MeasureAsymmetry() const;
//! compute the trace of a matrix.
double Trace() const;
//! symmetrize the matrix
void Symmetrize();

public:
//! Destructor.
Expand Down
16 changes: 16 additions & 0 deletions Source/Fortran/PSMatrixAlgebraModule.F90
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ MODULE PSMatrixAlgebraModule
PUBLIC :: MatrixTrace
PUBLIC :: SimilarityTransform
PUBLIC :: MeasureAsymmetry
PUBLIC :: SymmetrizeMatrix
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
INTERFACE MatrixSigma
MODULE PROCEDURE MatrixSigma_ps
Expand Down Expand Up @@ -543,6 +544,21 @@ FUNCTION MeasureAsymmetry(this) RESULT(norm_value)
CALL DestructMatrix(tmat)

END FUNCTION MeasureAsymmetry
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Make the matrix symmetric
SUBROUTINE SymmetrizeMatrix(this)
!> The matrix to symmetrize
TYPE(Matrix_ps), INTENT(INOUT) :: this
!! Local variables
TYPE(Matrix_ps) :: tmat

CALL TransposeMatrix(this, tmat)
CALL ConjugateMatrix(tmat)
CALL IncrementMatrix(tmat, this, alpha_in=1.0_NTREAL)
CALL ScaleMatrix(this, 0.5_NTREAL)
CALL DestructMatrix(tmat)

END SUBROUTINE SymmetrizeMatrix
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Transform a matrix B = P * A * P^-1
!! This routine will check if P is the identity matrix, and if so
Expand Down
11 changes: 11 additions & 0 deletions Source/Wrapper/PSMatrixAlgebraModule_wrp.F90
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ MODULE PSMatrixAlgebraModule_wrp
PUBLIC :: MatrixNorm_ps_wrp
PUBLIC :: MatrixTrace_ps_wrp
PUBLIC :: MeasureAsymmetry_ps_wrp
PUBLIC :: SymmetrizeMatrix_ps_wrp
CONTAINS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Matrix B = alpha*Matrix A + Matrix B (AXPY)
SUBROUTINE IncrementMatrix_ps_wrp(ih_matA, ih_matB, alpha_in,threshold_in) &
Expand Down Expand Up @@ -156,5 +157,15 @@ FUNCTION MeasureAsymmetry_ps_wrp(ih_this) &
h_this = TRANSFER(ih_this,h_this)
norm_value = MeasureAsymmetry(h_this%DATA)
END FUNCTION MeasureAsymmetry_ps_wrp
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Symmetrize a matrix
SUBROUTINE SymmetrizeMatrix_ps_wrp(ih_matA) &
& BIND(c,NAME="SymmetrizeMatrix_ps_wrp")
INTEGER(KIND=c_int), INTENT(INOUT) :: ih_matA(SIZE_wrp)
TYPE(Matrix_ps_wrp) :: h_matA

h_matA = TRANSFER(ih_matA,h_matA)
CALL SymmetrizeMatrix(h_matA%DATA)
END SUBROUTINE SymmetrizeMatrix_ps_wrp
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
END MODULE PSMatrixAlgebraModule_wrp
25 changes: 24 additions & 1 deletion UnitTests/test_psmatrixalgebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def test_reverse(self):
self.check_result()

def test_asymmetry(self):
'''Test routines to permute a matrix.'''
'''Test routines to measure asymmetry of a matrix.'''
from numpy import inf
from scipy.linalg import norm
for param in self.parameters:
Expand All @@ -305,6 +305,29 @@ def test_asymmetry(self):

self.check_floats(ref, comp)

def test_symmetrize(self):
'''Test routines to symmetrize a matrix.'''
from numpy import inf
from scipy.linalg import norm
for param in self.parameters:
matrix1 = param.create_matrix(snum=1, complex=self.complex1)
self.write_matrix(matrix1, self.input_file1)

comm.barrier()

if param.sparsity > 0.0:
ntmatrix1 = nt.Matrix_ps(self.input_file1, False)
else:
ntmatrix1 = nt.Matrix_ps(param.rows)

self.CheckMat = 0.5 * (matrix1 + matrix1.H)
ntmatrix1 = nt.Matrix_ps(self.input_file1, False)
ntmatrix1.Symmetrize()
ntmatrix1.WriteToMatrixMarket(self.result_file)
comm.barrier()

self.check_result()


class TestPSMatrixAlgebra_r(TestPSMatrixAlgebra, unittest.TestCase):
'''Special routines for real algebra'''
Expand Down

0 comments on commit f365108

Please sign in to comment.