Guest User

Untitled

a guest
Mar 18th, 2026
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. def test(wordLength, numTrials=10000):
  2. F.<a,b> = FreeGroup()
  3. W = FiniteWords('abAB')
  4.  
  5. def letterToGroup(c):
  6. if c == 'a': return a
  7. if c == 'b': return b
  8. if c == 'A': return a^(-1)
  9. if c == 'B': return b^(-1)
  10.  
  11. def wordToReln(w):
  12. return prod([letterToGroup(c) for c in w])
  13.  
  14. numPerfect = 0
  15. numNontrivial = 0
  16.  
  17. while numPerfect < numTrials:
  18. if numPerfect % 1000 == 0: print(numPerfect, "/", numTrials)
  19. R = W.random_element(length=wordLength)
  20. S = W.random_element(length=wordLength)
  21.  
  22. # Check if the abelianization is trivial by checking
  23. # if the determinant of (R^ab | S^ab) is +/- 1
  24. det = (R.count('a') - R.count('A')) * (S.count('b') - S.count('B')) - \
  25. (R.count('b') - R.count('B')) * (S.count('a') - S.count('A'))
  26.  
  27. if abs(det) == 1:
  28. numPerfect += 1
  29.  
  30. G = F / [wordToReln(R), wordToReln(S)]
  31.  
  32. # How can I make this line of code time out after 3 seconds
  33. # so we can move on to another random presentation?
  34. if G.cardinality() != 1:
  35. numNontrivial += 1
  36.  
  37. return (numNontrivial / numPerfect).n()
Advertisement
Add Comment
Please, Sign In to add comment