Monte Carlo methods: Difference between revisions
Content added Content deleted
(→JS ES6) |
|||
Line 669: | Line 669: | ||
<lang Futhark> |
<lang Futhark> |
||
include futlib.numeric |
|||
default(f32) |
default(f32) |
||
fun dirvcts(): [2][30] |
fun dirvcts(): [2][30]i32 = |
||
[ |
[ |
||
[ |
[ |
||
Line 682: | Line 684: | ||
fun grayCode(x: |
fun grayCode(x: i32): i32 = (x >> 1) ^ x |
||
---------------------------------------- |
---------------------------------------- |
||
--- Sobol Generator |
--- Sobol Generator |
||
---------------------------------------- |
---------------------------------------- |
||
fun testBit(n: |
fun testBit(n: i32, ind: i32): bool = |
||
let t = (1 << ind) in (n & t) == t |
let t = (1 << ind) in (n & t) == t |
||
fun xorInds(n: |
fun xorInds(n: i32) (dir_vs: [num_bits]i32): i32 = |
||
let reldv_vals = zipWith ( |
let reldv_vals = zipWith (\ dv i -> |
||
if testBit(grayCode n,i) |
if testBit(grayCode n,i) |
||
then dv else 0) |
then dv else 0) |
||
Line 697: | Line 699: | ||
in reduce (^) 0 reldv_vals |
in reduce (^) 0 reldv_vals |
||
fun sobolIndI (dir_vs: [m][num_bits] |
fun sobolIndI (dir_vs: [m][num_bits]i32, n: i32): [m]i32 = |
||
map (xorInds n) dir_vs |
map (xorInds n) dir_vs |
||
fun sobolIndR(dir_vs: [m][num_bits] |
fun sobolIndR(dir_vs: [m][num_bits]i32) (n: i32 ): [m]f32 = |
||
let divisor = 2.0 ** f32(num_bits) |
let divisor = 2.0 ** f32(num_bits) |
||
let arri = sobolIndI( dir_vs, n ) |
let arri = sobolIndI( dir_vs, n ) |
||
in map ( |
in map (\ (x: i32): f32 -> f32(x) / divisor) arri |
||
fun main(n: |
fun main(n: i32): f32 = |
||
let rand_nums = map (sobolIndR (dirvcts())) (iota n) |
let rand_nums = map (sobolIndR (dirvcts())) (iota n) |
||
let dists = map ( |
let dists = map (\xy -> |
||
let (x,y) = (xy[0],xy[1]) in |
let (x,y) = (xy[0],xy[1]) in F32.sqrt(x*x + y*y)) |
||
rand_nums |
rand_nums |
||
let bs = map ( |
let bs = map (\d -> if d <= 1.0f32 then 1 else 0) dists |
||
let inside = reduce (+) 0 bs |
let inside = reduce (+) 0 bs |