#! /usr/bin/env python # def hyperball01_sample ( m, n, seed ): #*****************************************************************************80 # ## HYPERBALL01_SAMPLE uniformly samples the unit hyperball in M dimensions. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 June 2015 # # Author: # # John Burkardt # # Reference: # # Russell Cheng, # Random Variate Generation, # in Handbook of Simulation, # edited by Jerry Banks, # Wiley, 1998, pages 168. # # Reuven Rubinstein, # Monte Carlo Optimization, Simulation, and Sensitivity # of Queueing Networks, # Krieger, 1992, # ISBN: 0894647644, # LC: QA298.R79. # # Parameters: # # Input, integer M, the spatial dimension. # # Input, integer N, the number of points. # # Input/output, integer SEED, a seed for the random # number generator. # # Output, real X(M,N), the points. # import numpy as np from r8_uniform_01 import r8_uniform_01 from r8vec_norm import r8vec_norm from r8vec_normal_01 import r8vec_normal_01 x = np.zeros ( [ m, n ] ) exponent = 1.0 / float ( m ) for j in range ( 0, n ): # # Fill a vector with normally distributed values. # v, seed = r8vec_normal_01 ( m, seed ) # # Compute the length of the vector. # norm = r8vec_norm ( m, v ) # # Normalize the vector. # for i in range ( 0, m ): v[i] = v[i] / norm # # Now compute a value to map the point ON the sphere INTO the sphere. # r, seed = r8_uniform_01 ( seed ) for i in range ( 0, m ): x[i,j] = r ** exponent * v[i] return x, seed def hyperball01_sample_test ( ): #*****************************************************************************80 # ## HYPERBALL01_SAMPLE_TEST tests HYPERBALL01_SAMPLE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 June 2015 # # Author: # # John Burkardt # import platform from r8mat_transpose_print import r8mat_transpose_print print ( '' ) print ( 'HYPERBALL01_SAMPLE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' HYPERBALL01_SAMPLE samples the unit hyperball.' ) m = 3 n = 10 seed = 123456789 x, seed = hyperball01_sample ( m, n, seed ) r8mat_transpose_print ( m, n, x, ' Sample points in the unit hyperball.' ) # # Terminate. # print ( '' ) print ( 'HYPERBALL01_SAMPLE_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) hyperball01_sample_test ( ) timestamp ( )