-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLMSetTheory
More file actions
326 lines (292 loc) · 10.9 KB
/
Copy pathLMSetTheory
File metadata and controls
326 lines (292 loc) · 10.9 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
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
import numpy
class LM:
def countMin(arr):
small = min(arr);
tot = 0;
for x in range(len(arr)):
if (arr[x] == small):
tot+=1;
return tot;
def ignoreZeros(arr): #Not used in LM/SetTheory
final = 0;
for x in range(len(arr)):
final+=arr[x];
if final > 0:
break;
elif x == len(arr) and final == 0:
final = 1;
return final;
def nestedIndex(arr,srch): #Returns the index of sub-array srch within arr if included.
result = [];
for a in range(len(arr)):
result.append(arr[a][srch]);
return result;
def checkIndexes(arr):
result = [];
for x in range(1,len(arr)):
if arr[x] == arr[x-1]:
result.append(True);
return len(result) == len(arr)-1;
def mindex(arr):
small = min(arr);
for x in range(len(arr)):
if arr[x] == small:
return x;
def arrSame(arr):
collect = [];
trueMatrix = [];
total = 0;
for a in range(len(arr)):
for b in range(len(arr[a])):
collect.append(LM.nestedIndex(arr,b));
total+=1;
for c in range(len(collect)):
temp = [];
for d in range(1,len(collect[c])):
if (collect[c][d] == collect[c][d-1]):
temp.append(True);
trueMatrix.append(temp);
final = [];
for x in range(len(trueMatrix)):
if len(trueMatrix[x]) == len(arr)-1:
final.append(True);
return len(final) == total;
def progSum(arr,mod):
winner = [];
option1 = arr;
semi = SetTheory.retro(option1);
option2 = SetTheory.invert(semi,0,mod)
for x in range(1,len(option1)):
if SetTheory.modulus(option1[x]-option1[x-1],mod) < SetTheory.modulus(option2[x]-option2[x-1],mod):
winner.append(1);
elif SetTheory.modulus(option1[x]-option1[x-1],mod) > SetTheory.modulus(option2[x]-option2[x-1],mod):
winner.append(2);
else:
winner.append(0);
semi = LM.ignoreZeros(winner);
if semi == 1:
return option1;
else:
return option2;
def rotate(arr):
result = [];
big = arr+arr;
for x in range(len(arr)):
result.append(big[x:x+len(arr)]);
return result;
def duplicateSubArr(arr): #Works on its own, fails in context of abstractSubsets
dups = [];
for x in range(len(arr)):
for y in range(len(arr)):
if x < y:
temp = [];
for a in range(len(arr[x])):
for b in range(len(arr[y])):
if arr[x][a] == arr[y][b]:
temp.append(True);
if len(temp) == (len(arr[y])):
dups.append(y);
dups = sorted(numpy.unique(dups),reverse=True);
for z in dups:
del(arr[z]);
return arr;
def subCount(srch,arr):
res = []
fin = [srch,0]
start = list(filter(lambda a: len(a) == len(srch),arr));
for x in range(len(start)):
tru = [];
for y in range(len(start[x])):
for z in range(len(srch)):
if srch[z] == start[x][y]:
tru.append(True);
res.append(tru);
for a in range(len(res)):
if len(res[a]) == len(srch):
fin[1]+=1;
return fin;
class SetTheory: #Collection of methods used for set class analysis.
def modulus(num,mod):
if num >=0 and num < mod:
return num;
elif num < 0:
return SetTheory.modulus(num+mod,mod);
elif num >= mod:
return SetTheory.modulus(num-mod,mod);
def cycleLen(ints,mod):
tot = 0;
for x in ints:
tot+=x;
z = 1;
while tot*z%mod != 0:
z+=1;
return z*len(ints);
def cycle(start,int,terms):
res = [start];
for x in range(terms-1):
res.append(res[x]+int);
return res;
def combCycle(start,ints,mod):
bigInts = [];
result = [start];
for x in range(SetTheory.cycleLen(ints,mod)//len(ints)):
for y in range(len(ints)):
bigInts.append(ints[y]);
for z in range(len(bigInts)):
result.append(SetTheory.modulus(result[z]+bigInts[z],mod));
return result;
def transpose(set,index,mod):
res =[];
for x in set:
res.append(SetTheory.modulus(x+index,mod));
return res;
def complement(num,length):
return length-num;
def retro(arr):
result = [];
for x in range(len(arr)):
result.append(arr[SetTheory.complement(x,len(arr)-1)]);
return result;
def AIS(arr,mod): #This runs just fine on its own and within normalOrder and primeForm, but fails in ultimateSubs only in cardinalities above 7??
ais = [];
for x in range(1,len(arr)):
ais.append(SetTheory.modulus(arr[x]-arr[x-1],mod));
return ais;
def bounds(arr,mod):
res = [];
for x in range(len(arr)):
if (SetTheory.modulus(arr[x]-arr[0],mod)) != 0:
res.append(SetTheory.modulus(arr[x]-arr[0],mod));
return SetTheory.retro(res);
def intervalClass(val,mod):
if val <= mod*.5:
return val;
else:
return mod-val;
def rotBound(arr,mod):
res = [];
for x in arr:
res.append(SetTheory.bounds(x,mod));
return res;
def invert(arr,index,mod):
result = [];
for x in arr:
temp = mod-x;
result.append(SetTheory.modulus(temp+index,mod));
return result;
def normalOrder(arr,mod): #Gets the normal order of a set modulo n;
step1 = sorted(arr);
step2 = LM.rotate(step1);
step3 = SetTheory.rotBound(step2,mod);
if LM.arrSame(step3) == True:
return sorted(arr);
else:
for x in range(len(step3)-1):
step4 = LM.nestedIndex(step3,x);
if LM.countMin(step4) > 1:
for y in range(1,len(arr)):
step4 = LM.nestedIndex(step3,(x-y));
elif LM.countMin(step4) == 1:
no = step2[LM.mindex(step4)];
return no;
def primeForm(arr,mod):
pf = [];
start = SetTheory.normalOrder(arr,mod);
ints = SetTheory.AIS(start,mod);
if LM.checkIndexes(ints) == True:
for x in range(len(start)):
pf.append(SetTheory.modulus(start[x]-start[0],mod));
else:
next = LM.progSum(start,mod);
for z in next:
pf.append(SetTheory.modulus(z-next[0],mod));
return pf;
def intervalVector(arr,mod,out):
ints = [];
vector = []
for a in range(len(arr)):
for b in range(len(arr)):
if b>a:
ints.append(SetTheory.modulus(arr[b]-arr[a],mod));
for i in range(1,(mod//2)+1):
vector.append([[i,SetTheory.modulus(mod-i,mod)],[0]]);
for j in range(len(ints)):
for k in range(len(vector)):
if ints[j] == vector[k][0][0] or ints[j] == mod-vector[k][0][0]:
vector[k][1][0]+=1;
if out == 0:
return vector;
elif out == 1:
woot = [];
for l in range(len(vector)):
woot.append(vector[l][1][0]);
return woot;
def indexVector(arr,mod,out):
ints = [];
vect = [];
for a in range(len(arr)):
for b in range(len(arr)):
ints.append(SetTheory.modulus(arr[a]+arr[b],mod));
for i in range(mod):
vect.append([i,0]);
for j in range(len(vect)):
for k in range(len(ints)):
if ints[k] == vect[j][0]:
vect[j][1]+=1;
if out == 0:
return vect;
elif out == 1:
wowz = [];
for l in range(len(vect)):
wowz.append(vect[l][1]);
return wowz;
def powerSet(arr):
results = [];
arrLen = 2**(len(arr));
for a in range(arrLen):
temp = [];
for b in range(len(arr)):
if a & 2**b:
temp.append(arr[b]);
if len(temp) != 0:
results.append(temp);
return results;
def filtPowerSet(arr,filt): #Prints the subsets of cardinality filt from powerset of a collection. (Does not include the empty set).
start = SetTheory.powerSet(arr);
fin = list(filter(lambda x: len(x) == filt,start));
return fin;
def literalSubsets(arr): #Literal collections contained within a superset.
start = SetTheory.powerSet(arr);
return start;
def setClass(set,mod): #Generates the collection of all sets transpositionally and inversionally equivalent to the input set.
t_s = [];
i_s = [];
for x in range(mod):
t_s.append(["T"+str(x),SetTheory.transpose(set,x,mod)]);
i_s.append(["I"+str(x),SetTheory.retro(SetTheory.invert(set,x,mod))]);
return [t_s,i_s];
def srchSet(set,super): #Searches a superset for the primeform specified.
tot = SetTheory.powerSet(super);
primes = [];
for x in tot:
primes.append(SetTheory.primeForm(x,12));
return LM.subCount(set,primes);
def abstractSubsets(arr):
res = [];
start = SetTheory.literalSubsets(arr);
for x in start:
res.append(SetTheory.primeForm(x,12))
return LM.duplicateSubArr(res);
def ultimateSubs(set): #Counts the number of instances of each abstract subset within a collection, fails in cards over 7?
ps = [];
res = [];
start = SetTheory.abstractSubsets(set);
tot = SetTheory.powerSet(set);
for x in tot:
ps.append(SetTheory.primeForm(x,12));
for y in start:
res.append(LM.subCount(y,ps));
return res;
mySet = [0,2,4,5,7,9,11]
print(SetTheory.ultimateSubs(mySet))
print(SetTheory.abstractSubsets(mySet));