1 | initial version |
One doesn't need any symbolic variable to get a numerical integral :
sage: reset()
sage: %time monte_carlo_integral(lambda u,v:u*v, [0, 0], [3, 3], 1000000)
CPU times: user 119 ms, sys: 0 ns, total: 119 ms
Wall time: 119 ms
(20.25454841467989, 0.017863719435520488)
But do you really need a numerical integration ? Compare :
sage: reset()
sage: var("l_a, u_a, l_b, u_b")
(l_a, u_a, l_b, u_b)
sage: f(x,y)=x*y
sage: %time f(x,y).integrate(y, l_b, u_b).integrate(x, l_a, u_a).subs({l_a:0, l_b:0,u_a:3, u_b:3}) .n()
CPU times: user 21.9 ms, sys: 99 µs, total: 22 ms
Wall time: 21.1 ms
20.2500000000000
and the original result (81/4) is exact (if the symbolic ntegral can be found...)