Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- class MonteCarlo:
- def __init__(self, function, iterations):
- """
- Initializes the MonteCarlo integrator.
- :param function: Inject the function that the user whishes to integrate.
- :param iterations: Give the number of iterations the MonteCarlo integrator is allowed to perform.
- :return self: Return the object (the __init__() method does this by default)
- """
- self.__setFunction(function)
- self.__setIterations(iterations)
- def setBoundaries(self, lowerBoundary, upperBoundary):
- """
- Set the integration boundaries and return the object.
- :param lowerBoundary: Define the lower boundary.
- :param upperBoundary: Define the upper boundary.
- :return: Return the object such that we can run other methods on this instance.
- """
- self.lowerBoundary = lowerBoundary
- self.upperBoundary = upperBoundary
- return self
- def setBoundaryCondition(self, requirement):
- """
- Set a boundary condition by injecting a anonymous (lambda) function.
- :param requirement: A anonymous function which implements a boundary condition, let's call it a requirement.
- :return: Return the object such that we can run other methods on this instance.
- """
- self.requirement = requirement
- return self
- def integrate(self):
- """
- Perform the actual integration once all the required parameters are defined/injected in this instance.
- :return: Return the integration sum divided by the number of iterations.
- """
- sum = 0
- for i in range(self.iterations):
- randomNumberForX = self.__generatePseudoRandomNumber(self.lowerBoundary, self.upperBoundary)
- randomNumberForY = self.__generatePseudoRandomNumber(self.lowerBoundary, self.upperBoundary)
- if self.__validateBoundaryCondition(randomNumberForX, randomNumberForY):
- continue
- sum += self.function(randomNumberForX, randomNumberForY)
- return (sum / self.iterations)
- def __validateBoundaryCondition(self, x, y) -> bool:
- """
- For a randomly generated x and y value, check whether they fulfill the condition or requirement.
- :param x: x value.
- :param y: y value.
- :return: Return a boolean that indicated whether the condition or requirement has been fulfilled.
- """
- return self.requirement(x, y)
- def __setFunction(self, function):
- """
- Set the integrand.
- :param function: Anonymous function that represents the integrand.
- :return: None.
- """
- self.function = function
- def __setIterations(self, iterations):
- """
- Define the number of iterations that the Monte Carlo integrator is allowed to perform.
- :param iterations: Set the number of iterations.
- :return: None
- """
- self.iterations = iterations
- def __generatePseudoRandomNumber(self, lowerBoundary, upperBoundary):
- """
- Generate a pseudorandom number required for the Monte Carlo technique.
- :param lowerBoundary: The lower boundary for the random number.
- :param upperBoundary: The upper boundary for the random number.
- :return: Random float.
- """
- return random.uniform(lowerBoundary, upperBoundary)
- """
- Create an instance of the MonteCarlo class and define the integrand and the number of iterations.
- """
- integrator = MonteCarlo(
- lambda x, y: 1,
- 1000000
- )
- """
- After we created an instance, saved in the var 'integrator', define the boundaries and inject an anonymous function
- that represents the integration boundary condition. If that is all done, call the integrate() method to perform
- the actual integration.
- """
- result = integrator.setBoundaries(0, 1).setBoundaryCondition(
- lambda x, y: False if math.pow(x, 2) + math.pow(y, 2) <= 1 else True
- ).integrate();
- """
- Simply print the result and multiply it by four since we only performed the integration for one quadrant of the
- unit circle. Therefore its boundary [0,1]. The latter should return an approximation of the value of pi.
- """
- print(result)
- print(result * 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement