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
18 -- Since the norm is defined on a vector space, we should be able to
19 -- add and subtract anything on which a norm is defined. Of course
20 -- 'Num' is a bad choice here, but we really prefer to use the normal
21 -- addition and subtraction operators.
23 norm_p :: (ToInteger.C c, Algebraic.C b, Absolute.C b) => c -> a -> b
24 norm_infty :: (RealField.C b) => a -> b
26 -- | The "usual" norm. Defaults to the Euclidean norm.
27 norm :: (Algebraic.C b, Absolute.C b) => a -> b
28 norm = norm_p (2 :: Integer)
30 -- Define instances for common numeric types.
31 instance Normed Integer where
32 norm_p _ = abs . fromInteger
33 norm_infty = abs . fromInteger
35 instance Normed Rational where
36 norm_p _ = abs . fromRational'
37 norm_infty = abs . fromRational'
39 instance Epsilon e => Normed (BigFloat e) where
40 norm_p _ = abs . fromRational' . toRational
41 norm_infty = abs . fromRational' . toRational
43 instance Normed Float where
44 norm_p _ = abs . fromRational' . toRational
45 norm_infty = abs . fromRational' . toRational
47 instance Normed Double where
48 norm_p _ = abs . fromRational' . toRational
49 norm_infty = abs . fromRational' . toRational