]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Normed.hs
8554bd97f2eba0bd66a4b458a05cdb334f2e4f87
[numerical-analysis.git] / src / Normed.hs
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.
5 module Normed
6 where
7
8 import BigFloat
9
10 import NumericPrelude hiding (abs)
11 import Algebra.Absolute
12 import Algebra.Field
13 import Algebra.Ring
14 import Algebra.ToInteger
15
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,
22 Algebra.Field.C b,
23 Algebra.Absolute.C b)
24 => c -> a -> b
25
26 norm_infty :: (Algebra.Field.C b,
27 Algebra.Absolute.C b)
28 => a -> b
29
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)
33
34 -- Define instances for common numeric types.
35 instance Normed Integer where
36 norm_p _ = abs . fromInteger
37 norm_infty = abs . fromInteger
38
39 instance Normed Rational where
40 norm_p _ = abs . fromRational'
41 norm_infty = abs . fromRational'
42
43 instance Epsilon e => Normed (BigFloat e) where
44 norm_p _ = abs . fromRational' . toRational
45 norm_infty = abs . fromRational' . toRational
46
47 instance Normed Float where
48 norm_p _ = abs . fromRational' . toRational
49 norm_infty = abs . fromRational' . toRational
50
51 instance Normed Double where
52 norm_p _ = abs . fromRational' . toRational
53 norm_infty = abs . fromRational' . toRational