X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FNormed.hs;h=9bef7631221b9076fe57a261299c98147301233d;hb=c3905924154d9a8d56bdc57e2f36fe48b8524eef;hp=b60c2b12fe84d51e0bf83b60ce0f958af55f7c10;hpb=74e79199dcfec0639133ae9990dc33a2c5a095f0;p=numerical-analysis.git diff --git a/src/Normed.hs b/src/Normed.hs index b60c2b1..9bef763 100644 --- a/src/Normed.hs +++ b/src/Normed.hs @@ -7,23 +7,35 @@ where import Data.Number.BigFloat -class Normed a where +-- Since the norm is defined on a vector space, we should be able to +-- add and subtract anything on which a norm is defined. Of course +-- 'Num' is a bad choice here, but we really prefer to use the normal +-- addition and subtraction operators. +class (Num a) => Normed a where norm_p :: (Integral c, RealFrac b) => c -> a -> b norm_infty :: RealFrac b => a -> b + -- | The "usual" norm. Defaults to the Euclidean norm. + norm :: RealFrac b => a -> b + norm = norm_p (2 :: Integer) + -- Define instances for common numeric types. instance Normed Integer where norm_p _ = fromInteger norm_infty = fromInteger instance Normed Rational where - norm_p _ = fromRational - norm_infty = fromRational + norm_p _ = realToFrac + norm_infty = realToFrac instance Epsilon e => Normed (BigFloat e) where - norm_p _ = fromRational . toRational - norm_infty = fromRational . toRational + norm_p _ = realToFrac + norm_infty = realToFrac + +instance Normed Float where + norm_p _ = realToFrac + norm_infty = realToFrac instance Normed Double where - norm_p _ = fromRational . toRational - norm_infty = fromRational . toRational + norm_p _ = realToFrac + norm_infty = realToFrac