-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpythonconv_maths.py
More file actions
89 lines (59 loc) · 1.69 KB
/
pythonconv_maths.py
File metadata and controls
89 lines (59 loc) · 1.69 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
import numpy as np
from scipy import signal
##########################################
# Convolution étendue
def convolution(A, M):
return signal.convolve2d(A, M, mode='full')
##########################################
# Matrices aléatoires de taille n x p
n, p = 6, 4
A = np.random.randint(0, 10, size = (n,p))
# Motifs aléatoires de taille 3 x 3
M = np.random.randint(-5,5, size = (3,3))
N = np.random.randint(-5,5, size = (3,3))
##########################################
# Exemple d'associativité
# Calcul de (A*M)*N
AM = convolution(A, M)
AMN1 = convolution(AM, N)
# Calcul de A*(M*N)
MN = convolution(M, N)
AMN2 = convolution(A, MN)
print('A =', A)
print('M =', M)
print('N =', N)
print('(A*M)*N =', AMN1)
print('A*(M*N) =', AMN2)
print("Associativité pour la convolution ?\n", AMN1==AMN2)
##########################################
# Retournement
def retourner(M):
M1 = M.flatten()
M2 = M1[::-1]
M3 = np.reshape(M2,np.shape(M))
return M3
##########################################
# Retournement
def correlation(A, M):
B = convolution(A,retourner(M))
return B
##########################################
# Exemple de non associativité pour la correlation
n, p = 4, 4
A = np.random.randint(0, 10, size = (n,p))
M = np.random.randint(-5,5, size = (3,3))
N = np.random.randint(-5,5, size = (3,3))
##########################################
# Exemple d'associativité
# Calcul de (A*M)*N
AM = correlation(A, M)
AMN1 = correlation(AM, N)
# Calcul de A*(M*N)
MN = correlation(M, N)
AMN2 = correlation(A, MN)
print('A =', A)
print('M =', M)
print('N =', N)
print('(A*M)*N =', AMN1)
print('A*(M*N) =', AMN2)
print("Associativité pour la corrélation ?\n", AMN1==AMN2)