-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm_order.py
More file actions
74 lines (63 loc) · 2.01 KB
/
m_order.py
File metadata and controls
74 lines (63 loc) · 2.01 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
import itertools
def generate_matrices(arr, p):
matrices = {}
for a, b, c, d in itertools.product(arr, repeat=4):
det = (a*d - b*c) % p
if det == 1:
matrix = [[a, b], [c, d]]
# get the dimensions of the matrix
rows, cols = len(matrix), len(matrix[0])
# calculate the powers of the matrix until it equals the identity matrix
n = 1
identity = [[1, 0], [0, 1]]
while not matrix_equals(matrix_power(matrix, n, p), identity):
n += 1
matrices[str(matrix)] = n
return matrices
def matrix_multiply(A, B, p):
rows_A, cols_A = len(A), len(A[0])
rows_B, cols_B = len(B), len(B[0])
assert cols_A == rows_B
C = [[0] * cols_B for _ in range(rows_A)]
for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
C[i][j] += A[i][k] * B[k][j]
C[i][j] %= p
return C
def matrix_power(A, n, p):
if n == 0:
return [[1, 0], [0, 1]]
elif n == 1:
return A
else:
half = matrix_power(A, n // 2, p)
if n % 2 == 0:
return matrix_multiply(half, half, p)
else:
return matrix_multiply(matrix_multiply(half, half, p), A, p)
def matrix_equals(A, B):
rows_A, cols_A = len(A), len(A[0])
rows_B, cols_B = len(B), len(B[0])
if rows_A != rows_B or cols_A != cols_B:
return False
for i in range(rows_A):
for j in range(cols_A):
if A[i][j] != B[i][j]:
return False
return True
p=23
arr = [x for x in range(p)]
m = generate_matrices(arr,p)
matrix_list = list(m.keys())
print(m)
print("\n\n")
count_dict = {}
numbers = list(m.values())
for i in range(1,max(numbers)+1):
count_dict[i] = numbers.count(i)
print("Order no of elements")
print("\n")
for key,value in count_dict.items():
print(key," ",value)
print("\n")