Subato

Resource Files

Polynomberechnungen

Schreiben Sie eine kleine Bibliothek zum Rechnen mit Polynomen. Hierzu sei der Datentyp Poly gegeben.


> module Poly where > import Data.Ratio > import Data.List > data Monom = M Rational Int > deriving (Show,Eq) > newtype Poly = P [Monom] > deriving (Show,Eq) > infix 8 #^ > a #^ b = P [M a b] > polyEx = 3#^2 > class ToLaTeX a where > toLaTeX :: a -> String > instance (Integral a,Eq a,Num a,Show a) => ToLaTeX (Ratio a)where > toLaTeX r > |n==1 = show z > |otherwise = "\\frac{"++show z++"}{"++show n++"}" > where > z = numerator r > n = denominator r > instance ToLaTeX Monom where > toLaTeX (M k 0) = toLaTeX k > toLaTeX (M k 1) > |k==1 = "x" > |otherwise = toLaTeX k++"*x" > toLaTeX (M k e) > |k==1 = "x^{"++show e++"}" > |otherwise = toLaTeX k++"*x^{"++show e++"}" > instance ToLaTeX Poly where > toLaTeX (P []) = "0" > toLaTeX (P (x:xs)) > = (toLaTeX x) > ++ (foldl (++) "" > $ map (\m@(M k e)-> > (if (k>=0) then "+" else "")++toLaTeX m) > xs) > infix 9 § > (§) :: Poly -> Rational -> Rational > (P xs)§x = 0 > normalize :: Poly -> Poly > normalize = id > add :: Poly -> Poly -> Poly > add p1 p2 = P[] > negat :: Poly -> Poly > negat = id > mult :: Poly -> Poly -> Poly > mult p1 p2 = P[] > infix 2 /% > (/%) :: Poly -> Poly -> (Poly,Poly) > p1 /% p2 = (p1,p2) > derivation :: Poly -> Poly > derivation = id > instance Num Poly where > fromInteger x = error "not implemented" > negate p = negat p > p1 + p2 = add p1 p2 > abs (P xs) = error "not implemented" > signum _ = error "not implemented" > p1 * p2 = mult p1 p2 > example1 = 6 #^ 6 - 2#^5 - 4#^3 + 3#^1 + 3 > example2 = 2#^3 + 2#^1 - 3 > res1 = example1 /% example1 > res2 = example1 /% example2
lhs