vendredi 27 juillet 2018

Haskell: Efficiency iterative map with added noise

I am wondering how to improve the time performance of the white noise addition to the logistic map? The noise is only allowed to be added after calculating the values (as it is an iterative map.)

 module Generating

import System.Random (Random,randomR,random,mkStdGen,StdGen)
import Data.Random.Normal (mkNormals,normal)
import qualified Data.Vector.Unboxed as U 
import Control.Monad.State

genR :: (Random a, Fractional a) => Int -> (a, StdGen)
genR x = randomR (0,1.0) (mkStdGen x)


new ::Double-> Double ->Int -> (Double,Int) -> U.Vector (Double,Int)
new skal r n = U.unfoldrN n go
  where  
   go (x0,g0)  = let  !eins= (1.0-x0)
                      !x=x0 `seq` eins `seq` r*x0*eins
                      !g=g0+1
                      !noise= skal*(fst $ genR g)
             in Just ((x0+noise,g0),(x,g))

fs :: (t, t1) -> t
fs (x,y)=x

first :: U.Vector (Double,Int)->U.Vector Double
first  =U.map (\(x,y)->x)  

As you can see, I actually only want the first value of the tuple, but the generator needs to be updated.

Any suggestions? Maybe State Monads?




Aucun commentaire:

Enregistrer un commentaire