X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FIntegration%2FSimpson.hs;h=6bfe258eb8e454b7a9dcc0c7221d4638eed3f61f;hb=6b6bae4206bab66823617e2ba77cdf3e8d3fb752;hp=2481f850b3bf6e15d7c7fac25c9c65de7d8b6be1;hpb=c3905924154d9a8d56bdc57e2f36fe48b8524eef;p=numerical-analysis.git diff --git a/src/Integration/Simpson.hs b/src/Integration/Simpson.hs index 2481f85..6bfe258 100644 --- a/src/Integration/Simpson.hs +++ b/src/Integration/Simpson.hs @@ -1,8 +1,14 @@ +{-# LANGUAGE RebindableSyntax #-} + module Integration.Simpson where import Misc (partition) +import NumericPrelude hiding (abs) +import qualified Algebra.RealField as RealField +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@]. @@ -17,6 +23,7 @@ import Misc (partition) -- >>> simpson_1 f (-1) 1 -- 0.0 -- +-- >>> import Algebra.Absolute (abs) -- >>> let f x = x^2 -- >>> let area = simpson_1 f (-1) 1 -- >>> abs (area - (2/3)) < 1/10^12 @@ -30,7 +37,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 +45,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 @@ -47,6 +54,7 @@ simpson_1 f a b = -- -- Examples: -- +-- >>> import Algebra.Absolute (abs) -- >>> let f x = x^4 -- >>> let area = simpson 10 f (-1) 1 -- >>> abs (area - (2/5)) < 0.0001 @@ -55,11 +63,16 @@ simpson_1 f a b = -- Note that the convergence here is much faster than the Trapezoid -- rule! -- +-- >>> import Algebra.Absolute (abs) -- >>> let area = simpson 10 sin 0 pi -- >>> 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@