-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLetters.py
executable file
·119 lines (93 loc) · 3.19 KB
/
Letters.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import numpy
from numpy import *
from pylab import *
LetterA = array( [ [1, 1, 1, 1, 1, -1, -1, -1, -1, -1],
[1, 1, 1, 1, 1, 1, 1, -1, -1, -1],
[-1, -1, -1, 1, 1, 1, 1, 1, -1, -1],
[-1, -1, -1, -1, 1, 1, -1, 1, 1, -1],
[-1, -1, -1, -1, 1, 1, -1, -1, 1, 1],
[-1, -1, -1, -1, 1, 1, -1, -1, 1, 1],
[-1, -1, -1, -1, 1, 1, -1, 1, 1, -1],
[-1, -1, -1, 1, 1, 1, 1, 1, -1, -1],
[1, 1, 1, 1, 1, 1, 1, -1, -1, -1],
[1, 1, 1, 1, 1, -1, -1, -1, -1, -1] ])
DamagedA = array( [ [1, 1, 1, 1, 1, -1, -1, -1, -1, -1],
[1, 1, 1, 1, 1, 1, 1, -1, -1, -1],
[-1, -1, -1, 1, 1, 1, 1, 1, -1, -1],
[-1, -1, -1, -1, 1, 1, -1, 1, 1, -1],
[-1, -1, -1, -1, 1, 1, -1, -1, 1, 1],
[-1, -1, -1, -1, 1, 1, -1, -1, 1, 1],
[-1, -1, -1, -1, 1, 1, -1, 1, 1, -1],
[-1, -1, -1, 1, 1, 1, 1, 1, -1, -1],
[1, 1, 1, 1, 1, 1, 1, -1, -1, -1],
[1, 1, 1, 1, 1, -1, -1, -1, -1, -1] ])
L = len(LetterA[0])
N = L**2
def print_config(config):
for i in range(L):
for j in range(L):
if (config[i, j] == -1):
print('+'),
else:
print(' '),
print('')
def damage(n, damaged):
for i in range(n):
damaged[int(L*numpy.random.random(1)), int(L*numpy.random.random(1))] *= -1
damage(50, DamagedA)
#print_config(LetterA)
#print_config(DamagedA)
Jarray = zeros((L, L, L, L))
for i in range(L):
for j in range(L):
for k in range(L):
for l in range(L):
Jarray[i, j, k, l] = LetterA[i, j]*LetterA[k, l]
RandomConfiguration = numpy.random.random([L, L])
for i in range(L):
for j in range(L):
if RandomConfiguration[i, j] < .5:
RandomConfiguration[i, j] = -1
else:
RandomConfiguration[i, j] = 1
def monteCarloStep(array): # nonweave version
randomArray = numpy.random.random([L, L, L, L]) # random number for Metropolis algorithm
for i in range(L):
for j in range(L):
dE = 0
for k in range(L):
for l in range(L):
dE += 2*Jarray[i, j, k, l]*array[i, j]*array[k, l]
if dE < 0: #or exp(-dE/1.0) > randomArray[i, j, k, l])
array[i, j] *= -1
def plot(array):
x = indices((L, L))[0,:,:]
y = indices((L, L))[1,:,:]
color = []
for i in range(L):
for j in range(L):
if array[i, j] == 1:
color.append([0.0, 0.0, 1.0])
else:
color.append([1.0, 1.0, 1.0])
scatter(x.ravel(), y.ravel(), s=30.0, c=color, edgecolors='none')
for i in range(100):
if i == 0:
figure(0)
plot(DamagedA)
figure(4)
plot(LetterA)
monteCarloStep(DamagedA)
if i == 0:
figure(1)
plot(DamagedA)
#print_config(Configuration)
if i == 1:
figure(2)
plot(DamagedA)
#print_config(Configuration)
if i == 50:
figure(3)
plot(DamagedA)
#print_config(Configuration)
show()