1 {-# LANGUAGE RebindableSyntax #-}
2 {-# LANGUAGE ScopedTypeVariables #-}
4 -- | Stuff for which I'm too lazy to come up with a decent name.
9 import Algebra.Field ( C )
10 import Algebra.RealRing ( C )
11 import Algebra.ToInteger ( C )
13 -- | Partition the interval [@a@, @b@] into @n@ subintervals, which we
14 -- then return as a list of pairs.
18 -- >>> partition 1 (-1) 1
21 -- >>> partition 4 (-1) 1
22 -- [(-1.0,-0.5),(-0.5,0.0),(0.0,0.5),(0.5,1.0)]
24 partition :: forall a b. (Algebra.Field.C a, Algebra.ToInteger.C b, Enum b)
25 => b -- ^ The number of subintervals to use, @n@
26 -> a -- ^ The \"left\" endpoint of the interval, @a@
27 -> a -- ^ The \"right\" endpoint of the interval, @b@
29 -- Somebody asked for zero subintervals? Ok.
32 | n < 0 = error "partition: asked for a negative number of subintervals"
34 [ (xi, xj) | k <- [0..n-1],
35 let k' = fromIntegral k :: a,
37 let xj = x + (k'+1)*h ]
39 coerced_n = fromIntegral $ toInteger n :: a
43 -- | Compute the unit roundoff (machine epsilon) for this machine. We
44 -- find the largest number epsilon such that 1+epsilon <= 1. If you
45 -- request anything other than a Float or Double from this, expect
48 unit_roundoff :: forall a. (Algebra.RealRing.C a, Algebra.Field.C a) => a
50 head [ 1/2^(k-1) | k <- [0..], 1 + 1/(2^k) <= (1::a) ]