#! /usr/bin/env python3 # def disk01_positive_monte_carlo_test ( ): #*****************************************************************************80 # ## disk01_positive_monte_carlo_test() tests disk01_positive_monte_carlo(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 23 January 2020 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'disk01_positive_monte_carlo_test():' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' Test disk01_positive_monte_carlo().' ) disk01_positive_area_test ( ) disk01_positive_monomial_integral_test ( ) disk01_positive_sample_test ( ) # # Terminate. # print ( '' ) print ( 'disk01_positive_monte_carlo_test():' ) print ( ' Normal end of execution.' ) return def disk01_positive_area ( ): #*****************************************************************************80 # ## disk01_positive_area() returns the area of the unit positive_disk. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 May 2016 # # Author: # # John Burkardt # # Output: # # real AREA, the area. # import numpy as np value = np.pi / 4.0 return value def disk01_positive_area_test ( ) : #*****************************************************************************80 # ## disk01_positive_area_test() tests disk01_positive_area(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 May 2016 # # Author: # # John Burkardt # print ( '' ) print ( 'disk01_positive_area_test():' ) print ( ' disk01_positive_area returns the area of the unit positive disk.' ) value = disk01_positive_area ( ) print ( '' ) print ( ' disk01_positive_area() = %g' % ( value ) ) return def disk01_positive_monomial_integral ( e ): #*****************************************************************************80 # ## disk01_positive_monomial_integral(): monomial integrals in unit positive disk. # # Discussion: # # The integration region is # # X^2 + Y^2 <= 1. # 0 <= X, 0 <= Y. # # The monomial is F(X,Y) = X^E(1) * Y^E(2). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 May 2016 # # Author: # # John Burkardt # # Input: # # integer E(2), the exponents of X and Y in the # monomial. Each exponent must be nonnegative. # # Output: # # real INTEGRAL, the integral. # from scipy.special import gamma f1 = gamma ( ( e[0] + 3 ) / 2.0 ) f2 = gamma ( ( e[1] + 1 ) / 2.0 ) f3 = gamma ( ( e[0] + e[1] + 4 ) / 2.0 ) integral = f1 * f2 / f3 / 2.0 / ( 1.0 + e[0] ) return integral def disk01_positive_monomial_integral_test ( ): #*****************************************************************************80 # ## disk01_positive_monomial_integral_test() tests disk01_positive_monomial_integral(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 May 2016 # # Author: # # John Burkardt # from numpy.random import default_rng import numpy as np rng = default_rng ( ) m = 2 n = 4192 test_num = 20 print ( '' ) print ( 'disk01_positive_monomial_integral_test():' ) print ( ' disk01_positive_monomial_integral computes monomial integrals' ) print ( ' over the interior of the unit disk in 2D.' ) print ( ' Compare with a Monte Carlo value.' ) # # Get sample points. # x = disk01_positive_sample ( n ) print ( '' ) print ( ' Number of sample points used is %d' % ( n ) ) # # Randomly choose X,Y exponents. # print ( '' ) print ( ' We will restrict this test to randomly chosen even exponents.' ) print ( '' ) print ( ' Ex Ey MC-Estimate Exact Error' ) print ( '' ) for test in range ( 0, test_num ): e = rng.integers ( low = 0, high = 4, size = m, endpoint = True ) value = monomial_value ( e, x ) result = disk01_positive_area ( ) * np.sum ( value ) / float ( n ) exact = disk01_positive_monomial_integral ( e ) error = abs ( result - exact ) print ( ' %2d %2d %14.6g %14.6g %10.2g' \ % ( e[0], e[1], result, exact, error ) ) return def disk01_positive_sample ( n ): #*****************************************************************************80 # ## disk01_positive_sample() uniformly samples the unit positive disk. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 August 2023 # # Author: # # John Burkardt # # Input: # # integer N, the number of points. # # Output: # # real x(n,2): the points. # from numpy.random import default_rng import numpy as np rng = default_rng ( ) x = np.zeros ( [ n, 2 ] ) for i in range ( 0, n ): # # Fill a vector with normally distributed values. # v = rng.standard_normal ( size = 2 ) # # Compute the length of the vector. # norm = np.sqrt ( v[0] ** 2 + v[1] ** 2 ) # # Normalize the vector. # v[0] = v[0] / norm v[1] = v[1] / norm # # Now compute a value to map the point ON the disk INTO the disk. # r = rng.random ( size = 1 ) x[i,0] = np.sqrt ( r ) * np.abs ( v[0] ) x[i,1] = np.sqrt ( r ) * np.abs ( v[1] ) return x def disk01_positive_sample_test ( ): #*****************************************************************************80 # ## disk01_positive_sample_test() tests disk01_positive_sample(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 August 2023 # # Author: # # John Burkardt # import platform print ( '' ) print ( 'disk01_positive_sample_test():' ) print ( ' disk01_positive_sample() samples the unit positive disk.' ) n = 10 x = disk01_positive_sample ( n ) print ( '' ) print ( ' Sample points in the unit positive disk.' ) print ( x ) return def monomial_value ( e, x ): #*****************************************************************************80 # ## monomial_value() evaluates a monomial. # # Discussion: # # This routine evaluates a monomial of the form # # product ( 1 <= i <= m ) x(i)^e(i) # # The combination 0.0^0, if encountered, is treated as 1.0. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 August 2023 # # Author: # # John Burkardt # # Input: # # integer E(D), the exponents. # # real X(N,D), the point coordinates. # # Output: # # real V(N), the monomial values. # import numpy as np n, d = x.shape v = np.ones ( n ) for j in range ( 0, d ): if ( 0 != e[j] ): for i in range ( 0, n ): v[i] = v[i] * x[i,j] ** e[j] return v def r8mat_print ( m, n, a, title ): #*****************************************************************************80 # ## r8mat_print() prints an R8MAT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # # Input: # # integer M, the number of rows in A. # # integer N, the number of columns in A. # # real A(M,N), the matrix. # # string TITLE, a title. # r8mat_print_some ( m, n, a, 0, 0, m - 1, n - 1, title ) return def r8mat_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ): #*****************************************************************************80 # ## r8mat_print_some() prints out a portion of an R8MAT. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 February 2015 # # Author: # # John Burkardt # # Input: # # integer M, N, the number of rows and columns of the matrix. # # real A(M,N), an M by N matrix to be printed. # # integer ILO, JLO, the first row and column to print. # # integer IHI, JHI, the last row and column to print. # # string TITLE, a title. # incx = 5 print ( '' ) print ( title ) if ( m <= 0 or n <= 0 ): print ( '' ) print ( ' (None)' ) return for j2lo in range ( max ( jlo, 0 ), min ( jhi + 1, n ), incx ): j2hi = j2lo + incx - 1 j2hi = min ( j2hi, n ) j2hi = min ( j2hi, jhi ) print ( '' ) print ( ' Col: ', end = '' ) for j in range ( j2lo, j2hi + 1 ): print ( '%7d ' % ( j ), end = '' ) print ( '' ) print ( ' Row' ) i2lo = max ( ilo, 0 ) i2hi = min ( ihi, m ) for i in range ( i2lo, i2hi + 1 ): print ( '%7d :' % ( i ), end = '' ) for j in range ( j2lo, j2hi + 1 ): print ( '%12g ' % ( a[i,j] ), end = '' ) print ( '' ) return def r8mat_transpose_print ( m, n, a, title ): #*****************************************************************************80 # ## r8mat_transpose_print() prints an R8MAT, transposed. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # # Input: # # integer M, the number of rows in A. # # integer N, the number of columns in A. # # real A(M,N), the matrix. # # string TITLE, a title. # r8mat_transpose_print_some ( m, n, a, 0, 0, m - 1, n - 1, title ) return def r8mat_transpose_print_test ( ): #*****************************************************************************80 # ## r8mat_transpose_print_test() tests r8mat_transpose_print(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 October 2014 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'r8mat_transpose_print_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' r8mat_transpose_print prints an R8MAT.' ) m = 4 n = 3 v = np.array ( [ \ [ 11.0, 12.0, 13.0 ], [ 21.0, 22.0, 23.0 ], [ 31.0, 32.0, 33.0 ], [ 41.0, 42.0, 43.0 ] ], dtype = np.float64 ) r8mat_transpose_print ( m, n, v, ' Here is an R8MAT, transposed:' ) return def r8mat_transpose_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ): #*****************************************************************************80 # ## r8mat_transpose_print_some() prints a portion of an R8MAT, transposed. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 13 November 2014 # # Author: # # John Burkardt # # Input: # # integer M, N, the number of rows and columns of the matrix. # # real A(M,N), an M by N matrix to be printed. # # integer ILO, JLO, the first row and column to print. # # integer IHI, JHI, the last row and column to print. # # string TITLE, a title. # incx = 5 print ( '' ) print ( title ) if ( m <= 0 or n <= 0 ): print ( '' ) print ( ' (None)' ) return for i2lo in range ( max ( ilo, 0 ), min ( ihi, m - 1 ), incx ): i2hi = i2lo + incx - 1 i2hi = min ( i2hi, m - 1 ) i2hi = min ( i2hi, ihi ) print ( '' ) print ( ' Row: ', end = '' ) for i in range ( i2lo, i2hi + 1 ): print ( '%7d ' % ( i ), end = '' ) print ( '' ) print ( ' Col' ) j2lo = max ( jlo, 0 ) j2hi = min ( jhi, n - 1 ) for j in range ( j2lo, j2hi + 1 ): print ( '%7d :' % ( j ), end = '' ) for i in range ( i2lo, i2hi + 1 ): print ( '%12g ' % ( a[i,j] ), end = '' ) print ( '' ) return def r8mat_transpose_print_some_test ( ): #*****************************************************************************80 # ## r8mat_transpose_print_some_test() tests r8mat_transpose_print_some(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 October 2014 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'r8mat_transpose_print_some_test' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' r8mat_transpose_print_some prints some of an R8MAT, transposed.' ) m = 4 n = 6 v = np.array ( [ \ [ 11.0, 12.0, 13.0, 14.0, 15.0, 16.0 ], [ 21.0, 22.0, 23.0, 24.0, 25.0, 26.0 ], [ 31.0, 32.0, 33.0, 34.0, 35.0, 36.0 ], [ 41.0, 42.0, 43.0, 44.0, 45.0, 46.0 ] ], dtype = np.float64 ) r8mat_transpose_print_some ( m, n, v, 0, 3, 2, 5, ' R8MAT, rows 0:2, cols 3:5:' ) return def r8vec_print ( n, a, title ): #*****************************************************************************80 # ## r8vec_print() prints an R8VEC. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 31 August 2014 # # Author: # # John Burkardt # # Input: # # integer N, the dimension of the vector. # # real A(N), the vector to be printed. # # string TITLE, a title. # print ( '' ) print ( title ) print ( '' ) for i in range ( 0, n ): print ( '%6d: %12g' % ( i, a[i] ) ) def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return None if ( __name__ == '__main__' ): timestamp ( ) disk01_positive_monte_carlo_test ( ) timestamp ( )