-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrover.py
More file actions
162 lines (100 loc) · 4.13 KB
/
Copy pathGrover.py
File metadata and controls
162 lines (100 loc) · 4.13 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import pygame,time,numpy as np
from math import sqrt,cos,sin,asin
from random import randint
from algorithms.data_struct.queue import queue
from algorithms.data_struct.stack import stack
from algorithms.colors import *
pygame.init()
screen_height = 700
screen_width = 1300
screen = pygame.display.set_mode((screen_width,screen_height))
screen.fill((0,0,0))
square_width = 2
space = 1
index_color = (0,200,0)
index2_color = (200,200,0)
numb_color = (50,50,250)
const_color = (255,255,255)
sum_color = (250,250,0)
duplicate_color = (255,0,0)
ground = 450
size_rate = 500
rect_color = []
def print_rect(screen,arr,i,color):
if i >= len(arr): return
pygame.draw.rect( screen , color , (10 + (square_width+space)*i , ground - size_rate*arr[i], square_width , size_rate*arr[i]) )
pygame.display.update()
def display(screen,arr):
pygame.draw.rect(screen,Black,(0,0,screen_width-150,screen_height))
for i in range(len(arr)):
x = 10 + (square_width+space)*i
y = ground - size_rate*arr[i] if arr[i] > 0 else ground+2
square_height = abs(size_rate*arr[i])
pygame.draw.rect( screen , rect_color[i] , (x, y, square_width , square_height) )
pygame.display.update()
def take_possession(color,left,right):
for i in range(left,right+1):
rect_color[i] = color
n = 8
N = 2**n
theta = asin(1/sqrt(N))
fi = theta
initial_amplitude = sin(fi)
rect_color = list(White for _ in range(N))
amplitude = np.array(list(sin(fi) for _ in range(N)))
display(screen,amplitude)
# line of 0
pygame.draw.line(screen,Cyan,(10,ground+1), (10+N*(space+square_width),ground+1) )
pygame.draw.line(screen,Cyan,(10,ground+1-size_rate*(initial_amplitude) - 1), (10+N*(space+square_width),ground+1-size_rate*(initial_amplitude) - 1) )
pygame.display.update()
time.sleep(0.5)
amplitude[N//2] *= -1
display(screen,amplitude)
# line of 0
pygame.draw.line(screen,Cyan,(10,ground+1), (10+N*(space+square_width),ground+1) )
pygame.draw.line(screen,Cyan,(10,ground+1-size_rate*(initial_amplitude) - 1), (10+N*(space+square_width),ground+1-size_rate*(initial_amplitude) - 1) )
pygame.display.update()
fi += 2*theta
amplitude = np.array([cos(fi)/sqrt(N) for _ in range(N)])
amplitude[N//2] = sin(fi)
time.sleep(0.5)
display(screen,amplitude)
# line of 0
pygame.draw.line(screen,Cyan,(10,ground+1), (10+N*(space+square_width),ground+1) )
pygame.draw.line(screen,Cyan,(10,ground+1-size_rate*(initial_amplitude) - 1), (10+N*(space+square_width),ground+1-size_rate*(initial_amplitude) - 1) )
pygame.display.update()
r = int(sqrt(N/2))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #exit pygame,
running = False #exit() program
if r == 0:
time.sleep(1)
r = 0
continue
r-=1
display(screen,amplitude)
# line of 0
pygame.draw.line(screen,Cyan,(10,ground+1), (10+N*(space+square_width),ground+1) )
pygame.draw.line(screen,Cyan,(10,ground+1-size_rate*(initial_amplitude) - 1), (10+N*(space+square_width),ground+1-size_rate*(initial_amplitude) - 1) )
pygame.display.update()
time.sleep(1)
amplitude[N//2] *= -1
amplitude[N//2+5] *= -1
display(screen,amplitude)
# line of 0
pygame.draw.line(screen,Cyan,(10,ground+1), (10+N*(space+square_width),ground+1) )
pygame.draw.line(screen,Cyan,(10,ground+1-size_rate*(initial_amplitude) - 1), (10+N*(space+square_width),ground+1-size_rate*(initial_amplitude) - 1) )
pygame.display.update()
fi += 2*theta
amplitude = np.array([cos(fi)/sqrt(N-1) for _ in range(N)])
amplitude[N//2] = sin(fi)/sqrt(2)
amplitude[N//2+5] = sin(fi)/sqrt(2)
time.sleep(1)
display(screen,amplitude)
# line of 0
pygame.draw.line(screen,Cyan,(10,ground+1), (10+N*(space+square_width),ground+1) )
pygame.draw.line(screen,Cyan,(10,ground+1-size_rate*(initial_amplitude) - 1), (10+N*(space+square_width),ground+1-size_rate*(initial_amplitude) - 1) )
pygame.display.update()