From bb7f6082639f8589a851487c02ab396f7f91b927 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 20 Feb 2013 21:46:56 -0500 Subject: [PATCH] Remove assumptions on the Normed class. --- src/Normed.hs | 25 +++++++++++-------------- src/Roots/Fast.hs | 8 +++++--- src/Roots/Simple.hs | 14 +++++++++++--- 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/Normed.hs b/src/Normed.hs index 8554bd9..3752edc 100644 --- a/src/Normed.hs +++ b/src/Normed.hs @@ -1,5 +1,6 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE RebindableSyntax #-} + -- | The 'Normed' class represents elements of a normed vector -- space. We define instances for all common numeric types. module Normed @@ -8,27 +9,23 @@ where import BigFloat import NumericPrelude hiding (abs) -import Algebra.Absolute -import Algebra.Field -import Algebra.Ring -import Algebra.ToInteger +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.RealRing as RealRing +import qualified Algebra.ToInteger as ToInteger -- 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 (Algebra.Ring.C a, Algebra.Absolute.C a) => Normed a where - norm_p :: (Algebra.ToInteger.C c, - Algebra.Field.C b, - Algebra.Absolute.C b) - => c -> a -> b - - norm_infty :: (Algebra.Field.C b, - Algebra.Absolute.C b) - => a -> b +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 :: (Algebra.Field.C b, Algebra.Absolute.C b) => a -> b + norm :: (Algebraic.C b, Absolute.C b) => a -> b norm = norm_p (2 :: Integer) -- Define instances for common numeric types. diff --git a/src/Roots/Fast.hs b/src/Roots/Fast.hs index 78c299a..0deb1fd 100644 --- a/src/Roots/Fast.hs +++ b/src/Roots/Fast.hs @@ -14,6 +14,8 @@ import Normed import NumericPrelude hiding (abs) import qualified Algebra.Absolute as Absolute +import qualified Algebra.Additive as Additive +import qualified Algebra.Algebraic as Algebraic import qualified Algebra.Field as Field import qualified Algebra.RealRing as RealRing import qualified Algebra.RealField as RealField @@ -116,9 +118,9 @@ fixed_point_iterations f x0 = -- We also return the number of iterations required. -- fixed_point_with_iterations :: (Normed a, - Field.C b, - Absolute.C b, - Ord b) + Algebraic.C a, + RealField.C b, + Algebraic.C b) => (a -> a) -- ^ The function @f@ to iterate. -> b -- ^ The tolerance, @epsilon@. -> a -- ^ The initial value @x0@. diff --git a/src/Roots/Simple.hs b/src/Roots/Simple.hs index 0a1debf..d3c10cd 100644 --- a/src/Roots/Simple.hs +++ b/src/Roots/Simple.hs @@ -20,6 +20,8 @@ import qualified Roots.Fast as F import NumericPrelude hiding (abs) import qualified Algebra.Absolute as Absolute import Algebra.Absolute (abs) +import qualified Algebra.Additive as Additive +import qualified Algebra.Algebraic as Algebraic import qualified Algebra.Field as Field import qualified Algebra.RealField as RealField import qualified Algebra.RealRing as RealRing @@ -87,7 +89,7 @@ bisect f a b epsilon = -- at x0. We delegate to the version that returns the number of -- iterations and simply discard the number of iterations. -- -fixed_point :: (Normed a, RealField.C b) +fixed_point :: (Normed a, Algebraic.C a, Algebraic.C b, RealField.C b) => (a -> a) -- ^ The function @f@ to iterate. -> b -- ^ The tolerance, @epsilon@. -> a -- ^ The initial value @x0@. @@ -100,7 +102,10 @@ fixed_point f epsilon x0 = -- the function @f@ with the search starting at x0 and tolerance -- @epsilon@. We delegate to the version that returns the number of -- iterations and simply discard the fixed point. -fixed_point_iteration_count :: (Normed a, RealField.C b) +fixed_point_iteration_count :: (Normed a, + Algebraic.C a, + RealField.C b, + Algebraic.C b) => (a -> a) -- ^ The function @f@ to iterate. -> b -- ^ The tolerance, @epsilon@. -> a -- ^ The initial value @x0@. @@ -118,7 +123,10 @@ fixed_point_iteration_count f epsilon x0 = -- -- This is used to determine the rate of convergence. -- -fixed_point_error_ratios :: (Normed a, RealField.C b) +fixed_point_error_ratios :: (Normed a, + Additive.C a, + RealField.C b, + Algebraic.C b) => (a -> a) -- ^ The function @f@ to iterate. -> a -- ^ The initial value @x0@. -> a -- ^ The true solution, @x_star@. -- 2.43.2