X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FIntegration%2FTrapezoid.hs;h=df4da781b6cea30e7fbe763ee20da9ba85549db7;hb=ae914d13235a4582077a5cb2b1edd630d9c6ad62;hp=959800135ea27be2da25aacd8bb3fc4a690139a6;hpb=57a982c464095201f7635977aac937ad6a08c0b0;p=numerical-analysis.git diff --git a/src/Integration/Trapezoid.hs b/src/Integration/Trapezoid.hs index 9598001..df4da78 100644 --- a/src/Integration/Trapezoid.hs +++ b/src/Integration/Trapezoid.hs @@ -1,25 +1,18 @@ -module Integration.Trapezoid +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE RebindableSyntax #-} + +module Integration.Trapezoid ( + trapezoid, + trapezoid_1 ) where +import Misc ( partition ) --- | Partition the interval [@a@, @b@] into @n@ subintervals, which we --- then return as a list of pairs. -partition :: (RealFrac a, Integral b) - => b -- ^ The number of subintervals to use, @n@ - -> a -- ^ The \"left\" endpoint of the interval, @a@ - -> a -- ^ The \"right\" endpoint of the interval, @b@ - -> [(a,a)] - -- Somebody asked for zero subintervals? Ok. -partition 0 _ _ = [] -partition n a b - | n < 0 = error "partition: asked for a negative number of subintervals" - | otherwise = - [ (xi, xj) | k <- [0..n-1], - let k' = fromIntegral k, - let xi = a + k'*h, - let xj = a + (k'+1)*h ] - where - h = fromRational $ (toRational (b-a))/(toRational n) +import NumericPrelude hiding ( abs ) +import qualified Algebra.Field as Field ( C ) +import qualified Algebra.RealField as RealField ( C ) +import qualified Algebra.ToInteger as ToInteger ( C ) +import qualified Algebra.ToRational as ToRational ( C ) -- | Use the trapezoid rule to numerically integrate @f@ over the @@ -43,30 +36,37 @@ partition n a b -- >>> trapezoid_1 f (-1) 1 -- 2.0 -- -trapezoid_1 :: (RealFrac a, Fractional b, Num b) +trapezoid_1 :: (Field.C a, ToRational.C a, Field.C b) => (a -> b) -- ^ The function @f@ -> a -- ^ The \"left\" endpoint, @a@ -> a -- ^ The \"right\" endpoint, @b@ -> b trapezoid_1 f a b = - (((f a) + (f b)) / 2) * (fromRational $ toRational (b - a)) - + (((f a) + (f b)) / 2) * coerced_interval_length + where + coerced_interval_length = fromRational' $ toRational (b - a) --- | Use the composite trapezoid tule to numerically integrate @f@ +-- | Use the composite trapezoid rule to numerically integrate @f@ -- over @n@ subintervals of [@a@, @b@]. -- -- Examples: -- +-- >>> import Algebra.Absolute (abs) -- >>> let f x = x^2 -- >>> let area = trapezoid 1000 f (-1) 1 --- abs (area - (2/3)) < 0.00001 +-- >>> abs (area - (2/3)) < 0.00001 -- True -- --- >>> let area = trapezoid 1000 sin (-1) 1 --- >>> abs (area - 2) < 0.00001 +-- >>> import Algebra.Absolute (abs) +-- >>> let area = trapezoid 1000 sin 0 pi +-- >>> abs (area - 2) < 0.0001 -- True -- -trapezoid :: (RealFrac a, Fractional b, Num b, Integral c) +trapezoid :: (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@