I am working on a program in Python that, right now, goes row by row through a 2D numpy array and looks for an identical row in a different array. If it finds a duplicate it will run a small bit of code using that index of the first array.
This works fine and is efficient enough when the arrays are small (~2x500 and 2x500) but quickly becomes inefficient for longer arrays. I am wondering if someone knows of a method using numpy (I am currently using other numpy features elsewhere so it would be best to not have to change data types), or perhaps something else that would be more efficient. I am sure there is something faster than two for loops through the arrays. Thanks in advance.
import random
import numpy as np
N = 1000
speed = 50
longueur = 20000
largeur = 30000
quadrillage = 50
p= 0.8
def stick():
u = random.random()
if u <p:
a = 1 #The particle is stuck
else:
a =0 #The particle did not stick, it will instead bounce
return a
obstacle_number =2000
maxstuck = 4
numbstuck = np.zeros((obstacle_number))
spacinglarg = largeur/quadrillage
spacinglong = longueur/quadrillage
obs0 = np.random.randint(0, spacinglarg,(obstacle_number,1)) *quadrillage
obs1 = np.random.randint(0, spacinglong,(obstacle_number,1)) *quadrillage
obs = np.concatenate([obs0,obs1], axis =1)
s=(N,2)
global A
A = np.zeros(s)
for i in range (0,N):
a = i*longueur/N
b = 50
A[i,0]= b
A[i,1]= a
T = 50*np.round(A/(50))
B=np.zeros(s)
tp = 2*np.pi
for i in range(0,nombre_atomes):
aa = random.randint(0,360)/tp
B[i,0]=np.cos(aa)*speed
B[i,1]=np.sin(aa)*speed
for i in range(0, N):
for j in range(0,len(obs)):
if T[i,0] == obs[j,0] and T[i,1] == obs[j,1]:
if numbstuck[j] <= maxstuck and abs(B[i,0]) != 0:
sss= stick()
if sss == 1: #if it sticks
B[i,0]=0
B[i,1]=0
numbstuck[j] += 1
else:
B[i,0]=-B[i,0]
B[i,1]=-B[i,1]