From: Michael Orlitzky Date: Fri, 23 Nov 2012 18:39:42 +0000 (-0500) Subject: Move the partition function out of Integration.Trapezoid and into Misc. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=92d61dc6ab8dc3e8c9e5b4c869134f1e8079013f;p=numerical-analysis.git Move the partition function out of Integration.Trapezoid and into Misc. Fix some trapezoid doctests. --- diff --git a/src/Integration/Trapezoid.hs b/src/Integration/Trapezoid.hs index 9598001..8d4b769 100644 --- a/src/Integration/Trapezoid.hs +++ b/src/Integration/Trapezoid.hs @@ -1,26 +1,7 @@ module Integration.Trapezoid where - --- | 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 Misc (partition) -- | Use the trapezoid rule to numerically integrate @f@ over the -- interval [@a@, @b@]. @@ -52,18 +33,18 @@ trapezoid_1 f a b = (((f a) + (f b)) / 2) * (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: -- -- >>> 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 +-- >>> let area = trapezoid 1000 sin 0 pi +-- >>> abs (area - 2) < 0.0001 -- True -- trapezoid :: (RealFrac a, Fractional b, Num b, Integral c) diff --git a/src/Misc.hs b/src/Misc.hs new file mode 100644 index 0000000..4d8f3e7 --- /dev/null +++ b/src/Misc.hs @@ -0,0 +1,31 @@ +-- | Stuff for which I'm too lazy to come up with a decent name. +module Misc +where + +-- | Partition the interval [@a@, @b@] into @n@ subintervals, which we +-- then return as a list of pairs. +-- +-- Examples: +-- +-- >>> partition 1 (-1) 1 +-- [(-1.0,1.0)] +-- +-- >>> partition 4 (-1) 1 +-- [(-1.0,-0.5),(-0.5,0.0),(0.0,0.5),(0.5,1.0)] +-- +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)