mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-15 12:03:42 +02:00
linting tested again
This commit is contained in:
parent
2f11d0d30b
commit
909ef85ff8
1 changed files with 65 additions and 87 deletions
|
@ -123,13 +123,13 @@ class Corr:
|
|||
raise Exception("Vectors are of wrong shape!")
|
||||
if normalize:
|
||||
vector_l, vector_r = vector_l / np.sqrt((vector_l @ vector_l)), vector_r / np.sqrt(vector_r @ vector_r)
|
||||
#if (not (0.95 < vector_r @ vector_r < 1.05)) or (not (0.95 < vector_l @ vector_l < 1.05)):
|
||||
#print("Vectors are normalized before projection!")
|
||||
# if (not (0.95 < vector_r @ vector_r < 1.05)) or (not (0.95 < vector_l @ vector_l < 1.05)):
|
||||
# print("Vectors are normalized before projection!")
|
||||
|
||||
newcontent = [None if (item is None) else np.asarray([vector_l.T @ item @ vector_r]) for item in self.content]
|
||||
|
||||
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.
|
||||
if normalize:
|
||||
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])
|
||||
|
@ -286,18 +286,18 @@ class Corr:
|
|||
|
||||
def wrap(i):
|
||||
if i >= self.T:
|
||||
return i-self.T
|
||||
return i - self.T
|
||||
return i
|
||||
|
||||
for t in range(self.T):
|
||||
for i in range(N):
|
||||
for j in range(N):
|
||||
if periodic:
|
||||
new_content[t][i, j] = self.content[wrap(t+i+j)][0]
|
||||
elif (t+i+j) >= self.T:
|
||||
new_content[t]=None
|
||||
new_content[t][i, j] = self.content[wrap(t + i + j)][0]
|
||||
elif (t + i + j) >= self.T:
|
||||
new_content[t] = None
|
||||
else:
|
||||
new_content[t][i, j] = self.content[t+i+j][0]
|
||||
new_content[t][i, j] = self.content[t + i + j][0]
|
||||
|
||||
return Corr(new_content)
|
||||
|
||||
|
@ -313,7 +313,7 @@ class Corr:
|
|||
|
||||
def reverse(self):
|
||||
"""Reverse the time ordering of the Corr"""
|
||||
return Corr(self.content[::-1])
|
||||
return Corr(self.content[:: -1])
|
||||
|
||||
def correlate(self, partner):
|
||||
"""Correlate the correlator with another correlator or Obs
|
||||
|
@ -335,7 +335,7 @@ class Corr:
|
|||
new_content.append(None)
|
||||
else:
|
||||
new_content.append(np.array([correlate(o, partner.content[x0][0]) for o in t_slice]))
|
||||
elif isinstance(partner, Obs): # Should this include CObs?
|
||||
elif isinstance(partner, Obs): # Should this include CObs?
|
||||
new_content.append(np.array([correlate(o, partner) for o in t_slice]))
|
||||
else:
|
||||
raise Exception("Can only correlate with an Obs or a Corr.")
|
||||
|
@ -676,18 +676,15 @@ class Corr:
|
|||
def __repr__(self, range=[0, None]):
|
||||
content_string = ""
|
||||
|
||||
content_string+="Corr T="+str(self.T)+" N="+str(self.N) +"\n"#+" filled with"+ str(type(self.content[0][0])) there should be a good solution here
|
||||
|
||||
|
||||
content_string += "Corr T=" + str(self.T) + " N=" + str(self.N) + "\n" # +" filled with"+ str(type(self.content[0][0])) there should be a good solution here
|
||||
|
||||
if self.tag is not None:
|
||||
content_string += "Description: " + self.tag + "\n"
|
||||
if self.N!=1:
|
||||
if self.N != 1:
|
||||
return content_string
|
||||
# This avoids a crash for N>1. I do not know, what else to do here. I like the list representation for N==1. We could print only one "smearing" or one matrix. Printing everything will just
|
||||
# be a wall of numbers.
|
||||
|
||||
|
||||
if range[1]:
|
||||
range[1] += 1
|
||||
content_string += 'x0/a\tCorr(x0/a)\n------------------\n'
|
||||
|
@ -837,7 +834,7 @@ class Corr:
|
|||
return Corr(newcontent, prange=self.prange)
|
||||
|
||||
def _apply_func_to_corr(self, func):
|
||||
newcontent = [None if (item is None ) else func(item) for item in self.content]
|
||||
newcontent = [None if (item is None) else func(item) for item in self.content]
|
||||
for t in range(self.T):
|
||||
if newcontent[t] is None:
|
||||
continue
|
||||
|
@ -902,7 +899,7 @@ class Corr:
|
|||
if isinstance(obs_OR_cobs, CObs):
|
||||
return obs_OR_cobs.real
|
||||
else:
|
||||
return obs_OR_cobs
|
||||
return obs_OR_cobs
|
||||
|
||||
return self._apply_func_to_corr(return_real)
|
||||
|
||||
|
@ -912,72 +909,53 @@ class Corr:
|
|||
if isinstance(obs_OR_cobs, CObs):
|
||||
return obs_OR_cobs.imag
|
||||
else:
|
||||
return obs_OR_cobs*0 # So it stays the right type
|
||||
return obs_OR_cobs * 0 # So it stays the right type
|
||||
|
||||
return self._apply_func_to_corr(return_imag)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def sort_vectors(vec_set, ts): # Helper function used to find a set of Eigenvectors consistent over all timeslices
|
||||
reference_sorting=np.array(vec_set[ts])
|
||||
N=reference_sorting.shape[0]
|
||||
sorted_vec_set=[]
|
||||
def sort_vectors(vec_set, ts): # Helper function used to find a set of Eigenvectors consistent over all timeslices
|
||||
reference_sorting = np.array(vec_set[ts])
|
||||
N = reference_sorting.shape[0]
|
||||
sorted_vec_set = []
|
||||
for t in range(len(vec_set)):
|
||||
if vec_set[t] is None:
|
||||
sorted_vec_set.append(None)
|
||||
elif not t==ts:
|
||||
perms=permutation([i for i in range(N)])
|
||||
best_score=0
|
||||
elif not t == ts:
|
||||
perms = permutation([i for i in range(N)])
|
||||
best_score = 0
|
||||
for perm in perms:
|
||||
current_score=1
|
||||
current_score = 1
|
||||
for k in range(N):
|
||||
new_sorting=reference_sorting.copy()
|
||||
new_sorting[perm[k],:]=vec_set[t][k]
|
||||
new_sorting = reference_sorting.copy()
|
||||
new_sorting[perm[k], :] = vec_set[t][k]
|
||||
current_score *= abs(np.linalg.det(new_sorting))
|
||||
if current_score>best_score:
|
||||
best_score=current_score
|
||||
best_perm=perm
|
||||
#print("best perm", best_perm)
|
||||
if current_score > best_score:
|
||||
best_score = current_score
|
||||
best_perm = perm
|
||||
# print("best perm", best_perm)
|
||||
sorted_vec_set.append([vec_set[t][k] for k in best_perm])
|
||||
else:
|
||||
sorted_vec_set.append(vec_set[t])
|
||||
|
||||
|
||||
return sorted_vec_set
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def permutation(lst): # Shamelessly copied
|
||||
def permutation(lst): # Shamelessly copied
|
||||
if len(lst) == 1:
|
||||
return [lst]
|
||||
l = []
|
||||
ll = []
|
||||
for i in range(len(lst)):
|
||||
m = lst[i]
|
||||
remLst = lst[:i] + lst[i+1:]
|
||||
remLst = lst[:i] + lst[i + 1:]
|
||||
# Generating all permutations where m is first
|
||||
for p in permutation(remLst):
|
||||
l.append([m] + p)
|
||||
return l
|
||||
ll.append([m] + p)
|
||||
return ll
|
||||
|
||||
|
||||
def GEVP_solver(Gt,G0): # Just so normalization an sorting does not need to be repeated. Here we could later put in some checks
|
||||
def GEVP_solver(Gt, G0): # Just so normalization an sorting does not need to be repeated. Here we could later put in some checks
|
||||
sp_val, sp_vecs = scipy.linalg.eig(Gt, G0)
|
||||
sp_vecs=[sp_vecs[:, np.argsort(sp_val)[-i]]for i in range(1,sp_vecs.shape[0]+1) ]
|
||||
sp_vecs=[v/np.sqrt((v.T@G0@v)) for v in sp_vecs]
|
||||
sp_vecs = [sp_vecs[:, np.argsort(sp_val)[-i]] for i in range(1, sp_vecs.shape[0] + 1)]
|
||||
sp_vecs = [v / np.sqrt((v.T @ G0 @ v)) for v in sp_vecs]
|
||||
return sp_vecs
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue