]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Normed.hs
f339ebfd6757a86123f8111c4cb1bb7f5ef35534
[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 class Normed a where
19 norm_p :: (ToInteger.C c, Algebraic.C b, Absolute.C b) => c -> a -> b
20 norm_infty :: (RealField.C b) => a -> b
21
22 -- | The "usual" norm. Defaults to the Euclidean norm.
23 norm :: (Algebraic.C b, Absolute.C b) => a -> b
24 norm = norm_p (2 :: Integer)
25
26 -- Define instances for common numeric types.
27 instance Normed Integer where
28 norm_p _ = abs . fromInteger
29 norm_infty = abs . fromInteger
30
31 instance Normed Rational where
32 norm_p _ = abs . fromRational'
33 norm_infty = abs . fromRational'
34
35 instance Epsilon e => Normed (BigFloat e) where
36 norm_p _ = abs . fromRational' . toRational
37 norm_infty = abs . fromRational' . toRational
38
39 instance Normed Float where
40 norm_p _ = abs . fromRational' . toRational
41 norm_infty = abs . fromRational' . toRational
42
43 instance Normed Double where
44 norm_p _ = abs . fromRational' . toRational
45 norm_infty = abs . fromRational' . toRational