X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FNormed.hs;h=8554bd97f2eba0bd66a4b458a05cdb334f2e4f87;hb=fe73028041fe3becce6ce1ff268181d55d54a011;hp=9bef7631221b9076fe57a261299c98147301233d;hpb=807d976941a8dd426ecf43b18b876413a58384f2;p=numerical-analysis.git diff --git a/src/Normed.hs b/src/Normed.hs index 9bef763..8554bd9 100644 --- a/src/Normed.hs +++ b/src/Normed.hs @@ -1,41 +1,53 @@ {-# LANGUAGE FlexibleInstances #-} - +{-# LANGUAGE RebindableSyntax #-} -- | The 'Normed' class represents elements of a normed vector -- space. We define instances for all common numeric types. module Normed where -import Data.Number.BigFloat +import BigFloat + +import NumericPrelude hiding (abs) +import Algebra.Absolute +import Algebra.Field +import Algebra.Ring +import Algebra.ToInteger -- 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 +class (Algebra.Ring.C a, Algebra.Absolute.C a) => Normed a where + norm_p :: (Algebra.ToInteger.C c, + Algebra.Field.C b, + Algebra.Absolute.C b) + => c -> a -> b + + norm_infty :: (Algebra.Field.C b, + Algebra.Absolute.C b) + => a -> b -- | The "usual" norm. Defaults to the Euclidean norm. - norm :: RealFrac b => a -> b + norm :: (Algebra.Field.C b, Algebra.Absolute.C b) => a -> b norm = norm_p (2 :: Integer) -- Define instances for common numeric types. instance Normed Integer where - norm_p _ = fromInteger - norm_infty = fromInteger + norm_p _ = abs . fromInteger + norm_infty = abs . fromInteger instance Normed Rational where - norm_p _ = realToFrac - norm_infty = realToFrac + norm_p _ = abs . fromRational' + norm_infty = abs . fromRational' instance Epsilon e => Normed (BigFloat e) where - norm_p _ = realToFrac - norm_infty = realToFrac + norm_p _ = abs . fromRational' . toRational + norm_infty = abs . fromRational' . toRational instance Normed Float where - norm_p _ = realToFrac - norm_infty = realToFrac + norm_p _ = abs . fromRational' . toRational + norm_infty = abs . fromRational' . toRational instance Normed Double where - norm_p _ = realToFrac - norm_infty = realToFrac + norm_p _ = abs . fromRational' . toRational + norm_infty = abs . fromRational' . toRational