Class reference¶
- class montecarlo.MonteCarloIntegrator(rectangle: list, parallel: bool = False, seed: bool | int = False)[source]¶
Class to compute a 2D integral using a MonteCarlo method
- __init__(rectangle: list, parallel: bool = False, seed: bool | int = False)[source]¶
- Parameters:
- rectangle: tuple
with 4 elements (x0, x1, y0, y1) defining the integration rectangle R
- parallel: bool
If true, it will use multiprocessing to evaluate each point. Otherwise, single process.
- seed: float or False
Seed for the random number generator (repeatability). If False, random.
- static _compute_sample(point: tuple, f: Callable, g: Callable) float | None[source]¶
Computes the value of the function f in the point x, y. If the point is inside of g, the function returns the value of the function in f. Otherwise, the function returns None.
- Parameters:
- point: tuple
x, y pair of values
- f: function
callable function defining the function to be integrated
- g: function
callable function defining the region
- Returns:
- compute_integral()[source]¶
Compute integral using the MonteCarlo Method. Each point is computed by the static method
_compute_sample(). Then, it applies the theory of the mean-value theorem to compute the area. The return value is stored within the class, and also returned.- Returns:
- Value of the integral.
- property f¶
Function of x,y that is to be integrated. Defined as F(x,y) = 0. If 1 is provided, the integration provides the area.
- Parameters:
- User defined function f
- property g¶
Function that defines the integration region. It should be defined implicitly as G(x,y) = 0.
- Returns:
- User defined function g
- plot(all_random_points: bool = False)[source]¶
Function to plot the process. In general, it will plot the contour plot filled of f, the domain defined by g with a shadowed area and border, and the montecarlo points. If any of these is not available, it will simply skip it. Uses two auxiliary functions to actually plot.
- Parameters:
- all_random_points: bool
If True, it will plot all the points. This may be slow if n is large.
- Returns:
- fig: figure handles
- ax: axes handles