Advertisement
Guest User

Untitled

a guest
May 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. {-# LANGUAGE NoMonomorphismRestriction #-}
  2.  
  3. -- Experiments with permutations
  4.  
  5. import Data.List
  6. -- import qualified Math.Algebra.Group.PermutationGroup as PG
  7.  
  8. -- number of inversions in a permutation
  9. inversions as = sum $ map go (tails as)
  10. where go [] = 0
  11. go (x:xs) = length $ filter (<x) xs
  12.  
  13. evenPerm as = even (inversions as)
  14.  
  15. parity as = if evenPerm as then 0 else 1
  16.  
  17. alternating n = [ p | p <- permutations [1..n], evenPerm p ]
  18.  
  19. factorial n = product [1..n]
  20.  
  21. holes xs = zip3 (inits xs) xs (tail $ tails xs)
  22.  
  23. -- list perms in lexigrapical order
  24. lexPerms [] = [ [] ]
  25. lexPerms as = do (xs,x,ys) <- holes as
  26. map (x:) (lexPerms (xs++ys))
  27.  
  28. -- kth lexigraphical permutation
  29. kthPerm k [] = []
  30. kthPerm k as =
  31. let n = length as
  32. f = factorial (n-1)
  33. (q,r) = divMod k f
  34. (xs, (x:ys)) = splitAt q as
  35. in x : kthPerm r (xs++ys)
  36.  
  37. invert blk = map (1-) blk
  38. alternate n blk = concat (replicate n blk)
  39. ++ concat (replicate n (invert blk))
  40.  
  41. blk0 = alternate 1 [0,1] -- length 4 1 = 1*1
  42. blk1 = alternate 6 [0,1,1,0] -- length 24 6 = 2*3
  43. blk2 = alternate 15 blk1 -- length 1440 15 = 3*5
  44. blk3 = alternate 28 blk2 -- length 80640 27 = 4*7
  45. blk4 = alternate 45 blk3 -- length 7257600 45 = 5*9
  46.  
  47. -- the pattern of the permutation parities when listed in
  48. -- lexigraphical order -- good up to at least S_11
  49. pattern = concat $ repeat blk4
  50.  
  51. -- perhaps the sequence continues...
  52. blk5 = alternate (6*11) blk4
  53. blk6 = alternate (7*13) blk5
  54. -- ...
  55.  
  56. -- e.g.: checkPattern blk5 8
  57. checkPattern expected n =
  58. let perms = lexPerms [1..n] :: [[Int]]
  59. parities = map parity perms
  60. check = zipWith (==) parities expected
  61. groups = map length (group check)
  62. in groups
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement