-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArithmeticCongruenceMonoid.sage
270 lines (218 loc) · 9.16 KB
/
ArithmeticCongruenceMonoid.sage
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
# Class: ArithmeticCongruenceMonoid
#
# Author: Jacob Hartzer
# Last Edited 3/3/2016
#
# This is a python class based around the factorization of Arithmetical Congruence
# Monoids (ACMs). Most of the operations are primitive, but nonetheless very useful
# in analyzing patterns in ACMs.
#
#####################################################################################
#
# Usage
#
# load('/path/to/file/ArithmeticalCongruenceMonoid.sage)
#
# Hilbert = ArithmeticCongruenceMonoid([1,4])
# Hilbert.function()
#
#####################################################################################
class ArithmeticCongruenceMonoid:
def __init__(self, elements):
if (elements[0]^2)%elements[1]!=elements[0]:
print 'error: a must be equivalent to a^2 mod b'
return null
self.elements = elements
self.ACMdict = {1:[[]]}
self.IrreduciblesDict = {}
def ArithmeticFactorizations(self, num):
import itertools
if num in self.ACMdict:
return self.ACMdict[num]
#Builds lists of possible factors by all possible combinations of each prime factor
Factors = factor(num)
A = [1]
for i in range(len(Factors)):
B = []
for j in range(1,Factors[i][1]+1):
for k in A:
B.append(k*(Factors[i][0])^j)
A = A + B
#removes all numbers not in the monoid
Arithmetic_Factors = [i for i in A if (i%self.elements[1] == self.elements[0] or i == 1)]
#runs the recursive program to find list of factorizations
return self.ACMfactor(num, Arithmetic_Factors)
def ACMfactor(self,num,factors):
# Magical recursive program that builds lists of factorizations by tree. It
# builds from bottom up, adding factors at each branch. Just trust that it works. I checked
if num in self.ACMdict:
return self.ACMdict[num]
else:
self.ACMdict[num] = []
for f in factors[1:]:
if num/f in factors and ((num/f == 1 and len(self.ACMdict[num]) == 0) or len(self.ACMfactor(f,factors)[0]) == 1):
smallerfactors = []
smallerfactors = deepcopy(self.ACMfactor(num/f,factors))
for k in range(len(smallerfactors)):
if smallerfactors[k] == [] or f >= smallerfactors[k][-1]:
smallerfactors[k].append(f)
self.ACMdict[num] = self.ACMdict[num] + [smallerfactors[k]]
return self.ACMdict[num]
def ArithmeticFactorizationsUpToElement(self, nmax):
# Finds all arithmetic factorizations up to a certain element. Useful to run before long calculations
for i in range(self.elements[0],nmax+1,self.elements[1]):
self.ArithmeticFactorizations(i)
return "done"
def LongestFactorizationsInRange(self, nmax):
# returns indices and length of element with longest factorization
self.ArithmeticFactorizationsUpToElement(nmax)
index = []
max_length = 0
for i in range(self.elements[0],nmax+1,self.elements[1]):
if len(self.ArithmeticFactorizations(i)) > max_length:
max_length = len(self.ArithmeticFactorizations(i))
index = [i]
elif len(self.ArithmeticFactorizations(i)) == max_length:
index.append(i)
return index, max_length
def NumberOfFactorizations(self,num):
# returns the number of factorizations of an element
return len(self.ArithmeticFactorizations(num))
def PlotNumberOfFactorizationsToElement(self,nmax):
# plots number of factorizations up to a given element
plot_list=[]
for i in range(self.elements[0]+self.elements[1],nmax+1,self.elements[1]):
plot_list.append([i,self.NumberOfFactorizations(i)])
return list_plot(plot_list)
def MaxFactorizationLength(self,num):
# finds the max factorization length of a given element
max_length = 1
for i in range(self.NumberOfFactorizations(num)):
if len(self.ArithmeticFactorizations(num)[i]) > max_length:
max_length = len(self.ArithmeticFactorizations(num)[i])
return max_length
def MinFactorizationLength(self,num):
# finds the min factorization length of a given element
min = len(self.ArithmeticFactorizations(num)[0])
for i in range(self.NumberOfFactorizations(num)):
if len(self.ArithmeticFactorizations(num)[i]) < min:
min = len(self.ArithmeticFactorizations(num)[i])
return min
def PlotMaxFactorizationLengthToElement(self,nmax):
# plots max factorization length up to a given element
plot_list=[]
for i in range(self.elements[0]+self.elements[1],nmax+1,self.elements[1]):
plot_list.append([i,self.MaxFactorizationLength(i)])
return list_plot(plot_list)
def PlotMinFactorizationLengthToElement(self,nmax):
# plots min factorization length up to a given element
plot_list=[]
for i in range(self.elements[0]+self.elements[1],nmax+1,self.elements[1]):
plot_list.append([i,self.MinFactorizationLength(i)])
return list_plot(plot_list)
def Elasticity(self,num):
# finds elasticity of an element: max factorization length / min...
if num == 1:
return 1
return float(self.MaxFactorizationLength(num))/float(self.MinFactorizationLength(num))
def MaxElasticityToElement(self,nmax):
# Fins max elasticity to given element.
max_elasticity = 0
for i in range(self.elements[0]+self.elements[1],nmax+1,self.elements[1]):
if self.Elasticity(i) >= max_elasticity:
max_elasticity = self.Elasticity(i)
return max_elasticity
def PlotElasticitiesToElement(self,nmax):
# plots elasticities to a given element
plot_list=[]
for i in range(self.elements[0]+self.elements[1],nmax+1,self.elements[1]):
plot_list.append([i,self.Elasticity(i)])
p = list_plot(plot_list,figsize=(10,4),size = 5,title = 'Elasticities of M_4,6')
return p
def IsIrreducible(self,num):
# checks if an element is irreducible.
if self.MaxFactorizationLength(num) == 1:
self.IrreduciblesDict[num] = 1
return "true"
else:
return "false"
def PlotIrreduciblesToElement(self,nmax):
# plots irreducibles to a given element (irreducible = 1)
self.IrreduciblesUpToElement(nmax)
p = list_plot(self.IrreduciblesDict,figsize=(10,4),size = 50,title = 'Irreducibles of M_4,6',ymin = -0.5,ymax = 1.5)
return p
def IrreduciblesUpToElement(self,nmax):
# creates a dict of the irreducibles up to a given element
start = self.elements[0]
if self.elements[0] == 1:
start = start + self.elements[1]
for i in range(start,nmax,self.elements[1]):
self.IrreduciblesDict[i] = 1 #pre allocates dictionary memory
for i in range(start,nmax,self.elements[1]):
for j in range(i,nmax/i+1,self.elements[1]):#for each product of two elements, sets dict(product) = 0
self.IrreduciblesDict[i*j] = 0 # essentially a Sieve of Eratosthenes for ACMs
def FindMaxDeltaSet_Irreducibles(self,nmax):
# Finds the largest distance between two irreducible elements
self.IrreduciblesUpToElement(nmax)
max_delta = 1
start = self.elements[0]
if start == 1:
start = start + self.elements[1]
hold = start
for i in range(start,nmax,self.elements[1]):
if self.IrreduciblesDict[i] == 1:
if (i-hold)>max_delta:
max_delta = i-hold
print max_delta,i,factor(i)
hold = i
def FindMaxDeltaSet_Reducibles(self,nmax):
# Finds the largest distance between two reducible elements
self.IrreduciblesUpToElement(nmax)
max_delta = 1
start = self.elements[0]
if start == 1:
start = start + self.elements[1]
hold = start
for i in range(start,nmax,self.elements[1]):
if self.IrreduciblesDict[i] == 0:
if (i-hold)>max_delta:
max_delta = i-hold
print max_delta,i,factor(i)
hold = i
def PlotDeltaSet_Reducibles(self,nmax):
# plots the delta set of reducible elements to a given element.
# delta set is the distance between consecutive reducibles
self.IrreduciblesUpToElement(nmax)
hold = self.elements[0]
delta_list = []
for i in range(self.elements[0]+self.elements[1],nmax,self.elements[1]):
if self.IrreduciblesDict[i] == 0:
delta_list = delta_list + [i-hold]
hold = i
return list_plot(delta_list)
def PlotDeltaSet_Irreducibles(self,nmax):
# plots the delta set of irreducible elements to a given element.
# delta set is the distance between consecutive irreducibles
self.IrreduciblesUpToElement(nmax)
hold = self.elements[0]
delta_list = []
for i in range(self.elements[0]+self.elements[1],nmax,self.elements[1]):
if self.IrreduciblesDict[i] == 1:
delta_list = delta_list + [i-hold]
hold = i
return list_plot(delta_list)
def PlotVariableDeltaSet_Reducibles(self,nmax,start,period):
# if you want to change the period in which the program looks for irreducibles
# this will do that for you.
self.IrreduciblesUpToElement(nmax)
hold = self.elements[0] + start*self.elements[1]
delta_list = []
for i in range(self.elements[0] + start*self.elements[1], nmax, period*self.elements[1]):
if self.IrreduciblesDict[i] == 0:
delta_list = delta_list + [(i-hold)/period]
hold = i
return list_plot(delta_list)
def Percent_Irreducible(self,nmax):
# returns the percent of elements that irreducible to a given element
self.IrreduciblesUpToElement(nmax)
return float(sum(self.IrreduciblesDict.values())/len(self.IrreduciblesDict))