X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FRoots%2FFast.hs;h=47fa512a75476310403d1faed03b9c54bdabe171;hb=fe73028041fe3becce6ce1ff268181d55d54a011;hp=5efdf3be99eec871931d18c6095333b8bd31ce83;hpb=807d976941a8dd426ecf43b18b876413a58384f2;p=numerical-analysis.git diff --git a/src/Roots/Fast.hs b/src/Roots/Fast.hs index 5efdf3b..47fa512 100644 --- a/src/Roots/Fast.hs +++ b/src/Roots/Fast.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE RebindableSyntax #-} + -- | The Roots.Fast module contains faster implementations of the -- 'Roots.Simple' algorithms. Generally, we will pass precomputed -- values to the next iteration of a function rather than passing @@ -10,8 +12,16 @@ import Data.List (find) import Normed +import NumericPrelude hiding (abs) +import Algebra.Absolute +import Algebra.Field +import Algebra.Ring -has_root :: (Fractional a, Ord a, Ord b, Num b) +has_root :: (Algebra.Field.C a, + Ord a, + Algebra.Ring.C b, + Ord b, + Algebra.Absolute.C b) => (a -> b) -- ^ The function @f@ -> a -- ^ The \"left\" endpoint, @a@ -> a -- ^ The \"right\" endpoint, @b@ @@ -51,8 +61,11 @@ has_root f a b epsilon f_of_a f_of_b = c = (a + b)/2 - -bisect :: (Fractional a, Ord a, Num b, Ord b) +bisect :: (Algebra.Field.C a, + Ord a, + Algebra.Ring.C b, + Ord b, + Algebra.Absolute.C b) => (a -> b) -- ^ The function @f@ whose root we seek -> a -- ^ The \"left\" endpoint of the interval, @a@ -> a -- ^ The \"right\" endpoint of the interval, @b@ @@ -88,6 +101,7 @@ bisect f a b epsilon f_of_a f_of_b + -- | Iterate the function @f@ with the initial guess @x0@ in hopes of -- finding a fixed point. fixed_point_iterations :: (a -> a) -- ^ The function @f@ to iterate. @@ -104,7 +118,10 @@ fixed_point_iterations f x0 = -- -- We also return the number of iterations required. -- -fixed_point_with_iterations :: (Normed a, RealFrac b) +fixed_point_with_iterations :: (Normed a, + Algebra.Field.C b, + Algebra.Absolute.C b, + Ord b) => (a -> a) -- ^ The function @f@ to iterate. -> b -- ^ The tolerance, @epsilon@. -> a -- ^ The initial value @x0@. @@ -133,4 +150,3 @@ fixed_point_with_iterations f epsilon x0 = -- "safe" since the list is infinite. We'll succeed or loop -- forever. Just winning_pair = find (\(_, diff) -> diff < epsilon) pairs -