module Math.Algebra.Group.RandomSchreierSims where
import System.Random
import Data.List as L
import qualified Data.Map as M
import Data.Maybe
import Control.Monad
import Data.Array.MArray
import Data.Array.IO
import System.IO.Unsafe
import Math.Common.ListSet (toListSet)
import Math.Core.Utils hiding (elts)
import Math.Algebra.Group.PermutationGroup
import Math.Algebra.Group.SchreierSims (sift, cosetRepsGx, ss')
testProdRepl = do (r,xs) <- initProdRepl $ _D 10
hs <- replicateM 20 $ nextProdRepl (r,xs)
mapM_ print hs
initProdRepl :: (Ord a, Show a) => [Permutation a] -> IO (Int, IOArray Int (Permutation a))
initProdRepl gs =
let n = length gs
r = max 10 n
xs = (1:) $ take r $ concat $ repeat gs
in do xs' <- newListArray (0,r) xs
replicateM_ 60 $ nextProdRepl (r,xs')
return (r,xs')
nextProdRepl :: (Ord a, Show a) => (Int, IOArray Int (Permutation a)) -> IO (Maybe (Permutation a))
nextProdRepl (r,xs) =
do s <- randomRIO (1,r)
t <- randomRIO (1,r)
u <- randomRIO (0,3 :: Int)
out <- updateArray xs s t u
return out
updateArray xs s t u =
let (swap,invert) = quotRem u 2 in
if s == t
then return Nothing
else do
x_0 <- readArray xs 0
x_s <- readArray xs s
x_t <- readArray xs t
let x_s' = mult (swap,invert) x_s x_t
x_0' = mult (swap,0) x_0 x_s'
writeArray xs 0 x_0'
writeArray xs s x_s'
return (Just x_0')
where mult (swap,invert) a b = case (swap,invert) of
(0,0) -> a * b
(0,1) -> a * b^-1
(1,0) -> b * a
(1,1) -> b^-1 * a
sgs :: (Ord a, Show a) => [Permutation a] -> [Permutation a]
sgs gs = toListSet $ concatMap snd $ rss gs
rss gs = unsafePerformIO $
do (r,xs) <- initProdRepl gs
rss' (r,xs) (initLevels gs) 0
rss' (r,xs) levels i
| i == 25 = return levels
| otherwise = do g <- nextProdRepl (r,xs)
let (changed,levels') = updateLevels levels g
rss' (r,xs) levels' (if changed then 0 else i+1)
initLevels gs = [((b,M.singleton b 1),[]) | b <- bs]
where bs = toListSet $ concatMap supp gs
updateLevels levels Nothing = (False,levels)
updateLevels levels (Just g) =
case sift (map fst levels) g of
Nothing -> (False, levels)
Just g' -> (True, updateLevels' [] levels g' (minsupp g'))
updateLevels' ls (r@((b,t),s):rs) h b' =
if b == b'
then reverse ls ++ ((b, cosetRepsGx (h:s) b), h:s) : rs
else updateLevels' (r:ls) rs h b'
baseTransversalsSGS gs = [let hs = filter ( (b <=) . minsupp ) gs in (b, cosetRepsGx hs b) | b <- bs]
where bs = toListSet $ map minsupp gs
isMemberSGS :: (Ord a, Show a) => [Permutation a] -> Permutation a -> Bool
isMemberSGS gs h = let bts = baseTransversalsSGS gs in isNothing $ sift bts h