module Math.Projects.RootSystem where
import Prelude hiding ( (*>) )
import Data.Ratio
import qualified Data.List as L
import qualified Data.Set as S
import Math.Algebra.LinearAlgebra
import Math.Algebra.Group.PermutationGroup hiding (elts, order, closure)
import Math.Algebra.Group.SchreierSims as SS
import Math.Algebra.Group.StringRewriting as SG
import Math.Algebra.Field.Base
data Type = A | B | C | D | E | F | G
basisElt :: Int -> Int -> [Q]
basisElt n i = replicate (i-1) 0 ++ 1 : replicate (n-i) 0
simpleSystem A n | n >= 1 = [e i <-> e (i+1) | i <- [1..n]]
where e = basisElt (n+1)
simpleSystem B n | n >= 2 = [e i <-> e (i+1) | i <- [1..n-1]] ++ [e n]
where e = basisElt n
simpleSystem C n | n >= 2 = [e i <-> e (i+1) | i <- [1..n-1]] ++ [2 *> e n]
where e = basisElt n
simpleSystem D n | n >= 4 = [e i <-> e (i+1) | i <- [1..n-1]] ++ [e (n-1) <+> e n]
where e = basisElt n
simpleSystem E n | n `elem` [6,7,8] = take n simpleroots
where e = basisElt 8
simpleroots = ((1/2) *> (e 1 <-> e 2 <-> e 3 <-> e 4 <-> e 5 <-> e 6 <-> e 7 <+> e 8))
: (e 1 <+> e 2)
: [e (i-1) <-> e (i-2) | i <- [3..8]]
simpleSystem F 4 = [e 2 <-> e 3, e 3 <-> e 4, e 4, (1/2) *> (e 1 <-> e 2 <-> e 3 <-> e 4)]
where e = basisElt 4
simpleSystem G 2 = [e 1 <-> e 2, ((-2) *> e 1) <+> e 2 <+> e 3]
where e = basisElt 3
w r s = s <-> (2 * (s <.> r) / (r <.> r)) *> r
closure rs = S.toList $ closure' S.empty (S.fromList rs) where
closure' interior boundary
| S.null boundary = interior
| otherwise =
let interior' = S.union interior boundary
boundary' = S.fromList [w r s | r <- rs, s <- S.toList boundary] S.\\ interior'
in closure' interior' boundary'
weylPerms t n =
let rs = simpleSystem t n
xs = closure rs
toPerm r = fromPairs [(x, w r x) | x <- xs]
in map toPerm rs
weylMatrices t n = map wMx (simpleSystem t n)
wMx r = map (w r) [e i | i <- [1..d]]
where d = length r
e = basisElt d
cartanMatrix t n = [[2 * (ai <.> aj) / (ai <.> ai) | aj <- roots] | ai <- roots]
where roots = simpleSystem t n
setDiag c mx@((x:xs):rs) = (c:xs) : zipWith (:) (map head rs) (setDiag c $ map tail rs)
setDiag _ [[]] = [[]]
dynkinFromCartan aij = setDiag 0 $ (zipWith . zipWith) (*) aij (L.transpose aij)
dynkinDiagram t n = dynkinFromCartan $ cartanMatrix t n
coxeterFromDynkin nij = setDiag 1 $ (map . map) f nij
where f 0 = 2; f 1 = 3; f 2 = 4; f 3 = 6
coxeterMatrix t n = coxeterFromDynkin $ dynkinDiagram t n
fromCoxeterMatrix mx = (gs,rs) where
n = length mx
gs = map s_ [1..n]
rs = rules mx 1
rules [] _ = []
rules ((1:xs):rs) i = ([s_ i, s_ i],[]) : [powerRelation i j m | (j,m) <- zip [i+1..] xs] ++ rules (map tail rs) (i+1)
powerRelation i j m = (concat $ replicate m [s_ i, s_ j],[])
fromCoxeterMatrix2 mx = (gs,rs) where
n = length mx
gs = map s_ [1..n]
rs = rules mx 1
rules [] _ = []
rules ((1:xs):rs) i = ([s_ i, s_ i],[]) : [braidRelation i j m | (j,m) <- zip [i+1..] xs] ++ rules (map tail rs) (i+1)
braidRelation i j m = (take m $ cycle [s_ j, s_ i], take m $ cycle [s_ i, s_ j])
coxeterPresentation t n = fromCoxeterMatrix $ coxeterMatrix t n
eltsCoxeter t n = SG.elts $ fromCoxeterMatrix2 $ coxeterMatrix t n
poincarePoly t n = map length $ L.group $ map length $ eltsCoxeter t n
elemMx n i j = replicate (i-1) z ++ e j : replicate (n-i) z
where z = replicate n 0
e = basisElt n
lieMult x y = x*y - y*x
(+|+) = zipWith (++)
(+-+) = (++)
form D n = (zMx n +|+ idMx n)
+-+
(idMx n +|+ zMx n)
form C n = (2 : replicate (2*n) 0) :
(map (0:) (form D n))
form B n = let id' = (-1) *>> idMx n
in (zMx n +|+ idMx n)
+-+
(id' +|+ zMx n)
rootSystem A n | n >= 1 = [e i <-> e j | i <- [1..n+1], j <- [1..n+1], i /= j]
where e = basisElt (n+1)
rootSystem B n | n >= 2 = shortRoots ++ longRoots
where e = basisElt n
shortRoots = [e i | i <- [1..n]]
++ [[] <-> e i | i <- [1..n]]
longRoots = [e i <+> e j | i <- [1..n], j <- [i+1..n]]
++ [e i <-> e j | i <- [1..n], j <- [i+1..n]]
++ [[] <-> e i <+> e j | i <- [1..n], j <- [i+1..n]]
++ [[] <-> e i <-> e j | i <- [1..n], j <- [i+1..n]]
rootSystem C n | n >= 2 = longRoots ++ shortRoots
where e = basisElt n
longRoots = [2 *> e i | i <- [1..n]]
++ [[] <-> (2 *> e i) | i <- [1..n]]
shortRoots = [e i <+> e j | i <- [1..n], j <- [i+1..n]]
++ [e i <-> e j | i <- [1..n], j <- [i+1..n]]
++ [[] <-> e i <+> e j | i <- [1..n], j <- [i+1..n]]
++ [[] <-> e i <-> e j | i <- [1..n], j <- [i+1..n]]
rootSystem D n | n >= 4 =
[e i <+> e j | i <- [1..n], j <- [i+1..n]]
++ [e i <-> e j | i <- [1..n], j <- [i+1..n]]
++ [[] <-> e i <+> e j | i <- [1..n], j <- [i+1..n]]
++ [[] <-> e i <-> e j | i <- [1..n], j <- [i+1..n]]
where e = basisElt n
rootSystem G 2 = shortRoots ++ longRoots
where e = basisElt 3
shortRoots = [e i <-> e j | i <- [1..3], j <- [1..3], i /= j]
longRoots = concatMap (\r-> [r,[] <-> r]) [2 *> e i <-> e j <-> e k | i <- [1..3], [j,k] <- [[1..3] L.\\ [i]] ]
numRoots A n = n*(n+1)
numRoots B n = 2*n*n
numRoots C n = 2*n*n
numRoots D n = 2*n*(n-1)
numRoots E 6 = 72
numRoots E 7 = 126
numRoots E 8 = 240
numRoots F 4 = 48
numRoots G 2 = 12
orderWeyl A n = factorial (n+1)
orderWeyl B n = 2^n * factorial n
orderWeyl C n = 2^n * factorial n
orderWeyl D n = 2^(n-1) * factorial n
orderWeyl E 6 = 2^7 * 3^4 * 5
orderWeyl E 7 = 2^10 * 3^4 * 5 * 7
orderWeyl E 8 = 2^14 * 3^5 * 5^2 * 7
orderWeyl F 4 = 2^7 * 3^2
orderWeyl G 2 = 12
factorial n = product [1..toInteger n]