X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FIntegration%2FSimpson.hs;h=c3d59ff797c0538c377a08196065a63b43178003;hb=29f7502f34bdd54dff446a3a886f0e24b7e44493;hp=2481f850b3bf6e15d7c7fac25c9c65de7d8b6be1;hpb=59c49750fd2455574fe4e67ddd7e67a20321c8a8;p=numerical-analysis.git diff --git a/src/Integration/Simpson.hs b/src/Integration/Simpson.hs index 2481f85..c3d59ff 100644 --- a/src/Integration/Simpson.hs +++ b/src/Integration/Simpson.hs @@ -1,8 +1,17 @@ +{-# LANGUAGE RebindableSyntax #-} + module Integration.Simpson where import Misc (partition) +import NumericPrelude hiding (abs) +import Algebra.Absolute (abs) +import qualified Algebra.Field as Field +import qualified Algebra.RealField as RealField +import qualified Algebra.RealRing as RealRing +import qualified Algebra.ToInteger as ToInteger +import qualified Algebra.ToRational as ToRational -- | Use the Simpson's rule to numerically integrate @f@ over the -- interval [@a@, @b@]. @@ -30,7 +39,7 @@ import Misc (partition) -- >>> simpson_1 f 0 1 -- 0.25 -- -simpson_1 :: (RealFrac a, Fractional b, Num b) +simpson_1 :: (RealField.C a, ToRational.C a, RealField.C b) => (a -> b) -- ^ The function @f@ -> a -- ^ The \"left\" endpoint, @a@ -> a -- ^ The \"right\" endpoint, @b@ @@ -38,7 +47,7 @@ simpson_1 :: (RealFrac a, Fractional b, Num b) simpson_1 f a b = coefficient * ((f a) + 4*(f midpoint) + (f b)) where - coefficient = (realToFrac (b - a)) / 6 + coefficient = (fromRational' $ toRational (b - a)) / 6 midpoint = (a + b) / 2 @@ -59,7 +68,11 @@ simpson_1 f a b = -- >>> abs (area - 2) < 0.00001 -- True -- -simpson :: (RealFrac a, Fractional b, Num b, Integral c) +simpson :: (RealField.C a, + ToRational.C a, + RealField.C b, + ToInteger.C c, + Enum c) => c -- ^ The number of subintervals to use, @n@ -> (a -> b) -- ^ The function @f@ -> a -- ^ The \"left\" endpoint, @a@