I am new to Python and I wrote the following code to generate some matrix A. Is there a way to improve the performance of this function? Can I avoid some or all of the for-loops and if-statements?
import numpy as np
# input: some other matrix B and a threshold epsilon
def generate_A(epsilon, n, B):
A = np.zeros((n,n))
for v in range(n):
for w in [x for x in range(n) if x != v]:
if B[v,w] >= B[w,v]:
A[w,v] = 1
for i in range(n):
for j in range(n):
if i != j:
for k in [x for x in range(n) if x != i and x !=j]:
if np.abs(B[i,j] - B[i,k] * B[k,j]) < epsilon:
if A[i,j] == 0 and A [i,k] * A[k,j] == 1:
A[i,j] = 1
if A[k,i] == 0 and A[k,j] * A[j,i] == 1:
A[k,i] = 1
if A[j,k] == 0 and A[j,i] * A[i,k] == 1:
A[j,k] = 1
return(A)
Aucun commentaire:
Enregistrer un commentaire