X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTwoTuple.hs;h=becb11eb05435f5a3b45223a5d718475f32e9878;hb=e73e40c515938df4de629dbc88463c5d88bca7c8;hp=8493114ed7cac3dcc956a801127e5b43eca0efef;hpb=06c8df4932b9a46e48b3fc99c3bc44e787b182fc;p=numerical-analysis.git diff --git a/src/TwoTuple.hs b/src/TwoTuple.hs index 8493114..becb11e 100644 --- a/src/TwoTuple.hs +++ b/src/TwoTuple.hs @@ -1,16 +1,37 @@ -- | Implement ordered pairs all over again for fun (and to make sure --- that we can manipulate them algebraically). +-- that we can manipulate them algebraically). Also require (as +-- opposed to the built-in ordered pairs) that the elements have +-- matching types. -- module TwoTuple where +import Normed + + data TwoTuple a = TwoTuple a a - deriving (Eq, Show) + deriving (Eq) + +instance (Show a) => Show (TwoTuple a) where + show (TwoTuple x y) = "(" ++ (show x) ++ ", " ++ (show y) ++ ")" instance Functor TwoTuple where f `fmap` (TwoTuple x1 y1) = TwoTuple (f x1) (f y1) +instance (RealFloat a) => Normed (TwoTuple a) where + -- The standard Euclidean 2-norm. We need RealFloat for the square + -- root. + norm (TwoTuple x y) = fromRational $ toRational (sqrt(x^2 + y^2)) + + -- The infinity norm, i.e. the maximum entry. + norm_infty (TwoTuple x y) = + fromRational $ max absx absy + where + absx = abs (toRational x) + absy = abs (toRational y) +-- | It's not correct to use Num here, but I really don't want to have +-- to define my own addition and subtraction. instance Num a => Num (TwoTuple a) where -- Standard componentwise addition. (TwoTuple x1 y1) + (TwoTuple x2 y2) =