]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Normed.hs
3752edc04b9fef7fa04f3ec5fd9a074d5789a0bb
[numerical-analysis.git] / src / Normed.hs
1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE RebindableSyntax #-}
3
4 -- | The 'Normed' class represents elements of a normed vector
5 -- space. We define instances for all common numeric types.
6 module Normed
7 where
8
9 import BigFloat
10
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.RealRing as RealRing
17 import qualified Algebra.ToInteger as ToInteger
18
19 -- Since the norm is defined on a vector space, we should be able to
20 -- add and subtract anything on which a norm is defined. Of course
21 -- 'Num' is a bad choice here, but we really prefer to use the normal
22 -- addition and subtraction operators.
23 class Normed a where
24 norm_p :: (ToInteger.C c, Algebraic.C b, Absolute.C b) => c -> a -> b
25 norm_infty :: (RealField.C b) => a -> b
26
27 -- | The "usual" norm. Defaults to the Euclidean norm.
28 norm :: (Algebraic.C b, Absolute.C b) => a -> b
29 norm = norm_p (2 :: Integer)
30
31 -- Define instances for common numeric types.
32 instance Normed Integer where
33 norm_p _ = abs . fromInteger
34 norm_infty = abs . fromInteger
35
36 instance Normed Rational where
37 norm_p _ = abs . fromRational'
38 norm_infty = abs . fromRational'
39
40 instance Epsilon e => Normed (BigFloat e) where
41 norm_p _ = abs . fromRational' . toRational
42 norm_infty = abs . fromRational' . toRational
43
44 instance Normed Float where
45 norm_p _ = abs . fromRational' . toRational
46 norm_infty = abs . fromRational' . toRational
47
48 instance Normed Double where
49 norm_p _ = abs . fromRational' . toRational
50 norm_infty = abs . fromRational' . toRational