]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Normed.hs
Replace the custom 'Vector' typeclass with 'Normed' everywhere.
[numerical-analysis.git] / src / Normed.hs
1 {-# LANGUAGE FlexibleInstances #-}
2
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 Data.Number.BigFloat
9
10 -- Since the norm is defined on a vector space, we should be able to
11 -- add and subtract anything on which a norm is defined. Of course
12 -- 'Num' is a bad choice here, but we really prefer to use the normal
13 -- addition and subtraction operators.
14 class (Num a) => Normed a where
15 norm_p :: (Integral c, RealFrac b) => c -> a -> b
16 norm_infty :: RealFrac b => a -> b
17
18 -- | The "usual" norm. Defaults to the Euclidean norm.
19 norm :: RealFrac b => a -> b
20 norm = norm_p (2 :: Integer)
21
22 -- Define instances for common numeric types.
23 instance Normed Integer where
24 norm_p _ = fromInteger
25 norm_infty = fromInteger
26
27 instance Normed Rational where
28 norm_p _ = fromRational
29 norm_infty = fromRational
30
31 instance Epsilon e => Normed (BigFloat e) where
32 norm_p _ = fromRational . toRational
33 norm_infty = fromRational . toRational
34
35 instance Normed Double where
36 norm_p _ = fromRational . toRational
37 norm_infty = fromRational . toRational