module Simulation.Aivika.Lattice.Internal.Lattice
(LIOLattice(..),
lattice,
newRandomLattice) 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
}
newRandomLattice :: Int -> IO LIOLattice
newRandomLattice 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 <= 0.5
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
}
lattice :: Int
-> (Int -> Int -> Int)
-> LIOLattice
lattice m f = LIOLattice f m