]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Normed.hs
Add Gauss-Seidel and SOR iterative methods.
[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.ToInteger as ToInteger
17
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.
22 class Normed a where
23 norm_p :: (ToInteger.C c, Algebraic.C b, Absolute.C b) => c -> a -> b
24 norm_infty :: (RealField.C b) => a -> b
25
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)
29
30 -- Define instances for common numeric types.
31 instance Normed Integer where
32 norm_p _ = abs . fromInteger
33 norm_infty = abs . fromInteger
34
35 instance Normed Rational where
36 norm_p _ = abs . fromRational'
37 norm_infty = abs . fromRational'
38
39 instance Epsilon e => Normed (BigFloat e) where
40 norm_p _ = abs . fromRational' . toRational
41 norm_infty = abs . fromRational' . toRational
42
43 instance Normed Float where
44 norm_p _ = abs . fromRational' . toRational
45 norm_infty = abs . fromRational' . toRational
46
47 instance Normed Double where
48 norm_p _ = abs . fromRational' . toRational
49 norm_infty = abs . fromRational' . toRational