X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FMisc.hs;h=61c9d69c9613bd39268b10be26a675c7b3d06c02;hb=33e0d2f5682b117b3aca945aaad5ab36958fa852;hp=24fd5b756febd19bd512cf9a7b06682f2aba9f7d;hpb=807d976941a8dd426ecf43b18b876413a58384f2;p=numerical-analysis.git diff --git a/src/Misc.hs b/src/Misc.hs index 24fd5b7..61c9d69 100644 --- a/src/Misc.hs +++ b/src/Misc.hs @@ -1,12 +1,14 @@ {-# LANGUAGE RebindableSyntax #-} +{-# LANGUAGE ScopedTypeVariables #-} -- | Stuff for which I'm too lazy to come up with a decent name. module Misc where import NumericPrelude -import Algebra.Field -import Algebra.ToInteger +import Algebra.Field ( C ) +import Algebra.RealRing ( C ) +import Algebra.ToInteger ( C ) -- | Partition the interval [@a@, @b@] into @n@ subintervals, which we -- then return as a list of pairs. @@ -19,19 +21,30 @@ import Algebra.ToInteger -- >>> partition 4 (-1) 1 -- [(-1.0,-0.5),(-0.5,0.0),(0.0,0.5),(0.5,1.0)] -- -partition :: (Algebra.Field.C a, Algebra.ToInteger.C b, Enum b) +partition :: forall a b. (Algebra.Field.C a, Algebra.ToInteger.C b, Enum 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 +partition n x y | 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 ] + let k' = fromIntegral k :: a, + let xi = x + k'*h, + let xj = x + (k'+1)*h ] where - h = (b-a)/(fromIntegral $ toInteger n) + coerced_n = fromIntegral $ toInteger n :: a + h = (y-x)/coerced_n + + +-- | Compute the unit roundoff (machine epsilon) for this machine. We +-- find the largest number epsilon such that 1+epsilon <= 1. If you +-- request anything other than a Float or Double from this, expect +-- to wait a while. +-- +unit_roundoff :: forall a. (Algebra.RealRing.C a, Algebra.Field.C a) => a +unit_roundoff = + head [ 1/2^(k-1) | k <- [0..], 1 + 1/(2^k) <= (1::a) ]