-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbondperc.py
More file actions
60 lines (53 loc) · 1.57 KB
/
bondperc.py
File metadata and controls
60 lines (53 loc) · 1.57 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import numpy as np
import matplotlib.pyplot as plt
N = 40
p = 0.3
def createVertices(p, N):
mat = np.zeros((N, N))
for x in np.nditer(mat, op_flags=['readwrite']):
if np.random.uniform() < p:
x[...] = -1
return mat
# analyse a cell
def analyse(val, r, c, mat, cluster):
val = mat[r, c]
if (mat[r, c] == (-1)): # if vertex is open
val = mat[r, c]
mat[r, c] = cluster
if c < (N-1) and mat[r,c + 1] == (-1):
#print '[{}, {}]: up exists'.format(r, c)
analyse(val, r, c+1, mat, cluster)
if c > (0) and mat[r,c - 1] == (-1):
#print '[{}, {}]: down exists'.format(r, c)
analyse(val, r, c-1, mat, cluster)
if r < (N-1) and mat[r + 1,c] == (-1):
#print '[{}, {}]: right exists'.format(r, c)
analyse(val, r+1, c, mat, cluster)
if r > (0) and mat[r - 1,c] == (-1):
#print '[{}, {}]: left exists'.format(r, c)
analyse(val, r-1, c, mat, cluster)
else:
return mat
else:
return mat
# init
M = createVertices(p, N)
cl = 1
# compute
for row in xrange(M.shape[0]):
for col in xrange(M.shape[1]):
if M[row,col] == (-1): # if vertex is open
M = analyse(M[row,col], row, col, M, cl)
cl += 1
plt.imshow(np.transpose(M), cmap='Accent', interpolation='nearest')
plt.show()
# make sure there are no consecutive cells with different cluster
test = []
for row in xrange(M.shape[0]-1):
for col in xrange(M.shape[1]-1):
if M[row, col] != 0:
if M[row+1, col] != 0:
test.append(M[row, col] != M[row+1, col])
if M[row, col+1] != 0:
test.append(M[row, col] != M[row, col+1])
print '{}/{}={}'.format(sum(test), len(test), float(sum(test))/len(test))