packages feed

flower 0.1.1 → 0.1.2

raw patch · 3 files changed

+93/−1 lines, 3 files

Files

flower.cabal view
@@ -1,5 +1,5 @@ Name:           flower-Version:        0.1.1+Version:        0.1.2 License:        GPL  Author:         Ketil Malde@@ -22,5 +22,6 @@  -- Data-files:     README Executable:     flower Main-Is:        Flower.hs+Other-Modules:	Print, Statistics Hs-Source-Dirs: src Ghc-Options:    
+ src/Print.hs view
@@ -0,0 +1,47 @@+module Print +    (+     Builder, toLazyByteString, mconcat, fromByteString, char+    , putInt, putInt2, putDate, putTime+    ) where ++import Data.Binary.Builder+import Data.Monoid+import Data.ByteString.Char8 (ByteString, pack)+import qualified Data.ByteString.Lazy.Char8 as LB+import Data.Array.Unboxed+import Data.Char (ord)++-- import Test.QuickCheck++char = singleton . fromIntegral . ord++putInt :: Int -> Builder+putInt y = if y < 0 then char '-' `append` putInt' (negate y)+           else putInt' y+    where putInt' x =  case x `divMod` 1000 of+             (0,r) -> fromByteString (ints!r)+             (a,r) -> putInt a `append` putInt3 r++-- zero padded ints++putInt2 :: Int -> Builder+putInt2 x = fromByteString (int2s!x)++putInt3 :: Int -> Builder+putInt3 x = fromByteString (int3s!x)++ints, int2s, int3s :: Array Int ByteString+ints  = listArray (0,999) [pack (show i) | i <- [0..999::Int]]+int2s = listArray (0,99) (map (pack . ('0':) . show) [0..9::Int]++[pack (show i) | i <- [10..99::Int]])+int3s = listArray (0,999) (map (pack . ('0':) . ('0':) . show) [0..9::Int] ++ map (pack . ('0':) . show ) [10..99::Int] ++ map (pack . show) [100..999::Int])++putDate :: Int -> Int -> Int -> Builder+putDate y m d = mconcat [putInt y, dash, putInt2 m, dash, putInt2 d]+    where dash = char '-'++putTime :: Int -> Int -> Int -> Builder+putTime h m s = mconcat [putInt2 h, col, putInt2 m, col, putInt2 s]+    where col = char ':'+++prop_int i = toLazyByteString (putInt i) == LB.pack (show i)
+ src/Statistics.hs view
@@ -0,0 +1,44 @@+-- | Yet another simple module for implementing statistics stuff.++module Statistics (normal, normals, stdnormal, stdnormals, module System.Random) where+import System.Random++stdnormal :: StdGen -> (Double,StdGen)+stdnormal = normal 0 1++stdnormals :: StdGen -> [Double]+stdnormals = normals 0 1++normal :: Double -> Double -> StdGen -> (Double,StdGen)+normal mu sigma = \g -> let (x,g') = randomR (0,1) g in (invcumnorm mu sigma x,g') ++normals :: Double -> Double -> StdGen -> [Double]+normals mu sigma = \g -> let (x,g') = normal mu sigma g in x : normals mu sigma g'++lognormal :: Double -> Double -> StdGen -> Double+lognormal mu sigma = undefined++-- support++invcumnorm mu sigma z = mu + search (-limit*sigma) (limit*sigma)+    where search a b = let c = (a+b)/2+                           cn = cumnorm 0 sigma c+                       in if abs (z - cn) < 10*epsilon || abs (a-b) < epsilon then c+                            else if cn > z then search a c+                                 else search c b++cumstdnorm :: Double -> Double+cumstdnorm x = 0.5*(1+erf (x/sqrt 2))++cumnorm :: Double -> Double -> Double -> Double+cumnorm mu sigma x = 0.5*(1+erf((x-mu)/(sigma*sqrt 2)))++-- taylor expansion, see wikipedia "error function".  Tested within the range (-limit..limit)+erf :: Double -> Double+erf x | x>limit         = 1+      | x< negate limit = 0+      | otherwise   = (2/sqrt pi)*sum (reverse $ takeWhile ((>=epsilon).abs) [ ((-1)**n*x**(2*n+1)) / (fac n*(2*n+1)) | n <- [0..]])++epsilon = 0.0000000001+limit = 4.4 :: Double+fac x = product [2..x]