-
Notifications
You must be signed in to change notification settings - Fork 0
/
some_tools.py
224 lines (157 loc) · 5.77 KB
/
some_tools.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import math
from itertools import product # cartesian product
# break apart a string of ints into a list
def parse(str):
return [int(c) for c in str]
# combine a list of ints into a string
def combine(lst):
string = ""
for c in lst:
string += str(c)
return string
def dot(a, b):
sum = 0
# convert to list of ints if str
if type(a) is str:
a = parse(a)
b = parse(b)
for i in range(len(a)):
sum += (a[i] * b[i])
return sum
def solutions(zs):
# all bit strings that when dotted into z_i, yield 0
temps = []
# intersection of values in temps
sols = []
# all zs will have same length, so use length of first z to
# generate a list of n bits: 0 or 1
bits = [[1,0] for i in range(len(zs[0]))]
states = []
# combine these n bits into 2^n possible states
for state in product(*bits):
# product returns a tuple, could use list(x) to convert to list
states.append(state)
# try to find every combination s.t. z_i * s = 0
for i, z in enumerate(zs):
# add set for current z_i --> nice for finding intersection later
temps.append(set())
zi = parse(z)
for state in states:
if dot(zi, state) % 2 == 0:
temps[i].add(state)
# combine all temp lists into one list
big = []
for temp in temps:
# print(temp)
big.append(temp)
# the solution is contained in the intersection between all possible answers
sols = set.intersection(*big)
return sols
def hidden_dot():
# find all the possible bit strings such that
# the dot product with each value of z is 0 mod 2
# zs = ["011", "110", "010"]
zs = ["001", "111"]
sols = solutions(zs)
print("\nz values:")
for i, z in enumerate(zs):
print(f" {i+1}) {z}")
print("possible values of s:")
for i, sol in enumerate(sols):
sol = combine(sol)
print(f" {i+1}) {sol}")
def binary_to_decimal(bin):
dec = 0
split = ["0", "0"]
# check if binary value contains a fraction
# if not, then just leave the fraction element as 0
if "." in bin:
split = bin.split(".")
else:
split[0] = bin
# reverse order of the integer string to make summation easier
# 2^n + ... + 2^1 + 2^0 "." + 2^-1 + ... + 2^-n
# 2^0 + 2^1 + ... + 2^n "." + 2^-1 + ... + 2^-n
# split = [val[::-1] for val in split]
# list[<start>:<stop>:<step>] - walk though whole thing backwards with step 1
split[0] = split[0][::-1]
# calculate integer part
for i, val in enumerate(split[0]):
dec += (int(val) * 2**i)
# calculate fraction part
for i, val in enumerate(split[1]):
i += 1 # start with 1/2 after decimal
# print()
# print(i, val)
# print(val, " * ", 2**(-i))
dec += (int(val) * 2**(-i))
return dec
def binary_fractions():
bins = ["0.0001101", "0.00011101", "10", "1.1", "1.01"]
decs = [[bin, binary_to_decimal(bin)] for bin in bins]
print("\nresults:")
for [bin, dec] in decs:
print("base-2:", bin)
print("base-10:", dec)
print()
# this could easily be a class where we store the terms a_0,...,a_n in a list
def continued_fractions():
def div(a, b):
# divide b by a and return the result of integer division (floored) and remainder
return (b // a, b % a)
def create_fraction(decimal):
# return a list of terms in decimal's continued fraction
terms = []
# list[<start>:<stop>:<step>] - walk though whole thing backwards with step 1
# so count the number of characters to the right of the decimal
decimal_places = str(decimal)[::-1].find('.')
power_of_10 = 10**decimal_places
# add the first integer part
terms.append(math.trunc(decimal))
# begin expressing
denominator = power_of_10
numerator = decimal * power_of_10
while True:
# swap the numerator and denominator
numerator, denominator = denominator, numerator
if denominator == 0 or denominator == 1:
break
# divide and add the integer to the list of continued fraction terms
# basically, create a mixed fraction with the given items
quotient, remainder = div(denominator, numerator)
# the quotient is the next term
terms.append(int(quotient))
# and the remainder is the numerator
numerator = remainder
return terms
def calculate_convergents(decimal, fraction):
# calculate the convergents of a given continued fraction
print(f"{decimal} convergents: ")
for i in range(len(fraction)):
convergent = 0
for j in range(0, i):
convergent += fraction[j]
print(f" convergent {i}: {convergent}")
# wrong, not a sum -- need to place terms into continued fraction
# simple continued fraction expansion of pi: http://oeis.org/A001203
# decimals = [0.3438, 0.5, 0.6562, 0.8438, 1.61, math.pi, 0.1562]
decimals = [0.3438, 0.5, 0.6562, 0.8438]
fractions = []
for decimal in decimals:
fractions.append(create_fraction(decimal))
for decimal, fraction in zip(decimals, fractions):
print(decimal, fraction)
# for decimal, fraction in zip(decimals, fractions):
# print()
# calculate_convergents(decimal, fraction)
def modular_expansion():
strings = []
# convert it to binary fraction
# create a continued fraction
# approximate s/r
if __name__ == '__main__':
hidden_dot()
# binary_fractions()
# continued_fractions()
# modular_expansion()
print(5 * 0.125 / 2.0)