UsageΒΆ
Start by importing MonteCarlo Integral VKI integrator class.
from montecarlo import MonteCarloIntegrator
Then, we define the equation of the function \(f(x,y)\) to be integrated. This function defines a surface. In this example, we keep it equal to 1 for simplicity.
def f(x,y):
return 1
Following, we define the integration region using a function in implicit form. Let us integrate a circle of radius 1:
def g_circle(x,y):
return 1 if ((x-1.5)**2+(y-3.5)**2) < 1 else -1 # circle definition
Next, we instantiate a MonteCarloIntegrator object. We define the rectangle around in which the circle is inscribed.
We set parallel to False. If set to True, it will use the number of processors available in multiprocessing.cpu_count().
In this case, the seed is set to 8 for repeatability, but normally it defaults to False.
integrator = MonteCarloIntegrator(rectangle=(0, 3, 2, 5), parallel=False, seed=8)
Once the integrator object is created, we can link the functions for the surface and the circumference:
integrator.g = g_rectangle
integrator.f = f
Now, we can generate the random MonteCarlo points:
integrator.generate_points(n=6000)
Before performing the integration, we may want to visualize the region to ensure we are plotting the correct areas:
fig, ax = integrator.plot()
fig.savefig('regions.png')
Finally, we integrate:
integrator.compute_integral()