module Math.Algebras.Commutative where
import Math.Algebra.Field.Base hiding (powers)
import Math.Algebras.VectorSpace
import Math.Algebras.TensorProduct
import Math.Algebras.Structures
data GlexMonomial v = Glex Int [(v,Int)] deriving (Eq)
instance Ord v => Ord (GlexMonomial v) where
compare (Glex si xis) (Glex sj yjs) = compare (si, [(x,i) | (x,i) <- xis]) (sj, [(y,j) | (y,j) <- yjs])
instance Show v => Show (GlexMonomial v) where
show (Glex _ []) = "1"
show (Glex _ xis) = concatMap (\(x,i) -> if i==1 then showVar x else showVar x ++ "^" ++ show i) xis
where showVar x = filter ( /= '"' ) (show x)
instance (Num k, Ord v) => Algebra k (GlexMonomial v) where
unit x = x *> return munit
where munit = Glex 0 []
mult xy = nf $ fmap (\(a,b) -> a `mmult` b) xy
where mmult (Glex si xis) (Glex sj yjs) = Glex (si+sj) $ addmerge xis yjs
instance Num k => Coalgebra k (GlexMonomial v) where
counit = unwrap . nf . fmap (\m -> () )
comult = fmap (\m -> (m,m) )
type GlexPoly k v = Vect k (GlexMonomial v)
glexVar v = V [(Glex 1 [(v,1)], 1)]
class Monomial m where
var :: v -> Vect Q (m v)
powers :: m v -> [(v,Int)]
bind :: (Monomial m, Num k, Ord b, Show b, Algebra k b) =>
Vect k (m v) -> (v -> Vect k b) -> Vect k b
V ts `bind` f = sum [c *> product [f x ^ i | (x,i) <- powers m] | (m, c) <- ts]
instance Monomial GlexMonomial where
var = glexVar
powers (Glex _ xis) = xis
lt (V (t:ts)) = t
class DivisionBasis b where
dividesB :: b -> b -> Bool
divB :: b -> b -> b
dividesT (b1,x1) (b2,x2) = dividesB b1 b2
divT (b1,x1) (b2,x2) = (divB b1 b2, x1/x2)
quotRemMP f gs = quotRemMP' f (replicate n 0, 0) where
n = length gs
quotRemMP' 0 (us,r) = (us,r)
quotRemMP' h (us,r) = divisionStep h (gs,[],us,r)
divisionStep h (g:gs,us',u:us,r) =
if lt g `dividesT` lt h
then let t = V [lt h `divT` lt g]
h' = h t*g
u' = u+t
in quotRemMP' h' (reverse us' ++ u':us, r)
else divisionStep h (gs,u:us',us,r)
divisionStep h ([],us',[],r) =
let (lth,h') = splitlt h
in quotRemMP' h' (reverse us', r+lth)
splitlt (V (t:ts)) = (V [t], V ts)
infixl 7 %%
(%%) :: (Fractional k, Ord b, Show b, Algebra k b, DivisionBasis b)
=> Vect k b -> [Vect k b] -> Vect k b
f %% gs = r where (_,r) = quotRemMP f gs
instance Ord v => DivisionBasis (GlexMonomial v) where
dividesB (Glex si xis) (Glex sj yjs) = si <= sj && dividesB' xis yjs where
dividesB' ((x,i):xis) ((y,j):yjs) =
case compare x y of
LT -> False
GT -> dividesB' ((x,i):xis) yjs
EQ -> if i<=j then dividesB' xis yjs else False
dividesB' [] _ = True
dividesB' _ [] = False
divB (Glex si xis) (Glex sj yjs) = Glex (sisj) $ divB' xis yjs where
divB' ((x,i):xis) ((y,j):yjs) =
case compare x y of
LT -> (x,i) : divB' xis ((y,j):yjs)
EQ -> if i == j then divB' xis yjs else (x,ij) : divB' xis yjs
GT -> error "divB'"
divB' xis [] = xis
divB' [] yjs = error "divB'"