#! /usr/bin/env python # def hyperball01_monomial_integral ( m, e ): #*****************************************************************************80 # ## HYPERBALL01_MONOMIAL_INTEGRAL: integrals in unit hyperball in M dimensions. # # Discussion: # # The integration region is # # sum ( 1 <= I <= M ) X(I)^2 <= 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: # # Gerald Folland, # How to Integrate a Polynomial Over a Sphere, # American Mathematical Monthly, # Volume 108, Number 5, May 2001, pages 446-448. # # Parameters: # # Input, integer M, the spatial dimension. # # Input, integer E(M), the exponents. Each exponent must be nonnegative. # # Output, real INTEGRAL, the integral. # from r8_gamma import r8_gamma from sys import exit for i in range ( 0, m ): if ( e[i] < 0 ): print ( '' ) print ( 'HYPERBALL01_MONOMIAL_INTEGRAL - Fatal error!' ) print ( ' All exponents must be nonnegative.' ) exit ( 'HYPERBALL01_MONOMIAL_INTEGRAL - Fatal error!' ) # # Integrate over the surface. # for i in range ( 0, m ): if ( ( e[i] % 2 ) == 1 ): integral = 0.0 return integral integral = 2.0 for i in range ( 0, m ): integral = integral * r8_gamma ( 0.5 * float ( e[i] + 1 ) ) s = 0 for i in range ( 0, m ): s = s + e[i] + 1 integral = integral / r8_gamma ( 0.5 * float ( s ) ) # # The surface integral is now adjusted to give the volume integral. # r = 1.0 integral = integral * r ** s / float ( s ) return integral def hyperball01_monomial_integral_test ( ): #*****************************************************************************80 # ## HYPERBALL01_MONOMIAL_INTEGRAL_TEST tests HYPERBALL01_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 hyperball01_sample import hyperball01_sample from hyperball01_volume import hyperball01_volume from i4vec_uniform_ab import i4vec_uniform_ab from monomial_value import monomial_value m = 3 n = 4192 test_num = 20 print ( '' ) print ( 'HYPERBALL01_MONOMIAL_INTEGRAL_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' HYPERBALL01_MONOMIAL_INTEGRAL computes the integral of a monomial' ) print ( ' over the interior of the unit hyperball in M dimensions.' ) print ( ' Compare with a Monte Carlo estimate.' ) print ( '' ) print ( ' Spatial dimension M = %d' % ( m ) ) # # Get sample points. # seed = 123456789 x, seed = hyperball01_sample ( m, n, seed ) print ( '' ) print ( ' Number of sample points used is %d' % ( n ) ) # # Randomly choose exponents between 0 and 8. # print ( '' ) print ( ' If any exponent is odd, the integral is zero.' ) print ( ' We will restrict this test to randomly chosen even 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 ) for i in range ( 0, m ): e[i] = e[i] * 2 value = monomial_value ( m, n, e, x ) result = hyperball01_volume ( m ) * np.sum ( value ) / float ( n ) exact = hyperball01_monomial_integral ( m, e ) error = abs ( result - exact ) for i in range ( 0, m ): print ( ' %2d' % ( e[i] ), end = '' ) print ( ' %14.6g %14.6g %10.2g' % ( result, exact, error ) ) # # Terminate. # print ( '' ) print ( 'HYPERBALL01_MONOMIAL_INTEGRAL_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) hyperball01_monomial_integral_test ( ) timestamp ( )