#! /usr/bin/env python # def cube01_monomial_integral ( e ): #*****************************************************************************80 # #% CUBE01_MONOMIAL_INTEGRAL: integrals over the unit cube in 3D. # # Discussion: # # The integration region is # # 0 <= X <= 1, # 0 <= Y <= 1, # 0 <= Z <= 1. # # The monomial is F(X,Y,Z) = X^E(1) * Y^E(2) * Z^E(3). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 18 January 2014 # # Author: # # John Burkardt # # Reference: # # Philip Davis, Philip Rabinowitz, # Methods of Numerical Integration, # Second Edition, # Academic Press, 1984, page 263. # # Parameters: # # Input, integer E(3), the exponents. Each exponent must be nonnegative. # # Output, real INTEGRAL, the integral. # from sys import exit m = 3 if ( e[0] < 0 or e[1] < 0 or e[2] < 0 ): print ( '' ) print ( 'CUBE01_MONOMIAL_INTEGRAL - Fatal error!' ) print ( ' All exponents must be nonnegative.' ) exit ( 'CUBE01_MONOMIAL_INTEGRAL - Fatal error!' ) integral = 1.0 for i in range ( 0, m ): integral = integral / float ( e[i] + 1 ) return integral def cube01_monomial_integral_test ( ): #*****************************************************************************80 # #% CUBE01_MONOMIAL_INTEGRAL_TEST tests CUBE01_MONOMIAL_INTEGRAL. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 21 June 2015 # # Author: # # John Burkardt # import numpy as np import platform from cube01_sample import cube01_sample from cube01_volume import cube01_volume from i4vec_uniform_ab import i4vec_uniform_ab from monomial_value import monomial_value m = 3 n = 4192 test_num = 20 print ( '' ) print ( 'CUBE01_MONOMIAL_INTEGRAL_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' CUBE01_MONOMIAL_INTEGRAL computes the integral of a monomial' ) print ( ' within the interior of the unit cube in 3D.' ) print ( ' Compare with a Monte Carlo estimate.' ) # # Get sample points. # seed = 123456789 x, seed = cube01_sample ( n, seed ) print ( '' ) print ( ' Number of sample points used is %d' % ( n ) ) # # Randomly choose exponents. # print ( '' ) print ( ' Ex Ey Ez MC-Estimate Exact Error' ) print ( '' ) for test in range ( 0, test_num ): e, seed = i4vec_uniform_ab ( m, 0, 7, seed ) value = monomial_value ( m, n, e, x ) result = cube01_volume ( ) * np.sum ( value ) / float ( n ) exact = cube01_monomial_integral ( e ) error = abs ( result - exact ) print ( ' %2d %2d %2d %14.6g %14.6g %10.2g' \ % ( e[0], e[1], e[2], result, exact, error ) ) # # Terminate. # print ( '' ) print ( 'CUBE01_MONOMIAL_INTEGRAL_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) cube01_monomial_integral_test ( ) timestamp ( )