From b56c4e7694f96d7e1ed7138d69bb733caf9a5db4 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 19 Jul 2022 12:13:16 +0100 Subject: [PATCH 1/7] feat: method is_matrix_symmetric added. --- pyerrors/correlators.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index e0cccec8..0d562c15 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -237,6 +237,17 @@ class Corr: raise Exception("Corr could not be symmetrized: No redundant values") return Corr(newcontent, prange=self.prange) + def is_matrix_symmetric(self): + """Checks whether a correlator matrices is symmetric on every timeslice.""" + if self.N == 1: + raise Exception("Only works for correlator matrices.") + for t in range(self.T): + for i in range(self.N): + for j in range(i + 1, self.N): + if hash(self[t][i, j]) != hash(self[t][j, i]): + return False + return True + def matrix_symmetric(self): """Symmetrizes the correlator matrices on every timeslice.""" if self.N > 1: From 7d280b3e2695fc6a0b510628785be51a6f0cc4fb Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 19 Jul 2022 12:18:26 +0100 Subject: [PATCH 2/7] feat: faster check for object equivalence added to is_matrix_symmetric --- pyerrors/correlators.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index 0d562c15..c3391ce9 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -244,6 +244,8 @@ class Corr: for t in range(self.T): for i in range(self.N): for j in range(i + 1, self.N): + if self[t][i, j] is self[t][j, i]: + continue if hash(self[t][i, j]) != hash(self[t][j, i]): return False return True From 42f62380b4a3255009b5d4320d2f1c4c7fca95a2 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 19 Jul 2022 12:22:54 +0100 Subject: [PATCH 3/7] feat: check for symmetric matrix added to GEVP for speed up, None case treated correctly in is_matrix_symmetric. --- pyerrors/correlators.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index c3391ce9..9bcfeb81 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -242,6 +242,8 @@ class Corr: if self.N == 1: raise Exception("Only works for correlator matrices.") for t in range(self.T): + if self[t] is None: + continue for i in range(self.N): for j in range(i + 1, self.N): if self[t][i, j] is self[t][j, i]: @@ -297,7 +299,10 @@ class Corr: warnings.warn("Argument 'sorted_list' is deprecated, use 'sort' instead.", DeprecationWarning) sort = kwargs.get("sorted_list") - symmetric_corr = self.matrix_symmetric() + if self.is_matrix_symmetric(): + symmetric_corr = self + else: + symmetric_corr = self.matrix_symmetric() if sort is None: if (ts is None): raise Exception("ts is required if sort=None.") From 4a6dfe348205b7d0dc062653810b2c9de6706ee2 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 19 Jul 2022 12:31:10 +0100 Subject: [PATCH 4/7] tests: test for is_matrix_symmetric added. --- tests/correlators_test.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/correlators_test.py b/tests/correlators_test.py index ee2c3eb7..bf6711ae 100644 --- a/tests/correlators_test.py +++ b/tests/correlators_test.py @@ -367,6 +367,28 @@ def test_matrix_symmetric(): corr3.matrix_symmetric() +def test_is_matrix_symmetric(): + corr_data = [] + for t in range(4): + mat = np.zeros((4, 4), dtype=object) + for i in range(4): + for j in range(i, 4): + obs = pe.pseudo_Obs(0.1, 0.047, "rgetrasrewe53455b153v13v5/*/*sdfgb") + mat[i, j] = obs + if i != j: + mat[j, i] = obs + corr_data.append(mat) + corr = pe.Corr(corr_data) + + assert corr.is_matrix_symmetric() + corr[0][0, 1] = 1.0 * corr[0][0, 1] + assert corr.is_matrix_symmetric() + corr[3][2, 1] = (1 + 1e-14) * corr[3][2, 1] + assert corr.is_matrix_symmetric() + corr[0][0, 1] = 1.1 * corr[0][0, 1] + assert not corr.is_matrix_symmetric() + + def test_GEVP_solver(): mat1 = np.random.rand(15, 15) From 821d36aea9db419b0f985e7c539859308e98582f Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 19 Jul 2022 12:34:31 +0100 Subject: [PATCH 5/7] refactor: refactored check for symmetric correlator matrix into matrix_symmetric. --- pyerrors/correlators.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index 9bcfeb81..e11ef6f9 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -254,11 +254,13 @@ class Corr: def matrix_symmetric(self): """Symmetrizes the correlator matrices on every timeslice.""" - if self.N > 1: - transposed = [None if _check_for_none(self, G) else G.T for G in self.content] - return 0.5 * (Corr(transposed) + self) if self.N == 1: raise Exception("Trying to symmetrize a correlator matrix, that already has N=1.") + if self.is_matrix_symmetric(): + return 1.0 * self + else: + transposed = [None if _check_for_none(self, G) else G.T for G in self.content] + return 0.5 * (Corr(transposed) + self) def GEVP(self, t0, ts=None, sort="Eigenvalue", **kwargs): r'''Solve the generalized eigenvalue problem on the correlator matrix and returns the corresponding eigenvectors. @@ -299,10 +301,7 @@ class Corr: warnings.warn("Argument 'sorted_list' is deprecated, use 'sort' instead.", DeprecationWarning) sort = kwargs.get("sorted_list") - if self.is_matrix_symmetric(): - symmetric_corr = self - else: - symmetric_corr = self.matrix_symmetric() + symmetric_corr = self.matrix_symmetric() if sort is None: if (ts is None): raise Exception("ts is required if sort=None.") From 2c2890d5ea9c845e53b3f432edef1b9451a06ee7 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 19 Jul 2022 12:42:45 +0100 Subject: [PATCH 6/7] tests: padding added to is_matrix_symmetric test. --- tests/correlators_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/correlators_test.py b/tests/correlators_test.py index bf6711ae..d10dc144 100644 --- a/tests/correlators_test.py +++ b/tests/correlators_test.py @@ -378,7 +378,7 @@ def test_is_matrix_symmetric(): if i != j: mat[j, i] = obs corr_data.append(mat) - corr = pe.Corr(corr_data) + corr = pe.Corr(corr_data, padding=[0, 2]) assert corr.is_matrix_symmetric() corr[0][0, 1] = 1.0 * corr[0][0, 1] From 1d2c41ae54bdf15d1c1639d3f0b58f80e9beb512 Mon Sep 17 00:00:00 2001 From: Fabian Joswig Date: Tue, 19 Jul 2022 12:49:32 +0100 Subject: [PATCH 7/7] feat: further speed up of GEVP by skipping the call to matrix_symmetric if matrix is already symmetric. --- pyerrors/correlators.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pyerrors/correlators.py b/pyerrors/correlators.py index e11ef6f9..b4a26f53 100644 --- a/pyerrors/correlators.py +++ b/pyerrors/correlators.py @@ -301,7 +301,11 @@ class Corr: warnings.warn("Argument 'sorted_list' is deprecated, use 'sort' instead.", DeprecationWarning) sort = kwargs.get("sorted_list") - symmetric_corr = self.matrix_symmetric() + if self.is_matrix_symmetric(): + symmetric_corr = self + else: + symmetric_corr = self.matrix_symmetric() + if sort is None: if (ts is None): raise Exception("ts is required if sort=None.")