#! /usr/bin/env python # def hypercube01_monomial_integral ( m, e ): #*****************************************************************************80 # ## HYPERCUBE01_MONOMIAL_INTEGRAL: integrals over the unit hypercube in M dimensions. # # Discussion: # # The integration region is # # 0 <= X(1:M) <= 1, # # The monomial is F(X) = product ( 1 <= I <= M ) X(I)^E(I). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 June 2015 # # Author: # # John Burkardt # # Reference: # # Philip Davis, Philip Rabinowitz, # Methods of Numerical Integration, # Second Edition, # Academic Press, 1984, page 263. # # Parameters: # # Input, integer M, the spatial dimension. # # Input, integer E(M), the exponents. Each exponent must be nonnegative. # # Output, real INTEGRAL, the integral. # from sys import exit for i in range ( 0, m ): if ( e[i] < 0 ): print ( '' ) print ( 'HYPERCUBE01_MONOMIAL_INTEGRAL - Fatal error!' ) print ( ' All exponents must be nonnegative.' ) error ( 'HYPERCUBE01_MONOMIAL_INTEGRAL - Fatal error!' ) integral = 1.0 for i in range ( 0, m ): integral = integral / float ( e[i] + 1 ) return integral def hypercube01_monomial_integral_test ( ): #*****************************************************************************80 # ## HYPERCUBE01_MONOMIAL_INTEGRAL_TEST tests HYPERCUBE01_MONOMIAL_INTEGRAL. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 June 2015 # # Author: # # John Burkardt # import numpy as np import platform from hypercube01_sample import hypercube01_sample from hypercube01_volume import hypercube01_volume from i4vec_uniform_ab import i4vec_uniform_ab from monomial_value import monomial_value m = 3 n = 4192 test_num = 20 print ( '' ) print ( 'HYPERCUBE01_MONOMIAL_INTEGRAL_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' HYPERCUBE01_MONOMIAL_INTEGRAL returns the integral of a monomial' ) print ( ' over the interior of the unit hypercube in 3D.' ) print ( ' Compare with a Monte Carlo estimate.' ) print ( '' ) print ( ' Using M = %d' % ( m ) ) # # Get sample points. # seed = 123456789 x, seed = hypercube01_sample ( m, 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, 4, seed ) value = monomial_value ( m, n, e, x ) result = hypercube01_volume ( m ) * np.sum ( value ) / float ( n ) exact = hypercube01_monomial_integral ( m, e ) error = abs ( result - exact ) for i in range ( 0, m ): print ( ' %2d' % ( e[i] ) ), print ( ' %14.6g %14.6g %10.2g' % ( result, exact, error ) ) # # Terminate. # print ( '' ) print ( 'HYPERCUBE01_MONOMIAL_INTEGRAL_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) hypercube01_monomial_integral_test ( ) timestamp ( )