# Creates matrix a n by n matrix from a string def make_matrix(string,n): m = [] row = [] for i in range(0,n * n): if string[i] == '\n': continue if string[i] == ' ': continue row.append(Integer(string[i]) + 1) if len(row) == n: m.append(row) row = [] return matrix(m) # Gets Matrices from a text file def fetch_matrices(file_name,n): matrices = [] with open(file_name) as f: L = f.readlines() for i in L: matrices.append(make_matrix(i,n)) return matrices # Applies symbol permutations to a matrix def permute_matrix(matrix, permutation,n): copy = deepcopy(matrix) for i in range(0, n): for j in range(0 , n): copy[i,j] = permutation[copy[i][j] - 1] return copy # This applies all symbol permutations to the Isotopy # classes of order n which are read in from a file def create_determinant_list(file_name,n): the_list = [] permu = (Permutations(n)).list() matrices = fetch_matrices(file_name,n) for i in range(0,len(matrices)): for j in permu: copy = permute_matrix(matrices[i],j,n) the_list.append([i,j,copy.determinant()]) return the_list # This gets the Distinct determinants from # order n Latin Squares def get_Dn(sets,index): determinants = [] for i in sets: determinants.append(i[index]) for i in range(0,len(determinants)): if determinants[i] < 0: determinants[i] *= -1 determinants = uniq(determinants) determinants = sorted(determinants) return determinants # Produces sequence A309257, excludes the first three terms # since those are trivial def sequence_A309257(): file_for_4 = "latin_is4.txt" file_for_5 = "latin_is5.txt" file_for_6 = "latin_is6.txt" # Order 7 takes around 40 minutes to compute file_for_7 = "latin_is7.txt" files = [file_for_4,file_for_5,file_for_6,file_for_7] sequence = [] for i, isotopy in enumerate(files): print "Working on order ",i+4 dets_temp = create_determinant_list(isotopy,i+4) Dn_temp = get_Dn(dets_temp,2) sequence.append(gcd(Dn_temp)) return sequence