X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FRoots%2FFast.hs;h=b3b5818782fede0db3e6b465dc9186d8e29a1ca5;hb=d86da7a71b498a24b544ded9ad7191707fd0a123;hp=cda999ceab62a2fcc3b1f77e07560fb22403d6d3;hpb=ad7ad985ec59ac865483ce810603a43f180fc5ec;p=numerical-analysis.git diff --git a/src/Roots/Fast.hs b/src/Roots/Fast.hs index cda999c..b3b5818 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 @@ -8,10 +10,18 @@ where import Data.List (find) -import Vector +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.RealRing as RealRing +import qualified Algebra.RealField as RealField -has_root :: (Fractional a, Ord a, Ord b, Num b) +has_root :: (RealField.C a, + RealRing.C b, + Absolute.C b) => (a -> b) -- ^ The function @f@ -> a -- ^ The \"left\" endpoint, @a@ -> a -- ^ The \"right\" endpoint, @b@ @@ -51,8 +61,9 @@ 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 :: (RealField.C a, + RealRing.C b, + 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 +99,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 +116,10 @@ fixed_point_iterations f x0 = -- -- We also return the number of iterations required. -- -fixed_point_with_iterations :: (Vector a, RealFrac b) +fixed_point_with_iterations :: (Normed a, + Additive.C a, + RealField.C b, + Algebraic.C b) => (a -> a) -- ^ The function @f@ to iterate. -> b -- ^ The tolerance, @epsilon@. -> a -- ^ The initial value @x0@. @@ -115,7 +130,7 @@ fixed_point_with_iterations f epsilon x0 = xn = fixed_point_iterations f x0 xn_plus_one = tail xn - abs_diff v w = norm_2 (v - w) + abs_diff v w = norm (v - w) -- The nth entry in this list is the absolute value of x_{n} - -- x_{n+1}. @@ -133,4 +148,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 -