summaryrefslogtreecommitdiff
path: root/Algebra/Ring/Polynomial/Internal.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Algebra/Ring/Polynomial/Internal.hs')
-rw-r--r--Algebra/Ring/Polynomial/Internal.hs43
1 files changed, 11 insertions, 32 deletions
diff --git a/Algebra/Ring/Polynomial/Internal.hs b/Algebra/Ring/Polynomial/Internal.hs
index 87288b7..ef1025b 100644
--- a/Algebra/Ring/Polynomial/Internal.hs
+++ b/Algebra/Ring/Polynomial/Internal.hs
@@ -33,7 +33,6 @@ import AlgebraicPrelude
import Control.DeepSeq (NFData)
import Control.Lens hiding (assign)
import qualified Data.Coerce as C
-import qualified Data.Foldable as F
import qualified Data.HashSet as HS
import Data.Map (Map)
import qualified Data.Map.Strict as M
@@ -93,9 +92,6 @@ instance (KnownNat n, IsMonomialOrder n ord, CoeffRing r) => IsPolynomial (Order
extractPower = runMult . ifoldMap (\ o -> Mult . pow (mor o) . fromIntegral) . getMonomial
{-# INLINE liftMap #-}
-ordVec :: forall n. KnownNat n => Sized n (Ordinal n)
-ordVec = unsafeFromList' $ enumOrdinal (sing :: SNat n)
-
instance (KnownNat n, CoeffRing r, IsMonomialOrder n ord)
=> IsOrderedPolynomial (OrderedPolynomial r ord n) where
-- | coefficient for a degree.
@@ -170,7 +166,7 @@ instance (Eq r) => Eq (OrderedPolynomial r order n) where
-- -- | By Hilbert's finite basis theorem, a polynomial ring over a noetherian ring is also a noetherian ring.
instance (IsMonomialOrder n order, CoeffRing r, KnownNat n) => Ring (OrderedPolynomial r order n) where
fromInteger 0 = Polynomial M.empty
- fromInteger n = Polynomial $ M.singleton one (fromInteger' n)
+ fromInteger n = Polynomial $ M.singleton one (NA.fromInteger n)
{-# INLINE fromInteger #-}
decZero :: DecidableZero r => r -> Maybe r
@@ -239,41 +235,17 @@ instance (KnownNat n, CoeffRing r, IsMonomialOrder n order, PrettyCoeff r)
=> Show (OrderedPolynomial r order n) where
showsPrec = showsPolynomialWith $ generate sing (\i -> "X_" ++ show (fromEnum i))
-showPolynomialWithVars :: (CoeffRing a, Show a, KnownNat n, IsMonomialOrder n ordering)
- => [(Int, String)] -> OrderedPolynomial a ordering n -> String
-showPolynomialWithVars dic p0@(Polynomial d)
- | isZero p0 = "0"
- | otherwise = intercalate " + " $ mapMaybe showTerm $ M.toDescList d
- where
- showTerm (getMonomial -> deg, c)
- | isZero c = Nothing
- | otherwise =
- let cstr = if (not (isZero $ c - one) || isConstantMonomial deg)
- then show c ++ " "
- else if isZero (c - one) then ""
- else if isZero (c + one)
- then if any (not . isZero) (F.toList deg) then "-" else "-1"
- else ""
- in Just $ cstr ++ unwords (mapMaybe showDeg (zip [0..] $ F.toList deg))
- showDeg (n, p) | p == 0 = Nothing
- | p == 1 = Just $ showVar n
- | otherwise = Just $ showVar n ++ "^" ++ show p
- showVar n = fromMaybe ("X_" ++ show n) $ lookup n dic
-
-isConstantMonomial :: Monomial n -> Bool
-isConstantMonomial v = all (== 0) $ F.toList v
-
-- | We provide Num instance to use trivial injection R into R[X].
-- Do not use signum or abs.
instance (IsMonomialOrder n order, CoeffRing r, KnownNat n)
=> P.Num (OrderedPolynomial r order n) where
- (+) = (+)
+ (+) = (NA.+)
{-# INLINE (+) #-}
- (*) = (*)
+ (*) = (NA.*)
{-# INLINE (*) #-}
- fromInteger = normalize . injectCoeff . fromInteger'
+ fromInteger = NA.fromInteger
{-# INLINE fromInteger #-}
signum f = if isZero f then zero else injectCoeff one
@@ -506,6 +478,8 @@ instance (KnownNat n, IsMonomialOrder n ord, IsPolynomial poly,
=> RightModule (Scalar r) (PadPolyL n ord poly) where
PadPolyL f *. r = PadPolyL $ mapCoeff' (*. r) f
{-# INLINE (*.) #-}
+deriving instance (KnownNat n, IsMonomialOrder n ord, IsPolynomial poly)
+ => Num (PadPolyL n ord poly)
instance (KnownNat n, IsMonomialOrder n ord, IsPolynomial poly)
=> IsPolynomial (PadPolyL n ord poly) where
@@ -547,3 +521,8 @@ instance (SingI (Replicate n 1), KnownNat n, IsMonomialOrder n ord, IsOrderedPol
padLeftPoly :: (IsMonomialOrder n ord, IsPolynomial poly)
=> Sing n -> ord -> poly -> PadPolyL n ord poly
padLeftPoly n _ = withKnownNat n $ PadPolyL . injectCoeff
+
+instance (r ~ Coefficient poly, IsPolynomial poly,
+ KnownNat n, CoeffRing r, IsMonomialOrder n order, PrettyCoeff r)
+ => Show (PadPolyL n order poly) where
+ showsPrec = showsPolynomialWith $ generate sing (\i -> "X_" ++ show (fromEnum i))