module Simulation.Aivika.Lattice.Internal.Lattice
(LIOLattice(..),
lattice,
newRandomLattice,
newRandomLatticeWithProb) where
import Control.Monad
import Control.Monad.Trans
import Data.Array
import qualified System.Random.MWC as MWC
data LIOLattice =
LIOLattice { lioParentMemberIndex :: Int -> Int -> Int,
lioSize :: Int
}
newRandomLatticeWithProb :: Double -> Int -> IO LIOLattice
newRandomLatticeWithProb p m =
do g <- MWC.withSystemRandom (return :: MWC.GenIO -> IO MWC.GenIO)
xss0 <- forM [0 .. m] $ \i ->
do xs0 <- forM [0 .. i] $ \k ->
if k == 0
then return k
else if k == i
then return (k 1)
else do x <- MWC.uniform g :: IO Double
if x > p
then return (k 1)
else return k
return $ listArray (0, i) xs0
let xss = listArray (0, m) xss0
return LIOLattice { lioParentMemberIndex = \i k -> (xss ! i) ! k,
lioSize = m
}
newRandomLattice :: Int -> IO LIOLattice
newRandomLattice = newRandomLatticeWithProb 0.5
lattice :: Int
-> (Int -> Int -> Int)
-> LIOLattice
lattice m f = LIOLattice f m