module Data.Random.Distribution.Uniform
( Uniform(..)
, uniform
, uniformT
, StdUniform(..)
, stdUniform
, stdUniformT
, stdUniformPos
, stdUniformPosT
, integralUniform
, realFloatUniform
, floatUniform
, doubleUniform
, fixedUniform
, enumUniform
, boundedStdUniform
, boundedEnumStdUniform
, realFloatStdUniform
, fixedStdUniform
, floatStdUniform
, doubleStdUniform
, boundedStdUniformCDF
, realStdUniformCDF
, realUniformCDF
, enumUniformCDF
) where
import Data.Random.Internal.TH
import Data.Random.Internal.Words
import Data.Random.Internal.Fixed
import Data.Random.Source
import Data.Random.Distribution
import Data.Random.RVar
import Data.Fixed
import Data.Word
import Data.Int
import Control.Monad.Loops
integralUniform :: (Integral a) => a -> a -> RVarT m a
integralUniform !x !y = if x < y then integralUniform' x y else integralUniform' y x
integralUniform' :: (Integral a) => a -> a -> RVarT m a
integralUniform' !l !u
| nReject == 0 = fmap shift prim
| otherwise = fmap shift loop
where
m = 1 + toInteger u toInteger l
(bytes, nPossible) = bytesNeeded m
nReject = nPossible `mod` m
!prim = getRandomNByteInteger bytes
!shift = \(!z) -> l + (fromInteger $! (z `mod` m))
loop = do
z <- prim
if z < nReject
then loop
else return z
integralUniformCDF :: (Integral a, Fractional b) => a -> a -> a -> b
integralUniformCDF a b x
| b < a = integralUniformCDF b a x
| x < a = 0
| x > b = 1
| otherwise = (fromIntegral x fromIntegral a) / (fromIntegral b fromIntegral a)
bytesNeeded :: Integer -> (Int, Integer)
bytesNeeded x = head (dropWhile ((<= x).snd) powersOf256)
powersOf256 :: [(Int, Integer)]
powersOf256 = zip [0..] (iterate (256 *) 1)
boundedStdUniform :: (Distribution Uniform a, Bounded a) => RVar a
boundedStdUniform = uniform minBound maxBound
boundedStdUniformCDF :: (CDF Uniform a, Bounded a) => a -> Double
boundedStdUniformCDF = cdf (Uniform minBound maxBound)
boundedEnumStdUniform :: (Enum a, Bounded a) => RVarT m a
boundedEnumStdUniform = enumUniform minBound maxBound
boundedEnumStdUniformCDF :: (Enum a, Bounded a, Ord a) => a -> Double
boundedEnumStdUniformCDF = enumUniformCDF minBound maxBound
floatStdUniform :: RVarT m Float
floatStdUniform = do
x <- getRandomWord32
return (word32ToFloat x)
doubleStdUniform :: RVarT m Double
doubleStdUniform = getRandomDouble
realFloatStdUniform :: RealFloat a => RVarT m a
realFloatStdUniform = do
let (b, e) = decodeFloat one
x <- uniformT 0 (b1)
if x == 0
then return (0 `asTypeOf` one)
else return (encodeFloat x e)
where one = 1
fixedStdUniform :: HasResolution r => RVarT m (Fixed r)
fixedStdUniform = x
where
res = resolutionOf2 x
x = do
u <- uniformT 0 (res)
return (mkFixed u)
realStdUniformCDF :: Real a => a -> Double
realStdUniformCDF x
| x <= 0 = 0
| x >= 1 = 1
| otherwise = realToFrac x
realStdUniformPDF :: Real a => a -> Double
realStdUniformPDF x
| x <= 0 = 0
| x >= 1 = 0
| otherwise = 1
lerp :: Num a => a -> a -> a -> a
lerp x y a = (1a)*x + a*y
floatUniform :: Float -> Float -> RVarT m Float
floatUniform 0 1 = floatStdUniform
floatUniform a b = do
x <- floatStdUniform
return (lerp a b x)
doubleUniform :: Double -> Double -> RVarT m Double
doubleUniform 0 1 = doubleStdUniform
doubleUniform a b = do
x <- doubleStdUniform
return (lerp a b x)
realFloatUniform :: RealFloat a => a -> a -> RVarT m a
realFloatUniform 0 1 = realFloatStdUniform
realFloatUniform a b = do
x <- realFloatStdUniform
return (lerp a b x)
fixedUniform :: HasResolution r => Fixed r -> Fixed r -> RVarT m (Fixed r)
fixedUniform a b = do
u <- integralUniform (unMkFixed a) (unMkFixed b)
return (mkFixed u)
realUniformCDF :: RealFrac a => a -> a -> a -> Double
realUniformCDF a b x
| b < a = realUniformCDF b a x
| x <= a = 0
| x >= b = 1
| otherwise = realToFrac ((xa) / (ba))
enumUniform :: Enum a => a -> a -> RVarT m a
enumUniform a b = do
x <- integralUniform (fromEnum a) (fromEnum b)
return (toEnum x)
enumUniformCDF :: (Enum a, Ord a) => a -> a -> a -> Double
enumUniformCDF a b x
| b < a = enumUniformCDF b a x
| x <= a = 0
| x >= b = 1
| otherwise = (e2f x e2f a) / (e2f b e2f a)
where e2f = fromIntegral . fromEnum
uniform :: Distribution Uniform a => a -> a -> RVar a
uniform a b = rvar (Uniform a b)
uniformT :: Distribution Uniform a => a -> a -> RVarT m a
uniformT a b = rvarT (Uniform a b)
stdUniform :: (Distribution StdUniform a) => RVar a
stdUniform = rvar StdUniform
stdUniformT :: (Distribution StdUniform a) => RVarT m a
stdUniformT = rvarT StdUniform
stdUniformNonneg :: (Distribution StdUniform a, Num a, Eq a) => RVarT m a
stdUniformNonneg = fmap abs stdUniformT
stdUniformPos :: (Distribution StdUniform a, Num a, Eq a) => RVar a
stdUniformPos = stdUniformPosT
stdUniformPosT :: (Distribution StdUniform a, Num a, Eq a) => RVarT m a
stdUniformPosT = iterateUntil (/= 0) stdUniformNonneg
data Uniform t =
Uniform !t !t
data StdUniform t = StdUniform
$( replicateInstances ''Int integralTypes [d|
instance Distribution Uniform Int where rvarT (Uniform a b) = integralUniform a b
instance CDF Uniform Int where cdf (Uniform a b) = integralUniformCDF a b
|])
instance Distribution StdUniform Word8 where rvarT _ = getRandomWord8
instance Distribution StdUniform Word16 where rvarT _ = getRandomWord16
instance Distribution StdUniform Word32 where rvarT _ = getRandomWord32
instance Distribution StdUniform Word64 where rvarT _ = getRandomWord64
instance Distribution StdUniform Int8 where rvarT _ = fromIntegral `fmap` getRandomWord8
instance Distribution StdUniform Int16 where rvarT _ = fromIntegral `fmap` getRandomWord16
instance Distribution StdUniform Int32 where rvarT _ = fromIntegral `fmap` getRandomWord32
instance Distribution StdUniform Int64 where rvarT _ = fromIntegral `fmap` getRandomWord64
instance Distribution StdUniform Int where
rvar _ =
$(if toInteger (maxBound :: Int) > toInteger (maxBound :: Int32)
then [|fromIntegral `fmap` getRandomWord64 :: RVar Int|]
else [|fromIntegral `fmap` getRandomWord32 :: RVar Int|])
instance Distribution StdUniform Word where
rvar _ =
$(if toInteger (maxBound :: Word) > toInteger (maxBound :: Word32)
then [|fromIntegral `fmap` getRandomWord64 :: RVar Word|]
else [|fromIntegral `fmap` getRandomWord32 :: RVar Word|])
instance CDF StdUniform Word8 where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Word16 where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Word32 where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Word64 where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Word where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Int8 where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Int16 where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Int32 where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Int64 where cdf _ = integralUniformCDF minBound maxBound
instance CDF StdUniform Int where cdf _ = integralUniformCDF minBound maxBound
instance Distribution Uniform Float where rvarT (Uniform a b) = floatUniform a b
instance Distribution Uniform Double where rvarT (Uniform a b) = doubleUniform a b
instance CDF Uniform Float where cdf (Uniform a b) = realUniformCDF a b
instance CDF Uniform Double where cdf (Uniform a b) = realUniformCDF a b
instance Distribution StdUniform Float where rvarT _ = floatStdUniform
instance Distribution StdUniform Double where rvarT _ = getRandomDouble
instance CDF StdUniform Float where cdf _ = realStdUniformCDF
instance CDF StdUniform Double where cdf _ = realStdUniformCDF
instance PDF StdUniform Float where pdf _ = realStdUniformPDF
instance PDF StdUniform Double where pdf _ = realStdUniformPDF
instance HasResolution r =>
Distribution Uniform (Fixed r) where rvarT (Uniform a b) = fixedUniform a b
instance HasResolution r =>
CDF Uniform (Fixed r) where cdf (Uniform a b) = realUniformCDF a b
instance HasResolution r =>
Distribution StdUniform (Fixed r) where rvarT ~StdUniform = fixedStdUniform
instance HasResolution r =>
CDF StdUniform (Fixed r) where cdf ~StdUniform = realStdUniformCDF
instance Distribution Uniform () where rvarT (Uniform _ _) = return ()
instance CDF Uniform () where cdf (Uniform _ _) = return 1
$( replicateInstances ''Char [''Char, ''Bool, ''Ordering] [d|
instance Distribution Uniform Char where rvarT (Uniform a b) = enumUniform a b
instance CDF Uniform Char where cdf (Uniform a b) = enumUniformCDF a b
|])
instance Distribution StdUniform () where rvarT ~StdUniform = return ()
instance CDF StdUniform () where cdf ~StdUniform = return 1
instance Distribution StdUniform Bool where rvarT ~StdUniform = fmap even (getRandomWord8)
instance CDF StdUniform Bool where cdf ~StdUniform = boundedEnumStdUniformCDF
instance Distribution StdUniform Char where rvarT ~StdUniform = boundedEnumStdUniform
instance CDF StdUniform Char where cdf ~StdUniform = boundedEnumStdUniformCDF
instance Distribution StdUniform Ordering where rvarT ~StdUniform = boundedEnumStdUniform
instance CDF StdUniform Ordering where cdf ~StdUniform = boundedEnumStdUniformCDF