X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FNormed.hs;h=8b4829538b8a8064bafa15928c86641280f1dfd5;hb=504257320e56784b914ff69e8efb22d6191e3372;hp=0187df916de7fd5d08338fc192504428c0668153;hpb=e505e7e507758201f9b1af450b987d9cb0e6a640;p=numerical-analysis.git diff --git a/src/Normed.hs b/src/Normed.hs index 0187df9..8b48295 100644 --- a/src/Normed.hs +++ b/src/Normed.hs @@ -1,41 +1,49 @@ {-# 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 (abs) +import qualified Algebra.Absolute as Absolute +import qualified Algebra.Algebraic as Algebraic +import qualified Algebra.RealField as RealField +import qualified Algebra.ToInteger as 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 Normed a where + norm_p :: (ToInteger.C c, Algebraic.C b, Absolute.C b) => c -> a -> b + norm_infty :: (RealField.C b) => a -> b -- | The "usual" norm. Defaults to the Euclidean norm. - norm :: RealFrac b => a -> b + norm :: (Algebraic.C b, 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 _ = fromRational - norm_infty = fromRational + norm_p _ = abs . fromRational' + norm_infty = abs . fromRational' instance Epsilon e => Normed (BigFloat e) where - norm_p _ = fromRational . toRational - norm_infty = fromRational . toRational + norm_p _ = abs . fromRational' . toRational + norm_infty = abs . fromRational' . toRational instance Normed Float where - norm_p _ = fromRational . toRational - norm_infty = fromRational . toRational + norm_p _ = abs . fromRational' . toRational + norm_infty = abs . fromRational' . toRational instance Normed Double where - norm_p _ = fromRational . toRational - norm_infty = fromRational . toRational + norm_p _ = abs . fromRational' . toRational + norm_infty = abs . fromRational' . toRational