def bstAddValue(val, head): cur = head while cur != "NULL": par = cur if val == cur.value: return head elif val > cur.value: cur = cur.childr pos = "r" else: cur = cur.childl pos = "l" if pos == "r": par.childr = ADT.bstNode(val, "NULL", "NULL") else: par.childl = ADT.bstNode(val, "NULL", "NULL") return head
def cllNewNode(val): new = ADT.cllNode(val, "NULL", "NULL") return new
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT, ADTOp L = ADT.ADT([-12, 14, -16, -22, -20, -6, 2, -4, -18, -10, 8], [-1, 1, -1, 1, 1, -1, 1, -1, 1, 1, -1]) print L.possibleR2Down() # Should be arcs: 1, 3, 7, 8, 12, 14, 20, 21
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT K = ADT.ADT([4,8,12,2,14,6,10], [1, 1, 1, 1, -1, 1, -1]) Kname = "7_6" print "%s : %s\n" % (Kname, K.to_string()) for i in range(1,15): L = K.copy() L.crossing_change(i) print "%s with crossing change %d : %s" % (Kname, i, L.to_string())
def bstNewNode(val): new = ADT.bstNode(val, "NULL", "NULL") return new
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT K = ADT.ADT([6, 12, 8, 2, 10, -4], [-1, -1, -1, -1, 1, 1]) L = K.shiftLabel() print L.to_list() # Should be ([-6, 10, -12, -4, -8, -2], [-1, 1, -1, -1, 1, -1]) if L != ADT.ADT([-6, 10, -12, -4, -8, -2], [-1, 1, -1, -1, 1, -1]): raise TypeError("This is a different diagram!") M = L.shiftLabel() print M.to_list() # Should be ([10, 6, 12, 8, -2, 4], [-1, -1, -1, 1, 1, -1]) if M != ADT.ADT([10, 6, 12, 8, -2, 4], [-1, -1, -1, 1, 1, -1]): raise TypeError("This is a different diagram!") n = K.number_crossings() around = K.copy() for i in range(2 * n): around = around.shiftLabel() # Should be K again. if around != K: raise TypeError("This is a different diagram!") else: print "Back to where we started." K = ADT.ADT([2], [-1]) L = K.shiftLabel()
import sage import os, sys, TestInverses lib_path = os.path.abspath('../../../../') sys.path.append(lib_path) import ADT, ADTOp, ADTOpList, ADTOpPopulation, ADTtoGC, knots K = ADT.ADT([-4, -2, -8, -6, -12, 10], [-1, -1, -1, -1, 1, -1]) print ADTtoGC.ADTtoGC(K)
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT, ADTOpPopulation, ADTOpPopulationSets from datetime import datetime print "Starting script." start = datetime.now() #K = ADT.ADT([6, -2, -10, -14, 4, 12, 8, 16], [-1, -1, 1, 1, -1, 1, -1, -1]) #K = ADT.ADT([10, 8, 14, 4, 12, 2, 6], [1, -1, 1, -1, 1, 1, 1]) K = ADT.ADT([-4, -18, -22, -14, -6, -20, -8, -10, -2, -12, -16], [1, 1, 1, -1, 1, -1, -1, 1, 1, -1, 1]) #K = ADT.ADT([ def fit(ol): # print "Starting fit function." # startfit = datetime.now() L = K.copy() d, min_ol = ol.apply(L) if d.number_crossings() < 3: bonus = 10000 else: bonus = 1 ccCount = min_ol.ccCount() # print "Finishing fit function. Took: ", datetime.now() - startfit return 1.0 + bonus / (d.number_crossings()**3.0 + ccCount**2.0 + min_ol.length() + 1.0)
def sllNewNode(val): new = ADT.sllNode(val, "NULL") return new
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT #K = ADT.ADT([4, 6, 8, 2], [-1, 1, -1, 1]) #for i in range(1, 9): # print "i = {}".format(i) # print K.R2Candidates(i, 'r') K = ADT.ADT([-8, -14, 12, -2, -10, -4, 6], [-1, 1, -1, -1, -1, 1, -1]) #print K.R2Candidates(9, 'l') #print K.R2Candidates(9, 'r') print "arc = 10, side = r" #print K.R2Candidates(10, 'r') print K.regions(10, 'r') print "arc = 1, side = l" #print K.R2Candidates(1, 'l') print K.regions(1, 'l') print "arc = 9, side = l" #print K.R2Candidates(9, 'l') print K.regions(9, 'l') print "arc = 9, side = r" #print K.R2Candidates(9, 'r') print K.regions(9, 'r') print "arc = 8, side = l" #print K.R2Candidates(8, 'l')
print d.to_string() print "It has {} crossings.".format(d.number_crossings()) print "The (effective) length of the sequence is {}".format( len(min_ol.toList())) print "\n" if d.number_crossings() < 3: return 1 else: return 0 attempts = 10 successes = 0 for j in range(attempts): K = ADT.ADT([], []) length = 12 while K.number_crossings() < length: M = ADTOp.coarseRandomMove(upBias=3) M.apply(K) print "K is ", K.to_string() print "\n" successes += testSimplification() print "{} successes so far, out of {} attempts.".format(successes, j + 1) print "\n" print "We had {}% success!".format(float(successes) / float(attempts) * 100) print "Finished script. Took: ", datetime.now() - start
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT K = ADT.ADT([6, 12, 8, 2, 10, -4], [-1, -1, -1, -1, 1, 1]) L = ADT.ADT([-6, 10, -12, -4, -8, -2], [-1, 1, -1, -1, 1, -1]) print K.sameDiagram(L) # Should be True if not K.sameDiagram(L): raise TypeError("These are different diagrams!") M = ADT.ADT([10, 6, 12, 8, -2, 4], [-1, -1, -1, 1, 1, -1]) print K.sameDiagram(M) # Should be True if not K.sameDiagram(M): raise TypeError("These are different diagrams!") n = K.number_crossings() around = K.copy() for i in range(2*n): around = around.shiftLabel() print K.sameDiagram(around) # Should be True if not K.sameDiagram(around): raise TypeError("These are different diagrams!") N = ADT.ADT([6, 12, 8, 2, 10, -4], [-1, -1, -1, -1, 1, -1]) print K.sameDiagram(N) # Should be False
# if i.apply(L): # result = TestInverses.findInverse(M, i) # if not result[0]: # Errors.append([result[1], result[2]]) # if result[1] > 4: # print "#"*30 # print "This is a big one." # print "original: ", K.to_string() # print "move: ", i.toString() # break # K = L # # print Errors # original = ADT.ADT([6, 2, -10, 4, 8], [1, -1, -1, 1, -1]) print "Original diagram: ", original.to_string() move = ADTOp.ADTOp(2, "D", {'arc': 10}) print "Move to invert: ", move.toString() print '\n' K = original.copy() print "Applying the move to the diagram results in:" move.apply(K) print K.to_string() # K = [6, 2, 4], [-1, -1, 1] print '\n' inverse = ADTOp.ADTOp(2, "U", {'arc': 1, 'side': "L", 'target': [5, 4]}) print "The inverse move should be: ", inverse.toString()
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) from ADT import * from ADTComplex import * #L = ADT([2],[1]) #L = ADT([4,6,2],[1,1,1]) L = ADT([6, 8, 2, 4], [-1, 1, -1, 1]) K = ADTComplex(L) K.draw_graph() print K.crossings print K.arcs print K.regions
import ADT, ADTOp def testR2Up(diagram, arc, side, target, expected=None): print "The diagram before the move is: ", diagram.to_list() print "Attempting to perform an R2Up move at arc {}, on side {}, with target {}".format(arc, side, target) diagram.R2Up(arc, side, target) print "The result is: ", diagram.to_list() if not diagram.isrealisable(): raise TypeError("R2Up move is not producing a valid diagram!") if expected != None: if not diagram.sameDiagram(expected): print "The result should be: ", expected.to_list() raise TypeError("R2Up move is not producing the expected diagram!") print '\n' K = ADT.ADT([2], [-1]) for i in [1,2]: for j in ["L", "R"]: for k in [[1, 2], [2, 1]]: bool = K.R2Up(i, j, k) print bool # Should be False if bool: raise TypeError("Shouldn't be able to perform this move!") print '\n' K = ADT.ADT([], []) for j in ["L", "R"]: for k in [[], [1, 1], [1, 2]]: bool = K.R2Up(1, j, k) print bool
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) from ADT import * from graph_tool.all import * g = Graph() diags = [] edges = [] #diags.append(ADT([],[])) diags.append(ADT([4, 6, 2], [1, 1, 1])) #diags.append(ADT([4,6,8,2],[-1,1,-1,1])) g.add_vertex() maxlvl = 1 def find_diag(D): try: idx = diags.index(D) return idx except ValueError: return -1 def propagate(cur, lvl): if lvl > maxlvl: return print "propagate(%d,%d)" % (cur, lvl) v = g.vertex(cur) K = diags[cur]
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT K = ADT.ADT([4,8,12,2,14,6,10], [1, 1, 1, 1, -1, 1, -1]) Kname = "7_6" print "%s : %s\n" % (Kname, K.to_string()) for i in range(1,15): for sign in [1,-1]: for side in ['L','R']: L = K.copy() L.R1Up(i,side,sign) print "%s with R1Up(%d,%s,%s) : %s" % \ (Kname, \ i, \ side, \ ('+' if sign == 1 else '-'), \ L.to_string()) for j in L.possibleR1Down(): LL = L.copy() arc = j['arc'] LL.R1Down(arc) print " R1Down(%d) gives : %s" % \ (arc, \ (Kname if LL == K else "NOT %s" % (Kname))) print "" print ""
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT K = ADT.ADT([-6, 10, 12, -2, 4, -8], [1, 1, -1, 1, 1, 1]) #print K.isrealisable() #print K.to_list() #print "\n" #for i in range(1,12): # print i,K.right(i),K.doubleRight(i) print "#############################################################" print "#### Colin's Original example" print "#############################################################" print K.to_list() print " " ret = K.R3(8, 'r') print "return is: ", ret print " " print "We get ", K.to_list() print "Should get ([-6, -8, 12, -2, 10, -4], [1, 1, -1, 1, 1, 1])." print " " print "#############################################################" print "#### Colin's Original example shuffled round" print "#############################################################"
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT K = ADT.ADT([6, -8, -10, 12, 4, 2], [-1, -1, -1, -1, 1, 1]) print "K is:" print K.to_list() print "\n" print "Performing R2Down(4):" print K.R2Down(4) # Should be True print K.to_list() # Should be: [4, -6, 8, 2], [-1, -1, -1, 1] if K != ADT.ADT([4, -6, 8, 2], [-1, -1, -1, 1]): raise TypeError("Not the right result!") print "\n" print "Performing R2Down(4):" print K.R2Down(4) # Should be False print K.to_list() if K != ADT.ADT([4, -6, 8, 2], [-1, -1, -1, 1]): raise TypeError("Not the right result!") print "\n" print "Now, performing R2Down(2):" print K.R2Down(2) # Should be True
import os, sys lib_path = os.path.abspath('../') sys.path.append(lib_path) import ADT, ADTOp, ADTOpList K = ADT.ADT([4, 6, 5], [1, 1, 1]) Kname = "trefoil" # K = ADT.ADT([4,8,12,2,14,6,10], [1, 1, 1, 1, -1, 1, -1]) # Kname = "7_6" print "%s : %s\n" % (Kname, K.to_string()) n = 10 moves = ADTOpList.randomMoveList(n, n, upBias=5, downBias=5).toList() print "We have a list of moves." for i in moves: print i.toString() print "***** applying moves" for i in moves: L = K.copy() if i.apply(L): print "successful application of move" print K.to_string() K = L else: print "unsuccessful application of move"