-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClusters_Ising.py
More file actions
45 lines (30 loc) · 1.03 KB
/
Clusters_Ising.py
File metadata and controls
45 lines (30 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
def find_clusters(sample):
N = sample.shape[0]
mask = np.ones(N**2, dtype=bool)
done = set()
todo = set()
cluster_sizes = []
while mask.sum() > 0:
s0 = np.random.choice(np.arange(N**2)[mask])
i0 = s0//N; j0 = s0%N
sign = sample[i0,j0]
todo.add(s0)
count = 1
while len(todo) > 0:
s = todo.pop()
i, j = s//N , s%N
possible = {(1, 0), (-1, 0), (0, 1), (0, -1)}
for (delta_i, delta_j) in possible:
ii = (i+delta_i)%N
jj = (j+delta_j)%N
ss = N*ii + jj
if sample[ii,jj] != sign:
continue
if (ss not in done) and (ss not in todo) and mask[ss]:
todo.add(ss)
count += 1
done.add(s)
mask[s] = False
cluster_sizes.append(count)
return cluster_sizes