1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE RebindableSyntax #-}
4 -- | The 'Normed' class represents elements of a normed vector
5 -- space. We define instances for all common numeric types.
11 import NumericPrelude hiding (abs)
12 import Algebra.Absolute (abs)
13 import qualified Algebra.Absolute as Absolute
14 import qualified Algebra.Algebraic as Algebraic
15 import qualified Algebra.RealField as RealField
16 import qualified Algebra.ToInteger as ToInteger
19 norm_p :: (ToInteger.C c, Algebraic.C b, Absolute.C b) => c -> a -> b
20 norm_infty :: (RealField.C b) => a -> b
22 -- | The "usual" norm. Defaults to the Euclidean norm.
23 norm :: (Algebraic.C b, Absolute.C b) => a -> b
24 norm = norm_p (2 :: Integer)
26 -- Define instances for common numeric types.
27 instance Normed Integer where
28 norm_p _ = abs . fromInteger
29 norm_infty = abs . fromInteger
31 instance Normed Rational where
32 norm_p _ = abs . fromRational'
33 norm_infty = abs . fromRational'
35 instance Epsilon e => Normed (BigFloat e) where
36 norm_p _ = abs . fromRational' . toRational
37 norm_infty = abs . fromRational' . toRational
39 instance Normed Float where
40 norm_p _ = abs . fromRational' . toRational
41 norm_infty = abs . fromRational' . toRational
43 instance Normed Double where
44 norm_p _ = abs . fromRational' . toRational
45 norm_infty = abs . fromRational' . toRational