]> gitweb.michael.orlitzky.com - numerical-analysis.git/blob - src/Misc.hs
Update numeric-prelude and fixed-vector.
[numerical-analysis.git] / src / Misc.hs
1 {-# LANGUAGE RebindableSyntax #-}
2
3 -- | Stuff for which I'm too lazy to come up with a decent name.
4 module Misc
5 where
6
7 import NumericPrelude
8 import Algebra.Field
9 import Algebra.ToInteger
10
11 -- | Partition the interval [@a@, @b@] into @n@ subintervals, which we
12 -- then return as a list of pairs.
13 --
14 -- Examples:
15 --
16 -- >>> partition 1 (-1) 1
17 -- [(-1.0,1.0)]
18 --
19 -- >>> partition 4 (-1) 1
20 -- [(-1.0,-0.5),(-0.5,0.0),(0.0,0.5),(0.5,1.0)]
21 --
22 partition :: (Algebra.Field.C a, Algebra.ToInteger.C b, Enum b)
23 => b -- ^ The number of subintervals to use, @n@
24 -> a -- ^ The \"left\" endpoint of the interval, @a@
25 -> a -- ^ The \"right\" endpoint of the interval, @b@
26 -> [(a,a)]
27 -- Somebody asked for zero subintervals? Ok.
28 partition 0 _ _ = []
29 partition n a b
30 | n < 0 = error "partition: asked for a negative number of subintervals"
31 | otherwise =
32 [ (xi, xj) | k <- [0..n-1],
33 let k' = fromIntegral k,
34 let xi = a + k'*h,
35 let xj = a + (k'+1)*h ]
36 where
37 h = (b-a)/(fromIntegral $ toInteger n)