{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE RebindableSyntax #-} -- | The 'Normed' class represents elements of a normed vector -- space. We define instances for all common numeric types. module Normed where import BigFloat import NumericPrelude hiding (abs) import Algebra.Absolute (abs) import qualified Algebra.Absolute as Absolute import qualified Algebra.Algebraic as Algebraic import qualified Algebra.RealField as RealField import qualified Algebra.ToInteger as ToInteger class Normed a where norm_p :: (ToInteger.C c, Algebraic.C b, Absolute.C b) => c -> a -> b norm_infty :: (RealField.C b) => a -> b -- | The "usual" norm. Defaults to the Euclidean norm. norm :: (Algebraic.C b, Absolute.C b) => a -> b norm = norm_p (2 :: Integer) -- Define instances for common numeric types. instance Normed Integer where norm_p _ = abs . fromInteger norm_infty = abs . fromInteger instance Normed Rational where norm_p _ = abs . fromRational' norm_infty = abs . fromRational' instance Epsilon e => Normed (BigFloat e) where norm_p _ = abs . fromRational' . toRational norm_infty = abs . fromRational' . toRational instance Normed Float where norm_p _ = abs . fromRational' . toRational norm_infty = abs . fromRational' . toRational instance Normed Double where norm_p _ = abs . fromRational' . toRational norm_infty = abs . fromRational' . toRational