module Factory.Data.MonicPolynomial(
MonicPolynomial(getPolynomial),
mkMonicPolynomial
) where
import qualified Control.Arrow
import qualified Factory.Data.Monomial as Data.Monomial
import Factory.Data.Polynomial((*=))
import qualified Factory.Data.Polynomial as Data.Polynomial
import qualified Factory.Data.QuotientRing as Data.QuotientRing
import Factory.Data.Ring((=*=), (=+=), (=-=))
import qualified Factory.Data.Ring as Data.Ring
import qualified ToolShed.Data.Pair
newtype MonicPolynomial c e = MkMonicPolynomial {
getPolynomial :: Data.Polynomial.Polynomial c e
} deriving (Eq, Show)
mkMonicPolynomial :: (
Eq c,
Num c,
Ord e,
Show c,
Show e
) => Data.Polynomial.Polynomial c e -> MonicPolynomial c e
mkMonicPolynomial polynomial
| not $ Data.Polynomial.isMonic polynomial = error $ "Factory.Data.MonicPolynomial.mkMonicPolynomial:\tnot monic; " ++ show polynomial
| otherwise = MkMonicPolynomial polynomial
instance (
Eq c,
Num c,
Num e,
Ord e,
Show c,
Show e
) => Data.Ring.Ring (MonicPolynomial c e) where
MkMonicPolynomial l =*= MkMonicPolynomial r = MkMonicPolynomial $ l =*= r
MkMonicPolynomial l =+= MkMonicPolynomial r = mkMonicPolynomial $ l =+= r
additiveInverse _ = error "Factory.Data.MonicPolynomial.additiveInverse:\tresult isn't monic"
multiplicativeIdentity = MkMonicPolynomial Data.Ring.multiplicativeIdentity
additiveIdentity = MkMonicPolynomial Data.Ring.additiveIdentity
instance (
Eq c,
Num c,
Num e,
Ord e,
Show c,
Show e
) => Data.QuotientRing.QuotientRing (MonicPolynomial c e) where
MkMonicPolynomial polynomialN `quotRem'` MkMonicPolynomial polynomialD = ToolShed.Data.Pair.mirror MkMonicPolynomial $ longDivide polynomialN where
longDivide numerator
| Data.Polynomial.isZero numerator || Data.Monomial.getExponent quotient < 0 = (Data.Polynomial.zero, numerator)
| otherwise = Control.Arrow.first (Data.Polynomial.lift (quotient :)) $ longDivide (numerator =-= polynomialD *= quotient)
where
quotient = Data.Polynomial.getLeadingTerm numerator `Data.Monomial.shiftExponent` negate (Data.Monomial.getExponent $ Data.Polynomial.getLeadingTerm polynomialD)