1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE RebindableSyntax #-}
3 -- | The 'Normed' class represents elements of a normed vector
4 -- space. We define instances for all common numeric types.
10 import NumericPrelude hiding (abs)
11 import Algebra.Absolute
14 import Algebra.ToInteger
16 -- Since the norm is defined on a vector space, we should be able to
17 -- add and subtract anything on which a norm is defined. Of course
18 -- 'Num' is a bad choice here, but we really prefer to use the normal
19 -- addition and subtraction operators.
20 class (Algebra.Ring.C a, Algebra.Absolute.C a) => Normed a where
21 norm_p :: (Algebra.ToInteger.C c,
26 norm_infty :: (Algebra.Field.C b,
30 -- | The "usual" norm. Defaults to the Euclidean norm.
31 norm :: (Algebra.Field.C b, Algebra.Absolute.C b) => a -> b
32 norm = norm_p (2 :: Integer)
34 -- Define instances for common numeric types.
35 instance Normed Integer where
36 norm_p _ = abs . fromInteger
37 norm_infty = abs . fromInteger
39 instance Normed Rational where
40 norm_p _ = abs . fromRational'
41 norm_infty = abs . fromRational'
43 instance Epsilon e => Normed (BigFloat e) where
44 norm_p _ = abs . fromRational' . toRational
45 norm_infty = abs . fromRational' . toRational
47 instance Normed Float where
48 norm_p _ = abs . fromRational' . toRational
49 norm_infty = abs . fromRational' . toRational
51 instance Normed Double where
52 norm_p _ = abs . fromRational' . toRational
53 norm_infty = abs . fromRational' . toRational