From 863c2cfce870c294b2c7c0c5e6b5e530a5378928 Mon Sep 17 00:00:00 2001 From: Joe Anderson Date: Tue, 1 Jul 2014 11:40:24 -0500 Subject: Python scripts for analysis --- common.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ connected.py | 46 ++++++++++++++++++++++++++++++++++++++ reverse.py | 53 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100755 common.py create mode 100755 connected.py create mode 100755 reverse.py diff --git a/common.py b/common.py new file mode 100755 index 0000000..411867b --- /dev/null +++ b/common.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +from itertools import combinations + +def increment(dictionary, key): + dictionary[key] = dictionary.get(key, 0) + 1 + +def print_dict(dictionary): + keys = sorted(list(dictionary.keys())) + for key in keys: + print(key, dictionary[key]) + +def distribution(counts, capset, num): + combs = tuple(combinations(range(counts["dim"]), num)) + num_i = len(combs) + num_j = 3**num + dists = [[0 for j in range(num_j)] + for i in range(num_i)] + for card in capset: + for i, indices in enumerate(combs): + substr = "".join(card[indices[n]] for n in range(num)) + j = int(substr, 3) + dists[i][j] += 1 + metadist = {} + for i in range(num_i): + dists[i] = "".join(map(str, sorted(dists[i]))) + name = "dist" + str(num) + ":" + dists[i] + increment(counts, name) + increment(metadist, name) + if counts["premeta" + str(num)]: + z = tuple(zip(*counts["premeta" + str(num)].items())) + else: + z = ([],[]) + if metadist in z[1]: + key = z[0][z[1].index(metadist)] + else: + key = len(z[0]) + counts["premeta" + str(num)][key] = metadist + name = "premeta" + str(num) + ":" + str(key) + increment(counts, name) + +def metadistribution(counts, num): + dists = tuple(sorted(key for key in counts.keys() + if key.startswith("dist" + str(num)))) + for key in counts["premeta" + str(num)]: + count = counts["premeta" + str(num) + ":" + str(key)] + metadist = counts["premeta" + str(num)][key] + newstr = "".join(str(metadist.get(dist, 0)) for dist in dists) + counts["meta" + str(num) + ":" + newstr] = count + for key in counts.copy(): + if key.startswith("premeta" + str(num)): + del counts[key] + +def main(): + capsets = [] + capset_file = open("capset.out", "r") + for line in capset_file: + capsets.append(line[1:-2].split(" ")) + capset_file.close() + + counts = {"count" : 0, + "dim" : len(capsets[0][0])} + nums = range(1, counts["dim"]) + for num in nums: + counts["premeta" + str(num)] = {} + for capset in capsets: + counts["count"] += 1 + for num in nums: + distribution(counts, capset, num) + for num in nums: + metadistribution(counts, num) + print_dict(counts) + +if __name__ == "__main__": main() diff --git a/connected.py b/connected.py new file mode 100755 index 0000000..ee3ae70 --- /dev/null +++ b/connected.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +from sys import argv +if len(argv) == 1: NUM = 1 +if len(argv) == 2: NUM = int(argv[1]) + +def is_connected(capset0, capset1): + count = 0 + for card in capset0: + if card in capset1: + continue + count += 1 + return count <= NUM + +def next_connected(disconnected): + newly_connected = [disconnected.pop()] + connected = [] + while newly_connected: + check_capsets = iter(newly_connected[:]) + connected.extend(newly_connected) + newly_connected = [] + for con_capset in check_capsets: + for index, dis_capset in reversed(list(enumerate(disconnected[:]))): + if is_connected(con_capset, dis_capset): + newly_connected.append(disconnected.pop(index)) + return connected + +def main(): + capsets = [] + capset_file = open("capset.out", "r") + for line in capset_file: + capsets.append(tuple(line[1:-2].split(" "))) + capset_file.close() + + connecteds = [] + while capsets: + connecteds.append(next_connected(capsets)) + #for connected in connecteds: + # print("\nThe following are connected:") + # for capset in connected: + # print(capset) + print("\nThere are", len(connecteds), "partitions.") + lengths = list(map(len, connecteds)) + for length in sorted(set(lengths)): + print("There are", lengths.count(length), "with length", length) + +if __name__ == "__main__": main() diff --git a/reverse.py b/reverse.py new file mode 100755 index 0000000..304baa1 --- /dev/null +++ b/reverse.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +from sys import argv + +def int2card(num, dim=4): + if num != int(num): + raise Exception("Number must be an integer.") + if num < 0: + raise Exception("Number must be nonnegative.") + if num >= 3**dim: + raise Exception("Number must be less than {}.".format(3**dim)) + strlist = [] + for i in range(dim): + strlist.append(str(num%3)) + num //= 3 + return ''.join(reversed(strlist)) + +def main(): + """Reverses the card identities in a capset.out file. + + 222 becomes 000, etc. + By default, reads in capset.out and writes to capset.new + You may give other input/output files as optional arguments. + It will NOT reverse in-place: you must give a different output file. + """ + inname = "capset.out" + outname = "capset.new" + if len(argv) == 2: + inname = argv[1] + if len(argv) == 3: + inname = argv[1] + outname = argv[2] + if inname == outname: + raise Exception("Input and output files must differ!") + infile = open(inname, "r") + outfile = open(outname, "w") + + firstline = infile.readline() + infile.seek(0) + capset = firstline[1:-2].split(" ") + dim = len(capset[0]) + + for line in infile: + capset = line[1:-2].split(" ") + newcards = [] + for card in capset: + newcard = 3**dim - 1 - int(card, 3) + newcards.append(int2card(newcard, dim)) + outfile.write("[" + " ".join(newcards) + "]\n") + + infile.close() + outfile.close() + +if __name__ == "__main__": main() -- cgit v1.2.3-70-g09d2