X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FMisc.hs;h=c81d3594fad218bd6d76d224e3aa942071c0a1cf;hb=ae914d13235a4582077a5cb2b1edd630d9c6ad62;hp=4d8f3e7f10f81f2abdd7e0fd9379e9192bc4b897;hpb=92d61dc6ab8dc3e8c9e5b4c869134f1e8079013f;p=numerical-analysis.git diff --git a/src/Misc.hs b/src/Misc.hs index 4d8f3e7..c81d359 100644 --- a/src/Misc.hs +++ b/src/Misc.hs @@ -1,7 +1,15 @@ +{-# 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 ( 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. -- @@ -13,7 +21,7 @@ where -- >>> 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) +partition :: (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@ @@ -28,4 +36,15 @@ partition n a b let xi = a + k'*h, let xj = a + (k'+1)*h ] where - h = fromRational $ (toRational (b-a))/(toRational n) + coerced_n = fromIntegral $ toInteger n + h = (b-a)/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) ]