#! /usr/bin/env python # def hypersphere01_sample ( m, n, seed ): #*****************************************************************************80 # ## HYPERSPHERE01_SAMPLE uniformly samples the surface of the unit hypersphere. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 June 2015 # # Author: # # John Burkardt # # 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 r8vec_norm import r8vec_norm from r8vec_normal_01 import r8vec_normal_01 x = np.zeros ( [ m, n ] ) 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 ): x[i,j] = v[i] / norm return x, seed def hypersphere01_sample_test ( ): #*****************************************************************************80 # ## HYPERSPHERE01_SAMPLE_TEST tests HYPERSPHERE01_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 ( 'HYPERSPHERE01_SAMPLE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' HYPERSPHERE01_SAMPLE samples the unit hypersphere' ) print ( ' in M dimensions.' ) m = 3 n = 10 seed = 123456789 x, seed = hypersphere01_sample ( m, n, seed ) r8mat_transpose_print ( m, n, x, ' Sample points on the unit hypersphere.' ) # # Terminate. # print ( '' ) print ( 'HYPERSPHERE01_SAMPLE_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) hypersphere01_sample_test ( ) timestamp ( )