-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconversion.py
103 lines (84 loc) · 3.1 KB
/
conversion.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
from common import *
def main():
#print(ref_to_bin(21))
print(ref_to_h(76695844))
print(h_to_mat(ref_to_h(76695844)))
#print(h_to_mat(ref_to_h(21)))
print("done")
'''
# Conversion between numbers and binary strings
print(bin_to_ref("1010011"))
print(ref_to_bin(83))
print()
# Conversion between binary strings and unreduced h (here, h is in reduced form)
print(bin_to_h_unreduced("0000000001010011"))
print(h_to_bin_unreduced("ABAABB"))
print()
# Conversion between binary strings and unreduced h (here, h is not in reduced form)
print(bin_to_h_unreduced("0000000010001110"))
print(h_to_bin_unreduced("AAABBBA"))
print()
# Conversion between binary strings and reduced h
print(bin_to_h_reduced("0000000010001110")) # This produces something completely different from bin_to_h_unreduced
print(h_to_bin_reduced("ABAABB")) # This is not implemented yet, but from the enumeration below we know it should give 41
# Conversion between h and matrix
print(h_to_mat("ABAABB"))
# Enumeration of reduced h
for i in range(2, 100):
s = bin_to_h_reduced(ref_to_bin(i))
print(str(i).rjust(2)+": "+s)
'''
def h_to_mat(s, mod=0):
mA = A.copy()
mB = B.copy()
if mod > 1:
mA %= mod
mB %= mod
result = I3
for i in range(len(s)):
m = mA if s[i] == "A" else mB
result = result@m
if mod > 1: result %= mod
return result
def ref_to_bin(n):
return np.binary_repr(n)
def bin_to_ref(b):
a = np.array([int(d) for d in list(b)])
return a.dot(2**np.arange(a.size)[::-1])
def bin_to_h_unreduced(b, alphabet=True):
b = b.lstrip("0") # Remove any leading zeroes
if len(b) == 0: return None
b = b[1:] # Remove the leading 1
if alphabet: b = b.replace("0", "A").replace("1", "B")
return b
def h_to_bin_unreduced(s, alphabet=True):
if alphabet: s = s.replace("A", "0").replace("B", "1")
return "1"+s
def ref_to_h(n, alphabet=True):
return bin_to_h_unreduced(ref_to_bin(n), alphabet)
def h_to_ref(s, alphabet=True):
return bin_to_ref(h_to_bin_unreduced(s, alphabet))
def bin_to_h_reduced(b): # Turns out this doesn't work.
b = b.lstrip("0") # Remove any leading zeroes
b = b[1:] # Remove the leading 1
result = ""
i = 0
while i < len(b):
previous = ("xxxxxxxxxxxxxxx"+result)[-15:] # Get the 15 previous factors, substituting x for any empty spots
next_factor = None
if previous[-2:] == "AA": next_factor = "B"
elif previous[-2:] == "BB": next_factor = "A"
elif previous[-7:] == "ABABABA": next_factor = "A"
elif previous[-7:] == "BABABAB": next_factor = "B"
elif previous == "AABBAABBAABBAAB": next_factor = "A"
elif previous == "BBAABBAABBAABBA": next_factor = "B"
if next_factor is None:
result += ("A" if b[i] == "0" else "B")
i += 1
else:
result += next_factor
return result
def h_to_bin_reduced(s):
pass # I'll leave this to Michaela (or future Gabriel) to implement
if __name__ == "__main__":
main()