module Algebra.Structures.BezoutDomain
( BezoutDomain(..)
, propBezoutDomain
, dividesB
, intersectionB, intersectionBWitness
, solveB
) where
import Test.QuickCheck
import Algebra.Structures.IntegralDomain
import Algebra.Structures.Coherent
import Algebra.Structures.EuclideanDomain
import Algebra.Structures.StronglyDiscrete
import Algebra.Matrix
import Algebra.Ideal
class IntegralDomain a => BezoutDomain a where
toPrincipal :: Ideal a -> (Ideal a,[a],[a])
propToPrincipal :: (BezoutDomain a, Eq a) => Ideal a -> Bool
propToPrincipal = isPrincipal . (\(a,_,_) -> a) . toPrincipal
propIsSameIdeal :: (BezoutDomain a, Eq a) => Ideal a -> Bool
propIsSameIdeal (Id as) =
let (Id [a], us, vs) = toPrincipal (Id as)
in a == foldr1 (<+>) (zipWith (<*>) as us)
&& and [ ai == a <*> vi | (ai,vi) <- zip as vs ]
&& length us == l_as && length vs == l_as
where l_as = length as
propBezoutDomain :: (BezoutDomain a, Eq a) => Ideal a -> a -> a -> a -> Property
propBezoutDomain id@(Id xs) a b c = zero `notElem` xs ==>
if propToPrincipal id
then if propIsSameIdeal id
then propIntegralDomain a b c
else whenFail (print "propIsSameIdeal") False
else whenFail (print "propToPrincipal") False
dividesB :: (BezoutDomain a, Eq a) => a -> a -> Bool
dividesB a b = a == x || a == neg x
where (Id [x],_,_) = toPrincipal (Id [a,b])
instance (EuclideanDomain a, Eq a) => BezoutDomain a where
toPrincipal (Id [x]) = (Id [x], [one], [one])
toPrincipal (Id xs) = (Id [a], as, [ quotient ai a | ai <- xs ])
where
a = genEuclidAlg xs
as = genExtendedEuclidAlg xs
intersectionBWitness :: (BezoutDomain a, Eq a)
=> Ideal a
-> Ideal a
-> (Ideal a, [[a]], [[a]])
intersectionBWitness (Id xs) (Id ys)
| xs' == [] = zeroIdealWitnesses xs ys
| ys' == [] = zeroIdealWitnesses xs ys
| otherwise = (Id [l], [handleZero xs as], [handleZero ys bs])
where
xs' = filter (/= zero) xs
ys' = filter (/= zero) ys
(Id [a],us1,vs1) = toPrincipal (Id xs')
(Id [b],us2,vs2) = toPrincipal (Id ys')
(Id [g],[u1,u2],[v1,v2]) = toPrincipal (Id [a,b])
l = g <*> v1 <*> v2
as = map (v2 <*>) us1
bs = map (v1 <*>) us2
handleZero :: (Ring a, Eq a) => [a] -> [a] -> [a]
handleZero xs []
| all (==zero) xs = xs
| otherwise = error "intersectionB: This should be impossible"
handleZero (x:xs) (a:as)
| x == zero = zero : handleZero xs (a:as)
| otherwise = a : handleZero xs as
handleZero [] _ = error "intersectionB: This should be impossible"
intersectionB :: (BezoutDomain a, Eq a) => Ideal a -> Ideal a -> Ideal a
intersectionB a b = (\(x,_,_) -> x) $ intersectionBWitness a b
solveB :: (BezoutDomain a, Eq a) => Vector a -> Matrix a
solveB x = solveWithIntersection x intersectionBWitness
instance (BezoutDomain a, Eq a) => StronglyDiscrete a where
member x (Id xs) | x == zero = Just (replicate (length xs) zero)
| otherwise = if a == g
then Just witness
else Nothing
where
(Id [g], as, bs) = toPrincipal (Id (filter (/= zero) xs))
(Id [a], _,[q1,q2]) = toPrincipal (Id [x,g])
witness = handleZero xs (map (q1 <*>) as)