-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathposition.py
More file actions
205 lines (151 loc) · 4.25 KB
/
Copy pathposition.py
File metadata and controls
205 lines (151 loc) · 4.25 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Plongement positionnel
import numpy as np
import matplotlib.pyplot as plt
n = 10 # dimension du plongement
K = 4 # nombre de mots à plonger
p = 100 # nombre de divisions
### Partie A : Plongement positionnel
def period_omega(j, n, p=100):
return 1/np.power(p, 2*j/n)
def position_matrix(K, n, p=100):
P = np.zeros((K, n))
for k in range(K):
for j in range(n//2):
omega = period_omega(j, n, p=p)
P[k, 2*j] = np.cos(k*omega)
P[k, 2*j+1] = np.sin(k*omega)
return P
# Test
def exemple_position():
P = position_matrix(K=4, n=6, p=100)
print(P.shape)
print("n =", n, "K =", K)
print("Matrice des plongements positionnels (transposée) :")
print(P.T)
return
exemple_position()
def exemple_position_bis():
P = position_matrix(K=4, n=6, p=100)
print("n =", n, "K =", K)
for k in range(K):
print(f"Plongement de la position {k} :")
print(P[k, :])
return
exemple_position_bis()
### Partie B : Position relative et rotation
def rotation_matrix(omega):
""" Matrice de rotation d'angle omega """
return np.array([[np.cos(omega), -np.sin(omega)],
[np.sin(omega), np.cos(omega)]])
def blocks_rotation_matrix(l, K, n, p=100):
""" Matrice de rotation ayant des blocs de taille 2 correspondant à un décalage de longueur l """
R = np.zeros((n, n))
for j in range(n//2):
omega = period_omega(j, n, p=p)
R[2*j:2*j+2, 2*j:2*j+2] = rotation_matrix(l*omega)
return R
# Test
def exemple_rotation():
P = position_matrix(K=4, n=4, p=100)
l = 2
R = blocks_rotation_matrix(l=l, K=4, n=4, p=100)
# print(R.shape)
print("Matrice de rotation par blocs :")
print(R)
k = 0
Pk = P[k, :]
Pl = P[k+l, :]
Pll = R @ Pk
print("Vecteur position au rang k + l")
print(" Valeur directe : ", Pl)
print(" Par rotation : ", Pll)
return
exemple_rotation()
### Partie C : Visualisation
def plot_position_matrix(P):
""" Visualisation de la matrice de position """
plt.figure(figsize=(10, 10))
plt.imshow(P, cmap='hot', interpolation='nearest')
plt.xticks([])
plt.yticks([])
plt.tight_layout()
plt.savefig("position-matrix.png", dpi=600)
plt.show()
return
P = position_matrix(K=100, n=100, p=10000)
plot_position_matrix(P.T)
def plot_kcst():
""" Visualisation des fonctions avec k constant """
p = 10000
n = 100
plt.figure(figsize=(10, 10))
X = np.linspace(0, 20, 200)
# Affichage sur des sous-graphes
lmax = 5
for l in range(0, lmax):
plt.subplot(lmax, 1, l+1)
k = 10*l
Omega = 1/np.power(p, 2*X/n)
plt.plot(X, np.sin(k*Omega))
plt.xticks([])
plt.yticks([])
plt.title(f"k = {k}")
plt.tight_layout()
# plt.savefig("position-kcst.png", dpi=600)
plt.show()
return
plot_kcst()
def plot_icst():
""" Visualisation des fonctions avec i constant """
p = 10000
n = 100
plt.figure(figsize=(10, 10))
X = np.linspace(0, 50, 200)
# Affichage sur des sous-graphes
imax = 5
for i in range(0, imax):
plt.subplot(imax, 1, i+1)
omega = 1/np.power(p, 2*i/n)
plt.plot(X, np.cos(X*omega))
plt.xticks([])
plt.yticks([])
plt.title(f"i = {i}")
plt.tight_layout()
# plt.savefig("position-icst.png", dpi=600)
plt.show()
return
plot_icst()
def plot_surface():
""" Visualisation de la surface (x,y) -> sin(x/y) """
cX = np.linspace(0.05, 3, 100)
cY = np.linspace(0.05, 3, 100)
X, Y = np.meshgrid(cX, cY)
Z = np.sin(X/Y)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(X, Y, Z, cmap='viridis')
plt.show()
# plt.savefig("position-surface.png", dpi=600)
return
plot_surface()
### Annexe : Motivation
def digits(k, b):
""" Retourne les chiffres de n en base b """
liste_d = []
i = 0
while True:
d = (k // b**i) % b
if b**i > k:
break
i += 1
liste_d.append(d)
liste_d.reverse()
return liste_d
# test
def exemple_digits():
k = 23
b = 2
print("A la main :", digits(k, b))
print("Python :", bin(k))
return
exemple_digits()