New patches: [unrevert anonymous**20090922065621] hunk ./test/Demo.hs 3 module Demo where -import qualified Data.Number.MPFR.Up as M +import qualified Data.Number.MPFR as M --import functions +import Data.Number.MPFR.Instances.Up -- import instances hunk ./test/Demo.hs 6 --- compute the sum from 1 to n with precision of p bits rounded to Near -s :: M.Precision -> Int -> M.MPFR -s p n = s' 1 0 - where s' k acc | k <= n = s' (succ k) (M.add M.Near p acc (M.fromInt M.Near 32 k)) - | otherwise = acc +import qualified Data.Number.MPFR.Mutable as MM + +import Control.Monad.ST(runST, ST) hunk ./test/Demo.hs 10 -s' :: M.Precision -> Int -> M.MPFR -s' p = foldl (M.addi M.Near p) 0 . enumFromTo 1 +-- compute the sum from 1 to n with precision of p bits rounded to Near +s1 :: M.Precision -> Int -> M.MPFR +s1 p n = s1' 1 0 + where s1' k acc | k <= n = s1' (succ k) (M.add M.Near p acc (M.fromInt M.Near 32 k)) + | otherwise = acc hunk ./test/Demo.hs 16 -s'' :: M.Precision -> Int -> M.MPFR -s'' p = foldl ((. M.fromInt M.Up p) . (+)) M.zero . enumFromTo 1 +-- or the same using addi + foldl instead of add +s2 :: M.Precision -> Int -> M.MPFR +s2 p = foldl (M.addi M.Near p) 0 . enumFromTo 1 hunk ./test/Demo.hs 20 --- compute pi with precision of n bits -pi' n = M.pi M.Near n +-- or the same as s1 except with foldl +s3 :: M.Precision -> Int -> M.MPFR +s3 p = foldl ((. M.fromInt M.Up p) . (+)) M.zero . enumFromTo 1 hunk ./test/Demo.hs 24 --- compute pi and get an indicator where the result is rounded -pi'' n = fst (M.mpfrToString M.Near 0 10 p) ++ case compare i 0 of - GT -> " result is rounded up" - EQ -> " result is exact" - LT -> " result is rounded down" - where (p, i) = M.pi_ M.Near n +-- or idiomatically using the MPFR Num instance +-- note that this version is a lot slower than previous three +-- guess why :) +s4 :: M.Precision -> Int -> M.MPFR +s4 p = sum . map (M.fromInt M.Up p) . enumFromTo 1 hunk ./test/Demo.hs 30 +-- or with mutable MPFR +s5 p n = runST $ go n =<< MM.unsafeThaw (M.fromInt M.Near p 0) + where go 0 acc = MM.unsafeFreeze acc + go m acc = MM.addi acc acc m M.Near >> go (m-1) acc hunk ./test/Demo.hs 35 +-- or, if you're feeling haskelly +s6 p n = runST $ MM.unsafeThaw (M.fromInt M.Near p 0) >>= + \acc -> mapM_ (flip (MM.addi acc acc) M.Near) [1..n] >> + MM.unsafeFreeze acc -- sum up first n terms of a Taylor series for e with precision p hunk ./test/Demo.hs 41 -e :: M.Precision -> Int -> M.MPFR -e p n = e' 1 1 1 +e1 :: M.Precision -> Int -> M.MPFR +e1 p n = e' 1 1 1 where e' k acc acc' | k == n = acc hunk ./test/Demo.hs 44 - | otherwise= e' (succ k) (M.add M.Down p acc (M.div M.Down p M.one acc'')) acc'' - where acc'' = M.muli M.Up p acc' k + | otherwise= e' (succ k) (M.add M.Up p acc acc'') acc'' + where acc'' = M.divi M.Up p acc' k hunk ./test/Demo.hs 47 --- factorial of n with p bits rounded to Near -fac :: M.Precision -> Int -> M.MPFR -fac p n = s' 1 1 - where s' k acc | k <= n = s' (succ k) (M.muli M.Near p acc k) - | otherwise = acc +-- or using mutable MPFR's +e2 :: M.Precision -> Int -> M.MPFR +e2 p n = let one = M.fromInt M.Near p 1 + in runST $ do acc <- MM.unsafeThaw one + acc' <- MM.thaw one + mapM_ ((>> MM.add acc acc acc' M.Up) + . flip (MM.divi acc' acc') M.Up) [1..n-1] + MM.unsafeFreeze acc hunk ./test/Demo.hs 57 +main = do print $ s1 1000 1000 + print $ s6 1000 1000 + print $ e1 1000 1000 + print $ e2 1000 1000 Context: [Updated .cabal file with new modules. Ales Bizjak **20090921163946 Ignore-this: 1566f54bc23e14fbfcb3a63959988037 ] [Added mutable interface. Ales Bizjak **20090921163657 Ignore-this: 240b8d26f20edd782d6a7d33101cf098 ] [Refactored modules MPFR, Up, Down, Near, Zero. Ales Bizjak **20090921123348 Ignore-this: b68013722084a304bd2c57c9e3b30dcf ] [Removed Data.Number.MPFR.Base module. It didn't have any purpose. Ales Bizjak **20090921102549 Ignore-this: e2b1de674f4c34d5b05c5feba39e0709 ] [Updated module descriptions in headers. Ales Bizjak **20090918144024 Ignore-this: 9f219cd3bd77ae6155a7d87cc7d4dbc7 ] [Precision is now a newtype wrapped Word with custom Num, Real, Integral. Ales Bizjak **20090908202006 Ignore-this: 16a26b268d526410e0a51d45fc9eb474 This should prevent bugs like the one with fromIntegral allocating insane amounts of memory because of the fact that fromIntegra (-1) :: Word is maxBound :: Word -1 ] [Removed reference to dyadic, bumped version number, spelling mistakes. Ales Bizjak **20090907134331 Ignore-this: bb200e10fbbd85f9c60b00f15e054baf ] [TAG 0.2.2 Ales Bizjak **20090902121251 Ignore-this: 8ee41ce742854deb6747bd556d51e23a ] Patch bundle hash: a2e554a4b66274c41602d9c7a6d238826f0b7a9c