Jump to content

Monte Carlo methods: Difference between revisions

m
Line 669:
 
<lang Futhark>
include futlib.numeric
 
default(f32)
 
fun dirvcts(): [2][30]inti32 =
[
[
Line 682 ⟶ 684:
 
 
fun grayCode(x: inti32): inti32 = (x >> 1) ^ x
 
----------------------------------------
--- Sobol Generator
----------------------------------------
fun testBit(n: inti32, ind: inti32): bool =
let t = (1 << ind) in (n & t) == t
 
fun xorInds(n: inti32) (dir_vs: [num_bits]inti32): inti32 =
let reldv_vals = zipWith (fn \ dv i =->
if testBit(grayCode n,i)
then dv else 0)
Line 697 ⟶ 699:
in reduce (^) 0 reldv_vals
 
fun sobolIndI (dir_vs: [m][num_bits]inti32, n: inti32): [m]inti32 =
map (xorInds n) dir_vs
 
fun sobolIndR(dir_vs: [m][num_bits]inti32) (n: inti32 ): [m]f32 =
let divisor = 2.0 ** f32(num_bits)
let arri = sobolIndI( dir_vs, n )
in map (fn \ (x: inti32): f32 =-> f32(x) / divisor) arri
 
fun main(n: inti32): f32 =
let rand_nums = map (sobolIndR (dirvcts())) (iota n)
let dists = map (fn \xy =->
let (x,y) = (xy[0],xy[1]) in sqrt32F32.sqrt(x*x + y*y))
rand_nums
 
let bs = map (fn \d =-> if d <= 1.0f32 then 1 else 0) dists
 
let inside = reduce (+) 0 bs
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.