As the title says, I actually use a triple for a loop to process a numpy matrix called A in my python program, and as we can expect, it is very slow. I have a function, which we will call "return_bool", which returns a logical value in the function of the parameter h, and I and j are the index of the matrix element. I want to use this function for several h values ββto make the type of "gradient matrix". Here is my code:
A = np.zero(iindex,jindex)
for h in hvalue:
for in iindex:
for j in jindex:
if (return_bool(h,i,j) : A[i][j] =+ 1
Are there any tips for increasing the scanning speed of this matrix?
Here is the code for my return_bool function (all values ββare float or dots (x, y) with dots x and y):
def inclu_geo(coord1,coord2,y1,y2 , hh , hh2,y1droite,y2droite, hhdroite , hh2droite,intersec1,intersec2,rho):
global y
global yprime
global largeur_pale
equdroite1 = eqdroite(y1,y2,hh,hh2)
equdroite2 = eqdroite(y1droite,y2droite,hhdroite,hh2droite)
if 0< rho < 90 :
if (intersec1!=(0,0) or intersec2!=(0,0))and(inclu(intersec1[0],intersec1[1]) or inclu(intersec2[0],intersec2[1])):
if inclu(intersec1[0],intersec1[1]):
b = ((y<=coord1<=y1)and(hh2<=coord2<=hh))or((intersec1[0]<=coord1<=yprime)and(hh2<=coord2<=intersec1[1]))or((y1<=coord1<=intersec1[0])and(hh2<=coord2<=(equdroite1[0]*coord1+equdroite1[1])))
if inclu(intersec2[0],intersec2[1]):
b = ((y<=coord1<=intersec2[0])and(hh2droite<=coord2<=intersec2[1]))or((y1droite<=coord1<=yprime)and(hh2droite<=coord2<=hhdroite))or((intersec2[0]<=coord1<=y1droite)and(hh2<=coord2<=(equdroite2[0]*coord1+equdroite2[1])))
else:
if (hh != 0) and inclu(y1,hh):
b = ((y<=coord1<=y1)and(hh2<=coord2<=hh))or((y1<=coord1<=y2)and(hh2<=coord2<=(equdroite1[0]*coord1+equdroite1[1])))
elif (hhdroite != 0) and inclu(y1droite,hhdroite):
b = ((y1droite<=coord1<=yprime)and(hh2droite<=coord2<=hhdroite))or((y2droite<=coord1<=y1droite)and(hh2<=coord2<=(equdroite2[0]*coord1+equdroite2[1])))
elif (hhdroite != 0) or (hh != 0):
b = True
else:
b = False
else:
if (intersec1!=(0,0) or intersec2!=(0,0))and(inclu(intersec1[0],intersec1[1]) or inclu(intersec2[0],intersec2[1])):
if inclu(intersec1[0],intersec1[1]):
b = ((y<=coord1<=y1)and(hh<=coord2<=hh2))or((intersec1[0]<=coord1<=yprime)and(intersec1[1]<=coord2<=hh2))or((y1<=coord1<=intersec1[0])and((equdroite1[0]*coord1+equdroite1[1])<=coord2<=hh2))
if inclu(intersec2[0],intersec2[1]):
b = ((y<=coord1<=intersec2[0])and(intersec2[1]<=coord2<=hh2droite))or((y1droite<=coord1<=yprime)and(hhdroite<=coord2<=hh2droite))or((intersec2[0]<=coord1<=y1droite)and((equdroite2[0]*coord1+equdroite2[1])<=coord2<=hh2))
else:
if (hh != largeur_pale) and inclu(y1,hh):
b = ((y<=coord1<=y1)and(hh<=coord2<=hh2))or((y1<=coord1<=y2)and((equdroite1[0]*coord1+equdroite1[1])<=coord2<=hh2))
elif (hhdroite != largeur_pale) and inclu(y1droite,hhdroite):
b = ((y1droite<=coord1<=yprime)and(hhdroite<=coord2<=hh2droite))or((y2droite<=coord1<=y1droite)and((equdroite2[0]*coord1+equdroite2[1])<=coord2<=hh2))
elif (hhdroite != largeur_pale) or (hh != largeur_pale):
b = True
else:
b = False
return b