I am using a numpy array to hold a list of ordered pairs (representing grid coordinates). The algorithm I am writing needs to check if a newly generated ordered pair is already in this array. Below is a schematic of the code:
cluster=np.array([[x1,y1]])
cluster=np.append(cluster,[[x2,y2]],axis=0)
cluster=np.append...etc.
new_spin=np.array([[x,y]])
if new_spin in cluster==False:
do something
The problem with this current code is that it gives false positives. If x or y appear in the cluster, then new_spin in cluster evaluates as true. At first I thought a simple fix would be to ask if x and y appear in cluster, but this would not ensure that they appear as an ordered pair. To make sure they appear as an ordered pair I’d have to find the indices where x and y appear in cluster and compare them, which seems very clunky and inelegant, and I’m certain there must be a better solution out there. However, I have not been able to work it out myself.
Thanks for any help.
Let’s work through an example:
new_spin == clusteris a numpy array of dtypebool. It is True where the value inclusterequals the corresponding value innew_spin.For
new_spinto be “in”cluster, a row of the above boolean array must all be True. We can find such rows by calling theall(axis = 1)method:So
new_spinis “in”cluster, ifanyof the rows is all True:By the way,
np.appendis a very slow operation — slower than Pythonlist.append. Chances are, you will get much better performance if you avoidnp.append. Ifclusteris not too large, you may be better off making cluster a Python list of lists — at least until you are done appending items. Then, if needed, convertclusterto a numpy array withcluster = np.array(cluster).