/
mathobjects.py
executable file
·345 lines (289 loc) · 9.23 KB
/
mathobjects.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 4 16:47:27 2021
@author: Kirby Urner
"""
from math import gcd
from random import shuffle
from string import ascii_lowercase # all lowercase letters
from nums import invmod
class Rat:
def __init__(self, numer, denom=1):
"""Simplify to lowest terms upon creation"""
common = gcd(numer, denom)
self.numer = numer // common
self.denom = denom // common
def __mul__(self, other):
if type(other) == int:
other = Rat(other, 1)
return Rat(self.numer * other.numer, self.denom * other.denom)
__rmul__ = __mul__
def __truediv__(self, other):
"""Multiply by the multiplicative inverse (reciprocal)"""
return self * ~other
def __invert__(self):
"""Flip Over, Reciprocal: self * ~self == (1/1)"""
return Rat(self.denom, self.numer)
def __add__(self, other):
if type(other) == int:
other = Rat(other)
return Rat(
(self.numer * other.denom) + (other.numer * self.denom),
self.denom * other.denom)
__radd__ = __add__
def __sub__(self, other):
"""Add the additive inverse"""
return self + (-other)
__rsub__ = __sub__
def __neg__(self):
"Additive inverse"
return Rat(-self.numer, self.denom)
def __pow__(self, n: int):
if type(n) != int:
raise TypeError
if n == 0:
return Rat(1, 1)
if n == 1:
return Rat(self.numer, self.denom)
if n < 0:
result = ~self
n = abs(n)
for _ in range(n-1):
result = result * ~self
return result
else:
result = self
for _ in range(n-1):
result = result * self
return result
def __repr__(self):
if self.denom == 1:
return f"({self.numer})"
return f"({self.numer}/{self.denom})"
class Mod:
_modulus = 12
def __init__(self, n):
self.value = n % self._modulus
def __eq__(self, other):
return self.value == other.value % self._modulus
def __repr__(self):
return "({} mod {})".format(self.value, self._modulus)
def __add__(self, other):
return type(self)(self.value + other.value)
def __mul__(self, other):
return type(self)(self.value * other.value)
def __truediv__(self, other):
return self * ~other
def __neg__(self):
return type(self)(-self.value)
def __sub__(self, other):
return self + -other
def __invert__(self):
return invmod(self.value, self._modulus)
#for i in range(1, self._modulus):
# if self * Mod(i) == Mod(1):
# return Mod(i)
def __pow__(self, n):
if n < 0:
value = (~self).value
n = abs(n)
else:
value = self.value
return type(self)(pow(value, n, self._modulus))
def __hash__(self):
return self.value
class P:
"""
A Permutation
self._code: a dict, is a mapping of iterable elements
to themselves in any order.
"""
def __init__(self,
the_code = None, # direct inject
inv_table = None, # construct
iterable = ascii_lowercase + ' '): # default domain
"""
start out with Identity, or directly inject the mapping as
a dict or use an inversions table to construct the permutation
"""
if the_code:
self._code = the_code
elif inv_table:
values = []
for key in sorted(inv_table, reverse=True):
if inv_table[key] >= len(values):
values.append(key)
else:
values.insert(inv_table[key], key)
self._code = dict(zip(sorted(inv_table), values))
elif iterable:
try:
# create two iterators for zipping together
iter1 = iter(iterable)
iter2 = iter(iterable)
except:
raise TypeError
self._code = dict(zip(iter1, iter2))
def shuffle(self):
"""
return a random permutation of this permutation
"""
# use shuffle
# something like
the_keys = list(self._code.keys()) # grab keys
shuffle(the_keys) # shuffles other one
newP = P()
newP._code = dict(zip(self._code.keys(), the_keys))
return newP
def encrypt(self, plain):
"""
turn plaintext into cyphertext using self._code
"""
output = "" # empty string
for c in plain:
output = output + self._code.get(c, c)
return output
def decrypt(self, cypher):
"""
Turn cyphertext into plaintext using ~self
"""
reverse_P = ~self # invert me!
output = ""
for c in cypher:
output = output + reverse_P._code.get(c, c)
return output
def __getitem__(self, key):
return self._code.get(key, None)
def __repr__(self):
return "P class: " + str(tuple(self._code.items())[:3]) + "..."
def cyclic(self):
"""
cyclic notation, a compact view of a group
"""
output = []
the_dict = self._code.copy()
while the_dict:
start = tuple(the_dict.keys())[0]
the_cycle = [start]
the_next = the_dict.pop(start)
while the_next != start:
the_cycle.append(the_next)
the_next = the_dict.pop(the_next)
output.append(tuple(the_cycle))
return tuple(output)
def __mul__(self, other):
"""
look up my keys to get values that serve
as keys to get others "target" values
"""
new_code = {}
for c in self._code: # going through my keys
target = other._code[ self._code[c] ]
new_code[c] = target
new_P = P(' ')
new_P._code = new_code
return new_P
def __truediv__(self, other):
return self * ~other
def __pow__(self, exp):
"""
multiply self * self the right number of times
"""
if exp == 0:
output = P()
else:
output = self
for x in range(1, abs(exp)):
output *= self
if exp < 0:
output = ~output
return output
def __call__(self, s):
"""
another way to encrypt
"""
return self.encrypt(s)
def __invert__(self):
"""
create new P with reversed dict
"""
newP = P(' ')
newP._code = dict(zip(self._code.values(), self._code.keys()))
return newP
def __eq__(self, other):
"""
are these permutation the same?
Yes if self._cod==e other._code
"""
return self._code == other._code
def inversion_table(self):
invs = {}
invP = ~self
keys = sorted(self._code)
for key in keys:
x = invP[key] # position of key
cnt = 0
for left_of_key in keys: # in order up to position
if left_of_key == x: # none more to left
break
if self._code[left_of_key] > key:
cnt += 1
invs[key] = cnt
return invs
def test_me():
p = P() # identity permutation
new_p = p.shuffle()
inv_p = ~new_p
try:
assert p == inv_p * new_p # should be True
print("First Test Succeeds")
except AssertionError:
print("First Test Fails")
#==========
p = P().shuffle()
try:
assert p ** -1 == ~p
assert p ** -2 == ~(p * p)
assert p ** -2 == (~p * ~p)
print("Second Test Succeeds")
except AssertionError:
print("Second Test Fails")
#==========
p = P().shuffle()
s = "able was i ere i saw elba"
c = p(s)
print("Plain: ", s)
print("Cipher: ", c)
try:
assert p.decrypt(c) == s
print("Third Test Succeeds")
except AssertionError:
print("Third Test Fails")
#==========
knuth = {1:5, 2:9, 3:1, 4:8, 5:2, 6:6, 7:4, 8:7, 9:3} # vol 3 pg. 12
expected = {1:2, 2:3, 3:6, 4:4, 5:0, 6:2, 7:2, 8:1, 9:0} # Ibid
k = P(the_code=knuth)
try:
assert k.inversion_table() == expected
print("Fourth Test Succeeds")
except AssertionError:
print("Fourth Test Fails")
#==========
p = P(inv_table = expected)
try:
assert p == k
print("Fifth Test Succeeds")
except AssertionError:
print("Fifth Test Fails")
#==========
p = P().shuffle()
inv = p.inversion_table()
print("Perm:", p._code)
print("Inv table:", inv)
new_p = P(inv_table = inv)
try:
assert p == new_p
print("Sixth Test Succeeds")
except AssertionError:
print("Sixth Test Fails")