Writing some test cases

Now, we will finally get to see how to use the CUDA Math API to write some test cases for our class by way of the math_function parameter. These will be fairly straightforward if you have any experience with the C/C++ standard math library. Again, these functions are overloaded so that we don't have to change the names of anything when we switch between single and double precision.

We've already seen one example, namely y = sin(x). Let's try something a little more ambitious:

We will integrate this function from a=11.733 to b=18.472, and then check the output of our Monte Carlo integrator against the known value of this integral from another source. Here, Mathematica indicates that the value of this definite integral is 8.9999, so we will check against that. 

Now, let's think of how to represent this function: here, log refers to the base-e logarithm (also known as ln), and this is just log(x) in the Math API. We already set up a macro for squaring, so we can represent sin2(x) as _P2(sin(x)). We can now represent the entire function with y = log(x)*_P2(sin(x)).

Let's use the following equation, integrating from a=.9 to b=4:

Remembering that _R is the macro we set up for a reciprocal, we can write the function with the Math API like so:

'y = _R( 1 + sinh(2*x)*_P2(log(x)) )' 

Before we move on, let's note that Mathematica tells us that the value of this definite integral is .584977.

Let's check on one more function. Let's be a little ambitious and say that it's this:

We can represent this as 'y = (cosh(x)*sin(x))/ sqrt( pow(x,3) + _P2(sin(x)))'; naturally sqrt is the square root in the denominator, and pow allows us to take a value of arbitrary power. Of course, sin(x) is sin(x) and cosh(x) is cosh(x). We integrate this from a=1.85 to b=4.81; Mathematica tells us that the true value of this integral is -3.34553.

We are now ready to check some test cases and verify that our Monte Carlo integral is working! Let's iterate over a list, whose first value is a string indicating the function (using the Math API), the second value indicates the lower bound of integration, the third indicates the upper bound of integration, and the last value indicates the expected value that was calculated with Mathematica:

if __name__ == '__main__':

integral_tests = [('y =log(x)*_P2(sin(x))', 11.733 , 18.472, 8.9999), ('y = _R( 1 + sinh(2*x)*_P2(log(x)) )', .9, 4, .584977), ('y = (cosh(x)*sin(x))/ sqrt( pow(x,3) + _P2(sin(x)))', 1.85, 4.81, -3.34553) ]

We can now iterate over this list and see how well our algorithm works compared to Mathematica:

for f, lo, hi, expected in integral_tests:
mci = MonteCarloIntegrator(math_function=f, precision='d', lo=lo, hi=hi)
print 'The Monte Carlo numerical integration of the function f: x -> %s from x = %s to x = %s is : %s ' % (f, lo, hi, mci.definite_integral())
print 'where the expected value is : %s ' % expected

Let's run this right now:

This is also available as the monte_carlo_integrator.py file under the Chapter08 directory in this book's repository.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.223.160