mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-03-15 14:50:25 +01:00
Merge pull request #117 from fjosw/feat/is_matrix_symmetric
is_matrix_symmetric
This commit is contained in:
commit
58779a8063
2 changed files with 47 additions and 4 deletions
|
@ -237,13 +237,30 @@ 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):
|
||||
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]:
|
||||
continue
|
||||
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:
|
||||
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.
|
||||
|
@ -284,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.")
|
||||
|
|
|
@ -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, padding=[0, 2])
|
||||
|
||||
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)
|
||||
|
|
Loading…
Add table
Reference in a new issue