Reputation: 13
I'm new to Python and I need some help translating the following formulas into Python code:
I am currently working it out with numpy but with little to no progress. Any reference materials will be well appreciated.
Upvotes: 0
Views: 65
Reputation: 22534
Here is one possibility. Some of the assumptions are in the comments. There are other ways to do this, of course. The size of the set in the denominator of the second formula could be done more simply as the length of a set or list, but my way avoids the memory usage of the set/list and is more consistent with the numerator.
def formula1(X, n, a, b):
"""Return the first formula for matrix X, size n, and indices a and b.
"""
return sum(X[a][t] - X[b][t] for t in range(1, n+1)) / n
def formula2(X, n, i, j, x, y, a, P):
"""Return the second formula for matrix X, size n, indices i, j, x, and y,
array or mapping a, array or mapping of sets P.
"""
numer = sum(abs(X[i][t] - X[j][t])
for t in range(1, n+1)
if a[t] in P[x] or a[t] in P[y])
denom = sum(1
for t in range(1, n+1)
if a[t] in P[x] or a[t] in P[y])
return numer / denom
Upvotes: 1