{-# LANGUAGE FlexibleInstances #-} -- | The 'Normed' class represents elements of a normed vector -- space. We define instances for all common numeric types. module Normed where import Data.Number.BigFloat -- Since the norm is defined on a vector space, we should be able to -- add and subtract anything on which a norm is defined. Of course -- 'Num' is a bad choice here, but we really prefer to use the normal -- addition and subtraction operators. class (Num a) => Normed a where norm_p :: (Integral c, RealFrac b) => c -> a -> b norm_infty :: RealFrac b => a -> b -- | The "usual" norm. Defaults to the Euclidean norm. norm :: RealFrac b => a -> b norm = norm_p (2 :: Integer) -- Define instances for common numeric types. instance Normed Integer where norm_p _ = fromInteger norm_infty = fromInteger instance Normed Rational where norm_p _ = fromRational norm_infty = fromRational instance Epsilon e => Normed (BigFloat e) where norm_p _ = fromRational . toRational norm_infty = fromRational . toRational instance Normed Double where norm_p _ = fromRational . toRational norm_infty = fromRational . toRational