]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Misc.hs
Move the partition function out of Integration.Trapezoid and into Misc.
[numerical-analysis.git] / src / Misc.hs
1 -- | Stuff for which I'm too lazy to come up with a decent name.
2 module Misc
3 where
4
5 -- | Partition the interval [@a@, @b@] into @n@ subintervals, which we
6 -- then return as a list of pairs.
7 --
8 -- Examples:
9 --
10 -- >>> partition 1 (-1) 1
11 -- [(-1.0,1.0)]
12 --
13 -- >>> partition 4 (-1) 1
14 -- [(-1.0,-0.5),(-0.5,0.0),(0.0,0.5),(0.5,1.0)]
15 --
16 partition :: (RealFrac a, Integral b)
17 => b -- ^ The number of subintervals to use, @n@
18 -> a -- ^ The \"left\" endpoint of the interval, @a@
19 -> a -- ^ The \"right\" endpoint of the interval, @b@
20 -> [(a,a)]
21 -- Somebody asked for zero subintervals? Ok.
22 partition 0 _ _ = []
23 partition n a b
24 | n < 0 = error "partition: asked for a negative number of subintervals"
25 | otherwise =
26 [ (xi, xj) | k <- [0..n-1],
27 let k' = fromIntegral k,
28 let xi = a + k'*h,
29 let xj = a + (k'+1)*h ]
30 where
31 h = fromRational $ (toRational (b-a))/(toRational n)