Skip to content

Instantly share code, notes, and snippets.

@naenumtou
Created January 18, 2023 13:09
Show Gist options
  • Save naenumtou/8f972b1d788c62f588601c1d9c578334 to your computer and use it in GitHub Desktop.
Save naenumtou/8f972b1d788c62f588601c1d9c578334 to your computer and use it in GitHub Desktop.
# Define exact generator function
def ExactGenerator(Q):
# Define Gi and Bi
# Gi
GiMatrix = Q.copy()
for i in range(Q.shape[1]):
for j in range(Q.shape[0]):
if i == j: #Diagonal matrix
GiMatrix.iloc[i, j] = abs(GiMatrix.iloc[i, j])
else: #Non-Diagonal matrix
GiMatrix.iloc[i, j] = max(GiMatrix.iloc[i, j], 0)
Gi = GiMatrix.sum(axis = 1)
# Bi
BiMatrix = Q.copy()
for i in range(Q.shape[1]):
for j in range(Q.shape[0]):
if i == j: #Diagonal matrix
BiMatrix.iloc[i, j] = 0
else: #Non-Diagonal matrix
BiMatrix.iloc[i, j] = max(-BiMatrix.iloc[i, j], 0)
Bi = BiMatrix.sum(axis = 1)
# Exact generator
exactQ = Q.copy()
for i in range(Q.shape[1]):
for j in range(Q.shape[0]):
if i != j and Q.iloc[i, j] < 0: #Non-Diagonal matrix
exactQ.iloc[i, j] = 0
else:
exactQ.iloc[i, j] = Q.iloc[i, j] - ((Bi[i] * abs(Q.iloc[i, j])) / Gi[i])
return exactQ.fillna(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment