mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-14 19:43:41 +02:00
fix: check for correlator None entries refactored and added to all
elementary operations. Tests added.
This commit is contained in:
parent
5359a30b97
commit
ed50240d29
2 changed files with 30 additions and 20 deletions
|
@ -155,7 +155,7 @@ class Corr:
|
||||||
raise Exception("Vectors are of wrong shape!")
|
raise Exception("Vectors are of wrong shape!")
|
||||||
if normalize:
|
if normalize:
|
||||||
vector_l, vector_r = vector_l / np.sqrt((vector_l @ vector_l)), vector_r / np.sqrt(vector_r @ vector_r)
|
vector_l, vector_r = vector_l / np.sqrt((vector_l @ vector_l)), vector_r / np.sqrt(vector_r @ vector_r)
|
||||||
newcontent = [None if len(list(filter(None, np.asarray(item).flatten()))) < self.N ** 2 else np.asarray([vector_l.T @ item @ vector_r]) for item in self.content]
|
newcontent = [None if _check_for_none(self, item) else np.asarray([vector_l.T @ item @ vector_r]) for item in self.content]
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# There are no checks here yet. There are so many possible scenarios, where this can go wrong.
|
# There are no checks here yet. There are so many possible scenarios, where this can go wrong.
|
||||||
|
@ -163,7 +163,7 @@ class Corr:
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
vector_l[t], vector_r[t] = vector_l[t] / np.sqrt((vector_l[t] @ vector_l[t])), vector_r[t] / np.sqrt(vector_r[t] @ vector_r[t])
|
vector_l[t], vector_r[t] = vector_l[t] / np.sqrt((vector_l[t] @ vector_l[t])), vector_r[t] / np.sqrt(vector_r[t] @ vector_r[t])
|
||||||
|
|
||||||
newcontent = [None if (len(list(filter(None, np.asarray(self.content[t]).flatten()))) < self.N ** 2 or vector_l[t] is None or vector_r[t] is None) else np.asarray([vector_l[t].T @ self.content[t] @ vector_r[t]]) for t in range(self.T)]
|
newcontent = [None if (_check_for_none(self, self.content[t]) or vector_l[t] is None or vector_r[t] is None) else np.asarray([vector_l[t].T @ self.content[t] @ vector_r[t]]) for t in range(self.T)]
|
||||||
return Corr(newcontent)
|
return Corr(newcontent)
|
||||||
|
|
||||||
def item(self, i, j):
|
def item(self, i, j):
|
||||||
|
@ -236,7 +236,7 @@ class Corr:
|
||||||
def matrix_symmetric(self):
|
def matrix_symmetric(self):
|
||||||
"""Symmetrizes the correlator matrices on every timeslice."""
|
"""Symmetrizes the correlator matrices on every timeslice."""
|
||||||
if self.N > 1:
|
if self.N > 1:
|
||||||
transposed = [None if len(list(filter(None, np.asarray(G).flatten()))) < self.N ** 2 else G.T for G in self.content]
|
transposed = [None if _check_for_none(self, G) else G.T for G in self.content]
|
||||||
return 0.5 * (Corr(transposed) + self)
|
return 0.5 * (Corr(transposed) + self)
|
||||||
if self.N == 1:
|
if self.N == 1:
|
||||||
raise Exception("Trying to symmetrize a correlator matrix, that already has N=1.")
|
raise Exception("Trying to symmetrize a correlator matrix, that already has N=1.")
|
||||||
|
@ -923,7 +923,7 @@ class Corr:
|
||||||
raise Exception("Addition of Corrs with different shape")
|
raise Exception("Addition of Corrs with different shape")
|
||||||
newcontent = []
|
newcontent = []
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if (self.content[t] is None) or (y.content[t] is None):
|
if _check_for_none(self, self.content[t]) or _check_for_none(y, y.content[t]):
|
||||||
newcontent.append(None)
|
newcontent.append(None)
|
||||||
else:
|
else:
|
||||||
newcontent.append(self.content[t] + y.content[t])
|
newcontent.append(self.content[t] + y.content[t])
|
||||||
|
@ -932,7 +932,7 @@ class Corr:
|
||||||
elif isinstance(y, (Obs, int, float, CObs)):
|
elif isinstance(y, (Obs, int, float, CObs)):
|
||||||
newcontent = []
|
newcontent = []
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if (self.content[t] is None):
|
if _check_for_none(self, self.content[t]):
|
||||||
newcontent.append(None)
|
newcontent.append(None)
|
||||||
else:
|
else:
|
||||||
newcontent.append(self.content[t] + y)
|
newcontent.append(self.content[t] + y)
|
||||||
|
@ -951,7 +951,7 @@ class Corr:
|
||||||
raise Exception("Multiplication of Corr object requires N=N or N=1 and T=T")
|
raise Exception("Multiplication of Corr object requires N=N or N=1 and T=T")
|
||||||
newcontent = []
|
newcontent = []
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if (self.content[t] is None) or (y.content[t] is None):
|
if _check_for_none(self, self.content[t]) or _check_for_none(y, y.content[t]):
|
||||||
newcontent.append(None)
|
newcontent.append(None)
|
||||||
else:
|
else:
|
||||||
newcontent.append(self.content[t] * y.content[t])
|
newcontent.append(self.content[t] * y.content[t])
|
||||||
|
@ -960,7 +960,7 @@ class Corr:
|
||||||
elif isinstance(y, (Obs, int, float, CObs)):
|
elif isinstance(y, (Obs, int, float, CObs)):
|
||||||
newcontent = []
|
newcontent = []
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if (self.content[t] is None):
|
if _check_for_none(self, self.content[t]):
|
||||||
newcontent.append(None)
|
newcontent.append(None)
|
||||||
else:
|
else:
|
||||||
newcontent.append(self.content[t] * y)
|
newcontent.append(self.content[t] * y)
|
||||||
|
@ -979,12 +979,12 @@ class Corr:
|
||||||
raise Exception("Multiplication of Corr object requires N=N or N=1 and T=T")
|
raise Exception("Multiplication of Corr object requires N=N or N=1 and T=T")
|
||||||
newcontent = []
|
newcontent = []
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if (self.content[t] is None) or (y.content[t] is None):
|
if _check_for_none(self, self.content[t]) or _check_for_none(y, y.content[t]):
|
||||||
newcontent.append(None)
|
newcontent.append(None)
|
||||||
else:
|
else:
|
||||||
newcontent.append(self.content[t] / y.content[t])
|
newcontent.append(self.content[t] / y.content[t])
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if newcontent[t] is None:
|
if _check_for_none(self, newcontent[t]):
|
||||||
continue
|
continue
|
||||||
if np.isnan(np.sum(newcontent[t]).value):
|
if np.isnan(np.sum(newcontent[t]).value):
|
||||||
newcontent[t] = None
|
newcontent[t] = None
|
||||||
|
@ -1003,7 +1003,7 @@ class Corr:
|
||||||
|
|
||||||
newcontent = []
|
newcontent = []
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if (self.content[t] is None):
|
if _check_for_none(self, self.content[t]):
|
||||||
newcontent.append(None)
|
newcontent.append(None)
|
||||||
else:
|
else:
|
||||||
newcontent.append(self.content[t] / y)
|
newcontent.append(self.content[t] / y)
|
||||||
|
@ -1014,7 +1014,7 @@ class Corr:
|
||||||
raise Exception('Division by zero will return undefined correlator')
|
raise Exception('Division by zero will return undefined correlator')
|
||||||
newcontent = []
|
newcontent = []
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if (self.content[t] is None):
|
if _check_for_none(self, self.content[t]):
|
||||||
newcontent.append(None)
|
newcontent.append(None)
|
||||||
else:
|
else:
|
||||||
newcontent.append(self.content[t] / y)
|
newcontent.append(self.content[t] / y)
|
||||||
|
@ -1028,7 +1028,7 @@ class Corr:
|
||||||
raise TypeError('Corr / wrong type')
|
raise TypeError('Corr / wrong type')
|
||||||
|
|
||||||
def __neg__(self):
|
def __neg__(self):
|
||||||
newcontent = [None if (item is None) else -1. * item for item in self.content]
|
newcontent = [None if _check_for_none(self, item) else -1. * item for item in self.content]
|
||||||
return Corr(newcontent, prange=self.prange)
|
return Corr(newcontent, prange=self.prange)
|
||||||
|
|
||||||
def __sub__(self, y):
|
def __sub__(self, y):
|
||||||
|
@ -1036,31 +1036,31 @@ class Corr:
|
||||||
|
|
||||||
def __pow__(self, y):
|
def __pow__(self, y):
|
||||||
if isinstance(y, (Obs, int, float, CObs)):
|
if isinstance(y, (Obs, int, float, CObs)):
|
||||||
newcontent = [None if (item is None) else item**y for item in self.content]
|
newcontent = [None if _check_for_none(self, item) else item**y for item in self.content]
|
||||||
return Corr(newcontent, prange=self.prange)
|
return Corr(newcontent, prange=self.prange)
|
||||||
else:
|
else:
|
||||||
raise TypeError('Type of exponent not supported')
|
raise TypeError('Type of exponent not supported')
|
||||||
|
|
||||||
def __abs__(self):
|
def __abs__(self):
|
||||||
newcontent = [None if (item is None) else np.abs(item) for item in self.content]
|
newcontent = [None if _check_for_none(self, item) else np.abs(item) for item in self.content]
|
||||||
return Corr(newcontent, prange=self.prange)
|
return Corr(newcontent, prange=self.prange)
|
||||||
|
|
||||||
# The numpy functions:
|
# The numpy functions:
|
||||||
def sqrt(self):
|
def sqrt(self):
|
||||||
return self**0.5
|
return self ** 0.5
|
||||||
|
|
||||||
def log(self):
|
def log(self):
|
||||||
newcontent = [None if (item is None) else np.log(item) for item in self.content]
|
newcontent = [None if _check_for_none(self, item) else np.log(item) for item in self.content]
|
||||||
return Corr(newcontent, prange=self.prange)
|
return Corr(newcontent, prange=self.prange)
|
||||||
|
|
||||||
def exp(self):
|
def exp(self):
|
||||||
newcontent = [None if (item is None) else np.exp(item) for item in self.content]
|
newcontent = [None if _check_for_none(self, item) else np.exp(item) for item in self.content]
|
||||||
return Corr(newcontent, prange=self.prange)
|
return Corr(newcontent, prange=self.prange)
|
||||||
|
|
||||||
def _apply_func_to_corr(self, func):
|
def _apply_func_to_corr(self, func):
|
||||||
newcontent = [None if (item is None) else func(item) for item in self.content]
|
newcontent = [None if _check_for_none(self, item) else func(item) for item in self.content]
|
||||||
for t in range(self.T):
|
for t in range(self.T):
|
||||||
if newcontent[t] is None:
|
if _check_for_none(self, newcontent[t]):
|
||||||
continue
|
continue
|
||||||
if np.isnan(np.sum(newcontent[t]).value):
|
if np.isnan(np.sum(newcontent[t]).value):
|
||||||
newcontent[t] = None
|
newcontent[t] = None
|
||||||
|
@ -1221,6 +1221,10 @@ def _sort_vectors(vec_set, ts):
|
||||||
|
|
||||||
return sorted_vec_set
|
return sorted_vec_set
|
||||||
|
|
||||||
|
def _check_for_none(corr, entry):
|
||||||
|
"""Checks if entry for correlator corr is None"""
|
||||||
|
return len(list(filter(None, np.asarray(entry).flatten()))) < corr.N ** 2
|
||||||
|
|
||||||
|
|
||||||
def _GEVP_solver(Gt, G0):
|
def _GEVP_solver(Gt, G0):
|
||||||
"""Helper function for solving the GEVP and sorting the eigenvectors.
|
"""Helper function for solving the GEVP and sorting the eigenvectors.
|
||||||
|
|
|
@ -246,7 +246,7 @@ def test_matrix_corr():
|
||||||
corr_mat.Eigenvalue(2, state=0)
|
corr_mat.Eigenvalue(2, state=0)
|
||||||
|
|
||||||
|
|
||||||
def test_projected_none():
|
def test_corr_none_entries():
|
||||||
a = pe.pseudo_Obs(1.0, 0.1, 'a')
|
a = pe.pseudo_Obs(1.0, 0.1, 'a')
|
||||||
l = np.asarray([[a, a], [a, a]])
|
l = np.asarray([[a, a], [a, a]])
|
||||||
n = np.asarray([[None, None], [None, None]])
|
n = np.asarray([[None, None], [None, None]])
|
||||||
|
@ -254,6 +254,12 @@ def test_projected_none():
|
||||||
matr = pe.Corr(x)
|
matr = pe.Corr(x)
|
||||||
matr.projected(np.asarray([1.0, 0.0]))
|
matr.projected(np.asarray([1.0, 0.0]))
|
||||||
|
|
||||||
|
matr * 2 - 2 * matr
|
||||||
|
matr * matr + matr ** 2 / matr
|
||||||
|
|
||||||
|
for func in [np.sqrt, np.log, np.exp, np.sin, np.cos, np.tan, np.sinh, np.cosh, np.tanh]:
|
||||||
|
func(matr)
|
||||||
|
|
||||||
|
|
||||||
def test_GEVP_warnings():
|
def test_GEVP_warnings():
|
||||||
corr_aa = _gen_corr(1)
|
corr_aa = _gen_corr(1)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue