Bryan O'Sullivan avatar Bryan O'Sullivan committed f8507d5 Merge

Merge

Comments (0)

Files changed (3)

System/Random/MWC.hs

 type instance Unsigned Int16 = Word16
 type instance Unsigned Int32 = Word32
 type instance Unsigned Int64 = Word64
-type instance Unsigned Int   = Word
 
 type instance Unsigned Word8  = Word8
 type instance Unsigned Word16 = Word16
 type instance Unsigned Word32 = Word32
 type instance Unsigned Word64 = Word64
-type instance Unsigned Word   = Word
+
+-- This is workaround for bug #25.
+--
+-- GHC-7.6 have bug (#8072) which results in calculation of wrongs
+-- number of buckets in function `uniformRange'. Consequently unformR
+-- generates values in wrong range.
+--
+-- Bug only affects 32-bit systems and Int/Word data types. Word32
+-- works just fine. So we set Word32 as unsigned counterpart for Int
+-- and Word on 32-bit systems. It's done only for GHC-7.6 because
+-- other versions are unaffected by the bug and we expect that GHC may
+-- optimise code which uses Word better.
+#if (WORD_SIZE_IN_BITS < 64) && (__GLASGOW_HASKELL__ == 706)
+type instance Unsigned Int   = Word32
+type instance Unsigned Word  = Word32
+#else
+type instance Unsigned Int   = Word
+type instance Unsigned Word  = Word
+#endif
+
 
 -- Subtract two numbers under assumption that x>=y and store result in
 -- unsigned data type of same size

System/Random/MWC/CondensedTable.hs

 -- the case, this algorithm will construct a table for some
 -- distribution that may bear no resemblance to what you intended.
 tableFromProbabilities
-    :: (Vector v (a,Word32), Vector v (a,Double), Vector v a, Vector v Word32, Show a)
+    :: (Vector v (a,Word32), Vector v (a,Double), Vector v a, Vector v Word32)
        => v (a, Double) -> CondensedTable v a
 {-# INLINE tableFromProbabilities #-}
 tableFromProbabilities v
 -- probilities. Non-positive weights are discarded, and those
 -- remaining are normalized to 1.
 tableFromWeights
-    :: (Vector v (a,Word32), Vector v (a,Double), Vector v a, Vector v Word32, Show a)
+    :: (Vector v (a,Word32), Vector v (a,Double), Vector v a, Vector v Word32)
        => v (a, Double) -> CondensedTable v a
 {-# INLINE tableFromWeights #-}
 tableFromWeights = tableFromProbabilities . normalize . G.filter ((> 0) . snd)

System/Random/MWC/Distributions.hs

       normal
     , standard
     , exponential
+    , truncatedExp
     , gamma
     , chiSquare
     , geometric0
   return $! - log x / beta
 
 
+-- | Generate truncated exponentially distributed random variate.
+truncatedExp :: PrimMonad m
+             => Double            -- ^ Scale parameter
+             -> (Double,Double)   -- ^ Range to which distribution is
+                                  --   truncated. Values may be negative.
+             -> Gen (PrimState m) -- ^ Generator.
+             -> m Double
+{-# INLINE truncatedExp #-}
+truncatedExp beta (a,b) gen = do
+  -- We shift a to 0 and then generate distribution truncated to [0,b-a]
+  -- It's easier
+  let delta = b - a
+  p <- uniform gen
+  return $! a - log ( (1 - p) + p*exp(-beta*delta)) / beta
+
 -- | Random variate generator for gamma distribution.
 gamma :: PrimMonad m
       => Double                 -- ^ Shape parameter
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.