X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FTwoTuple.hs;h=3950fee9afeabd5bfb8d884ca6543c4689873c24;hb=74e79199dcfec0639133ae9990dc33a2c5a095f0;hp=daeb6e41f8cdbbbe43e11139236dbc88f5955c18;hpb=ff83c063ad5cdb1bf9476678a010aa0cfb782a8f;p=numerical-analysis.git diff --git a/src/TwoTuple.hs b/src/TwoTuple.hs index daeb6e4..3950fee 100644 --- a/src/TwoTuple.hs +++ b/src/TwoTuple.hs @@ -1,5 +1,7 @@ -- | 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 @@ -8,7 +10,10 @@ import Vector 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) @@ -16,7 +21,14 @@ instance Functor TwoTuple where instance (RealFloat a) => Vector (TwoTuple a) where -- The standard Euclidean 2-norm. We need RealFloat for the square -- root. - norm (TwoTuple x1 y1) = fromRational $ toRational (sqrt(x1^2 + y1^2)) + norm_2 (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.