#include "oo/Function.h" #include "oo/MonteCarloIntegrator.h" #include "oo/RandRandomGenerator.h" namespace integrators { namespace oo { // MonteCarloIntegrator methods MonteCarloIntegrator::MonteCarloIntegrator () { function_m = 0; random_m = 0; } void MonteCarloIntegrator::setFunction (functions::Function * function_to_integrate) { function_m = function_to_integrate; } void MonteCarloIntegrator:: setRandomGenerator (randomgen::RandomGenerator * randomGen) { random_m = randomGen; } double MonteCarloIntegrator::integrate (double lowBound, double upBound, int count) { if (!random_m) random_m = new randomgen::oo::RandRandomGenerator (); double sum = 0.0; for (int i = 0; i < count; i++) { double x = lowBound + (upBound - lowBound) * random_m->getRandomNumber (); sum = sum + function_m->evaluate (x); } return (upBound - lowBound) * sum / count; } } }