{-# LANGUAGE
MultiParamTypeClasses,
FlexibleContexts, FlexibleInstances,
UndecidableInstances, GADTs
#-}
module Data.Random.Distribution.Simplex
( StdSimplex(..)
, stdSimplex
, stdSimplexT
, fractionalStdSimplex
) where
import Control.Monad
import Data.List
import Data.Random.RVar
import Data.Random.Distribution
import Data.Random.Distribution.Uniform
newtype StdSimplex as =
StdSimplex Int
deriving (Eq, Show)
instance (Ord a, Fractional a, Distribution StdUniform a) => Distribution StdSimplex [a] where
rvar (StdSimplex k) = fractionalStdSimplex k
stdSimplex :: Distribution StdSimplex [a] => Int -> RVar [a]
stdSimplex k = rvar (StdSimplex k)
stdSimplexT :: Distribution StdSimplex [a] => Int -> RVarT m [a]
stdSimplexT k = rvarT (StdSimplex k)
fractionalStdSimplex :: (Ord a, Fractional a, Distribution StdUniform a) => Int -> RVar [a]
fractionalStdSimplex k = do us <- replicateM k stdUniform
let us' = sort us ++ [1]
return $ zipWith (-) us' (0 : us')